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

import com.kingdee.bos.BOSException;
import com.kingdee.eas.custom.compensation.basedata.*;
import com.kingdee.eas.hr.ats.AtsUtil;
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
import com.kingdee.shr.base.syssetting.exception.ShrWebBizException;
import com.kingdee.shr.base.syssetting.json.GridDataEntity;
import com.kingdee.shr.base.syssetting.web.handler.SHRSubBasicItemListHandler;
import com.kingdee.shr.compensation.CmpItemInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 岗位薪酬标准列表 Handler。
 * 在列表数据加载后，将每条记录的薪酬项目（CmpItem）编号和金额展开为动态列。
 */
public class PositionSalaryStandardListHandler extends SHRSubBasicItemListHandler {

    /**
     * 列表数据加载后处理：遍历每条记录的 entry 子表，按薪酬项目编码展开金额到主行。
     */
    protected void afterGetListData(
            HttpServletRequest request,
            HttpServletResponse response,
            GridDataEntity gridDataEntity
    ) throws SHRWebException {
        try {
            List<Map<String, Object>> rows = gridDataEntity.getRows();
            // 构建主记录 ID → 行索引映射
            Map<String, Integer> shrBasicItems = new HashMap<String, Integer>();
            for (int i = 0; i < rows.size(); i++) {
                Map<String, Object> map = rows.get(i);
                shrBasicItems.put(map.get("id").toString(), i);
            }
            // 批量查询带 entry 子表和薪酬项目的记录
            IPositionSalaryStandard iPositionSalaryStandard =
                    PositionSalaryStandardFactory.getLocalInstance(this.getContext());
            PositionSalaryStandardCollection positionSalaryStandardCol =
                    iPositionSalaryStandard.getPositionSalaryStandardCollection(
                            "select id,entry.*,entry.cmpItem.number where id in ("
                                    + AtsUtil.convertSetToString(shrBasicItems.keySet()) + ")");
            // 展开 entry 子表数据到主行
            for (int i = 0; i < positionSalaryStandardCol.size(); i++) {
                PositionSalaryStandardInfo info = positionSalaryStandardCol.get(i);
                Integer index = shrBasicItems.get(info.getId().toString());
                Map<String, Object> map = rows.get(index);

                PositionSalaryStandardEntryCollection entryCol = info.getEntry();
                for (int j = 0; j < entryCol.size(); j++) {
                    PositionSalaryStandardEntryInfo entryInfo = entryCol.get(j);
                    CmpItemInfo cmpItem = entryInfo.getCmpItem();
                    if (cmpItem != null) {
                        // 以薪酬项目编码为 key，金额为 value 放入行数据
                        map.put(cmpItem.getNumber(), entryInfo.getMoney());
                    }
                }
            }
        } catch (BOSException e) {
            throw new ShrWebBizException(e);
        }
    }

    /**
     * 导出数据后处理：与列表展示逻辑一致，将 entry 子表数据展开到每行。
     */
    public void afterGetExportData(
            HttpServletRequest request,
            HttpServletResponse response,
            List rows
    ) throws SHRWebException {
        try {
            // 构建主记录 ID → 行索引映射
            Map<String, Integer> shrBasicItems = new HashMap<String, Integer>();
            for (int i = 0; i < rows.size(); i++) {
                Map<String, Object> map = (Map<String, Object>) rows.get(i);
                shrBasicItems.put(map.get("id").toString(), i);
            }
            // 批量查询带 entry 子表和薪酬项目的记录
            IPositionSalaryStandard iPositionSalaryStandard =
                    PositionSalaryStandardFactory.getLocalInstance(this.getContext());
            PositionSalaryStandardCollection positionSalaryStandardCol =
                    iPositionSalaryStandard.getPositionSalaryStandardCollection(
                            "select id,entry.*,entry.cmpItem.number where id in ("
                                    + AtsUtil.convertSetToString(shrBasicItems.keySet()) + ")");
            // 展开 entry 子表数据到行
            for (int i = 0; i < positionSalaryStandardCol.size(); i++) {
                PositionSalaryStandardInfo info = positionSalaryStandardCol.get(i);
                Integer index = shrBasicItems.get(info.getId().toString());
                Map<String, Object> map = (Map<String, Object>) rows.get(index);

                PositionSalaryStandardEntryCollection entryCol = info.getEntry();
                for (int j = 0; j < entryCol.size(); j++) {
                    PositionSalaryStandardEntryInfo entryInfo = entryCol.get(j);
                    CmpItemInfo cmpItem = entryInfo.getCmpItem();
                    if (cmpItem != null) {
                        map.put(cmpItem.getNumber(), entryInfo.getMoney());
                    }
                }
            }
        } catch (BOSException e) {
            throw new ShrWebBizException(e);
        }
    }
}
