diff --git a/src/main/java/com/zmzm/finance/common/controller/StatisticsController.java b/src/main/java/com/zmzm/finance/common/controller/StatisticsController.java index 6be5398..c43bd92 100644 --- a/src/main/java/com/zmzm/finance/common/controller/StatisticsController.java +++ b/src/main/java/com/zmzm/finance/common/controller/StatisticsController.java @@ -1,20 +1,22 @@ package com.zmzm.finance.common.controller; import com.github.yulichang.wrapper.MPJLambdaWrapper; -import com.zmzm.finance.common.entity.Orders; -import com.zmzm.finance.common.entity.Payment; -import com.zmzm.finance.common.entity.User; +import com.zmzm.finance.common.entity.*; import com.zmzm.finance.common.service.IOrdersService; import com.zmzm.finance.common.service.IPaymentService; import com.zmzm.finance.common.service.IUserService; +import com.zmzm.finance.common.service.IUserVipLogService; import com.zmzm.finance.util.R; import lombok.extern.slf4j.Slf4j; +import org.apache.http.client.utils.DateUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.annotation.Order; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import java.math.BigDecimal; +import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -29,28 +31,100 @@ public class StatisticsController { @Autowired private IOrdersService ordersService; @Autowired - private IPaymentService paymentService; + private IUserVipLogService userVipLogService; //天医币统计 @RequestMapping("/pointStatistics") public R pointStatistics(@RequestBody Map params){ Map map = new HashMap<>(); - map.put("income",0); - map.putAll(userService.getMap(new MPJLambdaWrapper() - .selectSum(User::getPoint,"totalPoint"))); - Map income = ordersService.getMap(new MPJLambdaWrapper() - .eq(Orders::getType,0).eq(Orders::getUseFlag,1) + //收入 + List> incomes = ordersService.listMaps(new MPJLambdaWrapper() + .rightJoin(PaymentToOrder.class,PaymentToOrder::getOrderId,Orders::getId) + .leftJoin(Payment.class,Payment::getId,PaymentToOrder::getPaymentId) + .eq(Orders::getType,0) .apply("DATE_FORMAT(t.order_time, '%Y-%m') = '"+params.get("year")+"-"+params.get("month")+"'") - .selectSum(Orders::getFee,"income")); - if (income!=null){ - map.putAll(income); + .groupBy(Payment::getType) + .select("IF(t2.type=0,'微信',IF(t2.type=1,'支付宝','银行')) type") + .selectSum(Orders::getFee,"fee")); + map.put("incomes",incomes); + //消费 + List> consumes = ordersService.listMaps(new MPJLambdaWrapper() + .eq(Orders::getUseFlag,1).eq(Orders::getPayType,1) + .apply("DATE_FORMAT(t.order_time, '%Y-%m') = '"+params.get("year")+"-"+params.get("month")+"'") + .groupBy(Orders::getType) + .select("IF(type=1,'vip',IF(type=2,'课程',IF(type=3,'实物','培训班'))) type") + .selectSum(Orders::getFee,"fee")); + map.put("consumes",consumes); + //总额 + Map totalPoint = userService.getMap(new MPJLambdaWrapper() + .selectSum(User::getPoint,"totalPoint")); + //指定月份以后的冲销差额 + Map diff = ordersService.getMap(new MPJLambdaWrapper() + .eq(Orders::getUseFlag,1).and(t->t.eq(Orders::getType,0).or().eq(Orders::getPayType,1)) + .apply("DATE_FORMAT(t.order_time, '%Y-%m') > '"+params.get("year")+"-"+params.get("month")+"'") + .select("SUM(IF(type=0,fee,fee*-1)) diff")); + //目前总额+差额 + map.put("surplus",new BigDecimal(totalPoint.get("totalPoint").toString()).add(diff==null?BigDecimal.ZERO:new BigDecimal(diff.get("diff").toString()))); + return R.ok().putData("map",map); + } + + //vip + @RequestMapping("/vipStatistics") + public R vipStatistics(@RequestBody Map params){ + //收入 + List> incomes = ordersService.listMaps(new MPJLambdaWrapper() + .leftJoin(PaymentToOrder.class,PaymentToOrder::getOrderId,Orders::getId) + .leftJoin(Payment.class,Payment::getId,PaymentToOrder::getPaymentId) + .disableSubLogicDel() + .eq(Orders::getUseFlag,1).eq(Orders::getType,1) + .apply("DATE_FORMAT(t.order_time, '%Y-%m') = '"+params.get("year")+"-"+params.get("month")+"'") + .select("IF(t.pay_type=1,'天医币',IF(t2.type=0,'微信',IF(t2.type=1,'支付宝','银行'))) type,SUM(t.fee) fee") + .groupBy(Payment::getType)); + //所有vip订单 + List vipOrders = ordersService.list(new MPJLambdaWrapper() + .eq(Orders::getUseFlag,1).eq(Orders::getType,1)); + String date = DateUtils.formatDate(new Date(),"yyyy-MM-dd"); + + for(Orders vipOrder:vipOrders){ + if (vipOrder.getSource()==1){ + Map wumenVipLog = userVipLogService.getMap(new MPJLambdaWrapper() + .eq(UserVipLog::getOrderSn,vipOrder.getOrderSn()) + .apply("DATE_FORMAT(start_time, '%Y-%m') <= DATE_FORMAT('"+date+"', '%Y-%m') and DATE_FORMAT(end_time, '%Y-%m') >= DATE_FORMAT('"+date+"', '%Y-%m')") + .select("sum(fee) fee,start_time,end_time") + .groupBy(UserVipLog::getOrderSn)); + } } + return R.ok().putData("incomes",incomes); + } + //实物统计 + @RequestMapping("/physicalStatistics") + public R physicalStatistics(@RequestBody Map params){ + List> physical = ordersService.listMaps(new MPJLambdaWrapper() + .leftJoin(PaymentToOrder.class,PaymentToOrder::getOrderId,Orders::getId) + .leftJoin(Payment.class,Payment::getId,PaymentToOrder::getPaymentId) + .disableSubLogicDel() + .eq(Orders::getUseFlag,1).eq(Orders::getType,3) + .apply("DATE_FORMAT(t.order_time, '%Y-%m') = '"+params.get("year")+"-"+params.get("month")+"'") + .select("IF(t.pay_type=1,'天医币',IF(t2.type=0,'微信',IF(t2.type=1,'支付宝','银行'))) type,SUM(t.fee) fee") + .groupBy(Payment::getType)); + return R.ok().putData("map",physical); + } - - return R.ok().putData("map",map); + //培训班 + @RequestMapping("/trainingClassStatistics") + public R trainingClassStatistics(@RequestBody Map params){ + List> trainingClass = ordersService.listMaps(new MPJLambdaWrapper() + .leftJoin(PaymentToOrder.class,PaymentToOrder::getOrderId,Orders::getId) + .leftJoin(Payment.class,Payment::getId,PaymentToOrder::getPaymentId) + .disableSubLogicDel() + .eq(Orders::getUseFlag,1).eq(Orders::getType,4) + .apply("DATE_FORMAT(t.order_time, '%Y-%m') = '"+params.get("year")+"-"+params.get("month")+"'") + .select("IF(t.pay_type=1,'天医币',IF(t2.type=0,'微信',IF(t2.type=1,'支付宝','银行'))) type,SUM(t.fee) fee") + .groupBy(Payment::getType)); + return R.ok().putData("map",trainingClass); }