package com.youchain.modules.quartz.task;

import cn.hutool.core.date.DateUtil;
import com.youchain.annotation.AnonymousAccess;
import com.youchain.annotation.Log;
import com.youchain.businessdata.inputJson.OrderInfo;
import com.youchain.businessdata.inputJson.OrderParams;
import com.youchain.businessdata.service.MlsService;
import com.youchain.exception.handler.ApiResult;
import com.youchain.service.LogService;
import com.youchain.utils.UrlApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 获取送货单定时任务
 */

@Slf4j
@Service
@RestController
@RequestMapping("/api/orderTask")
public class orderTask {

    @Autowired
    public MlsService mlsService;

    @Autowired
    public LogService logService;


    public void orderTask() {
        long startTime = System.currentTimeMillis();
        OrderInfo orderInfo = getOrderInfo(new Date());
        synchronized ("mlsOrder"){
            String resultJson = mlsService.getOrderJson(orderInfo);
            mlsService.getOrderInfo(resultJson);
            long time = System.currentTimeMillis() - startTime;
            // 保存日志
            logService.saveLogInfo(orderInfo, UrlApi.publicApi, null, "获取送货单信息", time,"INFO");
        }
    }
    @Log("获取MLS送货单数据")
    @PostMapping(value = "/mlsOrder")
    @AnonymousAccess
    public ResponseEntity<Object> mlsOrder(@RequestBody List<Date> dateList){
        for (Date date:dateList){
            synchronized ("mlsOrder"){
                OrderInfo orderInfo = getOrderInfo(date);
                String resultJson = mlsService.getOrderJson(orderInfo);
                mlsService.getOrderInfo(resultJson);
            }
        }
        return new ResponseEntity<>(ApiResult.fail(200, "获取成功", null), HttpStatus.OK);
    }

    public OrderInfo getOrderInfo(Date date) {

        OrderParams params = new OrderParams();
        params.setOrgId(808);
        Set invCodes = new HashSet<>();
        invCodes.add("MA2111");
        params.setInvCodes(invCodes);
        params.setIqcResult("");
        String startTime = DateUtil.format(DateUtil.beginOfDay(date), "yyyy-MM-dd HH:mm:ss");
        params.setStartTime(startTime);
        String endData = DateUtil.format(date, "yyyy-MM-dd");
        params.setEndTime(endData+" 23:59:59");
        params.setPageNo(1);
        params.setPageSize(10000);
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setIfaceCode("MLS-WQ-002");
        orderInfo.setSourceSystem("WQ");
        orderInfo.setParams(params);
        return orderInfo;
    }

}
