package com.wms_main.service.quartz_job.job_executor;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.wms_main.constant.enums.wcs.WcsApiResponseCodeEnums;
import com.wms_main.constant.enums.wcs.WcsConveyTaskTypeEnums;
import com.wms_main.constant.enums.wms.WmsPickTaskStatusEnum;
import com.wms_main.dao.ITAppPickTaskService;
import com.wms_main.model.bo.wcs.WcsPickTask;
import com.wms_main.model.dto.request.wcs.WcsPickTaskRequest;
import com.wms_main.model.dto.response.wcs.WcsApiResponse;
import com.wms_main.model.po.TAppPickTask;
import com.wms_main.service.api.IWcsApiService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 定时任务定义类---发送拣选任务
 */
@Slf4j
@PersistJobDataAfterExecution
// 以下注解用于实现fixed_delay
@DisallowConcurrentExecution
@RequiredArgsConstructor
public class WcsPickTaskSender implements Job {
    /**
     * 拣选任务服务
     */
    private final ITAppPickTaskService appPickTaskService;
    /**
     * Wcs接口服务
     */
    private final IWcsApiService wcsApiService;

    /**
     * 运行定时任务
     * 向Wcs发送拣选任务
     *
     * @param jobExecutionContext 定时任务执行的环境（上下文）
     */
    @Override
    public void execute(JobExecutionContext jobExecutionContext) {
        // 获取到所有待下发的拣选任务
        List<TAppPickTask> waitSendPickTaskList = appPickTaskService.list(
                new LambdaQueryWrapper<TAppPickTask>()
                        .eq(TAppPickTask::getPickStatus, WmsPickTaskStatusEnum.WAIT.getCode())
        );
        // 轮询发送任务
        for (TAppPickTask pickTask : waitSendPickTaskList) {
            // 生成请求
            WcsPickTaskRequest request = new WcsPickTaskRequest(
                    pickTask.getPickId(),
                    pickTask.getVehicleId(),
                    WcsConveyTaskTypeEnums.CONVEY_TASK_TYPE_PICK.getCode(),
                    pickTask.getPickStand(),
                    ""
            );
            try {
                WcsApiResponse<WcsPickTask> wcsResponse = wcsApiService.sendPickTask(request);
                if (wcsResponse != null && WcsApiResponseCodeEnums.SUCCESS.getCode() == wcsResponse.getCode()) {
                    // 更新这个载具这个站台的拣选任务的状态
                    appPickTaskService.update(
                            new LambdaUpdateWrapper<TAppPickTask>()
                                    .set(TAppPickTask::getPickStatus, WmsPickTaskStatusEnum.SEND.getCode())
                                    .set(TAppPickTask::getSendTime, LocalDateTime.now())
                                    .eq(TAppPickTask::getVehicleId, pickTask.getVehicleId())
                                    .eq(TAppPickTask::getPickStand, pickTask.getPickStand())
                    );
                }
            } catch (Exception e) {
                log.error("拣选任务发送失败，任务号：{}，载具号：{}，拣选站台：{}。", request.getTaskId(), request.getVehicleNo(), request.getLocation());
            }
        }
    }


}
