jyc 3 месяцев назад
Родитель
Сommit
2ebfdc5b21

+ 69 - 0
alien-entity/src/main/java/shop/alien/entity/store/LifeGroupBuyCount.java

@@ -0,0 +1,69 @@
+package shop.alien.entity.store;
+
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import java.util.Date;
+
+import com.fasterxml.jackson.annotation.JsonFormat;
+import com.fasterxml.jackson.annotation.JsonInclude;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+/**
+ * 团购数据统计
+ * @TableName life_group_buy_count
+ */
+@TableName(value ="life_group_buy_count")
+@JsonInclude
+@Data
+public class LifeGroupBuyCount {
+    /**
+     * 主键id
+     */
+    @TableId
+    private Integer id;
+
+    /**
+     * 统计类型(0访问/1访客/2上架/3下架)
+     */
+    @ApiModelProperty(value = "统计类型(0访问/1访客/2上架/3下架)")
+    @TableField("count_type")
+    private String countType;
+
+    /**
+     * 店铺id
+     */
+    @ApiModelProperty(value = "店铺id")
+    @TableField("store_id")
+    private Integer storeId;
+
+    /**
+     * 团购id
+     */
+    @ApiModelProperty(value = "团购id")
+    @TableField("group_id")
+    private Integer groupId;
+
+    /**
+     * 用户id
+     */
+    @ApiModelProperty(value = "用户id")
+    @TableField("user_id")
+    private Integer userId;
+
+    /**
+     * 数量
+     */
+    @ApiModelProperty(value = "数量")
+    @TableField("count_num")
+    private Integer countNum;
+
+    /**
+     * 统计日期
+     */
+    @ApiModelProperty(value = "统计日期")
+    @TableField("count_date")
+    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
+    private Date countDate;
+}

+ 56 - 0
alien-entity/src/main/java/shop/alien/entity/store/vo/LifeGroupBuyCountDateVo.java

@@ -0,0 +1,56 @@
+package shop.alien.entity.store.vo;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+import shop.alien.entity.store.LifeGroupBuyCount;
+
+@JsonInclude
+@Data
+public class LifeGroupBuyCountDateVo{
+
+    @ApiModelProperty(value = "商品访问量")
+    private String spfwl;
+
+    @ApiModelProperty(value = "人均访问数")
+    private String rjfws;
+
+    @ApiModelProperty(value = "商品访客数")
+    private String spfks;
+
+    @ApiModelProperty(value = "上架套餐数")
+    private String sjtcs;
+
+    @ApiModelProperty(value = "下架套餐数")
+    private String xjtcs;
+
+    @ApiModelProperty(value = "消费人数")
+    private String xfrs;
+
+    @ApiModelProperty(value = "加购人数")
+    private String jgrs;
+
+    @ApiModelProperty(value = "上架商品数")
+    private String sjsps;
+
+    @ApiModelProperty(value = "下架商品数")
+    private String xjsps;
+
+    @ApiModelProperty(value = "商品收藏量")
+    private String spscl;
+
+    @ApiModelProperty(value = "商品取消收藏量")
+    private String spqxscl;
+
+    @ApiModelProperty(value = "收藏率")
+    private String scl;
+
+    @ApiModelProperty(value = "团购id")
+    private String groupId;
+
+    @ApiModelProperty(value = "团购名称")
+    private String groupName;
+
+    @ApiModelProperty(value = "日期")
+    private String countDate;
+}

+ 21 - 0
alien-entity/src/main/java/shop/alien/entity/store/vo/LifeGroupBuyCountVo.java

@@ -0,0 +1,21 @@
+package shop.alien.entity.store.vo;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+import shop.alien.entity.store.LifeGroupBuyCount;
+
+@JsonInclude
+@Data
+public class LifeGroupBuyCountVo extends LifeGroupBuyCount {
+
+    private String personNum;
+
+    private String yesPersonNum;
+
+    @ApiModelProperty(value = "昨日数据")
+    private String yesCountNum;
+
+    @ApiModelProperty(value = "百分比")
+    private String jqrBfb;
+}

+ 48 - 0
alien-entity/src/main/java/shop/alien/mapper/LifeGroupBuyCountMapper.java

