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

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.framework.ejb.EJBFactory;
import com.kingdee.bos.metadata.entity.*;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.basedata.org.AdminOrgUnitInfo;
import com.kingdee.eas.basedata.org.HROrgUnitInfo;
import com.kingdee.eas.basedata.org.PositionInfo;
import com.kingdee.eas.basedata.person.PersonInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.dormitorysystem.dormitory.DormitoryInfo;
import com.kingdee.eas.custom.dormitorysystem.waterelectricity.*;
import com.kingdee.eas.hr.base.HRBillStateEnum;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.jdbc.rowset.IRowSet;
import com.kingdee.shr.ats.web.util.SHRBillUtil;
import com.kingdee.shr.base.syssetting.api.bean.BatchMessageTipsHeader;
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.request.ParseRequestHelper;
import com.kingdee.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.ModelMap;
import shaded.com.google.common.collect.Lists;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.Instant;
import java.time.LocalDate;
import java.time.Month;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * 特殊水电费明细计算
 * description: WaterElectricityListHandler <br>
 * date: 2025/4/24 18:09 <br>
 * author: lhbj <br>
 * version: 1.0 <br>
 */
public class WaterElectricitySpecialListHandler extends WaterElectricityBatchListHandler {
    private static Logger logger = LoggerFactory.getLogger(WaterElectricitySpecialListHandler.class);
    public static final String TABLENAME = "CT_WOE_WaterElectricitySpecial";

    /**
     * 获取水电费详细Sql
     * wey.FBizDate CFCheckInDate,wey.CFLeaveDate CFCheckOutDate,wey.CFDormitoryID dormitoryID
     * @param startPeriodDateStr
     * @param endPeriodDateStr
     * @return
     */
    protected StringBuilder getPersonWaterElectricitySql(String startPeriodDateStr, String endPeriodDateStr, Collection<String> billids) {
        StringBuilder sel = new StringBuilder();
        sel.append(" select  p.fid pid,p.fname_l2 pName,p.fnumber pNumber,ops.fid opsid,wey.FBizDate CFCheckInDate,wey.CFLeaveDate CFCheckOutDate,wey.CFDormitoryID dormitoryID, ");
        sel.append(" wpp.fid wppid,wpp.CFStartDate,wpp.CFEndDate,wpp.CFWaterprice,wpp.CFElectricityprice, ");
        sel.append(" wey.fid wpmrid,wey.CFStartDate wpmrStartDate,wey.CFEndDate wpmrEndDate, ");
        sel.append(" isnull(wey.CFWaterTotalCost,0) CFTotalWaterTons,isnull(wey.CFElectricityTotalCost,0) CFTotalElecDegrees, ");
        sel.append(" isnull(wey.CFSpecialWaterTotal,0) CFCurMonthWater,isnull(wey.CFSpecialElectricityTotal,0) CFCurMonthElecDegrees, ");
//        sel.append(" wpmr.fid wpmrid,wpmr.CFYears wpmrStartDate,DATEADD(DD,-1,DATEADD(MM,DATEDIFF(MM,0,wpmr.CFYears)+1,0)) wpmrEndDate, ");
//        sel.append(" isnull(wpmr.CFTotalWaterTons,0) CFTotalWaterTons,isnull(wpmr.CFTotalElecDegrees,0) CFTotalElecDegrees, ");
//        sel.append(" isnull(wpmr.CFCurMonthWater,0) CFCurMonthWater,isnull(wpmr.CFCurMonthElecDegrees,0) CFCurMonthElecDegrees, ");
        sel.append(" pp.FPrimaryPositionID,pp.FPersonDep, ");
//        sel.append(" case ");
        sel.append(" bt.fname_l2  InService  ");
//        sel.append(" when 1 then 'active' ");
//        sel.append(" when 4 then 'active' ");
//        sel.append(" else 'inactive' ");
//        sel.append(" end as InService ");
        sel.append(" from CT_WOE_WaterElectricitySpecial wey ");
        sel.append(" left join t_bd_person p on wey.CFPERSONID = p.fid ");
        sel.append(" left join T_HR_PersonPosition pp on pp.FPERSONID = p.fid ");
        sel.append(" left join CT_OCC_Occupants ops on ops.CFPersonPositionID = pp.FID ");
        sel.append(" and wey.CFDormitoryID=ops.CFDormitoryID ");
        sel.append(" and ops.CFCheckOutDate >= wey.CFStartDate ");
        sel.append(" and ops.CFCheckInDate <=wey.CFEndDate ");
        sel.append(" left join T_HR_BDEmployeeType bt on bt.fid = p.FEmployeeTypeID ");
        sel.append(" left join CT_DOR_Dormitory dy on ops.CFDormitoryID=dy.fid ");
        sel.append(" left join CT_WAT_WaterPowerPricing wpp on wpp.CFStartDate<=wey.CFEndDate ");
        sel.append(" and wey.CFStartDate<=wpp.CFEndDate ");
//        sel.append(" left join CT_MAN_WaterPowerMeterReading wpmr on wpmr.CFDormitoryID=ops.CFDormitoryID ");
//        sel.append(" and wpmr.CFYears<=ops.CFCheckOutDate ");
//        sel.append(" and DATEADD(DD,-1,DATEADD(MM,DATEDIFF(MM,0,wpmr.CFYears)+1,0))>= ops.CFCheckInDate ");
        sel.append(" where ");
        sel.append(" wey.CFStartDate='" + startPeriodDateStr + "' ");
        sel.append(" and wey.CFEndDate='" + endPeriodDateStr + "' ");
        if (null != billids && (!billids.isEmpty())) {
            sel.append(" and wey.fid in('' ");
            for (String id : billids) {
                sel.append(",'");
                sel.append(id);
                sel.append("'");
            }
            sel.append(")");
        }
        System.out.println("getPersonWaterElectricitySql:" + sel);
        return sel;
    }

