puzhibing
2023-07-26 709a4a10be56952ead6340e4822fce41a66e47cd
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package com.stylefeng.guns.modular.system.service.impl;
 
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.modular.system.controller.resp.TAgentResp;
import com.stylefeng.guns.modular.system.dao.*;
import com.stylefeng.guns.modular.system.enums.OrderStateEnum;
import com.stylefeng.guns.modular.system.enums.PayStatusEnum;
import com.stylefeng.guns.modular.system.enums.StatusEnum;
import com.stylefeng.guns.modular.system.enums.UserTypeEnum;
import com.stylefeng.guns.modular.system.model.*;
import com.stylefeng.guns.modular.system.service.ITAgentService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
 
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 代理商 服务实现类
 * </p>
 *
 * @author stylefeng
 * @since 2023-02-20
 */
@Service
public class TAgentServiceImpl extends ServiceImpl<TAgentMapper, TAgent> implements ITAgentService {
 
 
    @Autowired
    private TAgentMapper tAgentMapper;
 
    @Autowired
    private TOrderMapper tOrderMapper;
 
    @Autowired
    private TCouponMapper tCouponMapper;
 
    @Autowired
    private TDriverMapper tDriverMapper;
 
    @Autowired
    private TRechargeRecordMapper tRechargeRecordMapper;
    @Autowired
    private TUserToCouponMapper tUserToCouponMapper;
 
    @Override
    public EntityWrapper<TAgent> getAgentWrapper(String principal, String principalPhone, String createTime,Integer status) {
        EntityWrapper<TAgent> wrapper = new EntityWrapper<>();
        // 昵称
        if(StringUtils.hasLength(principal)){
            wrapper.like("principal",principal);
        }
        // 手机号
        if(StringUtils.hasLength(principalPhone)){
            wrapper.like("principalPhone",principalPhone);
        }
        // 开始,结束时间
        if(StringUtils.hasLength(createTime)){
            String[] split = createTime.split(" - ");
            Date startTime = DateUtil.getDate_str3(split[0]+" 00:00:00");
            Date endTime = DateUtil.getDate_str3(split[1]+" 23:59:59");
            wrapper.between("createTime",startTime,endTime);
        }
        // 判断是否为代理商
        if(Objects.requireNonNull(ShiroKit.getUser()).getRoleType() == 3){
            wrapper.eq("id",ShiroKit.getUser().getObjectId());
        }
        // 状态
        if(Objects.nonNull(status)){
            wrapper.eq("status",status);
        }
        wrapper.ne("status", StatusEnum.DELETE.getCode());
        wrapper.orderBy("createTime",false);
        return wrapper;
    }
 
