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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
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.mybatis.core.query.MPJLambdaWrapperX;
import cn.iocoder.yudao.module.pms.controller.admin.iotmaintenancebom.vo.IotMaintenanceBomPageReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.IotDeviceDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotmaintenancebom.IotMaintenanceBomDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * PMS 保养计划明细BOM Mapper
 *
 * @author ruiqi
 */
@Mapper
public interface IotMaintenanceBomMapper extends BaseMapperX<IotMaintenanceBomDO> {

    default PageResult<IotMaintenanceBomDO> selectPage(IotMaintenanceBomPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<IotMaintenanceBomDO>()
                .eqIfPresent(IotMaintenanceBomDO::getPlanId, reqVO.getPlanId())
                .eqIfPresent(IotMaintenanceBomDO::getPlanDetailId, reqVO.getPlanDetailId())
                .eqIfPresent(IotMaintenanceBomDO::getDeviceCategoryId, reqVO.getDeviceCategoryId())
                .eqIfPresent(IotMaintenanceBomDO::getDeviceId, reqVO.getDeviceId())
                .eqIfPresent(IotMaintenanceBomDO::getRule, reqVO.getRule())
                .betweenIfPresent(IotMaintenanceBomDO::getLastRunningTime, reqVO.getLastRunningTime())
                .betweenIfPresent(IotMaintenanceBomDO::getNextRunningTime, reqVO.getNextRunningTime())
                .eqIfPresent(IotMaintenanceBomDO::getLastRunningKilometers, reqVO.getLastRunningKilometers())
                .eqIfPresent(IotMaintenanceBomDO::getNextRunningKilometers, reqVO.getNextRunningKilometers())
                .betweenIfPresent(IotMaintenanceBomDO::getLastNaturalDate, reqVO.getLastNaturalDate())
                .eqIfPresent(IotMaintenanceBomDO::getNextNaturalDate, reqVO.getNextNaturalDate())
                .likeIfPresent(IotMaintenanceBomDO::getName, reqVO.getName())
                .eqIfPresent(IotMaintenanceBomDO::getCode, reqVO.getCode())
                .eqIfPresent(IotMaintenanceBomDO::getParentId, reqVO.getParentId())
                .eqIfPresent(IotMaintenanceBomDO::getChildIds, reqVO.getChildIds())
                .eqIfPresent(IotMaintenanceBomDO::getLevel, reqVO.getLevel())
                .eqIfPresent(IotMaintenanceBomDO::getLeafFlag, reqVO.getLeafFlag())
                .eqIfPresent(IotMaintenanceBomDO::getSort, reqVO.getSort())
                .eqIfPresent(IotMaintenanceBomDO::getType, reqVO.getType())
                .eqIfPresent(IotMaintenanceBomDO::getStatus, reqVO.getStatus())
                .eqIfPresent(IotMaintenanceBomDO::getRemark, reqVO.getRemark())
                .eqIfPresent(IotMaintenanceBomDO::getVersion, reqVO.getVersion())
                .betweenIfPresent(IotMaintenanceBomDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(IotMaintenanceBomDO::getId));
    }

    default PageResult<IotMaintenanceBomDO> deviceAssociateBomPage(IotMaintenanceBomPageReqVO reqVO) {
        MPJLambdaWrapperX<IotMaintenanceBomDO> query = new MPJLambdaWrapperX<IotMaintenanceBomDO>();
        query.leftJoin(IotDeviceDO.class, IotDeviceDO::getAssetClass, IotMaintenanceBomDO::getDeviceCategoryId)
                .like(StrUtil.isNotBlank(reqVO.getDeviceName()), IotDeviceDO::getDeviceName, reqVO.getDeviceName())
                .like(StrUtil.isNotBlank(reqVO.getName()), IotMaintenanceBomDO::getName, reqVO.getName());

        return selectJoinPage(reqVO, IotMaintenanceBomDO.class, query);
    }

