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

import cn.hutool.core.util.ObjUtil;
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.iotmainworkorder.vo.IotMainWorkOrderPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotmainworkorder.vo.IotMainWorkOrderRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.maintain.vo.IotMaintainPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.stat.vo.AllOrderResp;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotmainworkorder.IotMainWorkOrderDO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

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

/**
 * 保养工单 Mapper
 *
 * @author ruiqi
 */
@Mapper
public interface IotMainWorkOrderMapper extends BaseMapperX<IotMainWorkOrderDO> {

    default PageResult<IotMainWorkOrderDO> selectPage(IotMainWorkOrderPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<IotMainWorkOrderDO>()
                .eqIfPresent(IotMainWorkOrderDO::getPlanId, reqVO.getPlanId())
                .eqIfPresent(IotMainWorkOrderDO::getPlanSerialNumber, reqVO.getPlanSerialNumber())
                .eqIfPresent(IotMainWorkOrderDO::getDeptId, reqVO.getDeptId())
                .eqIfPresent(IotMainWorkOrderDO::getOrderNumber, reqVO.getOrderNumber())
                .likeIfPresent(IotMainWorkOrderDO::getName, reqVO.getName())
                .eqIfPresent(IotMainWorkOrderDO::getType, reqVO.getType())
                .eqIfPresent(IotMainWorkOrderDO::getResponsiblePerson, reqVO.getResponsiblePerson())
                .likeIfPresent(IotMainWorkOrderDO::getResponsiblePersonName, reqVO.getResponsiblePersonName())
                .eqIfPresent(IotMainWorkOrderDO::getCost, reqVO.getCost())
                .eqIfPresent(IotMainWorkOrderDO::getResult, reqVO.getResult())
                .eqIfPresent(IotMainWorkOrderDO::getOtherCost, reqVO.getOtherCost())
                .eqIfPresent(IotMainWorkOrderDO::getLaborCost, reqVO.getLaborCost())
                .eqIfPresent(IotMainWorkOrderDO::getOutsourcingFlag, reqVO.getOutsourcingFlag())
                .betweenIfPresent(IotMainWorkOrderDO::getActualStartTime, reqVO.getActualStartTime())
                .betweenIfPresent(IotMainWorkOrderDO::getActualEndTime, reqVO.getActualEndTime())
                .eqIfPresent(IotMainWorkOrderDO::getRemark, reqVO.getRemark())
                .eqIfPresent(IotMainWorkOrderDO::getStatus, reqVO.getStatus())
                .eqIfPresent(IotMainWorkOrderDO::getProcessInstanceId, reqVO.getProcessInstanceId())
                .eqIfPresent(IotMainWorkOrderDO::getAuditStatus, reqVO.getAuditStatus())
                .betweenIfPresent(IotMainWorkOrderDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(IotMainWorkOrderDO::getId));
    }

