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

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
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.springframework.ui.ModelMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Date;
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());
        //try {
        //    FilterInfo filterInfo = new FilterInfo();
        //    FilterItemCollection filterItems = filterInfo.getFilterItems();
        //    filterItems.add(new FilterItemInfo("years", years));
        //    filterItems.add(new FilterItemInfo("dormNumber", dormNumber));
        //    filterItems.add(new FilterItemInfo("id", waterPowerMeterReadingInfo.getId(), CompareType.NOTEQUALS));
        //    IWaterPowerMeterReading iWaterPowerMeterReading = WaterPowerMeterReadingFactory.getLocalInstance(ctx);
        //    boolean exists = iWaterPowerMeterReading.exists(filterInfo);
        //    if (exists) {
        //        throw new ShrWebBizException(dormNumber + "房间已经存在" + formatter.format(years) + "月的抄表信息！");
        //    }
        //} catch (Exception e) {
        //    e.printStackTrace();
        //    throw new ShrWebBizException(e);
        //}
    }

    /**
     * 获取当月用水电吨数
     *
     * @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 date
    // * @param dormNumber
    // * @return
    // * @throws BOSException
    // */
    //private Map getWaterPower(String date, String dormNumber) throws BOSException {
    //    Map<String, BigDecimal> map = new HashMap();
    //    FilterInfo filterInfo = new FilterInfo();
    //    FilterItemCollection filterItems = filterInfo.getFilterItems();
    //    //上个月:previousMonth   上上个月:
    //    filterItems.add(new FilterItemInfo("years", date + "-01 00:00:00"));
    //    filterItems.add(new FilterItemInfo("dormNumber", dormNumber));
    //    EntityViewInfo entityViewInfo = EntityViewInfo.getInstance(filterInfo, null, null);
    //    //水电每月抄表信息
    //    IWaterPowerMeterReading iWaterPowerMeterReading = WaterPowerMeterReadingFactory.getLocalInstance(ctx);
    //    WaterPowerMeterReadingCollection waterPowerMeterReadingCollection = iWaterPowerMeterReading.getWaterPowerMeterReadingCollection(entityViewInfo);
    //
    //    for (int i = 0; i < waterPowerMeterReadingCollection.size(); i++) {
    //        WaterPowerMeterReadingInfo waterPowerMeterReadingInfo = waterPowerMeterReadingCollection.get(i);
    //        map.put("water", waterPowerMeterReadingInfo.getTotalWaterTons());
    //        map.put("power", waterPowerMeterReadingInfo.getTotalElecDegrees());
    //    }
    //    return map;
    //}
    //
    //public Map getDate(String dateStr) {
    //    Map map = new HashMap();
    //    // 定义日期格式
    //    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
    //    // 将字符串转换为 YearMonth 对象
    //    YearMonth targetDate = YearMonth.parse(dateStr, formatter);
    //    // 获取上个月
    //    YearMonth previousMonth = targetDate.minusMonths(1);
    //    map.put("previousMonth", formatter.format(previousMonth));
    //    // 获取上上个月
    //    YearMonth twoMonthsAgo = targetDate.minusMonths(2);
    //    map.put("twoMonthsAgo", formatter.format(twoMonthsAgo));
    //    return map;
    //
    //}
}
