package com.kingdee.eas.custom.projectbonus.web.handler;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.custom.projectbonus.*;
import com.kingdee.eas.custom.projectbonus.web.vo.*;
import com.kingdee.eas.framework.ICoreBase;
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.apache.log4j.Logger;
import org.springframework.ui.ModelMap;

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

/**
 * @Description 分配人员明细列表动态列表handler（专员）
 * @Date 2024/4/22 17:56
 * @Created by Heyuan
 */
public class PersonnelDetailDynamicListHandler extends ProjectBonusDynamicHandler {
    private static Logger logger = Logger.getLogger(PersonnelDetailDynamicListHandler.class);


    /**
     * 获取生效日期过滤器
     * 移除父类方法过滤条件
     *
     * @param effectDate
     * @return
     */
    protected FilterInfo getEffectDateFilter(Date effectDate) {
        return null;
    }


    /**
     * 获取默认编辑列
     *
     * @param request
     * @return
     */
    protected Map<String, Map<String, Object>> getDefultEditColumnsSetting(HttpServletRequest request) {
        try {
            //设置F7编辑列属性
            Map<String, Map<String, Object>> result = new HashMap();
            //奖金期限截止日期
            EditColumnInfo dateEditColumnInfo = new EditColumnInfo();
            dateEditColumnInfo.setName("bdpapd.bonusduedate");
            dateEditColumnInfo.setFormatter("shrDateTimePicker");
            dateEditColumnInfo.setEdittype("shrDateTimePicker");
            dateEditColumnInfo.setType("shrDateTimePicker");
            EditOptions dateEditoptions = dateEditColumnInfo.getEditoptions();
            dateEditoptions.getDatepickerjson();
            result.put("bdpapd.bonusduedate", dateEditColumnInfo.convertToMap());
            //奖金期限起算日期
            EditColumnInfo dateEditColumnInfo2 = new EditColumnInfo();
            dateEditColumnInfo2.setName("bdpapd.bonusstartingdate");
            dateEditColumnInfo2.setFormatter("shrDateTimePicker");
            dateEditColumnInfo2.setEdittype("shrDateTimePicker");
            dateEditColumnInfo2.setType("shrDateTimePicker");
            EditOptions dateEditoptions2 = dateEditColumnInfo2.getEditoptions();
            dateEditoptions2.getDatepickerjson();
            result.put("bdpapd.bonusstartingdate", dateEditColumnInfo2.convertToMap());
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * 动态表单保存
     *
     * @param request
     * @param response
     * @param modelMap
     * @return
     * @throws SHRWebException
     */
    public void saveDynGridAction(HttpServletRequest request,
                                  HttpServletResponse response,
                                  ModelMap modelMap) throws SHRWebException {
        List<Map<String, Object>> dataList = (List) JSONUtils.convertJsonToObject(request.getParameter("modi"), List.class);
        try {
            IBonusCycleBusDepProjectAssPersonnelDetail iPersonnelDetail = (IBonusCycleBusDepProjectAssPersonnelDetail) getBizInterface(request);
            //待更新数据集合
            Map<String, BonusCycleBusDepProjectAssPersonnelDetailInfo> updateCols = new HashMap<String, BonusCycleBusDepProjectAssPersonnelDetailInfo>();
            //待更新字段
            Map<String, SelectorItemCollection> updateSics = new HashMap<String, SelectorItemCollection>();
            for (int i = 0; i < dataList.size(); i++) {
                Map<String, Object> map = dataList.get(i);
                String idxValue = (String) map.get("fid");
                BonusCycleBusDepProjectAssPersonnelDetailInfo info;
                SelectorItemCollection updateSic;
                if (updateCols.containsKey(idxValue)) {
                    info = updateCols.get(idxValue);
                } else {
                    info = new BonusCycleBusDepProjectAssPersonnelDetailInfo();
                    info.setId(BOSUuid.read(idxValue));
                    updateCols.put(idxValue, info);
                }
                //保存更新列
                if (updateSics.containsKey(idxValue)) {
                    updateSic = updateSics.get(idxValue);
                } else {
                    updateSic = new SelectorItemCollection();
                    updateSics.put(idxValue, updateSic);
                }
                //列名
                String colName = (String) map.get("column");
                //数据类型
                String dataType = (String) map.get("type");
                //值
                Object valueObj = map.get("value");
                if ("BigDecimal".equals(dataType) && StringUtils.isEmpty(valueObj.toString())) {
                    valueObj = BigDecimal.ZERO;
                }
                updateSic.add(colName);
                info.put(colName, valueObj);
            }
            for (String id : updateCols.keySet()) {
                BonusCycleBusDepProjectAssPersonnelDetailInfo info = updateCols.get(id);
                SelectorItemCollection updateSic = updateSics.get(id);
                //更新
                iPersonnelDetail.updatePartial(info, updateSic);
            }
            JSONUtils.SUCCESS("成功修改" + updateCols.size() + "条数据");
        } catch (Exception var27) {
            var27.printStackTrace();
            logger.error(var27.getMessage());
            throw new ShrWebBizException(var27.getMessage(), var27);
        }
    }


    /**
     * @param request
     * @return
     * @throws SHRWebException
     */
    @Override
    protected ICoreBase getBizInterface(HttpServletRequest request) throws SHRWebException {
        Context ctx = SHRContext.getInstance().getContext();
        try {
            return BonusCycleBusDepProjectAssPersonnelDetailFactory.getLocalInstance(ctx);
        } catch (BOSException e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            throw new ShrWebBizException(e.getMessage());
        }
    }
}
