陶杰
2024-08-22 ee9032d9baf5f33e376d2d2699136e0a7b26bec7
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
package com.mzl.flower.service.payment;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mzl.flower.component.SequenceNo;
import com.mzl.flower.config.exception.ValidationException;
import com.mzl.flower.config.security.SecurityUtils;
import com.mzl.flower.constant.Constants;
import com.mzl.flower.dto.request.payment.*;
import com.mzl.flower.dto.response.payment.OrderItemSalesNewDTO;
import com.mzl.flower.dto.response.payment.OrderItemSalesNewListDTO;
import com.mzl.flower.entity.payment.*;
import com.mzl.flower.entity.supplier.Station;
import com.mzl.flower.mapper.payment.*;
import com.mzl.flower.mapper.supplier.StationMapper;
import com.mzl.flower.service.BaseService;
import com.mzl.flower.utils.UUIDGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
 
@Slf4j
@Service
@Transactional
public class OrderItemSalesService extends BaseService {
 
    @Autowired
    private OrderItemSalesMapper orderItemSalesMapper;
 
    @Autowired
    private OrderMapper orderMapper;
 
    @Autowired
    private OrderItemMapper orderItemMapper;
 
    @Autowired
    private UserPaymentV3Service paymentV3Service;
 
    @Autowired
    private SequenceNo sequenceNo;
 
    @Autowired
    private StationMapper stationMapper;
 
    public String createSales(OrderItemSalesCreateDTO dto){
        Integer num = dto.getNum();
        if(num == null || num == 0){
            throw new ValidationException("申请数量不能为空");
        }
 
        OrderItem oi = orderItemMapper.selectById(dto.getOrderItemId());
        if(oi == null){
            throw new ValidationException("订单商品不存在");
        }
 
        if(num > oi.getNum()){
            throw new ValidationException("申请数量不能超过总数");
        }
 
        //收货前可以申请售后,售后最多申请两次
        Order o = orderMapper.selectById(oi.getOrderId());
        String status = o.getStatus();
        if(!Constants.ORDER_STATUS.RECEIVE.name().equals(status)){
            throw new ValidationException("无法申请售后");
        }
 
        String userId = SecurityUtils.getUserId();
        int count = orderItemSalesMapper.selectCount(new QueryWrapper<OrderItemSales>()
                .eq("order_item_id", oi.getId()));
        if(count >= 2){
            throw new ValidationException("售后申请超过限额");
        }
 
        String title = count > 0 ? "第二次售后" : "";
 
        count = orderItemSalesMapper.selectCount(new QueryWrapper<OrderItemSales>()
                .eq("order_item_id", oi.getId())
                .eq("status", Constants.ORDER_SALES_STATUS.PENDING.name())
        );
        if(count > 0){
            throw new ValidationException("有未处理售后,无法申请");
        }
 
        OrderItemSales s = new OrderItemSales();
        s.setId(UUIDGenerator.getUUID());
        s.setOrderId(oi.getOrderId());
        s.setOrderItemId(oi.getId());
        s.setSalesNo(getSalesNo());
        s.setTitle(title);
        s.setPictures(toJSONString(dto.getPictureList()));
        s.setVideos(toJSONString(dto.getVideoList()));
        s.setReason(dto.getReason());
        s.setStatus(Constants.ORDER_SALES_STATUS.PENDING.name());
        s.create(userId);
        orderItemSalesMapper.insert(s);
 
        return s.getId();
    }
 
    private String getSalesNo(){
        String seq  = sequenceNo.getSeqNo(SequenceNo.ORDER_ITEM_SALES);
        return "SH" + format(LocalDateTime.now(), "yyyyMMdd") + seq;
    }
 
    public void cancelSales(String id){
        OrderItemSales s = orderItemSalesMapper.selectById(id);
        String userId = SecurityUtils.getUserId();
        if(!userId.equals(s.getCreateBy())){
            throw new ValidationException("无权操作");
        }
 
        String status = s.getStatus();
        if(!Constants.SALES_STATUS.PENDING.name().equals(status)){
            throw new ValidationException("已处理完成不用取消");
        }
 
        s.setStatus(Constants.SALES_STATUS.CANCEL.name());
        s.update(userId);
        orderItemSalesMapper.updateById(s);
    }
 
    public Page<OrderItemSalesNewListDTO> selectSalesList(Page page, OrderItemSalesQueryDTO dto){
        dto.setOrderStartDate(parseLocalDateTime(dto.getOrderStartDateStr()));
        dto.setOrderEndDate(parseLocalDateTime(dto.getOrderEndDateStr()));
        dto.setSalesStartDate(parseLocalDateTime(dto.getSalesStartDateStr()));
        dto.setSalesEndDate(parseLocalDateTime(dto.getSalesEndDateStr()));
 
        List<OrderItemSalesNewListDTO> ls = orderItemSalesMapper.selectItemSalesList(page, dto);
 
        page.setRecords(ls);
        return page;
    }
 
