guyue
2025-08-01 e9771fd8d8c68eb6753ce19726286302a64bbc5f
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
package com.stylefeng.guns.modular.system.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.modular.crossCity.model.OrderCrossCity;
import com.stylefeng.guns.modular.crossCity.server.IOrderCrossCityService;
import com.stylefeng.guns.modular.smallLogistics.model.OrderLogistics;
import com.stylefeng.guns.modular.smallLogistics.server.IOrderLogisticsService;
import com.stylefeng.guns.modular.specialTrain.model.OrderPrivateCar;
import com.stylefeng.guns.modular.specialTrain.server.IOrderPrivateCarService;
import com.stylefeng.guns.modular.system.dao.InvoiceMapper;
import com.stylefeng.guns.modular.system.model.Invoice;
import com.stylefeng.guns.modular.system.service.IInvoiceService;
import com.stylefeng.guns.modular.taxi.model.OrderTaxi;
import com.stylefeng.guns.modular.taxi.service.IOrderTaxiService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
 
 
@Service
public class InvoiceServiceImpl extends ServiceImpl<InvoiceMapper, Invoice> implements IInvoiceService {
 
    @Resource
    private InvoiceMapper invoiceMapper;
 
    @Autowired
    private IOrderTaxiService orderTaxiService;
 
    @Autowired
    private IOrderPrivateCarService orderPrivateCarService;
 
    @Autowired
    private IOrderCrossCityService orderCrossCityService;
 
    @Autowired
    private IOrderLogisticsService orderLogisticsService;
 
 
 
    /**
     * 申请开票操作
     * @param invoice
     * @param order
     * @throws Exception
     */
    @Override
    public void invoicing(Invoice invoice, String order, Integer uid) throws Exception {
        JSONArray jsonArray = JSON.parseArray(order);
        // 1. 按companyId分组存放订单(key: companyId, value: 该公司的所有订单)
        Map<Integer, List<JSONObject>> companyOrdersMap = new HashMap<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            Integer orderType = jsonObject.getIntValue("type");
            Integer orderId = jsonObject.getIntValue("id");
            Integer companyId = null;
 
            // 2. 获取每个订单的companyId
            switch (orderType) {
                case 1: // 专车
                    OrderPrivateCar privateCar = orderPrivateCarService.selectById(orderId);
                    companyId = privateCar.getCompanyId();
                    break;
                case 2: // 出租车
                    OrderTaxi taxi = orderTaxiService.selectById(orderId);
                    companyId = taxi.getCompanyId();
                    break;
                case 3: // 跨城出行
                    OrderCrossCity crossCity = orderCrossCityService.selectById(orderId);
                    companyId = crossCity.getCompanyId();
                    break;
                case 4: // 同城小件物流
                case 5: // 跨城小件物流
                    OrderLogistics logistics = orderLogisticsService.selectById(orderId);
                    companyId = logistics.getCompanyId();
                    break;
            }
 
            // 3. 将订单按companyId分组
            if (companyId == null) {
                throw new Exception("订单ID=" + orderId + "未找到关联公司");
            }
            companyOrdersMap.computeIfAbsent(companyId, k -> new ArrayList<>()).add(jsonObject);
        }
        // 4. 为每个公司单独创建发票
        for (Map.Entry<Integer, List<JSONObject>> entry : companyOrdersMap.entrySet()) {
            Integer companyId = entry.getKey();
            List<JSONObject> companyOrders = entry.getValue();
 
            // 计算该公司订单的总金额
            BigDecimal sum = new BigDecimal(0);
            for (JSONObject orderJson : companyOrders) {
                Integer orderType = orderJson.getIntValue("type");
                Integer orderId = orderJson.getIntValue("id");
 
                switch (orderType) {
                    case 1:
                        OrderPrivateCar privateCar = orderPrivateCarService.selectById(orderId);
                        sum = sum.add(new BigDecimal(privateCar.getPayMoney()));
                        break;
                    case 2:
                        OrderTaxi taxi = orderTaxiService.selectById(orderId);
                        sum = sum.add(new BigDecimal(taxi.getPayMoney()));
                        break;
                    case 3:
                        OrderCrossCity crossCity = orderCrossCityService.selectById(orderId);
                        sum = sum.add(new BigDecimal(crossCity.getPayMoney()));
                        break;
                    case 4:
                    case 5:
                        OrderLogistics logistics = orderLogisticsService.selectById(orderId);
                        sum = sum.add(new BigDecimal(logistics.getPayMoney()));
                        break;
                }
            }
 
            // 创建该公司的发票(复制原invoice的基础信息,覆盖companyId和金额)
            Invoice companyInvoice = new Invoice();
            // 复制原发票的非金额相关字段(如发票类型、抬头等)
            BeanUtils.copyProperties(invoice, companyInvoice, "id", "money", "orderNum", "insertTime", "state", "companyId");
            companyInvoice.setCompanyId(companyId); // 关联公司ID
            companyInvoice.setOrderNum(companyOrders.size()); // 该公司的订单数量
            companyInvoice.setMoney(sum.setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue()); // 该公司的总金额
            companyInvoice.setUserId(uid);
            companyInvoice.setInsertTime(new Date());
            companyInvoice.setState(1);
            this.insert(companyInvoice); // 保存发票
 
            // 调用第三方开票SDK(按公司发票单独开票)
            // TODO: 调用开发票第三方SDK,传入companyInvoice参数
 
            // 5. 将该公司的订单关联到对应的发票ID
            for (JSONObject orderJson : companyOrders) {
                Integer orderType = orderJson.getIntValue("type");
                Integer orderId = orderJson.getIntValue("id");
 
                switch (orderType) {
                    case 1:
                        OrderPrivateCar privateCar = orderPrivateCarService.selectById(orderId);
                        privateCar.setInvoiceId(companyInvoice.getId());
                        orderPrivateCarService.updateById(privateCar);
                        break;
                    case 2:
                        OrderTaxi taxi = orderTaxiService.selectById(orderId);
                        taxi.setInvoiceId(companyInvoice.getId());
                        orderTaxiService.updateById(taxi);
                        break;
                    case 3:
                        OrderCrossCity crossCity = orderCrossCityService.selectById(orderId);
                        crossCity.setInvoiceId(companyInvoice.getId());
                        orderCrossCityService.updateById(crossCity);
                        break;
                    case 4:
                    case 5:
                        OrderLogistics logistics = orderLogisticsService.selectById(orderId);
                        logistics.setInvoiceId(companyInvoice.getId());
                        orderLogisticsService.updateById(logistics);
                        break;
                }
            }
        }
 
    }
 
 
    /**
     * 获取发票历史记录
     * @param pageNum
     * @param size
     * @param uid
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String, Object>> queryMyInvoice(Integer pageNum, Integer size, Integer uid) throws Exception {
        pageNum = (pageNum - 1) * size;
        return invoiceMapper.queryMyInvoice(pageNum, size, uid);
    }
}