陶杰
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
package com.mzl.flower.service.transport;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mzl.flower.config.exception.ValidationException;
import com.mzl.flower.config.security.SecurityUtils;
import com.mzl.flower.dto.AreaDTO;
import com.mzl.flower.dto.request.transport.*;
import com.mzl.flower.dto.response.transport.*;
import com.mzl.flower.entity.customer.Address;
import com.mzl.flower.entity.customer.Customer;
import com.mzl.flower.entity.transport.Transport;
import com.mzl.flower.entity.transport.TransportFee;
import com.mzl.flower.entity.transport.TransportFeeArea;
import com.mzl.flower.mapper.customer.AddressMapper;
import com.mzl.flower.mapper.transport.TransportFeeAreaMapper;
import com.mzl.flower.mapper.transport.TransportFeeMapper;
import com.mzl.flower.mapper.transport.TransportMapper;
import com.mzl.flower.service.BaseService;
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.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Slf4j
@Service
@Transactional
public class TransportService extends BaseService {
 
    @Autowired
    private TransportMapper transportMapper;
 
    @Autowired
    private TransportFeeMapper feeMapper;
 
    @Autowired
    private TransportFeeAreaMapper feeAreaMapper;
 
    @Autowired
    private AddressMapper addressMapper;
 
    public Long addTransport(TransportCreateDTO dto){
        Transport g = new Transport();
 
        BeanUtils.copyProperties(dto, g);
        g.create(SecurityUtils.getUserId());
 
        transportMapper.insert(g);
 
        return g.getId();
    }
 
    public Long updateTransport(TransportUpdateDTO dto){
        Long id = dto.getId();
        Transport g = transportMapper.selectById(id);
 
        BeanUtils.copyProperties(dto, g);
        g.update(SecurityUtils.getUserId());
 
        transportMapper.updateById(g);
 
        return id;
    }
 
    public TransportDTO getTransport(Long id){
        TransportDTO dto = new TransportDTO();
        Transport d = transportMapper.selectById(id);
        BeanUtils.copyProperties(d, dto);
 
        return dto;
    }
 
    public Page<TransportDTO> selectTransportList(Page page, TransportQueryDTO dto){
        List<TransportDTO> ls = transportMapper.selectTransportList(page, dto);
 
        page.setRecords(ls);
 
        return page;
    }
 
    public void deleteTransport(Long id){
        transportMapper.deleteById(id);
    }
 
    public List<TransportFeeListDTO> getTransportFees(Long id){
        List<TransportFee> ls = feeMapper.selectList(new QueryWrapper<TransportFee>().eq("transport_id", id));
 
        List<TransportFeeArea> areaAll = feeAreaMapper.selectList(new QueryWrapper<TransportFeeArea>().eq("transport_id", id));
        Map<Long, List<AreaDTO>> map = new HashMap<>();
        if(ls != null){
            for(TransportFeeArea fa : areaAll){
                AreaDTO a = new AreaDTO();
                BeanUtils.copyProperties(fa, a);
                List<AreaDTO> areas = map.computeIfAbsent(fa.getTransportFeeId(), k -> new ArrayList<>());
                areas.add(a);
            }
        }
 
        List<TransportFeeListDTO> result = new ArrayList<>();
        if(ls != null && ls.size() > 0){
            for(TransportFee f : ls){
                TransportFeeListDTO dto = new TransportFeeListDTO();
                BeanUtils.copyProperties(f, dto);
                dto.setAreas(map.get(f.getId()));
                result.add(dto);
            }
        }
 
        return result;
    }
 
    public Long addFee(TransportFeeCreateDTO dto){
        TransportFee f = new TransportFee();
        BeanUtils.copyProperties(dto, f);
        f.create(SecurityUtils.getUserId());
 
        feeMapper.insert(f);
 
        saveFeeArea(f.getTransportId(), f.getId(), dto.getAreas());
 
        return f.getId();
    }
 
