package com.prolog.rdc.tms.app.service.flow.handle;

import com.prolog.framework.core.exception.BizException;
import com.prolog.rdc.tms.app.constant.DataDictionaryConstant;
import com.prolog.rdc.tms.app.model.entity.ShipbillNodeTracking;
import com.prolog.rdc.tms.app.model.entity.TmsLoadingBillDetailModel;
import com.prolog.rdc.tms.app.model.entity.TmsLoadingBillHeaderModel;
import com.prolog.rdc.tms.app.model.entity.TmsShipbillHeaderModel;
import com.prolog.rdc.tms.app.model.entity.TmsShipbillOperateLogModel;
import com.prolog.rdc.tms.app.model.entity.TmsShipbillTransLogModel;
import com.prolog.rdc.tms.app.service.flow.FlowContext;
import com.prolog.rdc.tms.app.service.flow.FlowNodeHandle;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 退货运单调度审核
 * @author hongbo.pan
 * @date 2022/10/17
 */
@Component
public class ReturnShipBillCheckHandle extends FlowNodeHandle {


    @Override
    public void doHandle(FlowContext context) {
        if (!DataDictionaryConstant.LOADING_BILL_STATUS_WSH.equals(context.getTmsLoadingBillHeaderModel().getStatus())) {
            throw new BizException("派车单状态不是未审核，loadingBillId=" + context.getTmsLoadingBillHeaderModel().getLoadingBillId());
        }
        TmsShipbillHeaderModel tmsShipbillHeaderModel = context.getTmsShipBillHeaderModel();
        tmsShipbillHeaderModel.setStatus(DataDictionaryConstant.SHIPBILL_STATUS_QY);
        tmsShipbillHeaderModel.setUpdateperson(context.getUserEntity().getNickname());
        tmsShipbillHeaderModel.setUpdatetime(new Date());
        tmsShipbillHeaderMapper.update(tmsShipbillHeaderModel);
        context.setTmsShipBillHeaderModel(tmsShipbillHeaderModel);

        TmsLoadingBillHeaderModel tmsLoadingBillHeaderModel = context.getTmsLoadingBillHeaderModel();
        tmsLoadingBillHeaderModel.setStatus(DataDictionaryConstant.LOADING_BILL_STATUS_YSH);
        tmsLoadingBillHeaderModel.setUpdateperson(context.getUserEntity().getNickname());
        tmsLoadingBillHeaderModel.setUpdatetime(new Date());
        tmsLoadingBillHeaderMapper.update(tmsLoadingBillHeaderModel);
        context.setTmsLoadingBillHeaderModel(tmsLoadingBillHeaderModel);

        List<TmsLoadingBillDetailModel> tmsLoadingBillDetailModelList = context.getTmsLoadingBillDetailModelList();
        tmsLoadingBillDetailModelList.forEach(detailModel -> {
            detailModel.setStatus(DataDictionaryConstant.SHIPBILL_STATUS_QY);
            detailModel.setUpdateperson(context.getUserEntity().getNickname());
            detailModel.setUpdatetime(new Date());
            tmsLoadingBillDetailMapper.update(detailModel);
        });
        context.setTmsLoadingBillDetailModelList(tmsLoadingBillDetailModelList);
    }

    @Override
    public String getFlowNode() {
        return DataDictionaryConstant.FLOW_CONFIG_COMMIT;
    }

    @Override
    public ShipbillNodeTracking buildShipBillNodeTracking() {
        ShipbillNodeTracking shipbillNodeTracking = new ShipbillNodeTracking();
        shipbillNodeTracking.setStatus(DataDictionaryConstant.SHIPBILL_STATUS_QY);
        shipbillNodeTracking.setNodeEvent(DataDictionaryConstant.SHIPBILL_NODE_STATUSNAME_PC);
        return shipbillNodeTracking;
    }

    @Override
    public TmsShipbillOperateLogModel buildShipBillOperateLog() {
        TmsShipbillOperateLogModel tmsShipbillOperateLogModel = new TmsShipbillOperateLogModel();
        tmsShipbillOperateLogModel.setOperateEvent(DataDictionaryConstant.SHIPBILL_NODE_STATUSNAME_PC);
        tmsShipbillOperateLogModel.setOldStatus(DataDictionaryConstant.SHIPBILL_STATUS_DISPATCH);
        tmsShipbillOperateLogModel.setNewStatus(DataDictionaryConstant.SHIPBILL_STATUS_QY);
        return tmsShipbillOperateLogModel;
    }

    @Override
    public TmsShipbillTransLogModel buildShipBillTransLog() {
        TmsShipbillTransLogModel tmsShipbillTransLogModel = new TmsShipbillTransLogModel();
        tmsShipbillTransLogModel.setDisCheckDt(new Date());
        return tmsShipbillTransLogModel;
    }
}
