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

import com.xisc.boot.common.feign.pojo.MtUserInfo;
import com.xisc.boot.common.sys.UserClient;
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;
import io.choerodon.core.exception.CommonException;
import org.hzero.core.base.BaseConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Map;

/**
 * 组板合同组板释放事件，策略处理类
 * @author jun.zeng
 *
 */
@Component
public class PdEventStrategyHp05 implements PdEventStrategy {

    @Autowired
    private UserClient userClient;

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

    /**
     * (3)  若event_code = HP05, 修改生产合同主表的正在组板标记，置合同未组板中：
     * a)  检查组板合同正在组板释放条件，根据合同号从生产合同主表PM_PRO_CONT获取正在组板标记ON_PD_FLAG、
     *  组板程序PD_PROGRAM、组板计划员ID PD_PLANNER。校验：若合同正在组板标记为1，且（组板计划员ID与事件责任者不相等），
     *  则返回消息：合同【order_no】正在被责任者【PD_PROGRAM】程序【api_code】锁定，不能被【rec_creator】解锁；
     * b)  若通过校验，且ON_PD_FLAG=1，置合同未组板中：修改生产合同主表PM_PRO_CONT正在组板标记ON_PD_FLAG=0, 
     *  组板计划员ID PD_PLANER=‘ ’, 组板程序PD_PROGRAM = ‘ ’, who字段修改；

     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PmPdEventResDTO handleEvent(PmPdRec pmPdRec, PmContMscTech pmContMscTech, PmProCont pmProCont, PmProContProdTtr pmProContProdTtr) {
        
        if(pmProCont.getOnPdFlag() != null && BaseConstants.Flag.YES.longValue() == pmProCont.getOnPdFlag().longValue()) {
            // 合同【order_no】 正在被责任者【rec_creator】程序【api_code】组板，不能使用!
            if(!pmPdRec.getEventMaker().equals(pmProCont.getPdPlanner()) || !pmPdRec.getApiCode().equals(pmProCont.getPdProgram())) {
                Map<Long, MtUserInfo> mtUserInfoMap = userClient.userInfoBatchGet(pmProCont.getTenantId(),
                        Arrays.asList(Long.parseLong(pmProCont.getPdPlanner())));
                String loginName = mtUserInfoMap.containsKey(Long.parseLong(pmProCont.getPdPlanner())) ?
                        mtUserInfoMap.get(Long.parseLong(pmProCont.getPdPlanner())).getLoginName(): pmProCont.getPdPlanner();
                throw new CommonException("pm.pm_pd_rec_006" ,pmProCont.getOrderNo() , loginName, pmProCont.getPdProgram());
            }
        }
        
        PmProCont updatePmProCont = new PmProCont();
        updatePmProCont.setObjectVersionNumber(pmProCont.getObjectVersionNumber());
        updatePmProCont.setProContId(pmProCont.getProContId());
        updatePmProCont.setOnPdFlag(BaseConstants.Flag.NO.longValue());
        updatePmProCont.setPdPlanner("");
        updatePmProCont.setPdProgram("");
        defaultHandle(pmPdRec, pmContMscTech, pmProCont, updatePmProCont, pmProContProdTtr);
        return new PmPdEventResDTO(null, updatePmProCont, null);
    }

}
