xiaoyong931011
2021-08-18 83cf3aeb3a5591f49b010696c8e5d7cca674d9fd
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
package com.xzx.gc.shop.service;
 
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xzx.gc.common.exception.RestException;
import com.xzx.gc.common.utils.StringUtils;
import com.xzx.gc.entity.*;
import com.xzx.gc.shop.dto.AssignJhyOrderDto;
import com.xzx.gc.shop.dto.CancelJhyOrderDto;
import com.xzx.gc.shop.dto.QueryJhyOrderListDto;
import com.xzx.gc.shop.mapper.JhyOrderItemsMapper;
import com.xzx.gc.shop.mapper.JhyOrderMapper;
import com.xzx.gc.shop.mapper.OrderInfoMapper;
import com.xzx.gc.shop.mapper.ScoreOrderMapper;
import com.xzx.gc.shop.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Service
@Transactional
@Slf4j
public class JhyService {
 
    @Resource
    JhyOrderMapper jhyOrderMapper;
    @Resource
    JhyOrderItemsMapper jhyOrderItemsMapper;
 
    @Autowired
    private OrderInfoMapper orderInfoMapper;
 
    @Autowired
    private ScoreOrderMapper scoreOrderMapper;
 
    public Map<String, Object> queryOrderList(QueryJhyOrderListDto model) {
        PageHelper.startPage(model.getPage(), model.getLimit());
        String accountPhone = model.getAccount();
//        String name = StrUtil.isEmpty(model.getAccount())?model.getAccount():StringUtils.encode(model.getAccount());
        String name = model.getAccount();
        model.setAccount(name);
        model.setAccountPhone(accountPhone);
        List<Integer> status = new ArrayList<>();
        if(CollUtil.isEmpty(model.getStatus())){
            status.add(1);
            status.add(2);
            status.add(3);
            status.add(4);
            status.add(5);
            status.add(6);
            model.setStatus(status);
        }
 
        List<QueryJhyOrderListVo> maps = jhyOrderMapper.queryOrderList(model);
        if(CollUtil.isNotEmpty(maps)){
            for(QueryJhyOrderListVo queryJhyOrderListVo : maps){
                Long id = queryJhyOrderListVo.getId();
                List<JhyOrderItemsVo> jhyOrderItemsVos = jhyOrderItemsMapper.selectByOrderId(id);
                queryJhyOrderListVo.setJhyOrderItemsVos(jhyOrderItemsVos);
            }
        }
//        if(CollUtil.isNotEmpty(maps)){
//            for(QueryJhyOrderListVo queryJhyOrderListVo : maps){
//                String decode = StringUtils.decode(queryJhyOrderListVo.getName());
//                queryJhyOrderListVo.setName(decode);
//            }
//        }
        PageInfo pageInfo = new PageInfo(maps);
        int count = Convert.toInt(pageInfo.getTotal());
        Map<String, Object> map = new HashMap<>();
        map.put("data", maps);
        map.put("count", count);
        map.put("code", 0);
        return map;
    }
 
    public ViewJhyOrderVo viewJhyOrder(Long id) {
        ViewJhyOrderVo viewJhyOrderVo = new ViewJhyOrderVo();
        viewJhyOrderVo = jhyOrderMapper.viewJhyOrder(id);
 
        String decode = StringUtils.decode(viewJhyOrderVo.getNickName());
        viewJhyOrderVo.setNickName(decode);
 
        List<JhyOrderItemsVo> jhyOrderItemsList = jhyOrderItemsMapper.selectJhyOrderItemListByOrderId(id);
        if(CollUtil.isNotEmpty(jhyOrderItemsList)){
            for(JhyOrderItemsVo jhyOrderItemsVo : jhyOrderItemsList){
                BigDecimal multiply = jhyOrderItemsVo.getPrice().multiply(new BigDecimal(40));
                jhyOrderItemsVo.setPrice(multiply);
                jhyOrderItemsVo.setAllPrice(new BigDecimal(StrUtil.isEmpty(jhyOrderItemsVo.getScore())?"0":jhyOrderItemsVo.getScore()).setScale( 2, BigDecimal.ROUND_DOWN ));
            }
        }
        viewJhyOrderVo.setJhyOrderItems(jhyOrderItemsList);
 
        return viewJhyOrderVo;
    }
 
