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

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.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.deviceledger.vo.*;

/**
 * 设备类型 Mapper
 *
 * @author 内蒙必硕
 */
@Mapper
public interface DeviceLedgerMapper extends BaseMapperX<DeviceLedgerDO> {

    default PageResult<DeviceLedgerDO> selectPage(DeviceLedgerPageReqVO reqVO) {

        LambdaQueryWrapperX<DeviceLedgerDO> deviceLedgerDOLambdaQueryWrapperX = new LambdaQueryWrapperX<>();
        deviceLedgerDOLambdaQueryWrapperX
                .eqIfPresent(DeviceLedgerDO::getDeviceCode, reqVO.getDeviceCode())
                .likeIfPresent(DeviceLedgerDO::getDeviceName, reqVO.getDeviceName())
                .eqIfPresent(DeviceLedgerDO::getDeviceStatus, reqVO.getDeviceStatus())
                .eqIfPresent(DeviceLedgerDO::getDeviceBrand, reqVO.getDeviceBrand())
                .eqIfPresent(DeviceLedgerDO::getDeviceModel, reqVO.getDeviceModel())
                .eqIfPresent(DeviceLedgerDO::getDeviceSpec, reqVO.getDeviceSpec())
                .eqIfPresent(DeviceLedgerDO::getDeviceType, reqVO.getDeviceType())
                .eqIfPresent(DeviceLedgerDO::getSupplier, reqVO.getSupplier())
                .eqIfPresent(DeviceLedgerDO::getWorkshop, reqVO.getWorkshop())
                .eqIfPresent(DeviceLedgerDO::getSystemOrg, reqVO.getSystemOrg())
                .eqIfPresent(DeviceLedgerDO::getDeviceLocation, reqVO.getDeviceLocation())
                .eqIfPresent(DeviceLedgerDO::getDeviceManager, reqVO.getDeviceManager())
                .eqIfPresent(DeviceLedgerDO::getDeviceRemark, reqVO.getDeviceRemark())
                .eqIfPresent(DeviceLedgerDO::getRemark, reqVO.getRemark())
                .eqIfPresent(DeviceLedgerDO::getSort, reqVO.getSort())
                .betweenIfPresent(DeviceLedgerDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(DeviceLedgerDO::getCreateTime);

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

        return selectPage(reqVO, deviceLedgerDOLambdaQueryWrapperX);
    }

}