package cn.iocoder.yudao.module.mes.dal.mysql.criticalcomponent;

import java.util.*;
import java.util.stream.Collectors;

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.iot.dal.dataobject.devicemodelattribute.DeviceModelAttributeDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.criticalcomponent.CriticalComponentDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.deviceledger.DeviceLedgerDO;
import com.alibaba.excel.util.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.mes.controller.admin.criticalcomponent.vo.*;

/**
 * 设备关键件 Mapper
 *
 * @author 内蒙必硕
 */
@Mapper
public interface CriticalComponentMapper extends BaseMapperX<CriticalComponentDO> {

    default PageResult<CriticalComponentDO> selectPage(CriticalComponentPageReqVO reqVO) {

        LambdaQueryWrapperX<CriticalComponentDO> criticalComponentDOLambdaQueryWrapperX = new LambdaQueryWrapperX<>();
        criticalComponentDOLambdaQueryWrapperX
                .eqIfPresent(CriticalComponentDO::getCode, reqVO.getCode())
                .likeIfPresent(CriticalComponentDO::getName, reqVO.getName())
                .eqIfPresent(CriticalComponentDO::getDescription, reqVO.getDescription())
                .eqIfPresent(CriticalComponentDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(CriticalComponentDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(CriticalComponentDO::getCreateTime);

        // 单独处理 ids 条件
        if (StringUtils.isNotBlank(reqVO.getIds())) {
            List<Long> idList = Arrays.stream(reqVO.getIds().split(","))
                    .map(String::trim)
                    .map(Long::valueOf)
                    .collect(Collectors.toList());
            criticalComponentDOLambdaQueryWrapperX.in(CriticalComponentDO::getId, idList);
        }

        return selectPage(reqVO, criticalComponentDOLambdaQueryWrapperX);
    }

    default CriticalComponentDO selectByCode(String code) {
        return selectOne(CriticalComponentDO::getCode, code);
    }

}