123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- package com.szwl.controller;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
- import com.baomidou.mybatisplus.core.toolkit.Wrappers;
- import com.fasterxml.jackson.databind.JsonNode;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import com.fasterxml.jackson.databind.json.JsonMapper;
- import com.fasterxml.jackson.databind.node.ArrayNode;
- import com.fasterxml.jackson.databind.node.JsonNodeFactory;
- import com.fasterxml.jackson.databind.node.ObjectNode;
- import com.szwl.constant.AirwallexConstant;
- import com.szwl.constant.HuifuConstant;
- import com.szwl.feign.bean.SzwlFeign;
- import com.szwl.mapper.TCoinOrderMapper;
- import com.szwl.model.bo.R;
- import com.szwl.model.bo.ResponseModel;
- import com.szwl.model.entity.*;
- import com.szwl.service.TAirwallexMchService;
- import com.szwl.service.TAirwallexWalletService;
- import com.szwl.service.TCoinOrderService;
- import io.swagger.annotations.ApiOperation;
- import org.apache.commons.lang.StringUtils;
- import org.springframework.web.bind.annotation.*;
- import javax.annotation.Resource;
- import java.math.BigDecimal;
- import java.math.RoundingMode;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Objects;
- /**
- * <p>
- * 前端控制器
- * </p>
- *
- * @author wuhs
- * @since 2023-08-29
- */
- @RestController
- @RequestMapping("/tCoinOrder")
- public class TCoinOrderController {
- TCoinOrderService coinOrderService;
- SzwlFeign szwlFeign;
- TAirwallexWalletService airwallexWalletService;
- @Resource
- TCoinOrderMapper coinOrderMapper;
- public TCoinOrderController(TCoinOrderService coinOrderService, SzwlFeign szwlFeign, TAirwallexWalletService airwallexWalletService) {
- this.coinOrderService = coinOrderService;
- this.szwlFeign = szwlFeign;
- this.airwallexWalletService = airwallexWalletService;
- }
- @ApiOperation(value = "查询某范围内的设备的订单总和")
- @GetMapping("/sumPriceLatLon")
- public ResponseModel<?> sumPriceLatLon(String latitude1, String latitude2, String longitude1, String longitude2){
- if (StringUtils.isEmpty(latitude1) || StringUtils.isEmpty(latitude2) || StringUtils.isEmpty(longitude1) || StringUtils.isEmpty(longitude2)) {
- return R.fail("参数不能为空");
- }
- // 根据经纬度查设备信息
- List<TEquipment> equipmentsByLatLon = R.getDataIfSuccess(szwlFeign.getEquipmentByLatLon(latitude1, latitude2, longitude1, longitude2));
- if (equipmentsByLatLon.isEmpty()) {
- return R.ok(0);
- }
- BigDecimal sumPrice = new BigDecimal(0);
- for (TEquipment equipment : equipmentsByLatLon) {
- String clientId = equipment.getClientId();
- // 根据clientId查订单总额
- BigDecimal priceByClientId = coinOrderMapper.sumPriceByClientId(clientId);
- if (Objects.nonNull(priceByClientId)) {
- sumPrice = sumPrice.add(priceByClientId);
- }
- }
- return R.ok(equipmentsByLatLon.size() + "台设备的总销售额是:" + sumPrice);
- }
- @ApiOperation(value = "添加海外订单")
- @PostMapping("/addCoinOrder")
- public ResponseModel<?> addCoinOrder(@RequestBody TCoinOrder coinOrder) {
- boolean save = coinOrderService.save(coinOrder);
- return R.ok(save);
- }
- /**
- * 获取 Airwallex 分销明细
- *
- * @param proportion
- * @param price
- * @return
- */
- public ObjectNode getDistributionDetails(TProportion proportion, BigDecimal price) {
- // 获取分账有几方:0:分账方2个,1:分账方3个,2:分账方4个,3:分账方超4个
- Integer type = proportion.getType();
- // 空中云汇 分账明细参数
- // ArrayNode altInfos = new ObjectMapper().createArrayNode();
- ArrayNode altInfos = JsonNodeFactory.instance.arrayNode();
- ObjectNode distributionDetails = JsonNodeFactory.instance.objectNode();
- BigDecimal airProportion = null, // 空中云汇平台分销百分比,假设是0
- selfProportion = null, // 商家自己的分销百分比
- agencyProportion = null, // 一级分销商分销百分比
- merchantProportion = null; // 二级分销商分销百分比
- BigDecimal airAmount = null, // 空中云汇的钱,假设是0
- selfAmount = null, // 商家自己所得的钱
- agencyAmount = null, // 一级分销商所得的钱
- merchantAmount = null; // 二级分销商所得的钱
- TAirwallexWallet selfAirWallet = null, // 商家收款信息
- agencyAirWallet = null, // 一级分销商收款信息
- merchantAirWallet = null; // 二级分销商收款信息
- ObjectNode airJson = null, // 空中云汇
- selfJson = null, // 商家
- agencyJson = null, // 一级
- merchantJson = null; // 二级
- // 取消平台扣手续费,将price四舍五入保留2位小数
- BigDecimal cutPrice = price.setScale(2, RoundingMode.HALF_UP);
- BigDecimal refuseAmount = new BigDecimal(0.00);
- switch (type) {
- case 0: // 分账方有2个:申泽admin + 商家proportion
- // 目前都暂定空中云汇平台分销为 0
- // airProportion = proportion.getAdminProportion();
- // 申泽的分销 eg.1% 不显示
- // adminProportion = proportion.getAdminProportion();
- // 商家自己的分销
- selfProportion = proportion.getProportion();
- // 平台分销获得利润
- // airAmount = cutPrice.multiply(airProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_UP);
- // 商家分销获得利润,五舍六入
- selfAmount = cutPrice.multiply(selfProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
- // Airwallex 平台收款信息表
- // Long adminId = proportion.getAdminId();
- LambdaQueryWrapper<TAirwallexWallet> wrapper = Wrappers.lambdaQuery();
- wrapper.eq(TAirwallexWallet::getAdminId, proportion.getAdminId());
- selfAirWallet = airwallexWalletService.getOne(wrapper);
- // 平台分销
- // airJson = JsonNodeFactory.instance.objectNode();
- // // 分账方受益人编号
- // airJson.put("altMchId", selfAirMch.getBeneficiaryId());
- // // 受益人所分金额,单位美元$
- // airJson.put("altAirAmount", airAmount.toString());
- // altInfos.add(airJson);
- // 商家分销
- selfJson = JsonNodeFactory.instance.objectNode();
- selfJson.put("airBeneId", selfAirWallet.getAdminId());
- selfJson.put("airAmount", selfAmount.toString());
- altInfos.add(selfJson);
- break;
- case 1: // 分账方有3个:申泽admin + 商家proportion + 商家一级agency
- // 商家自己的分销
- selfProportion = proportion.getProportion();
- // 一级代理的分销
- agencyProportion = proportion.getAgencyProportion();
- // 商家分销获得利润
- selfAmount = cutPrice.multiply(selfProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
- // Airwallex 平台收款信息表
- agencyAirWallet = airwallexWalletService.getAirWallet(proportion.getAgencyId());
- selfAirWallet = airwallexWalletService.getAirWallet(proportion.getAdminId());
- if (!(agencyProportion.compareTo(refuseAmount) == 0 || agencyProportion == null)) {
- // 一级代理获得利润
- agencyAmount = cutPrice.multiply(agencyProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
- agencyJson = JsonNodeFactory.instance.objectNode();
- agencyJson.put("airBeneId", agencyAirWallet.getAdminId());
- agencyJson.put("airAmount", agencyAmount.toString());
- altInfos.add(agencyJson);
- }
- // 商家自己获得利润
- selfJson = JsonNodeFactory.instance.objectNode();
- selfJson.put("airBeneId", selfAirWallet.getAdminId());
- selfJson.put("airAmount", selfAmount.toString());
- altInfos.add(selfJson);
- break;
- case 2: // 分账方有4个:申泽admin + 商家self + 商家一级agency + 商家二级merchant
- // 商家自己的分销
- selfProportion = proportion.getProportion();
- // 一级代理的分销
- agencyProportion = proportion.getAgencyProportion();
- // 二级代理的分销
- merchantProportion = proportion.getMerchantProportion();
- // 商家分销获得利润,五舍六入
- selfAmount = cutPrice.multiply(selfProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
- // Airwallex 平台收款信息表
- selfAirWallet = airwallexWalletService.getAirWallet(proportion.getAdminId());
- agencyAirWallet = airwallexWalletService.getAirWallet(proportion.getAgencyId());
- merchantAirWallet = airwallexWalletService.getAirWallet(proportion.getMerchantId());
- if (!(agencyProportion.compareTo(refuseAmount) == 0 || agencyProportion == null)) {
- // 一级代理获得利润
- agencyAmount = cutPrice.multiply(agencyProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
- agencyJson = JsonNodeFactory.instance.objectNode();
- agencyJson.put("airBeneId", agencyAirWallet.getAdminId());
- agencyJson.put("airAmount", agencyAmount.toString());
- altInfos.add(agencyJson);
- }
- if (!(merchantProportion.compareTo(refuseAmount) == 0 || merchantProportion == null)) {
- // 二级经销商获得利润
- merchantAmount = cutPrice.multiply(merchantProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
- merchantJson = JsonNodeFactory.instance.objectNode();
- merchantJson.put("airBeneId", merchantAirWallet.getAdminId());
- merchantJson.put("airAmount", merchantAmount.toString());
- altInfos.add(merchantJson);
- }
- // 商家自己获得利润
- selfJson = JsonNodeFactory.instance.objectNode();
- selfJson.put("airBeneId", selfAirWallet.getAdminId());
- selfJson.put("airAmount", selfAmount.toString());
- altInfos.add(selfJson);
- break;
- case 3: // 分账方超过4个:申泽admin + 商家self + 商家一级agency + 商家二级merchant + 商家三级personage
- // personageProportion = proportion.getPersonageProportion();
- break;
- }
- // 计算airAmount的总和
- BigDecimal sumAmount = BigDecimal.ZERO;
- Iterator<JsonNode> iterator = altInfos.iterator();
- while (iterator.hasNext()) {
- JsonNode jsonNode = iterator.next();
- String airAmounts = jsonNode.get("airAmount").asText();
- if (airAmounts.equals("0.00")) {
- iterator.remove();
- }
- if (jsonNode.has("airAmount")) {
- BigDecimal airAmountSum = new BigDecimal(jsonNode.get("airAmount").asText());
- sumAmount = sumAmount.add(airAmountSum);
- }
- }
- // 如果sumAmount大于原本price
- if (sumAmount.compareTo(price) > 0) {
- BigDecimal subtract = sumAmount.subtract(price);
- Iterator<JsonNode> iterator1 = altInfos.iterator();
- while (iterator1.hasNext()) {
- JsonNode jsonNode = iterator1.next();
- String airBeneId = jsonNode.get("airBeneId").asText();
- // String airBeneId1 = jsonNode.get("airBeneId").toString();
- if (airBeneId.equals(AirwallexConstant.clientid)) {
- BigDecimal airAmount1 = new BigDecimal(jsonNode.get("airAmount").asText());
- BigDecimal fixAirAmount = airAmount1.subtract(subtract).max(BigDecimal.ZERO);
- // ObjectMapper objectMapper = new ObjectMapper();
- // ObjectNode objectNode = objectMapper.convertValue(jsonNode, ObjectNode.class);
- // objectNode.put("airAmount", fixAirAmount.toString());
- ((ObjectNode) jsonNode).put("airAmount", fixAirAmount.toString());
- break;
- }
- }
- }
- distributionDetails.put("alt_infos", altInfos);
- return distributionDetails;
- }
- }
|