| | |
| | | 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.core.toolkit.Wrappers; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.order.api.model.TOrderInvoice; |
| | | import com.ruoyi.account.api.feignClient.AppUserClient; |
| | | import com.ruoyi.account.api.model.TAppUser; |
| | | import com.ruoyi.account.api.model.TAppUserCar; |
| | | 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.core.web.page.PageInfo; |
| | | import com.ruoyi.common.security.service.TokenService; |
| | | import com.ruoyi.order.api.model.*; |
| | | import com.ruoyi.order.api.query.TOrderInvoiceQuery; |
| | | import com.ruoyi.order.api.vo.TOrderEvaluateVO; |
| | | import com.ruoyi.order.api.vo.TOrderInvoiceVO; |
| | | import com.ruoyi.order.dto.*; |
| | | import com.ruoyi.order.mapper.TOrderInvoiceMapper; |
| | | import com.ruoyi.order.service.TOrderInvoiceService; |
| | | import com.ruoyi.order.service.*; |
| | | 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 org.springframework.util.CollectionUtils; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.math.BigDecimal; |
| | | import java.text.SimpleDateFormat; |
| | | import java.time.LocalDateTime; |
| | | 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; |
| | | @Resource |
| | | private AppUserClient appUserClient; |
| | | |
| | | @Resource |
| | | private TChargingOrderAccountingStrategyService chargingOrderAccountingStrategyService; |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * 添加开票申请 |
| | | * @param addOrderInvoice |
| | | * @return |
| | | */ |
| | | @Override |
| | | @GlobalTransactional(rollbackFor = Exception.class) |
| | | public AjaxResult addOrderInvoice(AddOrderInvoice addOrderInvoice) { |
| | | Long userId = tokenService.getLoginUserApplet().getUserId(); |
| | | String orders = addOrderInvoice.getOrders(); |
| | | if(StringUtils.isEmpty(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 = Double.valueOf(Math.random() * 1000).intValue() + sdf.format(new Date()); |
| | | //获取开票类型 |
| | | TInvoiceType invoiceType = invoiceTypeClient.getInvoiceType(addOrderInvoice.getInvoiceTypeId()).getData(); |
| | | addOrderInvoice.setInvoicingCompany(invoiceType.getInvoicingCompany()); |
| | | addOrderInvoice.setInvoiceType(invoiceType.getName()); |
| | | addOrderInvoice.setInvoiceMaterial(2); |
| | | addOrderInvoice.setInvoicingMethod(invoiceType.getInvoicingMethod()); |
| | | addOrderInvoice.setAppUserId(userId); |
| | | addOrderInvoice.setCode(code); |
| | | addOrderInvoice.setStatus(1); |
| | | addOrderInvoice.setCreateTime(LocalDateTime.now()); |
| | | this.save(addOrderInvoice); |
| | | for (Long orderId : orderIds) { |
| | | List<TChargingOrderAccountingStrategy> list = chargingOrderAccountingStrategyService.list(new LambdaQueryWrapper<TChargingOrderAccountingStrategy>() |
| | | .eq(TChargingOrderAccountingStrategy::getChargingOrderId, orderId)); |
| | | BigDecimal electrovalence = list.stream().map(TChargingOrderAccountingStrategy::getElectrovalence).reduce(BigDecimal.ZERO, BigDecimal::add); |
| | | BigDecimal serviceCharge = list.stream().map(TChargingOrderAccountingStrategy::getServiceCharge).reduce(BigDecimal.ZERO, BigDecimal::add); |
| | | |
| | | 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()); |
| | | orderInvoiceDetail.setElectrovalence(electrovalence); |
| | | orderInvoiceDetail.setServiceCharge(serviceCharge); |
| | | orderInvoiceDetail.setAddedService(serviceCharge.multiply(null == invoiceType.getAddedServiceTariff() |
| | | ? new BigDecimal(1) : invoiceType.getAddedServiceTariff().divide(new BigDecimal(100)))); |
| | | orderInvoiceDetailService.save(orderInvoiceDetail); |
| | | } |
| | | return AjaxResult.success(); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取开票记录列表 |
| | | * @param query |
| | | * @return |
| | | */ |
| | | @Override |
| | | public List<OrderInvoiceList> getMyOrderInvoiceList(GetOrderInvoiceList query) { |
| | | Long userId = tokenService.getLoginUserApplet().getUserId(); |
| | | Integer pageCurr = (query.getPageCurr() - 1) * query.getPageSize(); |
| | | LambdaQueryWrapper<TOrderInvoice> wrapper = new LambdaQueryWrapper<TOrderInvoice>().eq(TOrderInvoice::getAppUserId, userId); |
| | | if(query.getStatus() != 1){ |
| | | wrapper.eq(TOrderInvoice::getStatus, query.getStatus()); |
| | | } |
| | | List<TOrderInvoice> list = this.list(wrapper.orderByDesc(TOrderInvoice::getCreateTime).last(" limit " + pageCurr + ", " + 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"))); |
| | | myChargingOrderList.setElectricCharge(tChargingOrder.getElectrovalence()); |
| | | myChargingOrderList.setServiceCharge(tChargingOrder.getServiceCharge()); |
| | | 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()); |
| | | myShoppingOrderList.setCreateTime(tShoppingOrder.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy.MM.dd HH:mm:ss"))); |
| | | shoppingOrder.add(myShoppingOrderList); |
| | | } |
| | | myOrderInvoiceInfo.setShoppingOrder(shoppingOrder); |
| | | } |
| | | return myOrderInvoiceInfo; |
| | | } |
| | | |
| | | @Override |
| | | public PageInfo<TOrderInvoiceVO> pageList(TOrderInvoiceQuery query) { |
| | | List<TAppUser> tAppUsers = new ArrayList<>(); |
| | | // 查询申请用户 |
| | | if (org.springframework.util.StringUtils.hasLength(query.getUserPhone())){ |
| | | tAppUsers = appUserClient.selectByPhoneLike(query.getUserPhone()).getData(); |
| | | if(CollectionUtils.isEmpty(tAppUsers)){ |
| | | return new PageInfo<>(); |
| | | } |
| | | } |
| | | query.setUserIds(tAppUsers.stream().map(TAppUser::getId).collect(Collectors.toList())); |
| | | PageInfo<TOrderInvoiceVO> pageInfo = new PageInfo<>(query.getPageCurr(),query.getPageSize()); |
| | | List<TOrderInvoiceVO> list = this.baseMapper.pageList(query,pageInfo); |
| | | for (TOrderInvoiceVO tOrderInvoiceVO : list) { |
| | | tOrderInvoiceVO.setUid(tOrderInvoiceVO.getId().toString()); |
| | | List<Long> collect = orderInvoiceDetailService.lambdaQuery().eq(TOrderInvoiceDetail::getOrderInvoiceId, tOrderInvoiceVO.getId()).list().stream() |
| | | .map(TOrderInvoiceDetail::getOrderId).collect(Collectors.toList()); |
| | | // 将其全部转化为String |
| | | tOrderInvoiceVO.setIds(collect.stream().map(String::valueOf).collect(Collectors.toList())); |
| | | TAppUser appUser = appUserClient.getUserById(tOrderInvoiceVO.getAppUserId()).getData(); |
| | | if (appUser!=null){ |
| | | tOrderInvoiceVO.setUserPhone(appUser.getPhone()); |
| | | } |
| | | } |
| | | // 查询这个开票的订单ids |
| | | |
| | | if (!list.isEmpty()){ |
| | | List<Long> ids = list.stream().map(TOrderInvoiceVO::getId).collect(Collectors.toList()); |
| | | |
| | | List<TOrderInvoiceDetail> orderInvoiceDetailList = orderInvoiceDetailService.list(new LambdaQueryWrapper<TOrderInvoiceDetail>() |
| | | .in(TOrderInvoiceDetail::getOrderInvoiceId, ids)); |
| | | |
| | | List<TAppUser> finalTAppUsers = tAppUsers; |
| | | list.forEach(e->{ |
| | | if (e.getOrderType()!=null&&e.getOrderType()==1){ |
| | | e.setServiceTariff(orderInvoiceDetailList.get(0).getServiceTariff()); |
| | | }else{ |
| | | e.setServiceTariff(new BigDecimal("0")); |
| | | } |
| | | e.setElectricityTariff(orderInvoiceDetailList.get(0).getElectricityTariff()); |
| | | e.setAddedServiceTariff(orderInvoiceDetailList.get(0).getAddedServiceTariff()); |
| | | e.setAddedService(orderInvoiceDetailList.stream().filter(t->t.getOrderInvoiceId().equals(e.getId()) &&t.getAddedService()!=null).map(TOrderInvoiceDetail::getAddedService).reduce(BigDecimal::add).get()); |
| | | e.setElectrovalence(orderInvoiceDetailList.stream().filter(t->t.getOrderInvoiceId().equals(e.getId()) &&t.getElectrovalence()!=null).map(TOrderInvoiceDetail::getElectrovalence).reduce(BigDecimal::add).get()); |
| | | e.setServiceCharge(orderInvoiceDetailList.stream().filter(t->t.getOrderInvoiceId().equals(e.getId()) && t.getServiceCharge()!=null).map(TOrderInvoiceDetail::getServiceCharge).reduce(BigDecimal::add).get()); |
| | | }); |
| | | } |
| | | |
| | | |
| | | pageInfo.setRecords(list); |
| | | return pageInfo; |
| | | } |
| | | |
| | | @Override |
| | | public Map<String, Integer> statusCount(TOrderInvoiceQuery query) { |
| | | Map<String, Integer> map = new HashMap<>(); |
| | | List<TAppUser> tAppUsers = new ArrayList<>(); |
| | | |
| | | if (org.springframework.util.StringUtils.hasLength(query.getUserPhone())){ |
| | | tAppUsers = appUserClient.selectByPhoneLike(query.getUserPhone()).getData(); |
| | | if(CollectionUtils.isEmpty(tAppUsers)){ |
| | | map.put("notAccepted",0); |
| | | map.put("processing",0); |
| | | map.put("finished",0); |
| | | return map; |
| | | } |
| | | } |
| | | |
| | | query.setUserIds(tAppUsers.stream().map(TAppUser::getId).collect(Collectors.toList())); |
| | | map = this.baseMapper.statusCount(query); |
| | | return map; |
| | | } |
| | | |
| | | @Override |
| | | public List<TOrderInvoiceVO> export(TOrderInvoiceQuery query) { |
| | | List<TAppUser> tAppUsers = new ArrayList<>(); |
| | | // 查询申请用户 |
| | | if (org.springframework.util.StringUtils.hasLength(query.getUserPhone())){ |
| | | tAppUsers = appUserClient.selectByPhoneLike(query.getUserPhone()).getData(); |
| | | if(CollectionUtils.isEmpty(tAppUsers)){ |
| | | return new ArrayList<TOrderInvoiceVO>(); |
| | | } |
| | | } |
| | | query.setUserIds(tAppUsers.stream().map(TAppUser::getId).collect(Collectors.toList())); |
| | | PageInfo<TOrderInvoiceVO> pageInfo = new PageInfo<>(query.getPageCurr(),query.getPageSize()); |
| | | List<TOrderInvoiceVO> list = this.baseMapper.export(query); |
| | | List<Long> ids = list.stream().map(TOrderInvoiceVO::getId).collect(Collectors.toList()); |
| | | List<TOrderInvoiceDetail> orderInvoiceDetailList = orderInvoiceDetailService.list(new LambdaQueryWrapper<TOrderInvoiceDetail>() |
| | | .in(TOrderInvoiceDetail::getOrderInvoiceId, ids)); |
| | | List<TAppUser> finalTAppUsers = tAppUsers; |
| | | list.forEach(e->{ |
| | | e.setServiceTariff(orderInvoiceDetailList.get(0).getServiceTariff()); |
| | | e.setElectricityTariff(orderInvoiceDetailList.get(0).getElectricityTariff()); |
| | | e.setAddedServiceTariff(orderInvoiceDetailList.get(0).getAddedServiceTariff()); |
| | | e.setAddedService(orderInvoiceDetailList.stream().filter(t->t.getAddedService()!=null).map(TOrderInvoiceDetail::getAddedService).reduce(BigDecimal::add).get()); |
| | | e.setElectrovalence(orderInvoiceDetailList.stream().filter(t->t.getElectrovalence()!=null).map(TOrderInvoiceDetail::getElectrovalence).reduce(BigDecimal::add).get()); |
| | | e.setServiceCharge(orderInvoiceDetailList.stream().filter(t->t.getServiceCharge()!=null).map(TOrderInvoiceDetail::getServiceCharge).reduce(BigDecimal::add).get()); |
| | | if (e.getBillingUserId()!=null){ |
| | | e.setUserPhone(finalTAppUsers.stream().filter(m->m.getId().equals(Long.parseLong(e.getBillingUserId().toString()))).findFirst().get().getPhone()); |
| | | } |
| | | }); |
| | | return list; |
| | | } |
| | | } |