doum
14 小时以前 0a27546e1491fcaee616683349218d0c35805947
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package com.doumee.service.business.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.doumee.biz.system.SystemDictDataBiz;
import com.doumee.config.rocketmq5.NormalMsgProducer;
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.model.LoginUserInfo;
import com.doumee.core.model.PageData;
import com.doumee.core.model.PageWrap;
import com.doumee.core.utils.Constants;
import com.doumee.core.utils.DateUtil;
import com.doumee.core.utils.Utils;
import com.doumee.dao.business.*;
import com.doumee.dao.business.model.*;
import com.doumee.dao.system.SystemUserMapper;
import com.doumee.dao.system.dto.CountDataDTO;
import com.doumee.dao.system.model.SystemUser;
import com.doumee.dao.system.vo.BusinessDataCountVO;
import com.doumee.dao.system.vo.CountDataVO;
import com.doumee.dao.system.vo.NumberDataCountVO;
import com.doumee.dao.web.dto.CollectDTO;
import com.doumee.dao.web.dto.MemberDTO;
import com.doumee.dao.web.dto.MultiFileDTO;
import com.doumee.dao.web.dto.OrgDTO;
import com.doumee.dao.web.dto.activity.*;
import com.doumee.dao.web.dto.shop.ShopSimpleDTO;
import com.doumee.dao.web.request.ActivityPageListRequest;
import com.doumee.service.business.*;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * 活动、资讯、探店信息表Service实现
 *
 * @author 江蹄蹄
 * @date 2023/03/21 15:48
 */
