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.IotInspectOrderPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.inspect.order.vo.IotInspectOrderRespVO;
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.controller.admin.stat.vo.OrderVo;
import cn.iocoder.yudao.module.pms.controller.admin.vo.IotDevicePageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.vo.IotDeviceRespVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.inspect.IotInspectOrderDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotcountdata.IotCountDataDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.StringUtils;
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 IotInspectOrderMapper extends BaseMapperX<IotInspectOrderDO> {
    default List<IotInspectOrderDO> selectList(IotInspectOrderPageReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<IotInspectOrderDO>()
                .likeIfPresent(IotInspectOrderDO::getInspectOrderTitle, reqVO.getInspectOrderTitle()).orderByDesc(IotInspectOrderDO::getId));
    }
    default PageResult<IotInspectOrderDO> selectPage(IotInspectOrderPageReqVO reqVO, Collection<Long> ids, Collection<Long> idList) {
        return selectPage(reqVO, new LambdaQueryWrapperX<IotInspectOrderDO>()
                .likeIfPresent(IotInspectOrderDO::getInspectOrderTitle, reqVO.getInspectOrderTitle())
                .eqIfPresent(IotInspectOrderDO::getInspectOrderCode, reqVO.getInspectOrderCode())
                .eqIfPresent(IotInspectOrderDO::getStatus, reqVO.getStatus())
                .eqIfPresent(IotInspectOrderDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(IotInspectOrderDO::getCreateTime, reqVO.getCreateTime())
                .inIfPresent(IotInspectOrderDO::getDeptId, ids)
                .inIfPresent(IotInspectOrderDO::getId, idList)
                .eqIfPresent(IotInspectOrderDO::getDeviceIds, reqVO.getDeviceIds())
                .eqIfPresent(IotInspectOrderDO::getCharge, reqVO.getCharge())
                .orderByDesc(IotInspectOrderDO::getId));
    }

    default PageResult<IotInspectOrderDO> selectPageApp(IotInspectOrderPageReqVO reqVO, Collection<Long> ids, Collection<Long> idList) {
        LambdaQueryWrapper<IotInspectOrderDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(reqVO.getCommonParam()), IotInspectOrderDO::getChargeName, reqVO.getCommonParam())
                .or()
                .like(StringUtils.isNotBlank(reqVO.getCommonParam()), IotInspectOrderDO::getInspectOrderCode, reqVO.getCommonParam())
                .or()
                .like(StringUtils.isNotBlank(reqVO.getCommonParam()), IotInspectOrderDO::getInspectOrderTitle, reqVO.getCommonParam())
                .or()
                .like(StringUtils.isNotBlank(reqVO.getCommonParam()), IotInspectOrderDO::getPlanCode, reqVO.getCommonParam())
                .orderByDesc(IotInspectOrderDO::getStatus).orderByDesc(IotInspectOrderDO::getCreateTime);
        return selectPage(reqVO, wrapper);
    }

    default Long selectCountByTimeAndStatus(IotInspectOrderPageReqVO reqVO) {
        return selectCount(new LambdaQueryWrapperX<IotInspectOrderDO>()
                .eqIfPresent(IotInspectOrderDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(IotInspectOrderDO::getCreateTime, reqVO.getCreateTime()));
    }
    default List<IotInspectOrderDO> selectListByCreateTime(IotInspectOrderPageReqVO reqVO, Set<Long> ids) {
        return selectList(new LambdaQueryWrapperX<IotInspectOrderDO>()
                .betweenIfPresent(IotInspectOrderDO::getCreateTime, reqVO.getCreateTime()).inIfPresent(IotInspectOrderDO::getDeptId,ids)
                .orderByAsc(IotInspectOrderDO::getCreateTime));
    }

    default List<IotInspectOrderDO> selectListStat(IotCountDataDO vo, Set<Long> ids) {
        return selectList(new LambdaQueryWrapperX<IotInspectOrderDO>()
                .betweenIfPresent(IotInspectOrderDO::getCreateTime, vo.getCreateTime())
                        .inIfPresent(IotInspectOrderDO::getDeptId, ids)
                );
    }

    default Long selectCount(IotCountDataDO vo, Set<Long> ids, String status) {
        return selectCount(new LambdaQueryWrapperX<IotInspectOrderDO>()
                .betweenIfPresent(IotInspectOrderDO::getCreateTime, vo.getCreateTime())
                .eq(IotInspectOrderDO::getStatus, status)
                .inIfPresent(IotInspectOrderDO::getDeptId, ids)
        );
    }

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