|
@@ -0,0 +1,218 @@
|
|
|
+package com.iotechn.unimall.admin.api.sys;
|
|
|
+
|
|
|
+
|
|
|
+import com.iotechn.unimall.core.exception.AdminServiceException;
|
|
|
+import com.iotechn.unimall.core.exception.ExceptionDefinition;
|
|
|
+import com.iotechn.unimall.core.exception.ServiceException;
|
|
|
+import com.iotechn.unimall.data.domain.ProductSalesDO;
|
|
|
+import com.iotechn.unimall.data.dto.StatisticsView;
|
|
|
+import com.iotechn.unimall.data.mapper.ProductSalesMapper;
|
|
|
+import com.iotechn.unimall.data.model.Page;
|
|
|
+import com.baomidou.mybatisplus.mapper.EntityWrapper;
|
|
|
+import com.baomidou.mybatisplus.mapper.Wrapper;
|
|
|
+import com.iotechn.unimall.data.domain.ExpenditureManageDO;
|
|
|
+import com.iotechn.unimall.data.dto.ExpenditureManageDTO;
|
|
|
+import com.iotechn.unimall.data.mapper.ExpenditureManageMapper;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.apache.ibatis.session.RowBounds;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+import tk.mybatis.mapper.util.StringUtil;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Generate Code By Unimall
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class ExpenditureServiceImpl implements ExpenditureService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ExpenditureManageMapper expenditureManageMapper;
|
|
|
+ @Autowired
|
|
|
+ private ProductSalesMapper productSalesMapper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean delete(Long id, Long adminId) throws ServiceException {
|
|
|
+ return expenditureManageMapper.deleteById(id) > 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Page<ExpenditureManageDO> list(ExpenditureManageDTO selectDTO, Integer page, Integer limit, Long adminId) throws ServiceException {
|
|
|
+ Wrapper<ExpenditureManageDO> wrapper = new EntityWrapper<ExpenditureManageDO>();
|
|
|
+ List<ExpenditureManageDO> list = expenditureManageMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
|
|
|
+ Integer count = expenditureManageMapper.selectCount(wrapper);
|
|
|
+ return new Page<ExpenditureManageDO>(list, page, limit, count);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public ExpenditureManageDO create(ExpenditureManageDTO insertDTO, Long adminId) throws ServiceException {
|
|
|
+ Date now = new Date();
|
|
|
+ ExpenditureManageDO insertDO = new ExpenditureManageDO();
|
|
|
+ BeanUtils.copyProperties(insertDTO, insertDO);
|
|
|
+ insertDO.setGmtUpdate(now);
|
|
|
+ insertDO.setGmtCreate(now);
|
|
|
+ if (expenditureManageMapper.insert(insertDO) > 0) {
|
|
|
+ return insertDO;
|
|
|
+ }
|
|
|
+ throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public String edit(ExpenditureManageDTO editDTO, Long adminId) throws ServiceException {
|
|
|
+ Date now = new Date();
|
|
|
+ ExpenditureManageDO updateDO = new ExpenditureManageDO();
|
|
|
+ BeanUtils.copyProperties(editDTO, updateDO);
|
|
|
+ updateDO.setGmtUpdate(now);
|
|
|
+ if (expenditureManageMapper.updateById(updateDO) > 0) {
|
|
|
+ return "ok";
|
|
|
+ }
|
|
|
+ throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<StatisticsView> listStatistics(StatisticsView statisticsView) throws ServiceException {
|
|
|
+ List<StatisticsView> statisticsViews = new ArrayList<>();
|
|
|
+ Wrapper<ExpenditureManageDO> wrapper = new EntityWrapper<ExpenditureManageDO>();
|
|
|
+ if (statisticsView != null && StringUtil.isNotEmpty(statisticsView.getStartDate())) {
|
|
|
+ wrapper.and("DATE_FORMAT(expenditure_date,'%Y%m%d') >= DATE_FORMAT('" + statisticsView.getStartDate() + "','%Y%m%d')");
|
|
|
+ }
|
|
|
+ if (statisticsView != null && StringUtil.isNotEmpty(statisticsView.getEndDate())) {
|
|
|
+ wrapper.and("DATE_FORMAT(expenditure_date,'%Y%m%d') <= DATE_FORMAT('" + statisticsView.getEndDate() + "','%Y%m%d')");
|
|
|
+
|
|
|
+ }
|
|
|
+ wrapper.groupBy("expenditure_date");
|
|
|
+ wrapper.orderBy("expenditure_date", true);
|
|
|
+ List<ExpenditureManageDO> list = expenditureManageMapper.selectList(wrapper);
|
|
|
+
|
|
|
+ Wrapper<ProductSalesDO> wrapper1 = new EntityWrapper<ProductSalesDO>();
|
|
|
+ if (statisticsView != null && StringUtil.isNotEmpty(statisticsView.getStartDate())) {
|
|
|
+ wrapper1.and("DATE_FORMAT(sale_date,'%Y%m%d') >= DATE_FORMAT('" + statisticsView.getStartDate() + "','%Y%m%d')");
|
|
|
+ }
|
|
|
+ if (statisticsView != null && StringUtil.isNotEmpty(statisticsView.getEndDate())) {
|
|
|
+ wrapper1.and("DATE_FORMAT(sale_date,'%Y%m%d') <= DATE_FORMAT('" + statisticsView.getEndDate() + "','%Y%m%d')");
|
|
|
+
|
|
|
+ }
|
|
|
+ wrapper.groupBy("sale_date");
|
|
|
+ wrapper.orderBy("sale_date", true);
|
|
|
+ List<ProductSalesDO> list1 = productSalesMapper.selectList(wrapper1);
|
|
|
+
|
|
|
+ if (!CollectionUtils.isEmpty(list) && !CollectionUtils.isEmpty(list1)) {
|
|
|
+ // 获取支出日期
|
|
|
+ List<String> dateList = list.stream().map(ExpenditureManageDO::getExpenditureDate).collect(Collectors.toList());
|
|
|
+ // 获取收款日期
|
|
|
+ List<String> dateSalesList = list1.stream().map(ProductSalesDO::getSaleDate).collect(Collectors.toList());
|
|
|
+ for (int i = 0; i < dateSalesList.size(); i++) {
|
|
|
+ if (!dateList.contains(dateSalesList.get(i))) {
|
|
|
+ dateList.add(dateSalesList.get(i));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Collections.sort(dateList);
|
|
|
+ for (int i = 0; i < dateList.size(); i++) {
|
|
|
+ String date = dateList.get(i);
|
|
|
+ Wrapper<ProductSalesDO> salesDOWrapper = new EntityWrapper<ProductSalesDO>();
|
|
|
+ salesDOWrapper.eq("sale_date", date);
|
|
|
+ List<ProductSalesDO> salesDOS = productSalesMapper.selectList(salesDOWrapper);
|
|
|
+ Wrapper<ExpenditureManageDO> expenditureManageDOWrapper = new EntityWrapper<ExpenditureManageDO>();
|
|
|
+ salesDOWrapper.eq("sale_date", date);
|
|
|
+ List<ExpenditureManageDO> expenditureManageDOS = expenditureManageMapper.selectList(expenditureManageDOWrapper);
|
|
|
+ double saleSum = 0d;
|
|
|
+ double expenditureSum = 0d;
|
|
|
+ if (!CollectionUtils.isEmpty(salesDOS)) {
|
|
|
+ for (int j = 0; j < salesDOS.size(); j++) {
|
|
|
+ saleSum = saleSum + salesDOS.get(j).getPaidAmount();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!CollectionUtils.isEmpty(expenditureManageDOS)) {
|
|
|
+ for (int j = 0; j < expenditureManageDOS.size(); j++) {
|
|
|
+ expenditureSum = expenditureSum + expenditureManageDOS.get(j).getExpenditureAmount();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ StatisticsView statisticsView1 = new StatisticsView();
|
|
|
+ statisticsView1.setStaticDate(date);
|
|
|
+ statisticsView1.setRevenueAmount(saleSum);
|
|
|
+ statisticsView1.setExpenditureAmount(expenditureSum);
|
|
|
+ statisticsView1.setReceipts(saleSum - expenditureSum);
|
|
|
+ statisticsViews.add(statisticsView1);
|
|
|
+
|
|
|
+ }
|
|
|
+ } else if (!CollectionUtils.isEmpty(list) && CollectionUtils.isEmpty(list1)) {
|
|
|
+// 获取支出日期
|
|
|
+ List<String> dateList = list.stream().map(ExpenditureManageDO::getExpenditureDate).collect(Collectors.toList());
|
|
|
+
|
|
|
+ for (int i = 0; i < dateList.size(); i++) {
|
|
|
+ String date = dateList.get(i);
|
|
|
+ Wrapper<ProductSalesDO> salesDOWrapper = new EntityWrapper<ProductSalesDO>();
|
|
|
+ salesDOWrapper.eq("sale_date", date);
|
|
|
+ List<ProductSalesDO> salesDOS = productSalesMapper.selectList(salesDOWrapper);
|
|
|
+ Wrapper<ExpenditureManageDO> expenditureManageDOWrapper = new EntityWrapper<ExpenditureManageDO>();
|
|
|
+ salesDOWrapper.eq("sale_date", date);
|
|
|
+ List<ExpenditureManageDO> expenditureManageDOS = expenditureManageMapper.selectList(expenditureManageDOWrapper);
|
|
|
+ double saleSum = 0d;
|
|
|
+ double expenditureSum = 0d;
|
|
|
+ if (!CollectionUtils.isEmpty(salesDOS)) {
|
|
|
+ for (int j = 0; j < salesDOS.size(); j++) {
|
|
|
+ saleSum = saleSum + salesDOS.get(j).getPaidAmount();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!CollectionUtils.isEmpty(expenditureManageDOS)) {
|
|
|
+ for (int j = 0; j < expenditureManageDOS.size(); j++) {
|
|
|
+ expenditureSum = expenditureSum + expenditureManageDOS.get(j).getExpenditureAmount();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ StatisticsView statisticsView1 = new StatisticsView();
|
|
|
+ statisticsView1.setStaticDate(date);
|
|
|
+ statisticsView1.setRevenueAmount(saleSum);
|
|
|
+ statisticsView1.setExpenditureAmount(expenditureSum);
|
|
|
+ statisticsView1.setReceipts(saleSum - expenditureSum);
|
|
|
+ statisticsViews.add(statisticsView1);
|
|
|
+
|
|
|
+ }
|
|
|
+ } else if (CollectionUtils.isEmpty(list) && !CollectionUtils.isEmpty(list1)) {
|
|
|
+ // 获取收款日期
|
|
|
+ List<String> dateList = list1.stream().map(ProductSalesDO::getSaleDate).collect(Collectors.toList());
|
|
|
+ for (int i = 0; i < dateList.size(); i++) {
|
|
|
+ String date = dateList.get(i);
|
|
|
+ Wrapper<ProductSalesDO> salesDOWrapper = new EntityWrapper<ProductSalesDO>();
|
|
|
+ salesDOWrapper.eq("sale_date", date);
|
|
|
+ List<ProductSalesDO> salesDOS = productSalesMapper.selectList(salesDOWrapper);
|
|
|
+ Wrapper<ExpenditureManageDO> expenditureManageDOWrapper = new EntityWrapper<ExpenditureManageDO>();
|
|
|
+ salesDOWrapper.eq("sale_date", date);
|
|
|
+ List<ExpenditureManageDO> expenditureManageDOS = expenditureManageMapper.selectList(expenditureManageDOWrapper);
|
|
|
+ double saleSum = 0d;
|
|
|
+ double expenditureSum = 0d;
|
|
|
+ if (!CollectionUtils.isEmpty(salesDOS)) {
|
|
|
+ for (int j = 0; j < salesDOS.size(); j++) {
|
|
|
+ saleSum = saleSum + salesDOS.get(j).getPaidAmount();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!CollectionUtils.isEmpty(expenditureManageDOS)) {
|
|
|
+ for (int j = 0; j < expenditureManageDOS.size(); j++) {
|
|
|
+ expenditureSum = expenditureSum + expenditureManageDOS.get(j).getExpenditureAmount();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ StatisticsView statisticsView1 = new StatisticsView();
|
|
|
+ statisticsView1.setStaticDate(date);
|
|
|
+ statisticsView1.setRevenueAmount(saleSum);
|
|
|
+ statisticsView1.setExpenditureAmount(expenditureSum);
|
|
|
+ statisticsView1.setReceipts(saleSum - expenditureSum);
|
|
|
+ statisticsViews.add(statisticsView1);
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return statisticsViews;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|