1.
phpcjl
2024-12-10 1690d4d06b0c20b3606e50e0844ef67e07947a0d
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
package com.ruoyi.order.service.impl;
 
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.account.api.feignClient.AppUserClient;
import com.ruoyi.account.api.model.AppUserShop;
import com.ruoyi.account.api.model.UserAddress;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.order.enums.OrderStatus;
import com.ruoyi.order.enums.OrderType;
import com.ruoyi.order.mapper.OrderGoodMapper;
import com.ruoyi.order.mapper.OrderMapper;
import com.ruoyi.order.model.Order;
import com.ruoyi.order.model.OrderGood;
import com.ruoyi.order.service.CommissionService;
import com.ruoyi.order.service.OrderService;
import com.ruoyi.order.vo.OrderDetailVO;
import com.ruoyi.order.vo.OrderGoodsVO;
import com.ruoyi.order.vo.OrderVO;
import com.ruoyi.other.api.domain.*;
import com.ruoyi.other.api.feignClient.BaseSettingClient;
import com.ruoyi.other.api.feignClient.ShopClient;
import com.ruoyi.other.api.feignClient.TechnicianClient;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author luodangjia
 * @since 2024-11-21
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private OrderGoodMapper orderGoodMapper;
    @Resource
    private AppUserClient appUserClient;
    @Resource
    private TokenService tokenService;
    @Resource
    private TechnicianClient technicianClient;
    @Resource
    private ShopClient shopClient;
    @Resource
    private CommissionService commissionService;
    @Resource
    private BaseSettingClient baseSettingClient;
 
 
    @Override
    public List<OrderVO> selectOrderListByUserId(Integer status, Long userId) {
        return orderMapper.selectOrderListByUserId(status, userId);
    }
 
 
 
 
    @Override
    public OrderDetailVO getOrderDetail(Long orderId) {
        Order order = orderMapper.selectById(orderId);
        if (order == null){
            throw new ServiceException("订单不存在");
        }
        R<Shop> shopR = shopClient.getShopById(order.getShopId());
        if (!R.isSuccess(shopR)){
            throw new ServiceException("获取门店信息失败");
        }
 
        // 商品
        List<OrderGood> orderGoods = orderGoodMapper.selectList(new LambdaQueryWrapper<OrderGood>()
                .eq(OrderGood::getOrderId, orderId));
 
        List<OrderGoodsVO> goodsList = new ArrayList<>();
        for (OrderGood orderGood : orderGoods) {
            String goodJson = orderGood.getGoodJson();
            Goods goods = JSONObject.parseObject(goodJson, Goods.class);
            OrderGoodsVO orderGoodsVO = new OrderGoodsVO();
            orderGoodsVO.setGoodsId(orderGood.getGoodsId());
            orderGoodsVO.setNum(orderGood.getNum());
            orderGoodsVO.setGoodsName(goods.getName());
            orderGoodsVO.setType(goods.getType());
            orderGoodsVO.setGoodsPic(goods.getHomePagePicture());
            orderGoodsVO.setSellingPrice(goods.getSellingPrice());
            orderGoodsVO.setOriginalPrice(goods.getOriginalPrice());
            goodsList.add(orderGoodsVO);
        }
 
 
 
        // 收货地址
        String addressJson = order.getAddressJson();
        UserAddress userAddress = new UserAddress();
        if (StringUtils.isNotEmpty(addressJson)){
            userAddress = JSONObject.parseObject(addressJson, UserAddress.class);
 
        }
 
        // 优惠券
        String couponJson = order.getCouponJson();
        CouponInfo couponInfo = new CouponInfo();
        if (StringUtils.isNotEmpty(couponJson)){
            couponInfo = JSONObject.parseObject(couponJson, CouponInfo.class);
        }
 
        // 参与活动
        String activityJson = order.getActivityJson();
        OrderActivityInfo orderActivityInfo = new OrderActivityInfo();
        if (StringUtils.isNotEmpty(activityJson)){
            orderActivityInfo = JSONObject.parseObject(activityJson, OrderActivityInfo.class);
        }
 
        OrderDetailVO orderDetailVO = new OrderDetailVO();
 
        if (CollectionUtil.isNotEmpty(orderGoods)){
            String goodJson = orderGoods.get(0).getGoodJson();
            Goods goods = JSONObject.parseObject(goodJson, Goods.class);
            orderDetailVO.setDistributionMode(goods.getDistributionMode());
        }
 
        Technician technician = new Technician();
        if (order.getTechnicianId() != null){
            R<Technician> shopdetail = technicianClient.shopdetail(order.getTechnicianId());
            if (shopdetail.getCode() != R.SUCCESS){
                throw new ServiceException("获取技师信息失败");
            }
            technician = shopdetail.getData();
        }
        Shop shop = shopR.getData();
        orderDetailVO.setId(order.getId());
        orderDetailVO.setOrderStatus(order.getOrderStatus());
        orderDetailVO.setPoint(order.getPoint());
        orderDetailVO.setAddressId(userAddress.getId());
        orderDetailVO.setRecieveName(userAddress.getRecieveName());
        orderDetailVO.setRecievePhone(userAddress.getRecievePhone());
        orderDetailVO.setRecieveAddress(userAddress.getRecieveAddress());
        orderDetailVO.setOrderNumber(order.getOrderNumber());
        orderDetailVO.setCreateTime(order.getCreateTime());
        orderDetailVO.setTotalAmount(order.getTotalAmount());
        orderDetailVO.setCouponName(couponInfo.getCouponName());
        orderDetailVO.setActivityName(orderActivityInfo.getActivityName());
        orderDetailVO.setCouponAmount(order.getDiscountTotalAmount());
        orderDetailVO.setExpressAmount(order.getExpressAmount());
        orderDetailVO.setPointAmount(order.getGetPoint());
        orderDetailVO.setPaymentAmount(order.getPaymentAmount());
        orderDetailVO.setGoodsList(goodsList);
        orderDetailVO.setShopName(shop.getName());
        orderDetailVO.setShopAddress(shop.getAddress());
        orderDetailVO.setLongitude(shop.getLongitude());
        orderDetailVO.setLatitude(shop.getLatitude());
        orderDetailVO.setShopId(shop.getId());
        orderDetailVO.setTechnicianName(technician.getName());
        return orderDetailVO;
    }
 
    @Override
    public boolean check(Order order, Integer shopId, Long userId) {
        R<List<AppUserShop>> r = appUserClient.getAppUserShop(userId);
        if (r.getCode() != R.SUCCESS){
            throw new ServiceException("获取用户门店信息失败");
        }
        List<AppUserShop> appUserShopList = r.getData();
        if (appUserShopList == null || appUserShopList.isEmpty()){
            return false;
        }
 
        // 判断用户是否拥有该门店
        List<AppUserShop> userShopList = appUserShopList.stream()
                .filter(appUserShop -> appUserShop.getShopId().equals(shopId))
                .collect(Collectors.toList());
        if (userShopList.isEmpty()){
            return false;
        }
 
        // 判断订单是否属于该门店
        if (order == null){
            throw new ServiceException("订单不存在");
        }
 
        return order.getShopId().equals(shopId);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void writeOff(String code,Integer shopId) {
        LoginUser loginUserApplet = tokenService.getLoginUserApplet();
        Order order = orderMapper.selectOne(new LambdaQueryWrapper<Order>()
                .eq(Order::getOrderNumber, code));
        boolean check = check(order, shopId, loginUserApplet.getUserid());
        if (!check){
            throw new ServiceException("订单不存在");
        }
        order.setOrderStatus(OrderStatus.COMPLETED.getCode());
        orderMapper.updateById(order);
        Integer orderType = order.getOrderType();
        if (orderType.equals(OrderType.SERVICE.getCode())){
            R<TechnicianSubscribe> subscribeR = technicianClient.getSubscribeByOrderId(order.getId());
            if (R.isError(subscribeR)){
                throw new ServiceException("获取预约信息失败");
            }
            TechnicianSubscribe subscribe = subscribeR.getData();
            subscribe.setStatus(2);
            R<Void> r = technicianClient.updateStatus(subscribe.getStatus(), subscribe.getId());
            if (R.isError(r)){
                throw new ServiceException("更新预约状态失败");
            }
        }
 
        // 售后设置
        R<BaseSetting> baseSettingR = baseSettingClient.getBaseSetting(5);
        if (R.isError(baseSettingR)) {
            throw new ServiceException("售后设置获取失败");
        }
        BaseSetting baseSetting = baseSettingR.getData();
        if (baseSetting == null) {
            throw new ServiceException("售后设置获取失败");
        }
        String content = baseSetting.getContent();
        JSONObject jsonObject = JSONObject.parseObject(content);
        Long days = jsonObject.getLong("days");
        commissionService.addToCommissionDelayQueue(order.getId(), LocalDateTime.now().plusDays(days));
    }
}