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

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

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

    default PageResult<TaskManagementDO> selectPage(TaskManagementPageReqVO reqVO) {


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

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


        return selectPage(reqVO, taskManagementDOLambdaQueryWrapperX);
    }

}