@@ -0,0 +1,48 @@
+package shop.alien.mapper;
+
+import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.annotations.Select;
+import shop.alien.entity.store.LifeGroupBuyCount;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import shop.alien.entity.store.vo.LifeGroupBuyCountDateVo;
+import shop.alien.entity.store.vo.LifeGroupBuyCountVo;
+
+import java.util.List;
+
+/**
+* @author youch
+* @description 针对表【life_group_buy_count(团购数据统计)】的数据库操作Mapper
+* @createDate 2025-09-04 15:21:58
+* @Entity shop.alien.entity.store.LifeGroupBuyCount
+*/
+public interface LifeGroupBuyCountMapper extends BaseMapper<LifeGroupBuyCount> {
+
+    void insertList(List<LifeGroupBuyCount> lifeGroupBuyCounts);
+
+    @Select("select sum(count_num) countNum,count(0) personNum,count_type countType,count_date countDate from life_group_buy_count where store_id = #{storeId} and count_date = #{date} group by count_type")
+    List<LifeGroupBuyCountVo> selectJysjListByStoreId(@Param("storeId") String storeId, @Param("date") String date);
+
+    @Select("select count_date countDate, " +
+            "(select count(0) from life_group_buy_count where count_type = '0' and count_date = countDate and store_id = #{storeId}) spfks, " +
+            "ifnull((select sum(count_num) from life_group_buy_count where count_type = '0' and count_date = countDate and store_id = #{storeId}),0) spfwl, " +
+            "ifnull((select sum(count_num) from life_group_buy_count where count_type = '2' and count_date = countDate and store_id = #{storeId}),0) sjtcs, " +
+            "ifnull((select sum(count_num) from life_group_buy_count where count_type = '3' and count_date = countDate and store_id = #{storeId}),0) xjtcs, " +
+            "(select count(0) from life_user_order where store_id = #{storeId} and status = '7' and finish_time LIKE CONCAT('%', countDate, '%')) xfrs, " +
+            "(select count(0) from life_collect where store_id = #{storeId} and created_time LIKE CONCAT('%', countDate, '%') and business_type is null and delete_flag = 0) spscl, " +
+            "(select count(0) from life_collect where store_id = #{storeId} and created_time LIKE CONCAT('%', countDate, '%') and business_type is null and delete_flag = 1) spqxscl " +
+            "from life_group_buy_count where store_id = #{storeId} and count_date between #{startDate} and #{endDate} group by count_date ORDER BY count_date asc")
+    List<LifeGroupBuyCountDateVo> selectSphxzbListByStoreId(@Param("storeId") String storeId, @Param("startDate") String startDate, @Param("endDate") String endDate);
+
+    @Select("select id groupId,group_name groupName, " +
+            "ifnull((select sum(count_num) from life_group_buy_count where count_type = '0' and group_id = groupId),0) spfwl, " +
+            "(select count(DISTINCT(user_id)) from life_group_buy_count where count_type = '0' and group_id = groupId group by count_date) spfks, " +
+            "(select count(0) from life_collect where coupon_id = groupId and business_type is null and delete_flag = 0) spscl, " +
+            "(select count(DISTINCT(luo.user_id)) from life_user_order luo left join order_coupon_middle ocm on luo.id = ocm.order_id  where luo.coupon_type = 2 and luo.status = '7' and ocm.coupon_id = groupId) jgrs, " +
+            "(select count(0) from life_user_order luo left join order_coupon_middle ocm on luo.id = ocm.order_id  where luo.coupon_type = 2 and luo.status = '7' and ocm.coupon_id = groupId) xfrs " +
+            "from life_group_buy_main where store_id = #{storeId} order by spfwl desc")
+    List<LifeGroupBuyCountDateVo> selectDpzbpmListByStoreId(@Param("storeId") String storeId);
+}
+
+
+
+

+ 45 - 0
alien-entity/src/main/resources/mapper/LifeGroupBuyCountMapper.xml

@@ -0,0 +1,45 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper
+        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
+        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="shop.alien.mapper.LifeGroupBuyCountMapper">
+
+    <resultMap id="BaseResultMap" type="shop.alien.entity.store.LifeGroupBuyCount">
+            <id property="id" column="id" />
+            <result property="countType" column="count_type" />
+            <result property="storeId" column="store_id" />
+            <result property="groupId" column="group_id" />
+            <result property="userId" column="user_id" />
+            <result property="countNum" column="count_num" />
+            <result property="countDate" column="count_date" />
+    </resultMap>
+
+    <sql id="Base_Column_List">
+        id,count_type,store_id,group_id,user_id,count_num,
+        count_date
+    </sql>
+
+    <insert id="insertList" parameterType="java.util.List">
+        INSERT INTO life_group_buy_count (
+        id,
+        count_type,
+        store_id,
+        group_id,
+        user_id,
+        count_num,
+        count_date
+        ) VALUES
+        <foreach collection="list" item="item" separator=",">
+            (
+            #{item.id},
+            #{item.countType},
+            #{item.storeId},
+            #{item.groupId},
+            #{item.userId},
+            #{item.countNum},
+            #{item.countDate}
+            )
+        </foreach>
+    </insert>
+
+</mapper>

