package com.prolog.cs.biz.outbound.controller;


import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import com.prolog.cs.basic.common.model.print.PrintParam;
import com.prolog.cs.biz.outbound.model.WmsFaceOrderConvert;
import com.prolog.cs.biz.outbound.model.dto.RePrintBulkDto;
import com.prolog.cs.biz.outbound.model.dto.print.PrintAllVO;
import com.prolog.cs.biz.outbound.model.entity.PrintLog;
import com.prolog.cs.biz.outbound.service.FaceOrderService;
import com.prolog.cs.biz.outbound.service.PickTaskAppService;
import com.prolog.cs.biz.outbound.service.PickTaskService;
import com.prolog.cs.biz.outbound.service.PrintLogService;
import com.prolog.framework.authority.util.HystrixVarUtil;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.log.aop.annotation.UserLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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 org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Api(tags = "面单")
@Slf4j
@RestController
@RequestMapping("/faceOrder")
public class FaceOrderController {

    @Autowired
    private FaceOrderService faceOrderService;


    @Autowired
    private PickTaskService pickTaskService;

    @Autowired
    private PickTaskAppService pickTaskAppService;
    @Autowired
    private PrintLogService printLogService;

    @UserLog
    @ApiOperation(value = "面单取消批量")
    @PostMapping("/v1/cancel")
    public RestMessage<?> faceCancel(@RequestBody List<String> wayBillIds) {
        faceOrderService.faceCancel(wayBillIds, true);
        return RestMessage.success("取消成功", null);
    }


    @UserLog
    @ApiOperation(value = "重选承运商")
    @PostMapping("/v1/convert/carrier")
    public RestMessage<Object> convertCarrier(@RequestBody WmsFaceOrderConvert wmsFaceOrderConvert) {
        Map<String, Object> map = faceOrderService.convertCarrier(wmsFaceOrderConvert);
        Object data = map.get("printData");
        if(data != null){
            ServletRequestAttributes reqa = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
            new Thread(()-> {
                RequestContextHolder.setRequestAttributes(reqa, false);
                ServletRequestAttributes servletRequestAttributes =
                        (ServletRequestAttributes)RequestContextHolder.currentRequestAttributes();
                String userkey = servletRequestAttributes.getRequest().getHeader("UserKey");

                if (!HystrixRequestContext.isCurrentThreadInitialized()) {
                    HystrixRequestContext.initializeContext();
                }
                HystrixVarUtil.setUserKey(userkey);
                try {
                    List<PrintParam> printData = (List<PrintParam>) data;
                    RestMessage<?> restMessage = pickTaskAppService.print(printData);
                    //保存打印日志
                    List<PrintLog> printLogs = new ArrayList<>();
                    for (PrintParam printDatum : printData) {
                        PrintLog printLog = new PrintLog();
                        printLog.setPrintCode("faceOrder");
                        printLog.setBillno(wmsFaceOrderConvert.getBillNo());
                        printLog.setAttrStr3(printDatum.getParams().get("waybillCode").toString());
                        if(restMessage.isSuccess()){
                            printLog.setStatus("200");
                        }else {
                            printLog.setStatus("0");
                        }
                        printLogs.add(printLog);
                    }
                    printLogService.saveBatch(printLogs);
                }catch (Exception e) {
                    log.error("打印面单/标签失败：{}", e.getMessage());
                }finally {
                    RequestContextHolder.resetRequestAttributes();
                }
            }).start();
        }
        return RestMessage.success("操作成功", null);
    }

/*    @UserLog
    @ApiOperation(value = "获取承运商列表")
    @PostMapping("/v1/pageCarrier")
    public RestMessage<Page<PageCarrierVO>> pageCarrier(@RequestBody PageCarrierVO pageCarrierVO) {
        return RestMessage.newInstance(true, "查询成功", faceOrderService.pageCarrier(pageCarrierVO));
    }*/
}
