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 PdEventStrategyHp10 implements PdEventStrategy {

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

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

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

        // 组板总件数 = 组板总件数 + 件数
        BigDecimal pdTotQty = pmProCont.getPdTotQty() == null ? BigDecimal.ZERO : pmProCont.getPdTotQty();
        pdTotQty = pdTotQty.add(pmPdRec.getQty());

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

        PmContMscTech updatePmContMscTech = new PmContMscTech();
        updatePmContMscTech.setObjectVersionNumber(pmContMscTech.getObjectVersionNumber());
        updatePmContMscTech.setContMscId(pmContMscTech.getContMscId());
        updatePmContMscTech.setSlabPdQty(slabPdQty);

        PmProCont updatePmProCont = new PmProCont();
        updatePmProCont.setObjectVersionNumber(pmProCont.getObjectVersionNumber());
        updatePmProCont.setProContId(pmProCont.getProContId());
        updatePmProCont.setPdLackQty(pdLackQty);
        updatePmProCont.setPdTotQty(pdTotQty);
        defaultHandle(pmPdRec, pmContMscTech, pmProCont, updatePmProCont, pmProContProdTtr);
        return new PmPdEventResDTO(updatePmContMscTech, updatePmProCont, null);
    }

}
