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

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

/**
 * 项目方案关联 Mapper
 *
 * @author 内蒙必硕
 */
@Mapper
public interface TicketManagementMapper extends BaseMapperX<TicketManagementDO> {

    default PageResult<TicketManagementDO> selectPage(TicketManagementPageReqVO reqVO) {

        LambdaQueryWrapperX<TicketManagementDO> ticketManagementDOLambdaQueryWrapperX = new LambdaQueryWrapperX<>();
        ticketManagementDOLambdaQueryWrapperX
                .eqIfPresent(TicketManagementDO::getTaskId, reqVO.getTaskId())
                .eqIfPresent(TicketManagementDO::getPlanId, reqVO.getPlanId())
                .eqIfPresent(TicketManagementDO::getPlanNo, reqVO.getPlanNo())
                .likeIfPresent(TicketManagementDO::getDeviceName, reqVO.getDeviceName())
                .eqIfPresent(TicketManagementDO::getPlanType, reqVO.getPlanType())
                .likeIfPresent(TicketManagementDO::getConfigName, reqVO.getConfigName())
                .eqIfPresent(TicketManagementDO::getJobStatus, reqVO.getJobStatus())
                .eqIfPresent(TicketManagementDO::getJobResult, reqVO.getJobResult())
                .likeIfPresent(TicketManagementDO::getCancelReason, reqVO.getCancelReason())
                .orderByDesc(TicketManagementDO::getId);


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


        return selectPage(reqVO,ticketManagementDOLambdaQueryWrapperX);
    }


    /**
     * 批量更新作业状态
     *
     * @param idList 工单ID列表
     * @param jobStatus 作业状态
     * @return 更新记录数
     */
    int batchUpdateJobStatus(@Param("idList") List<Long> idList,
                             @Param("jobStatus") Integer jobStatus,
                             @Param("cancelReason") String cancelReason);

}