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.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;

/**
 * 退货运单提交
 * @author hongbo.pan
 * @date 2022/10/17
 */
@Component
public class ReturnShipBillCommitHandle extends FlowNodeHandle {


    @Override
    public void doHandle(FlowContext context) {
        if (!DataDictionaryConstant.SHIPBILL_STATUS_CSH.equals(context.getTmsShipBillHeaderModel().getStatus())) {
            throw new BizException("运单状态不是初始化，shipBillId=" + context.getTmsShipBillHeaderModel().getShipbillId());
        }
        TmsShipbillHeaderModel tmsShipbillHeaderModel = context.getTmsShipBillHeaderModel();
        tmsShipbillHeaderModel.setStatus(DataDictionaryConstant.SHIPBILL_STATUS_YTJ);
        tmsShipbillHeaderModel.setUpdateperson(context.getUserEntity().getNickname());
        tmsShipbillHeaderModel.setUpdatetime(new Date());
        tmsShipbillHeaderMapper.update(tmsShipbillHeaderModel);
        context.setTmsShipBillHeaderModel(tmsShipbillHeaderModel);
    }

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

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

    @Override
    public TmsShipbillOperateLogModel buildShipBillOperateLog() {
        TmsShipbillOperateLogModel tmsShipbillOperateLogModel = new TmsShipbillOperateLogModel();
        tmsShipbillOperateLogModel.setOperateEvent(DataDictionaryConstant.SHIPBILL_STATUSNAME_YTJ);
        tmsShipbillOperateLogModel.setOldStatus(DataDictionaryConstant.SHIPBILL_STATUS_CSH);
        tmsShipbillOperateLogModel.setNewStatus(DataDictionaryConstant.SHIPBILL_STATUS_YTJ);
        return tmsShipbillOperateLogModel;
    }

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