package com.kingdee.eas.custom.dormitorysystem.waterelectricity.handler;

import com.kingdee.bos.Context;
import com.kingdee.bos.rabbitmq.guava.Lists;
import com.kingdee.bos.util.EASResource;
import com.kingdee.eas.hr.base.SqlParam;
import com.kingdee.eas.hr.base.app.filter.FilterUtil;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.jdbc.rowset.IRowSet;
import com.kingdee.shr.base.syssetting.context.SHRContext;
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
import com.kingdee.shr.base.syssetting.exception.ShrWebBizException;
import com.kingdee.shr.compensation.exception.CmpCalDynamicListBizException;
import com.kingdee.shr.compensation.exception.ExceptionHandle;
import com.kingdee.shr.compensation.request.ParseRequestHelper;
import com.kingdee.shr.compensation.util.BaseUtils;
import com.kingdee.shr.compensation.util.CmpStrUtil;
import com.kingdee.shr.compensation.web.handler.CmpCalDynamicListHandler;
import com.kingdee.util.StringUtils;
import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.ui.ModelMap;

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

/**
 * description: CmpCalDynamicListHandlerEx <br>
 * date: 2025/4/30 16:15 <br>
 * author: lhbj <br>
 * version: 1.0 <br>
 */
public class CmpCalDynamicListHandlerEx extends CmpCalDynamicListHandler {

    private String getCalKeyFromSeesion(HttpServletRequest request) throws SHRWebException {
        String filterSql = "";
        String selectedData = request.getParameter("selectedData");
        if (!StringUtils.isEmpty(selectedData)) {
            selectedData = StringEscapeUtils.escapeSql(selectedData);
            filterSql = "'" + CmpStrUtil.join(selectedData.split(","), "','", (String)null) + "'";
        } else {
            HttpSession session = request.getSession();
            Object cmpschemeid = ParseRequestHelper.getParameterByRequest(request, "cmpschemeid");
            Object sqlParamObject = session.getAttribute(cmpschemeid == null ? "" : cmpschemeid.toString() + "sqlParam");
            SqlParam param;
            if (sqlParamObject instanceof Map) {
                Map<String, Object> sqlParamMap = (Map)sqlParamObject;
                param = (SqlParam)sqlParamMap.get(cmpschemeid == null ? "" : cmpschemeid.toString() + "sqlParam");
            } else {
                param = (SqlParam)session.getAttribute(cmpschemeid == null ? "" : cmpschemeid.toString() + "sqlParam");
            }

            Map<String, String> map = new HashMap();
            map.put("cmp_ctb.id", "cmp_ctb.id");
            param.setSelectMapping(map);
            String originalSql = "";

            try {
                originalSql = FilterUtil.getSql(SHRContext.getInstance().getContext(), param);
            } catch (Exception var14) {
                var14.printStackTrace();
            }

            String key = "cal_" + originalSql;
            String tempName = "";

            try {
                tempName = null == BaseUtils.getCacheMap(key) ? null : BaseUtils.getCacheMap(key).toString();
                if (null != tempName) {
                    return tempName;
                }

                filterSql = this.util.getTempTable(key, originalSql);
            } catch (Exception var13) {
                filterSql = this.util.getTempTable(key, originalSql);
            }
        }

        return filterSql;
    }
    private void verifyMultiUipkScheme(HttpServletRequest request, String cmpschemeid) throws SHRWebException {
        HttpSession session = request.getSession();
        String uipk = request.getParameter("uipk");
        Object uipkObj = session.getAttribute(cmpschemeid + "uipk");
        String queryUipk = uipkObj == null ? "" : uipkObj.toString();
        if (!StringUtils.isEmpty(queryUipk) && !queryUipk.equals(uipk)) {
            throw new ShrWebBizException(new CmpCalDynamicListBizException(CmpCalDynamicListBizException.EXCEPTION148));
        } else {
            String cmpSchemeId = String.valueOf(session.getAttribute("calscheme")).trim();
            if (!cmpSchemeId.equals(cmpschemeid)) {
                throw new ShrWebBizException(new CmpCalDynamicListBizException(CmpCalDynamicListBizException.EXCEPTION149));
            }
        }
    }
    public String lockDynGridAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws SHRWebException {
        Context ctx = SHRContext.getInstance().getContext();

        try {
            String cmpSchemeId = request.getParameter("cmpschemeid");
            this.verifyMultiUipkScheme(request, cmpSchemeId);
            String filterSql = this.getCalKeyFromSeesion(request);
            String updateSql = "select fid from t_hr_scmpcaltable where Fid in (" + filterSql + ")";
            StringBuilder sel = new StringBuilder();
            sel.append(" select ");
            sel.append(" we.fid from t_hr_scmpcaltable scm ");
            sel.append(" left join CT_WOE_WaterElectricity we on scm.FPersonID = we.cfpersonid ");
            sel.append(" and scm.FBeginDate = we.CFStartDate ");
            sel.append(" and scm.FEndDate = we.CFEndDate ");
            sel.append(" where ");
            sel.append(" scm.FBeginDate = we.CFStartDate ");
            sel.append(" and scm.FEndDate = we.CFEndDate ");
            sel.append(" and scm.fid in("+updateSql+")");
            IRowSet rs = DbUtil.executeQuery(ctx,sel.toString());
            List<String> list = Lists.newArrayList();
            while (rs.next()){
                String id = rs.getString("fid");
                list.add(id);
            }
            StringBuilder selSpecial = new StringBuilder();
            selSpecial.append(" select ");
            selSpecial.append(" we.fid from t_hr_scmpcaltable scm ");
            selSpecial.append(" left join CT_WOE_WaterElectricitySpecial we on scm.FPersonID = we.cfpersonid ");
            selSpecial.append(" and scm.FBeginDate = we.CFStartDate ");
            selSpecial.append(" and scm.FEndDate = we.CFEndDate ");
            selSpecial.append(" where ");
            selSpecial.append(" scm.FBeginDate = we.CFStartDate ");
            selSpecial.append(" and scm.FEndDate = we.CFEndDate ");
            selSpecial.append(" and scm.fid in("+updateSql+")");
            IRowSet rsSpecial = DbUtil.executeQuery(ctx,selSpecial.toString());
            List<String> listSpecial = Lists.newArrayList();
            while (rsSpecial.next()){
                String id = rsSpecial.getString("fid");
                listSpecial.add(id);
            }
            WaterElectricityListHandler.enable(ctx,WaterElectricityListHandler.convertListToString(list.toArray()));
            WaterElectricityListHandler.enable(ctx,WaterElectricityListHandler.convertListToString(listSpecial.toArray()));

            Map<String, Object> params = new HashMap();
            params.put("action", EASResource.getString("com.kingdee.shr.compensation.resource.CmpAgentResource", "label36", ctx.getLocale()));
            params.put("updateSql", updateSql);
            params.put("cmpCalId", filterSql);
            params.put("cmpSchemeId", cmpSchemeId);
            super.lockDynGridAction(request,response,modelMap);
        } catch (Exception var11) {
            ExceptionHandle.handleException(var11);
        }
        return null;
    }

