package com.bosshand.virgo.api.service; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.bosshand.virgo.api.dao.*; import com.bosshand.virgo.api.model.*; import com.bosshand.virgo.core.utils.StringUtil; 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.*; import java.util.stream.Collectors; @Service public class PaymentService { @Autowired private PaymentDao paymentDao; @Autowired private ContractDao contractDao; @Autowired private PaymentInvoiceDao paymentInvoiceDao; @Autowired private PaymentRecordDao paymentRecordDao; @Autowired private PaymentOrdinaryDao paymentOrdinaryDao; public void generate(long clauseId) { LinkedList list = new LinkedList<>(); Contract contract = contractDao.get(clauseId); List clauseList = contract.getClauseList(); // 1:当天, 2:提前1天, 3:提前3天, 4:提前5天 Map map = new HashMap<>(); map.put("1", 0);map.put("2", 1);map.put("3", 3);map.put("4", 5); // 提醒值 long reminder = 0; // 开始时间 String start = null; // 结束时间 String end = null; // 几月一付 long payCycle = 0; // 保证金 BigDecimal earnestMoney = null; // 单价租金 BigDecimal unitPrice = null; JSONArray earnestMoneyArray = new JSONArray(); // 计数 int a = 0; for (Clause clause : clauseList) { // 租赁条款 if (clause.getType() == 1) { start = clause.getStartTime(); end = clause.getEndTime(); reminder = map.get(clause.getPayTime()); 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()); earnestMoneyArray.add(json); BigDecimal bigDecimal = new BigDecimal(StringUtil.blank(clause.getEarnestMoney()) ? "0" : clause.getEarnestMoney()); earnestMoney = earnestMoney.add(bigDecimal); } else { json.put("earnestMoneyType", clause.getEarnestMoneyType()); json.put("earnestMoney", clause.getEarnestMoney()); earnestMoneyArray.add(json); earnestMoney = new BigDecimal(StringUtil.blank(clause.getEarnestMoney()) ? "0" : clause.getEarnestMoney()); a = a + 1; } } } LocalDate startDate = LocalDate.parse(start); LocalDate endDate = LocalDate.parse(end); // 计算期数 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(); payment.setProjectId(contract.getProjectId()); payment.setContractCode(contract.getCode()); payment.setContractId(clauseId); payment.setPayMerchantId(contract.getMerchantId()); payment.setOrganizationId(contract.getOrganizationId()); payment.setPayClientId(contract.getClientId()); JSONArray unitPriceArray = new JSONArray(); JSONObject json = new JSONObject(); if (i == 0) { payment.setReminderDate(startDate.minusDays(reminder).toString()); payment.setStartDate(start); // 计算指定日期后几个月的日期 payment.setEndDate(startDate.plusMonths(payCycle).toString()); BigDecimal cycle = new BigDecimal(payCycle); payment.setAmount(unitPrice.multiply(cycle).add(earnestMoney)); json.put("payCycle",payCycle); json.put("unitPrice", unitPrice); earnestMoneyArray.add(json); payment.setData(earnestMoneyArray.toJSONString()); payment.setPhase(b + i); list.add(payment); continue; } int c = i - 1; payment.setReminderDate(LocalDate.parse(list.get(c).getEndDate()).minusDays(reminder).toString()); payment.setStartDate(list.get(c).getEndDate()); payment.setEndDate(LocalDate.parse(list.get(c).getEndDate()).plusMonths(payCycle).toString()); BigDecimal cycle = new BigDecimal(payCycle); payment.setAmount(unitPrice.multiply(cycle)); json.put("payCycle",payCycle); json.put("unitPrice", unitPrice); unitPriceArray.add(json); payment.setData(unitPriceArray.toJSONString()); payment.setPhase(b + i); list.add(payment); } paymentDao.batchInsert(list); } public int updateStatus(long id, Integer status) { return paymentDao.updateStatus(id, status); } public List getList(Payment payment) { return paymentDao.getList(payment); } public List getContractId(long contractId) { return paymentDao.getContractId(contractId); } public int getTotalCount(Payment payment) { return paymentDao.getTotalCount(payment); } public List getLimit(Payment payment, int currPage, int pageSize) { int currIndex = (currPage - 1) * pageSize; List list = paymentDao.getLimit(payment, currIndex, pageSize); if (list.size() > 0) { List ids = new ArrayList<>(); list.forEach(ls -> ids.add(ls.getId())); List paymentInvoiceList = paymentInvoiceDao.getPaymentIds(ids); Map> groupedByPaymentInvoice = paymentInvoiceList.stream().collect(Collectors.groupingBy(PaymentInvoice::getPaymentId)); List paymentRecordList = paymentRecordDao.getPaymentIds(ids); Map> groupedByPaymentRecord = paymentRecordList.stream().collect(Collectors.groupingBy(PaymentRecord::getPaymentId)); for (Payment po : list) { po.setPaymentInvoiceList(groupedByPaymentInvoice.get(po.getId())); po.setPaymentRecordList(groupedByPaymentRecord.get(po.getId())); } } return list; } public Payment get(long id) { Payment payment = paymentDao.get(id); payment.setPaymentInvoiceList(paymentInvoiceDao.getPaymentId(id)); payment.setPaymentRecordList(paymentRecordDao.getPaymentId(id)); return payment; } /** * 发票记录 */ public PaymentInvoice getInvoice(long id) { return paymentInvoiceDao.get(id); } public void insertInvoice(PaymentInvoice paymentInvoice) { if (paymentInvoice.getPaymentId() != 0) { Payment payment = paymentDao.get(paymentInvoice.getPaymentId()); Contract contract = contractDao.get(payment.getContractId()); paymentInvoice.setProjectId(payment.getProjectId()); paymentInvoice.setProjectItemTargetRoomIds(contract.getProjectItemTargetRoomIds()); paymentInvoice.setPayClientId(payment.getPayClientId()); paymentInvoice.setPayMerchantId(payment.getPayMerchantId()); } if (paymentInvoice.getPaymentOrdinaryId() != 0) { PaymentOrdinary paymentOrdinary = paymentOrdinaryDao.get(paymentInvoice.getPaymentOrdinaryId()); paymentInvoice.setProjectId(paymentOrdinary.getProjectId()); paymentInvoice.setPayClientId(paymentOrdinary.getPayClientId()); paymentInvoice.setPayMerchantId(paymentOrdinary.getPayMerchantId()); } paymentInvoiceDao.insert(paymentInvoice); } public int getPaymentInvoiceTotalCount(PaymentInvoice paymentInvoice) { return paymentInvoiceDao.getTotalCount(paymentInvoice); } public List getPaymentInvoiceLimit(PaymentInvoice paymentInvoice, int currPage, int pageSize) { int currIndex = (currPage - 1) * pageSize; List list = paymentInvoiceDao.getLimit(paymentInvoice, currIndex, pageSize); if (list.size() > 0) { Set paymentIds = new HashSet<>(); list.forEach(ls -> paymentIds.add(ls.getPaymentId())); List paymentList = paymentDao.getIds(new ArrayList<>(paymentIds)); Map groupedByPayment = new HashMap<>(); for (Payment pm : paymentList) { groupedByPayment.put(pm.getId(), pm); } for (PaymentInvoice p : list) { p.setPayment(groupedByPayment.get(p.getPaymentId())); } } return list; } public List getInvoiceList(PaymentInvoice paymentInvoice) { return paymentInvoiceDao.getList(paymentInvoice); } public int updateInvoice(PaymentInvoice paymentInvoice) { return paymentInvoiceDao.update(paymentInvoice); } public int deleteInvoice(long id) { return paymentInvoiceDao.delete(id); } /** * 付款记录 */ public PaymentRecord getPaymentRecord(long id) { return paymentRecordDao.get(id); } public int insertPaymentRecord(PaymentRecord paymentRecord) { return paymentRecordDao.insert(paymentRecord); } public List getPaymentRecordList(PaymentRecord paymentRecord) { return paymentRecordDao.getList(paymentRecord); } public int updatePaymentRecord(PaymentRecord paymentRecord) { return paymentRecordDao.update(paymentRecord); } public int deletePaymentRecord(long id) { return paymentRecordDao.delete(id); } /** * 普通账单 */ public int getTotalCount(PaymentOrdinary paymentOrdinary) { return paymentOrdinaryDao.getTotalCount(paymentOrdinary); } public List getLimit(PaymentOrdinary paymentOrdinary, int currPage, int pageSize) { int currIndex = (currPage - 1) * pageSize; List list = paymentOrdinaryDao.getLimit(paymentOrdinary, currIndex, pageSize); if (list.size() > 0) { List ids = new ArrayList<>(); list.forEach(ls -> ids.add(ls.getId())); List paymentInvoiceList = paymentInvoiceDao.getPaymentOrdinaryIds(ids); Map> groupedByPaymentInvoice = paymentInvoiceList.stream().collect(Collectors.groupingBy(PaymentInvoice::getPaymentOrdinaryId)); List paymentRecordList = paymentRecordDao.getPaymentOrdinaryIds(ids); Map> groupedByPaymentRecord = paymentRecordList.stream().collect(Collectors.groupingBy(PaymentRecord::getPaymentOrdinaryId)); for (PaymentOrdinary po : list) { po.setPaymentInvoiceList(groupedByPaymentInvoice.get(po.getId())); po.setPaymentRecordList(groupedByPaymentRecord.get(po.getId())); } } return list; } public PaymentOrdinary getPaymentOrdinary(long id) { PaymentOrdinary paymentOrdinary = paymentOrdinaryDao.get(id); paymentOrdinary.setPaymentInvoiceList(paymentInvoiceDao.getPaymentOrdinaryId(id)); paymentOrdinary.setPaymentRecordList(paymentRecordDao.getPaymentOrdinaryId(id)); return paymentOrdinary; } public int deletePaymentOrdinary(long id) { return paymentOrdinaryDao.delete(id); } public int insertPaymentOrdinary(PaymentOrdinary paymentOrdinary) { return paymentOrdinaryDao.insert(paymentOrdinary); } public int updatePaymentOrdinary(PaymentOrdinary paymentOrdinary) { return paymentOrdinaryDao.update(paymentOrdinary); } }