| package com.dy.pipIrrSell.cardOperate; | 
|   | 
| import com.alibaba.fastjson2.JSON; | 
| import com.alibaba.fastjson2.JSONArray; | 
| import com.alibaba.fastjson2.JSONObject; | 
| import com.dy.common.webUtil.BaseResponse; | 
| import com.dy.common.webUtil.BaseResponseUtils; | 
| import com.dy.common.webUtil.QueryResultVo; | 
| import com.dy.pipIrrGlobal.daoSe.SeCardOperateMapper; | 
| import com.dy.pipIrrGlobal.daoSe.SeClientCardMapper; | 
| import com.dy.pipIrrGlobal.daoSe.SeClientMapper; | 
| import com.dy.pipIrrGlobal.daoSe.SeGeneralMapper; | 
| import com.dy.pipIrrGlobal.pojoSe.SeCardOperate; | 
| import com.dy.pipIrrGlobal.pojoSe.SeClientCard; | 
| import com.dy.pipIrrGlobal.voSe.*; | 
| import com.dy.pipIrrSell.cardOperate.dto.DtoRecharge; | 
| import com.dy.pipIrrSell.cardOperate.enums.OperateTypeENUM; | 
| import com.dy.pipIrrSell.cardOperate.qo.*; | 
| import com.dy.pipIrrSell.clientCard.ClientCardSv; | 
| import com.dy.pipIrrSell.clientCard.LastOperateENUM; | 
| import com.dy.pipIrrSell.result.SellResultCode; | 
| import lombok.extern.slf4j.Slf4j; | 
| import org.apache.dubbo.common.utils.PojoUtils; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.stereotype.Service; | 
|   | 
| import java.text.DecimalFormat; | 
| import java.util.*; | 
|   | 
| /** | 
|  * @author ZhuBaoMin | 
|  * @date 2024-01-18 19:36 | 
|  * @LastEditTime 2024-01-18 19:36 | 
|  * @Description | 
|  */ | 
|   | 
| @Slf4j | 
| @Service | 
| public class CardOperateSv { | 
|     @Autowired | 
|     private SeCardOperateMapper seCardOperateMapper; | 
|   | 
|     @Autowired | 
|     private SeClientCardMapper seClientCardMapper; | 
|   | 
|     @Autowired | 
|     private SeClientMapper seClientMapper; | 
|   | 
|     @Autowired | 
|     private ClientCardSv clientCardSv; | 
|   | 
|     @Autowired | 
|     private SeGeneralMapper seGeneralMapper; | 
|   | 
|   | 
|     /** | 
|      * 添加开卡记录 | 
|      * @param po 水卡操作对象 | 
|      * @return 开卡记录主键 | 
|      */ | 
|     public Long activeCard(SeCardOperate po) { | 
|         seCardOperateMapper.insert(po); | 
|         return po.getId(); | 
|     } | 
|   | 
|     /** | 
|      * 根据农户编号获取5级行政区划代码,开卡使用 | 
|      * @param clientNum 农户编号 | 
|      * @return 5级行政区划代码 | 
|      */ | 
|     public Long getAreaCodeByNum(String clientNum) { | 
|         return seClientMapper.getAreaCodeByNum(clientNum); | 
|     } | 
|   | 
|     /** | 
|      * 根据农户编号获取5级行政区划串areaCode,补卡过程中开新卡使用 | 
|      * @param clientId | 
|      * @return | 
|      */ | 
|     public Long getAreaCodeById(Long clientId) { | 
|         return seClientMapper.getAreaCodeById(clientId); | 
|     } | 
|     /** | 
|      * 根据行政区划串模块查询水卡编号,开卡使用 | 
|      * @param areaCode | 
|      * @return | 
|      */ | 
|     String getCardNumOfMax(String areaCode) { | 
|         return seClientCardMapper.getCardNumOfMax(areaCode); | 
|     } | 
|   | 
|     /** | 
|      * 根据农户编号获取农户ID,开卡使用 | 
|      * @param clientNum | 
|      * @return | 
|      */ | 
|     public Long getClientIdByNum(String clientNum) { | 
|         return seClientMapper.getClientIdByNum(clientNum); | 
|     } | 
|   | 
|     /** | 
|      * 添加水卡操作对象,各操作都使用 | 
|      * @param po 水卡操作对象 | 
|      * @return 操作记录主键 | 
|      */ | 
|     public Long add(SeCardOperate po) { | 
|         seCardOperateMapper.insert(po); | 
|         return po.getId(); | 
|     } | 
|   | 
|     /** | 
|      * 添加充值记录 | 
|      * 修改农户信息、添加充值记录 | 
|      * @param po | 
|      * @return | 
|      */ | 
|     public BaseResponse<Boolean> addRecharge(DtoRecharge po){ | 
|         /** | 
|          * cardId           水卡编号(依据水卡编号获取) | 
|          * clientId         农户编号(依据水卡编号获取) | 
|          * cardNum          水卡编号 | 
|          * money            卡片余额 | 
|          * amount           充值金额 | 
|          * gift             赠送金额 | 
|          * afterRecharge    充值后余额 | 
|          * paymentId        支付方式编号 | 
|          * price            水价 | 
|          * remarks          备注 | 
|          * operator         操作人编号 | 
|          * rechargeTime     充值时间 | 
|          */ | 
|         Long cardId = 0L; | 
|         Long clientId = 0L; | 
|         Long cardNum = po.getCardNum(); | 
|         Float money = po.getMoney(); | 
|         Float amount = po.getAmount(); | 
|         Float gift = po.getGift(); | 
|         Float afterRecharge = money + amount + gift; | 
|         Long paymentId = po.getPaymentId(); | 
|         Float price = po.getPrice(); | 
|         String remarks = po.getRemarks(); | 
|         Long operator = po.getOperator(); | 
|         Date rechargeTime = new Date(); | 
|   | 
|   | 
|         // 验证水卡状态是否支持当前操作 | 
|         String stateName = Optional.ofNullable(clientCardSv.getCardStateByCardNum(cardNum)).orElse(""); | 
|         if(stateName.length() == 0 || !stateName.equals("正常")) { | 
|             return BaseResponseUtils.buildFail(stateName + ", " + SellResultCode.THE_CARD_NOT_SUPPORT_THIS_OPERATION.getMessage()); | 
|         } | 
|   | 
|         /** | 
|          * 依据水卡编号获取水卡表主键及农户编号 | 
|          */ | 
|         Map map = Optional.ofNullable(clientCardSv.getCardIdAndClientNum(cardNum)).orElse(new HashMap()); | 
|         if(map == null || map.size() <= 0) { | 
|             return BaseResponseUtils.buildFail(SellResultCode.CARD_NUMBER_MISTAKE.getMessage()); | 
|         } | 
|         cardId = Long.parseLong(map.get("cardId").toString()); | 
|         clientId = Long.parseLong(map.get("clientId").toString()); | 
|   | 
|         /** | 
|          * 修改农户卡信息: | 
|          *      补卡时间 | 
|          *      最后操作类型-2 | 
|          */ | 
|         SeClientCard seClientCard = new SeClientCard(); | 
|         seClientCard.setId(cardId); | 
|         seClientCard.setMoney(afterRecharge); | 
|         seClientCard.setRechargedt(rechargeTime); | 
|         seClientCard.setLastoper(LastOperateENUM.RECHARGE.getCode ()); | 
|         Integer rec_updateClientCard = Optional.ofNullable(clientCardSv.UpdateClientCard(seClientCard)).orElse(0); | 
|         if(rec_updateClientCard == 0) { | 
|             return BaseResponseUtils.buildFail(SellResultCode.RECHARGE_FAIL_WRITE_CLIENT_CARD_ERROR.getMessage()); | 
|         } | 
|   | 
|         /** | 
|          * 添加充值记录 | 
|          */ | 
|         SeCardOperate seCardOperate = new SeCardOperate(); | 
|         seCardOperate.setCardId(cardId); | 
|         seCardOperate.setClientId(clientId); | 
|         seCardOperate.setMoney(money); | 
|         seCardOperate.setTradeAmount(amount); | 
|         seCardOperate.setGift(gift); | 
|         seCardOperate.setOperateType(OperateTypeENUM.RECHARGE.getCode()); | 
|         seCardOperate.setPaymentId(paymentId); | 
|         seCardOperate.setPrice(price); | 
|         seCardOperate.setRemarks(remarks); | 
|         seCardOperate.setOperator(operator); | 
|         seCardOperate.setOperateDt(rechargeTime); | 
|   | 
|         seCardOperateMapper.insert(seCardOperate); | 
|         Long rec = Optional.ofNullable(seCardOperate.getId()).orElse(0L); | 
|         if(rec == 0) { | 
|             return BaseResponseUtils.buildFail(SellResultCode.REPLACE_FAIL_WRITE_RECHARGE_ERROR.getMessage()); | 
|         } | 
|   | 
|         return BaseResponseUtils.buildSuccess(true) ; | 
|     } | 
|   | 
|     /** | 
|      * 根据指定条件获取充值记录 | 
|      * @param queryVo | 
|      * @return | 
|      */ | 
|     public QueryResultVo<List<VoRecharge>> getRecharges(QoRecharge queryVo) { | 
|         //完善查询充值记录的起止时间 | 
|         String rechargeTimeStart = queryVo.rechargeTimeStart; | 
|         String rechargeTimeStop = queryVo.rechargeTimeStop; | 
|         if(rechargeTimeStart != null) { | 
|             rechargeTimeStart = rechargeTimeStart + " 00:00:00"; | 
|             queryVo.setRechargeTimeStart(rechargeTimeStart); | 
|         } | 
|         if(rechargeTimeStop != null) { | 
|             rechargeTimeStop = rechargeTimeStop + " 23:59:59"; | 
|             queryVo.setRechargeTimeStop(rechargeTimeStop); | 
|         } | 
|   | 
|         Map<String, Object> params = (Map<String, Object>) PojoUtils.generalize(queryVo); | 
|   | 
|         Long itemTotal = seCardOperateMapper.getRecordCount(params); | 
|   | 
|         QueryResultVo<List<VoRecharge>> rsVo = new QueryResultVo<>() ; | 
|         rsVo.pageSize = queryVo.pageSize ; | 
|         rsVo.pageCurr = queryVo.pageCurr ; | 
|   | 
|         rsVo.calculateAndSet(itemTotal, params); | 
|         rsVo.obj = seCardOperateMapper.getRecharges(params); | 
|         return rsVo ; | 
|     } | 
|   | 
|     /** | 
|      * 根据指定条件获取交易记录 | 
|      * @param vo | 
|      * @return | 
|      */ | 
|     public Map getTransactions(QoTransaction vo){ | 
|         DecimalFormat df = new DecimalFormat("0.00"); | 
|         // 补齐查询时间 | 
|         String operateTimeStart = vo.operateTimeStart; | 
|         String operateTimeStop = vo.operateTimeStop; | 
|         if(operateTimeStart != null) { | 
|             operateTimeStart = operateTimeStart + " 00:00:00"; | 
|             vo.setOperateTimeStart(operateTimeStart); | 
|         } | 
|         if(operateTimeStop != null) { | 
|             operateTimeStop = operateTimeStop + " 23:59:59"; | 
|             vo.setOperateTimeStop(operateTimeStop); | 
|         } | 
|   | 
|         // 生成查询参数 | 
|         Map<String, Object> params = (Map<String, Object>) PojoUtils.generalize(vo) ; | 
|   | 
|         // 获取符合条件的记录数 | 
|         Long itemTotal = Optional.ofNullable(seCardOperateMapper.getTransactionRecordCount(params)).orElse(0L); | 
|   | 
|         List<VoTradeDetails> list = seCardOperateMapper.getTransactions(params); | 
|         if(list.size() == 0) { | 
|             return new HashMap(); | 
|         } | 
|   | 
|         // 遍历交易明细记录,汇总购水金额、购卡金额 | 
|         Double totalWaterCost = 0.0; | 
|         Double totalCardCost = 0.0; | 
|         JSONArray array= JSONArray.parseArray(JSON.toJSONString(list)); | 
|         for(int i = 0; i < array.size(); i++) { | 
|             JSONObject job = array.getJSONObject(i); | 
|             Double waterCost = Optional.ofNullable(job.getDouble("waterCost")).orElse(0.0); | 
|             Double cardCost = Optional.ofNullable(job.getDouble("cardCost")).orElse(0.0); | 
|             totalWaterCost = totalWaterCost + waterCost; | 
|             totalCardCost = totalCardCost + cardCost; | 
|         } | 
|   | 
|         Map map_record = new HashMap(); | 
|         map_record.put("itemTotal", itemTotal); | 
|         map_record.put("list", list); | 
|   | 
|         Map map_result = new HashMap(); | 
|         map_result.put("waterCost", df.format(totalWaterCost)); | 
|         map_result.put("cardCost", df.format(totalCardCost)); | 
|         map_result.put("records", map_record); | 
|   | 
|         return map_result; | 
|     } | 
|   | 
|     /** | 
|      * 根据指定条件获取开卡记录 | 
|      * @param queryVo | 
|      * @return | 
|      */ | 
|     public QueryResultVo<List<VoActiveCard>> getActiveCards(QoActiveCard queryVo) { | 
|         //完善查询充值记录的起止时间 | 
|         String activeTimeStart = queryVo.activeTimeStart; | 
|         String activeTimeStop = queryVo.activeTimeStop; | 
|         if(activeTimeStart != null) { | 
|             activeTimeStart = activeTimeStart + " 00:00:00"; | 
|             queryVo.setActiveTimeStart(activeTimeStart); | 
|         } | 
|         if(activeTimeStop != null) { | 
|             activeTimeStop = activeTimeStop + " 23:59:59"; | 
|             queryVo.setActiveTimeStop(activeTimeStop); | 
|         } | 
|   | 
|         Map<String, Object> params = (Map<String, Object>) PojoUtils.generalize(queryVo); | 
|   | 
|         Long itemTotal = seCardOperateMapper.getActiveCardRecordCount(params); | 
|   | 
|         QueryResultVo<List<VoActiveCard>> rsVo = new QueryResultVo<>() ; | 
|   | 
|         rsVo.pageSize = queryVo.pageSize ; | 
|         rsVo.pageCurr = queryVo.pageCurr ; | 
|   | 
|         rsVo.calculateAndSet(itemTotal, params); | 
|         rsVo.obj = seCardOperateMapper.getActiveCards(params); | 
|         return rsVo ; | 
|     } | 
|   | 
|     /** | 
|      * 根据指定条件获取补卡记录 | 
|      * @param queryVo | 
|      * @return | 
|      */ | 
|     public QueryResultVo<List<VoReissueCard>> getReissueCards(QoReissueCard queryVo) { | 
|         //完善查询充值记录的起止时间 | 
|         String reissueCardTimeStart = queryVo.reissueCardTimeStart; | 
|         String reissueCardTimeStop = queryVo.reissueCardTimeStop; | 
|         if(reissueCardTimeStart != null) { | 
|             reissueCardTimeStart = reissueCardTimeStart + " 00:00:00"; | 
|             queryVo.setReissueCardTimeStart(reissueCardTimeStart); | 
|         } | 
|         if(reissueCardTimeStop != null) { | 
|             reissueCardTimeStop = reissueCardTimeStop + " 23:59:59"; | 
|             queryVo.setReissueCardTimeStop(reissueCardTimeStop); | 
|         } | 
|   | 
|         Map<String, Object> params = (Map<String, Object>) PojoUtils.generalize(queryVo); | 
|   | 
|         Long itemTotal = seCardOperateMapper.getReissueCardRecordCount(params); | 
|   | 
|         QueryResultVo<List<VoReissueCard>> rsVo = new QueryResultVo<>() ; | 
|   | 
|         rsVo.pageSize = queryVo.pageSize ; | 
|         rsVo.pageCurr = queryVo.pageCurr ; | 
|   | 
|         rsVo.calculateAndSet(itemTotal, params); | 
|         rsVo.obj = seCardOperateMapper.getReissueCards(params); | 
|         return rsVo ; | 
|     } | 
|   | 
|     /** | 
|      * 根据指定条件获取IC卡注销记录 | 
|      * @param queryVo | 
|      * @return | 
|      */ | 
|     public QueryResultVo<List<VoOperate>> getCommonOperations(QoCommonOperate queryVo) { | 
|         //完善查询充值记录的起止时间 | 
|         String timeStart = queryVo.getTimeStart(); | 
|         String timeStop = queryVo.getTimeStop(); | 
|         if(timeStart != null) { | 
|             timeStart = timeStart + " 00:00:00"; | 
|             queryVo.setTimeStart(timeStart); | 
|         } | 
|         if(timeStop != null) { | 
|             timeStop = timeStop + " 23:59:59"; | 
|             queryVo.setTimeStop(timeStop); | 
|         } | 
|   | 
|         Map<String, Object> params = (Map<String, Object>) PojoUtils.generalize(queryVo); | 
|   | 
|         Long itemTotal = Optional.ofNullable(seCardOperateMapper.getCommonOperationRecordCount(params)).orElse(0L); | 
|   | 
|         QueryResultVo<List<VoOperate>> rsVo = new QueryResultVo<>() ; | 
|   | 
|         rsVo.pageSize = queryVo.pageSize ; | 
|         rsVo.pageCurr = queryVo.pageCurr ; | 
|   | 
|         rsVo.calculateAndSet(itemTotal, params); | 
|         rsVo.obj = seCardOperateMapper.getCommonOperations(params); | 
|         return rsVo ; | 
|     } | 
|   | 
|     /** | 
|      * 根据指定条件获取收据列表 | 
|      * @param queryVo | 
|      * @return | 
|      */ | 
|     public Map getReceipts(QoReceipt queryVo) { | 
|         Map<String, Object> params = (Map<String, Object>) PojoUtils.generalize(queryVo); | 
|   | 
|         DecimalFormat df = new DecimalFormat("#.00"); | 
|         Double totalAmount = Optional.ofNullable(seCardOperateMapper.getTotalAmount(params)).orElse(0.0); | 
|   | 
|         Long itemTotal = seCardOperateMapper.getReceiptsRecordCount(params); | 
|   | 
|         QueryResultVo<List<VoReceipt>> rsVo = new QueryResultVo<>() ; | 
|         rsVo.pageSize = queryVo.pageSize ; | 
|         rsVo.pageCurr = queryVo.pageCurr ; | 
|   | 
|         rsVo.calculateAndSet(itemTotal, params); | 
|         //rsVo.obj = seCardOperateMapper.getReceipts(params); | 
|         //return rsVo ; | 
|   | 
|         List<VoReceipt> list = seCardOperateMapper.getReceipts(params); | 
|         Map map_record = new HashMap(); | 
|         map_record.put("itemTotal", rsVo.itemTotal); | 
|         map_record.put("pageCurr", rsVo.pageCurr); | 
|         map_record.put("pageSize", rsVo.pageSize); | 
|         map_record.put("pageTotal", rsVo.pageTotal); | 
|         map_record.put("list", list); | 
|   | 
|         Map map_result = new HashMap(); | 
|         map_result.put("totalAmount", df.format(totalAmount)); | 
|         map_result.put("records", map_record); | 
|   | 
|         return map_result; | 
|     } | 
|   | 
|     /** | 
|      * 判断指定水卡是否为挂失状态且无补卡记录 | 
|      * @param cardNum 水卡编号 | 
|      * @return 是否已挂失未补卡 | 
|      */ | 
|     public Boolean isLostAndUnreplaced(Long cardNum) { | 
|         Integer rec = seClientCardMapper.isLostAndUnreplaced(cardNum); | 
|         if(rec != null && rec > 0) { | 
|             return false; | 
|         } | 
|         return true; | 
|     } | 
|   | 
|     /** | 
|      * 修改农户水卡 | 
|      * @param po | 
|      * @return | 
|      */ | 
|     public Integer updateClientCard(SeClientCard po) { | 
|         return seClientCardMapper.updateByPrimaryKeySelective(po); | 
|     } | 
| } |