    default List<IotMaintenanceBomDO> selectList(IotMaintenanceBomPageReqVO reqVO) {
        LambdaQueryWrapperX<IotMaintenanceBomDO> wrapper = new LambdaQueryWrapperX<IotMaintenanceBomDO>()
                .eqIfPresent(IotMaintenanceBomDO::getPlanId, reqVO.getPlanId())
                .inIfPresent(IotMaintenanceBomDO::getPlanId, reqVO.getPlanIds())
                .neIfPresent(IotMaintenanceBomDO::getPlanId, reqVO.getNotPlanId())
                .eqIfPresent(IotMaintenanceBomDO::getPlanDetailId, reqVO.getPlanDetailId())
                .eqIfPresent(IotMaintenanceBomDO::getDeviceCategoryId, reqVO.getDeviceCategoryId())
                .eqIfPresent(IotMaintenanceBomDO::getDeviceId, reqVO.getDeviceId())
                .inIfPresent(IotMaintenanceBomDO::getDeviceId, reqVO.getDeviceIds())
                .eqIfPresent(IotMaintenanceBomDO::getRule, reqVO.getRule())
                .betweenIfPresent(IotMaintenanceBomDO::getLastRunningTime, reqVO.getLastRunningTime())
                .betweenIfPresent(IotMaintenanceBomDO::getNextRunningTime, reqVO.getNextRunningTime())
                .eqIfPresent(IotMaintenanceBomDO::getLastRunningKilometers, reqVO.getLastRunningKilometers())
                .eqIfPresent(IotMaintenanceBomDO::getNextRunningKilometers, reqVO.getNextRunningKilometers())
                .betweenIfPresent(IotMaintenanceBomDO::getLastNaturalDate, reqVO.getLastNaturalDate())
                .eqIfPresent(IotMaintenanceBomDO::getNextNaturalDate, reqVO.getNextNaturalDate())
                .inIfPresent(IotMaintenanceBomDO::getBomNodeId, reqVO.getBomNodeIds())
                .likeIfPresent(IotMaintenanceBomDO::getName, reqVO.getName())
                .inIfPresent(IotMaintenanceBomDO::getName, reqVO.getBomNodeNames())
                .eqIfPresent(IotMaintenanceBomDO::getCode, reqVO.getCode())
                .eqIfPresent(IotMaintenanceBomDO::getParentId, reqVO.getParentId())
                .eqIfPresent(IotMaintenanceBomDO::getChildIds, reqVO.getChildIds())
                .eqIfPresent(IotMaintenanceBomDO::getLevel, reqVO.getLevel())
                .eqIfPresent(IotMaintenanceBomDO::getLeafFlag, reqVO.getLeafFlag())
                .eqIfPresent(IotMaintenanceBomDO::getSort, reqVO.getSort())
                .eqIfPresent(IotMaintenanceBomDO::getType, reqVO.getType())
                .eqIfPresent(IotMaintenanceBomDO::getStatus, reqVO.getStatus())
                .eqIfPresent(IotMaintenanceBomDO::getRemark, reqVO.getRemark())
                .eqIfPresent(IotMaintenanceBomDO::getVersion, reqVO.getVersion())
                .betweenIfPresent(IotMaintenanceBomDO::getCreateTime, reqVO.getCreateTime());

        if (CollUtil.isNotEmpty(reqVO.getCodesOrTypes())) {
            wrapper.and(w -> w.in(IotMaintenanceBomDO::getType, reqVO.getCodesOrTypes())
                    .or()
                    .in(IotMaintenanceBomDO::getCode, reqVO.getCodesOrTypes()));
        }

        return selectList(wrapper);
    }

    default List<IotMaintenanceBomDO> selectAlarmList(IotMaintenanceBomPageReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<IotMaintenanceBomDO>()
                .notIn(IotMaintenanceBomDO::getDeviceId, reqVO.getDeviceIds()));
    }

    default int deleteByPlanId(Long planId) {
        return delete(IotMaintenanceBomDO::getPlanId, planId);
    }
}