    @Override
    public void detail(Integer agentId, Model model) {
        // 查询代理商用户信息
        TAgent tAgent = tAgentMapper.selectById(agentId);
        model.addAttribute("createTime",DateUtil.formatDate(tAgent.getCreateTime()));
 
        model.addAttribute("startTimeToEndTime",new SimpleDateFormat("yyyy-MM-dd").format(tAgent.getCreateTime()).replace("-",".") + "-" +
                new SimpleDateFormat("yyyy-MM-dd").format(new Date()).replace("-","."));
 
        model.addAttribute("principal",tAgent.getPrincipal());
        model.addAttribute("principalPhone",tAgent.getPrincipalPhone());
        model.addAttribute("email",tAgent.getEmail());
        model.addAttribute("serviceCalls",tAgent.getServiceCalls());
        model.addAttribute("area",tAgent.getProvinceName()+tAgent.getCityName());
        // 订单信息
        // 订单总量
        List<TOrder> orderList = tOrderMapper.selectList(new EntityWrapper<TOrder>().eq("agentId",tAgent.getId()));
        model.addAttribute("orderSum",orderList.size());
        // 过滤已完成和待评价订单,且支付金额在14元以上
        List<TOrder> orders = orderList.stream().filter(order -> (order.getState().equals(OrderStateEnum.FINISH.getCode()) || order.getState().equals(OrderStateEnum.WAIT_EVALUATED.getCode()))
                && 0 < order.getOrderMoney().compareTo(new BigDecimal("14")) && order.getAgentId().equals(tAgent.getId())).collect(Collectors.toList());
        model.addAttribute("validOrder",orders.size());
        // 过滤已使用优惠券
        List<TOrder> usedCoupon = orderList.stream().filter(order -> (order.getState().equals(OrderStateEnum.FINISH.getCode()) || order.getState().equals(OrderStateEnum.WAIT_EVALUATED.getCode()))
                && Objects.nonNull(order.getCouponId()) && order.getAgentId().equals(tAgent.getId())).collect(Collectors.toList());
//        model.addAttribute("usedCoupon",usedCoupon.size());
        // 已使用优惠券金额求和,已使用优惠券id
//        List<Integer> usedCouponId = usedCoupon.stream().map(TOrder::getCouponId).collect(Collectors.toList());
//        if(CollectionUtils.isEmpty(usedCouponId)){
//            // 如果优惠券为空
//            model.addAttribute("couponPriceSum", BigDecimal.ZERO);
//        }else {
//            // 查询已使用优惠券金额求和
//            List<TCoupon> tCoupons1 = tCouponMapper.selectList(new EntityWrapper<TCoupon>().in("id", usedCouponId));
//            if(CollectionUtils.isEmpty(tCoupons1)){
//                model.addAttribute("couponPriceSum", BigDecimal.ZERO);
//            }else {
//                tCoupons1.stream().map(TCoupon::getCouponPreferentialAmount).reduce(BigDecimal::add).ifPresent(bigDecimal -> model.addAttribute("couponPriceSum", bigDecimal));
//            }
//        }
        // 优惠券信息
        // 过滤代理商发放的优惠券
        List<TUserToCoupon> tUserToCoupons = tUserToCouponMapper.selectList(new EntityWrapper<TUserToCoupon>().eq("objectId",tAgent.getId())
                .eq("roleType",3));
        BigDecimal couponPriceSum = BigDecimal.ZERO;
        for (TUserToCoupon userToCoupon : tUserToCoupons) {
            TCoupon tCoupons1 = tCouponMapper.selectById(userToCoupon.getCouponId());
            if(Objects.nonNull(tCoupons1)){
                couponPriceSum = couponPriceSum.add(tCoupons1.getCouponPreferentialAmount().multiply(new BigDecimal(userToCoupon.getCouponTotal())));
            }
        }
        model.addAttribute("couponPriceSum", couponPriceSum);
        // 过滤代理商发放的优惠券
        int sum = tUserToCoupons.stream().mapToInt(TUserToCoupon::getCouponTotal).sum();
        model.addAttribute("issuedCoupon",sum);
        // 总量减去有效数量和过期数量为使用数量
        int validCount = tUserToCoupons.stream().mapToInt(TUserToCoupon::getValidCount).sum();//有效数量
        int expireCount = tUserToCoupons.stream().mapToInt(TUserToCoupon::getExpireCount).sum();//过期数量
        model.addAttribute("usedCoupon",sum-validCount-expireCount);
        // 司机信息
        // 司机数
        List<TDriver> drivers = tDriverMapper.selectList(new EntityWrapper<TDriver>().eq("agentId", tAgent.getId()));
        model.addAttribute("driverCount",drivers.size());
        // 司机充值计算
        List<Integer> driverIds = drivers.stream().map(TDriver::getId).collect(Collectors.toList());
        List<TRechargeRecord> tRechargeRecords = tRechargeRecordMapper.selectList(new EntityWrapper<TRechargeRecord>()
                .in("userId", driverIds)
                .eq("type", UserTypeEnum.AGENT.getCode())
                .eq("payStatus", PayStatusEnum.FINISH.getCode()));
        if(CollectionUtils.isEmpty(tRechargeRecords)){
            model.addAttribute("driverRecharge",BigDecimal.ZERO);
        }else {
            tRechargeRecords.stream().map(TRechargeRecord::getAmount).reduce(BigDecimal::add).ifPresent(record->model.addAttribute("driverRecharge",record));
        }
    }
 
