package com.kingdee.eas.custom.expandassess.hadler;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.expandassess.*;
import com.kingdee.eas.custom.expandassess.app.ExpandStatusEunm;
import com.kingdee.eas.custom.expandassess.service.impl.ExpandAssessServiceImpl;
import com.kingdee.eas.framework.CoreBaseCollection;
import com.kingdee.eas.hr.ats.AtsUtil;
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.base.syssetting.web.json.JSONUtils;
import com.kingdee.util.StringUtils;
import org.springframework.ui.ModelMap;

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

/**
 * @author qingwu
 * @date 2024/5/16
 * @apiNote 拓展人员考核考核人维护
 */
public class ExpandAssessKHRDynamicListHandler extends DynamicListHandlerEx {
    ExpandAssessServiceImpl expandAssessService = new ExpandAssessServiceImpl();
    private Context ctx = SHRContext.getInstance().getContext();

    /**
     * 提交至负责人
     *
     * @param request
     * @param response
     * @param modelMap
     * @throws ShrWebBizException
     */
    public void submitPrincipalAction(HttpServletRequest request,
                                      HttpServletResponse response,
                                      ModelMap modelMap) throws ShrWebBizException {
        //获取删除ID
        String idArray = request.getParameter("idArray");
        if (StringUtils.isEmpty(idArray)) {
            throw new ShrWebBizException("勾选数据无效，请联系管理员！");
        }
        try {
            expandAssessService.updateAsseStatusAll(ctx, idArray, ExpandStatusEunm.START, ExpandStatusEunm.ASSESUBMINT, "请勾选状态为启用考核的数据,否则无法提交");
        } catch (ShrWebBizException | BOSException e) {
            e.printStackTrace();
            throw new ShrWebBizException(e);
        } catch (EASBizException e) {
            throw new ShrWebBizException(e);
        }
    }

    /**
     * 动态表单保存
     *
     * @param request
     * @param response
     * @param modelMap
     * @throws SHRWebException
     * @returnA
     */
    public void saveDynGridAction(HttpServletRequest request,
                                  HttpServletResponse response,
                                  ModelMap modelMap) throws SHRWebException {
        Context ctx = SHRContext.getInstance().getContext();
        try {
            List<Map<String, String>> dataList = (List) JSONUtils.convertJsonToObject(request.getParameter("modi"), List.class);
            IExpandAssessEntry iExpandAssessEntry = ExpandAssessEntryFactory.getLocalInstance(ctx);
            //待更新数据集合
            Map<String, ExpandAssessEntryInfo> updEntryCols = new HashMap<String, ExpandAssessEntryInfo>();
            //待更新字段
            Map<String, SelectorItemCollection> updSics = new HashMap<String, SelectorItemCollection>();

            for (int i = 0; i < dataList.size(); i++) {
                Map<String, String> map = dataList.get(i);
                ExpandAssessEntryInfo entryInfo;
                SelectorItemCollection updSic;
                String fid = map.get("fid");
                //保存修改数据
                if (updEntryCols.containsKey(fid)) {
                    entryInfo = updEntryCols.get(fid);
                } else {
                    entryInfo = iExpandAssessEntry.getExpandAssessEntryInfo(new ObjectUuidPK(fid));
                    updEntryCols.put(fid, entryInfo);
                }

                //保存修改字段
                if (updSics.containsKey(fid)) {
                    updSic = updSics.get(fid);
                } else {
                    updSic = new SelectorItemCollection();
                    updSics.put(fid, updSic);
                }
                //字段名称
                String colName = map.get("column");
                //值类型
                String type = map.get("type");
                //修改值
                Object value = map.get("value");
                updSic.add(colName);
                entryInfo.put(colName, value);
            }
            for (String id : updEntryCols.keySet()) {
                //修改的实体
                ExpandAssessEntryInfo entryInfo = updEntryCols.get(id);
                if (entryInfo.getAsseStatus() == ExpandStatusEunm.START) {
                    //修改的字段
                    SelectorItemCollection updSic = updSics.get(id);
                    updSic.add("contracts");
                    iExpandAssessEntry.updatePartial(entryInfo, updSic);
                }
            }
            JSONUtils.SUCCESS("成功修改" + dataList.size() + "条数据");
        } catch (Exception e) {
            e.printStackTrace();
            throw new ShrWebBizException(e);
        }

    }


}
