选装售后
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FssPaccountPayServiceImpl.java 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. package com.qtzl.alterSales.manager.service;
  2. import cn.com.taiji.common.manager.net.http.ServiceHandleException;
  3. import cn.hutool.core.bean.BeanUtil;
  4. import com.alibaba.fastjson.JSON;
  5. import com.google.common.collect.Lists;
  6. import com.qtzl.alterSales.dao.entity.primary.AflApPayOrder;
  7. import com.qtzl.alterSales.dao.entity.primary.FssPaccountPay;
  8. import com.qtzl.alterSales.dao.entity.primary.WechatBillPayApply;
  9. import com.qtzl.alterSales.dao.entity.primary.WechatBillPaySuccess;
  10. import com.qtzl.alterSales.dao.entity.second.AflPaccountRefundCount;
  11. import com.qtzl.alterSales.dao.repo.jpa.primary.AflApPayOrderRepo;
  12. import com.qtzl.alterSales.dao.repo.jpa.primary.AflSupplementaryPayOrderRepo;
  13. import com.qtzl.alterSales.dao.repo.jpa.primary.FssPaccountPayRepo;
  14. import com.qtzl.alterSales.dao.repo.jpa.primary.FssPaccountRefundRepo;
  15. import com.qtzl.alterSales.dao.repo.jpa.primary.WechatBillPayApplyRepo;
  16. import com.qtzl.alterSales.dao.repo.jpa.primary.WechatBillPaySuccessRepo;
  17. import com.qtzl.alterSales.dao.repo.jpa.second.AflPaccountRefundCountRepo;
  18. import com.qtzl.alterSales.manager.enums.VehiclePlateColorEnum;
  19. import com.qtzl.alterSales.manager.model.protocol.UcServiceError;
  20. import com.qtzl.alterSales.manager.model.protocol.sales.FssPaccountPayResultFindByPayIdResponse;
  21. import com.qtzl.alterSales.manager.vo.AflPaccountRefundCountFindVo;
  22. import com.qtzl.alterSales.manager.vo.VehicleAgreementNumBillsVo;
  23. import org.apache.commons.lang3.StringUtils;
  24. import org.slf4j.Logger;
  25. import org.slf4j.LoggerFactory;
  26. import org.springframework.beans.BeanUtils;
  27. import org.springframework.stereotype.Service;
  28. import org.springframework.util.CollectionUtils;
  29. import javax.annotation.Resource;
  30. import java.time.LocalDate;
  31. import java.time.LocalDateTime;
  32. import java.util.ArrayList;
  33. import java.util.List;
  34. import java.util.Map;
  35. import java.util.UUID;
  36. /***
  37. * <p>
  38. * 选装-原始流水 service
  39. * </p>
  40. * @author hou yi
  41. * {@code @date} 2023/10/12 11:43
  42. **/
  43. @Service
  44. public class FssPaccountPayServiceImpl implements FssPaccountPayService {
  45. private static final Logger log = LoggerFactory.getLogger(FssPaccountPayServiceImpl.class);
  46. @Resource
  47. private FssPaccountPayRepo paccountPayRepo;
  48. @Resource
  49. private AflPaccountRefundCountRepo aflPaccountRefundCountRepo;
  50. @Resource
  51. private FssPaccountRefundRepo fssPaccountRefundRepo;
  52. @Resource
  53. private AflSupplementaryPayOrderRepo supplementaryPayOrderRepo;
  54. @Resource
  55. private WechatBillPayApplyRepo wechatBillPayApplyRepo;
  56. @Resource
  57. private WechatBillPaySuccessRepo successRepo;
  58. @Resource
  59. private AflApPayOrderRepo apPayOrderRepo;
  60. @Override
  61. public List<VehicleAgreementNumBillsVo> getAgreementBills(String plateNumber, Integer plateColor) throws ServiceHandleException {
  62. if (StringUtils.isEmpty(plateNumber) || null == plateColor) {
  63. throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("请指定车牌号、车牌颜色");
  64. }
  65. List<FssPaccountPay> agreementBills = paccountPayRepo.findByVehicleId(plateNumber + "_" + plateColor);
  66. if (CollectionUtils.isEmpty(agreementBills)) {
  67. return Lists.newArrayList();
  68. }
  69. /* // 过滤掉已生成补缴单的流水
  70. final List<String> payIds = agreementBills.stream().map(FssPaccountPay::getPayId).toList();
  71. final List<AflSupplementaryPayOrder> orders = supplementaryPayOrderRepo.findAll((root, query, cb) -> query
  72. .where(
  73. root.<String>get("payId").in(payIds)
  74. ).getRestriction());
  75. if (!CollectionUtils.isEmpty(orders)) {
  76. final List<String> orderPayIds = orders.stream().map(AflSupplementaryPayOrder::getPayId).toList();
  77. agreementBills = agreementBills.stream().filter(vo -> !orderPayIds.contains(vo.getPayId())).toList();
  78. }*/
  79. return toVo(agreementBills);
  80. }
  81. @Override
  82. public List<VehicleAgreementNumBillsVo> findByPayId(List<String> payIds) {
  83. if (CollectionUtils.isEmpty(payIds)) {
  84. return Lists.newArrayList();
  85. }
  86. return toVo(paccountPayRepo.findByPayIdIn(payIds));
  87. }
  88. @Override
  89. public void count(int day) throws ServiceHandleException {
  90. // 获取当前日期前一天的日期
  91. LocalDate localDate = LocalDate.now().minusDays(day);//1代表提前多少天
  92. String yesterday = localDate.toString();
  93. System.out.println("当前导入记录日期=" + yesterday);
  94. Integer i = aflPaccountRefundCountRepo.findByToDay(yesterday.replaceAll("-", ""));
  95. if (i != null && i > 0) {
  96. throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("当前n+" + day + "天记录已存在");
  97. }
  98. List<Map<String, Object>> countTotal = paccountPayRepo.findCountTotal(yesterday + " 00:00:00", yesterday + " " +
  99. "23:59:59");
  100. List<AflPaccountRefundCountFindVo> list = new ArrayList<AflPaccountRefundCountFindVo>();
  101. for (Map<String, Object> stringObjectMap : countTotal) {
  102. AflPaccountRefundCountFindVo aflPaccountRefundCountVo =
  103. JSON.parseObject(JSON.toJSONString(stringObjectMap), AflPaccountRefundCountFindVo.class);
  104. list.add(aflPaccountRefundCountVo);
  105. }
  106. if (list == null || list.size() < 1) {
  107. throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("当前n+" + day + "天记录无数据");
  108. }
  109. log.info("本次处理数据:{}", JSON.toJSON(list));
  110. AflPaccountRefundCount bydAflPaccountRefundCount = null;
  111. for (AflPaccountRefundCountFindVo aflPaccountRefundCountVo : list) {
  112. AflPaccountRefundCount aflPaccountRefundCount = new AflPaccountRefundCount();
  113. Long refundMoney = fssPaccountRefundRepo.findByCountRefundMoney(aflPaccountRefundCountVo.getAgentId(),
  114. aflPaccountRefundCountVo.getAccountDate());
  115. BeanUtils.copyProperties(aflPaccountRefundCountVo, aflPaccountRefundCount);
  116. aflPaccountRefundCount.setRefundMoney(refundMoney);
  117. if (!StringUtils.isEmpty(aflPaccountRefundCountVo.getAccountDate())) {
  118. String accountDate = aflPaccountRefundCountVo.getAccountDate().replaceAll("-", "");
  119. Integer newAccountDate = Integer.parseInt(accountDate);
  120. aflPaccountRefundCount.setAccountDate(newAccountDate);
  121. }
  122. if ("52010188999".equals(aflPaccountRefundCount.getAgentId()) && aflPaccountRefundCount.getAccountDate() != null) {
  123. bydAflPaccountRefundCount = aflPaccountRefundCount;
  124. AflPaccountRefundCount bydNewAflPaccountRefundCount = aflPaccountRefundCountRepo.finByDateAndAgentId(
  125. "52010188930", aflPaccountRefundCount.getAccountDate());
  126. if (bydNewAflPaccountRefundCount != null) {
  127. if (bydAflPaccountRefundCount.getMoneyTotal() != null) {
  128. bydNewAflPaccountRefundCount.setMoneyTotal(bydNewAflPaccountRefundCount.getMoneyTotal() + bydAflPaccountRefundCount.getMoneyTotal());
  129. }
  130. if (bydAflPaccountRefundCount.getRefundMoney() != null) {
  131. bydNewAflPaccountRefundCount.setRefundMoney(bydNewAflPaccountRefundCount.getRefundMoney() + bydAflPaccountRefundCount.getRefundMoney());
  132. }
  133. if (bydAflPaccountRefundCount.getCollectMoney() != null) {
  134. bydNewAflPaccountRefundCount.setCollectMoney(bydNewAflPaccountRefundCount.getCollectMoney() + bydAflPaccountRefundCount.getCollectMoney());
  135. }
  136. if (bydAflPaccountRefundCount.getNumberTotal() != null) {
  137. bydNewAflPaccountRefundCount.setNumberTotal(bydNewAflPaccountRefundCount.getNumberTotal() + bydAflPaccountRefundCount.getNumberTotal());
  138. }
  139. aflPaccountRefundCountRepo.save(bydNewAflPaccountRefundCount);
  140. }
  141. continue;
  142. }
  143. if ("52010188930".equals(aflPaccountRefundCount.getAgentId()) && bydAflPaccountRefundCount != null) {
  144. if (bydAflPaccountRefundCount.getMoneyTotal() != null) {
  145. aflPaccountRefundCount.setMoneyTotal(aflPaccountRefundCount.getMoneyTotal() + bydAflPaccountRefundCount.getMoneyTotal());
  146. }
  147. if (bydAflPaccountRefundCount.getRefundMoney() != null) {
  148. aflPaccountRefundCount.setRefundMoney(aflPaccountRefundCount.getRefundMoney() + bydAflPaccountRefundCount.getRefundMoney());
  149. }
  150. if (bydAflPaccountRefundCount.getCollectMoney() != null) {
  151. aflPaccountRefundCount.setCollectMoney(aflPaccountRefundCount.getCollectMoney() + bydAflPaccountRefundCount.getCollectMoney());
  152. }
  153. if (bydAflPaccountRefundCount.getNumberTotal() != null) {
  154. aflPaccountRefundCount.setCollectMoney(aflPaccountRefundCount.getCollectMoney() + bydAflPaccountRefundCount.getCollectMoney());
  155. }
  156. }
  157. aflPaccountRefundCount.setId(UUID.randomUUID().toString().replace("-", ""));
  158. aflPaccountRefundCount.setUpdateTime(LocalDateTime.now());
  159. aflPaccountRefundCount.setInsertTime(LocalDateTime.now());
  160. aflPaccountRefundCountRepo.save(aflPaccountRefundCount);
  161. }
  162. }
  163. /**
  164. * 更新统计
  165. * @param day
  166. * @throws ServiceHandleException
  167. */
  168. @Override
  169. public void newCount(int day) throws ServiceHandleException {
  170. // 获取当前日期前一天的日期
  171. //1代表提前多少天
  172. LocalDate localDate = LocalDate.now().minusDays(day);
  173. String yesterday = localDate.toString();
  174. log.info("当前导入记录日期="+yesterday);
  175. List<Map<String, Object>> countTotal = paccountPayRepo.findCountTotal(yesterday+" 00:00:00",yesterday+" 23:59:59");
  176. List<AflPaccountRefundCountFindVo> list = new ArrayList<AflPaccountRefundCountFindVo>();
  177. for (Map<String, Object> stringObjectMap : countTotal) {
  178. AflPaccountRefundCountFindVo aflPaccountRefundCountVo = JSON.parseObject(JSON.toJSONString(stringObjectMap), AflPaccountRefundCountFindVo.class);
  179. list.add(aflPaccountRefundCountVo);
  180. }
  181. if (list==null||list.size()<1){
  182. log.error("当前n+"+day+"天记录无数据");
  183. return;
  184. // throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("当前n+"+day+"天记录无数据");
  185. }
  186. log.info("本次处理数据:{}", JSON.toJSON(list));
  187. AflPaccountRefundCount bydAflPaccountRefundCount = null;
  188. for (AflPaccountRefundCountFindVo aflPaccountRefundCountVo : list) {
  189. Integer newAccountDate1 =null;
  190. if (!StringUtils.isEmpty(aflPaccountRefundCountVo.getAccountDate())){
  191. String accountDate=aflPaccountRefundCountVo.getAccountDate().replaceAll("-","");
  192. newAccountDate1 = Integer.parseInt(accountDate);
  193. }
  194. AflPaccountRefundCount aflPaccountRefundCount= null;
  195. if("52010188999".equals(aflPaccountRefundCountVo.getAgentId())&&newAccountDate1!=null){
  196. aflPaccountRefundCount= aflPaccountRefundCountRepo.finByDateAndAgentId("52010188930",newAccountDate1);
  197. }else {
  198. aflPaccountRefundCount= aflPaccountRefundCountRepo.finByDateAndAgentId(aflPaccountRefundCountVo.getAgentId(),newAccountDate1);
  199. }
  200. if (aflPaccountRefundCount==null){
  201. aflPaccountRefundCount = new AflPaccountRefundCount();
  202. }
  203. Long refundMoney = fssPaccountRefundRepo.findByCountRefundMoney(aflPaccountRefundCountVo.getAgentId(),aflPaccountRefundCountVo.getAccountDate());
  204. aflPaccountRefundCountVo.setId(aflPaccountRefundCount.getId());
  205. BeanUtils.copyProperties(aflPaccountRefundCountVo,aflPaccountRefundCount);
  206. aflPaccountRefundCount.setRefundMoney(refundMoney);
  207. if (!StringUtils.isEmpty(aflPaccountRefundCountVo.getAccountDate())){
  208. String accountDate=aflPaccountRefundCountVo.getAccountDate().replaceAll("-","");
  209. Integer newAccountDate = Integer.parseInt(accountDate);
  210. aflPaccountRefundCount.setAccountDate(newAccountDate);
  211. }
  212. if ("52010188999".equals(aflPaccountRefundCount.getAgentId())&&aflPaccountRefundCount.getAccountDate()!=null){
  213. bydAflPaccountRefundCount=aflPaccountRefundCount;
  214. AflPaccountRefundCount bydNewAflPaccountRefundCount= aflPaccountRefundCountRepo.finByDateAndAgentId("52010188930",aflPaccountRefundCount.getAccountDate());
  215. if (bydNewAflPaccountRefundCount!=null){
  216. if (bydAflPaccountRefundCount.getMoneyTotal()!=null){
  217. bydNewAflPaccountRefundCount.setMoneyTotal(bydNewAflPaccountRefundCount.getMoneyTotal()+bydAflPaccountRefundCount.getMoneyTotal());
  218. }
  219. if (bydAflPaccountRefundCount.getRefundMoney()!=null){
  220. bydNewAflPaccountRefundCount.setRefundMoney(bydNewAflPaccountRefundCount.getRefundMoney()+bydAflPaccountRefundCount.getRefundMoney());
  221. }
  222. if (bydAflPaccountRefundCount.getCollectMoney()!=null){
  223. bydNewAflPaccountRefundCount.setCollectMoney(bydNewAflPaccountRefundCount.getCollectMoney()+bydAflPaccountRefundCount.getCollectMoney());
  224. }
  225. if (bydAflPaccountRefundCount.getNumberTotal()!=null){
  226. bydNewAflPaccountRefundCount.setNumberTotal(bydNewAflPaccountRefundCount.getNumberTotal()+bydAflPaccountRefundCount.getNumberTotal());
  227. }
  228. aflPaccountRefundCountRepo.save(bydNewAflPaccountRefundCount);
  229. }
  230. continue;
  231. }
  232. if ("52010188930".equals(aflPaccountRefundCount.getAgentId())&&bydAflPaccountRefundCount!=null){
  233. if (bydAflPaccountRefundCount.getMoneyTotal()!=null){
  234. aflPaccountRefundCount.setMoneyTotal(aflPaccountRefundCount.getMoneyTotal()+bydAflPaccountRefundCount.getMoneyTotal());
  235. }
  236. if (bydAflPaccountRefundCount.getRefundMoney()!=null){
  237. aflPaccountRefundCount.setRefundMoney(aflPaccountRefundCount.getRefundMoney()+bydAflPaccountRefundCount.getRefundMoney());
  238. }
  239. if (bydAflPaccountRefundCount.getCollectMoney()!=null){
  240. aflPaccountRefundCount.setCollectMoney(aflPaccountRefundCount.getCollectMoney()+bydAflPaccountRefundCount.getCollectMoney());
  241. }
  242. if (bydAflPaccountRefundCount.getNumberTotal()!=null){
  243. aflPaccountRefundCount.setCollectMoney(aflPaccountRefundCount.getCollectMoney()+bydAflPaccountRefundCount.getCollectMoney());
  244. }
  245. }
  246. if (StringUtils.isEmpty(aflPaccountRefundCount.getId())){
  247. aflPaccountRefundCount.setId(UUID.randomUUID().toString().replace("-", ""));
  248. aflPaccountRefundCount.setInsertTime(LocalDateTime.now());
  249. }
  250. aflPaccountRefundCount.setUpdateTime(LocalDateTime.now());
  251. aflPaccountRefundCountRepo.save(aflPaccountRefundCount);
  252. }
  253. }
  254. private List<VehicleAgreementNumBillsVo> toVo(List<FssPaccountPay> payList) {
  255. if (CollectionUtils.isEmpty(payList)) {
  256. return Lists.newArrayList();
  257. }
  258. List<VehicleAgreementNumBillsVo> billsVos = Lists.newArrayList();
  259. VehicleAgreementNumBillsVo billsVo;
  260. for (FssPaccountPay bill : payList) {
  261. final LocalDateTime firstInsertTime = wechatBillPayApplyRepo.getFirstInsertTime(bill.getPayId());
  262. if (null == firstInsertTime) {
  263. // 过滤掉没有// 过滤掉没有轮扣过的流水
  264. continue;
  265. }
  266. billsVo = new VehicleAgreementNumBillsVo(bill.getPayId(), bill.getSceneTp(), bill.getReceiptAmt(),
  267. bill.getChargeTime());
  268. // 处理车牌号、车牌颜色
  269. if (!StringUtils.isEmpty(bill.getVehicleId()) && bill.getVehicleId().contains("_")) {
  270. final String[] vehicle = bill.getVehicleId().split("_");
  271. billsVo.setPlateNumber(vehicle[0]);
  272. billsVo.setPlateNumberColor(VehiclePlateColorEnum.find(Integer.parseInt(vehicle[1])).getColor());
  273. }
  274. // 处理出入口站名、停车场名称
  275. if (!StringUtils.isEmpty(bill.getTitle()) && null != bill.getSceneTp()) {
  276. final String[] titleSplit = bill.getTitle().split("[,,]");
  277. if (titleSplit.length != 5) {
  278. continue;
  279. }
  280. if (bill.getSceneTp().equals(2)) {
  281. // 停车场流水
  282. billsVo.setParkingLotName(titleSplit[3]);
  283. } else {
  284. // 非停车场流水的, 按照通行流水处理
  285. billsVo.setInboundName(titleSplit[2]);
  286. billsVo.setOutboundName(titleSplit[3]);
  287. }
  288. }
  289. billsVos.add(billsVo);
  290. }
  291. return billsVos;
  292. }
  293. @Override
  294. public FssPaccountPayResultFindByPayIdResponse findPaccountPayResultByPayId(String payId) throws ServiceHandleException {
  295. FssPaccountPay fssPaccountPay = paccountPayRepo.findByPayId(payId);
  296. if (fssPaccountPay == null) {
  297. throw UcServiceError.NOT_FOUND_DATA.toHandleException("未查询到数据");
  298. }
  299. FssPaccountPayResultFindByPayIdResponse response = new FssPaccountPayResultFindByPayIdResponse();
  300. BeanUtil.copyProperties(fssPaccountPay, response);
  301. response.setPayStatus(fssPaccountPay.getStatus());
  302. // 请款中的数据,查看是否存在扣款失败原因
  303. if (fssPaccountPay.getStatus().equals(2)) {
  304. final LocalDateTime firstInsertTime = wechatBillPayApplyRepo.getFirstInsertTime(payId);
  305. final WechatBillPayApply payApply = null == firstInsertTime ?
  306. null : wechatBillPayApplyRepo.findByPayIdAndInsertTime(payId, firstInsertTime);
  307. if (null != payApply) {
  308. response.setPayMsg(StringUtils.isEmpty(payApply.getErrCodeDes()) ? payApply.getResErrCodeDes() :
  309. payApply.getErrCodeDes());
  310. response.setWxOrderId(payApply.getOutTradeNo());
  311. }
  312. }
  313. //扣款成功,需返回微信支付订单号
  314. if (fssPaccountPay.getStatus().equals(1) && StringUtils.isNotBlank(fssPaccountPay.getWxOrderId())) {
  315. WechatBillPaySuccess paySuccess = successRepo.findByOutTradeNo(fssPaccountPay.getWxOrderId());
  316. if (null != paySuccess) {
  317. response.setTransactionId(paySuccess.getTransactionId());
  318. } else {
  319. AflApPayOrder aflApPayOrder = apPayOrderRepo.findByOrderNo(fssPaccountPay.getWxOrderId());
  320. if (null != aflApPayOrder) {
  321. response.setTransactionId(aflApPayOrder.getTransactionId());
  322. }
  323. }
  324. }
  325. return response;
  326. }
  327. }