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);
|
}
|
}
|