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

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.exception.SHRWebException;
import com.kingdee.shr.base.syssetting.exception.ShrWebBizException;
import com.kingdee.shr.base.syssetting.web.dynamic.model.UIViewInfo;
import com.kingdee.shr.base.syssetting.web.handler.SHRSubBasicItemEditHandler;
import com.kingdee.shr.base.syssetting.web.json.JSONUtils;
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 org.springframework.ui.ModelMap;

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

/**
 * @Description ¸ÚÎ»Ð½³ê±ê×¼
 * @Date 2026/7/16 14:44
 * @Created by HeYuan
 */
public class PositionSalaryStandardEditHandler extends SHRSubBasicItemEditHandler {

    protected void setInitData(
            HttpServletRequest request,
            HttpServletResponse response,
            ModelMap modelMap,
            Map<String, Object> initData
    ) throws SHRWebException {
        super.setInitData(request, response, modelMap, initData);
        Context ctx = this.getContext();

        UIViewInfo uiViewInfo = this.getUIViewInfo(request);
        String pageParamConfig = uiViewInfo.getPageParamConfig();
        HashMap map = JSONUtils.convertJsonToObject(ctx, pageParamConfig.replace(" ", ""));
        Object isHideAddRow = map.get("isHideAddRow");
        Object isHideDeleteRow = map.get("isHideDeleteRow");
        if (Boolean.parseBoolean(isHideAddRow.toString())) {
            initData.put("isHideAddRow", true);
        } else {
            initData.put("isHideAddRow", false);
        }
        if (Boolean.parseBoolean(isHideDeleteRow.toString())) {
            initData.put("isHideDeleteRow", true);
        } else {
            initData.put("isHideDeleteRow", false);
        }
    }


    /**
     * @param request
     * @param response
     * @param coreBaseInfo
     * @throws SHRWebException
     */
    @Override
    protected void afterCreateNewModel(
            HttpServletRequest request,
            HttpServletResponse response,
            CoreBaseInfo coreBaseInfo
    ) throws SHRWebException {
        try {
            Context ctx = this.getContext();
            PositionSalaryStandardInfo info = (PositionSalaryStandardInfo) coreBaseInfo;
            PositionSalaryStandardEntryCollection entryCol = info.getEntry();

            UIViewInfo uiViewInfo = this.getUIViewInfo(request);
            String pageParamConfig = uiViewInfo.getPageParamConfig();
            HashMap map = JSONUtils.convertJsonToObject(ctx, pageParamConfig.replace(" ", ""));
            ArrayList<String> cmpItems = (ArrayList<String>) map.get("cmpItems");
            if (cmpItems == null || cmpItems.isEmpty()) {
                return;
            }
            ICmpItem iCmpItem = CmpItemFactory.getLocalInstance(ctx);
            CmpItemCollection cmpItemCol = iCmpItem.getCmpItemCollection("select * where number in (" + AtsUtil.convertListToString(cmpItems) + ")");
            for (int i = 0; i < cmpItems.size(); i++) {
                String cmpItemNumber = cmpItems.get(i);
                for (int j = 0; j < cmpItemCol.size(); j++) {
                    CmpItemInfo cmpItemInfo = cmpItemCol.get(j);
                    if (cmpItemInfo.getNumber().equals(cmpItemNumber)) {
                        PositionSalaryStandardEntryInfo entryInfo = new PositionSalaryStandardEntryInfo();
                        entryInfo.setCmpItem(cmpItemInfo);
                        entryCol.add(entryInfo);
                        break;
                    }
                }
            }
        } catch (BOSException e) {
            throw new ShrWebBizException(e);
        }

        super.afterCreateNewModel(request, response, coreBaseInfo);
    }
}
