| | |
| | | package com.ruoyi.order.service.impl; |
| | | |
| | | import com.alibaba.fastjson2.JSON; |
| | | import com.alibaba.fastjson2.JSONArray; |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.chargingPile.api.feignClient.ChargingGunClient; |
| | | import com.ruoyi.chargingPile.api.feignClient.SiteClient; |
| | | import com.ruoyi.chargingPile.api.model.Site; |
| | | import com.ruoyi.common.core.utils.StringUtils; |
| | | import com.ruoyi.common.core.web.domain.AjaxResult; |
| | | import com.ruoyi.common.security.service.TokenService; |
| | | import com.ruoyi.order.api.model.TChargingOrder; |
| | | import com.ruoyi.order.api.model.TOrderInvoice; |
| | | import com.ruoyi.order.api.model.TOrderInvoiceDetail; |
| | | import com.ruoyi.order.api.model.TShoppingOrder; |
| | | import com.ruoyi.order.dto.*; |
| | | import com.ruoyi.order.mapper.TOrderInvoiceMapper; |
| | | import com.ruoyi.order.service.TChargingOrderService; |
| | | import com.ruoyi.order.service.TOrderInvoiceDetailService; |
| | | import com.ruoyi.order.service.TOrderInvoiceService; |
| | | import com.ruoyi.order.service.TShoppingOrderService; |
| | | import com.ruoyi.other.api.domain.TCoupon; |
| | | import com.ruoyi.other.api.domain.TGoods; |
| | | import com.ruoyi.other.api.domain.TInvoiceType; |
| | | import com.ruoyi.other.api.feignClient.CouponClient; |
| | | import com.ruoyi.other.api.feignClient.GoodsClient; |
| | | import com.ruoyi.other.api.feignClient.InvoiceTypeClient; |
| | | import io.seata.spring.annotation.GlobalTransactional; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.math.BigDecimal; |
| | | import java.text.SimpleDateFormat; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | */ |
| | | @Service |
| | | public class TOrderInvoiceServiceImpl extends ServiceImpl<TOrderInvoiceMapper, TOrderInvoice> implements TOrderInvoiceService { |
| | | |
| | | |
| | | @Resource |
| | | private TokenService tokenService; |
| | | |
| | | @Resource |
| | | private TOrderInvoiceDetailService orderInvoiceDetailService; |
| | | |
| | | @Resource |
| | | private InvoiceTypeClient invoiceTypeClient; |
| | | |
| | | @Resource |
| | | private TChargingOrderService chargingOrderService; |
| | | |
| | | @Resource |
| | | private TShoppingOrderService shoppingOrderService; |
| | | |
| | | @Resource |
| | | private ChargingGunClient chargingGunClient; |
| | | |
| | | @Resource |
| | | private SiteClient siteClient; |
| | | |
| | | @Resource |
| | | private GoodsClient goodsClient; |
| | | |
| | | @Resource |
| | | private CouponClient couponClient; |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * 添加开票申请 |
| | | * @param addOrderInvoice |
| | | * @return |
| | | */ |
| | | @Override |
| | | @GlobalTransactional(rollbackFor = Exception.class) |
| | | public AjaxResult addOrderInvoice(AddOrderInvoice addOrderInvoice) { |
| | | Long userId = tokenService.getLoginUserApplet().getUserId(); |
| | | String orders = addOrderInvoice.getOrders(); |
| | | if(StringUtils.isNotEmpty(orders)){ |
| | | return AjaxResult.error("请选择有效的订单"); |
| | | } |
| | | JSONArray parse = JSONArray.parse(orders); |
| | | List<Long> orderIds = new ArrayList<>(); |
| | | Map<Long, BigDecimal> map = new HashMap<>(); |
| | | for (int i = 0; i < parse.size(); i++) { |
| | | JSONObject jsonObject = parse.getJSONObject(i); |
| | | Long id = jsonObject.getLong("id"); |
| | | BigDecimal amount = jsonObject.getBigDecimal("amount"); |
| | | orderIds.add(id); |
| | | map.put(id, amount); |
| | | } |
| | | long count = orderInvoiceDetailService.count(new LambdaQueryWrapper<TOrderInvoiceDetail>().eq(TOrderInvoiceDetail::getOrderType, addOrderInvoice.getOrderType()) |
| | | .in(TOrderInvoiceDetail::getOrderId, orderIds)); |
| | | if(count > 0){ |
| | | return AjaxResult.error("不能重复申请开票,请刷新数据后重试"); |
| | | } |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS"); |
| | | String code = Math.random() * 1000 + sdf.format(new Date()); |
| | | addOrderInvoice.setAppUserId(userId); |
| | | addOrderInvoice.setCode(code); |
| | | addOrderInvoice.setStatus(1); |
| | | this.save(addOrderInvoice); |
| | | //获取开票类型 |
| | | TInvoiceType invoiceType = invoiceTypeClient.getInvoiceType(addOrderInvoice.getInvoiceTypeId()).getData(); |
| | | for (Long orderId : orderIds) { |
| | | TOrderInvoiceDetail orderInvoiceDetail = new TOrderInvoiceDetail(); |
| | | orderInvoiceDetail.setOrderInvoiceId(addOrderInvoice.getId()); |
| | | orderInvoiceDetail.setInvoiceAmount(map.get(orderId)); |
| | | orderInvoiceDetail.setOrderType(addOrderInvoice.getOrderType()); |
| | | orderInvoiceDetail.setOrderId(orderId); |
| | | orderInvoiceDetail.setElectricityTariff(invoiceType.getElectricityTariff()); |
| | | orderInvoiceDetail.setServiceTariff(invoiceType.getServiceTariff()); |
| | | orderInvoiceDetail.setAddedServiceTariff(invoiceType.getAddedServiceTariff()); |
| | | orderInvoiceDetailService.save(orderInvoiceDetail); |
| | | } |
| | | return AjaxResult.success(); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取开票记录列表 |
| | | * @param query |
| | | * @return |
| | | */ |
| | | @Override |
| | | public List<OrderInvoiceList> getMyOrderInvoiceList(GetOrderInvoiceList query) { |
| | | Long userId = tokenService.getLoginUserApplet().getUserId(); |
| | | List<TOrderInvoice> list = this.list(new LambdaQueryWrapper<TOrderInvoice>().eq(TOrderInvoice::getAppUserId, userId) |
| | | .eq(TOrderInvoice::getStatus, query.getStatus()).orderByDesc(TOrderInvoice::getCreateTime) |
| | | .last(" limit " + query.getPageCurr() + ", " + query.getPageSize())); |
| | | List<OrderInvoiceList> pageList = new ArrayList<>(); |
| | | for (TOrderInvoice tOrderInvoice : list) { |
| | | OrderInvoiceList orderInvoiceList = new OrderInvoiceList(); |
| | | orderInvoiceList.setId(tOrderInvoice.getId().toString()); |
| | | orderInvoiceList.setOrderType(tOrderInvoice.getOrderType()); |
| | | orderInvoiceList.setTotalAmount(tOrderInvoice.getTotalAmount()); |
| | | orderInvoiceList.setName(tOrderInvoice.getName()); |
| | | orderInvoiceList.setCreateTime(tOrderInvoice.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); |
| | | pageList.add(orderInvoiceList); |
| | | } |
| | | return pageList; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取开票申请详情 |
| | | * @param id |
| | | * @return |
| | | */ |
| | | @Override |
| | | public MyOrderInvoiceInfo getMyOrderInvoiceInfo(String id) { |
| | | TOrderInvoice orderInvoice = this.getById(id); |
| | | MyOrderInvoiceInfo myOrderInvoiceInfo = new MyOrderInvoiceInfo(); |
| | | myOrderInvoiceInfo.setId(id); |
| | | myOrderInvoiceInfo.setCode(orderInvoice.getCode()); |
| | | myOrderInvoiceInfo.setInvoiceType(orderInvoice.getInvoiceType()); |
| | | myOrderInvoiceInfo.setOrderType(orderInvoice.getOrderType()); |
| | | myOrderInvoiceInfo.setInvoicingObjectType(orderInvoice.getInvoicingObjectType()); |
| | | myOrderInvoiceInfo.setTotalAmount(orderInvoice.getTotalAmount()); |
| | | myOrderInvoiceInfo.setInvoiceUrl(orderInvoice.getInvoiceUrl()); |
| | | myOrderInvoiceInfo.setStatus(orderInvoice.getStatus()); |
| | | Integer orderType = orderInvoice.getOrderType(); |
| | | List<TOrderInvoiceDetail> list = orderInvoiceDetailService.list(new LambdaQueryWrapper<TOrderInvoiceDetail>().eq(TOrderInvoiceDetail::getOrderInvoiceId, id)); |
| | | List<Long> orderIds = list.stream().map(TOrderInvoiceDetail::getOrderId).collect(Collectors.toList()); |
| | | //充电订单 |
| | | if(orderType == 1){ |
| | | List<TChargingOrder> orderList = chargingOrderService.listByIds(orderIds); |
| | | List<MyChargingOrderList> chargingOrder = new ArrayList<>(); |
| | | for (TChargingOrder tChargingOrder : orderList) { |
| | | MyChargingOrderList myChargingOrderList = new MyChargingOrderList(); |
| | | myChargingOrderList.setId(tChargingOrder.getId().toString()); |
| | | myChargingOrderList.setStatus(tChargingOrder.getStatus()); |
| | | Site site = siteClient.getSiteByIds(Arrays.asList(tChargingOrder.getSiteId())).getData().get(0); |
| | | myChargingOrderList.setTitle(site.getName()); |
| | | myChargingOrderList.setChargingDegree(tChargingOrder.getChargingCapacity()); |
| | | String name = chargingGunClient.getAllName(tChargingOrder.getChargingGunId()).getData(); |
| | | myChargingOrderList.setName(name); |
| | | myChargingOrderList.setEndMode(tChargingOrder.getEndMode()); |
| | | BigDecimal payMoney = tChargingOrder.getStatus() < 4 ? tChargingOrder.getRechargeAmount() : tChargingOrder.getPaymentAmount(); |
| | | myChargingOrderList.setPayMoney(payMoney); |
| | | myChargingOrderList.setCreateTime(tChargingOrder.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); |
| | | chargingOrder.add(myChargingOrderList); |
| | | } |
| | | myOrderInvoiceInfo.setChargingOrder(chargingOrder); |
| | | } |
| | | //购物订单 |
| | | if(orderType == 2){ |
| | | List<TShoppingOrder> orderList = shoppingOrderService.listByIds(orderIds); |
| | | List<MyShoppingOrderList> shoppingOrder = new ArrayList<>(); |
| | | for (TShoppingOrder tShoppingOrder : orderList) { |
| | | MyShoppingOrderList myShoppingOrderList = new MyShoppingOrderList(); |
| | | myShoppingOrderList.setId(tShoppingOrder.getId().toString()); |
| | | String name = ""; |
| | | String imgUrl = ""; |
| | | if(tShoppingOrder.getOrderType() == 1){ |
| | | TGoods goods = goodsClient.getGoodsById(tShoppingOrder.getGoodsId()).getData(); |
| | | name = goods.getName(); |
| | | imgUrl = goods.getCoverPicture(); |
| | | }else{ |
| | | TCoupon coupon = couponClient.getCouponById1(tShoppingOrder.getGoodsId()).getData(); |
| | | name = coupon.getName(); |
| | | imgUrl = coupon.getCoverPicture(); |
| | | } |
| | | myShoppingOrderList.setName(name); |
| | | myShoppingOrderList.setImgUrl(imgUrl); |
| | | myShoppingOrderList.setStatus(tShoppingOrder.getStatus()); |
| | | BigDecimal unitPrice = tShoppingOrder.getPaymentAmount().divide(new BigDecimal(tShoppingOrder.getPurchaseQuantity())).setScale(2, BigDecimal.ROUND_HALF_EVEN); |
| | | myShoppingOrderList.setUnitPrice(unitPrice); |
| | | myShoppingOrderList.setNumber(tShoppingOrder.getPurchaseQuantity()); |
| | | myShoppingOrderList.setPaymentAmount(tShoppingOrder.getPaymentAmount()); |
| | | shoppingOrder.add(myShoppingOrderList); |
| | | } |
| | | myOrderInvoiceInfo.setShoppingOrder(shoppingOrder); |
| | | } |
| | | return myOrderInvoiceInfo; |
| | | } |
| | | } |