    @Override
    public List<TAgentResp> getAgentResp(List<TAgent> tAgents) {
        // 查询订单
        List<TOrder> tOrders = tOrderMapper.selectList(new EntityWrapper<TOrder>());
        // 查询优惠券
        List<TCoupon> tCoupons = tCouponMapper.selectList(new EntityWrapper<TCoupon>());
        // 用户优惠券
        List<TUserToCoupon> tUserToCoupons = tUserToCouponMapper.selectList(new EntityWrapper<TUserToCoupon>());
        // 查询司机信息
        List<TDriver> tDrivers = tDriverMapper.selectList(new EntityWrapper<TDriver>());
 
        List<TAgentResp> tAgentRespList = new ArrayList<>(tAgents.size());
 
        for (TAgent tAgent : tAgents) {
            TAgentResp tAgentResp = new TAgentResp();
            BeanUtils.copyProperties(tAgent,tAgentResp);
 
            tAgentResp.setArea(tAgent.getProvinceName()+tAgent.getCityName());
 
            if(!CollectionUtils.isEmpty(tOrders)){
                // 订单总量
                List<TOrder> orderList = tOrders.stream().filter(order -> tAgent.getId().equals(order.getAgentId())).collect(Collectors.toList());
                tAgentResp.setOrderSum(orderList.size());
                // 过滤已完成和待评价订单,且支付金额在14元以上
                List<TOrder> orders = tOrders.stream().filter(order -> (order.getState().equals(OrderStateEnum.FINISH.getCode()) || order.getState().equals(OrderStateEnum.WAIT_EVALUATED.getCode()))
                        && 0 < order.getOrderMoney().compareTo(new BigDecimal("14")) && tAgent.getId().equals(order.getAgentId())).collect(Collectors.toList());
                tAgentResp.setValidOrder(orders.size());
                // 过滤已使用优惠券
                List<TOrder> usedCoupon = tOrders.stream().filter(order -> (order.getState().equals(OrderStateEnum.FINISH.getCode()) || order.getState().equals(OrderStateEnum.WAIT_EVALUATED.getCode()))
                        && Objects.nonNull(order.getCouponId()) && tAgent.getId().equals(order.getAgentId())).collect(Collectors.toList());
                // 通过订单总量获取已完成订单的使用优惠券数量及优惠券金额
                tAgentResp.setUsedCoupon(usedCoupon.size());
                // 金额
                Optional<BigDecimal> reduce = usedCoupon.stream().map(TOrder::getDiscountedPrice).reduce(BigDecimal::add);
                reduce.ifPresent(tAgentResp::setCouponPriceSum);
 
 
                // 已使用优惠券金额求和,已使用优惠券id
//                List<Integer> usedCouponId = usedCoupon.stream().map(TOrder::getCouponId).collect(Collectors.toList());
//                if(CollectionUtils.isEmpty(usedCouponId)){
//                    tAgentResp.setCouponPriceSum(BigDecimal.ZERO);
//                }else {
//                    // 查询已使用优惠券金额求和
//                    BigDecimal couponPriceSum = BigDecimal.ZERO;
//                    for (Integer id : usedCouponId) {
//                        TCoupon tCoupons1 = tCouponMapper.selectById(id);
//                        couponPriceSum = couponPriceSum.add(tCoupons1.getCouponPreferentialAmount());
//                    }
//                    tAgentResp.setCouponPriceSum(couponPriceSum);
//                }
            }
 
//            if(!CollectionUtils.isEmpty(tUserToCoupons)){
//                // 过滤代理商发放的优惠券
//                List<TUserToCoupon> userToCoupons = tUserToCoupons.stream().filter(coupon -> tAgent.getId().equals(coupon.getObjectId()) && coupon.getRoleType() == 3).collect(Collectors.toList());
//                int sum = userToCoupons.stream().mapToInt(TUserToCoupon::getCouponTotal).sum();
//                tAgentResp.setIssuedCoupon(sum);
//                // 总量减去有效数量和过期数量为使用数量
//                int validCount = userToCoupons.stream().mapToInt(TUserToCoupon::getValidCount).sum();//有效数量
//                int expireCount = userToCoupons.stream().mapToInt(TUserToCoupon::getExpireCount).sum();//过期数量
//                tAgentResp.setUsedCoupon(sum-validCount-expireCount);
//                BigDecimal couponPriceSum = BigDecimal.ZERO;
//                for (TUserToCoupon userToCoupon : userToCoupons) {
//                    TCoupon tCoupons1 = tCouponMapper.selectById(userToCoupon.getCouponId());
//                    if(Objects.nonNull(tCoupons1)){
//                        couponPriceSum = couponPriceSum.add(tCoupons1.getCouponPreferentialAmount().multiply(new BigDecimal(userToCoupon.getCouponTotal())));
//                    }
//                }
//                tAgentResp.setCouponPriceSum(couponPriceSum);
//            }
 
            if(!CollectionUtils.isEmpty(tDrivers)){
                // 司机数
                List<TDriver> drivers = tDrivers.stream().filter(driver -> tAgent.getId().equals(driver.getAgentId())).collect(Collectors.toList());
                tAgentResp.setDriverCount(drivers.size());
                // 司机充值计算
                List<Integer> driverIds = drivers.stream().map(TDriver::getId).collect(Collectors.toList());
                List<TRechargeRecord> tRechargeRecords = tRechargeRecordMapper.selectList(new EntityWrapper<TRechargeRecord>()
                        .eq("agentId", tAgent.getId())
                        .eq("type", UserTypeEnum.AGENT.getCode())
                        .eq("payStatus", PayStatusEnum.FINISH.getCode()));
                tRechargeRecords.stream().map(TRechargeRecord::getAmount).reduce(BigDecimal::add).ifPresent(tAgentResp::setDriverRecharge);
            }
 
            tAgentRespList.add(tAgentResp);
        }
        return tAgentRespList;
    }
 
