TCoinOrderController.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. package com.szwl.controller;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  5. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  6. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  7. import com.fasterxml.jackson.databind.JsonNode;
  8. import com.fasterxml.jackson.databind.ObjectMapper;
  9. import com.fasterxml.jackson.databind.json.JsonMapper;
  10. import com.fasterxml.jackson.databind.node.ArrayNode;
  11. import com.fasterxml.jackson.databind.node.JsonNodeFactory;
  12. import com.fasterxml.jackson.databind.node.ObjectNode;
  13. import com.szwl.constant.AirwallexConstant;
  14. import com.szwl.constant.HuifuConstant;
  15. import com.szwl.feign.bean.SzwlFeign;
  16. import com.szwl.mapper.TCoinOrderMapper;
  17. import com.szwl.model.bo.R;
  18. import com.szwl.model.bo.ResponseModel;
  19. import com.szwl.model.entity.*;
  20. import com.szwl.service.TAirwallexMchService;
  21. import com.szwl.service.TAirwallexWalletService;
  22. import com.szwl.service.TCoinOrderService;
  23. import io.swagger.annotations.ApiOperation;
  24. import org.apache.commons.lang.StringUtils;
  25. import org.springframework.web.bind.annotation.*;
  26. import javax.annotation.Resource;
  27. import java.math.BigDecimal;
  28. import java.math.RoundingMode;
  29. import java.util.Iterator;
  30. import java.util.List;
  31. import java.util.Objects;
  32. /**
  33. * <p>
  34. * 前端控制器
  35. * </p>
  36. *
  37. * @author wuhs
  38. * @since 2023-08-29
  39. */
  40. @RestController
  41. @RequestMapping("/tCoinOrder")
  42. public class TCoinOrderController {
  43. TCoinOrderService coinOrderService;
  44. SzwlFeign szwlFeign;
  45. TAirwallexWalletService airwallexWalletService;
  46. @Resource
  47. TCoinOrderMapper coinOrderMapper;
  48. public TCoinOrderController(TCoinOrderService coinOrderService, SzwlFeign szwlFeign, TAirwallexWalletService airwallexWalletService) {
  49. this.coinOrderService = coinOrderService;
  50. this.szwlFeign = szwlFeign;
  51. this.airwallexWalletService = airwallexWalletService;
  52. }
  53. @ApiOperation(value = "查询某范围内的设备的订单总和")
  54. @GetMapping("/sumPriceLatLon")
  55. public ResponseModel<?> sumPriceLatLon(String latitude1, String latitude2, String longitude1, String longitude2){
  56. if (StringUtils.isEmpty(latitude1) || StringUtils.isEmpty(latitude2) || StringUtils.isEmpty(longitude1) || StringUtils.isEmpty(longitude2)) {
  57. return R.fail("参数不能为空");
  58. }
  59. // 根据经纬度查设备信息
  60. List<TEquipment> equipmentsByLatLon = R.getDataIfSuccess(szwlFeign.getEquipmentByLatLon(latitude1, latitude2, longitude1, longitude2));
  61. if (equipmentsByLatLon.isEmpty()) {
  62. return R.ok(0);
  63. }
  64. BigDecimal sumPrice = new BigDecimal(0);
  65. for (TEquipment equipment : equipmentsByLatLon) {
  66. String clientId = equipment.getClientId();
  67. // 根据clientId查订单总额
  68. BigDecimal priceByClientId = coinOrderMapper.sumPriceByClientId(clientId);
  69. if (Objects.nonNull(priceByClientId)) {
  70. sumPrice = sumPrice.add(priceByClientId);
  71. }
  72. }
  73. return R.ok(equipmentsByLatLon.size() + "台设备的总销售额是:" + sumPrice);
  74. }
  75. @ApiOperation(value = "添加海外订单")
  76. @PostMapping("/addCoinOrder")
  77. public ResponseModel<?> addCoinOrder(@RequestBody TCoinOrder coinOrder) {
  78. boolean save = coinOrderService.save(coinOrder);
  79. return R.ok(save);
  80. }
  81. /**
  82. * 获取 Airwallex 分销明细
  83. *
  84. * @param proportion
  85. * @param price
  86. * @return
  87. */
  88. public ObjectNode getDistributionDetails(TProportion proportion, BigDecimal price) {
  89. // 获取分账有几方:0:分账方2个,1:分账方3个,2:分账方4个,3:分账方超4个
  90. Integer type = proportion.getType();
  91. // 空中云汇 分账明细参数
  92. // ArrayNode altInfos = new ObjectMapper().createArrayNode();
  93. ArrayNode altInfos = JsonNodeFactory.instance.arrayNode();
  94. ObjectNode distributionDetails = JsonNodeFactory.instance.objectNode();
  95. BigDecimal airProportion = null, // 空中云汇平台分销百分比,假设是0
  96. selfProportion = null, // 商家自己的分销百分比
  97. agencyProportion = null, // 一级分销商分销百分比
  98. merchantProportion = null; // 二级分销商分销百分比
  99. BigDecimal airAmount = null, // 空中云汇的钱,假设是0
  100. selfAmount = null, // 商家自己所得的钱
  101. agencyAmount = null, // 一级分销商所得的钱
  102. merchantAmount = null; // 二级分销商所得的钱
  103. TAirwallexWallet selfAirWallet = null, // 商家收款信息
  104. agencyAirWallet = null, // 一级分销商收款信息
  105. merchantAirWallet = null; // 二级分销商收款信息
  106. ObjectNode airJson = null, // 空中云汇
  107. selfJson = null, // 商家
  108. agencyJson = null, // 一级
  109. merchantJson = null; // 二级
  110. // 取消平台扣手续费,将price四舍五入保留2位小数
  111. BigDecimal cutPrice = price.setScale(2, RoundingMode.HALF_UP);
  112. BigDecimal refuseAmount = new BigDecimal(0.00);
  113. switch (type) {
  114. case 0: // 分账方有2个:申泽admin + 商家proportion
  115. // 目前都暂定空中云汇平台分销为 0
  116. // airProportion = proportion.getAdminProportion();
  117. // 申泽的分销 eg.1% 不显示
  118. // adminProportion = proportion.getAdminProportion();
  119. // 商家自己的分销
  120. selfProportion = proportion.getProportion();
  121. // 平台分销获得利润
  122. // airAmount = cutPrice.multiply(airProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_UP);
  123. // 商家分销获得利润,五舍六入
  124. selfAmount = cutPrice.multiply(selfProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  125. // Airwallex 平台收款信息表
  126. // Long adminId = proportion.getAdminId();
  127. LambdaQueryWrapper<TAirwallexWallet> wrapper = Wrappers.lambdaQuery();
  128. wrapper.eq(TAirwallexWallet::getAdminId, proportion.getAdminId());
  129. selfAirWallet = airwallexWalletService.getOne(wrapper);
  130. // 平台分销
  131. // airJson = JsonNodeFactory.instance.objectNode();
  132. // // 分账方受益人编号
  133. // airJson.put("altMchId", selfAirMch.getBeneficiaryId());
  134. // // 受益人所分金额,单位美元$
  135. // airJson.put("altAirAmount", airAmount.toString());
  136. // altInfos.add(airJson);
  137. // 商家分销
  138. selfJson = JsonNodeFactory.instance.objectNode();
  139. selfJson.put("airBeneId", selfAirWallet.getAdminId());
  140. selfJson.put("airAmount", selfAmount.toString());
  141. altInfos.add(selfJson);
  142. break;
  143. case 1: // 分账方有3个:申泽admin + 商家proportion + 商家一级agency
  144. // 商家自己的分销
  145. selfProportion = proportion.getProportion();
  146. // 一级代理的分销
  147. agencyProportion = proportion.getAgencyProportion();
  148. // 商家分销获得利润
  149. selfAmount = cutPrice.multiply(selfProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  150. // Airwallex 平台收款信息表
  151. agencyAirWallet = airwallexWalletService.getAirWallet(proportion.getAgencyId());
  152. selfAirWallet = airwallexWalletService.getAirWallet(proportion.getAdminId());
  153. if (!(agencyProportion.compareTo(refuseAmount) == 0 || agencyProportion == null)) {
  154. // 一级代理获得利润
  155. agencyAmount = cutPrice.multiply(agencyProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  156. agencyJson = JsonNodeFactory.instance.objectNode();
  157. agencyJson.put("airBeneId", agencyAirWallet.getAdminId());
  158. agencyJson.put("airAmount", agencyAmount.toString());
  159. altInfos.add(agencyJson);
  160. }
  161. // 商家自己获得利润
  162. selfJson = JsonNodeFactory.instance.objectNode();
  163. selfJson.put("airBeneId", selfAirWallet.getAdminId());
  164. selfJson.put("airAmount", selfAmount.toString());
  165. altInfos.add(selfJson);
  166. break;
  167. case 2: // 分账方有4个:申泽admin + 商家self + 商家一级agency + 商家二级merchant
  168. // 商家自己的分销
  169. selfProportion = proportion.getProportion();
  170. // 一级代理的分销
  171. agencyProportion = proportion.getAgencyProportion();
  172. // 二级代理的分销
  173. merchantProportion = proportion.getMerchantProportion();
  174. // 商家分销获得利润,五舍六入
  175. selfAmount = cutPrice.multiply(selfProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  176. // Airwallex 平台收款信息表
  177. selfAirWallet = airwallexWalletService.getAirWallet(proportion.getAdminId());
  178. agencyAirWallet = airwallexWalletService.getAirWallet(proportion.getAgencyId());
  179. merchantAirWallet = airwallexWalletService.getAirWallet(proportion.getMerchantId());
  180. if (!(agencyProportion.compareTo(refuseAmount) == 0 || agencyProportion == null)) {
  181. // 一级代理获得利润
  182. agencyAmount = cutPrice.multiply(agencyProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  183. agencyJson = JsonNodeFactory.instance.objectNode();
  184. agencyJson.put("airBeneId", agencyAirWallet.getAdminId());
  185. agencyJson.put("airAmount", agencyAmount.toString());
  186. altInfos.add(agencyJson);
  187. }
  188. if (!(merchantProportion.compareTo(refuseAmount) == 0 || merchantProportion == null)) {
  189. // 二级经销商获得利润
  190. merchantAmount = cutPrice.multiply(merchantProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  191. merchantJson = JsonNodeFactory.instance.objectNode();
  192. merchantJson.put("airBeneId", merchantAirWallet.getAdminId());
  193. merchantJson.put("airAmount", merchantAmount.toString());
  194. altInfos.add(merchantJson);
  195. }
  196. // 商家自己获得利润
  197. selfJson = JsonNodeFactory.instance.objectNode();
  198. selfJson.put("airBeneId", selfAirWallet.getAdminId());
  199. selfJson.put("airAmount", selfAmount.toString());
  200. altInfos.add(selfJson);
  201. break;
  202. case 3: // 分账方超过4个:申泽admin + 商家self + 商家一级agency + 商家二级merchant + 商家三级personage
  203. // personageProportion = proportion.getPersonageProportion();
  204. break;
  205. }
  206. // 计算airAmount的总和
  207. BigDecimal sumAmount = BigDecimal.ZERO;
  208. Iterator<JsonNode> iterator = altInfos.iterator();
  209. while (iterator.hasNext()) {
  210. JsonNode jsonNode = iterator.next();
  211. String airAmounts = jsonNode.get("airAmount").asText();
  212. if (airAmounts.equals("0.00")) {
  213. iterator.remove();
  214. }
  215. if (jsonNode.has("airAmount")) {
  216. BigDecimal airAmountSum = new BigDecimal(jsonNode.get("airAmount").asText());
  217. sumAmount = sumAmount.add(airAmountSum);
  218. }
  219. }
  220. // 如果sumAmount大于原本price
  221. if (sumAmount.compareTo(price) > 0) {
  222. BigDecimal subtract = sumAmount.subtract(price);
  223. Iterator<JsonNode> iterator1 = altInfos.iterator();
  224. while (iterator1.hasNext()) {
  225. JsonNode jsonNode = iterator1.next();
  226. String airBeneId = jsonNode.get("airBeneId").asText();
  227. // String airBeneId1 = jsonNode.get("airBeneId").toString();
  228. if (airBeneId.equals(AirwallexConstant.clientid)) {
  229. BigDecimal airAmount1 = new BigDecimal(jsonNode.get("airAmount").asText());
  230. BigDecimal fixAirAmount = airAmount1.subtract(subtract).max(BigDecimal.ZERO);
  231. // ObjectMapper objectMapper = new ObjectMapper();
  232. // ObjectNode objectNode = objectMapper.convertValue(jsonNode, ObjectNode.class);
  233. // objectNode.put("airAmount", fixAirAmount.toString());
  234. ((ObjectNode) jsonNode).put("airAmount", fixAirAmount.toString());
  235. break;
  236. }
  237. }
  238. }
  239. distributionDetails.put("alt_infos", altInfos);
  240. return distributionDetails;
  241. }
  242. }