|
@@ -1,10 +1,19 @@
|
|
|
package com.bosshand.virgo.api.service;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.bosshand.virgo.api.dao.ContractDao;
|
|
|
import com.bosshand.virgo.api.dao.PaymentDao;
|
|
|
+import com.bosshand.virgo.api.model.Clause;
|
|
|
+import com.bosshand.virgo.api.model.Contract;
|
|
|
import com.bosshand.virgo.api.model.Payment;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.time.temporal.ChronoUnit;
|
|
|
+import java.util.LinkedList;
|
|
|
import java.util.List;
|
|
|
|
|
|
@Service
|
|
@@ -13,20 +22,123 @@ public class PaymentService {
|
|
|
@Autowired
|
|
|
private PaymentDao paymentDao;
|
|
|
|
|
|
- public int insert (Payment payment){
|
|
|
- return paymentDao.insert(payment);
|
|
|
- }
|
|
|
+ @Autowired
|
|
|
+ private ContractDao contractDao;
|
|
|
+
|
|
|
+ public void generate(long clauseId) {
|
|
|
+
|
|
|
+ LinkedList<Payment> list = new LinkedList<>();
|
|
|
+
|
|
|
+ Contract contract = contractDao.get(clauseId);
|
|
|
+ List<Clause> clauseList = contract.getClauseList();
|
|
|
+
|
|
|
+ // 开始时间
|
|
|
+ String start = null;
|
|
|
+ // 结束时间
|
|
|
+ String end = null;
|
|
|
+ // 几月一付
|
|
|
+ long payCycle = 0;
|
|
|
+ // 保证金
|
|
|
+ BigDecimal earnestMoney = null;
|
|
|
+ // 单价租金
|
|
|
+ BigDecimal unitPrice = null;
|
|
|
+
|
|
|
+ JSONArray array = new JSONArray();
|
|
|
+ // 计数
|
|
|
+ int a = 0;
|
|
|
+ for (Clause clause : clauseList) {
|
|
|
+ // 租赁条款
|
|
|
+ if (clause.getType() == 1) {
|
|
|
+ start = clause.getStartTime();
|
|
|
+ end = clause.getEndTime();
|
|
|
+ payCycle = Long.parseLong(clause.getPayCycle());
|
|
|
+ unitPrice = new BigDecimal(clause.getUnitPrice());
|
|
|
+ } else {
|
|
|
+ // 保证金条款
|
|
|
+ JSONObject json = new JSONObject();
|
|
|
+ if (a > 0) {
|
|
|
+ json.put("earnestMoneyType", clause.getEarnestMoneyType());
|
|
|
+ json.put("earnestMoney", clause.getEarnestMoney());
|
|
|
+ array.add(json);
|
|
|
+ BigDecimal bigDecimal = new BigDecimal(clause.getEarnestMoney());
|
|
|
+ earnestMoney.add(bigDecimal);
|
|
|
+ } else {
|
|
|
+ json.put("earnestMoneyType", clause.getEarnestMoneyType());
|
|
|
+ json.put("earnestMoney", clause.getEarnestMoney());
|
|
|
+ array.add(json);
|
|
|
+ earnestMoney = new BigDecimal(clause.getEarnestMoney());
|
|
|
+ a = a + 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ LocalDate startDate = LocalDate.parse(start);
|
|
|
+ LocalDate endDate = LocalDate.parse(end);
|
|
|
|
|
|
- public int update(Payment payment){
|
|
|
- return paymentDao.update(payment);
|
|
|
+ // 计算期数
|
|
|
+ long monthsDifference = ChronoUnit.MONTHS.between(startDate, endDate);
|
|
|
+ int in = (int) Math.ceil(monthsDifference / payCycle);
|
|
|
+
|
|
|
+ int b = 1;
|
|
|
+ for (int i = 0; i < in; i++) {
|
|
|
+ Payment payment = new Payment();
|
|
|
+ JSONObject json = new JSONObject();
|
|
|
+ if (i == 0) {
|
|
|
+ payment.setStartDate(start);
|
|
|
+ // 计算指定日期后几个月的日期
|
|
|
+ payment.setEndDate(startDate.plusMonths(payCycle).toString());
|
|
|
+ payment.setContractId(clauseId);
|
|
|
+ payment.setPayMerchantId(contract.getMerchantId());
|
|
|
+ payment.setOrganizationId(contract.getOrganizationId());
|
|
|
+ payment.setPayClientId(contract.getClientId());
|
|
|
+ BigDecimal cycle = new BigDecimal(payCycle);
|
|
|
+ payment.setAmount(unitPrice.multiply(cycle).add(earnestMoney));
|
|
|
+ json.put("payCycle",payCycle);
|
|
|
+ json.put("unitPrice", unitPrice);
|
|
|
+ array.add(json);
|
|
|
+ payment.setData(array.toJSONString());
|
|
|
+ payment.setPhase(b + i);
|
|
|
+ list.add(payment);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ int c = i - 1;
|
|
|
+ payment.setStartDate(list.get(c).getEndDate());
|
|
|
+ payment.setEndDate(LocalDate.parse(list.get(c).getEndDate()).plusMonths(payCycle).toString());
|
|
|
+ payment.setContractId(clauseId);
|
|
|
+ payment.setPayMerchantId(contract.getMerchantId());
|
|
|
+ payment.setOrganizationId(contract.getOrganizationId());
|
|
|
+ payment.setPayClientId(contract.getClientId());
|
|
|
+ BigDecimal cycle = new BigDecimal(payCycle);
|
|
|
+ payment.setAmount(unitPrice.multiply(cycle));
|
|
|
+ json.put("payCycle",payCycle);
|
|
|
+ json.put("unitPrice", unitPrice);
|
|
|
+ payment.setData(json.toJSONString());
|
|
|
+ payment.setPhase(b + i);
|
|
|
+ list.add(payment);
|
|
|
+ }
|
|
|
+ paymentDao.batchInsert(list);
|
|
|
}
|
|
|
|
|
|
- public int delete(long id){
|
|
|
- return paymentDao.delete(id);
|
|
|
+
|
|
|
+ public int updateStatus(long id, Integer status) {
|
|
|
+ return paymentDao.updateStatus(id, status);
|
|
|
}
|
|
|
|
|
|
- public List<Payment> getList(Payment payment){
|
|
|
+ public List<Payment> getList(Payment payment) {
|
|
|
return paymentDao.getList(payment);
|
|
|
}
|
|
|
|
|
|
+ public List<Payment> getContractId(long contractId) {
|
|
|
+ return paymentDao.getContractId(contractId);
|
|
|
+ }
|
|
|
+
|
|
|
+ public int getTotalCount(Payment payment) {
|
|
|
+ return paymentDao.getTotalCount(payment);
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<Payment> getLimit(Payment payment, int currPage, int pageSize) {
|
|
|
+ int currIndex = (currPage - 1) * pageSize;
|
|
|
+ return paymentDao.getLimit(payment, currIndex, pageSize);
|
|
|
+ }
|
|
|
+
|
|
|
}
|