    @Override
    public void getDataStatistics(Integer agentId, Model model,Map<String, Object> map) {
        // 代理商总数
        Integer agentTotal = tAgentMapper.selectCount(new EntityWrapper<TAgent>()
                .eq("id", agentId));
        // 代理商最近一个月新增数量
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime localDate = now.minusMonths(1);
        Integer agentMonthTotal = tAgentMapper.selectCount(new EntityWrapper<TAgent>()
                .eq("id", agentId)
                .between("createTime", localDate, now));
        model.addAttribute("agentTotal",agentTotal);
        model.addAttribute("agentMonthTotal",agentMonthTotal);
        map.put("agentTotal",agentTotal);
        map.put("agentMonthTotal",agentMonthTotal);
    }
 
    @Override
    public void getDataStatisticsByIds(List<Integer> ids, Model model, Map<String, Object> map) {
        // 代理商总数
        Integer agentTotal = tAgentMapper.selectCount(new EntityWrapper<TAgent>()
                .in("id", ids));
        // 代理商最近一个月新增数量
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime localDate = now.minusMonths(1);
        Integer agentMonthTotal = tAgentMapper.selectCount(new EntityWrapper<TAgent>()
                .in("id", ids)
                .between("createTime", localDate, now));
        model.addAttribute("agentTotal",agentTotal);
        model.addAttribute("agentMonthTotal",agentMonthTotal);
        map.put("agentTotal",agentTotal);
        map.put("agentMonthTotal",agentMonthTotal);
    }
}