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

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.SortablePageParam;
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.vo.IotCarDevicePlateVO;
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.controller.admin.vo.IotDeviceSimple;
import cn.iocoder.yudao.module.pms.dal.dataobject.IotDeviceDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
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 org.apache.ibatis.annotations.Update;

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

/**
 * 设备台账 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface IotDeviceMapper extends BaseMapperX<IotDeviceDO> {
    default PageResult<IotDeviceDO> selectPage(IotDevicePageReqVO reqVO, Collection<Long> deptIds, SortablePageParam sortablePageParam) {
//        return selectPage(reqVO, new LambdaQueryWrapperX<IotDeviceDO>()
        return selectPage(sortablePageParam, new LambdaQueryWrapperX<IotDeviceDO>()
                .likeIfPresent(IotDeviceDO::getYfDeviceCode, reqVO.getYfDeviceCode())
                .likeIfPresent(IotDeviceDO::getDeviceCode, reqVO.getDeviceCode())
                .likeIfPresent(IotDeviceDO::getDeviceName, reqVO.getDeviceName())
                .likeIfPresent(IotDeviceDO::getAssetOwnership, reqVO.getAssetOwnership())
                .likeIfPresent(IotDeviceDO::getAddress, reqVO.getAddress())
                .eqIfPresent(IotDeviceDO::getBrand, reqVO.getBrand())
                .likeIfPresent(IotDeviceDO::getModel, reqVO.getModel())
                .likeIfPresent(IotDeviceDO::getBrandName, reqVO.getBrandName())
                .eqIfPresent(IotDeviceDO::getDeviceGrade, reqVO.getDeviceGrade())
                //.eqIfPresent(IotDeviceDO::getOrgId, reqVO.getOrgId())
                .inIfPresent(IotDeviceDO::getDeptId, deptIds)
                .eqIfPresent(IotDeviceDO::getDeviceStatus, reqVO.getDeviceStatus())
                .eqIfPresent(IotDeviceDO::getAssetProperty, reqVO.getAssetProperty())
                .eqIfPresent(IotDeviceDO::getPicUrl, reqVO.getPicUrl())
                .eqIfPresent(IotDeviceDO::getRemark, reqVO.getRemark())
                .eqIfPresent(IotDeviceDO::getManufacturerId, reqVO.getManufacturerId())
                .eqIfPresent(IotDeviceDO::getSupplierId, reqVO.getSupplierId())
                .eqIfPresent(IotDeviceDO::getNameplate, reqVO.getNameplate())
                .eqIfPresent(IotDeviceDO::getExpires, reqVO.getExpires())
                .eqIfPresent(IotDeviceDO::getPlPrice, reqVO.getPlPrice())
                .likeIfPresent(IotDeviceDO::getYfClass, reqVO.getYfClass())
                .likeIfPresent(IotDeviceDO::getDeviceNo, reqVO.getDeviceNo()).likeIfPresent(IotDeviceDO::getCarNo, reqVO.getCarNo())
                //.betweenIfPresent(IotDeviceDO::getPlDate, reqVO.getPlDate())
                //.betweenIfPresent(IotDeviceDO::getManDate, reqVO.getManDate())
                .eqIfPresent(IotDeviceDO::getPlYear, reqVO.getPlYear())
                .betweenIfPresent(IotDeviceDO::getPlStartDate, reqVO.getPlStartDate())
                .eqIfPresent(IotDeviceDO::getPlMonthed, reqVO.getPlMonthed())
                .eqIfPresent(IotDeviceDO::getPlAmounted, reqVO.getPlAmounted())
                .eqIfPresent(IotDeviceDO::getRemainAmount, reqVO.getRemainAmount())
                .eqIfPresent(IotDeviceDO::getInfoId, reqVO.getInfoId())
                .eqIfPresent(IotDeviceDO::getInfoType, reqVO.getInfoType())
                .eqIfPresent(IotDeviceDO::getAssetClass, reqVO.getAssetClass())
                .likeIfPresent(IotDeviceDO::getInfoName, reqVO.getInfoName())
                .eqIfPresent(IotDeviceDO::getInfoRemark, reqVO.getInfoRemark())
                .eqIfPresent(IotDeviceDO::getInfoUrl, reqVO.getInfoUrl())
                .eqIfPresent(IotDeviceDO::getTemplateJson, reqVO.getTemplateJson())
                .likeIfPresent(IotDeviceDO::getChargeName, reqVO.getChargeName())
                .likeIfPresent(IotDeviceDO::getUseProject, reqVO.getUseProject())
                .eqIfPresent(IotDeviceDO::getBomSyncStatus, reqVO.getBomSyncStatus()));
//                .orderByAsc(IotDeviceDO::getSortColumn));
    }

    default PageResult<IotDeviceDO> selectPageReport(IotDevicePageReqVO reqVO, Collection<Long> deptIds) {
//        return selectPage(reqVO, new LambdaQueryWrapperX<IotDeviceDO>()
        return selectPage(reqVO, new LambdaQueryWrapperX<IotDeviceDO>()
                .likeIfPresent(IotDeviceDO::getYfDeviceCode, reqVO.getYfDeviceCode())
                .likeIfPresent(IotDeviceDO::getDeviceCode, reqVO.getDeviceCode())
                .likeIfPresent(IotDeviceDO::getDeviceName, reqVO.getDeviceName())
                .eqIfPresent(IotDeviceDO::getBrand, reqVO.getBrand())
                .eqIfPresent(IotDeviceDO::getModel, reqVO.getModel())
                .likeIfPresent(IotDeviceDO::getBrandName, reqVO.getBrandName())
                //.eqIfPresent(IotDeviceDO::getOrgId, reqVO.getOrgId())
                .inIfPresent(IotDeviceDO::getDeptId, deptIds)
                .eqIfPresent(IotDeviceDO::getDeviceStatus, reqVO.getDeviceStatus())
                .eqIfPresent(IotDeviceDO::getAssetProperty, reqVO.getAssetProperty())
                .eqIfPresent(IotDeviceDO::getPicUrl, reqVO.getPicUrl())
                .eqIfPresent(IotDeviceDO::getRemark, reqVO.getRemark())
                .eqIfPresent(IotDeviceDO::getManufacturerId, reqVO.getManufacturerId())
                .eqIfPresent(IotDeviceDO::getSupplierId, reqVO.getSupplierId())
                .eqIfPresent(IotDeviceDO::getNameplate, reqVO.getNameplate())
                .eqIfPresent(IotDeviceDO::getExpires, reqVO.getExpires())
                .eqIfPresent(IotDeviceDO::getPlPrice, reqVO.getPlPrice())
                .eqIfPresent(IotDeviceDO::getPlYear, reqVO.getPlYear())
                .betweenIfPresent(IotDeviceDO::getPlStartDate, reqVO.getPlStartDate())
                .eqIfPresent(IotDeviceDO::getPlMonthed, reqVO.getPlMonthed())
                .eqIfPresent(IotDeviceDO::getPlAmounted, reqVO.getPlAmounted())
                .eqIfPresent(IotDeviceDO::getRemainAmount, reqVO.getRemainAmount())
                .eqIfPresent(IotDeviceDO::getInfoId, reqVO.getInfoId())
                .eqIfPresent(IotDeviceDO::getInfoType, reqVO.getInfoType())
                .eqIfPresent(IotDeviceDO::getAssetClass, reqVO.getAssetClass())
                .likeIfPresent(IotDeviceDO::getInfoName, reqVO.getInfoName())
                .eqIfPresent(IotDeviceDO::getInfoRemark, reqVO.getInfoRemark())
                .eqIfPresent(IotDeviceDO::getInfoUrl, reqVO.getInfoUrl())
                .eqIfPresent(IotDeviceDO::getTemplateJson, reqVO.getTemplateJson())
                .eqIfPresent(IotDeviceDO::getBomSyncStatus, reqVO.getBomSyncStatus()));
//                .orderByAsc(IotDeviceDO::getSortColumn));
    }

    default PageResult<IotDeviceDO> selectPageApp(IotDevicePageReqVO reqVO, Collection<Long> deptIds, SortablePageParam sortablePageParam) {
        LambdaQueryWrapper<IotDeviceDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(reqVO.getCommonParam()), IotDeviceDO::getDeviceName, reqVO.getCommonParam())
                .or()
                .like(StringUtils.isNotBlank(reqVO.getCommonParam()), IotDeviceDO::getDeviceCode, reqVO.getCommonParam())
                .or()
                .eq(Objects.nonNull(reqVO.getDeptId()), IotDeviceDO::getDeptId, reqVO.getDeptId())
                ;
        return selectPage(sortablePageParam, wrapper);
    }

    default List<IotDeviceDO> selectList(IotDevicePageReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<IotDeviceDO>()
                .inIfPresent(IotDeviceDO::getDeptId, reqVO.getDeptIds())
                .likeIfPresent(IotDeviceDO::getYfDeviceCode, reqVO.getYfDeviceCode())
                .likeIfPresent(IotDeviceDO::getDeviceCode, reqVO.getDeviceCode())
                .likeIfPresent(IotDeviceDO::getDeviceName, reqVO.getDeviceName())
//                .eqIfPresent(IotDeviceDO::getDeptId, reqVO.getDeptId())
//                .inIfPresent(IotDeviceDO::getDeptId, reqVO.getDeptIds())
                .eqIfPresent(IotDeviceDO::getBrand, reqVO.getBrand())
                .eqIfPresent(IotDeviceDO::getModel, reqVO.getModel())
                .eqIfPresent(IotDeviceDO::getDeviceStatus, reqVO.getDeviceStatus())
                .eqIfPresent(IotDeviceDO::getAssetProperty, reqVO.getAssetProperty())
                .eqIfPresent(IotDeviceDO::getPicUrl, reqVO.getPicUrl())
                .eqIfPresent(IotDeviceDO::getRemark, reqVO.getRemark())
                .eqIfPresent(IotDeviceDO::getManufacturerId, reqVO.getManufacturerId())
                .eqIfPresent(IotDeviceDO::getSupplierId, reqVO.getSupplierId())
                .eqIfPresent(IotDeviceDO::getNameplate, reqVO.getNameplate())
                .eqIfPresent(IotDeviceDO::getExpires, reqVO.getExpires())
                .eqIfPresent(IotDeviceDO::getPlPrice, reqVO.getPlPrice())
                .eqIfPresent(IotDeviceDO::getPlYear, reqVO.getPlYear())
                .betweenIfPresent(IotDeviceDO::getPlStartDate, reqVO.getPlStartDate())
                .eqIfPresent(IotDeviceDO::getPlMonthed, reqVO.getPlMonthed())
                .eqIfPresent(IotDeviceDO::getPlAmounted, reqVO.getPlAmounted())
                .eqIfPresent(IotDeviceDO::getRemainAmount, reqVO.getRemainAmount())
                .eqIfPresent(IotDeviceDO::getInfoId, reqVO.getInfoId())
                .eqIfPresent(IotDeviceDO::getInfoType, reqVO.getInfoType())
                .eqIfPresent(IotDeviceDO::getAssetClass, reqVO.getAssetClass())
                .inIfPresent(IotDeviceDO::getAssetClass, reqVO.getAssetClasses())
                .likeIfPresent(IotDeviceDO::getInfoName, reqVO.getInfoName())
                .eqIfPresent(IotDeviceDO::getInfoRemark, reqVO.getInfoRemark())
                .eqIfPresent(IotDeviceDO::getInfoUrl, reqVO.getInfoUrl())
                .eqIfPresent(IotDeviceDO::getTemplateJson, reqVO.getTemplateJson())
                .eqIfPresent(IotDeviceDO::getBomSyncStatus, reqVO.getBomSyncStatus()));
    }

    default List<IotDeviceDO> selectListAlone(IotDevicePageReqVO reqVO) {
        LambdaQueryWrapperX<IotDeviceDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper
                .inIfPresent(IotDeviceDO::getId, reqVO.getDeviceIds())
                .inIfPresent(IotDeviceDO::getDeptId, reqVO.getDeptIds())
                .eqIfPresent(IotDeviceDO::getBrand, reqVO.getBrand())
                .eqIfPresent(IotDeviceDO::getModel, reqVO.getModel())
                .eqIfPresent(IotDeviceDO::getDeviceStatus, reqVO.getDeviceStatus())
                .eqIfPresent(IotDeviceDO::getAssetProperty, reqVO.getAssetProperty())
                .eqIfPresent(IotDeviceDO::getPicUrl, reqVO.getPicUrl())
                .eqIfPresent(IotDeviceDO::getRemark, reqVO.getRemark())
                .eqIfPresent(IotDeviceDO::getManufacturerId, reqVO.getManufacturerId())
                .eqIfPresent(IotDeviceDO::getSupplierId, reqVO.getSupplierId())
                .eqIfPresent(IotDeviceDO::getNameplate, reqVO.getNameplate())
                .eqIfPresent(IotDeviceDO::getExpires, reqVO.getExpires())
                .eqIfPresent(IotDeviceDO::getPlPrice, reqVO.getPlPrice())
                .eqIfPresent(IotDeviceDO::getPlYear, reqVO.getPlYear())
                .betweenIfPresent(IotDeviceDO::getPlStartDate, reqVO.getPlStartDate())
                .eqIfPresent(IotDeviceDO::getPlMonthed, reqVO.getPlMonthed())
                .eqIfPresent(IotDeviceDO::getPlAmounted, reqVO.getPlAmounted())
                .eqIfPresent(IotDeviceDO::getRemainAmount, reqVO.getRemainAmount())
                .eqIfPresent(IotDeviceDO::getInfoId, reqVO.getInfoId())
                .eqIfPresent(IotDeviceDO::getInfoType, reqVO.getInfoType())
                .eqIfPresent(IotDeviceDO::getAssetClass, reqVO.getAssetClass())
                .inIfPresent(IotDeviceDO::getAssetClass, reqVO.getAssetClasses())
                .likeIfPresent(IotDeviceDO::getInfoName, reqVO.getInfoName())
                .eqIfPresent(IotDeviceDO::getInfoRemark, reqVO.getInfoRemark())
                .eqIfPresent(IotDeviceDO::getInfoUrl, reqVO.getInfoUrl())
                .eqIfPresent(IotDeviceDO::getTemplateJson, reqVO.getTemplateJson())
                .eqIfPresent(IotDeviceDO::getBomSyncStatus, reqVO.getBomSyncStatus());
        if (StrUtil.isNotBlank(reqVO.getDeviceName())) {
            queryWrapper.and(i -> i.like(IotDeviceDO::getDeviceName, reqVO.getDeviceName())
                    .or()
                    .like(IotDeviceDO::getDeviceCode, reqVO.getDeviceName())
                    .or()
                    .like(IotDeviceDO::getYfDeviceCode, reqVO.getDeviceName()));
        }
        return selectList(queryWrapper);
    }

    default List<IotDeviceDO> selectDataCollect(IotDevicePageReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<IotDeviceDO>()
                .inIfPresent(IotDeviceDO::getDeptId, reqVO.getDeptIds())
                .isNotNull(IotDeviceDO::getYfDeviceId));
    }
    default List<IotDeviceDO> selectSimpleList(IotDevicePageReqVO reqVO, Collection<Long> deptIds) {
        return selectList(new LambdaQueryWrapperX<IotDeviceDO>()
                .inIfPresent(IotDeviceDO::getDeptId, reqVO.getDeptIds())
                .inIfPresent(IotDeviceDO::getId, reqVO.getDeviceIds())
                .likeIfPresent(IotDeviceDO::getDeviceCode, reqVO.getDeviceCode())
                .likeIfPresent(IotDeviceDO::getDeviceName, reqVO.getDeviceName())
                .inIfPresent(IotDeviceDO::getDeptId, deptIds));
    }

    default PageResult<IotDeviceDO> selectAllTdPage(IotDevicePageReqVO reqVO, Collection<Long> deptIds, List<String> codes) {
        return selectPage(reqVO, new LambdaQueryWrapperX<IotDeviceDO>()
//                .inIfPresent(IotDeviceDO::getDeviceCode, codes)
                .likeIfPresent(IotDeviceDO::getDeviceName, reqVO.getDeviceName())
                .eqIfPresent(IotDeviceDO::getBrand, reqVO.getBrand())
                .eqIfPresent(IotDeviceDO::getModel, reqVO.getModel())
                //.eqIfPresent(IotDeviceDO::getOrgId, reqVO.getOrgId())
                .inIfPresent(IotDeviceDO::getDeptId, deptIds)
                .eqIfPresent(IotDeviceDO::getDeviceStatus, reqVO.getDeviceStatus())
                .eqIfPresent(IotDeviceDO::getAssetProperty, reqVO.getAssetProperty())
                .eqIfPresent(IotDeviceDO::getPicUrl, reqVO.getPicUrl())
                .eqIfPresent(IotDeviceDO::getRemark, reqVO.getRemark())
                .eqIfPresent(IotDeviceDO::getManufacturerId, reqVO.getManufacturerId())
                .eqIfPresent(IotDeviceDO::getSupplierId, reqVO.getSupplierId())
                .eqIfPresent(IotDeviceDO::getNameplate, reqVO.getNameplate())
                .eqIfPresent(IotDeviceDO::getExpires, reqVO.getExpires())
                .eqIfPresent(IotDeviceDO::getPlPrice, reqVO.getPlPrice())
                //.betweenIfPresent(IotDeviceDO::getPlDate, reqVO.getPlDate())
                //.betweenIfPresent(IotDeviceDO::getManDate, reqVO.getManDate())
                .eqIfPresent(IotDeviceDO::getPlYear, reqVO.getPlYear())
                .betweenIfPresent(IotDeviceDO::getPlStartDate, reqVO.getPlStartDate())
                .eqIfPresent(IotDeviceDO::getPlMonthed, reqVO.getPlMonthed())
                .eqIfPresent(IotDeviceDO::getPlAmounted, reqVO.getPlAmounted())
                .eqIfPresent(IotDeviceDO::getRemainAmount, reqVO.getRemainAmount())
                .eqIfPresent(IotDeviceDO::getInfoId, reqVO.getInfoId())
                .eqIfPresent(IotDeviceDO::getInfoType, reqVO.getInfoType())
                .eqIfPresent(IotDeviceDO::getAssetClass, reqVO.getAssetClass())
                .likeIfPresent(IotDeviceDO::getInfoName, reqVO.getInfoName())
                .eqIfPresent(IotDeviceDO::getInfoRemark, reqVO.getInfoRemark())
                .eqIfPresent(IotDeviceDO::getInfoUrl, reqVO.getInfoUrl())
                .eqIfPresent(IotDeviceDO::getTemplateJson, reqVO.getTemplateJson())
                .likeIfPresent(IotDeviceDO::getDeviceCode, reqVO.getDeviceCode())
                .inIfPresent(IotDeviceDO::getDeviceCode, codes)
                .eqIfPresent(IotDeviceDO::getBomSyncStatus, reqVO.getBomSyncStatus())
                .eqIfPresent(IotDeviceDO::getIfInline, reqVO.getIfInline())
                .isNotNull(IotDeviceDO::getIfInline)
                .orderByAsc(IotDeviceDO::getIfInline).orderByDesc(IotDeviceDO::getLastInlineTime).orderByAsc(IotDeviceDO::getDeviceCode));
    }

    default PageResult<IotDeviceDO> selectTdPage(IotDevicePageReqVO reqVO, Collection<Long> deptIds, List<String> codes) {
        return selectPage(reqVO, new LambdaQueryWrapperX<IotDeviceDO>()
//                .inIfPresent(IotDeviceDO::getDeviceCode, codes)
                .likeIfPresent(IotDeviceDO::getDeviceName, reqVO.getDeviceName())
                .eqIfPresent(IotDeviceDO::getBrand, reqVO.getBrand())
                .eqIfPresent(IotDeviceDO::getModel, reqVO.getModel())
                //.eqIfPresent(IotDeviceDO::getOrgId, reqVO.getOrgId())
                .inIfPresent(IotDeviceDO::getDeptId, deptIds)
                .eqIfPresent(IotDeviceDO::getDeviceStatus, reqVO.getDeviceStatus())
                .eqIfPresent(IotDeviceDO::getAssetProperty, reqVO.getAssetProperty())
                .eqIfPresent(IotDeviceDO::getPicUrl, reqVO.getPicUrl())
                .eqIfPresent(IotDeviceDO::getRemark, reqVO.getRemark())
                .eqIfPresent(IotDeviceDO::getManufacturerId, reqVO.getManufacturerId())
                .eqIfPresent(IotDeviceDO::getSupplierId, reqVO.getSupplierId())
                .eqIfPresent(IotDeviceDO::getNameplate, reqVO.getNameplate())
                .eqIfPresent(IotDeviceDO::getExpires, reqVO.getExpires())
                .eqIfPresent(IotDeviceDO::getPlPrice, reqVO.getPlPrice())
                //.betweenIfPresent(IotDeviceDO::getPlDate, reqVO.getPlDate())
                //.betweenIfPresent(IotDeviceDO::getManDate, reqVO.getManDate())
                .eqIfPresent(IotDeviceDO::getPlYear, reqVO.getPlYear())
                .betweenIfPresent(IotDeviceDO::getPlStartDate, reqVO.getPlStartDate())
                .eqIfPresent(IotDeviceDO::getPlMonthed, reqVO.getPlMonthed())
                .eqIfPresent(IotDeviceDO::getPlAmounted, reqVO.getPlAmounted())
                .eqIfPresent(IotDeviceDO::getRemainAmount, reqVO.getRemainAmount())
                .eqIfPresent(IotDeviceDO::getInfoId, reqVO.getInfoId())
                .eqIfPresent(IotDeviceDO::getInfoType, reqVO.getInfoType())
                .eqIfPresent(IotDeviceDO::getAssetClass, reqVO.getAssetClass())
                .likeIfPresent(IotDeviceDO::getInfoName, reqVO.getInfoName())
                .eqIfPresent(IotDeviceDO::getInfoRemark, reqVO.getInfoRemark())
                .eqIfPresent(IotDeviceDO::getInfoUrl, reqVO.getInfoUrl())
                .eqIfPresent(IotDeviceDO::getTemplateJson, reqVO.getTemplateJson())
                .likeIfPresent(IotDeviceDO::getDeviceCode, reqVO.getDeviceCode())
                .inIfPresent(IotDeviceDO::getDeviceCode, codes)
                .eqIfPresent(IotDeviceDO::getBomSyncStatus, reqVO.getBomSyncStatus())
                .eqIfPresent(IotDeviceDO::getIfInline, reqVO.getIfInline())
                .isNotNull(IotDeviceDO::getIfInline)
                .isNull(IotDeviceDO::getVehicleName)
                .orderByAsc(IotDeviceDO::getIfInline).orderByDesc(IotDeviceDO::getLastInlineTime).orderByAsc(IotDeviceDO::getDeviceCode));
    }


    default PageResult<IotDeviceDO> selectZhbdTdPage(IotDevicePageReqVO reqVO, Collection<Long> deptIds, List<String> codes) {
        return selectPage(reqVO, new LambdaQueryWrapperX<IotDeviceDO>()
//                .inIfPresent(IotDeviceDO::getDeviceCode, codes)
                .likeIfPresent(IotDeviceDO::getDeviceName, reqVO.getDeviceName())
                .eqIfPresent(IotDeviceDO::getBrand, reqVO.getBrand())
                .eqIfPresent(IotDeviceDO::getModel, reqVO.getModel())
                //.eqIfPresent(IotDeviceDO::getOrgId, reqVO.getOrgId())
                .inIfPresent(IotDeviceDO::getDeptId, deptIds)
                .eqIfPresent(IotDeviceDO::getDeviceStatus, reqVO.getDeviceStatus())
                .eqIfPresent(IotDeviceDO::getAssetProperty, reqVO.getAssetProperty())
                .eqIfPresent(IotDeviceDO::getPicUrl, reqVO.getPicUrl())
                .eqIfPresent(IotDeviceDO::getRemark, reqVO.getRemark())
                .eqIfPresent(IotDeviceDO::getManufacturerId, reqVO.getManufacturerId())
                .eqIfPresent(IotDeviceDO::getSupplierId, reqVO.getSupplierId())
                .eqIfPresent(IotDeviceDO::getNameplate, reqVO.getNameplate())
                .eqIfPresent(IotDeviceDO::getExpires, reqVO.getExpires())
                .eqIfPresent(IotDeviceDO::getPlPrice, reqVO.getPlPrice())
                //.betweenIfPresent(IotDeviceDO::getPlDate, reqVO.getPlDate())
                //.betweenIfPresent(IotDeviceDO::getManDate, reqVO.getManDate())
                .eqIfPresent(IotDeviceDO::getPlYear, reqVO.getPlYear())
                .betweenIfPresent(IotDeviceDO::getPlStartDate, reqVO.getPlStartDate())
                .eqIfPresent(IotDeviceDO::getPlMonthed, reqVO.getPlMonthed())
                .eqIfPresent(IotDeviceDO::getPlAmounted, reqVO.getPlAmounted())
                .eqIfPresent(IotDeviceDO::getRemainAmount, reqVO.getRemainAmount())
                .eqIfPresent(IotDeviceDO::getInfoId, reqVO.getInfoId())
                .eqIfPresent(IotDeviceDO::getInfoType, reqVO.getInfoType())
                .eqIfPresent(IotDeviceDO::getAssetClass, reqVO.getAssetClass())
                .likeIfPresent(IotDeviceDO::getInfoName, reqVO.getInfoName())
                .eqIfPresent(IotDeviceDO::getInfoRemark, reqVO.getInfoRemark())
                .eqIfPresent(IotDeviceDO::getInfoUrl, reqVO.getInfoUrl())
                .eqIfPresent(IotDeviceDO::getTemplateJson, reqVO.getTemplateJson())
                .likeIfPresent(IotDeviceDO::getDeviceCode, reqVO.getDeviceCode())
                .inIfPresent(IotDeviceDO::getDeviceCode, codes)
                .eqIfPresent(IotDeviceDO::getBomSyncStatus, reqVO.getBomSyncStatus())
                .eqIfPresent(IotDeviceDO::getIfInline, reqVO.getIfInline())
                .isNotNull(IotDeviceDO::getIfInline)
                .isNotNull(IotDeviceDO::getVehicleName)
                .orderByAsc(IotDeviceDO::getIfInline).orderByDesc(IotDeviceDO::getLastInlineTime).orderByAsc(IotDeviceDO::getDeviceCode));
    }

    default PageResult<IotDeviceDO> selectTdPageApp(IotDevicePageReqVO reqVO, Collection<Long> deptIds) {
        LambdaQueryWrapper<IotDeviceDO> wrapper = new LambdaQueryWrapper<>();
//        wrapper.isNotNull(IotDeviceDO::getIfInline).like(StringUtils.isNotBlank(reqVO.getCommonParam()), IotDeviceDO::getDeviceName, reqVO.getCommonParam())
//                .or()
//                .like(StringUtils.isNotBlank(reqVO.getCommonParam()), IotDeviceDO::getDeviceCode, reqVO.getCommonParam())
//                .or()
//                .like(StringUtils.isNotBlank(reqVO.getCommonParam()), IotDeviceDO::getChargeName, reqVO.getCommonParam()).orderByDesc(IotDeviceDO::getLastInlineTime)
//        ;
//        wrapper.isNotNull(IotDeviceDO::getIfInline)
//                .and(i -> i.like(StringUtils.isNotBlank(reqVO.getCommonParam()), IotDeviceDO::getDeviceName, reqVO.getCommonParam())
//                        .or()
//                        .like(StringUtils.isNotBlank(reqVO.getCommonParam()), IotDeviceDO::getDeviceCode, reqVO.getCommonParam())
//                        .or()
//                        .like(StringUtils.isNotBlank(reqVO.getCommonParam()), IotDeviceDO::getChargeName, reqVO.getCommonParam())).orderByDesc(IotDeviceDO::getLastInlineTime);


        // 基础条件：isInline不为空
        wrapper.isNotNull(IotDeviceDO::getIfInline);
        // 获取查询参数
        String commonParam = reqVO.getCommonParam();
        // 只有当commonParam不为空时，才添加and条件
        if (StringUtils.isNotBlank(commonParam)) {
            wrapper.and(i -> i.like(IotDeviceDO::getDeviceName, commonParam)
                    .or()
                    .like(IotDeviceDO::getDeviceCode, commonParam)
                    .or()
                    .like(IotDeviceDO::getChargeName, commonParam));
        }
        // 排序条件
        wrapper.orderByAsc(IotDeviceDO::getIfInline).orderByDesc(IotDeviceDO::getLastInlineTime);

        return selectPage(reqVO, wrapper);
    }

    default List<IotDeviceDO> selectByCodeIn(Collection<String> codes) {
        return selectList(new LambdaQueryWrapperX<IotDeviceDO>()
                .in(IotDeviceDO::getDeviceCode, codes));
    }
    default Long selectByDept(Set<Long> ids) {
        return selectCount(new LambdaQueryWrapperX<IotDeviceDO>()
                .inIfPresent(IotDeviceDO::getDeptId, ids));
    }
    default Long selectCountByParam(Set<Long> ids, IotDevicePageReqVO iotDevicePageReqVO) {
        return selectCount(new LambdaQueryWrapperX<IotDeviceDO>()
                .inIfPresent(IotDeviceDO::getDeptId, ids).likeIfPresent(IotDeviceDO::getDeviceCode, iotDevicePageReqVO.getDeviceCode()).likeIfPresent(IotDeviceDO::getDeviceName, iotDevicePageReqVO.getDeviceName()));
    }
    /* default PageResult<IotDeviceDO> deviceAssociateBomPage(IotDevicePageReqVO reqVO, Collection<Long> deptIds) {
        MPJLambdaWrapperX<IotDeviceDO> query = new MPJLambdaWrapperX<IotDeviceDO>();
        query.innerJoin(IotBomDO.class, IotBomDO::getDeviceCategoryId, IotDeviceDO::getAssetClass)
                .eq(IotBomDO::getLeafFlag, 1)
                .like(IotBomDO::getType, "2")
                .in(CollUtil.isNotEmpty(deptIds), IotDeviceDO::getDeptId, deptIds)
                .like(StrUtil.isNotBlank(reqVO.getDeviceName()), IotDeviceDO::getDeviceName, reqVO.getDeviceName())
                .like(StrUtil.isNotBlank(reqVO.getName()), IotBomDO::getName, reqVO.getName());
        return selectJoinPage(reqVO, IotDeviceDO.class, query);
    } */

    IPage<IotDeviceRespVO> deviceAssociateBomPage(IPage<IotDeviceRespVO> page,
                                                  @Param("reqVO") IotDevicePageReqVO reqVO, @Param("deptIds") Collection<Long> deptIds);

    List<IotDeviceRespVO> deviceAssociateBomList(@Param("deviceIds") Collection<Long> deviceIds, @Param("bomFlag") String bomFlag);

    IPage<IotDeviceRespVO> deviceAssociateBomListPage(IPage<IotDeviceRespVO> page,
                                                      @Param("deviceIds") Collection<Long> deviceIds, @Param("bomFlag") String bomFlag);

    List<IotCarDevicePlateVO> allCarsContainsPlates();
    default Long selectCountByTimeAndStatus(IotDevicePageReqVO reqVO) {
        return selectCount(new LambdaQueryWrapperX<IotDeviceDO>()
                .eqIfPresent(IotDeviceDO::getDeviceStatus, reqVO.getDeviceStatus())
                .betweenIfPresent(IotDeviceDO::getCreateTime, reqVO.getCreateTime()));
    }

    IPage<IotDeviceRespVO> personRelationDevices(IPage<IotDeviceRespVO> page, @Param("reqVO") IotDevicePageReqVO reqVO,
                                                 @Param("deptIds") Collection<Long> deptIds, @Param("deviceIds") Collection<Long> deviceIds);

    IPage<IotDeviceRespVO> deviceStatuses(IPage<IotDeviceRespVO> page,
                                                 @Param("reqVO") IotDevicePageReqVO reqVO, @Param("deptIds") Collection<Long> deptIds);

    IPage<IotDeviceRespVO> deviceAllots(IPage<IotDeviceRespVO> page,
                                          @Param("reqVO") IotDevicePageReqVO reqVO, @Param("deptIds") Collection<Long> deptIds);

    @Update("UPDATE rq_iot_device SET bom_sync_status = 1 WHERE id = #{id}")
    void updateBomSyncStatus(@Param("id") Long id);


    IPage<IotDeviceRespVO> deviceDistances(IPage<IotDeviceRespVO> page, @Param("reqVO") IotMainWorkOrderPageReqVO reqVO,
                                           @Param("sortedDeviceIds") Collection<Long> sortedDeviceIds,
                                           @Param("deviceIds") Collection<Long> deviceIds,
                                           @Param("mainBomDeviceIds") Collection<Long> mainBomDeviceIds, @Param("deptIds") Collection<Long> deptIds);


    IPage<IotDeviceRespVO> allDeviceDistances(IPage<IotDeviceRespVO> page, @Param("reqVO") IotMainWorkOrderPageReqVO reqVO,
                                           @Param("alarmDeviceIds") Collection<Long> alarmDeviceIds,
                                           @Param("deptIds") Collection<Long> deptIds);

    List<IotDeviceRespVO> timeoutDeviceMaintenances(@Param("reqVO") IotMainWorkOrderPageReqVO reqVO,
                                              @Param("alarmDeviceIds") Collection<Long> alarmDeviceIds,
                                              @Param("deptIds") Collection<Long> deptIds);

    List<IotDeviceRespVO> deviceAlarmDistances(@Param("sortedDeviceIds") Collection<Long> sortedDeviceIds,
                                           @Param("deviceIds") Collection<Long> deviceIds,
                                           @Param("mainBomDeviceIds") Collection<Long> mainBomDeviceIds);

    List<IotDeviceRespVO> deviceAlarmDistancesSearch(@Param("sortedDeviceIds") Collection<Long> sortedDeviceIds,
                                               @Param("mainBomDeviceIds") Collection<Long> mainBomDeviceIds);

    List<IotDeviceSimple> selectCodeAndNameList();
    List<IotDeviceSimple> selectTypeNumber(@Param("reqVO") IotDevicePageReqVO reqVO,@Param("products") List<Integer> products);
    List<IotDeviceRespVO> selectExport(@Param("reqVO") IotDevicePageReqVO reqVO);
    Long getTotalA(@Param("reqVO") IotDevicePageReqVO reqVO);
    List<Long> getFailureCount(@Param("reqVO") IotDevicePageReqVO reqVO);
    List<Long> getRepairCount(@Param("reqVO") IotDevicePageReqVO reqVO);

    default int updateTdCron(IotDeviceDO iotDeviceDO) {
        // 非空校验保留
        Assert.notNull(iotDeviceDO);
        LambdaUpdateWrapper<IotDeviceDO> lambdaUpdateWrapper = new LambdaUpdateWrapper<IotDeviceDO>()
                // 逐个设置更新字段，MyBatis-Plus自动处理类型和SQL拼接
                .set(IotDeviceDO::getYfDeviceId, iotDeviceDO.getYfDeviceId())
                .set(IotDeviceDO::getLastInlineTime, iotDeviceDO.getLastInlineTime())
                .set(IotDeviceDO::getIfInline, iotDeviceDO.getIfInline())
                .set(IotDeviceDO::getLat, iotDeviceDO.getLat())
                .set(IotDeviceDO::getLng, iotDeviceDO.getLng())
                // 条件匹配：根据主键ID更新（原CAS逻辑保留）
                .eq(IotDeviceDO::getId, iotDeviceDO.getId());
        // 第一个参数传null，仅使用wrapper作为更新条件和字段，符合原逻辑
        return update(null, lambdaUpdateWrapper);
    }

    default int updateZhbd(IotDeviceDO iotDeviceDO) {
        // 非空校验保留
        Assert.notNull(iotDeviceDO);
        LambdaUpdateWrapper<IotDeviceDO> lambdaUpdateWrapper = new LambdaUpdateWrapper<IotDeviceDO>()
                // 逐个设置更新字段，MyBatis-Plus自动处理类型和SQL拼接
                .set(IotDeviceDO::getVehicleName, iotDeviceDO.getVehicleName())
                .set(IotDeviceDO::getCarOnline, iotDeviceDO.getCarOnline())
                // 条件匹配：根据主键ID更新（原CAS逻辑保留）
                .eq(IotDeviceDO::getId, iotDeviceDO.getId());
        // 第一个参数传null，仅使用wrapper作为更新条件和字段，符合原逻辑
        return update(null, lambdaUpdateWrapper);
    }
}
