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

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.common.dal.dataobject.moldrepair.MoldRepairDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.zjtask.ZjTaskDO;
import com.alibaba.excel.util.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.mes.controller.admin.zjtask.vo.*;
import org.apache.ibatis.annotations.Param;

/**
 * 质量管理-检验任务 Mapper
 *
 * @author 必硕科技
 */
@Mapper
public interface ZjTaskMapper extends BaseMapperX<ZjTaskDO> {

    default PageResult<ZjTaskDO> selectPage(ZjTaskPageReqVO reqVO) {
        LambdaQueryWrapperX<ZjTaskDO> zjTaskDOLambdaQueryWrapperX = new LambdaQueryWrapperX<>();
        zjTaskDOLambdaQueryWrapperX
                .eqIfPresent(ZjTaskDO::getCode, reqVO.getCode())
                .likeIfPresent(ZjTaskDO::getName, reqVO.getName())
                .eqIfPresent(ZjTaskDO::getType, reqVO.getType())
                .eqIfPresent(ZjTaskDO::getSchemaId, reqVO.getSchemaId())
                .likeIfPresent(ZjTaskDO::getSchemaName, reqVO.getSchemaName())
                .eqIfPresent(ZjTaskDO::getRemark, reqVO.getRemark())
                .eqIfPresent(ZjTaskDO::getTicket, reqVO.getTicket())
                .eqIfPresent(ZjTaskDO::getOrgType, reqVO.getOrgType())
                .eqIfPresent(ZjTaskDO::getManagerId, reqVO.getManagerId())
                .likeIfPresent(ZjTaskDO::getManagerName, reqVO.getManagerName())
                .eqIfPresent(ZjTaskDO::getExecutorId, reqVO.getExecutorId())
                .likeIfPresent(ZjTaskDO::getExecutorName, reqVO.getExecutorName())
                .eqIfPresent(ZjTaskDO::getStatus, reqVO.getStatus())
                .eqIfPresent(ZjTaskDO::getResult, reqVO.getResult())
                .betweenIfPresent(ZjTaskDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ZjTaskDO::getId);

        if (StringUtils.isNotBlank(reqVO.getIds())) {
            List<Long> idList = Arrays.stream(reqVO.getIds().split(","))
                    .map(String::trim)
                    .map(Long::valueOf)
                    .collect(Collectors.toList());
            zjTaskDOLambdaQueryWrapperX.in(ZjTaskDO::getId, idList);
        }

        return selectPage(reqVO,zjTaskDOLambdaQueryWrapperX );
    }

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

}