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

import com.kingdee.bos.Context;
import com.kingdee.eas.custom.dormitorysystem.dormitory.*;
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.ats.web.util.NumberCodeRule;
import com.kingdee.shr.base.syssetting.app.io.fileImport.BaseImportService;
import com.kingdee.shr.base.syssetting.app.io.fileImport.BaseRowInfo;
import com.kingdee.shr.base.syssetting.app.io.fileImport.ImportException;
import org.apache.log4j.Logger;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @author qingwu
 * @date 2025/4/22
 * @apiNote 水电每月抄表
 */
public class WaterPowerMeterReadingImportService extends BaseImportService {
    Logger logger = Logger.getLogger(this.getClass());

    @Override
    protected void verifyRow(BaseRowInfo row) throws ImportException {
        super.verifyRow(row);
        String dormNumber = row.getValueOfString("dormitory");//房间房号
        logger.error("verifyRow--dormNumber--" + dormNumber);
        String[] parts = dormNumber.split("##");
        String years = row.getValueOfString("years");//年月
        String totalWaterTons = row.getValueOfString("totalWaterTons");//用水总吨数
        String totalElecDegrees = row.getValueOfString("totalElecDegrees");//用电总度数
        Map dataMap = WaterPowerUtil.getDate(years.substring(0, years.lastIndexOf("-")));
        String previousMonth = (String) dataMap.get("previousMonth");
        try {
            Map<String, BigDecimal> waterPowerMap = WaterPowerUtil.getWaterPower(this.getContext(), previousMonth, dormNumber,true);
            if (waterPowerMap.size() > 0) {
                BigDecimal water = waterPowerMap.get("water");//上月总水费
                BigDecimal power = waterPowerMap.get("power");//上月总电费
                BigDecimal waterTons = new BigDecimal(totalWaterTons);
                BigDecimal elecDegrees = new BigDecimal(totalElecDegrees);
                //用水总吨数：水表累计读数，≥上月总吨数，且≥0；
                //用电总度数：电表累计读数，≥上月总度数，且≥0；
                if ((waterTons.compareTo(BigDecimal.ZERO) > 0 && elecDegrees.compareTo(BigDecimal.ZERO) > 0) &&
                        (elecDegrees.compareTo(power) < 0 && elecDegrees.compareTo(water) < 0)
                ) {
                    throw new ImportException("本月水电总吨数不可小于上月水电总吨数！");
                }
            }
            IDormitory iDormitory = DormitoryFactory.getLocalInstance(this.getContext());
            boolean exists = iDormitory.exists("where number ='" + dormNumber + "'");
            if (!exists) {
                throw new ImportException(dormNumber + "该房间不存在！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new ImportException(e);
        }
        //throw new ImportException(housePlace + roomNumber + "房间不存在，请在员工信息人才房信息进行维护！！");
    }

    @Override
    protected void beforeSubmitData(CoreBaseInfo coreBaseInfo) throws ImportException {
        Context ctx = this.getContext();
        WaterPowerMeterReadingInfo wpmrinfo = (WaterPowerMeterReadingInfo) coreBaseInfo;
        BigDecimal totalWaterTons = wpmrinfo.getTotalWaterTons();
        BigDecimal totalElecDegrees = wpmrinfo.getTotalElecDegrees();
        Date years = wpmrinfo.getYears();
        String dormNumber = wpmrinfo.getDormitory().getNumber();
        logger.error("beforeSubmitData--dormNumber--" + dormNumber);
        try {
            IDormitory iDormitory = DormitoryFactory.getLocalInstance(ctx);
            DormitoryCollection dormitoryCollection = iDormitory.getDormitoryCollection("where number ='" + dormNumber + "'");
            logger.error("beforeSubmitData--dormitoryCollection--" + dormitoryCollection.size());
            if (dormitoryCollection.size() > 0) {
                DormitoryInfo dormitoryInfo = dormitoryCollection.get(0);
                wpmrinfo.setDormitory(dormitoryInfo);
            }
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
            Map<String, BigDecimal> result = WaterPowerUtil.getThisMonthWaterPowerTons(ctx, formatter.format(years), dormNumber, totalWaterTons, totalElecDegrees);
            String number = NumberCodeRule.readCodeRuleNumber(ctx, wpmrinfo, NumberCodeRule.getMainOrgByCu(ctx));
            logger.error("beforeSubmitData--number--" + number);
            wpmrinfo.setNumber(number);
            wpmrinfo.setDormNumber(dormNumber);
            wpmrinfo.setCurMonthWater(result.get("curMonthWater"));
            wpmrinfo.setCurMonthElecDegrees(result.get("curMonthElecDegrees"));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        super.beforeSubmitData(coreBaseInfo);
    }
}
