package com.xisc.pm.app.service.strategy.pdevent;

import java.math.BigDecimal;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;

import com.xisc.pm.api.dto.PmPdEventResDTO;
import com.xisc.pm.api.dto.PmPdRecDTO;
import com.xisc.pm.domain.entity.PmContMscTech;
import com.xisc.pm.domain.entity.PmPdRec;
import com.xisc.pm.domain.entity.PmProCont;
import com.xisc.pm.domain.entity.PmProContProdTtr;
import com.xisc.pm.infra.enums.PdEventType;

public interface PdEventStrategy extends InitializingBean {

    /**
     * 履历事件处理接口
     * @param pmPdRec       履历事件
     * @param pmContMscTech
     * @param pmProCont
     */
    PmPdEventResDTO handleEvent(PmPdRec pmPdRec, PmContMscTech pmContMscTech, PmProCont pmProCont , PmProContProdTtr pmProContProdTtr);

    /**
     * 履历事件策略默认方法
     * @param pmPdRec
     * @param pmContMscTech
     * @param pmProCont
     * @param updatePmProCont
     * @param pmProContProdTtr
     */
    default void defaultHandle(PmPdRec pmPdRec, PmContMscTech pmContMscTech, PmProCont pmProCont , PmProCont updatePmProCont , PmProContProdTtr pmProContProdTtr) {
        if (!pmPdRec.getEventCode().contains("HPZ") && !PdEventType.HP45.getCode().equals(pmPdRec.getEventCode())
                && StringUtils.isNotBlank(pmPdRec.getOrderNo())) {
            BigDecimal orderUnitAimWt = pmProContProdTtr.getOrderUnitAimWt() == null ? BigDecimal.ZERO : pmProContProdTtr.getOrderUnitAimWt();
            if(updatePmProCont.getPdLackQty() != null) {
                updatePmProCont.setPdLackWt(updatePmProCont.getPdLackQty().multiply(orderUnitAimWt));
                if(StringUtils.isBlank(pmPdRec.getKeyvalue2())) {
                    pmPdRec.setKeyvalue2Desc(PmPdRecDTO.DESC_LACK_CHANGE);
                    pmPdRec.setKeyvalue2(String.format("欠 %s -> %s", pmProCont.getPdLackQty() == null ? "0":pmProCont.getPdLackQty().toPlainString()
                            , updatePmProCont.getPdLackQty().toPlainString()));
                }
            }
            if(updatePmProCont.getPdTotQty() != null) {
                updatePmProCont.setPdTotWt(updatePmProCont.getPdTotQty().multiply(orderUnitAimWt));
                if(StringUtils.isBlank(pmPdRec.getKeyvalue3())) {
                    pmPdRec.setKeyvalue3Desc(PmPdRecDTO.DESC_TOTAL_CHANGE);
                    pmPdRec.setKeyvalue3(String.format("组 %s -> %s", pmProCont.getPdTotQty() == null ? "0":pmProCont.getPdTotQty().toPlainString()
                            , updatePmProCont.getPdTotQty().toPlainString()));
                }
            }
        }
    }

}
