package cn.iocoder.yudao.module.common.dal.mysql.moldticketmanagement;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.common.controller.admin.moldticketmanagement.vo.MoldTicketManagementPageReqVO;
import cn.iocoder.yudao.module.common.dal.dataobject.moldticketmanagement.MoldTicketManagementDO;
import com.alibaba.excel.util.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

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

    default PageResult<MoldTicketManagementDO> selectPage(MoldTicketManagementPageReqVO reqVO) {

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


        // 单独处理 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(MoldTicketManagementDO::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);

}