Helius
2021-06-28 b1b61f9bbd329b593ee019e3de98e4df14466af3
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
package com.xzx.gc.shop.service;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xzx.gc.common.utils.StringUtils;
import com.xzx.gc.common.constant.Constants;
import com.xzx.gc.common.exception.RestException;
import com.xzx.gc.common.utils.IdUtils;
import com.xzx.gc.entity.*;
import com.xzx.gc.shop.dto.*;
import com.xzx.gc.shop.mapper.*;
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.*;
 
@Service
@Transactional
@Slf4j
public class OrderService {
 
    @Resource
    ScoreOrderMapper scoreOrderMapper;
 
    @Resource
    ScoreExpressInfoMapper scoreExpressInfoMapper;
 
    @Resource
    ScoreOrderDetailsMapper scoreOrderDetailsMapper;
 
    @Resource
    private AddressInfoMapper addressInfoMapper;
 
    @Autowired
    private AccountInfoMapper accountInfoMapper;
 
    @Autowired
    private ScoreGoodsStyleMapper scoreGoodsStyleMapper;
 
    @Autowired
    private ScoreGoodsSkuMapper scoreGoodsSkuMapper;
 
    @Autowired
    private ScoreGoodsMapper scoreGoodsMapper;
    @Autowired
    private ScoreDetailsMapper scoreDetailsMapper;
 
    @Autowired
    private IdUtils idUtils;
 