    default List<IotMainWorkOrderDO> selectList(IotMainWorkOrderPageReqVO reqVO) {
        LambdaQueryWrapperX<IotMainWorkOrderDO> queryWrapper = new LambdaQueryWrapperX<IotMainWorkOrderDO>()
                .inIfPresent(IotMainWorkOrderDO::getId, reqVO.getWorkOrderIds())
                .eqIfPresent(IotMainWorkOrderDO::getPlanId, reqVO.getPlanId())
                .eqIfPresent(IotMainWorkOrderDO::getPlanSerialNumber, reqVO.getPlanSerialNumber())
                .inIfPresent(IotMainWorkOrderDO::getDeptId, reqVO.getDeptIds())
                .eqIfPresent(IotMainWorkOrderDO::getOrderNumber, reqVO.getOrderNumber())
                .likeIfPresent(IotMainWorkOrderDO::getName, reqVO.getName())
                .eqIfPresent(IotMainWorkOrderDO::getType, reqVO.getType())
                .eqIfPresent(IotMainWorkOrderDO::getOrderGroupId, reqVO.getOrderGroupId())
                .eqIfPresent(IotMainWorkOrderDO::getResponsiblePerson, reqVO.getResponsiblePerson())
                .likeIfPresent(IotMainWorkOrderDO::getResponsiblePersonName, reqVO.getResponsiblePersonName())
                .eqIfPresent(IotMainWorkOrderDO::getCost, reqVO.getCost())
                .eqIfPresent(IotMainWorkOrderDO::getResult, reqVO.getResult())
                .eqIfPresent(IotMainWorkOrderDO::getOtherCost, reqVO.getOtherCost())
                .eqIfPresent(IotMainWorkOrderDO::getLaborCost, reqVO.getLaborCost())
                .eqIfPresent(IotMainWorkOrderDO::getOutsourcingFlag, reqVO.getOutsourcingFlag())
                .betweenIfPresent(IotMainWorkOrderDO::getActualStartTime, reqVO.getActualStartTime())
                .betweenIfPresent(IotMainWorkOrderDO::getActualEndTime, reqVO.getActualEndTime())
                .eqIfPresent(IotMainWorkOrderDO::getRemark, reqVO.getRemark())
                .eqIfPresent(IotMainWorkOrderDO::getStatus, reqVO.getStatus())
                .eqIfPresent(IotMainWorkOrderDO::getProcessInstanceId, reqVO.getProcessInstanceId())
                .eqIfPresent(IotMainWorkOrderDO::getAuditStatus, reqVO.getAuditStatus())
                .betweenIfPresent(IotMainWorkOrderDO::getCreateTime, reqVO.getCreateTime());

        // 关键修改：仅当year不为null时，才添加年份筛选条件
        if (ObjUtil.isNotEmpty(reqVO.getYear())) {
            queryWrapper.apply("YEAR(create_time) = {0}", reqVO.getYear());
        }
        return selectList(queryWrapper);
    }

    default List<IotMainWorkOrderDO> theMaxOne(){
        return selectList(new LambdaQueryWrapperX<IotMainWorkOrderDO>().orderByDesc(IotMainWorkOrderDO::getId));
    }
    default Long selectCountByTimeAndStatus(IotMainWorkOrderPageReqVO reqVO) {
        return selectCount(new LambdaQueryWrapperX<IotMainWorkOrderDO>()
                .eqIfPresent(IotMainWorkOrderDO::getResult, reqVO.getResult())
                .betweenIfPresent(IotMainWorkOrderDO::getCreateTime, reqVO.getCreateTime()));
    }
    default List<IotMainWorkOrderDO> selectListByCreateTime(IotMainWorkOrderPageReqVO reqVO, Set<Long> ids) {
        return selectList(new LambdaQueryWrapperX<IotMainWorkOrderDO>()
                .betweenIfPresent(IotMainWorkOrderDO::getCreateTime, reqVO.getCreateTime()).inIfPresent(IotMainWorkOrderDO::getDeptId, ids)
                .orderByAsc(IotMainWorkOrderDO::getCreateTime));
    }

    @Select("SELECT IFNULL(MAX(mwo.id), 0) maxId FROM rq_iot_main_work_order mwo")
    Long theMaxWorkOrderId();

    @MapKey("result")
    List<Map<String, Integer>> allWorkOrderCountByResult();

    IPage<IotMainWorkOrderRespVO> deviceOrderPage(IPage<IotMainWorkOrderRespVO> page, @Param("deviceId") Long deviceId);

    /**
     * 查询 保养工单 分页列表 按照工单保养项 最近保养时间 升序排列
     * @param page
     * @param reqVO
     * @param alarmWorkOrderIds
     * @return
     */
    IPage<IotMainWorkOrderRespVO> sortedMainWorkOrders(IPage<IotMainWorkOrderDO> page, @Param("reqVO") IotMainWorkOrderPageReqVO reqVO,
                                              @Param("alarmWorkOrderIds") Collection<Long> alarmWorkOrderIds,
                                                       @Param("deptIds") Collection<Long> deptIds, @Param("eligibleWorkOrderIds") Collection<Long> eligibleWorkOrderIds);


    List<AllOrderResp> selectStatusNumber(@Param("reqVO") IotMaintainPageReqVO reqVO);
}