    private void saveFeeArea(Long tId, Long feeId, List<AreaDTO> areas){
        feeAreaMapper.delete(new QueryWrapper<TransportFeeArea>().eq("transport_fee_id", feeId));
        feeAreaMapper.clearTransportArea(tId, areas);
        if(areas != null){
            for(AreaDTO a : areas){
                TransportFeeArea fa = new TransportFeeArea();
                BeanUtils.copyProperties(a, fa);
                fa.setTransportFeeId(feeId);
                fa.setTransportId(tId);
                feeAreaMapper.insert(fa);
            }
        }
    }
 
    public Long updateFee(TransportFeeUpdateDTO dto){
        TransportFee f = feeMapper.selectById(dto.getId());
        BeanUtils.copyProperties(dto, f);
        f.update(SecurityUtils.getUserId());
 
        feeMapper.updateById(f);
 
        saveFeeArea(f.getTransportId(), f.getId(), dto.getAreas());
 
        return f.getId();
    }
 
    public TransportFeeDTO getFee(Long id){
        TransportFeeDTO dto = new TransportFeeDTO();
        TransportFee f = feeMapper.selectById(id);
        BeanUtils.copyProperties(f, dto);
 
        List<TransportFeeArea> ls = feeAreaMapper.selectList(new QueryWrapper<TransportFeeArea>().eq("transport_fee_id", id));
        List<AreaDTO> areas = new ArrayList<>();
        if(ls != null){
            for(TransportFeeArea fa : ls){
                AreaDTO a = new AreaDTO();
                BeanUtils.copyProperties(fa, a);
                areas.add(a);
            }
        }
        dto.setAreas(areas);
 
        return dto;
    }
 
    public void deleteFee(Long id){
        feeMapper.deleteById(id);
        feeAreaMapper.delete(new QueryWrapper<TransportFeeArea>().eq("transport_fee_id", id));
    }
 
    public List<TransportOrderDTO> getPreOrderTransportList(Long addressId, Double weight){
        Address address = addressMapper.selectById(addressId);
        return getPreOrderTransportList(address, weight, null);
    }
 
    public List<TransportOrderDTO> getPreOrderTransportList(Address address, Double weight, Long transportId){
        if(address == null){
            throw new ValidationException("收货地址不存在");
        }
 
        Customer customer = getCurrentCustomer();
        Long partnerId = customer.getPartnerId();
 
        List<TransportOrderDTO> result = new ArrayList<>();
        if(partnerId != null){
            log.info("合伙人店铺购买不需要物流");
            return result;
        }
        double w = weight == null ? 0 : weight;
        List<TransportFeeMListDTO> tfLs = feeMapper.selectFeeList(address.getProvince(), address.getCity(), transportId);
        List<Long> tList = new ArrayList<>();
        if(tfLs != null && tfLs.size() > 0){
            for (TransportFeeMListDTO tf : tfLs) {
                Long tId = tf.getTransportId();
                if(tList.contains(tId)){
                    continue;
                }
                tList.add(transportId);
                TransportOrderDTO o = new TransportOrderDTO();
                o.setId(tId);
                o.setName(tf.getName());
                o.setEnName(tf.getEnName());
                o.setFee(getFee(w, tf.getFirstWeight(), tf.getAddedWeight()
                        , tf.getFirstWeightFee(), tf.getAddedWeightFee()));
                result.add(o);
            }
        }
        return result;
    }
 
    private BigDecimal getFee(double w, Double firstWeight, Double addedWeight
            , BigDecimal firstWeightFee, BigDecimal addedWeightFee){
        double f = firstWeight == null ? 0 : firstWeight;
        double a = addedWeight == null ? 0 : addedWeight;
        BigDecimal fee = firstWeightFee == null ? new BigDecimal(0) : firstWeightFee;
        BigDecimal af = addedWeightFee == null ? new BigDecimal(0) : addedWeightFee;
        if(a > 0){
            double wd = w - f;
            if(wd > 0){
                double num = Math.ceil(wd / a);
                fee = fee.add(af.multiply(new BigDecimal(num)));
            }
        }
 
        return fee;
    }
}