123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- 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<Payment> list = new LinkedList<>();
- Contract contract = contractDao.get(clauseId);
- List<Clause> clauseList = contract.getClauseList();
- // 1:当天, 2:提前1天, 3:提前3天, 4:提前5天
- Map<String, Integer> 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<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;
- List<Payment> list = paymentDao.getLimit(payment, currIndex, pageSize);
- if (list.size() > 0) {
- List<Long> ids = new ArrayList<>();
- list.forEach(ls -> ids.add(ls.getId()));
- List<PaymentInvoice> paymentInvoiceList = paymentInvoiceDao.getPaymentIds(ids);
- Map<Long, List<PaymentInvoice>> groupedByPaymentInvoice = paymentInvoiceList.stream().collect(Collectors.groupingBy(PaymentInvoice::getPaymentId));
- List<PaymentRecord> paymentRecordList = paymentRecordDao.getPaymentIds(ids);
- Map<Long, List<PaymentRecord>> 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<PaymentInvoice> getPaymentInvoiceLimit(PaymentInvoice paymentInvoice, int currPage, int pageSize) {
- int currIndex = (currPage - 1) * pageSize;
- List<PaymentInvoice> list = paymentInvoiceDao.getLimit(paymentInvoice, currIndex, pageSize);
- if (list.size() > 0) {
- Set<Long> paymentIds = new HashSet<>();
- list.forEach(ls -> paymentIds.add(ls.getPaymentId()));
- List<Payment> paymentList = paymentDao.getIds(new ArrayList<>(paymentIds));
- Map<Long, Payment> 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<PaymentInvoice> 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<PaymentRecord> 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<PaymentOrdinary> getLimit(PaymentOrdinary paymentOrdinary, int currPage, int pageSize) {
- int currIndex = (currPage - 1) * pageSize;
- List<PaymentOrdinary> list = paymentOrdinaryDao.getLimit(paymentOrdinary, currIndex, pageSize);
- if (list.size() > 0) {
- List<Long> ids = new ArrayList<>();
- list.forEach(ls -> ids.add(ls.getId()));
- List<PaymentInvoice> paymentInvoiceList = paymentInvoiceDao.getPaymentOrdinaryIds(ids);
- Map<Long, List<PaymentInvoice>> groupedByPaymentInvoice = paymentInvoiceList.stream().collect(Collectors.groupingBy(PaymentInvoice::getPaymentOrdinaryId));
- List<PaymentRecord> paymentRecordList = paymentRecordDao.getPaymentOrdinaryIds(ids);
- Map<Long, List<PaymentRecord>> 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);
- }
- }
|