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

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

    @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()));
        defaultHandle(pmPdRec, pmContMscTech, pmProCont, updatePmProCont, pmProContProdTtr);
        return new PmPdEventResDTO(null, updatePmProCont, null);
    }

}
