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

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.framework.tenant.core.aop.TenantIgnore;
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.stat.vo.ReportCost;
import cn.iocoder.yudao.module.pms.dal.dataobject.maintain.IotMaintainDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

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

/**
 * 维修工单 Mapper
 *
 * @author phli
 */
@Mapper
public interface IotMaintainMapper extends BaseMapperX<IotMaintainDO> {
    IPage<ReportCost> getCost(IPage<IotMaintainPageReqVO> page, @Param("reqVO")  IotMaintainPageReqVO reqVO);
    default PageResult<IotMaintainDO> selectPage(IotMaintainPageReqVO reqVO, Collection<Long> ids) {
        return selectPage(reqVO, new LambdaQueryWrapperX<IotMaintainDO>()
                .likeIfPresent(IotMaintainDO::getFailureCode, reqVO.getFailureCode())
                .likeIfPresent(IotMaintainDO::getFailureName, reqVO.getFailureName())
                .eqIfPresent(IotMaintainDO::getDeviceId, reqVO.getDeviceId())
                .eqIfPresent(IotMaintainDO::getStatus, reqVO.getStatus())
                .eqIfPresent(IotMaintainDO::getIfStop, reqVO.getIfStop())
                .betweenIfPresent(IotMaintainDO::getFailureTime, reqVO.getFailureTime())
                .eqIfPresent(IotMaintainDO::getFailureInfluence, reqVO.getFailureInfluence())
                .eqIfPresent(IotMaintainDO::getFailureSystem, reqVO.getFailureSystem())
                .eqIfPresent(IotMaintainDO::getDescription, reqVO.getDescription())
                .eqIfPresent(IotMaintainDO::getPic, reqVO.getPic())
                .eqIfPresent(IotMaintainDO::getSolution, reqVO.getSolution())
                .betweenIfPresent(IotMaintainDO::getMaintainStartTime, reqVO.getMaintainStartTime())
                .betweenIfPresent(IotMaintainDO::getMaintainEndTime, reqVO.getMaintainEndTime())
                .eqIfPresent(IotMaintainDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(IotMaintainDO::getCreateTime, reqVO.getCreateTime())
                .betweenIfPresent(IotMaintainDO::getExecuteDate, reqVO.getExecuteDate())
                .likeIfPresent(IotMaintainDO::getDeviceName, reqVO.getDeviceName())
                .eqIfPresent(IotMaintainDO::getProcessInstanceId, reqVO.getProcessInstanceId())
                .eqIfPresent(IotMaintainDO::getAuditStatus, reqVO.getAuditStatus())
                .inIfPresent(IotMaintainDO::getDeptId, ids)
                .orderByDesc(IotMaintainDO::getCreateTime));
    }

    default PageResult<IotMaintainDO> selectPageApp(IotMaintainPageReqVO reqVO, Collection<Long> ids) {

        LambdaQueryWrapper<IotMaintainDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(reqVO.getCommonParam()), IotMaintainDO::getDeviceCode, reqVO.getCommonParam())
                .or()
                .like(StringUtils.isNotBlank(reqVO.getCommonParam()), IotMaintainDO::getDeviceName, reqVO.getCommonParam())
                .or()
                .like(StringUtils.isNotBlank(reqVO.getCommonParam()), IotMaintainDO::getFailureCode, reqVO.getCommonParam())
                .or()
                .like(StringUtils.isNotBlank(reqVO.getCommonParam()), IotMaintainDO::getMaintainPersonName, reqVO.getCommonParam())
                .orderByDesc(IotMaintainDO::getId);
        ;
        return selectPage(reqVO, wrapper);
    }

    default Long selectCountByTimeAndStatus(IotMaintainPageReqVO reqVO) {
        return selectCount(new LambdaQueryWrapperX<IotMaintainDO>()
                .eqIfPresent(IotMaintainDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(IotMaintainDO::getCreateTime, reqVO.getCreateTime()));
    }

    default Long selectByDept(Set<Long> ids) {
        return selectCount(new LambdaQueryWrapperX<IotMaintainDO>()
                .inIfPresent(IotMaintainDO::getDeptId, ids));
    }

    default List<IotMaintainDO> selectByStatusAndAuditStatusAndDeviceId(String status, Integer auditStatus, List<Long> deviceIds) {
        return selectList(new LambdaQueryWrapperX<IotMaintainDO>()
                .eqIfPresent(IotMaintainDO::getStatus, status)
                .eqIfPresent(IotMaintainDO::getAuditStatus, auditStatus)
                .in(IotMaintainDO::getDeviceId, deviceIds));
    }
    IPage<OrderVo> selectNotice(IPage<OrderVo> page, @Param("reqVO")OrderVo reqVO);
    default List<IotMaintainDO> selectListByCreateTime(IotMaintainPageReqVO reqVO, Set<Long> ids) {
        return selectList(new LambdaQueryWrapperX<IotMaintainDO>()
                .betweenIfPresent(IotMaintainDO::getCreateTime, reqVO.getCreateTime()).inIfPresent(IotMaintainDO::getDeptId, ids));
    }

    List<AllOrderResp> selectStatusNumber(@Param("reqVO") IotMaintainPageReqVO reqVO);
    @TenantIgnore
    IPage<AllOrderResp> getAllOrder(IPage<IotMaintainPageReqVO> page, @Param("reqVO") IotMaintainPageReqVO reqVO);
    @TenantIgnore
    List<AllOrderResp> getAllOrderNoPage(@Param("reqVO") IotMaintainPageReqVO reqVO);

    BigDecimal getMaintainFee(@Param("reqVO") IotMaintainPageReqVO reqVO);
    BigDecimal getMaintainFeeIn(@Param("reqVO") IotMaintainPageReqVO reqVO);
}
