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

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.SorterItemCollection;
import com.kingdee.bos.metadata.entity.SorterItemInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.performancenew.*;
import com.kingdee.eas.custom.performancenew.app.scoreEnum;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.shr.base.syssetting.app.io.fileImport.*;
import com.kingdee.shr.ml.util.SHRServerResource;
import com.kingdee.shr.shrimport.SHRFileTemplateInfo;
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.*;

/**
 * @author qingwu
 * @date 2024/5/7
 * @apiNote 组织绩效与个人绩效分布对应关系表 带数据导入
 */
public class PersonRelationOrgImportService extends BaseImportService {
    @Override
    protected void verifyRow(BaseRowInfo row) throws ImportException {
        super.verifyRow(row);
        Integer meritA = Integer.parseInt(row.getValueOfString("meritA"));
        Integer meritBplus = Integer.parseInt(row.getValueOfString("meritBplus"));
        Integer meritB = Integer.parseInt(row.getValueOfString("meritB"));
        Integer meritBminus = Integer.parseInt(row.getValueOfString("meritBminus"));
        Integer meritC = Integer.parseInt(row.getValueOfString("meritC"));
        Integer meritD = Integer.parseInt(row.getValueOfString("meritD"));
        int sum = meritA + meritBplus + meritB + meritBminus + meritC + meritD;
        if (sum > 0 && sum != 100) {
            throw new ImportException("得分比例总和不等于100");
        }
    }

    @Override
    public void completeGenerateExcel(Map<String, BaseColumnInfo> columnInfoMap,
                                      XSSFWorkbook wb,
                                      XSSFSheet sheet) {
        Context ctx = getContext();

        int beginRow = 4;
        try {
            IPersonRelationOrg iPersonRelationOrg = PersonRelationOrgFactory.getLocalInstance(ctx);
            SorterItemCollection sorterItemCollection = new SorterItemCollection();
            sorterItemCollection.add(new SorterItemInfo("score"));
            EntityViewInfo entityViewInfo = EntityViewInfo.getInstance(null, null, sorterItemCollection);
            PersonRelationOrgCollection personRelationOrgCollection = iPersonRelationOrg.getPersonRelationOrgCollection(entityViewInfo);
            Map<Integer, String> columnIndexMap = getColumnIndexFn(columnInfoMap);
            int fillColumnCount = columnInfoMap.size();
            for (int i = 0; i < personRelationOrgCollection.size(); i++) {
                XSSFRow xSSFRow = sheet.createRow(i + beginRow);
                PersonRelationOrgInfo personRelationOrgInfo = personRelationOrgCollection.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 = personRelationOrgInfo.get(columnName);
                    if (obj != null) {
                        if (obj instanceof CoreBaseInfo) {
                            CoreBaseInfo coreBaseInfo = (CoreBaseInfo) personRelationOrgInfo.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 ("score".equals(columnName)) {
                                value = scoreEnum.getEnum((String) obj).getAlias();
                            } else {
                                value = obj.toString();
                            }

                        }
                    }
                    createCell.setCellValue(value);
                }
            }
        } catch (BOSException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void submitData(CoreBaseInfo coreBaseInfo) throws ImportException {
        StringBuilder sb = new StringBuilder();
        Integer meritBminus = null;
        PersonRelationOrgInfo personRelationOrgInfo = (PersonRelationOrgInfo) coreBaseInfo;

        if (personRelationOrgInfo.getMeritA() != 0) {
            sb.append("A：" + personRelationOrgInfo.getMeritA() + "%；");
        }
        if (personRelationOrgInfo.getMeritBplus() != 0) {
            sb.append("B+：" + personRelationOrgInfo.getMeritBplus() + "%；");
        }
        if (personRelationOrgInfo.getMeritB() != 0) {
            sb.append("B：" + personRelationOrgInfo.getMeritB() + "%；");
        }
        if (personRelationOrgInfo.getMeritBminus() != 0 ||
                personRelationOrgInfo.getMeritC() != 0 ||
                personRelationOrgInfo.getMeritD() != 0
        ) {
            meritBminus = personRelationOrgInfo.getMeritBminus() + personRelationOrgInfo.getMeritC() * 1 + personRelationOrgInfo.getMeritD();
            sb.append("B-/C/D：" + meritBminus + "%；");
        }
        personRelationOrgInfo.setRatioPerformance(sb.toString());
        try {
            IPersonRelationOrg iPersonRelationOrg = PersonRelationOrgFactory.getLocalInstance(getContext());
            iPersonRelationOrg.save(coreBaseInfo);
        } 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;
        }
    }

    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;
    }
}