    public String cancelLockDynGridAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws SHRWebException {
        Context ctx = SHRContext.getInstance().getContext();
        try {
            String cmpSchemeId = request.getParameter("cmpschemeid");
            this.verifyMultiUipkScheme(request, cmpSchemeId);
            String cilentIds = request.getParameter("clientIds");
            String filterSql = this.getCalKeyFromSeesion(request);
            String replaceFilterTarget = "(\"CMP_CTB\".FEndDate>=\"CMP_PERSON\".FEFFDT )";
            String stateFilter = " AND FCalState in(1,2,3,4,5,6,7) ";
            String newFilter = replaceFilterTarget + stateFilter;
            String targetSql = filterSql.replace(replaceFilterTarget, newFilter);
            String updateSql = "select fid from t_hr_scmpcaltable where Fid in (" + targetSql + ")" + stateFilter;
            StringBuilder sel = new StringBuilder();
            sel.append(" select ");
            sel.append(" we.fid from t_hr_scmpcaltable scm ");
            sel.append(" left join CT_WOE_WaterElectricity we on scm.FPersonID = we.cfpersonid ");
            sel.append(" and scm.FBeginDate = we.CFStartDate ");
            sel.append(" and scm.FEndDate = we.CFEndDate ");
            sel.append(" where ");
            sel.append(" scm.FBeginDate = we.CFStartDate ");
            sel.append(" and scm.FEndDate = we.CFEndDate ");
            sel.append(" and scm.fid in("+updateSql+")");
            IRowSet rs = DbUtil.executeQuery(ctx,sel.toString());
            List<String> list = Lists.newArrayList();
            while (rs.next()){
                String id = rs.getString("fid");
                list.add(id);
            }
            StringBuilder selSpecial = new StringBuilder();
            selSpecial.append(" select ");
            selSpecial.append(" we.fid from t_hr_scmpcaltable scm ");
            selSpecial.append(" left join CT_WOE_WaterElectricitySpecial we on scm.FPersonID = we.cfpersonid ");
            selSpecial.append(" and scm.FBeginDate = we.CFStartDate ");
            selSpecial.append(" and scm.FEndDate = we.CFEndDate ");
            selSpecial.append(" where ");
            selSpecial.append(" scm.FBeginDate = we.CFStartDate ");
            selSpecial.append(" and scm.FEndDate = we.CFEndDate ");
            selSpecial.append(" and scm.fid in("+updateSql+")");
            IRowSet rsSpecial = DbUtil.executeQuery(ctx,selSpecial.toString());
            List<String> listSpecial = Lists.newArrayList();
            while (rsSpecial.next()){
                String id = rsSpecial.getString("fid");
                listSpecial.add(id);
            }
            WaterElectricityListHandler.disable(ctx,WaterElectricityListHandler.convertListToString(list.toArray()));
            WaterElectricityListHandler.disable(ctx,WaterElectricityListHandler.convertListToString(listSpecial.toArray()));

            Map<String, Object> params = new HashMap();
            params.put("action", EASResource.getString("com.kingdee.shr.compensation.resource.CmpAgentResource", "label37", ctx.getLocale()));
            params.put("updateSql", updateSql);
            params.put("cmpCalId", filterSql);
            params.put("cmpSchemeId", cmpSchemeId);
            super.cancelLockDynGridAction(request,response,modelMap);
        } catch (Exception var16) {
            ExceptionHandle.handleException(var16);
        }
        return null;
    }

}
