package cn.iocoder.yudao.module.pms.dal.mysql.inspect;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.pms.controller.admin.inspect.order.vo.IotInspectDeviceVo;
import cn.iocoder.yudao.module.pms.controller.admin.inspect.order.vo.IotInspectOrderDetailPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.inspect.order.vo.IotInspectOrderPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.vo.IotDeviceSimple;
import cn.iocoder.yudao.module.pms.dal.dataobject.inspect.IotInspectOrderDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.inspect.IotInspectOrderDetailDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotcountdata.IotCountDataDO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * 巡检工单巡检明细 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface IotInspectOrderDetailMapper extends BaseMapperX<IotInspectOrderDetailDO> {

    default PageResult<IotInspectOrderDetailDO> selectPage(IotInspectOrderDetailPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<IotInspectOrderDetailDO>()
                .eqIfPresent(IotInspectOrderDetailDO::getOrderId, reqVO.getOrderId())
                .eqIfPresent(IotInspectOrderDetailDO::getDeviceId, reqVO.getDeviceId())
                .eqIfPresent(IotInspectOrderDetailDO::getItemId, reqVO.getItemId())
                .eqIfPresent(IotInspectOrderDetailDO::getIfNormal, reqVO.getIfNormal())
                .eqIfPresent(IotInspectOrderDetailDO::getDescription, reqVO.getDescription())
                .eqIfPresent(IotInspectOrderDetailDO::getPicUrl, reqVO.getPicUrl())
                .eqIfPresent(IotInspectOrderDetailDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(IotInspectOrderDetailDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(IotInspectOrderDetailDO::getDeptId, reqVO.getDeptId())
                .orderByDesc(IotInspectOrderDetailDO::getId));
    }

    default List<IotInspectOrderDetailDO> selectByTimeAndOrderIdAndDeviceId(IotInspectOrderDetailPageReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<IotInspectOrderDetailDO>().betweenIfPresent(IotInspectOrderDetailDO::getCreateTime, reqVO.getCreateTime()).eqIfPresent(IotInspectOrderDetailDO::getDeviceId, reqVO.getDeviceId())
                .eqIfPresent(IotInspectOrderDetailDO::getOrderId, reqVO.getOrderId())
        );
    }

    default List<IotInspectOrderDetailDO> selectListStat(IotCountDataDO vo, Set<Long> ids) {
        return selectList(new LambdaQueryWrapperX<IotInspectOrderDetailDO>()
                .betweenIfPresent(IotInspectOrderDetailDO::getCreateTime, vo.getCreateTime())
//                .inIfPresent(IotInspectOrderDetailDO::getOrderId, orderIds)
                        .inIfPresent(IotInspectOrderDetailDO::getDeptId, ids)
                .orderByDesc(IotInspectOrderDetailDO::getId)
        );
    }

    default Long selectDeviceException(IotCountDataDO vo, Set<Long> ids) {
        return selectCount(
                new QueryWrapper<IotInspectOrderDetailDO>()
                        .select("DISTINCT device_id")
                        // 核心：仅当 createTime 非空且是有效数组（长度≥2）时，才生效 between 条件
                        .between(
                                "create_time",
                                // 起始时间：createTime 非空且数组长度≥2 时取第一个元素，否则 null
                                (vo.getCreateTime() != null && vo.getCreateTime().length >= 2) ? vo.getCreateTime()[0] : null,
                                // 结束时间：同上，否则 null
                                (vo.getCreateTime() != null && vo.getCreateTime().length >= 2) ? vo.getCreateTime()[1] : null
                        )
                        // in 条件：ids 非空且非空集合时生效，否则失效（原生特性）
                        .in(ids != null && !ids.isEmpty(), "dept_id", ids)
                        .eq("if_normal", false)
        );
    }

    default Long selectExceptionReport(IotCountDataDO vo, Set<Long> ids) {
        return selectCount(new LambdaQueryWrapperX<IotInspectOrderDetailDO>()
                        .betweenIfPresent(IotInspectOrderDetailDO::getCreateTime, vo.getCreateTime())
//                .inIfPresent(IotInspectOrderDetailDO::getOrderId, orderIds)
                        .inIfPresent(IotInspectOrderDetailDO::getDeptId, ids)
                .eq(IotInspectOrderDetailDO::getIfNormal, false)
                        .orderByDesc(IotInspectOrderDetailDO::getId)
        );
    }

    default PageResult<IotInspectOrderDetailDO> selectItemPage(IotInspectOrderDetailPageReqVO reqVO, Set<Long> ids, Set<Long> deviceIds, Set<Long> orderIds) {
        return selectPage(reqVO, new LambdaQueryWrapperX<IotInspectOrderDetailDO>()
                .eq(IotInspectOrderDetailDO::getIfNormal, reqVO.getIfNormal())
                .inIfPresent(IotInspectOrderDetailDO::getDeptId, ids)
                .inIfPresent(IotInspectOrderDetailDO::getDeviceId, deviceIds)
                .inIfPresent(IotInspectOrderDetailDO::getOrderId, orderIds)
                .betweenIfPresent(IotInspectOrderDetailDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(IotInspectOrderDetailDO::getId));
    }

    default PageResult<IotInspectOrderDetailDO> selectItemPageNull(IotInspectOrderDetailPageReqVO reqVO, Set<Long> ids, Set<Long> deviceIds, Set<Long> orderIds) {
        return selectPage(reqVO, new LambdaQueryWrapperX<IotInspectOrderDetailDO>()
                .inIfPresent(IotInspectOrderDetailDO::getDeptId, ids)
                .betweenIfPresent(IotInspectOrderDetailDO::getCreateTime, reqVO.getCreateTime())
                .inIfPresent(IotInspectOrderDetailDO::getDeviceId, deviceIds)
                .inIfPresent(IotInspectOrderDetailDO::getOrderId, orderIds)
                .isNull(IotInspectOrderDetailDO::getIfNormal)
                .orderByDesc(IotInspectOrderDetailDO::getId));
    }
    IPage<IotInspectDeviceVo> getDeviceStatusWei(IPage<IotInspectOrderDetailPageReqVO> page, @Param("reqVO")  IotInspectOrderDetailPageReqVO reqVO, @Param("deptIds") Collection<Long> deptIds);
    IPage<IotInspectDeviceVo> getDeviceStatusYi(IPage<IotInspectOrderDetailPageReqVO> page, @Param("reqVO")  IotInspectOrderDetailPageReqVO reqVO, @Param("deptIds") Collection<Long> deptIds);
    IPage<IotInspectDeviceVo> getDeviceStatusIgnore(IPage<IotInspectOrderDetailPageReqVO> page, @Param("reqVO")  IotInspectOrderDetailPageReqVO reqVO, @Param("deptIds") Collection<Long> deptIds);
    //异常设备信息
    IPage<IotDeviceSimple> getExceptionDevicePage(IPage<IotInspectOrderPageReqVO> page, @Param("reqVO") IotInspectOrderPageReqVO reqVO);
}