+ 66 - 0
alien-store/src/main/java/shop/alien/store/controller/LifeGroupBuyCountController.java

@@ -0,0 +1,66 @@
+package shop.alien.store.controller;
+
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiImplicitParam;
+import io.swagger.annotations.ApiImplicitParams;
+import io.swagger.annotations.ApiOperation;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.web.bind.annotation.*;
+import shop.alien.entity.result.R;
+import shop.alien.entity.store.LifeGroupBuyCount;
+import shop.alien.entity.store.dto.LifeGroupBuyDto;
+import shop.alien.entity.store.vo.LifeGroupBuyThaliVo;
+import shop.alien.store.service.LifeGroupBuyCountService;
+
+/**
+ * 团购统计
+ */
+@Api(tags = {"团购统计"})
+@Slf4j
+@CrossOrigin
+@RestController
+@RequestMapping("/groupBuyCount")
+@RequiredArgsConstructor
+public class LifeGroupBuyCountController {
+
+    private final LifeGroupBuyCountService lifeGroupBuyCountService;
+
+    @ApiOperation("插入团购统计")
+    @PostMapping("/insertGroupBuyCount")
+    public R insertGroupBuyCount(@RequestBody LifeGroupBuyCount lifeGroupBuyCount) {
+        log.info("LifeGroupBuyCountController.insertGroupBuyCount?lifeGroupBuyCount={}", lifeGroupBuyCount.toString());
+        try {
+            lifeGroupBuyCountService.insertGroupBuyCount(lifeGroupBuyCount);
+            return R.success("成功");
+        }catch (Exception e){
+            log.error("LifeGroupBuyCountController.insertGroupBuyCount?lifeGroupBuyCount={}", lifeGroupBuyCount.toString(), e);
+            return R.fail("失败");
+        }
+    }
+
+    @ApiOperation("团购统计-经营数据")
+    @ApiImplicitParams({@ApiImplicitParam(name = "storeId", value = "门店id", dataType = "String", paramType = "query", required = true)})
+    @GetMapping("/getJysj")
+    private R getJysj(@RequestParam(value = "storeId") String storeId) {
+        log.info("LifeGroupBuyCountController.getJysj?storeId={}", storeId);
+        return R.data(lifeGroupBuyCountService.getJysj(storeId));
+    }
+
+    @ApiOperation("团购统计-商品核心指标趋势")
+    @ApiImplicitParams({@ApiImplicitParam(name = "storeId", value = "门店id", dataType = "String", paramType = "query", required = true)})
+    @GetMapping("/getSphxzb")
+    private R getSphxzb(@RequestParam(value = "storeId") String storeId) {
+        log.info("LifeGroupBuyCountController.getSphxzb?storeId={}", storeId);
+        return R.data(lifeGroupBuyCountService.getSphxzb(storeId));
+    }
+
+    @ApiOperation("团购统计-单品指标排名")
+    @ApiImplicitParams({@ApiImplicitParam(name = "storeId", value = "门店id", dataType = "String", paramType = "query", required = true)})
+    @GetMapping("/getDpzbpm")
+    private R getDpzbpm(@RequestParam(value = "storeId") String storeId) {
+        log.info("LifeGroupBuyCountController.getDpzbpm?storeId={}", storeId);
+        return R.data(lifeGroupBuyCountService.getDpzbpm(storeId));
+    }
+}

+ 22 - 0
alien-store/src/main/java/shop/alien/store/service/LifeGroupBuyCountService.java

@@ -0,0 +1,22 @@
+package shop.alien.store.service;
+
+import com.baomidou.mybatisplus.extension.service.IService;
+import shop.alien.entity.store.LifeGroupBuyCount;
+import shop.alien.entity.store.vo.LifeGroupBuyCountDateVo;
+import shop.alien.entity.store.vo.LifeGroupBuyCountVo;
+
+import java.util.List;
+
+/**
+ * 团购统计
+ */
+public interface LifeGroupBuyCountService extends IService<LifeGroupBuyCount> {
+
+    void insertGroupBuyCount(LifeGroupBuyCount lifeGroupBuyCount);
+
+    List<LifeGroupBuyCountVo> getJysj(String storeId);
+
+    List<LifeGroupBuyCountDateVo> getSphxzb(String storeId);
+
+    List<LifeGroupBuyCountDateVo> getDpzbpm(String storeId);
+}

