PaymentService.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. package com.bosshand.virgo.api.service;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.bosshand.virgo.api.dao.*;
  5. import com.bosshand.virgo.api.model.*;
  6. import com.bosshand.virgo.core.utils.StringUtil;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Service;
  9. import java.math.BigDecimal;
  10. import java.time.LocalDate;
  11. import java.time.temporal.ChronoUnit;
  12. import java.util.*;
  13. import java.util.stream.Collectors;
  14. @Service
  15. public class PaymentService {
  16. @Autowired
  17. private PaymentDao paymentDao;
  18. @Autowired
  19. private ContractDao contractDao;
  20. @Autowired
  21. private PaymentInvoiceDao paymentInvoiceDao;
  22. @Autowired
  23. private PaymentRecordDao paymentRecordDao;
  24. @Autowired
  25. private PaymentOrdinaryDao paymentOrdinaryDao;
  26. public void generate(long clauseId) {
  27. LinkedList<Payment> list = new LinkedList<>();
  28. Contract contract = contractDao.get(clauseId);
  29. List<Clause> clauseList = contract.getClauseList();
  30. // 1:当天, 2:提前1天, 3:提前3天, 4:提前5天
  31. Map<String, Integer> map = new HashMap<>();
  32. map.put("1", 0);map.put("2", 1);map.put("3", 3);map.put("4", 5);
  33. // 提醒值
  34. long reminder = 0;
  35. // 开始时间
  36. String start = null;
  37. // 结束时间
  38. String end = null;
  39. // 几月一付
  40. long payCycle = 0;
  41. // 保证金
  42. BigDecimal earnestMoney = null;
  43. // 单价租金
  44. BigDecimal unitPrice = null;
  45. JSONArray earnestMoneyArray = new JSONArray();
  46. // 计数
  47. int a = 0;
  48. for (Clause clause : clauseList) {
  49. // 租赁条款
  50. if (clause.getType() == 1) {
  51. start = clause.getStartTime();
  52. end = clause.getEndTime();
  53. reminder = map.get(clause.getPayTime());
  54. payCycle = Long.parseLong(clause.getPayCycle());
  55. unitPrice = new BigDecimal(clause.getUnitPrice());
  56. } else {
  57. // 保证金条款
  58. JSONObject json = new JSONObject();
  59. if (a > 0) {
  60. json.put("earnestMoneyType", clause.getEarnestMoneyType());
  61. json.put("earnestMoney", clause.getEarnestMoney());
  62. earnestMoneyArray.add(json);
  63. BigDecimal bigDecimal = new BigDecimal(StringUtil.blank(clause.getEarnestMoney()) ? "0" : clause.getEarnestMoney());
  64. earnestMoney = earnestMoney.add(bigDecimal);
  65. } else {
  66. json.put("earnestMoneyType", clause.getEarnestMoneyType());
  67. json.put("earnestMoney", clause.getEarnestMoney());
  68. earnestMoneyArray.add(json);
  69. earnestMoney = new BigDecimal(StringUtil.blank(clause.getEarnestMoney()) ? "0" : clause.getEarnestMoney());
  70. a = a + 1;
  71. }
  72. }
  73. }
  74. LocalDate startDate = LocalDate.parse(start);
  75. LocalDate endDate = LocalDate.parse(end);
  76. // 计算期数
  77. long monthsDifference = ChronoUnit.MONTHS.between(startDate, endDate);
  78. int in = (int) Math.ceil(monthsDifference / payCycle);
  79. int b = 1;
  80. for (int i = 0; i < in; i++) {
  81. Payment payment = new Payment();
  82. payment.setProjectId(contract.getProjectId());
  83. payment.setContractCode(contract.getCode());
  84. payment.setContractId(clauseId);
  85. payment.setPayMerchantId(contract.getMerchantId());
  86. payment.setOrganizationId(contract.getOrganizationId());
  87. payment.setPayClientId(contract.getClientId());
  88. JSONArray unitPriceArray = new JSONArray();
  89. JSONObject json = new JSONObject();
  90. if (i == 0) {
  91. payment.setReminderDate(startDate.minusDays(reminder).toString());
  92. payment.setStartDate(start);
  93. // 计算指定日期后几个月的日期
  94. payment.setEndDate(startDate.plusMonths(payCycle).toString());
  95. BigDecimal cycle = new BigDecimal(payCycle);
  96. payment.setAmount(unitPrice.multiply(cycle).add(earnestMoney));
  97. json.put("payCycle",payCycle);
  98. json.put("unitPrice", unitPrice);
  99. earnestMoneyArray.add(json);
  100. payment.setData(earnestMoneyArray.toJSONString());
  101. payment.setPhase(b + i);
  102. list.add(payment);
  103. continue;
  104. }
  105. int c = i - 1;
  106. payment.setReminderDate(LocalDate.parse(list.get(c).getEndDate()).minusDays(reminder).toString());
  107. payment.setStartDate(list.get(c).getEndDate());
  108. payment.setEndDate(LocalDate.parse(list.get(c).getEndDate()).plusMonths(payCycle).toString());
  109. BigDecimal cycle = new BigDecimal(payCycle);
  110. payment.setAmount(unitPrice.multiply(cycle));
  111. json.put("payCycle",payCycle);
  112. json.put("unitPrice", unitPrice);
  113. unitPriceArray.add(json);
  114. payment.setData(unitPriceArray.toJSONString());
  115. payment.setPhase(b + i);
  116. list.add(payment);
  117. }
  118. paymentDao.batchInsert(list);
  119. }
  120. public int updateStatus(long id, Integer status) {
  121. return paymentDao.updateStatus(id, status);
  122. }
  123. public List<Payment> getList(Payment payment) {
  124. return paymentDao.getList(payment);
  125. }
  126. public List<Payment> getContractId(long contractId) {
  127. return paymentDao.getContractId(contractId);
  128. }
  129. public int getTotalCount(Payment payment) {
  130. return paymentDao.getTotalCount(payment);
  131. }
  132. public List<Payment> getLimit(Payment payment, int currPage, int pageSize) {
  133. int currIndex = (currPage - 1) * pageSize;
  134. List<Payment> list = paymentDao.getLimit(payment, currIndex, pageSize);
  135. if (list.size() > 0) {
  136. List<Long> ids = new ArrayList<>();
  137. list.forEach(ls -> ids.add(ls.getId()));
  138. List<PaymentInvoice> paymentInvoiceList = paymentInvoiceDao.getPaymentIds(ids);
  139. Map<Long, List<PaymentInvoice>> groupedByPaymentInvoice = paymentInvoiceList.stream().collect(Collectors.groupingBy(PaymentInvoice::getPaymentId));
  140. List<PaymentRecord> paymentRecordList = paymentRecordDao.getPaymentIds(ids);
  141. Map<Long, List<PaymentRecord>> groupedByPaymentRecord = paymentRecordList.stream().collect(Collectors.groupingBy(PaymentRecord::getPaymentId));
  142. for (Payment po : list) {
  143. po.setPaymentInvoiceList(groupedByPaymentInvoice.get(po.getId()));
  144. po.setPaymentRecordList(groupedByPaymentRecord.get(po.getId()));
  145. }
  146. }
  147. return list;
  148. }
  149. public Payment get(long id) {
  150. Payment payment = paymentDao.get(id);
  151. payment.setPaymentInvoiceList(paymentInvoiceDao.getPaymentId(id));
  152. payment.setPaymentRecordList(paymentRecordDao.getPaymentId(id));
  153. return payment;
  154. }
  155. /**
  156. * 发票记录
  157. */
  158. public PaymentInvoice getInvoice(long id) {
  159. return paymentInvoiceDao.get(id);
  160. }
  161. public void insertInvoice(PaymentInvoice paymentInvoice) {
  162. if (paymentInvoice.getPaymentId() != 0) {
  163. Payment payment = paymentDao.get(paymentInvoice.getPaymentId());
  164. Contract contract = contractDao.get(payment.getContractId());
  165. paymentInvoice.setProjectId(payment.getProjectId());
  166. paymentInvoice.setProjectItemTargetRoomIds(contract.getProjectItemTargetRoomIds());
  167. paymentInvoice.setPayClientId(payment.getPayClientId());
  168. paymentInvoice.setPayMerchantId(payment.getPayMerchantId());
  169. }
  170. if (paymentInvoice.getPaymentOrdinaryId() != 0) {
  171. PaymentOrdinary paymentOrdinary = paymentOrdinaryDao.get(paymentInvoice.getPaymentOrdinaryId());
  172. paymentInvoice.setProjectId(paymentOrdinary.getProjectId());
  173. paymentInvoice.setPayClientId(paymentOrdinary.getPayClientId());
  174. paymentInvoice.setPayMerchantId(paymentOrdinary.getPayMerchantId());
  175. }
  176. paymentInvoiceDao.insert(paymentInvoice);
  177. }
  178. public int getPaymentInvoiceTotalCount(PaymentInvoice paymentInvoice) {
  179. return paymentInvoiceDao.getTotalCount(paymentInvoice);
  180. }
  181. public List<PaymentInvoice> getPaymentInvoiceLimit(PaymentInvoice paymentInvoice, int currPage, int pageSize) {
  182. int currIndex = (currPage - 1) * pageSize;
  183. List<PaymentInvoice> list = paymentInvoiceDao.getLimit(paymentInvoice, currIndex, pageSize);
  184. if (list.size() > 0) {
  185. Set<Long> paymentIds = new HashSet<>();
  186. list.forEach(ls -> paymentIds.add(ls.getPaymentId()));
  187. List<Payment> paymentList = paymentDao.getIds(new ArrayList<>(paymentIds));
  188. Map<Long, Payment> groupedByPayment = new HashMap<>();
  189. for (Payment pm : paymentList) {
  190. groupedByPayment.put(pm.getId(), pm);
  191. }
  192. for (PaymentInvoice p : list) {
  193. p.setPayment(groupedByPayment.get(p.getPaymentId()));
  194. }
  195. }
  196. return list;
  197. }
  198. public List<PaymentInvoice> getInvoiceList(PaymentInvoice paymentInvoice) {
  199. return paymentInvoiceDao.getList(paymentInvoice);
  200. }
  201. public int updateInvoice(PaymentInvoice paymentInvoice) {
  202. return paymentInvoiceDao.update(paymentInvoice);
  203. }
  204. public int deleteInvoice(long id) {
  205. return paymentInvoiceDao.delete(id);
  206. }
  207. /**
  208. * 付款记录
  209. */
  210. public PaymentRecord getPaymentRecord(long id) {
  211. return paymentRecordDao.get(id);
  212. }
  213. public int insertPaymentRecord(PaymentRecord paymentRecord) {
  214. return paymentRecordDao.insert(paymentRecord);
  215. }
  216. public List<PaymentRecord> getPaymentRecordList(PaymentRecord paymentRecord) {
  217. return paymentRecordDao.getList(paymentRecord);
  218. }
  219. public int updatePaymentRecord(PaymentRecord paymentRecord) {
  220. return paymentRecordDao.update(paymentRecord);
  221. }
  222. public int deletePaymentRecord(long id) {
  223. return paymentRecordDao.delete(id);
  224. }
  225. /**
  226. * 普通账单
  227. */
  228. public int getTotalCount(PaymentOrdinary paymentOrdinary) {
  229. return paymentOrdinaryDao.getTotalCount(paymentOrdinary);
  230. }
  231. public List<PaymentOrdinary> getLimit(PaymentOrdinary paymentOrdinary, int currPage, int pageSize) {
  232. int currIndex = (currPage - 1) * pageSize;
  233. List<PaymentOrdinary> list = paymentOrdinaryDao.getLimit(paymentOrdinary, currIndex, pageSize);
  234. if (list.size() > 0) {
  235. List<Long> ids = new ArrayList<>();
  236. list.forEach(ls -> ids.add(ls.getId()));
  237. List<PaymentInvoice> paymentInvoiceList = paymentInvoiceDao.getPaymentOrdinaryIds(ids);
  238. Map<Long, List<PaymentInvoice>> groupedByPaymentInvoice = paymentInvoiceList.stream().collect(Collectors.groupingBy(PaymentInvoice::getPaymentOrdinaryId));
  239. List<PaymentRecord> paymentRecordList = paymentRecordDao.getPaymentOrdinaryIds(ids);
  240. Map<Long, List<PaymentRecord>> groupedByPaymentRecord = paymentRecordList.stream().collect(Collectors.groupingBy(PaymentRecord::getPaymentOrdinaryId));
  241. for (PaymentOrdinary po : list) {
  242. po.setPaymentInvoiceList(groupedByPaymentInvoice.get(po.getId()));
  243. po.setPaymentRecordList(groupedByPaymentRecord.get(po.getId()));
  244. }
  245. }
  246. return list;
  247. }
  248. public PaymentOrdinary getPaymentOrdinary(long id) {
  249. PaymentOrdinary paymentOrdinary = paymentOrdinaryDao.get(id);
  250. paymentOrdinary.setPaymentInvoiceList(paymentInvoiceDao.getPaymentOrdinaryId(id));
  251. paymentOrdinary.setPaymentRecordList(paymentRecordDao.getPaymentOrdinaryId(id));
  252. return paymentOrdinary;
  253. }
  254. public int deletePaymentOrdinary(long id) {
  255. return paymentOrdinaryDao.delete(id);
  256. }
  257. public int insertPaymentOrdinary(PaymentOrdinary paymentOrdinary) {
  258. return paymentOrdinaryDao.insert(paymentOrdinary);
  259. }
  260. public int updatePaymentOrdinary(PaymentOrdinary paymentOrdinary) {
  261. return paymentOrdinaryDao.update(paymentOrdinary);
  262. }
  263. }