@Service
public class WorkbenchServiceImpl implements WorkbenchService {
    @Autowired
    private GoodsorderMapper goodsorderMapper;
    @Autowired
    private MemberCouponMapper memberCouponMapper;
    @Autowired
    private InviteRecordMapper inviteRecordMapper;
    @Autowired
    private IntegralMapper integralMapper;
    @Autowired
    private GoodsorderDetailMapper goodsorderDetailMapper;
    @Override
    public List<BusinessDataCountVO> shopRankList10(CountDataDTO param) {
        Date startDate = null,endDate = null;
        if(param.getYear()!=null && param.getMonth()!=null){
            List<Date> dList = DateUtil.getDatePeriodByYearAndMonth(param.getYear(),param.getMonth()-1);
            startDate = dList.get(0);
            endDate = dList.get(1);
        }else if(param.getYear()!=null && param.getMonth()==null){
            List<Date> dList = DateUtil.getDatePeriodByYear(param.getYear());
            startDate = dList.get(0);
            endDate = dList.get(1);
        }
        //统计订单销量和销售额集合
        List<BusinessDataCountVO> list1 =  goodsorderMapper.selectJoinList(BusinessDataCountVO.class,new MPJLambdaWrapper<Goodsorder>()
                .selectAs(Shop::getName,BusinessDataCountVO::getName)
                .select("(select count(distinct(t.member_id)) )",BusinessDataCountVO::getNum1)
                .select("(select count(t.id) )",BusinessDataCountVO::getNum)
                .select("(select sum(t.total_price) )",BusinessDataCountVO::getPrice)
                .leftJoin(Shop.class,Shop::getId,Goodsorder::getDistributionShopId)
                .ge(startDate!=null,Goodsorder::getCreateDate,startDate)
                .ne(endDate!=null,Goodsorder::getCreateDate,endDate)
                .eq(Goodsorder::getIsdeleted,Constants.ZERO)
                .in(Goodsorder::getStatus,Constants.OrderStatus.DONE.getKey(),Constants.OrderStatus.REFUND.getKey())
                .groupBy(Goodsorder::getDistributionShopId)
                .orderByDesc(Constants.equalsInteger(param.getType() ,0),"sum(t.total_price) ")
                .orderByDesc(Constants.equalsInteger(param.getType() ,1),"count(t.id) ")
                .orderByDesc(Constants.equalsInteger(param.getType() ,2),"count(distinct(t.member_id))")
                .last("limit 10")) ;
        return list1;
 
    }
    @Override
    public List<BusinessDataCountVO> goodsRankList10(CountDataDTO param) {
        Date startDate = null,endDate = null;
        if(param.getYear()!=null && param.getMonth()!=null){
            List<Date> dList = DateUtil.getDatePeriodByYearAndMonth(param.getYear(),param.getMonth()-1);
            startDate = dList.get(0);
            endDate = dList.get(1);
        }else if(param.getYear()!=null && param.getMonth()==null){
            List<Date> dList = DateUtil.getDatePeriodByYear(param.getYear());
            startDate = dList.get(0);
            endDate = dList.get(1);
        }
        //统计订单销量和销售额集合
        List<BusinessDataCountVO> list1 =  goodsorderDetailMapper.selectJoinList(BusinessDataCountVO.class,new MPJLambdaWrapper<GoodsorderDetail>()
                .selectAs(Goods::getName,BusinessDataCountVO::getName)
                .select("(select sum(t.goods_num) )",BusinessDataCountVO::getNum)
                .select("(select  sum(ifnull(t.price,0) * ifnull(t.goods_num,0)))",BusinessDataCountVO::getPrice)
                .leftJoin(Goodsorder.class,Goodsorder::getId,GoodsorderDetail::getOrderId)
                .leftJoin(Goods.class,Goods::getId,GoodsorderDetail::getGoodsId)
                .ge(startDate!=null,Goodsorder::getCreateDate,startDate)
                .ne(endDate!=null,Goodsorder::getCreateDate,endDate)
                .eq(Goodsorder::getIsdeleted,Constants.ZERO)
                .in(Goodsorder::getStatus,Constants.OrderStatus.DONE.getKey(),Constants.OrderStatus.REFUND.getKey())
                .groupBy(GoodsorderDetail::getGoodsId)
                .orderByDesc(Constants.equalsInteger(param.getType() ,1),"sum(ifnull(t.price,0) * ifnull(t.goods_num,0)) ")
                .orderByDesc(Constants.equalsInteger(param.getType() ,0),"sum(t.goods_num) ")
                .last("limit 10")) ;
        return list1;
    }
    @Override
    public    NumberDataCountVO couponData(CountDataDTO param){
        NumberDataCountVO data = new NumberDataCountVO();
        //发放类型统计
        List<MemberCoupon> r1 =  memberCouponMapper.selectList(new MPJLambdaWrapper<MemberCoupon>()
                .select("(select count(t.id) )",MemberCoupon::getNum)
                .select( MemberCoupon::getCouponType)
                .apply(param.getYear()!=null,"YEAR ( t.CREATE_DATE )="+param.getYear() )
                .eq(MemberCoupon::getIsdeleted,Constants.ZERO)
                .groupBy(MemberCoupon::getCouponType));
        if(r1!=null){
            for(MemberCoupon c : r1){
                if(Constants.equalsInteger(c.getCouponType(),Constants.ZERO)){
                    data.setNum(Constants.formatLongNum(c.getNum()));//满减券
                }else{
                    data.setNum1(Constants.formatLongNum(c.getNum()));//满减券
                }
            }
        }
        //已使用
        Long r2 =  memberCouponMapper.selectCount(new MPJLambdaWrapper<MemberCoupon>()
                .apply(param.getYear()!=null,"YEAR ( t.use_Date )="+param.getYear() )
                .eq(MemberCoupon::getIsdeleted,Constants.ZERO));
        data.setUseNum(Constants.formatLongNum(r2));
        //优惠金额和交易金额
        Goodsorder r3 =  goodsorderMapper.selectOne(new MPJLambdaWrapper<Goodsorder>()
                .select("(select sum(t.total_price) )",Goodsorder::getTotalPrice)
                .select("(select sum(t.coupon_price) )",Goodsorder::getCouponPrice)
                .apply(param.getYear()!=null,"YEAR ( t.CREATE_DATE )="+param.getYear() )
                .eq(Goodsorder::getIsdeleted,Constants.ZERO)
                .in(Goodsorder::getStatus,Constants.OrderStatus.DONE.getKey(),Constants.OrderStatus.REFUND.getKey()) );
        if(r3!=null){
            data.setPrice(Constants.formatBigdecimal(r3.getCouponPrice()));
            data.setOrderPrice(Constants.formatBigdecimal(r3.getTotalPrice()));
        }
        return  data;
 
    }
    @Override
    public    NumberDataCountVO integralData(CountDataDTO param){
        NumberDataCountVO data = new NumberDataCountVO();
        //发放积分类型统计
        List<Integral> r1 =  integralMapper.selectList(new MPJLambdaWrapper<Integral>()
                .select("(select sum(t.num) )",Integral::getNum)
                .select( Integral::getObjType)
                .apply(param.getYear()!=null,"YEAR ( t.CREATE_DATE )="+param.getYear() )
                .eq(Integral::getIsdeleted,Constants.ZERO)
                .eq(Integral::getType,Constants.ZERO)
                .in(Integral::getObjType,Constants.IntegralObjType.REGISTER.getKey(),
                        Constants.IntegralObjType.INVITENEWUSER.getKey(),
                        Constants.IntegralObjType.ORDER_DONATE.getKey(),
                        Constants.IntegralObjType.SYSTEM_RECHARGE.getKey())
                .groupBy(Integral::getObjType));
        if(r1!=null){
            for(Integral c : r1){
                if(Constants.equalsInteger(c.getObjType(),Constants.IntegralObjType.SYSTEM_RECHARGE.getKey())){
                    data.setNum(Constants.formatBigdecimal(c.getNum()).longValue());//平台添加
                }else if(Constants.equalsInteger(c.getObjType(),Constants.IntegralObjType.INVITENEWUSER.getKey())){
                    data.setNum1(Constants.formatBigdecimal(c.getNum()).longValue());//邀请
                }else if(Constants.equalsInteger(c.getObjType(),Constants.IntegralObjType.ORDER_DONATE.getKey())){
                    data.setNum2(Constants.formatBigdecimal(c.getNum()).longValue());//消费返利
                }else if(Constants.equalsInteger(c.getObjType(),Constants.IntegralObjType.REGISTER.getKey())){
                    data.setNum3(Constants.formatBigdecimal(c.getNum()).longValue());//注册
                }
            }
        }
        //优惠金额和交易金额
        Goodsorder r3 =  goodsorderMapper.selectOne(new MPJLambdaWrapper<Goodsorder>()
                .select("(select sum(t.integral) )",Goodsorder::getIntegral)
                .select("(select sum(t.use_integral) )",Goodsorder::getUseIntegral)
                .apply(param.getYear()!=null,"YEAR ( t.CREATE_DATE )="+param.getYear() )
                .eq(Goodsorder::getIsdeleted,Constants.ZERO)
                .in(Goodsorder::getStatus,Constants.OrderStatus.DONE.getKey(),Constants.OrderStatus.REFUND.getKey()) );
        if(r3!=null){
            data.setUseNum(Constants.formatBigdecimal(r3.getUseIntegral()).longValue());
            data.setPrice(Constants.formatBigdecimal(r3.getIntegral()));
        }
        return  data;
 
    }
    @Override
    public BusinessDataCountVO businessData(CountDataDTO param) {
        BusinessDataCountVO data = new BusinessDataCountVO();
        int dataType = param.getDateType();//0今日 1近七日 2近30日 3近1年
        String name = "HOUR(t.CREATE_DATE)";
        List<String> dateList = new ArrayList<>();
        Date date = Utils.Date.getEnd(new Date());//今日 23:59:59
        Date startDate = DateUtil.addDaysToDate(date,-1);
        switch (dataType) {
            case 1: {
                name = "DATE_FORMAT(t.CREATE_DATE, '%Y-%m-%d')";
                for(int i=1;i<=7;i++){
                    dateList.add(DateUtil.getShortTime(DateUtil.addDaysToDate(date,i-7)));
                }
                startDate = DateUtil.addDaysToDate(date,-7);
                break;
            } case 2: {
                name = "DATE_FORMAT(t.CREATE_DATE, '%Y-%m-%d')";
                for(int i=1;i<=30;i++){
                    dateList.add(DateUtil.getShortTime(DateUtil.addDaysToDate(date,i-30)));
                }
                startDate = DateUtil.addDaysToDate(date,-30);
                break;
            } case 3: {
                LocalDate now = LocalDate.now();
                startDate = DateUtil.increaseYear(date,-1);
                name = "DATE_FORMAT(t.CREATE_DATE, '%Y-%m')";
                for(int i=1;i<=12;i++){
                    dateList.add(DateUtil.minusMonthToDate(now,12-i));
                }
                break;
            } default:{
                for(int i=0;i<=23;i++){
                    dateList.add(i+"");
                }
                break;
            }
        }
 
        data.setDateStrList(dateList);
        //订单销量
        CountDataVO r =  goodsorderMapper.selectJoinOne(CountDataVO.class,new MPJLambdaWrapper<Goodsorder>()
                        .select("(select count(t.id) )",CountDataVO::getNum)
                        .select("(select sum(t.total_Price) )",CountDataVO::getPrice)
                .ge(Goodsorder::getCreateDate,startDate)
                .eq(Goodsorder::getIsdeleted,Constants.ZERO)
                .in(Goodsorder::getStatus,Constants.OrderStatus.DONE.getKey(),Constants.OrderStatus.REFUND.getKey()) );
        if (r != null) {
            data.setNum(Constants.formatLongNum(r.getNum()));//订单量
            data.setPrice(Constants.formatBigdecimal(r.getPrice()));//订单金额
        }
        //退款统计
        CountDataVO r1 =  goodsorderMapper.selectJoinOne(CountDataVO.class,new MPJLambdaWrapper<Goodsorder>()
                        .select("(select count(t.id) )",CountDataVO::getNum)
                        .select("(select sum(t.refund_money) )",CountDataVO::getPrice)
                .ge(Goodsorder::getRefundTime,startDate)
                .eq(Goodsorder::getIsdeleted,Constants.ZERO)
                .in(Goodsorder::getStatus,Constants.OrderStatus.REFUND.getKey())  );
 
 
        if (r1 != null) {
            data.setNum1(Constants.formatLongNum(r1.getNum()));//退款单量
            data.setPrice1(Constants.formatBigdecimal(r1.getPrice()));//退款金额
        }
 
        //统计订单销量和销售额集合
        List<CountDataVO> list1 =  goodsorderMapper.selectJoinList(CountDataVO.class,new MPJLambdaWrapper<Goodsorder>()
                .select("(select "+name+")",CountDataVO::getDateStr)
                .select("(select count(t.id) )",CountDataVO::getNum)
                .select("(select sum(t.total_price) )",CountDataVO::getPrice)
                .ge(Goodsorder::getCreateDate,startDate)
                .eq(Goodsorder::getIsdeleted,Constants.ZERO)
                .in(Goodsorder::getStatus,Constants.OrderStatus.DONE.getKey(),Constants.OrderStatus.REFUND.getKey())
                .groupBy(name) );
 
        List<Long> datalist1 = new ArrayList<>();
        List<BigDecimal> datalistPrice = new ArrayList<>();
        for(String str : dateList){
            long tnum =0;
            BigDecimal tprice = new BigDecimal(0);
            if(list1!=null){
                for(CountDataVO p : list1){
                    if(StringUtils.equals(p.getDateStr(),str)){
                        tnum =(Constants.formatLongNum(p.getNum()));
                        tprice = (Constants.formatBigdecimal(p.getPrice()));
                        break;
                    }
                }
            }
            datalist1.add( tnum );
            datalistPrice.add(tprice);
        }
        data.setDataList(datalist1 );
        data.setDataList2(datalistPrice );
 
        String name1 =name.replace("t.","t3.");
        //统计各类商品数据集合
        List<CountDataVO> list2 =  goodsorderDetailMapper.selectJoinList(CountDataVO.class,new MPJLambdaWrapper<GoodsorderDetail>()
                .select("(select "+name1+")",CountDataVO::getDateStr)
                .selectAs(Labels::getName,CountDataVO::getName)
                .select("(select sum(ifnull(t.price,0) * ifnull(t.goods_num,0)) )",CountDataVO::getPrice)
                .leftJoin(Goods.class,Goods::getId,GoodsorderDetail::getGoodsId)
                .leftJoin(Labels.class,Labels::getId,Goods::getCategoryId)
                .leftJoin(Goodsorder.class,Goodsorder::getId,GoodsorderDetail::getOrderId)
                .ge(Goodsorder::getCreateDate,startDate)
                .eq(Goodsorder::getIsdeleted,Constants.ZERO)
                .in(Goodsorder::getStatus,Constants.OrderStatus.DONE.getKey(),Constants.OrderStatus.REFUND.getKey())
                .groupBy(name1+",t1.category_id"));
 
        List<CountDataVO> cateList = getCateListFromDataList(list2);
        if(list2!=null &&list2.size()>0 && cateList!=null && cateList.size()>0){
            BigDecimal[] temp = new BigDecimal[dateList.size()];
            for(CountDataVO cate : cateList){
                for(int i=0;i<dateList.size();i++){
                    temp[i] = new BigDecimal(0);
                    for(CountDataVO model : list2){
                        if(StringUtils.equals(model.getDateStr(),dateList.get(i)) && StringUtils.equals(model.getName(),cate.getName())){
                            temp[i] = Constants.formatBigdecimal(model.getPrice());
                        }
                    }
                }
                cate.setData(temp);
            }
        }
        data.setCateList(cateList);
        return data;
    }
 
    private List<CountDataVO> getCateListFromDataList(List<CountDataVO> list2) {
        List<CountDataVO> list = new ArrayList<>();
        for(CountDataVO model :list2){
            boolean isnew = true;
            for(CountDataVO str :list){
                if(StringUtils.equals(str.getName(),model.getName())){
                    isnew = false;
                }
 
            }
            if(isnew ){
                list.add(model);
            }
        }
        return list;
    }
 
}