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

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.moldplanmaintenance.MoldPlanMaintenanceDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.moldtaskmanagement.MoldTaskManagementDO;
import com.alibaba.excel.util.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.mes.controller.admin.moldtaskmanagement.vo.*;

/**
 * 模具类型 Mapper
 *
 * @author 内蒙必硕
 */
@Mapper
public interface MoldTaskManagementMapper extends BaseMapperX<MoldTaskManagementDO> {

    default PageResult<MoldTaskManagementDO> selectPage(MoldTaskManagementPageReqVO reqVO) {


        LambdaQueryWrapperX<MoldTaskManagementDO> moldtaskManagementDOLambdaQueryWrapperX = new LambdaQueryWrapperX<>();
        moldtaskManagementDOLambdaQueryWrapperX
                .likeIfPresent(MoldTaskManagementDO::getName, reqVO.getName())
                .eqIfPresent(MoldTaskManagementDO::getTaskType, reqVO.getTaskType())
                .eqIfPresent(MoldTaskManagementDO::getMoldList, reqVO.getMoldList())
                .eqIfPresent(MoldTaskManagementDO::getProjectForm, reqVO.getProjectForm())
                .betweenIfPresent(MoldTaskManagementDO::getStartDate, reqVO.getStartDate())
                .betweenIfPresent(MoldTaskManagementDO::getEndDate, reqVO.getEndDate())
                .eqIfPresent(MoldTaskManagementDO::getCronExpression, reqVO.getCronExpression())
                .eqIfPresent(MoldTaskManagementDO::getOperableUsers, reqVO.getOperableUsers())
                .eqIfPresent(MoldTaskManagementDO::getEnabled, reqVO.getEnabled())
                .betweenIfPresent(MoldTaskManagementDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(MoldTaskManagementDO::getId);

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


        return selectPage(reqVO, moldtaskManagementDOLambdaQueryWrapperX);
    }

}