    /**
     * 获取水电费详细集合
     *
     * @param ctx
     * @param startPeriodDateStr
     * @param endPeriodDateStr
     * @return
     * @throws BOSException
     * @throws SQLException
     */
    protected List<Map<String, Object>> getPersonWaterElectricityList(Context ctx,
                                                                      String startPeriodDateStr, String endPeriodDateStr, Collection<String> billids)
            throws BOSException, SQLException, ShrWebBizException {

        StringBuilder sel = this.getPersonWaterElectricitySql(startPeriodDateStr, endPeriodDateStr, billids);
        IRowSet rs = DbUtil.executeQuery(ctx, sel.toString());
        List<Map<String, Object>> list = Lists.newArrayList();
        while (rs.next()) {
            Map<String, Object> rowMap = Maps.newHashMap();
            String pid = rs.getString("pid");
            String pName = rs.getString("pName");
            String pNumber = rs.getString("pNumber");
            String opsid = rs.getString("opsid");
            String InService = rs.getString("InService");
            Date checkInDate = rs.getDate("CFCheckInDate");//入住日期
            Date checkOutDate = rs.getDate("CFCheckOutDate");//离宿日期
            String dormitoryID = rs.getString("dormitoryID");//宿舍
            String wppid = rs.getString("wppid");//标准id
            Date startDate = rs.getDate("CFStartDate");//标准开始日期
            Date endDate = rs.getDate("CFEndDate");//标准结束日期
            BigDecimal waterprice = rs.getBigDecimal("CFWaterprice");//水费单价
            if (null==waterprice){
                throw new ShrWebBizException("没有找到水费电费标准单据，请在水电定价标准中维护！");
            }
            BigDecimal electricityprice = rs.getBigDecimal("CFElectricityprice");//电费单价
            String wpmrid = rs.getString("wpmrid");
            Date wpmrStartDate = rs.getDate("wpmrStartDate");
            Date wpmrEndDate = rs.getDate("wpmrEndDate");
            BigDecimal totalWaterTons = rs.getBigDecimal("CFTotalWaterTons");
            BigDecimal totalElecDegrees = rs.getBigDecimal("CFTotalElecDegrees");
            BigDecimal curMonthWater = rs.getBigDecimal("CFCurMonthWater");
            BigDecimal curMonthElecDegrees = rs.getBigDecimal("CFCurMonthElecDegrees");
            String FPrimaryPositionID = rs.getString("FPrimaryPositionID");
            String FPersonDep = rs.getString("FPersonDep");
            rowMap.put("pid", pid);
            rowMap.put("pName", pName);
            rowMap.put("pNumber", pNumber);
            rowMap.put("opsid", opsid);
            rowMap.put("InService", InService);
            rowMap.put("checkInDate", checkInDate);
            rowMap.put("checkOutDate", checkOutDate);
            rowMap.put("dormitoryID", dormitoryID);
            rowMap.put("wppid", wppid);
            rowMap.put("startDate", startDate);
            rowMap.put("endDate", endDate);
            rowMap.put("waterprice", waterprice);
            rowMap.put("electricityprice", electricityprice);
            rowMap.put("wpmrid", wpmrid);
            rowMap.put("wpmrStartDate", wpmrStartDate);
            rowMap.put("wpmrEndDate", wpmrEndDate);
            rowMap.put("totalWaterTons", totalWaterTons);
            rowMap.put("totalElecDegrees", totalElecDegrees);
            rowMap.put("curMonthWater", curMonthWater);
            rowMap.put("curMonthElecDegrees", curMonthElecDegrees);
            rowMap.put("primaryPositionID", FPrimaryPositionID);
            rowMap.put("personDep", FPersonDep);
            list.add(rowMap);
        }
        return list;
    }

