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

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.eas.custom.dormitorysystem.waterpower.WaterPowerMeterReadingInfo;
import com.kingdee.eas.custom.dormitorysystem.waterpower.utils.WaterPowerUtil;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.shr.base.syssetting.context.SHRContext;
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
import com.kingdee.shr.base.syssetting.web.handler.EditHandler;
import com.kingdee.shr.base.syssetting.web.json.JSONUtils;

import org.apache.commons.lang3.StringUtils;
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.Map;

/**
 * @author qingwu
 * @date 2025/4/21
 * @apiNote 水电每月抄表信息  com.kingdee.eas.custom.dormitorysystem.waterpower.app.WaterPowerMeterReading89.form
 */
public class WaterPowerMeterReadingEditHandler extends EditHandler {
    Context ctx = SHRContext.getInstance().getContext();

    //@Override
    //protected void afterInitModel(HttpServletRequest request, HttpServletResponse response, CoreBaseInfo coreBaseInfo) throws SHRWebException {
    //    super.afterInitModel(request, response, coreBaseInfo);
    //    WaterPowerMeterReadingInfo waterPowerMeterReadingInfo = (WaterPowerMeterReadingInfo) coreBaseInfo;
    //    String dormNumber = waterPowerMeterReadingInfo.getDormNumber();
    //    try {
    //        IDormitory iDormitory = DormitoryFactory.getLocalInstance(ctx);
    //        DormitoryCollection dormitoryCollection = iDormitory.getDormitoryCollection("where number ='" + dormNumber + "'");
    //        DormitoryInfo dormitoryInfo = dormitoryCollection.get(0);
    //        DormitoryInfo dormitory = waterPowerMeterReadingInfo.getDormitory();
    //        dormitory.setRoomNumber(dormitoryInfo.getRoomNumber());
    //    } catch (BOSException e) {
    //        e.printStackTrace();
    //        throw new RuntimeException(e);
    //    }
    //}

    @Override
    protected void beforeSave(HttpServletRequest request, HttpServletResponse response, CoreBaseInfo model) throws SHRWebException {
        super.beforeSave(request, response, model);
        WaterPowerMeterReadingInfo waterPowerMeterReadingInfo = (WaterPowerMeterReadingInfo) model;
        Date years = waterPowerMeterReadingInfo.getYears();
        String dormNumber = waterPowerMeterReadingInfo.getDormNumber();
        WaterPowerUtil.verifyDatEexists(ctx, years, dormNumber, waterPowerMeterReadingInfo.getId() == null ? "" : waterPowerMeterReadingInfo.getId().toString());
 
    }
    
    
    

    
    
    
    

    /**
     * 获取当月用水电吨数
     * @param request
     * @param response
     * @param modelMap
     */
    public void getThisMonthWaterPowerTonsAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws BOSException, SHRWebException {
        String years = request.getParameter("years");//年月
        String totalWaterTonsStr = request.getParameter("totalWaterTons") == "" ? "0" : request.getParameter("totalWaterTons");
        BigDecimal totalWaterTons = new BigDecimal(totalWaterTonsStr);//用水总吨数
        String totalElecDegreesStr = request.getParameter("totalElecDegrees") == "" ? "0" : request.getParameter("totalElecDegrees");
        BigDecimal totalElecDegrees = new BigDecimal(totalElecDegreesStr);//用电总吨数
        String dormNumber = request.getParameter("dormNumber");//宿舍编码
        boolean calcInterval = request.getParameter("calcInterval").isEmpty();//是否取消自动计算当月水电
        Map result = WaterPowerUtil.getThisMonthWaterPowerTons(ctx, years, dormNumber, totalWaterTons, totalElecDegrees);
        JSONUtils.writeJson(response, result);
    }
    
    
    
//    /**
//     * 获取当月用水电吨数
//     * @param request
//     * @param response
//     * @param modelMap
//     */
//    public void getWaterPowerNewAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws BOSException, SHRWebException {
//        String years = request.getParameter("years");//年月
//        //用水抄表数
//        String totalWaterTonsStr = request.getParameter("totalWaterTons") == "" ? "0" : request.getParameter("totalWaterTons");
//        BigDecimal totalWaterTons = new BigDecimal(totalWaterTonsStr);
//        //用电抄表数
//        String totalElecDegreesStr = request.getParameter("totalElecDegrees") == "" ? "0" : request.getParameter("totalElecDegrees");
//        BigDecimal totalElecDegrees = new BigDecimal(totalElecDegreesStr);
//      //用水基准数
//        String waterBaseRead = request.getParameter("waterBaseRead") == "" ? "0" : request.getParameter("waterBaseRead");
//        BigDecimal waterBaseReads = new BigDecimal(waterBaseRead);
//      //用电基准数
//        String elecBaseRead = request.getParameter("elecBaseRead") == "" ? "0" : request.getParameter("elecBaseRead");
//        BigDecimal elecBaseReads = new BigDecimal(elecBaseRead);
//        
//        String dormNumber = request.getParameter("dormNumber");//宿舍编码
//        boolean calcInterval = request.getParameter("calcInterval").isEmpty();//是否取消自动计算当月水电
//        Map result = WaterPowerUtil.getThisMonthWaterPowerTons(ctx, years, dormNumber, totalWaterTons, totalElecDegrees);
//        JSONUtils.writeJson(response, result);
//    }
    
    
    

    /**
     * 获取最后一次水电吨数
     * @throws BOSException
     */
    public void getLastMWaterPowerAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws BOSException {
    	  String dormNumber = request.getParameter("dormNumber");//宿舍编码
    	  String years = request.getParameter("years");//年月
    	  try {
    		  if(StringUtils.isNotBlank(dormNumber) && StringUtils.isNotBlank(years) ) {
    			  Map lastWaterPowerTons = WaterPowerUtil.getLastWaterPowerTons(ctx, years, dormNumber);
    			  JSONUtils.writeJson(response, lastWaterPowerTons);
    		  }
		} catch (SHRWebException e) {
			e.printStackTrace();
		}
    }
    
    
    
    
    
//    /**
//     * 获取上次用水电数
//     * @param request
//     * @param response
//     * @param modelMap
//     */
//    public void getLastWEValAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws BOSException, SHRWebException {
//    	String years = request.getParameter("years");//年月
//    	String dormNumber = request.getParameter("dormNumber");//宿舍编码
//    	Map result = WaterPowerUtil.getLastWEValue(ctx, years, dormNumber );
//    	JSONUtils.writeJson(response, result);
//    }
//    
    
    
    
    
   
}