    public OrderItemSalesNewDTO getSalesInfo(String id){
        OrderItemSalesNewDTO rr = new OrderItemSalesNewDTO();
        OrderItemSales sl = orderItemSalesMapper.selectById(id);
        if(sl == null){
            throw new ValidationException("售后单不存在");
        }
 
        BeanUtils.copyProperties(sl, rr);
        rr.setPictureList(parseArray(sl.getPictures(), String.class));
        rr.setVideoList(parseArray(sl.getVideos(), String.class));
 
        Order o = orderMapper.selectById(sl.getOrderId());
        rr.setOrderNo(o.getOrderNo());
        rr.setCustomer(o.getCustomer());
        rr.setCustomerTel(o.getCustomerTel());
        rr.setCustomerProvince(o.getCustomerProvince());
        rr.setCustomerCity(o.getCustomerCity());
        rr.setCustomerRegion(o.getCustomerRegion());
        rr.setCustomerAddress(o.getCustomerAddress());
 
        OrderItem oi = orderItemMapper.selectById(sl.getOrderItemId());
        rr.setFlowerName(oi.getFlowerName());
        rr.setFlowerUnit(oi.getFlowerUnit());
        rr.setFlowerColor(oi.getFlowerColor());
        rr.setFlowerCover(oi.getFlowerCover());
        rr.setFlowerLevel(oi.getFlowerLevel());
        rr.setFlowerCategory(oi.getFlowerCategory());
        rr.setFlowerNum(oi.getNum());
        rr.setSupplierName(oi.getSupplierName());
        rr.setPrice(oi.getPrice());
        rr.setTotal(oi.getTotal());
 
        Long stationId = oi.getStationId();
        if(stationId != null) {
            Station s = stationMapper.selectById(oi.getStationId());
            rr.setStationName(s.getName());
        }
 
        return rr;
    }
 
    public OrderItem doAudit(OrderItemSalesAuditDTO dto, String status){
        OrderItemSales sl = orderItemSalesMapper.selectById(dto.getId());
        if(sl == null){
            throw new ValidationException("售后单不存在");
        }
        if(!Constants.ORDER_SALES_STATUS.PENDING.name().equals(sl.getStatus())){
            throw new ValidationException("不可操作");
        }
 
        OrderItem oi = orderItemMapper.selectById(sl.getOrderItemId());
        if(oi == null){
            throw new ValidationException("订单商品不存在");
        }
 
        BigDecimal feeSupplier = getAmount(dto.getFeeSupplier());
        BigDecimal feePartner = getAmount(dto.getFeePartner());
        BigDecimal feePlatform = getAmount(dto.getFeePlatform());
        BigDecimal feePlatformPack = getAmount(dto.getFeePlatformPack());
        BigDecimal feePlatformCheck = getAmount(dto.getFeePlatformCheck());
        BigDecimal feePlatformTransport = getAmount(dto.getFeePlatformTransport());
 
        BigDecimal totalFee = feeSupplier.add(feePartner).add(feePlatform).add(feePlatformPack)
                .add(feePlatformCheck).add(feePlatformTransport);
 
        List<OrderItemSales> ls = orderItemSalesMapper.selectList(new QueryWrapper<OrderItemSales>()
                .eq("order_item_id", oi.getId())
                .eq("status", Constants.ORDER_SALES_STATUS.AGREED.name())
        );
        BigDecimal preFee = new BigDecimal(0);
        if(ls != null && ls.size() > 0){
            for(OrderItemSales s : ls){
                preFee = preFee.add(s.getTotalFee());
            }
        }
        preFee = preFee.add(totalFee);
        if(preFee.doubleValue() > oi.getTotal().doubleValue()){
            throw new ValidationException("退款金额不能大于商品总价");
        }
 
        sl.setRemarks(dto.getRemarks());
        sl.setFeeSupplier(feeSupplier);
        sl.setFeePartner(feePartner);
        sl.setFeePlatform(feePlatform);
        sl.setFeePlatformPack(feePlatformPack);
        sl.setFeePlatformCheck(feePlatformCheck);
        sl.setFeePlatformTransport(feePlatformTransport);
        sl.setTotalFee(totalFee);
        sl.setStatus(status);
        sl.setAuditTime(LocalDateTime.now());
        sl.update(SecurityUtils.getUserId());
 
        if(Constants.ORDER_SALES_STATUS.AGREED.name().equals(sl.getStatus())){
            if(totalFee.doubleValue() > 0) {
                Order o = orderMapper.selectById(sl.getOrderId());
                String refundId = paymentV3Service.refundOrderSub(o, totalFee);
                sl.setRefundId(refundId);
            }
        }
 
        orderItemSalesMapper.updateById(sl);
 
        return oi;
    }
 
    private BigDecimal getAmount(BigDecimal amount){
        return amount == null ? new BigDecimal(0) : amount;
    }
}