+ 134 - 0
alien-store/src/main/java/shop/alien/store/service/impl/LifeGroupBuyCountServiceImpl.java

@@ -0,0 +1,134 @@
+package shop.alien.store.service.impl;
+
+import com.alibaba.fastjson2.util.DateUtils;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
+import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import lombok.RequiredArgsConstructor;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import shop.alien.entity.store.LifeGroupBuyCount;
+import shop.alien.entity.store.LifeUserOrder;
+import shop.alien.entity.store.vo.LifeGroupBuyCountDateVo;
+import shop.alien.entity.store.vo.LifeGroupBuyCountVo;
+import shop.alien.mapper.LifeGroupBuyCountMapper;
+import shop.alien.mapper.LifeUserOrderMapper;
+import shop.alien.store.service.LifeGroupBuyCountService;
+
+import java.math.BigDecimal;
+import java.math.RoundingMode;
+import java.util.Comparator;
+import java.util.Date;
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * 团购统计
+ */
+@Service
+@RequiredArgsConstructor
+public class LifeGroupBuyCountServiceImpl extends ServiceImpl<LifeGroupBuyCountMapper, LifeGroupBuyCount> implements LifeGroupBuyCountService {
+
+    @Autowired
+    private LifeGroupBuyCountMapper lifeGroupBuyCountMapper;
+
+    @Autowired
+    private LifeUserOrderMapper lifeUserOrderMapper;
+
+    @Override
+    public void insertGroupBuyCount(LifeGroupBuyCount lifeGroupBuyCount) {
+        LambdaQueryWrapper<LifeGroupBuyCount> queryWrapper = new LambdaQueryWrapper<>();
+        queryWrapper.eq(LifeGroupBuyCount::getCountType, lifeGroupBuyCount.getCountType())
+                .eq(LifeGroupBuyCount::getStoreId, lifeGroupBuyCount.getStoreId())
+                .eq(LifeGroupBuyCount::getGroupId, lifeGroupBuyCount.getGroupId())
+                .eq(LifeGroupBuyCount::getUserId, lifeGroupBuyCount.getUserId())
+                .eq(LifeGroupBuyCount::getCountDate, DateUtils.format(new Date(), "yyyy-MM-dd"));
+        Integer count = lifeGroupBuyCountMapper.selectCount(queryWrapper);
+        if (count > 0) {
+            List<LifeGroupBuyCount> lifeGroupBuyCounts = lifeGroupBuyCountMapper.selectList(queryWrapper);
+            LambdaUpdateWrapper<LifeGroupBuyCount> updateWrapper = new LambdaUpdateWrapper<>();
+            updateWrapper.eq(LifeGroupBuyCount::getCountType, lifeGroupBuyCount.getCountType())
+                    .eq(LifeGroupBuyCount::getStoreId, lifeGroupBuyCount.getStoreId())
+                    .eq(LifeGroupBuyCount::getGroupId, lifeGroupBuyCount.getGroupId())
+                    .eq(LifeGroupBuyCount::getUserId, lifeGroupBuyCount.getUserId())
+                    .eq(LifeGroupBuyCount::getCountDate, DateUtils.format(new Date(), "yyyy-MM-dd"))
+                    .set(LifeGroupBuyCount::getCountNum, lifeGroupBuyCounts.get(0).getCountNum() + 1);
+            lifeGroupBuyCountMapper.update(null, queryWrapper);
+        } else {
+            lifeGroupBuyCount.setCountNum(1);
+            lifeGroupBuyCountMapper.insert(lifeGroupBuyCount);
+        }
+    }
+
+    @Override
+    public List<LifeGroupBuyCountVo> getJysj(String storeId) {
+        String today = DateUtils.format(new Date(), "yyyy-MM-dd");
+        String yesterday = DateUtils.format(shop.alien.util.common.DateUtils.calcDays(new Date(), -1), "yyyy-MM-dd");
+
+        List<LifeGroupBuyCountVo> todayList = lifeGroupBuyCountMapper.selectJysjListByStoreId(storeId, today);
+        List<LifeGroupBuyCountVo> yesterdayList = lifeGroupBuyCountMapper.selectJysjListByStoreId(storeId, yesterday);
+        if (ObjectUtils.isNotEmpty(todayList)) {
+            for (LifeGroupBuyCountVo lifeGroupBuyCountVo : todayList) {
+                //昨天数据
+                if (yesterdayList.stream().map(LifeGroupBuyCount::getCountType).collect(Collectors.toList()).contains(lifeGroupBuyCountVo.getCountType())) {
+                    yesterdayList.stream().filter(item -> item.getCountType().equals(lifeGroupBuyCountVo.getCountType())).findFirst().ifPresent(item -> lifeGroupBuyCountVo.setYesCountNum(item.getCountNum().toString()));
+                }
+            }
+        }
+
+        List<LifeGroupBuyCountVo> collect = todayList.stream().filter(item -> "0".equals(item.getCountType())).collect(Collectors.toList());
+        if (ObjectUtils.isNotEmpty(collect)) {
+            LifeGroupBuyCountVo lifeGroupBuyCountVo = collect.get(0);
+
+            LifeGroupBuyCountVo countVo = new LifeGroupBuyCountVo();
+            //访客
+            countVo.setCountType("1");
+            countVo.setCountNum(Integer.valueOf(lifeGroupBuyCountVo.getPersonNum()));
+            //昨天的访客人数
+            yesterdayList.stream().filter(item -> "0".equals(item.getCountType())).findFirst().ifPresent(item -> countVo.setYesCountNum(item.getPersonNum()));
+            todayList.add(countVo);
+        }
+
+        LambdaQueryWrapper<LifeUserOrder> orderLambdaQueryWrapper = new LambdaQueryWrapper<>();
+        orderLambdaQueryWrapper.eq(LifeUserOrder::getStoreId, storeId)
+                .eq(LifeUserOrder::getStatus, "7")
+                .like(LifeUserOrder::getFinishTime, today);
+        Integer todayOrderNum = lifeUserOrderMapper.selectCount(orderLambdaQueryWrapper);
+
+        LambdaQueryWrapper<LifeUserOrder> orderLambdaQueryWrapper1 = new LambdaQueryWrapper<>();
+        orderLambdaQueryWrapper1.eq(LifeUserOrder::getStoreId, storeId)
+                .eq(LifeUserOrder::getStatus, "7")
+                .like(LifeUserOrder::getFinishTime, yesterday);
+        Integer yesterdayOrderNum = lifeUserOrderMapper.selectCount(orderLambdaQueryWrapper1);
+        LifeGroupBuyCountVo countVo = new LifeGroupBuyCountVo();
+        countVo.setCountType("4");
+        countVo.setCountNum(todayOrderNum);
+        countVo.setYesCountNum(yesterdayOrderNum.toString());
+        todayList.add(countVo);
+        bfb(todayList);
+        return todayList.stream().sorted(Comparator.comparing(LifeGroupBuyCountVo::getCountType)).collect(Collectors.toList());
+    }
+
+    @Override
+    public List<LifeGroupBuyCountDateVo> getSphxzb(String storeId) {
+        return lifeGroupBuyCountMapper.selectSphxzbListByStoreId(storeId, DateUtils.format(new Date(), "yyyy-MM-dd"), DateUtils.format(shop.alien.util.common.DateUtils.calcDays(new Date(), -7), "yyyy-MM-dd"));
+    }
+
+    @Override
+    public List<LifeGroupBuyCountDateVo> getDpzbpm(String storeId) {
+        List<LifeGroupBuyCountDateVo> lifeGroupBuyCountDateVos = lifeGroupBuyCountMapper.selectDpzbpmListByStoreId(storeId);
+        for (LifeGroupBuyCountDateVo lifeGroupBuyCountDateVo : lifeGroupBuyCountDateVos) {
+            lifeGroupBuyCountDateVo.setRjfws(StringUtils.isNotEmpty(lifeGroupBuyCountDateVo.getSpfks()) ? new BigDecimal(lifeGroupBuyCountDateVo.getSpfwl()).divide(new BigDecimal(lifeGroupBuyCountDateVo.getSpfks()), 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100)) + "%" : "0.0%");
+            lifeGroupBuyCountDateVo.setScl(StringUtils.isNotEmpty(lifeGroupBuyCountDateVo.getSpfks()) ? new BigDecimal(lifeGroupBuyCountDateVo.getSpscl()).divide(new BigDecimal(lifeGroupBuyCountDateVo.getSpfks()), 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100)) + "%" : "0.0%");
+        }
+        return lifeGroupBuyCountDateVos;
+    }
+
+    private void bfb(List<LifeGroupBuyCountVo> list) {
+        for (LifeGroupBuyCountVo item : list) {
+                item.setJqrBfb(StringUtils.isNotEmpty(item.getYesCountNum()) && !"0".equals(item.getYesCountNum()) ? new BigDecimal(item.getYesCountNum()).subtract(new BigDecimal(item.getCountNum())).divide(new BigDecimal(item.getYesCountNum()), 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100)) + "%" : "0.0%");
+        }
+    }
+}