package com.kingdee.eas.custom.expandassess.service;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.metadata.entity.*;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.expandassess.*;
import com.kingdee.eas.custom.expandassess.app.AssessRatingEnum;
import com.kingdee.eas.custom.expandassess.app.ExpandStatusEunm;
import com.kingdee.eas.custom.performancenew.app.scoreEnum;
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.BaseImportService;
import com.kingdee.shr.base.syssetting.app.io.fileImport.BaseRowInfo;
import com.kingdee.shr.base.syssetting.app.io.fileImport.ImportException;
import com.kingdee.shr.ml.util.SHRServerResource;
import com.kingdee.util.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

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

/**
 * @author qingwu
 * @date 2024/5/17
 * @apiNote 拓展人员考核 专员修改模板导入
 */
public class ExpandAssessEntryTapeDataImportService extends BaseImportService {

    private static final SelectorItemCollection SIC = new SelectorItemCollection();

    static {
        SIC.add("adminorg.name");
        SIC.add("assessor.name");
        SIC.add("principal.name");
        SIC.add("threshold");
        SIC.add("target");
        SIC.add("challenge");
        SIC.add("checkRate");
        SIC.add("remark");
    }


    @Override
    protected void submitData(CoreBaseInfo coreBaseInfo) throws ImportException {
        try {
            IExpandAssessEntry iExpandAssessEntry = ExpandAssessEntryFactory.getLocalInstance(getContext());
            ExpandAssessEntryInfo expandAssessEntryInfo = (ExpandAssessEntryInfo) coreBaseInfo;
            ExpandStatusEunm asseStatus = expandAssessEntryInfo.getAsseStatus();
            SelectorItemCollection updateSic = getUpdateSic(asseStatus);
            iExpandAssessEntry.updatePartial(coreBaseInfo, updateSic);
        } catch (EASBizException var4) {
            ImportException importException = new ImportException(var4.getMessage(getContext().getLocale()), (Throwable) var4);
            throw importException;
        } catch (BOSException var5) {
            ImportException importException = new ImportException(SHRServerResource.getString("com.kingdee.shr.base.syssetting.CommonserviceResource", "save_fails_bos", getContext()), (Throwable) var5);
            throw importException;
        } catch (Exception var6) {
            ImportException importException = new ImportException(SHRServerResource.getString("com.kingdee.shr.base.syssetting.CommonserviceResource", "save_submit_fails", getContext()), var6);
            throw importException;
        }
    }

    /**
     * 模板生成完成后执行内容
     *
     * @param columnInfoMap
     * @param wb
     * @param sheet
     */
    @Override
    public void completeGenerateExcel(Map<String, BaseColumnInfo> columnInfoMap, XSSFWorkbook wb, XSSFSheet sheet) {
        Context ctx = getContext();
        String parentId = getCustomParam("parentId");
        if (StringUtils.isEmpty(parentId)) {
            throw new RuntimeException("拓展人员考核周期ID不可为空！！");
        }
        int beginRow = 4;
        try {
            //当前登录员工id
            String currentPersonId = ContextUtil.getCurrentUserInfo(ctx).getPerson().getId().toString();
            //过滤当前周期状态为已启用排序评分人等于登录人的数据
            FilterInfo filterInfo = new FilterInfo();
            FilterItemCollection filterItems = filterInfo.getFilterItems();
            filterItems.add(new FilterItemInfo("parent.id", parentId));
            //SorterItemCollection sorterItemCollection = new SorterItemCollection();
            //sorterItemCollection.add(new SorterItemInfo("rankings"));
            EntityViewInfo entityViewInfo = EntityViewInfo.getInstance(filterInfo, SIC, null);
            IExpandAssessEntry iExpandAssessEntry = ExpandAssessEntryFactory.getLocalInstance(getContext());
            //绩效考核分录实体
            ExpandAssessEntryCollection entryCollection = iExpandAssessEntry.getExpandAssessEntryCollection(entityViewInfo);
            Map<Integer, String> columnIndexMap = getColumnIndexFn(columnInfoMap);
            int fillColumnCount = columnInfoMap.size();
            for (int i = 0; i < entryCollection.size(); i++) {
                XSSFRow xSSFRow = sheet.createRow(i + beginRow);
                ExpandAssessEntryInfo expandAssessEntryInfo = entryCollection.get(i);
                for (int j = 0; j < fillColumnCount; j++) {
                    String value = null;
                    Cell createCell = xSSFRow.createCell(j);
                    String columnName = columnIndexMap.get(Integer.valueOf(j));
                    Object obj = expandAssessEntryInfo.get(columnName);
                    if (obj != null) {
                        if (obj instanceof CoreBaseInfo) {
                            CoreBaseInfo coreBaseInfo = (CoreBaseInfo) expandAssessEntryInfo.get(columnName);
                            //value = coreBaseInfo.get("number") + "##" + coreBaseInfo.get("name");
                            value = (String) coreBaseInfo.get("name");
                        } else if (obj instanceof BigDecimal) {
                            BigDecimal bigDecimal = (BigDecimal) obj;
                            value = bigDecimal.setScale(2).toPlainString();
                        } else {
                            if ("checkRate".equals(columnName)) {
                                value = AssessRatingEnum.getEnum((String) obj).getAlias();
                            } else {
                                value = obj.toString();
                            }
                        }
                    }
                    createCell.setCellValue(value);
                }
            }
        } catch (BOSException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    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(Integer.valueOf(var4++), key);
        }
        return columnIndexMap;
    }

    @Override
    protected String getPKSelectString(BaseRowInfo row) {
        String pkSelectString = super.getPKSelectString(row);
        String parentId = getCustomParam("parentId");
        if (StringUtils.isEmpty(parentId)) {
            throw new RuntimeException("拓展人员考核周期ID不可为空！！");
        }
        return pkSelectString + " and parent.id ='" + parentId + "'";
    }

    public SelectorItemCollection getUpdateSic(ExpandStatusEunm asseStatus) {
        SelectorItemCollection updateSic = new SelectorItemCollection();
        updateSic.add("remark");
        updateSic.add("checkRate");
        if (asseStatus == ExpandStatusEunm.UNSTART) {
            updateSic.add("assessor");
            updateSic.add("adminOrg");
            updateSic.add("threshold");
            updateSic.add("target");
            updateSic.add("challenge");
            updateSic.add("principal");
        }
        return updateSic;

    }
}
