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 ReturnShipBillReviewHandle extends FlowNodeHandle {


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

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

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

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

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