    /**
     * 生成水电费明细
     *
     * @param request
     * @param response
     * @param modelMap
     * @throws Exception
     */
    public void createMonthAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap, BatchMessageTipsHeader batchMessageTipsHeader) throws SHRWebException {
        this.calculateAction(request, response, modelMap, batchMessageTipsHeader);
    }

    /**
     * 计算
     * {"startDate":
     * {"values":{"date":"2025-04-01"},"isSaveToScheme":true,"dataType":"Date","queryDateType":""},
     * "applyDate":
     * {"values":"","isSaveToScheme":true,"dataType":"Date","dataItem":"","queryDateType":""}}
     *
     * @param request
     * @param response
     * @param modelMap
     * @return
     * @throws SHRWebException
     */
    public void calculateAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap, BatchMessageTipsHeader batchMessageTipsHeader) throws SHRWebException {
        Context ctx = SHRContext.getInstance().getContext();
        try {
            String billidStr = this.getBillId(request);
            Set<String> billids = Sets.newHashSet();
            if (!StringUtils.isEmpty(billidStr)) {
                billids.addAll(Arrays.asList(billidStr.split(",")));
            }
            String periodDate = (String) ParseRequestHelper.getParameterByRequest(request, "periodDate");
            LocalDate startPeriodDate = LocalDate.parse(periodDate + "-01", DateTimeFormatter.ISO_LOCAL_DATE);
            Month month = startPeriodDate.getMonth();
            LocalDate endPeriodDate = LocalDate.of(startPeriodDate.getYear(), month, month.length(startPeriodDate.isLeapYear()));
            //获取开始时间，结束时间
            String startPeriodDateStr = startPeriodDate.format(DateTimeFormatter.ISO_LOCAL_DATE);
            String endPeriodDateStr = endPeriodDate.format(DateTimeFormatter.ISO_LOCAL_DATE);
            Map<String, Object> param = Maps.newHashMap();
            param.put("periodDate", periodDate);
            param.put("startPeriodDate", startPeriodDate);
            param.put("endPeriodDate", endPeriodDate);
            param.put("startPeriodDateStr", startPeriodDateStr);
            param.put("endPeriodDateStr", endPeriodDateStr);

            //获取水电费详细集合
            List<Map<String, Object>> mapList = this.getPersonWaterElectricityList(ctx, startPeriodDateStr, endPeriodDateStr, billids);
            Set<String> dormitorySet = Sets.newHashSet();
            for (Map<String, Object> map : mapList) {
                String dormitoryID = (String) map.get("dormitoryID");//宿舍
                dormitorySet.add(dormitoryID);
            }
            logger.error("获取水电费详细集合:" + JSONUtils.convertObjectToJson(ctx, mapList));
            //获取当前宿舍的总入住天数
            Map<String, Map<String, Object>> occupantsMap = this.getPersonOccupantsList(ctx, dormitorySet, startPeriodDate, endPeriodDate);
            logger.error("获取当前宿舍的总入住天数:" + JSONUtils.convertObjectToJson(ctx, occupantsMap));
            Map<String, Set<String>> result = this.createUpdateWE(ctx, mapList, param, occupantsMap, "update");
            logger.error("结果:" + JSONUtils.convertObjectToJson(ctx, result));
            this.returnMessage(request, batchMessageTipsHeader, result, billids);

        } catch (BOSException e) {
            e.printStackTrace();
            throw new ShrWebBizException(e);
        } catch (EASBizException e) {
            e.printStackTrace();
            throw new ShrWebBizException(e);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new ShrWebBizException(e);
        }
    }

    protected Map<String, Set<String>> createUpdateWE(Context ctx, List<Map<String, Object>> mapList, Map<String, Object> param,
                                                      Map<String, Map<String, Object>> occupantsMap, String createUpdate
    ) throws BOSException, ShrWebBizException, EASBizException {
        Map<String, Set<String>> resutMap = Maps.newHashMap();
        Set<String> alls = Sets.newHashSet();
        Set<String> sess = Sets.newHashSet();
        Set<String> errs = Sets.newHashSet();
        LocalDate startPeriodDate = (LocalDate) param.get("startPeriodDate");
        LocalDate endPeriodDate = (LocalDate) param.get("endPeriodDate");
        String startPeriodDateStr = (String) param.get("startPeriodDateStr");
        String endPeriodDateStr = (String) param.get("endPeriodDateStr");
        String periodDate = (String) param.get("periodDate");
        IWaterElectricitySpecial we = WaterElectricitySpecialFactory.getLocalInstance(ctx);
        long index = 1000;
        for (Map<String, Object> map : mapList) {
            map.putAll(param);
            String pid = (String) map.get("pid");
            String pName = (String) map.get("pName");
            String pNumber = (String) map.get("pNumber");
            String opsid = (String) map.get("opsid");
            String InService = (String) map.get("InService");
            Date checkInDate = (Date) map.get("checkInDate");//入住日期
            Date checkOutDate = (Date) map.get("checkOutDate");//离宿日期
            String dormitoryID = (String) map.get("dormitoryID");//宿舍
            String wppid = (String) map.get("wppid");//标准id
            Date startDate = (Date) map.get("startDate");//标准开始日期
            Date endDate = (Date) map.get("endDate");//标准结束日期
            BigDecimal waterprice = (BigDecimal) map.get("waterprice");//水费单价
            BigDecimal electricityprice = (BigDecimal) map.get("electricityprice");//电费单价
            String wpmrid = (String) map.get("wpmrid");
            Date wpmrStartDate = (Date) map.get("wpmrStartDate");
            Date wpmrEndDate = (Date) map.get("wpmrEndDate");
            BigDecimal totalWaterTons = (BigDecimal) map.get("totalWaterTons");
            BigDecimal totalElecDegrees = (BigDecimal) map.get("totalElecDegrees");
            BigDecimal curMonthWater = (BigDecimal) map.get("curMonthWater");
            BigDecimal curMonthElecDegrees = (BigDecimal) map.get("curMonthElecDegrees");
            String primaryPositionID = (String) map.get("primaryPositionID");
            String personDep = (String) map.get("personDep");
            WaterElectricitySpecialInfo waterElectricityInfo = this.getWaterElectricityInfo(we, periodDate, pid, dormitoryID);
            if (null == waterElectricityInfo) {
                waterElectricityInfo = new WaterElectricitySpecialInfo();
            }
            if (StringUtils.isEmpty(waterElectricityInfo.getNumber())) {
                waterElectricityInfo.setNumber("TWE" + LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE) + index);
            }
            logger.error("获取waterElectricityInfo:" + JSONUtils.convertObjectToJson(ctx, waterElectricityInfo));
            waterElectricityInfo.setSourceFunction(opsid);
            //处理当月入住天数
            Map<String, Object> occupants = occupantsMap.get(dormitoryID);
            HROrgUnitInfo hrOrgUnitInfo = new HROrgUnitInfo();
            hrOrgUnitInfo.setId(BOSUuid.read("00000000-0000-0000-0000-000000000000CCE7AED4"));
            waterElectricityInfo.setHrOrgUnit(hrOrgUnitInfo);
            waterElectricityInfo.setYearMonth(periodDate);
            PersonInfo personInfo = new PersonInfo();
            personInfo.setId(BOSUuid.read(pid));
            waterElectricityInfo.setPerson(personInfo);
            waterElectricityInfo.setEmpName(pName);
            waterElectricityInfo.setEmpNumber(pNumber);
            waterElectricityInfo.setPositionStatus(InService);
            AdminOrgUnitInfo adminOrgUnitInfo = new AdminOrgUnitInfo();
            adminOrgUnitInfo.setId(BOSUuid.read(personDep));
            waterElectricityInfo.setAdminOrg(adminOrgUnitInfo);
            waterElectricityInfo.setBizDate(checkInDate);
            waterElectricityInfo.setLeaveDate(checkOutDate);
            waterElectricityInfo.put("curMonthWater",curMonthWater);
            waterElectricityInfo.put("curMonthElecDegrees",curMonthElecDegrees);
            DormitoryInfo dormitoryInfo = new DormitoryInfo();
            dormitoryInfo.setId(BOSUuid.read(dormitoryID));
            waterElectricityInfo.setDormitory(dormitoryInfo);

            this.calculateWaterElectricity(waterElectricityInfo, map, occupants);

            waterElectricityInfo.setMeterReading(wpmrid);
            waterElectricityInfo.setHydropowerStandards(wppid);
            PositionInfo positionInfo = new PositionInfo();
            positionInfo.setId(BOSUuid.read(primaryPositionID));
            waterElectricityInfo.setPosition(positionInfo);
            Instant instant1 = startPeriodDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
            // 获取时间戳（毫秒）
            long timestamp1 = instant1.toEpochMilli();
            waterElectricityInfo.setStartDate(new Date(timestamp1));
            Instant instant2 = endPeriodDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
            // 获取时间戳（毫秒）
            long timestamp2 = instant2.toEpochMilli();
            waterElectricityInfo.setEndDate(new Date(timestamp2));
            waterElectricityInfo.setApplyDate(checkInDate);
            PersonInfo applier = SHRBillUtil.getCurrPersonInfo();
            waterElectricityInfo.setApplier(applier);

            BOSUuid id = waterElectricityInfo.getId();
            if (null == id) {
                waterElectricityInfo.setStutas(WaterElectricityStutas.UnLock);
                waterElectricityInfo.setBillState(HRBillStateEnum.SAVED);
            }
            logger.error("保存前waterElectricityInfo:" + JSONUtils.convertObjectToJson(ctx, waterElectricityInfo));
            if ("update".equals(createUpdate)) {
                if (null != id && WaterElectricityStutas.UnLock.equals(waterElectricityInfo.getStutas())) {
                    IObjectPK pk = we.save(waterElectricityInfo);
                    sess.add(pk.toString());
                } else if (null != id) {
                    errs.add(id.toString());
                }
            }else {
                if (WaterElectricityStutas.UnLock.equals(waterElectricityInfo.getStutas())) {
                    IObjectPK pk = we.save(waterElectricityInfo);
                    sess.add(pk.toString());
                }
            }
            index++;
        }
        resutMap.put("alls", alls);
        resutMap.put("errs", errs);
        resutMap.put("sess", sess);
        return resutMap;
    }

    /**
     * 获取人员入住详情
     *
     * @param we
     * @param periodDate
     * @param pid
     * @param dormitoryID
     * @return
     * @throws BOSException
     */
    protected WaterElectricitySpecialInfo getWaterElectricityInfo(IWaterElectricitySpecial we,
                                                                  String periodDate, String pid, String dormitoryID
    ) throws BOSException {
        FilterInfo filter = new FilterInfo();
        filter.getFilterItems().add(new FilterItemInfo("yearMonth", periodDate, CompareType.EQUALS));
        filter.getFilterItems().add(new FilterItemInfo("person.id", pid, CompareType.EQUALS));
        filter.getFilterItems().add(new FilterItemInfo("dormitory.id", dormitoryID, CompareType.EQUALS));
        SelectorItemCollection selector = new SelectorItemCollection();
        selector.add(new SelectorItemInfo("*"));
        selector.add(new SelectorItemInfo("entrys.*"));
        selector.add(new SelectorItemInfo("person.*"));
        selector.add(new SelectorItemInfo("dormitory.*"));
        selector.add(new SelectorItemInfo("adminOrg.*"));
        selector.add(new SelectorItemInfo("position.*"));
        SorterItemCollection sorter = new SorterItemCollection();
        sorter.add(new SorterItemInfo("yearMonth"));
        EntityViewInfo viewInfo = EntityViewInfo.getInstance(filter, selector, sorter);
        WaterElectricitySpecialCollection wec = we.getWaterElectricitySpecialCollection(viewInfo);
        WaterElectricitySpecialInfo waterElectricitySpecialInfo = null;
        if (null != wec && wec.size() > 0) {
            waterElectricitySpecialInfo = wec.get(0);
        }
        return waterElectricitySpecialInfo;
    }

    @Override
    public String getTableName() {
        return TABLENAME;
    }

    /**
     * 锁定
     *
     * @param request
     * @param response
     * @param modelMap
     * @return
     * @throws SHRWebException
     */
    public String enableAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws SHRWebException {
        String billId = this.getBillId(request);
        Context ctx = SHRContext.getInstance().getContext();
        enable(ctx, billId);
        return null;
    }

    public static String enable(Context ctx, String billId) {
        if (null != billId) {
            String[] ids = billId.split(",");
            for (String id : ids) {
                try {
                    updateBillState(ctx, "enable", id);
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
        return billId;
    }

    /**
     * 解锁
     *
     * @param request
     * @param response
     * @param modelMap
     * @return
     * @throws SHRWebException
     */
    public String disableAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws SHRWebException {
        String billId = this.getBillId(request);
        Context ctx = SHRContext.getInstance().getContext();
        disable(ctx, billId);
        return null;
    }

    public static String disable(Context ctx, String billId) {
        if (null != billId) {
            String[] ids = billId.split(",");

            for (String id : ids) {
                try {
                    updateBillState(ctx, "disable", id);
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
        return billId;
    }

    public static void updateBillState(Context ctx, String math, String uuid) throws SQLException {
        Connection conn = null;
        Statement state = null;

        try {
            conn = EJBFactory.getQueryConnection(ctx);
            state = conn.createStatement();
            if ("enable".equals(math)) {

                //薪酬锁定
                state.execute("update " + TABLENAME + " set FBillState='2' where fid='" + uuid + "' and FBillState < 2 ");

            } else if ("disable".equals(math)) {
                //薪酬解锁
                state.execute("update " + TABLENAME + " set FBillState='0' where fid='" + uuid + "' and FBillState < 3 ");
            }
        } catch (SQLException var32) {
            var32.printStackTrace();
        } finally {
            if (state != null) {
                try {
                    state.close();
                } finally {
                    if (conn != null) {
                        conn.close();
                    }
                }
            }
        }
    }
}
