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

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.pms.controller.admin.failure.vo.IotFailureReportPageReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.IotDeviceDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.failure.IotFailureReportDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.maintain.IotMaintainDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;

/**
 * 故障上报 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface IotFailureReportMapper extends BaseMapperX<IotFailureReportDO> {

    default PageResult<IotFailureReportDO> selectPage(IotFailureReportPageReqVO reqVO, Collection<Long> ids) {

        return selectPage(reqVO, new LambdaQueryWrapperX<IotFailureReportDO>()
                .likeIfPresent(IotFailureReportDO::getFailureCode, reqVO.getFailureCode())
                .likeIfPresent(IotFailureReportDO::getFailureName, reqVO.getFailureName())
                .eqIfPresent(IotFailureReportDO::getDeviceId, reqVO.getDeviceId())
                .eqIfPresent(IotFailureReportDO::getStatus, reqVO.getStatus())
                .eqIfPresent(IotFailureReportDO::getIfStop, reqVO.getIfStop())
                .betweenIfPresent(IotFailureReportDO::getFailureTime, reqVO.getFailureTime())
                .eqIfPresent(IotFailureReportDO::getFailureInfluence, reqVO.getFailureInfluence())
                .eqIfPresent(IotFailureReportDO::getFailureSystem, reqVO.getFailureSystem())
                .eqIfPresent(IotFailureReportDO::getDescription, reqVO.getDescription())
                .eqIfPresent(IotFailureReportDO::getPic, reqVO.getPic())
                .eqIfPresent(IotFailureReportDO::getIfDeal, reqVO.getIfDeal())
                .eqIfPresent(IotFailureReportDO::getNeedHelp, reqVO.getNeedHelp())
                .eqIfPresent(IotFailureReportDO::getSolution, reqVO.getSolution())
                .eqIfPresent(IotFailureReportDO::getRemark, reqVO.getRemark())
                .inIfPresent(IotFailureReportDO::getDeptId, ids)
                .betweenIfPresent(IotFailureReportDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(IotFailureReportDO::getId));
    }

    default PageResult<IotFailureReportDO> selectPageApp(IotFailureReportPageReqVO reqVO, Collection<Long> ids) {

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

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

    default List<IotFailureReportDO> selectByStatusAndDeviceId(List<String> statusList, List<Long> deviceIds) {
        return selectList(new LambdaQueryWrapperX<IotFailureReportDO>()
                .in(IotFailureReportDO::getStatus, statusList)
                .in(IotFailureReportDO::getDeviceId, deviceIds));
    }

    default Long selectCountByTimeAndStatus(IotFailureReportPageReqVO reqVO,Set<Long> ids) {
        return selectCount(new LambdaQueryWrapperX<IotFailureReportDO>()
                .eqIfPresent(IotFailureReportDO::getStatus, reqVO.getStatus())
                .inIfPresent(IotFailureReportDO::getDeptId, ids)
                .betweenIfPresent(IotFailureReportDO::getCreateTime, reqVO.getCreateTime()));
    }
}
