package com.kingdee.eas.custom.projectbonus.web.service;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.metadata.entity.*;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.basedata.org.AdminOrgUnitInfo;
import com.kingdee.eas.basedata.person.PersonInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.projectbonus.*;
import com.kingdee.eas.custom.projectbonus.bizEnum.BusDepProjectStateEnum;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.util.app.ContextUtil;
import com.kingdee.shr.base.syssetting.app.io.fileImport.BaseColumnInfo;
import com.kingdee.shr.base.syssetting.app.io.fileImport.BaseRowInfo;
import com.kingdee.shr.base.syssetting.app.io.fileImport.ImportException;
import com.kingdee.util.StringUtils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


/**
 * @Description 分配人员明细列表导入模板(负责人)_新增导入
 * @Date 2024/5/7 21:28
 * @Created by Heyuan
 */
public class PrincipalDetailImportService extends ProjectBonusImportService {
    /**
     * 保存前置方法
     *
     * @param coreBaseInfo
     * @throws ImportException
     */
    @Override
    protected void beforeSubmitData(CoreBaseInfo coreBaseInfo) throws ImportException {
        String busDepId = this.getCustomParam("parentId");
        if (StringUtils.isEmpty(busDepId)) {
            throw new RuntimeException("事业部项目id不能为空!");
        }
        super.beforeSubmitData(coreBaseInfo);
        BonusCycleBusDepProjectAssPersonnelDetailInfo info = (BonusCycleBusDepProjectAssPersonnelDetailInfo) coreBaseInfo;
        info.put("parent", busDepId);
    }

    /**
     * 获取主键条件字符串
     *
     * @param row
     * @return
     */
    @Override
    protected String getPKSelectString(BaseRowInfo row) {
        String busDepId = this.getCustomParam("parentId");
        if (StringUtils.isEmpty(busDepId)) {
            throw new RuntimeException("事业部项目id不能为空!");
        }
        String pkSelectString = super.getPKSelectString(row);
        StringBuilder pkString = new StringBuilder(pkSelectString);
        pkString.append(" and parent.id ='").append(busDepId).append("' ");
        return pkString.toString();
    }

    /**
     * 设置其他字段
     *
     * @param baseInfo
     * @param row
     * @throws ImportException
     */
//    protected void setOtherFieldInfo(CoreBaseInfo baseInfo, BaseRowInfo row) throws ImportException {
//        String busDepId = this.getCustomParam("parentId");
//        if (StringUtils.isEmpty(busDepId)) {
//            throw new RuntimeException("事业部项目id不能为空!");
//        }
//        BonusCycleBusDepProjectAssPersonnelDetailInfo info = (BonusCycleBusDepProjectAssPersonnelDetailInfo) baseInfo;
//        try {
//            Context ctx = this.getContext();
//            IBonusCycleBusDepProject iBonusCycleBusDepProject = BonusCycleBusDepProjectFactory.getLocalInstance(ctx);
//            SelectorItemCollection sic = new SelectorItemCollection();
//            sic.add("*");
//            sic.add("busDep.name");
//            sic.add("busDep.number");
//            sic.add("project.name");
//            sic.add("project.number");
//            BonusCycleBusDepProjectInfo depProjectInfo = iBonusCycleBusDepProject.getBonusCycleBusDepProjectInfo(new ObjectUuidPK(busDepId));
//            info.setParent(depProjectInfo);
//        } catch (Exception e) {
//            e.printStackTrace();
//            throw new ImportException(e.getMessage());
//        }
//    }

    /**
     * 校验对象
     *
     * @param coreBaseInfo
     * @throws ImportException
     */
    protected void verifyRowBiz(CoreBaseInfo coreBaseInfo) throws ImportException {
        String busDepId = this.getCustomParam("parentId");
        if (StringUtils.isEmpty(busDepId)) {
            throw new RuntimeException("事业部项目id不能为空!");
        }
        BonusCycleBusDepProjectAssPersonnelDetailInfo info = (BonusCycleBusDepProjectAssPersonnelDetailInfo) coreBaseInfo;
        Context ctx = this.getContext();
        SelectorItemCollection sic = new SelectorItemCollection();
        sic.add("*");
        sic.add("busDep.name");
        sic.add("busDep.number");
        sic.add("project.name");
        sic.add("project.number");
        BonusCycleBusDepProjectInfo depProjectInfo = null;
        try {
            IBonusCycleBusDepProject iBonusCycleBusDepProject = BonusCycleBusDepProjectFactory.getLocalInstance(ctx);
            depProjectInfo = iBonusCycleBusDepProject.getBonusCycleBusDepProjectInfo(new ObjectUuidPK(busDepId), sic);
        } catch (BOSException e) {
            e.printStackTrace();
            throw new ImportException(e.getMessage());
        } catch (EASBizException e) {
            e.printStackTrace();
            throw new ImportException(e.getMessage());
        }
        if (depProjectInfo == null) {
            throw new ImportException("事业部项目数据未找到!");
        }
        //事业部状态
        BusDepProjectStateEnum state = depProjectInfo.getState();
        //负责人
        PersonInfo principal = depProjectInfo.getPrincipal();
        StringBuilder errorMsg = new StringBuilder();
        AdminOrgUnitInfo project = depProjectInfo.getProject();
        if (project != null) {
            String projectName = project.getName();
            errorMsg.append("项目[").append(projectName);
        } else {
            String busDepName = depProjectInfo.getBusDep().getName();
            errorMsg.append("事业部[").append(busDepName);
        }
        if (!BusDepProjectStateEnum.ENABLE.equals(state)) {
            errorMsg.append("]非启用状态，不允许新增数据");
            throw new ImportException(errorMsg.toString());
        }
        //当前登录人
        PersonInfo personInfo = ContextUtil.getCurrentUserInfo(ctx).getPerson();
        String personId = personInfo.getId().toString();
        if (!personId.equals(principal.getId().toString())) {
            errorMsg.append("]的负责人不是当前操作人，不允许新增数据");
            throw new ImportException(errorMsg.toString());
        }
    }

    protected void submitData(CoreBaseInfo coreBaseInfo) throws ImportException {
        try {
            coreBaseInfo.put("productionMode", this.getTask().isProductionMode());
            IObjectPK pk = this.getCoreBase().save(coreBaseInfo);
            coreBaseInfo.setId(BOSUuid.read(pk.toString()));
        } catch (Exception e) {
            e.printStackTrace();
            throw new ImportException(e.getMessage());
        }
    }

    /**
     * 将列对象数据转化成Map集合
     *
     * @param columnInfoMap
     * @return
     */
    protected Map<Integer, String> getColumnIndexFn(Map<String, BaseColumnInfo> columnInfoMap) {
        Map<Integer, String> columnIndexMap = new HashMap();
        Iterator<String> keys = columnInfoMap.keySet().iterator();
        int var4 = 0;
        while (keys.hasNext()) {
            String key = keys.next();
            columnIndexMap.put(var4++, key);
        }
        return columnIndexMap;
    }
}
