Pu Zhibing
2025-04-03 1f09f6daaf73bc83cceb4ae22b862b7b365635cf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package com.ruoyi.other.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.page.PageInfo;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.other.api.domain.Shop;
import com.ruoyi.other.api.domain.ShopGiveawayTemporary;
import com.ruoyi.other.api.domain.ShopPoint;
import com.ruoyi.other.api.domain.ShopPointCopy;
import com.ruoyi.other.api.enums.PointChangeType;
import com.ruoyi.other.mapper.ShopGiveawayTemporaryMapper;
import com.ruoyi.other.mapper.ShopPointCopyMapper;
import com.ruoyi.other.mapper.ShopPointMapper;
import com.ruoyi.other.service.ShopPointService;
import com.ruoyi.other.service.ShopService;
import com.ruoyi.other.vo.ShopPointStatistics;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.feignClient.SysUserClient;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author luodangjia
 * @since 2024-11-20
 */
@Service
public class ShopPointServiceImpl extends ServiceImpl<ShopPointMapper, ShopPoint> implements ShopPointService {
 
    @Resource
    private TokenService tokenService;
    @Resource
    private SysUserClient sysUserClient;
    @Resource
    private ShopService shopService;
    @Resource
    private ShopGiveawayTemporaryMapper shopGiveawayTemporaryMapper;
    
    @Resource
    private ShopPointCopyMapper shopPointCopyMapper;
    
 
 
 
 
    @Override
    public ShopPointStatistics statistics(IPage<ShopPoint> page, ShopPoint shopPoint) {
        Long userid = tokenService.getLoginUser().getUserid();
        SysUser sysUser = sysUserClient.getSysUser(userid).getData();
        if (sysUser.getRoleType()==1){
 
            ShopPointStatistics shopPointStatistics = new ShopPointStatistics();
            shopPointStatistics.setTotalPoint(0);
            shopPointStatistics.setSubShopCommissionPoint(0);
            shopPointStatistics.setShopPoint(0);
            shopPointStatistics.setShopCommissionPoint(0);
            
            IPage<ShopPoint> shopPointIPage = this.baseMapper.queryShpointPage(page, shopPoint);
            shopPointIPage.getRecords().forEach(s->s.setVariablePoint(s.getVariablePoint() * s.getChangeDirection()));
            shopPointStatistics.setShopPointIPage(shopPointIPage);
    
            IPage<ShopPoint> page1 = new PageInfo<>(0, 100000);
            IPage<ShopPoint> shopPointIPage1 = this.baseMapper.queryShpointPage(page1, shopPoint);
            List<ShopPoint> shopPointList = shopPointIPage1.getRecords();
            shopPointList.forEach(s->s.setVariablePoint(s.getVariablePoint() * s.getChangeDirection()));
            Map<Integer, List<ShopPoint>> shopPointByTypeMap = shopPointList.stream().collect(Collectors.groupingBy(ShopPoint::getType));
            shopPointByTypeMap.forEach((type, shopPoints) -> {
                shopPoints.stream().map(ShopPoint::getVariablePoint).reduce(Integer::sum).ifPresent(sum -> {
                    switch (type) {
                        case 1:
                            shopPointStatistics.setShopPoint(sum);
                            break;
                        case 2:
                            shopPointStatistics.setShopCommissionPoint(sum);
                            break;
                        case 3:
                            shopPointStatistics.setSubShopCommissionPoint(sum);
                            break;
                        default:
                            break;
                    }
                });
            });
 
            LocalDateTime startTime = shopPoint.getStartTime();
            LocalDateTime endTime = shopPoint.getEndTime();
            if(null != startTime){
                List<ShopPoint> list = this.list(new LambdaUpdateWrapper<ShopPoint>()
                        .eq(ShopPoint::getShopId, sysUser.getObjectId())
                        .last(" and create_time between '" + startTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + "' and '" + endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + "'"));
                List<String> date = new ArrayList<>();
                List<Map<String, Integer>> data = new ArrayList<>();
                String format = "MM.dd";
                while (true){
                    if(startTime.isAfter(endTime)){
                        break;
                    }
                    date.add(startTime.format(DateTimeFormatter.ofPattern(format)));
                    Map<String, Integer> map = new HashMap<>();
                    //返佣积分
                    LocalDateTime finalStartTime = startTime;
                    List<ShopPoint> collect = list.stream().filter(s -> s.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))
                            .equals(finalStartTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))) && s.getType() == 2).collect(Collectors.toList());
                    map.put("fyjf", collect.stream().mapToInt(ShopPoint::getVariablePoint).sum());
 
