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

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
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.domain.repository.PmProContRepository;
import com.xisc.pm.infra.enums.PdEventType;

/**
 * 成品充当挂合同事件，策略处理类
 * 
 * @author jun.zeng
 *
 */
@Component
public class PdEventStrategyHp46 implements PdEventStrategy {

    @Autowired
    private PmProContRepository pmProContRepository;

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

    /**
     * (10) 若event_code = HP46, 对应成品充当挂合同事件，修改生产合同主表组板工序欠量PD_LACK_QTY： a) 组板工序欠量 =
     * 组板工序欠量 – 件数，若结果小于等于0，则默认组板工序欠件=0，修改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());

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

}
