package cn.estsh.i3plus.ext.mes.pcn.apiservice.controller.busi;


import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesShippingLoadingCheckService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.util.MesPcnException;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.util.MesRedisLockUtil;
import cn.estsh.i3plus.ext.mes.pcn.pojo.constant.MesCommonConstant;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.MesShippingLoadingCheckModel;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.pojo.mes.bean.shipping.MesLoadingList;
import cn.estsh.impp.framework.boot.auth.AuthUtil;
import cn.estsh.impp.framework.boot.exception.ImppBusiException;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import cn.estsh.impp.framework.boot.util.ResultBean;
import cn.estsh.impp.framework.boot.util.ValidatorBean;
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.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags = "装车校验")
@RequestMapping(MesCommonConstant.MES_YANFEN + "/shipping-loading-check")
@Slf4j
public class MesShippingLoadingCheckController {

    @Autowired
    private IMesShippingLoadingCheckService shippingLoadingCheckService;

    @Autowired
    private MesRedisLockUtil redisLockUtil;

    private static final String key = ":BUSI_LOCK:SHIPPING_LOADING_CHECK:";

    @GetMapping("/query-loading-order")
    @ApiOperation(value = "查询装车单")
    public ResultBean queryLoadingOrderNo(MesLoadingList loadingVehiclesOrder) {

        Boolean lockFlg = false;
        String moduleKey = null;
        String organizeCode = null;
        String userInfo = null;
        try {

            if (StringUtils.isEmpty(loadingVehiclesOrder.getOrderCode())) MesPcnException.throwBusiException("请扫描装车单!");

            organizeCode = !StringUtils.isEmpty(loadingVehiclesOrder.getOrganizeCode()) ? loadingVehiclesOrder.getOrganizeCode() : AuthUtil.getOrganize().getOrganizeCode();

            userInfo = !StringUtils.isEmpty(loadingVehiclesOrder.getUserInfo()) ? loadingVehiclesOrder.getUserInfo() : AuthUtil.getSessionUser().getUserName();

            moduleKey = organizeCode + key + "ORDER_CODE:" + loadingVehiclesOrder.getOrderCode();

            log.info("工厂:{} 装车校验扫描装车单 --- {} --- START --- 操作人:{} --- THREAD:{}", organizeCode, moduleKey, userInfo, Thread.currentThread().getName());

            //加锁
            lockFlg = redisLockUtil.redisLock(moduleKey);

            log.info("工厂:{} 装车校验扫描装车单 --- {} --- LOCKED --- 操作人:{} --- THREAD:{}", organizeCode, moduleKey, userInfo, Thread.currentThread().getName());

            // 数据校验
            ValidatorBean.checkNotNull(loadingVehiclesOrder.getOrderCode(), "装车单不能为空");
            loadingVehiclesOrder.setOrganizeCode(organizeCode);
            ConvertBean.serviceModelUpdate(loadingVehiclesOrder,!StringUtils.isEmpty(loadingVehiclesOrder.getModifyUser()) ? loadingVehiclesOrder.getModifyUser() : AuthUtil.getSessionUser().getUserName());
            MesShippingLoadingCheckModel model = shippingLoadingCheckService.doLoadingOrderNoQuery(loadingVehiclesOrder);
            return ResultBean.success("查询成功").setResultObject(model);
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        } finally {
            //解锁
            if (lockFlg) {
                redisLockUtil.redisUnLock(lockFlg, moduleKey);
                log.info("工厂:{} 装车校验扫描装车单 --- {} --- UNLOCK --- 操作人:{} --- THREAD:{}", organizeCode, moduleKey, userInfo, Thread.currentThread().getName());
            }
        }
    }

    @PostMapping("/scan-loading-order-detail")
    @ApiOperation(value = "扫描装车单明细-发运单号")
    public ResultBean saveLoadingOrderNoDetail(@RequestBody MesShippingLoadingCheckModel model) {

        Boolean lockFlg = false;
        String moduleKey = null;
        String organizeCode = null;
        String userInfo = null;
        try {

            if (StringUtils.isEmpty(model.getShippingCode())) MesPcnException.throwBusiException("请扫描发运单!");

            organizeCode = !StringUtils.isEmpty(model.getOrganizeCode()) ? model.getOrganizeCode() : AuthUtil.getOrganize().getOrganizeCode();

            userInfo = !StringUtils.isEmpty(model.getUserInfo()) ? model.getUserInfo() : AuthUtil.getSessionUser().getUserName();

            moduleKey = organizeCode + key + "SHIPPING_CODE:" + model.getShippingCode();

            log.info("工厂:{} 装车校验扫描发运单 --- {} --- START --- 操作人:{} --- THREAD:{}", organizeCode, moduleKey, userInfo, Thread.currentThread().getName());

            //加锁
            lockFlg = redisLockUtil.redisLock(moduleKey);

            log.info("工厂:{} 装车校验扫描发运单 --- {} --- LOCKED --- 操作人:{} --- THREAD:{}", organizeCode, moduleKey, userInfo, Thread.currentThread().getName());

            // 数据校验
            ValidatorBean.checkNotNull(model.getShippingCode(), "发运单号不能为空");
            ValidatorBean.checkNotNull(model.getDetailList(), "装车单明细不能为空");

            shippingLoadingCheckService.saveVehiclesOrderAndDetail(model, organizeCode, userInfo);
            return ResultBean.success("查询成功").setResultObject(model);
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        } finally {
            //解锁
            if (lockFlg) {
                redisLockUtil.redisUnLock(lockFlg, moduleKey);
                log.info("工厂:{} 装车校验扫描发运单 --- {} --- UNLOCK --- 操作人:{} --- THREAD:{}", organizeCode, moduleKey, userInfo, Thread.currentThread().getName());
            }

        }
    }

    @GetMapping("/query-auto-push-order")
    @ApiOperation(value = "自动推单")
    public ResultBean queryAutoPushOrder(String shippingGroupNo, String organizeCode) {

        try {
            organizeCode = !StringUtils.isEmpty(organizeCode) ? organizeCode : AuthUtil.getOrganize().getOrganizeCode();
            MesLoadingList order = shippingLoadingCheckService.queryLoadingOrderNoByAutoPushOrder(shippingGroupNo, organizeCode);
            return ResultBean.success("查询成功").setResultObject(order);
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

}