                    //服务积分
                    List<ShopPoint> collect1 = list.stream().filter(s -> s.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))
                            .equals(finalStartTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))) && s.getType() == 1).collect(Collectors.toList());
                    map.put("fwjf", collect1.stream().mapToInt(ShopPoint::getVariablePoint).sum());
                    //绑定下级门店返佣积分
                    List<ShopPoint> collect2 = list.stream().filter(s -> s.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))
                            .equals(finalStartTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))) && s.getType() == 3).collect(Collectors.toList());
                    map.put("xjmdfyjf", collect2.stream().mapToInt(ShopPoint::getVariablePoint).sum());
                    data.add(map);
                    startTime = startTime.plusDays(1);
                }
                Map<String, Object> brokenLine = new HashMap<>();
                brokenLine.put("date", date);
                brokenLine.put("data", data);
                shopPointStatistics.setBrokenLine(brokenLine);
            }
    
            Integer shopPoint1 = shopPointStatistics.getShopPoint();
            Integer shopCommissionPoint = shopPointStatistics.getShopCommissionPoint();
            Integer subShopCommissionPoint = shopPointStatistics.getSubShopCommissionPoint();
            shopPointStatistics.setTotalPoint(shopPoint1 + shopCommissionPoint + subShopCommissionPoint);
            return shopPointStatistics;
        }else{
            Shop shop = shopService.getById(sysUser.getObjectId());
            List<ShopGiveawayTemporary> list1 = shopGiveawayTemporaryMapper.selectList(new LambdaQueryWrapper<ShopGiveawayTemporary>().eq(ShopGiveawayTemporary::getShopId, shop.getId()));
            int serverPointCopy = list1.stream().mapToInt(ShopGiveawayTemporary::getServerPoint).sum();
            int sharePointCopy = list1.stream().mapToInt(ShopGiveawayTemporary::getSharePoint).sum();
            int lowerLevelSharePointCopy = list1.stream().mapToInt(ShopGiveawayTemporary::getLowerLevelSharePoint).sum();
            shopPoint.setShopId(shop.getId());
            Integer serverPoint = shop.getServerPoint() + serverPointCopy;
            Integer sharePoint = shop.getSharePoint() + sharePointCopy;
            Integer lowerLevelSharePoint = shop.getLowerLevelSharePoint() + lowerLevelSharePointCopy;
            ShopPointStatistics shopPointStatistics = new ShopPointStatistics();
            shopPointStatistics.setTotalPoint(serverPoint + sharePoint + lowerLevelSharePoint);
            shopPointStatistics.setShopPoint(serverPoint);
            shopPointStatistics.setShopCommissionPoint(sharePoint);
            shopPointStatistics.setSubShopCommissionPoint(lowerLevelSharePoint);
 
            IPage<ShopPoint> shopPointIPage = this.baseMapper.queryShpointPage(page, shopPoint);
            shopPointIPage.getRecords().forEach(s->s.setVariablePoint(s.getVariablePoint() * s.getChangeDirection()));
            shopPointStatistics.setShopPointIPage(shopPointIPage);
 
            LocalDateTime startTime = shopPoint.getStartTime();
            LocalDateTime endTime = shopPoint.getEndTime();
            if(null != startTime){
                List<ShopPoint> list = this.list(new LambdaUpdateWrapper<ShopPoint>()
                        .eq(ShopPoint::getShopId, sysUser.getObjectId())
                        .last(" and create_time between '" + startTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + "' and '" + endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + "'"));
                List<String> date = new ArrayList<>();
                List<Map<String, Integer>> data = new ArrayList<>();
                String format = "MM.dd";
                while (true){
                    if(startTime.isAfter(endTime)){
                        break;
                    }
                    date.add(startTime.format(DateTimeFormatter.ofPattern(format)));
                    Map<String, Integer> map = new HashMap<>();
                    //返佣积分
                    LocalDateTime finalStartTime = startTime;
                    List<ShopPoint> collect = list.stream().filter(s -> s.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))
                            .equals(finalStartTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))) && s.getType() == 2).collect(Collectors.toList());
                    map.put("fyjf", collect.stream().mapToInt(ShopPoint::getVariablePoint).sum());
 
                    //服务积分
                    List<ShopPoint> collect1 = list.stream().filter(s -> s.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))
                            .equals(finalStartTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))) && s.getType() == 1).collect(Collectors.toList());
                    map.put("fwjf", collect1.stream().mapToInt(ShopPoint::getVariablePoint).sum());
                    //绑定下级门店返佣积分
                    List<ShopPoint> collect2 = list.stream().filter(s -> s.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))
                            .equals(finalStartTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))) && s.getType() == 3).collect(Collectors.toList());
                    map.put("xjmdfyjf", collect2.stream().mapToInt(ShopPoint::getVariablePoint).sum());
                    data.add(map);
                    startTime = startTime.plusDays(1);
                }
                Map<String, Object> brokenLine = new HashMap<>();
                brokenLine.put("date", date);
                brokenLine.put("data", data);
                shopPointStatistics.setBrokenLine(brokenLine);
            }
            return shopPointStatistics;
        }
    }
    
    
    /**
     * 获取门店积分流水
     * @param page
     * @param shopPoint
     * @return
     */
    @Override
    public IPage<ShopPoint> getShopPointPage(IPage<ShopPoint> page, ShopPoint shopPoint) {
        return this.baseMapper.queryShpointPage(page, shopPoint);
    }
}