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

import java.math.BigDecimal;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.xisc.pm.api.dto.PmPdEventResDTO;
import com.xisc.pm.app.service.strategy.PdEventStrategyFactory;
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;

/**
 * 连铸出钢材组板完成事件，策略处理类
 * 
 * @author jun.zeng
 *
 */
@Component
public class PdEventStrategyHp16 implements PdEventStrategy {

    @Override
    public void afterPropertiesSet() throws Exception {
        PdEventStrategyFactory.register(PdEventType.HP16, this);
    }

    /**
     * (8) 若event_code = HP16,
     * 对应连铸出钢材组板完成事件，根据合同号修改生产合同主表组板欠件PD_LACK_QTY、组板总件数PD_TOT_QTY，
     * 根据合同号、产线号修改合同产线工艺参数表连铸出钢材组板合同小板数CC_PD_QTY： a) 组板欠件 = 组板欠件 - 件数， 组板总件数 = 组板总件数
     * + 件数， 连铸出钢材组板合同小板数 = 连铸出钢材组板合同小板数 + 件数，修改who字段；
     * 
     * 
     * 
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PmPdEventResDTO handleEvent(PmPdRec pmPdRec, PmContMscTech pmContMscTech, PmProCont pmProCont, PmProContProdTtr pmProContProdTtr) {

        PmProCont updatePmProCont = new PmProCont();
        updatePmProCont.setObjectVersionNumber(pmProCont.getObjectVersionNumber());
        updatePmProCont.setProContId(pmProCont.getProContId());

        BigDecimal pdTotQty = pmProCont.getPdTotQty() == null ? BigDecimal.ZERO : pmProCont.getPdTotQty();

        // 组板欠件 = 组板欠件 - 件数
        BigDecimal pdLackQty = pmProCont.getPdLackQty() == null ? BigDecimal.ZERO : pmProCont.getPdLackQty();
        pdLackQty = pdLackQty.subtract(pmPdRec.getQty());

        updatePmProCont.setPdLackQty(pdLackQty);
        // 组板总件数 = 组板总件数 + 件数
        updatePmProCont.setPdTotQty(pdTotQty.add(pmPdRec.getQty()));

        // 余材板坯组板合同小板数 = 余材板坯组板合同小板数 + 件数
        BigDecimal ccPdQty = pmContMscTech.getCcPdQty() == null ? BigDecimal.ZERO : pmContMscTech.getCcPdQty();
        ccPdQty = ccPdQty.add(pmPdRec.getQty());

        PmContMscTech updatePmContMscTech = new PmContMscTech();
        updatePmContMscTech.setObjectVersionNumber(pmContMscTech.getObjectVersionNumber());
        updatePmContMscTech.setContMscId(pmContMscTech.getContMscId());
        updatePmContMscTech.setCcPdQty(ccPdQty);
        defaultHandle(pmPdRec, pmContMscTech, pmProCont, updatePmProCont, pmProContProdTtr);
        return new PmPdEventResDTO(updatePmContMscTech, updatePmProCont, null);
    }

}
