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

import com.kingdee.bos.Context;
import com.kingdee.bos.metadata.entity.*;
import com.kingdee.eas.basedata.org.AdminOrgUnitInfo;
import com.kingdee.eas.basedata.person.PersonInfo;
import com.kingdee.eas.custom.projectbonus.*;
import com.kingdee.eas.custom.projectbonus.bizEnum.BusDepProjectStateEnum;
import com.kingdee.eas.custom.projectbonus.bizEnum.CycleStateEnum;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.jdbc.rowset.IRowSet;
import com.kingdee.shr.base.syssetting.app.io.fileImport.BaseImportService;
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.math.BigDecimal;


/**
 * @Description 分配人员明细列表历史数据导入模板(专员)
 * @Date 2024/5/7 21:28
 * @Created by Heyuan
 */
public class PersonnelDetailHistoryImportService extends BaseImportService {

    /**
     * 校验对象
     *
     * @param coreBaseInfo
     * @throws ImportException
     */
    @Override
    protected void verifyRowBiz(CoreBaseInfo coreBaseInfo) throws ImportException {
        BonusCycleBusDepProjectAssPersonnelDetailInfo info = (BonusCycleBusDepProjectAssPersonnelDetailInfo) coreBaseInfo;
        BonusCycleBusDepProjectInfo parent = info.getParent();
        if (parent != null) {
            CycleStateEnum cycleState = parent.getParent().getState();
            if (CycleStateEnum.LOCK.equals(cycleState)) {
                throw new ImportException("周期已锁定，不允许执行导入操作！");
            }
        } else {
            throw new ImportException("事业部项目不存在!");
        }
    }

    /**
     * @param baseInfo
     * @param row
     * @throws ImportException
     */
    @Override
    protected void setOtherFieldInfo(CoreBaseInfo baseInfo,
                                     BaseRowInfo row) throws ImportException {
        try {
            Context ctx = this.getContext();
            BonusCycleBusDepProjectAssPersonnelDetailInfo info = (BonusCycleBusDepProjectAssPersonnelDetailInfo) baseInfo;
            AdminOrgUnitInfo busDep = (AdminOrgUnitInfo) row.getValueOfObject("BonusCycleBusDepProject.busDep");
            AdminOrgUnitInfo project = (AdminOrgUnitInfo) row.getValueOfObject("BonusCycleBusDepProject.project");
            PersonInfo principal = (PersonInfo) row.getValueOfObject("BonusCycleBusDepProject.principal");//负责人
            BigDecimal bonusAmount = (BigDecimal) row.getValueOfObject("BonusCycleBusDepProject.bonusAmount");//奖金分包金额
            String cycleName = row.getValueOfString("BonusCycle.cycleName");
            IBonusCycleBusDepProject iBonusCycleBusDepProject = BonusCycleBusDepProjectFactory.getLocalInstance(ctx);
            String querySql = "select fid from CT_PB_BonusCycle where cfcycleName='" + cycleName + "'";
            IRowSet iRowSet = DbUtil.executeQuery(ctx, querySql);
            String fid = null;
            while (iRowSet.next()) {
                fid = iRowSet.getString("fid");
            }
            if (StringUtils.isEmpty(fid)) {
                throw new ImportException("未找到周期[" + cycleName + "]");
            }
            FilterInfo filterInfo = new FilterInfo();
            FilterItemCollection filterItems = filterInfo.getFilterItems();
            filterItems.add(new FilterItemInfo("busDep", busDep.getId().toString()));
            filterItems.add(new FilterItemInfo("parent.id", fid));
            if (project != null) {
                filterItems.add(new FilterItemInfo("project", project.getId().toString()));
            }
            SelectorItemCollection sic = new SelectorItemCollection();
            sic.add("parent.state");//周期锁定状态
            EntityViewInfo entityViewInfo = EntityViewInfo.getInstance(filterInfo, sic, null);
            BonusCycleBusDepProjectCollection busDepProjectCols =
                    iBonusCycleBusDepProject.getBonusCycleBusDepProjectCollection(entityViewInfo);
            if (busDepProjectCols.size() > 0) {
                BonusCycleBusDepProjectInfo depProjectInfo = busDepProjectCols.get(0);
                depProjectInfo.setPrincipal(principal);
                depProjectInfo.setBonusAmount(bonusAmount);
                info.setParent(depProjectInfo);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new ImportException(e.getMessage());
        }
    }

    /**
     * 获取主键条件字符串
     *
     * @param row
     * @return
     */
    @Override
    protected String getPKSelectString(BaseRowInfo row) {
        String pkSelectString = super.getPKSelectString(row);
        pkSelectString = pkSelectString.replace("BonusCycle.", "parent.parent.");
        pkSelectString = pkSelectString.replace("BonusCycleBusDepProject.", "parent.");
        StringBuilder filter = new StringBuilder(pkSelectString);
        AdminOrgUnitInfo project = (AdminOrgUnitInfo) row.getValueOfObject("BonusCycleBusDepProject.project");
        if (project != null) {
            filter.append(" and parent.project.id ='").append(project.getId().toString()).append("'");
        }
        return filter.toString();
    }

    /**
     * 写入后执行
     *
     * @param coreBaseInfo
     * @throws ImportException
     */
    @Override
    protected void afterSubmitData(CoreBaseInfo coreBaseInfo) throws ImportException {
        Context ctx = this.getContext();
        super.afterSubmitData(coreBaseInfo);
        try {
            //将事业部项目状态更新为已复核
            BonusCycleBusDepProjectAssPersonnelDetailInfo info = (BonusCycleBusDepProjectAssPersonnelDetailInfo) coreBaseInfo;
            BonusCycleBusDepProjectInfo parent = info.getParent();
            parent.setState(BusDepProjectStateEnum.RECHECK);
            SelectorItemCollection updateSic = new SelectorItemCollection();
            updateSic.add("state");
            updateSic.add("principal");
            updateSic.add("bonusAmount");
            BonusCycleBusDepProjectFactory.getLocalInstance(ctx).updatePartial(parent, updateSic);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ImportException(e.getMessage());
        }
    }
}