    public void cancelJhyOrder(CancelJhyOrderDto model) {
        Long id = model.getId();
        JhyOrder jhyOrder = jhyOrderMapper.selectByPrimaryKey(id);
        jhyOrder.setStatus(JhyOrder.ORDER_STATUS_CANCEL);
        jhyOrder.setJhyId("");
        jhyOrderMapper.updateByPrimaryKey(jhyOrder);
    }
 
    public StatisticsVo orderStatistics(String userId) {
        // 骑手订单
        List<Map<String, Object>> qsAllData = orderInfoMapper.selectOrderTitleStatistics(null);
        List<Map<String, Object>> qsUserData = orderInfoMapper.selectOrderTitleStatistics(userId);
 
        // 集物员订单
        List<Map<String, Object>> jhyAllData = jhyOrderMapper.selectJhyOrderTitleStatistics(null);
        List<Map<String, Object>> jhyUserData = jhyOrderMapper.selectJhyOrderTitleStatistics(userId);
 
        BigDecimal allCarbon = totalData(qsAllData).add(totalData(jhyAllData));
        BigDecimal userCarbon = totalData(qsUserData).add(totalData(jhyUserData));
 
        Integer qsAllCnt = orderInfoMapper.selectOrderCnt(null);
        Integer qsUserCnt = orderInfoMapper.selectOrderCnt(userId);
 
        Integer jhyAllCnt = jhyOrderMapper.selectJhyOrderCnt(null);
        Integer jhyUserCnt = jhyOrderMapper.selectJhyOrderCnt(userId);
 
        Integer allCnt = scoreOrderMapper.selectScoreOrderCnt(null);
        BigDecimal score = jhyOrderMapper.selectTotalScoreHasGet(userId);
 
        StatisticsVo result = new StatisticsVo();
        result.setTotalReduceCarbon(allCarbon.divide(BigDecimal.valueOf(1000), 2, BigDecimal.ROUND_DOWN));
        result.setReduceCarbon(userCarbon.setScale(2, BigDecimal.ROUND_DOWN));
 
        result.setOrderCnt(qsUserCnt + jhyUserCnt);
        result.setTotalOrderCnt(qsAllCnt + jhyAllCnt);
 
        result.setTotalBuyCnt(allCnt);
        result.setScore(score);
        return result;
    }
 
    private BigDecimal totalData(List<Map<String, Object>> data) {
        Map<String, BigDecimal> type = new HashMap<>();
        type.put("金属", BigDecimal.valueOf(0.02));
        type.put("纸类", BigDecimal.valueOf(1.5));
        type.put("塑料", BigDecimal.valueOf(0.61));
        type.put("衣服", BigDecimal.valueOf(1.63));
 
        if (CollUtil.isEmpty(data)) {
            return BigDecimal.ZERO;
        }
 
        BigDecimal total = BigDecimal.ZERO;
        for (Map<String, Object> map : data) {
            String title = (String) map.get("title");
            BigDecimal value = type.get(title);
            if (value == null) {
                continue;
            }
 
            BigDecimal weight = new BigDecimal(map.get("weight").toString());
            total = total.add(weight.multiply(value));
        }
 
        return total;
    }
 
    public void assignJhyOrder(AssignJhyOrderDto model,String userId) {
        Long orderId = model.getOrderId();
        JhyOrder order = jhyOrderMapper.selectByPrimaryKey(orderId);
        order.setJhyId(userId);
        order.setStatus(JhyOrder.ORDER_STATUS_IMG);
        jhyOrderMapper.updateByPrimaryKey(order);
    }
}