    public Map<String, Object> queryOrderList(QueryOrderListDto model) {
        String name = model.getName();
        String orderNo = model.getOrderNo();
        Integer status = model.getStatus() == null ? 0 : model.getStatus();
        Date createdTimeStart = model.getCreatedTimeStart();
        Date createdTimeEnd = model.getCreatedTimeEnd();
        PageHelper.startPage(model.getPage(), model.getLimit());
        List<QueryOrderListVo> maps = scoreOrderMapper.queryOrderList(name,orderNo,status,createdTimeStart,createdTimeEnd);
        if(CollUtil.isNotEmpty(maps)){
            for(QueryOrderListVo queryOrderListVo : maps){
                String decode = StringUtils.decode(queryOrderListVo.getName());
                queryOrderListVo.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 ViewOrderVo viewOrder(Long id) {
        ViewOrderVo viewOrderVo = new ViewOrderVo();
        ScoreOrder scoreOrder = scoreOrderMapper.selectByPrimaryKey(id);
        if(ObjectUtil.isNotEmpty(scoreOrder)){
            /**
             * 获取基本信息
             * 获取物流信息
             * 获取商品订单详情
             */
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            viewOrderVo = objectMapper.convertValue(scoreOrder, ViewOrderVo.class);
//            String voucherImg = viewOrderVo.getVoucherImg();
            String voucherImg = scoreOrder.getVoucherImg();
            List<String> lists = StrUtil.splitTrim(voucherImg, ",");
            viewOrderVo.setVoucherImgs(lists);
            //2-待收货3-已收货4-已完成5-已评价,获取物流信息
            Integer status = scoreOrder.getStatus() == null ? 0:scoreOrder.getStatus();
//            if(ScoreOrder.STATUS_DOING == status
//                    || ScoreOrder.STATUS_DONE == status
//                    || ScoreOrder.STATUS_ON == status
//                    || ScoreOrder.STATUS_EVALUATE == status){
                Example exampleExpress = new Example(ScoreExpressInfo.class);
                Example.Criteria criteriaExpress = exampleExpress.createCriteria();
                criteriaExpress.andEqualTo("orderId",id);
                ScoreExpressInfo scoreExpressInfo = scoreExpressInfoMapper.selectOneByExample(exampleExpress);
                ExpressInfoVo expressInfoVo = objectMapper.convertValue(scoreExpressInfo, ExpressInfoVo.class);
                viewOrderVo.setExpressInfoVo(expressInfoVo);
//            }
            //订单详情
            Example exampleDetails = new Example(ScoreOrderDetails.class);
            Example.Criteria criteriaDetails = exampleDetails.createCriteria();
            criteriaDetails.andEqualTo("orderId",id);
            List<ScoreOrderDetails> scoreOrderDetails = scoreOrderDetailsMapper.selectByExample(exampleDetails);
            if(CollUtil.isNotEmpty(scoreOrderDetails)){
                viewOrderVo.setScoreOrderDetails(scoreOrderDetails);
            }
 
        }
        return viewOrderVo;
    }
 
    public Long deliverGoods(DeliverGoodsDto model) {
        /**
         * 获取物流表的信息
         * 更新物流信息
         * 更新订单状态
         */
        Long id = model.getId();
        Example exampleExpress = new Example(ScoreExpressInfo.class);
        Example.Criteria criteriaExpress = exampleExpress.createCriteria();
        criteriaExpress.andEqualTo("orderId",id);
        ScoreExpressInfo scoreExpressInfo = scoreExpressInfoMapper.selectOneByExample(exampleExpress);
        if(ObjectUtil.isNotEmpty(scoreExpressInfo)){
            scoreExpressInfo.setExpressNo(model.getExpressNo());
            scoreExpressInfo.setExpressCom(model.getExpressCom());
            scoreExpressInfoMapper.updateByPrimaryKeySelective(scoreExpressInfo);
 
            ScoreOrder scoreOrder = new ScoreOrder();
            scoreOrder.setId(id);
            scoreOrder.setStatus(ScoreOrder.STATUS_ON);
            scoreOrderMapper.updateByPrimaryKeySelective(scoreOrder);
        }
        return id;
    }
 
    public Long insureOrder(InsureOrderDto model) {
        ScoreOrder scoreOrder = new ScoreOrder();
        scoreOrder.setId(model.getId());
        scoreOrder.setStatus(ScoreOrder.STATUS_DONE);
        scoreOrder.setVoucherImg(CollUtil.join(model.getVoucherImgs(),","));
        scoreOrderMapper.updateByPrimaryKeySelective(scoreOrder);
        return scoreOrder.getId();
    }
 
    public void addOrder(AddGoodsOrderDto addGoodsOrderDto) {
        ScoreGoodsStyle style = scoreGoodsStyleMapper.selectByPrimaryKey(addGoodsOrderDto.getStyleId());
        ScoreGoodsSku sku = scoreGoodsSkuMapper.selectByPrimaryKey(addGoodsOrderDto.getSkuId());
        ScoreGoods goods = scoreGoodsMapper.selectByPrimaryKey(style.getGoodsId());
        if (goods == null || ScoreGoods.ISSALE_NO.equals(goods.getIsSale())) {
            throw new RestException(-3, "商品不存在或已下架");
        }
 
        if (ScoreGoods.ISQG_YES.equals(goods.getIsQg())) {
            if (new Date().before(goods.getQgStartTime())) {
                throw new RestException(-3, "抢购未开始");
            }
 
            if (new Date().after(goods.getQgEndTime())) {
                throw new RestException(-3, "抢购已结束");
            }
        }
 
        AccountInfo accountInfo = accountInfoMapper.selectAccountInfoByUserId(addGoodsOrderDto.getUserId());
 
        if (accountInfo.getCollectScore() == null) {
            throw new RestException(-3, "积分不足");
        }
 
        BigDecimal totalPrice = sku.getPresentPrice().multiply(BigDecimal.valueOf(addGoodsOrderDto.getCnt()));
        BigDecimal score = new BigDecimal(accountInfo.getCollectScore());
        if (score.compareTo(totalPrice) < 0) {
            throw new RestException(-3, "积分不足");
        }
 
        ScoreOrder order = new ScoreOrder();
        order.setOrderNo(idUtils.generate("JF", 9));
        order.setCnt(addGoodsOrderDto.getCnt());
        order.setTotalPrice(totalPrice);
        order.setCreatedTime(new Date());
        order.setUserId(addGoodsOrderDto.getUserId());
        order.setStatus(ScoreOrder.STATUS_READY);
        order.setGoodsName(goods.getName());
        order.setRemark(addGoodsOrderDto.getRemark());
        scoreOrderMapper.insert(order);
 
        ScoreOrderDetails orderDetails = new ScoreOrderDetails();
        orderDetails.setGoodsName(goods.getName());
        orderDetails.setStyle(style.getName());
        orderDetails.setSku(sku.getName());
        orderDetails.setOrderId(order.getId());
        orderDetails.setCnt(addGoodsOrderDto.getCnt());
        orderDetails.setTotalPrice(totalPrice);
        orderDetails.setUnitPrice(sku.getPresentPrice());
        orderDetails.setGoodsId(goods.getId());
        orderDetails.setThumb(goods.getThumb());
        scoreOrderDetailsMapper.insert(orderDetails);
 
        AddressInfo addressInfo = addressInfoMapper.selectByPrimaryKey(addGoodsOrderDto.getAddressId());
        ScoreExpressInfo expressInfo = new ScoreExpressInfo();
        expressInfo.setUsername(addressInfo.getRelaName());
        expressInfo.setPhone(addressInfo.getMobilePhone());
        expressInfo.setUserId(addressInfo.getUserId());
        expressInfo.setOrderId(order.getId());
        StringBuffer address = new StringBuffer();
        address.append(addressInfo.getAddressArea());
        address.append(addressInfo.getDetailAddress());
        if (StrUtil.isNotBlank(addressInfo.getTagName())) {
            address.append(StrUtil.isNotBlank(addressInfo.getHouseName()) ? addressInfo.getHouseName() : "");
            if (Constants.ADDRESS_TYPE_HOME.equals(addressInfo.getTagName())) {
                address.append(StrUtil.isNotBlank(addressInfo.getHouseNumber()) ? addressInfo.getHouseNumber() : "");
                address.append(StrUtil.isNotBlank(addressInfo.getUnitName()) ? addressInfo.getUnitName() : "");
            }
        }
        expressInfo.setAddress(address.toString());
        expressInfo.setCreatedTime(new Date());
        scoreExpressInfoMapper.insert(expressInfo);
 
        BigDecimal remianScore = score.subtract(totalPrice).setScale(0, BigDecimal.ROUND_DOWN);
        accountInfo.setCollectScore(remianScore.toString());
        accountInfoMapper.updateByPrimaryKey(accountInfo);
 
        ScoreDetails scoreDetails = new ScoreDetails();
        scoreDetails.setOrderNo(order.getOrderNo());
        scoreDetails.setUserId(addGoodsOrderDto.getUserId());
        scoreDetails.setType(ScoreDetails.SCORE_TYPE_SHOPPING);
        scoreDetails.setOriginalScore(score);
        scoreDetails.setCurrentScore(remianScore);
        scoreDetails.setChangeScore(totalPrice);
        scoreDetails.setCreatedTime(new Date());
        scoreDetailsMapper.insert(scoreDetails);
 
        sku.setStock(sku.getStock() - addGoodsOrderDto.getCnt());
        sku.setQuantity(sku.getQuantity() - addGoodsOrderDto.getCnt());
        scoreGoodsSkuMapper.updateByPrimaryKey(sku);
    }
 
    public Long cancelOrder(CancelOrderDto model) {
        /**
         * todo 只更新了订单状态,后续操作待增加
         */
        ScoreOrder scoreOrder = new ScoreOrder();
        scoreOrder.setId(model.getId());
        scoreOrder.setStatus(ScoreOrder.STATUS_CANCEL);
        scoreOrderMapper.updateByPrimaryKeySelective(scoreOrder);
        return scoreOrder.getId();
    }
 
    public PageInfo<XcxOrderListVo> orderList(XcxOrderListDto xcxOrderListDto) {
        PageHelper.startPage(xcxOrderListDto.getPageNo(), xcxOrderListDto.getPageSize());
        List<XcxOrderListVo> data = scoreOrderMapper.selectXcxOrderList(xcxOrderListDto);
        return new PageInfo<>(data);
    }
 
    public XcxOrderDetailsVo orderDetails(Long id) {
        return scoreOrderMapper.selectXcxOrderDetails(id);
    }
 
    public void confirmOrder(Long id, String userId) {
        ScoreOrder order = scoreOrderMapper.selectByPrimaryKey(id);
        if (order == null) {
            throw new RestException(-3, "订单不存在");
        }
 
        if (!order.getUserId().equals(userId)) {
            throw new RestException(-3, "订单不存在");
        }
 
        if (!ScoreOrder.STATUS_ON.equals(order.getStatus())) {
            throw new RestException(-3, "暂不能确认收货");
        }
 
        scoreOrderMapper.updateOrderStatus(id, ScoreOrder.STATUS_DOING, userId);
    }
 
    public void cancelOrder(Long id, String userId) {
        ScoreOrder order = scoreOrderMapper.selectByPrimaryKey(id);
        if (order == null) {
            throw new RestException(-3, "订单不存在");
        }
 
        if (!order.getUserId().equals(userId)) {
            throw new RestException(-3, "订单不存在");
        }
 
        if (!ScoreOrder.STATUS_READY.equals(order.getStatus())) {
            throw new RestException(-3, "订单不能取消");
        }
 
        scoreOrderMapper.updateOrderStatus(id, ScoreOrder.STATUS_CANCEL, userId);
 
 
        // 退积分
        AccountInfo accountInfo = accountInfoMapper.selectAccountInfoByUserId(userId);
        BigDecimal score = new BigDecimal(accountInfo.getCollectScore()).add(order.getTotalPrice()).setScale(0, BigDecimal.ROUND_DOWN);
 
        ScoreDetails scoreDetails = new ScoreDetails();
        scoreDetails.setOriginalScore(new BigDecimal(accountInfo.getCollectScore()));
        scoreDetails.setCurrentScore(score);
        scoreDetails.setChangeScore(order.getTotalPrice());
        scoreDetails.setOrderNo(order.getOrderNo());
        scoreDetails.setType(ScoreDetails.SCORE_TYPE_SHOPPING_RETURN);
        scoreDetails.setCreatedTime(new Date());
        scoreDetails.setUserId(userId);
        scoreDetailsMapper.insert(scoreDetails);
 
        accountInfo.setCollectScore(score.toString());
        accountInfoMapper.updateByPrimaryKey(accountInfo);
 
 
 
        // 减销量 加库存
        List<ScoreOrderDetails> details = scoreOrderDetailsMapper.selectOrderDetailsByOrderId(order.getId());
        if (CollUtil.isNotEmpty(details)) {
            for (ScoreOrderDetails detail : details) {
                ScoreGoodsSku sku = scoreGoodsSkuMapper.selectByPrimaryKey(detail.getSkuId());
                if (sku == null) {
                    continue;
                }
 
                sku.setQuantity(sku.getQuantity() - detail.getCnt());
                sku.setStock(sku.getStock() + detail.getCnt());
                scoreGoodsSkuMapper.updateByPrimaryKey(sku);
            }
        }
    }
}