package com.kingdee.shr.compensation.web.handler.integrate;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.ui.ModelMap;

import com.google.common.collect.Maps;
import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.framework.CoreBaseInfo;
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.base.syssetting.web.json.JSONUtils;
import com.kingdee.shr.compensation.app.integrate.BatchSubmitShemeBillFactory;
import com.kingdee.shr.compensation.app.integrate.BatchSubmitShemeBillInfo;
import com.kingdee.shr.compensation.app.integrate.CalSubmitSchemeFactory;
import com.kingdee.shr.compensation.app.integrate.CalSubmitSchemeInfo;
import com.kingdee.shr.compensation.util.columnModel.CmpColumnModelWrapper;
import com.kingdee.shr.compensation.util.columnModel.CmpColumnModels;
import com.kingdee.shr.customer.gtiit.helper.CmpDisplayViewAnalyzing;

/**
 * 专业用户提报单
 * @author issuser
 *
 */
public class BatchSubmitShemeBillEditHandlerEx extends BatchSubmitShemeBillEditHandler {

	private static Logger logger =
	        Logger.getLogger("com.kingdee.shr.compensation.web.handler.integrate.BatchSubmitShemeBillEditHandlerEx");
	
	@Override
	protected void beforeSave(HttpServletRequest request,
			HttpServletResponse respone, CoreBaseInfo model) throws SHRWebException {
		super.beforeSave(request, respone, model);
	}
	
	@Override
	public String getEditGridColModelAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap)
			throws SHRWebException {
		String submitSchemeId = request.getParameter("submitSchemeId");
        if (StringUtils.isEmpty(submitSchemeId)) {
            JSONUtils.writeJson(response, modelMap);
            return null;
        } else {
            boolean dynamicColmunRequired = this.isDynamicColmunRequired(request);
            Map<String, Object> params = Maps.newHashMap();
            params.put("datasource", Integer.valueOf(request.getParameter("datasource")));
            params.put("costTypeId", request.getParameter("costTypeId"));
            params.put("dynamicColmunRequired", dynamicColmunRequired);
            params.put("hrOrgUnitId", request.getParameter("hrOrgUnitId"));
            Context ctx = SHRContext.getInstance().getContext();
            CmpColumnModels cmpColumnModels = this.getEntryGenerator().getEntryColumnModels(ctx, submitSchemeId, params);
            assembleAddNewPropName(ctx, cmpColumnModels);
            modelMap.put("colNames", cmpColumnModels.getColNames());
            modelMap.put("colModel", cmpColumnModels.getColModel());
            modelMap.put("defaultSortname", (Object)null);

            CalSubmitSchemeInfo schemeInfo;
            try {
                schemeInfo = CalSubmitSchemeFactory.getLocalInstance(ctx).getCalSubmitSchemeInfo(new ObjectUuidPK(submitSchemeId));
            } catch (EASBizException var11) {
                throw new ShrWebBizException(var11.getMessage(), var11);
            } catch (BOSException var12) {
                throw new SHRWebException(var12.getMessage(), var12);
            }

            modelMap.put("description", schemeInfo.getDescription());
            JSONUtils.writeJson(response, modelMap);
            return null;
        }
	}
	
	/**
	 * 自定义字段添加
	 * @param ctx
	 * @param cmpColumnModels
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void assembleAddNewPropName(Context ctx, CmpColumnModels cmpColumnModels) {
		Map<String, Map<String, Boolean>> attrMap = null;
		try {
			attrMap = CmpDisplayViewAnalyzing.complementDefaultField(ctx, "com.kingdee.shr.compensation.app.integrate.BatchSubmitShemeBill.form.display");
		} catch (SHRWebException e) {
			e.printStackTrace();
		}
		
        String tempPropName = "备注";
        if(!(ctx.getLocale().toString().equalsIgnoreCase("zh_CN") || ctx.getLocale().toString().equalsIgnoreCase("l2") || ctx.getLocale().toString().equalsIgnoreCase("L2"))) {
        	tempPropName = "Remark"; 
		}
        Map<String, Boolean> attr = attrMap == null ? new HashMap() : (Map)attrMap.get("remark");
        cmpColumnModels.add(tempPropName, CmpColumnModelWrapper.createTextColModel("remark", tempPropName, 250).assembleTextInputEditOptions(false, 500).frozen((Boolean)((Map)attr).get("frozen")).hidden((Boolean)((Map)attr).get("hidden")).key().get());
	}
	
	@Override
	protected void verifyModel(HttpServletRequest request, HttpServletResponse response, CoreBaseInfo model)
			throws SHRWebException {
		super.verifyModel(request, response, model);
		Context ctx = SHRContext.getInstance().getContext();
		BatchSubmitShemeBillInfo info = (BatchSubmitShemeBillInfo) model;
		if(StringUtils.isNotBlank(info.getString("id"))) {
			try {
				info = BatchSubmitShemeBillFactory.getLocalInstance(ctx).getBatchSubmitShemeBillInfo(new ObjectUuidPK(info.getString("id")));
			} catch (EASBizException e) {
				e.printStackTrace();
			} catch (BOSException e) {
				e.printStackTrace();
			}
		}
		//参数校验
		BatchSubmitSchemeUtils.checkExists(info);
	}
	
	@Override
	protected void beforeSubmit(HttpServletRequest request, HttpServletResponse response, CoreBaseInfo model)
			throws SHRWebException {
		super.beforeSubmit(request, response, model);
		Context ctx = SHRContext.getInstance().getContext();
		BatchSubmitShemeBillInfo info = (BatchSubmitShemeBillInfo) model;
		if(StringUtils.isNotBlank(info.getString("id"))) {
			try {
				info = BatchSubmitShemeBillFactory.getLocalInstance(ctx).getBatchSubmitShemeBillInfo(new ObjectUuidPK(info.getString("id")));
			} catch (EASBizException e) {
				e.printStackTrace();
			} catch (BOSException e) {
				e.printStackTrace();
			}
		}
		//校验员工+提报月+方案只能提交一次
		BatchSubmitSchemeUtils.checkSubmitCount(info);
		//参数校验
		BatchSubmitSchemeUtils.checkExists(info);
		//校验只能提交发生日期和提报日期在一个月内的单据
		BatchSubmitSchemeUtils.checkSubmitDate(info);
		//校验分录人员是否重复
		BatchSubmitSchemeUtils.checkPersonRepeat(info);
		//校验员工+方案+发生日期不能重复
		BatchSubmitSchemeUtils.checkSubmitRepeat(info);
	}
	
	
	/**
	 * 获取提报方案中数据类型为数值的提报项目字段
	 * @param request
	 * @param response
	 * @param modelMap
	 * @throws SHRWebException
	 */
	public void getSchemeItemFildsAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws SHRWebException {
		Context ctx = SHRContext.getInstance().getContext();
		//提报方案ID
		String calSchemeId = request.getParameter("calSchemeId");
		List<String> list = new ArrayList<String>();
		String sql = "select b.FIELDSN from T_HR_SCalSubmitSchemeItem a left join T_HR_SCalSubmitItem b on a.FCALSUBMITITEMID = b.fid where a.FISSHOW = '1' and a.FSTATE = '1' and a.FCALSUBMITSCHEMEID = '"+calSchemeId+"'";
		try {
			IRowSet iRowSet = DbUtil.executeQuery(ctx, sql);
			while(iRowSet.next()) {
				list.add("S"+iRowSet.getString("FIELDSN"));
			}
		} catch (BOSException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		modelMap.put("schemeItemList", list);
		JSONUtils.writeJson(response, modelMap);
	}
	
}
