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

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.eas.custom.compensation.basedata.PositionSalaryStandardEntryCollection;
import com.kingdee.eas.custom.compensation.basedata.PositionSalaryStandardEntryInfo;
import com.kingdee.eas.custom.compensation.basedata.PositionSalaryStandardInfo;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.hr.ats.AtsUtil;
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.compensation.CmpItemCollection;
import com.kingdee.shr.compensation.CmpItemFactory;
import com.kingdee.shr.compensation.CmpItemInfo;
import com.kingdee.shr.compensation.ICmpItem;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 岗位薪酬标准导入服务。
 * 将 Excel 中自定义薪酬项目列的值解析为 entry 子表记录。
 */
public class PositionSalaryStandardImportService extends BaseImportService {

    private CmpItemCollection cmpItemCol = new CmpItemCollection();

    /**
     * 数值格式校验正则
     */
    private static final String NUMBER_PATTERN = "^\\d+(\\.\\d+)?$";

    @Override
    protected void initService() throws ImportException {
        try {
            Context ctx = this.getContext();
            // 收集所有自定义列（薪酬项目列）
            Map<String, BaseColumnInfo> columnMap = getColumnMap();
            List<String> cmpItems = new ArrayList<String>();
            for (String key : columnMap.keySet()) {
                BaseColumnInfo baseColumnInfo = columnMap.get(key);
                if (baseColumnInfo.isCustomField()) {
                    cmpItems.add(key);
                }
            }

            // 批量查询薪酬项目
            ICmpItem iCmpItem = CmpItemFactory.getLocalInstance(ctx);
            cmpItemCol = iCmpItem.getCmpItemCollection(
                    "select * where number in (" + AtsUtil.convertListToString(cmpItems) + ")");
        } catch (BOSException e) {
            throw new ImportException(e);
        }
    }


    @Override
    protected void setOtherFieldInfo(CoreBaseInfo baseInfo, BaseRowInfo row) throws ImportException {
        PositionSalaryStandardInfo info = (PositionSalaryStandardInfo) baseInfo;
        PositionSalaryStandardEntryCollection entryCollection = info.getEntry();

        // 遍历自定义列，匹配薪酬项目并设置金额
        for (int j = 0; j < cmpItemCol.size(); j++) {
            CmpItemInfo cmpItemInfo = cmpItemCol.get(j);
            // 校验金额是否为合法数值
            String money = row.getValueOfString(cmpItemInfo.getNumber());
            if (money == null || money.trim().isEmpty()) {
                // 空值跳过，不创建 entry
                break;
            }
            if (!money.trim().matches(NUMBER_PATTERN)) {
                throw new ImportException("薪酬项目[" + cmpItemInfo.getNumber() + "]的值[" + money + "]不是合法的数值格式");
            }

            PositionSalaryStandardEntryInfo entryInfo = new PositionSalaryStandardEntryInfo();
            entryInfo.setCmpItem(cmpItemInfo);
            entryInfo.setMoney(new BigDecimal(money.trim()));
            entryCollection.add(entryInfo);
        }
    }
}
