TOrderController.java 176 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844
  1. package com.szwl.controller;
  2. import cn.com.crbank.ommo.esUtil.BeanUtils;
  3. import cn.hutool.core.bean.BeanUtil;
  4. import cn.hutool.core.util.StrUtil;
  5. import com.alibaba.fastjson.JSON;
  6. import com.alibaba.fastjson.JSONArray;
  7. import com.alibaba.fastjson.JSONObject;
  8. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  9. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  10. import com.google.zxing.BarcodeFormat;
  11. import com.google.zxing.EncodeHintType;
  12. import com.google.zxing.client.j2se.MatrixToImageWriter;
  13. import com.google.zxing.common.BitMatrix;
  14. import com.google.zxing.qrcode.QRCodeWriter;
  15. import com.szwl.constant.HuifuConstant;
  16. import com.szwl.constant.JoinpayConstant;
  17. import com.szwl.constant.ResponseCodesEnum;
  18. import com.szwl.exception.BizException;
  19. import com.szwl.feign.bean.OrderFeign;
  20. import com.szwl.feign.bean.SzwlFeign;
  21. import com.szwl.model.bo.JsonMessage;
  22. import com.szwl.model.bo.R;
  23. import com.szwl.model.bo.ResponseModel;
  24. import com.szwl.model.dto.OrderDetailsDTO;
  25. import com.szwl.model.entity.*;
  26. import com.szwl.model.utils.*;
  27. import com.szwl.service.*;
  28. import com.szwl.service.es.EsTOrderService;
  29. import com.szwl.utils.HuifuUtils;
  30. import com.szwl.utils.IDGenerator;
  31. import io.swagger.annotations.ApiOperation;
  32. import lombok.extern.slf4j.Slf4j;
  33. import org.apache.commons.lang.StringUtils;
  34. import org.springframework.beans.factory.annotation.Autowired;
  35. import org.springframework.web.bind.annotation.*;
  36. import org.springframework.web.servlet.view.RedirectView;
  37. import javax.servlet.http.HttpServletRequest;
  38. import javax.servlet.http.HttpServletResponse;
  39. import java.io.ByteArrayOutputStream;
  40. import java.io.IOException;
  41. import java.io.UnsupportedEncodingException;
  42. import java.math.BigDecimal;
  43. import java.math.RoundingMode;
  44. import java.net.URLDecoder;
  45. import java.text.ParseException;
  46. import java.text.SimpleDateFormat;
  47. import java.util.*;
  48. /**
  49. * <p>
  50. * 前端控制器
  51. * </p>
  52. *
  53. * @author wuhs
  54. * @since 2022-06-17
  55. */
  56. @Slf4j
  57. @RestController
  58. @RequestMapping("/tOrder")
  59. public class TOrderController {
  60. @Autowired
  61. SzwlFeign szwlFeign;
  62. @Autowired
  63. OrderFeign orderFeign;
  64. @Autowired
  65. TOrderService orderService;
  66. @Autowired
  67. TOrderTaskService orderTaskService;
  68. @Autowired
  69. TJoinpayMchService joinpayMchService;
  70. @Autowired
  71. TShandeMchService shandeMchService;
  72. @Autowired
  73. THuifuMchService huifuMchService;
  74. @Autowired
  75. TEquipmentService equipmentService;
  76. @Autowired
  77. EsTOrderService esTOrderService;
  78. @Autowired
  79. TSzsmWxService szsmWxService;
  80. @Autowired
  81. private THuifuTempOrderService huifuTempOrderService;
  82. @Autowired
  83. private TOrderDetailsService orderDetailsService;
  84. @ApiOperation(value = "测试feign")
  85. @GetMapping("/testFeign")
  86. public ResponseModel<?> testFeign(String id) {
  87. return szwlFeign.testGetAdmin(id);
  88. }
  89. /**
  90. * 合并付款接口,根据终端跳转微信或支付宝
  91. * @param req
  92. * @param resp
  93. * @return
  94. * @throws Exception
  95. */
  96. @ApiOperation(value = "请求在线支付 主扫")
  97. @RequestMapping("/aliPay")
  98. public RedirectView alipayforward(String wx, String zfb, HttpServletRequest req, HttpServletResponse resp) throws Exception {
  99. String userAgent = req.getHeader("User-Agent");
  100. RedirectView redirectTarget = new RedirectView();
  101. redirectTarget.setContextRelative(true);
  102. if (userAgent.contains("MicroMessenger")){ // 微信
  103. redirectTarget.setUrl(wx);
  104. }else if(userAgent.contains("AlipayClient")){ // 支付宝
  105. redirectTarget.setUrl(zfb);
  106. }else{
  107. throw new BizException(ResponseCodesEnum.A0001,"请使用支付宝或微信扫码!");
  108. }
  109. return redirectTarget;
  110. }
  111. /**
  112. * 请求在线支付 主扫 优惠码 盲盒
  113. *
  114. * @param clientId 设备client id
  115. * @param productName 商品名称
  116. * @param frpCode 支付方式 支付宝:ALIPAY_NATIVE , 微信:WEIXIN_NATIVE
  117. * @param pri 有值代表是盲盒
  118. * @return
  119. */
  120. @ApiOperation(value = "请求在线支付 主扫")
  121. @GetMapping("/uniPay")
  122. public Object uniPay(String code, String clientId, String productName, String frpCode,String pri) {
  123. TEquipment equipment = R.getDataIfSuccess(szwlFeign.findEquipmentByClientId(clientId));
  124. if(equipment==null||equipment.getId()==null){
  125. return JsonMessage.error("找不到设备");
  126. }
  127. //验证优惠码 code=null 则不选用优惠码
  128. TPromoCode promoCode = new TPromoCode();
  129. if(StringUtils.isNotEmpty(code)){
  130. //有优惠码,需要对优惠码进行验证
  131. ResponseModel<TPromoCode> tPromoCodeResponseModel = szwlFeign.selectTPromoCode(code, equipment.getAdminId().toString());
  132. TPromoCode tPromoCode = tPromoCodeResponseModel.getData();
  133. promoCode = tPromoCode;
  134. if (tPromoCode == null||tPromoCode.getId()==null) {
  135. //不存在
  136. return JsonMessage.success("1");
  137. }
  138. if (promoCode.getId() == null) {
  139. //不存在
  140. return JsonMessage.success("1");
  141. }
  142. Date lastUseDate = null;
  143. if (promoCode != null&&promoCode.getId()!=null) {
  144. lastUseDate = promoCode.getLastUseDate();
  145. }
  146. if (lastUseDate != null && lastUseDate.getTime() < ((new Date()).getTime())) {
  147. promoCode.setIsUse("2");
  148. szwlFeign.updatePromoCode(String.valueOf(promoCode.getId()),"2");
  149. //过期
  150. return JsonMessage.success("4");
  151. }
  152. if (promoCode.getIsUse().equals("1")) {
  153. //被使用
  154. return JsonMessage.success("2");
  155. }
  156. if(!promoCode.getAdminId().equals("1")){
  157. if (String.valueOf(equipment.getAdminId()).equals(promoCode.getAdminId())) {
  158. } else {
  159. //不是本机
  160. return JsonMessage.success("3");
  161. }
  162. }
  163. if (promoCode.getDiscount() == null) {
  164. //旧优惠码
  165. return JsonMessage.success("0");
  166. }
  167. if (promoCode.getDiscount() != null && promoCode.getDiscount() == 0) {
  168. //0折
  169. return JsonMessage.success("0");
  170. }
  171. }
  172. // 支付方式
  173. if (!frpCode.equals("ALIPAY_NATIVE") && !frpCode.equals("WEIXIN_NATIVE")) {
  174. return JsonMessage.error("参数错误");
  175. }
  176. Long equipmentId = equipment.getId();
  177. //查找商品
  178. ResponseModel<TProduct> product1 = szwlFeign.getProduct(String.valueOf(equipmentId), productName);
  179. TProduct product = product1.getData();
  180. if (product == null) {
  181. return JsonMessage.error("找不到商品");
  182. }
  183. BigDecimal price = product.getRmbPrice();
  184. if(StringUtils.isNotEmpty(pri)){
  185. price = new BigDecimal(pri);
  186. }
  187. // 计算折扣后的价格
  188. if(StringUtils.isNotEmpty(code)){
  189. BigDecimal discount = BigDecimal.valueOf(promoCode.getDiscount());
  190. if (StringUtils.isNotEmpty(promoCode.getType()) && promoCode.getType().equals("1")) {
  191. if (price.compareTo(discount) > 0) {
  192. // 如果 price 大于 discount,从 price 中减去 discount
  193. price = price.subtract(discount).setScale(2, RoundingMode.HALF_DOWN);
  194. } else {
  195. // 如果 price 小于等于 discount,将 price 设置为 0
  196. price = BigDecimal.ZERO;
  197. }
  198. } else {
  199. price = product.getRmbPrice().multiply(discount).divide(new BigDecimal(10)).setScale(2, RoundingMode.HALF_DOWN);
  200. }
  201. }
  202. // 判断价格是否小于等于0
  203. if (BigDecimal.ZERO.compareTo(price) >= 0) {
  204. return JsonMessage.error("商品价格异常");
  205. }
  206. // 生成订单编号
  207. Long client6 = Long.parseLong(clientId.substring(clientId.length() - 6));
  208. String sn = orderService.initSn(client6);
  209. // 获取关联设备的分销
  210. TProportion proportion = R.getDataIfSuccess(szwlFeign.getProportion(String.valueOf(equipmentId)));
  211. if (proportion == null || proportion.getId() == null) {
  212. return JsonMessage.error("设备商家未完成分销设置");
  213. }
  214. TAdmin admin = R.getDataIfSuccess(szwlFeign.getAdmin(String.valueOf(equipment.getAdminId())));
  215. if (admin == null) {
  216. return JsonMessage.error("找不到设备商家");
  217. }
  218. // 支付方式:0或null为汇聚,1为汇付支付
  219. String payPlatform = admin.getPayPlatform();
  220. String companyType = admin.getCompanyType();
  221. // 汇聚分账参数
  222. JSONArray altInfo = new JSONArray();
  223. // 汇付分账明细参数
  224. JSONObject acctSplitBunch = new JSONObject();
  225. if(StringUtils.isNotEmpty(payPlatform)&&payPlatform.equals("1")){
  226. acctSplitBunch = getAcctSplitBunch(proportion, price, companyType);
  227. if (!(acctSplitBunch.size() > 0)) {
  228. return JsonMessage.error("设备商家未注册结算账号");
  229. }
  230. } else {
  231. altInfo = getAltInfo(proportion, price);
  232. if (!(altInfo.size() > 0)) {
  233. return JsonMessage.error("设备商家未注册结算账号");
  234. }
  235. }
  236. String orderNo = sn;
  237. BigDecimal amount = price.setScale(2, RoundingMode.HALF_DOWN);
  238. String productDesc = "";
  239. String commonParameter = "";
  240. String returnUrl = null;
  241. String notifyUrl = JoinpayConstant.Notify_Url;
  242. String isShowPic = "1";
  243. String openId = null;
  244. String authCode = null;
  245. String appid = null;
  246. String transactionModel = null;
  247. String tradeMerchantNo = admin.getTradeMerchantNo();
  248. String buyerId = null;
  249. String isAlt = "1";
  250. String altType = "11";
  251. String altUrl = null;
  252. BigDecimal marketingAmount = null;
  253. if(StringUtils.isNotEmpty(pri)){
  254. productName ="棉花糖";
  255. }
  256. Integer productNumber = 1;
  257. TOrder order = new TOrder();
  258. order.setId(IDGenerator.orderID());
  259. order.setCreateDate(new Date());
  260. order.setModifyDate(new Date());
  261. order.setAdminId(admin.getId());
  262. order.setSn(sn);
  263. order.setProductId(product.getId());
  264. order.setProductNo(product.getNo());
  265. order.setProductName(productName);
  266. order.setPrice(price);
  267. order.setClientId(equipment.getClientId());
  268. order.setEquipmentId(equipmentId);
  269. order.setFrpCode(frpCode);
  270. order.setAltInfo(altInfo.toString());
  271. order.setStatus(0);
  272. order.setType(admin.getType());
  273. order.setProductNumber(productNumber);
  274. order.setCompanyType(admin.getCompanyType());
  275. order.setMachineType(equipment.getMachineType());
  276. if(admin.getType()==0){
  277. order.setAdminProportion(proportion.getAdminProportion());
  278. order.setAgencyProportion(proportion.getAgencyProportion());
  279. order.setAgencyId(proportion.getAgencyId());
  280. }
  281. if(admin.getType()==1){
  282. order.setAdminProportion(proportion.getAdminProportion());
  283. order.setAgencyProportion(proportion.getAgencyProportion());
  284. order.setAgencyId(proportion.getAgencyId());
  285. order.setMerchantProportion(proportion.getMerchantProportion());
  286. order.setMerchantId(proportion.getMerchantId());
  287. }
  288. if(admin.getType()==2){
  289. order.setAdminProportion(proportion.getAdminProportion());
  290. order.setAgencyProportion(proportion.getAgencyProportion());
  291. order.setAgencyId(proportion.getAgencyId());
  292. order.setMerchantProportion(proportion.getMerchantProportion());
  293. order.setMerchantId(proportion.getMerchantId());
  294. order.setPersonageProportion(proportion.getPersonageProportion());
  295. order.setPersonageId(proportion.getPersonageId());
  296. }
  297. order.setPayPlatform(payPlatform);
  298. // 订单明细表
  299. TOrderDetails orderDetails = new TOrderDetails();
  300. orderDetails.setId(HuifuUtils.initDetailsId());
  301. orderDetails.setAdminId(admin.getId());
  302. orderDetails.setEquipmentId(equipmentId);
  303. orderDetails.setCreateDate(new Date());
  304. orderDetails.setOrderSn(sn);
  305. orderDetails.setPrice(price);
  306. orderDetails.setProductName(productName);
  307. orderDetails.setProductNo(product.getNo());
  308. orderDetails.setProductNumber(productNumber);
  309. orderDetails.setAmount(price);
  310. orderDetails.setRefundStatus("0");
  311. orderDetails.setCompanyType(companyType);
  312. orderDetails.setMachineType(equipment.getMachineType());
  313. // String client6 = clientId.substring(clientId.length() - 6);
  314. String productName1 = productName + "-" + equipment.getName() + "-" + client6;
  315. String result = null;
  316. if(StringUtils.isNotEmpty(payPlatform)&&payPlatform.equals("1")){
  317. // 汇付分账参数
  318. order.setAcctSplitBunch(acctSplitBunch.toString());
  319. if(frpCode.equals("WEIXIN_NATIVE")) {
  320. //添加到汇付临时表
  321. THuifuTempOrder tHuifuTempOrder = new THuifuTempOrder();
  322. String[] ignoreProperties = {"id"};
  323. String id = HuifuUtils.inithuifuTempId();
  324. tHuifuTempOrder.setId(id);
  325. BeanUtil.copyProperties(order,tHuifuTempOrder,ignoreProperties);
  326. huifuTempOrderService.save(tHuifuTempOrder);
  327. if (StrUtil.isNotEmpty(companyType)&&companyType.equals("1")) {
  328. result = HuifuConstant.SC_JUMP_URL+ "/#/weChatPay?id=" + id;
  329. } else {
  330. result = HuifuConstant.SZ_JUMP_URL+ "/#/weChatPay?id=" + id;
  331. }
  332. log.info("返回链接:{}", result);
  333. String qrcode = toQrcode(result);
  334. JSONObject kindData = new JSONObject();
  335. kindData.put("sn", sn);
  336. kindData.put("rd_Pic", qrcode);
  337. return JsonMessage.success(kindData.toString());
  338. }
  339. // 汇付支付
  340. try {
  341. result = huifuMchService.uniPay(
  342. orderNo, amount, productName1, productDesc,
  343. notifyUrl, frpCode, openId, appid, acctSplitBunch, companyType
  344. );
  345. } catch (Exception e) {
  346. e.printStackTrace();
  347. return JsonMessage.error("申请支付失败");
  348. }
  349. if (StrUtil.isEmpty(result)) {
  350. return JsonMessage.error("找不到支付图片");
  351. }
  352. String qrcode = toQrcode(result);
  353. JSONObject kindData = new JSONObject();
  354. kindData.put("sn", sn);
  355. kindData.put("rd_Pic", qrcode);
  356. orderService.save(order);
  357. orderDetailsService.save(orderDetails);
  358. return JsonMessage.success(kindData.toString());
  359. }else{
  360. try {
  361. result = joinpayMchService.uniPay(
  362. orderNo, amount, productName1, productDesc,
  363. commonParameter, returnUrl, notifyUrl, frpCode,
  364. isShowPic, openId, authCode, appid, transactionModel, tradeMerchantNo,
  365. buyerId, isAlt, altType, altInfo, altUrl, marketingAmount
  366. );
  367. } catch (UnsupportedEncodingException e) {
  368. e.printStackTrace();
  369. return JsonMessage.error("申请支付失败");
  370. }
  371. JSONObject resultJson = JSONObject.parseObject(result);
  372. // 汇聚支付支付申请返回支付二维码图片
  373. String rd_Pic = resultJson.getString("rd_Pic");
  374. if (resultJson == null || StringUtils.isBlank(rd_Pic)) {
  375. return JsonMessage.error("找不到支付图片");
  376. }
  377. JSONObject kindData = new JSONObject();
  378. kindData.put("sn", sn);
  379. kindData.put("rd_Pic", rd_Pic);
  380. orderService.save(order);
  381. orderDetailsService.save(orderDetails);
  382. return JsonMessage.success(kindData.toString());
  383. }
  384. }
  385. /**
  386. * 被扫支付 + 优惠码 盲盒
  387. *
  388. * @param code 优惠码
  389. * @param clientId 设备client id
  390. * @param productName 商品名称
  391. * @param frpCode 支付方式 支付宝:ALIPAY_CARD , 微信:WEIXIN_CARD
  392. * @return
  393. */
  394. @ApiOperation(value = "请求在线支付 被扫支付")
  395. @GetMapping("/CardPay")
  396. public Object CardPay(String code, String clientId, String productName, String frpCode, String authCode,String pri) {
  397. TEquipment equipment = R.getDataIfSuccess(szwlFeign.findEquipmentByClientId(clientId));
  398. if(equipment==null||equipment.getId()==null){
  399. return JsonMessage.error("找不到设备");
  400. }
  401. //验证优惠码 code=null 则不选用优惠码
  402. TPromoCode promoCode = new TPromoCode();
  403. if(StringUtils.isEmpty(code)){
  404. //没有优惠码
  405. }else {
  406. //有优惠码,需要对优惠码进行验证
  407. ResponseModel<TPromoCode> tPromoCodeResponseModel = szwlFeign.selectTPromoCode(code, equipment.getAdminId().toString());
  408. TPromoCode tPromoCode = tPromoCodeResponseModel.getData();
  409. promoCode = tPromoCode;
  410. if (tPromoCode == null||tPromoCode.getId()==null) {
  411. //不存在
  412. return JsonMessage.success("1");
  413. }
  414. if (promoCode.getId() == null) {
  415. //不存在
  416. return JsonMessage.success("1");
  417. }
  418. Date lastUseDate = null;
  419. if (promoCode != null&&promoCode.getId()!=null) {
  420. lastUseDate = promoCode.getLastUseDate();
  421. }
  422. if (lastUseDate != null && lastUseDate.getTime() < ((new Date()).getTime())) {
  423. promoCode.setIsUse("2");
  424. szwlFeign.updatePromoCode(String.valueOf(promoCode.getId()),"2");
  425. //过期
  426. return JsonMessage.success("4");
  427. }
  428. if (promoCode.getIsUse().equals("1")) {
  429. //被使用
  430. return JsonMessage.success("2");
  431. }
  432. if(!promoCode.getAdminId().equals("1")){
  433. if (String.valueOf(equipment.getAdminId()).equals(promoCode.getAdminId())) {
  434. } else {
  435. //不是本机
  436. return JsonMessage.success("3");
  437. }
  438. }
  439. if (promoCode.getDiscount() == null) {
  440. //旧优惠码
  441. return JsonMessage.success("0");
  442. }
  443. if (promoCode.getDiscount() != null && promoCode.getDiscount() == 0) {
  444. //0折
  445. return JsonMessage.success("0");
  446. }
  447. }
  448. if (!frpCode.equals("ALIPAY_CARD") && !frpCode.equals("WEIXIN_CARD")) {
  449. return JsonMessage.error("参数错误");
  450. }
  451. if (equipment == null) {
  452. return JsonMessage.error("找不到设备");
  453. }
  454. Long equipmentId = equipment.getId();
  455. //查找商品
  456. TProduct product = R.getDataIfSuccess(szwlFeign.getProduct(String.valueOf(equipmentId), productName));
  457. if (product == null) {
  458. return JsonMessage.error("找不到商品");
  459. }
  460. BigDecimal price = product.getRmbPrice();
  461. if(StringUtils.isNotEmpty(pri)){
  462. price = new BigDecimal(pri);
  463. }
  464. if(StringUtils.isNotEmpty(code)){
  465. BigDecimal discount = BigDecimal.valueOf(promoCode.getDiscount());
  466. if (StringUtils.isNotEmpty(promoCode.getType()) && promoCode.getType().equals("1")) {
  467. if (price.compareTo(discount) > 0) {
  468. // 如果 price 大于 discount,从 price 中减去 discount
  469. price = price.subtract(discount).setScale(2, RoundingMode.HALF_DOWN);
  470. } else {
  471. // 如果 price 小于等于 discount,将 price 设置为 0
  472. price = BigDecimal.ZERO;
  473. }
  474. } else {
  475. price = product.getRmbPrice().multiply(discount).divide(new BigDecimal(10)).setScale(2, RoundingMode.HALF_DOWN);
  476. }
  477. }
  478. if (BigDecimal.ZERO.compareTo(price) >= 0) {
  479. return JsonMessage.error("商品价格异常");
  480. }
  481. Long client6 = Long.parseLong(clientId.substring(clientId.length() - 6));
  482. String sn = orderService.initSn(client6);
  483. // 获取关联设备的分销
  484. TProportion proportion = R.getDataIfSuccess(szwlFeign.getProportion(String.valueOf(equipmentId)));
  485. if (proportion == null || proportion.getId() == null) {
  486. return JsonMessage.error("设备商家未完成分销设置");
  487. }
  488. TAdmin admin = R.getDataIfSuccess(szwlFeign.getAdmin(String.valueOf(equipment.getAdminId())));
  489. if (admin == null) {
  490. return JsonMessage.error("找不到设备商家");
  491. }
  492. // 支付方式:0或null为汇聚,1为汇付支付
  493. String payPlatform = admin.getPayPlatform();
  494. String companyType = admin.getCompanyType();
  495. // 汇聚分账参数
  496. JSONArray altInfo = new JSONArray();
  497. // 汇付分账明细参数
  498. JSONObject acctSplitBunch = new JSONObject();
  499. if(StringUtils.isNotEmpty(payPlatform)&&payPlatform.equals("1")){
  500. acctSplitBunch = getAcctSplitBunch(proportion, price, companyType);
  501. if (!(acctSplitBunch.size() > 0)) {
  502. return JsonMessage.error("设备商家未注册结算账号");
  503. }
  504. } else {
  505. altInfo = getAltInfo(proportion, price);
  506. if (!(altInfo.size() > 0)) {
  507. return JsonMessage.error("设备商家未注册结算账号");
  508. }
  509. }
  510. String orderNo = sn;
  511. Integer productNumber = 1;
  512. BigDecimal amount = price.setScale(2, RoundingMode.HALF_DOWN);
  513. String productDesc = "";
  514. String commonParameter = "";
  515. String returnUrl = null;
  516. String notifyUrl = JoinpayConstant.Notify_Url;
  517. String isShowPic = "1";
  518. String openId = null;
  519. String appid = null;
  520. String transactionModel = null;
  521. String tradeMerchantNo = admin.getTradeMerchantNo();
  522. String buyerId = null;
  523. String isAlt = "1";
  524. String altType = "11";
  525. String altUrl = null;
  526. if(StringUtils.isNotEmpty(pri)){
  527. productName ="棉花糖";
  528. }
  529. BigDecimal marketingAmount = null;
  530. TOrder order = new TOrder();
  531. order.setId(IDGenerator.orderID());
  532. order.setCreateDate(new Date());
  533. order.setModifyDate(new Date());
  534. order.setAdminId(admin.getId());
  535. order.setSn(sn);
  536. order.setProductId(product.getId());
  537. order.setProductNo(product.getNo());
  538. order.setProductName(productName);
  539. order.setPrice(price);
  540. order.setClientId(equipment.getClientId());
  541. order.setEquipmentId(equipmentId);
  542. order.setFrpCode(frpCode);
  543. order.setAltInfo(altInfo.toString());
  544. order.setStatus(0);
  545. order.setType(admin.getType());
  546. order.setProductNumber(productNumber);
  547. order.setCompanyType(admin.getCompanyType());
  548. order.setMachineType(equipment.getMachineType());
  549. if(admin.getType()==0){
  550. order.setAdminProportion(proportion.getAdminProportion());
  551. order.setAgencyProportion(proportion.getAgencyProportion());
  552. order.setAgencyId(proportion.getAgencyId());
  553. }
  554. if(admin.getType()==1){
  555. order.setAdminProportion(proportion.getAdminProportion());
  556. order.setAgencyProportion(proportion.getAgencyProportion());
  557. order.setAgencyId(proportion.getAgencyId());
  558. order.setMerchantProportion(proportion.getMerchantProportion());
  559. order.setMerchantId(proportion.getMerchantId());
  560. }
  561. if(admin.getType()==2){
  562. order.setAdminProportion(proportion.getAdminProportion());
  563. order.setAgencyProportion(proportion.getAgencyProportion());
  564. order.setAgencyId(proportion.getAgencyId());
  565. order.setMerchantProportion(proportion.getMerchantProportion());
  566. order.setMerchantId(proportion.getMerchantId());
  567. order.setPersonageProportion(proportion.getPersonageProportion());
  568. order.setPersonageId(proportion.getPersonageId());
  569. }
  570. order.setPayPlatform(payPlatform);
  571. if(StringUtils.isNotEmpty(payPlatform)&&payPlatform.equals("1")){
  572. order.setIsSettlement("0");
  573. }
  574. // 订单明细表
  575. TOrderDetails orderDetails = new TOrderDetails();
  576. orderDetails.setId(HuifuUtils.initDetailsId());
  577. orderDetails.setAdminId(admin.getId());
  578. orderDetails.setEquipmentId(equipmentId);
  579. orderDetails.setCreateDate(new Date());
  580. orderDetails.setOrderSn(sn);
  581. orderDetails.setPrice(price);
  582. orderDetails.setProductName(productName);
  583. orderDetails.setProductNo(product.getNo());
  584. orderDetails.setProductNumber(productNumber);
  585. orderDetails.setAmount(price);
  586. orderDetails.setRefundStatus("0");
  587. orderDetails.setCompanyType(companyType);
  588. orderDetails.setMachineType(equipment.getMachineType());
  589. // String client6 = clientId.substring(clientId.length() - 6);
  590. String productName1 = productName + "-" + equipment.getName() + "-" + client6;
  591. String result = null;
  592. if(StringUtils.isNotEmpty(payPlatform)&&payPlatform.equals("1")){
  593. order.setAcctSplitBunch(acctSplitBunch.toString());
  594. try {
  595. result = huifuMchService.cardPay(
  596. orderNo, amount, productName1, notifyUrl, authCode, acctSplitBunch, companyType
  597. );
  598. } catch (Exception e) {
  599. e.printStackTrace();
  600. return JsonMessage.error("申请支付失败");
  601. }
  602. if(result.equals(HuifuConstant.RESP_CODE_0)) {
  603. orderService.save(order);
  604. orderDetailsService.save(orderDetails);
  605. JSONObject kindData = new JSONObject();
  606. kindData.put("sn", sn);
  607. return JsonMessage.success(kindData.toString());
  608. } else {
  609. return JsonMessage.error("申请支付失败");
  610. }
  611. }else{
  612. try {
  613. result = joinpayMchService.uniPay(
  614. orderNo, amount, productName1, productDesc,
  615. commonParameter, returnUrl, notifyUrl, frpCode,
  616. isShowPic, openId, authCode, appid, transactionModel, tradeMerchantNo,
  617. buyerId, isAlt, altType, altInfo, altUrl, marketingAmount
  618. );
  619. } catch (UnsupportedEncodingException e) {
  620. e.printStackTrace();
  621. return JsonMessage.error("申请支付失败");
  622. }
  623. JSONObject resultJson = JSONObject.parseObject(result);
  624. String raCode = resultJson.getString("ra_Code");
  625. if(raCode.equals("100") || raCode.equals("102")){
  626. orderService.save(order);
  627. orderDetailsService.save(orderDetails);
  628. JSONObject kindData = new JSONObject();
  629. kindData.put("sn", sn);
  630. return JsonMessage.success(kindData.toString());
  631. }else {
  632. return JsonMessage.error("申请支付失败");
  633. }
  634. }
  635. }
  636. /**
  637. * 请求在线支付 主扫 优惠码 二码合一
  638. *
  639. * @param clientId 设备client id
  640. * @param productName 商品名称
  641. * @param pri DIY花型价格
  642. * @return
  643. */
  644. @ApiOperation(value = "请求在线支付 二码合一")
  645. @GetMapping("/twoPayCode")
  646. public Object twoPayCode(String code, String clientId, String productName, String pri) {
  647. TEquipment equipment = R.getDataIfSuccess(szwlFeign.findEquipmentByClientId(clientId));
  648. if(equipment==null||equipment.getId()==null){
  649. return JsonMessage.error("找不到设备");
  650. }
  651. //验证优惠码 code=null 则不选用优惠码
  652. TPromoCode promoCode = new TPromoCode();
  653. if(StringUtils.isEmpty(code)){
  654. //没有优惠码
  655. }else {
  656. //有优惠码,需要对优惠码进行验证
  657. ResponseModel<TPromoCode> tPromoCodeResponseModel = szwlFeign.selectTPromoCode(code, equipment.getAdminId().toString());
  658. TPromoCode tPromoCode = tPromoCodeResponseModel.getData();
  659. promoCode = tPromoCode;
  660. if (tPromoCode == null||tPromoCode.getId()==null) {
  661. //不存在
  662. return JsonMessage.success("1");
  663. }
  664. if (promoCode.getId() == null) {
  665. //不存在
  666. return JsonMessage.success("1");
  667. }
  668. Date lastUseDate = null;
  669. if (promoCode != null&&promoCode.getId()!=null) {
  670. lastUseDate = promoCode.getLastUseDate();
  671. }
  672. if (lastUseDate != null && lastUseDate.getTime() < ((new Date()).getTime())) {
  673. promoCode.setIsUse("2");
  674. szwlFeign.updatePromoCode(String.valueOf(promoCode.getId()),"2");
  675. //过期
  676. return JsonMessage.success("4");
  677. }
  678. if (promoCode.getIsUse().equals("1")) {
  679. //被使用
  680. return JsonMessage.success("2");
  681. }
  682. if(!promoCode.getAdminId().equals("1")){
  683. if (String.valueOf(equipment.getAdminId()).equals(promoCode.getAdminId())) {
  684. } else {
  685. //不是本机
  686. return JsonMessage.success("3");
  687. }
  688. }
  689. if (promoCode.getDiscount() == null) {
  690. //旧优惠码
  691. return JsonMessage.success("0");
  692. }
  693. if (promoCode.getDiscount() != null && promoCode.getDiscount() == 0) {
  694. //0折
  695. return JsonMessage.success("0");
  696. }
  697. }
  698. if (equipment == null) {
  699. return JsonMessage.error("找不到设备");
  700. }
  701. Long equipmentId = equipment.getId();
  702. //查找商品
  703. BigDecimal price;
  704. Long productId = null;
  705. // A99:DIY花型
  706. String productNo = "A99";
  707. if (StringUtils.isEmpty(pri)) {
  708. TProduct product = R.getDataIfSuccess(szwlFeign.getProduct(String.valueOf(equipmentId), productName));
  709. if (product == null) {
  710. return JsonMessage.error("找不到商品");
  711. }
  712. price = product.getRmbPrice();
  713. productId = product.getId();
  714. productNo = product.getNo();
  715. } else {
  716. price = new BigDecimal(pri);
  717. }
  718. if(StringUtils.isNotEmpty(code)){
  719. BigDecimal discount = BigDecimal.valueOf(promoCode.getDiscount());
  720. if (StringUtils.isNotEmpty(promoCode.getType()) && promoCode.getType().equals("1")) {
  721. if (price.compareTo(discount) > 0) {
  722. // 如果 price 大于 discount,从 price 中减去 discount
  723. price = price.subtract(discount).setScale(2, RoundingMode.HALF_DOWN);
  724. } else {
  725. // 如果 price 小于等于 discount,将 price 设置为 0
  726. price = BigDecimal.ZERO;
  727. }
  728. } else {
  729. price = price.multiply(discount).divide(new BigDecimal(10)).setScale(2, RoundingMode.HALF_DOWN);
  730. }
  731. }
  732. if (BigDecimal.ZERO.compareTo(price) >= 0) {
  733. return JsonMessage.error("商品价格异常");
  734. }
  735. Long client6 = Long.parseLong(clientId.substring(clientId.length() - 6));
  736. String sn1 = orderService.initSn(client6);
  737. String sn2 = orderService.initSn(client6);
  738. // 获取关联设备的分销
  739. TProportion proportion = R.getDataIfSuccess(szwlFeign.getProportion(String.valueOf(equipmentId)));
  740. if (proportion == null || proportion.getId()==null) {
  741. return JsonMessage.error("设备商家未完成分销设置");
  742. }
  743. TAdmin admin = R.getDataIfSuccess(szwlFeign.getAdmin(String.valueOf(equipment.getAdminId())));
  744. if (admin == null) {
  745. return JsonMessage.error("找不到设备商家");
  746. }
  747. // 支付方式:0或null为汇聚,1为汇付支付
  748. String payPlatform = admin.getPayPlatform();
  749. String companyType = admin.getCompanyType();
  750. // 汇聚分账参数
  751. JSONArray altInfo = new JSONArray();
  752. // 汇付分账明细参数
  753. JSONObject acctSplitBunch = new JSONObject();
  754. if(StringUtils.isNotEmpty(payPlatform)&&payPlatform.equals("1")){
  755. acctSplitBunch = getAcctSplitBunch(proportion, price, companyType);
  756. if (!(acctSplitBunch.size() > 0)) {
  757. return JsonMessage.error("设备商家未注册结算账号");
  758. }
  759. } else {
  760. altInfo = getAltInfo(proportion, price);
  761. if (!(altInfo.size() > 0)) {
  762. return JsonMessage.error("设备商家未注册结算账号");
  763. }
  764. }
  765. String orderNo1 = sn1;
  766. String orderNo2 = sn2;
  767. BigDecimal amount = price.setScale(2, RoundingMode.HALF_DOWN);
  768. String productDesc = "";
  769. String commonParameter = "";
  770. String returnUrl = null;
  771. String notifyUrl = JoinpayConstant.Notify_Url;
  772. String isShowPic = "1";
  773. String openId = null;
  774. String authCode = null;
  775. String appid = null;
  776. String transactionModel = null;
  777. String tradeMerchantNo = admin.getTradeMerchantNo();
  778. String buyerId = null;
  779. String isAlt = "1";
  780. String altType = "11";
  781. String altUrl = null;
  782. BigDecimal marketingAmount = null;
  783. String frpCode1 = "WEIXIN_NATIVE";
  784. String frpCode2 = "ALIPAY_NATIVE";
  785. Integer productNumber = 1;
  786. TOrder order = new TOrder();
  787. order.setId(IDGenerator.orderID());
  788. order.setCreateDate(new Date());
  789. order.setModifyDate(new Date());
  790. order.setAdminId(admin.getId());
  791. order.setSn(sn1);
  792. order.setProductId(productId);
  793. order.setProductNo(productNo);
  794. order.setProductName(productName);
  795. order.setProductNumber(productNumber);
  796. order.setPrice(price);
  797. order.setClientId(equipment.getClientId());
  798. order.setEquipmentId(equipmentId);
  799. order.setFrpCode(frpCode1);
  800. order.setAltInfo(altInfo.toString());
  801. order.setStatus(0);
  802. order.setType(admin.getType());
  803. order.setCompanyType(companyType);
  804. order.setMachineType(equipment.getMachineType());
  805. if(admin.getType()==0){
  806. order.setAdminProportion(proportion.getAdminProportion());
  807. order.setAgencyProportion(proportion.getAgencyProportion());
  808. order.setAgencyId(proportion.getAgencyId());
  809. }
  810. if(admin.getType()==1){
  811. order.setAdminProportion(proportion.getAdminProportion());
  812. order.setAgencyProportion(proportion.getAgencyProportion());
  813. order.setAgencyId(proportion.getAgencyId());
  814. order.setMerchantProportion(proportion.getMerchantProportion());
  815. order.setMerchantId(proportion.getMerchantId());
  816. }
  817. if(admin.getType()==2){
  818. order.setAdminProportion(proportion.getAdminProportion());
  819. order.setAgencyProportion(proportion.getAgencyProportion());
  820. order.setAgencyId(proportion.getAgencyId());
  821. order.setMerchantProportion(proportion.getMerchantProportion());
  822. order.setMerchantId(proportion.getMerchantId());
  823. order.setPersonageProportion(proportion.getPersonageProportion());
  824. order.setPersonageId(proportion.getPersonageId());
  825. }
  826. order.setPayPlatform(payPlatform);
  827. if(StringUtils.isNotEmpty(payPlatform)&&payPlatform.equals("1")){
  828. order.setIsSettlement("0");
  829. }
  830. TOrder order2 = new TOrder();
  831. order2.setId(IDGenerator.orderID());
  832. order2.setCreateDate(new Date());
  833. order2.setModifyDate(new Date());
  834. order2.setAdminId(admin.getId());
  835. order2.setSn(sn2);
  836. order2.setProductId(productId);
  837. order2.setProductNo(productNo);
  838. order2.setProductName(productName);
  839. order2.setProductNumber(productNumber);
  840. order2.setPrice(price);
  841. order2.setClientId(equipment.getClientId());
  842. order2.setEquipmentId(equipmentId);
  843. order2.setFrpCode(frpCode2);
  844. order2.setAltInfo(altInfo.toString());
  845. order2.setStatus(0);
  846. order2.setType(admin.getType());
  847. order2.setPayPlatform(payPlatform);
  848. order2.setCompanyType(companyType);
  849. order2.setMachineType(equipment.getMachineType());
  850. if(admin.getType()==0){
  851. order2.setAdminProportion(proportion.getAdminProportion());
  852. order2.setAgencyProportion(proportion.getAgencyProportion());
  853. order2.setAgencyId(proportion.getAgencyId());
  854. }
  855. if(admin.getType()==1){
  856. order2.setAdminProportion(proportion.getAdminProportion());
  857. order2.setAgencyProportion(proportion.getAgencyProportion());
  858. order2.setAgencyId(proportion.getAgencyId());
  859. order2.setMerchantProportion(proportion.getMerchantProportion());
  860. order2.setMerchantId(proportion.getMerchantId());
  861. }
  862. if(admin.getType()==2){
  863. order2.setAdminProportion(proportion.getAdminProportion());
  864. order2.setAgencyProportion(proportion.getAgencyProportion());
  865. order2.setAgencyId(proportion.getAgencyId());
  866. order2.setMerchantProportion(proportion.getMerchantProportion());
  867. order2.setMerchantId(proportion.getMerchantId());
  868. order2.setPersonageProportion(proportion.getPersonageProportion());
  869. order2.setPersonageId(proportion.getPersonageId());
  870. }
  871. if(StringUtils.isNotEmpty(payPlatform)&&payPlatform.equals("1")){
  872. order2.setIsSettlement("0");
  873. }
  874. // 订单明细表
  875. TOrderDetails orderDetails = new TOrderDetails();
  876. orderDetails.setAdminId(admin.getId());
  877. orderDetails.setEquipmentId(equipmentId);
  878. orderDetails.setCreateDate(new Date());
  879. orderDetails.setPrice(price);
  880. orderDetails.setProductName(productName);
  881. orderDetails.setProductNo(productNo);
  882. orderDetails.setProductNumber(productNumber);
  883. orderDetails.setAmount(price);
  884. orderDetails.setCompanyType(companyType);
  885. orderDetails.setRefundStatus("0");
  886. orderDetails.setMachineType(equipment.getMachineType());
  887. // String client6 = clientId.substring(clientId.length() - 6);
  888. String productName1 = productName + "-" + equipment.getName() + "-" + client6;
  889. String result = null;
  890. if(StringUtils.isNotEmpty(payPlatform)&&payPlatform.equals("1")){
  891. // 汇付分账参数
  892. order.setAcctSplitBunch(acctSplitBunch.toString());
  893. order2.setAcctSplitBunch(acctSplitBunch.toString());
  894. //添加到汇付临时表,微信
  895. THuifuTempOrder tHuifuTempOrder = new THuifuTempOrder();
  896. String[] ignoreProperties = {"id"};
  897. String id = HuifuUtils.inithuifuTempId();
  898. tHuifuTempOrder.setId(id);
  899. BeanUtil.copyProperties(order,tHuifuTempOrder,ignoreProperties);
  900. huifuTempOrderService.save(tHuifuTempOrder);
  901. String result1 = "";
  902. if (StrUtil.isNotEmpty(companyType)&&companyType.equals("1")) {
  903. result1 = HuifuConstant.SC_JUMP_URL+ "/#/weChatPay?id=" + id;
  904. } else {
  905. result1 = HuifuConstant.SZ_JUMP_URL+ "/#/weChatPay?id=" + id;
  906. }
  907. // 支付宝
  908. try {
  909. result = huifuMchService.uniPay(
  910. sn2, amount, productName1, productDesc,
  911. notifyUrl, frpCode2, openId, appid, acctSplitBunch,companyType
  912. );
  913. } catch (Exception e) {
  914. e.printStackTrace();
  915. return JsonMessage.error("申请支付失败");
  916. }
  917. if (StrUtil.isEmpty(result)) {
  918. return JsonMessage.error("找不到支付图片");
  919. }
  920. // 汇聚支付支付申请返回支付二维码图片
  921. String code1 = "http://sz.sunzee.com.cn/PAY-SERVER/tOrder/aliPay?zfb="+result+"&wx="+result1;
  922. String qrcode = toQrcode(code1);
  923. JSONObject kindData = new JSONObject();
  924. kindData.put("sn", sn1+"-"+sn2);
  925. kindData.put("rd_Pic", qrcode);
  926. orderDetails.setOrderSn(sn2);
  927. orderDetails.setId(HuifuUtils.initDetailsId());
  928. orderDetailsService.save(orderDetails);
  929. orderService.save(order2);
  930. return JsonMessage.success(kindData.toString());
  931. }else{
  932. try {
  933. result = joinpayMchService.uniPay(
  934. orderNo1, amount, productName1, productDesc,
  935. commonParameter, returnUrl, notifyUrl, frpCode1,
  936. isShowPic, openId, authCode, appid, transactionModel, tradeMerchantNo,
  937. buyerId, isAlt, altType, altInfo, altUrl, marketingAmount
  938. );
  939. } catch (UnsupportedEncodingException e) {
  940. e.printStackTrace();
  941. return JsonMessage.error("申请支付失败");
  942. }
  943. JSONObject resultJson = JSONObject.parseObject(result);
  944. String rc_Result1 = resultJson.getString("rc_Result");
  945. String result1 = null;
  946. try {
  947. result1 = joinpayMchService.uniPay(
  948. orderNo2, amount, productName1, productDesc,
  949. commonParameter, returnUrl, notifyUrl, frpCode2,
  950. isShowPic, openId, authCode, appid, transactionModel, tradeMerchantNo,
  951. buyerId, isAlt, altType, altInfo, altUrl, marketingAmount
  952. );
  953. } catch (UnsupportedEncodingException e) {
  954. e.printStackTrace();
  955. return JsonMessage.error("申请支付失败");
  956. }
  957. JSONObject resultJson1 = JSONObject.parseObject(result1);
  958. String rc_Result2 = resultJson1.getString("rc_Result");
  959. // 汇聚支付支付申请返回支付二维码图片
  960. String code1 = "http://sz.sunzee.com.cn/PAY-SERVER/tOrder/"+"aliPay?wx="+rc_Result1+"&zfb="+rc_Result2;
  961. String rd_Pic = toQrcode(code1);
  962. JSONObject kindData = new JSONObject();
  963. kindData.put("sn", sn1+"-"+sn2);
  964. kindData.put("rd_Pic", rd_Pic);
  965. orderService.save(order);
  966. orderDetails.setOrderSn(sn1);
  967. orderDetails.setId(HuifuUtils.initDetailsId());
  968. orderDetailsService.save(orderDetails);
  969. orderDetails.setOrderSn(sn2);
  970. orderDetails.setId(HuifuUtils.initDetailsId());
  971. orderDetailsService.save(orderDetails);
  972. orderService.save(order2);
  973. return JsonMessage.success(kindData.toString());
  974. }
  975. }
  976. @ApiOperation(value = "购物车 请求在线支付 二码合一")
  977. @PostMapping("/carsPay")
  978. public Object carsPay(String clientId, @RequestBody Map<String,String> productNameMap) {
  979. if(productNameMap==null){
  980. return JsonMessage.error("数据出错");
  981. }
  982. StringBuffer note = new StringBuffer();
  983. StringBuffer productName = new StringBuffer();
  984. String productNo = "";
  985. Map<String, Integer> productMap = new HashMap<>();
  986. Integer productNumber = 0;
  987. TEquipment equipment = R.getDataIfSuccess(szwlFeign.findEquipmentByClientId(clientId));
  988. if(equipment==null||equipment.getId()==null){
  989. return JsonMessage.error("找不到设备");
  990. }
  991. BigDecimal price = new BigDecimal("0.00");
  992. TEquipmentDesc equipmentDesc = R.getDataIfSuccess(szwlFeign.findEquipmentById(equipment.getId()));
  993. if(equipmentDesc==null||equipmentDesc.getEquipmentId()==null||equipmentDesc.getPayType()==null||equipmentDesc.getPayType().equals("0")){
  994. // 1、type=0,原始设定,没有第二件半价
  995. for (String key : productNameMap.keySet()) {
  996. String entryValue = String.valueOf(productNameMap.get(key));
  997. List<String> value = JSON.parseArray(entryValue, String.class);
  998. List<TPromoCode> promoCodeList = new ArrayList<>();
  999. // 1、校验优惠码
  1000. if(value.size()>0){
  1001. for(String code:value){
  1002. if(!code.equals("0")){
  1003. TPromoCode promoCode;
  1004. ResponseModel<TPromoCode> tPromoCodeResponseModel = szwlFeign.selectTPromoCode(code, equipment.getAdminId().toString());
  1005. TPromoCode tPromoCode = tPromoCodeResponseModel.getData();
  1006. promoCode = tPromoCode;
  1007. if (tPromoCode == null||tPromoCode.getId()==null) {
  1008. //不存在
  1009. return JsonMessage.success("1");
  1010. }
  1011. Date lastUseDate = null;
  1012. if (promoCode != null) {
  1013. lastUseDate = promoCode.getLastUseDate();
  1014. }
  1015. if (lastUseDate != null && lastUseDate.getTime() < ((new Date()).getTime())) {
  1016. promoCode.setIsUse("2");
  1017. szwlFeign.updatePromoCode(String.valueOf(promoCode.getId()),"2");
  1018. //过期
  1019. return JsonMessage.success("4");
  1020. }
  1021. if (promoCode.getId() == null) {
  1022. //不存在
  1023. return JsonMessage.success("1");
  1024. }
  1025. if (promoCode.getIsUse().equals("1")) {
  1026. //被使用
  1027. return JsonMessage.success("2");
  1028. }
  1029. if(!promoCode.getAdminId().equals("1")){
  1030. if (String.valueOf(equipment.getAdminId()).equals(promoCode.getAdminId())) {
  1031. } else {
  1032. //不是本机
  1033. return JsonMessage.success("3");
  1034. }
  1035. }
  1036. if (promoCode.getDiscount() == null) {
  1037. //旧优惠码
  1038. return JsonMessage.success("0");
  1039. }
  1040. if (promoCode.getDiscount() != null && promoCode.getDiscount() == 0) {
  1041. //0折
  1042. return JsonMessage.success("0");
  1043. }
  1044. promoCodeList.add(promoCode);
  1045. }
  1046. }
  1047. }
  1048. String[] productNum = key.split("-");
  1049. String productNamea = productNum[0];
  1050. String num = productNum[1];
  1051. productMap.put(productNamea,Integer.valueOf(num));
  1052. productNumber += Integer.valueOf(num);
  1053. productName.append(productNamea).append("x").append(num).append(",");
  1054. //确定价格,然后叠加
  1055. TProduct product = R.getDataIfSuccess(szwlFeign.getProduct(String.valueOf(equipment.getId()), productNamea));
  1056. if (product == null) {
  1057. return JsonMessage.error("找不到商品");
  1058. }
  1059. note.append(productNamea).append("-").append(product.getNo()).append(":");
  1060. BigDecimal productPrice = product.getRmbPrice();
  1061. productNo = product.getNo();
  1062. if(promoCodeList.size()>0){
  1063. //有优惠码
  1064. BigDecimal onePrice = new BigDecimal("0.00");
  1065. int i;
  1066. for( i = 0;i<promoCodeList.size();i++){
  1067. BigDecimal discount = BigDecimal.valueOf(promoCodeList.get(i).getDiscount()).setScale(2, RoundingMode.HALF_DOWN);
  1068. BigDecimal price1 = new BigDecimal(0.0);
  1069. if (StringUtils.isNotEmpty(promoCodeList.get(i).getType()) && promoCodeList.get(i).getType().equals("1")) {
  1070. if (productPrice.compareTo(discount) > 0) {
  1071. // 如果 price 大于 discount,从 price 中减去 discount
  1072. price1 = productPrice.subtract(discount).setScale(2, RoundingMode.HALF_DOWN);
  1073. } else {
  1074. // 如果 price 小于等于 discount,将 price 设置为 0
  1075. price1 = BigDecimal.ZERO;
  1076. }
  1077. } else {
  1078. price1 = productPrice.multiply(discount).divide(new BigDecimal(10)).setScale(2, RoundingMode.HALF_DOWN);
  1079. }
  1080. // BigDecimal price1 = productPrice.multiply(discount).divide(new BigDecimal(10)).setScale(2, RoundingMode.HALF_DOWN);
  1081. price = price.add(price1);
  1082. onePrice = onePrice.add(price1);
  1083. }
  1084. if(i<Integer.valueOf(num)){
  1085. int a = Integer.valueOf(num)-i;
  1086. BigDecimal price2 = productPrice.multiply(new BigDecimal(a)).setScale(2, RoundingMode.HALF_DOWN);
  1087. price =price.add(price2);
  1088. onePrice = onePrice.add(price2);
  1089. }
  1090. note.append(onePrice).append("-").append(num).append(",");
  1091. }else {
  1092. //1,type=0,原始设定,没有第二件半价
  1093. int a = Integer.valueOf(num);
  1094. BigDecimal price2 = productPrice.multiply(new BigDecimal(a)).setScale(2, RoundingMode.HALF_DOWN);
  1095. price =price.add(price2);
  1096. note.append(price2).append("-").append(num).append(",");
  1097. }
  1098. }
  1099. } else {
  1100. //2,type=1,第二件半价
  1101. //2.1 判定是否有第二件,有多少个第二件半价
  1102. for (String key : productNameMap.keySet()) {
  1103. String[] productNum = key.split("-");
  1104. String productNamea = productNum[0];
  1105. String num = productNum[1];
  1106. productMap.put(productNamea,Integer.valueOf(num));
  1107. TProduct product = R.getDataIfSuccess(szwlFeign.getProduct(String.valueOf(equipment.getId()), productNamea));
  1108. productNo = product.getNo();
  1109. BigDecimal productPrice = product.getRmbPrice();
  1110. productName.append(productNamea).append("x").append(num).append(",");
  1111. note.append(productNamea).append("-").append(product.getNo()).append(":");
  1112. switch (num) {
  1113. case "1":
  1114. //1个 0个半价
  1115. price =price.add(productPrice);
  1116. note.append(price).append("-").append(num).append(",");
  1117. break;
  1118. case "2":
  1119. //2个 1个半价
  1120. price =price.add(productPrice);
  1121. price =price.add(productPrice.multiply(new BigDecimal(5)).divide(new BigDecimal(10)).setScale(2, RoundingMode.HALF_DOWN));
  1122. note.append(price).append("-").append(num).append(",");
  1123. break;
  1124. case "3":
  1125. //3个 1个半价
  1126. price =price.add(productPrice);
  1127. price =price.add(productPrice.multiply(new BigDecimal(5)).divide(new BigDecimal(10)).setScale(2, RoundingMode.HALF_DOWN));
  1128. price =price.add(productPrice);
  1129. note.append(price).append("-").append(num).append(",");
  1130. break;
  1131. case "4":
  1132. //4个 2个半价
  1133. price =price.add(productPrice);
  1134. price =price.add(productPrice.multiply(new BigDecimal(5)).divide(new BigDecimal(10)).setScale(2, RoundingMode.HALF_DOWN));
  1135. price =price.add(productPrice);
  1136. price =price.add(productPrice.multiply(new BigDecimal(5)).divide(new BigDecimal(10)).setScale(2, RoundingMode.HALF_DOWN));
  1137. note.append(price).append("-").append(num).append(",");
  1138. break;
  1139. case "5":
  1140. //5个 2个半价
  1141. price =price.add(productPrice);
  1142. price =price.add(productPrice.multiply(new BigDecimal(5)).divide(new BigDecimal(10)).setScale(2, RoundingMode.HALF_DOWN));
  1143. price =price.add(productPrice);
  1144. price =price.add(productPrice.multiply(new BigDecimal(5)).divide(new BigDecimal(10)).setScale(2, RoundingMode.HALF_DOWN));
  1145. price =price.add(productPrice);
  1146. note.append(price).append("-").append(num).append(",");
  1147. break;
  1148. }
  1149. }
  1150. }
  1151. if (equipment == null) {
  1152. return JsonMessage.error("找不到设备");
  1153. }
  1154. Long equipmentId = equipment.getId();
  1155. //查找商品
  1156. if (BigDecimal.ZERO.compareTo(price) >= 0) {
  1157. return JsonMessage.error("商品价格异常");
  1158. }
  1159. Long client6 = Long.parseLong(clientId.substring(clientId.length() - 6));
  1160. String sn1 = orderService.initSn(client6);
  1161. String sn2 = orderService.initSn(client6);
  1162. // 获取关联设备的分销
  1163. TProportion proportion = R.getDataIfSuccess(szwlFeign.getProportion(String.valueOf(equipmentId)));
  1164. if (proportion == null || proportion.getId()==null) {
  1165. return JsonMessage.error("设备商家未完成分销设置");
  1166. }
  1167. TAdmin admin = R.getDataIfSuccess(szwlFeign.getAdmin(String.valueOf(equipment.getAdminId())));
  1168. if (admin == null) {
  1169. return JsonMessage.error("找不到设备商家");
  1170. }
  1171. // 支付方式:0或null为汇聚,1为汇付支付
  1172. String payPlatform = admin.getPayPlatform();
  1173. String companyType = admin.getCompanyType();
  1174. // 汇聚分账参数
  1175. JSONArray altInfo = new JSONArray();
  1176. // 汇付分账明细参数
  1177. JSONObject acctSplitBunch = new JSONObject();
  1178. if(StringUtils.isNotEmpty(payPlatform)&&payPlatform.equals("1")){
  1179. acctSplitBunch = getAcctSplitBunch(proportion, price, companyType);
  1180. if (!(acctSplitBunch.size() > 0)) {
  1181. return JsonMessage.error("设备商家未注册结算账号");
  1182. }
  1183. } else {
  1184. altInfo = getAltInfo(proportion, price);
  1185. if (!(altInfo.size() > 0)) {
  1186. return JsonMessage.error("设备商家未注册结算账号");
  1187. }
  1188. }
  1189. String orderNo1 = sn1;
  1190. String orderNo2 = sn2;
  1191. BigDecimal amount = price.setScale(2, RoundingMode.HALF_DOWN);
  1192. String productDesc = "";
  1193. String commonParameter = "";
  1194. String returnUrl = null;
  1195. String notifyUrl = JoinpayConstant.Notify_Url;
  1196. String isShowPic = "1";
  1197. String openId = null;
  1198. String authCode = null;
  1199. String appid = null;
  1200. String transactionModel = null;
  1201. String tradeMerchantNo = admin.getTradeMerchantNo();
  1202. String buyerId = null;
  1203. String isAlt = "1";
  1204. String altType = "11";
  1205. String altUrl = null;
  1206. BigDecimal marketingAmount = null;
  1207. String frpCode1 = "WEIXIN_NATIVE";
  1208. String frpCode2 = "ALIPAY_NATIVE";
  1209. TOrder order = new TOrder();
  1210. order.setId(IDGenerator.orderID());
  1211. order.setCreateDate(new Date());
  1212. order.setModifyDate(new Date());
  1213. order.setAdminId(admin.getId());
  1214. order.setSn(sn1);
  1215. // 去掉后面的逗号
  1216. order.setProductNo(productNo);
  1217. if(productNumber == 1) {
  1218. productName.delete(productName.length() - 3, productName.length());
  1219. } else {
  1220. productName.deleteCharAt(productName.length() - 1);
  1221. }
  1222. order.setProductName(productName.toString());
  1223. order.setPrice(price);
  1224. order.setClientId(equipment.getClientId());
  1225. order.setEquipmentId(equipmentId);
  1226. order.setFrpCode(frpCode1);
  1227. order.setAltInfo(altInfo.toString());
  1228. order.setProductNumber(productNumber);
  1229. order.setStatus(0);
  1230. order.setType(admin.getType());
  1231. order.setCompanyType(companyType);
  1232. order.setMachineType(equipment.getMachineType());
  1233. if(admin.getType()==0){
  1234. order.setAdminProportion(proportion.getAdminProportion());
  1235. order.setAgencyProportion(proportion.getAgencyProportion());
  1236. order.setAgencyId(proportion.getAgencyId());
  1237. }
  1238. if(admin.getType()==1){
  1239. order.setAdminProportion(proportion.getAdminProportion());
  1240. order.setAgencyProportion(proportion.getAgencyProportion());
  1241. order.setAgencyId(proportion.getAgencyId());
  1242. order.setMerchantProportion(proportion.getMerchantProportion());
  1243. order.setMerchantId(proportion.getMerchantId());
  1244. }
  1245. if(admin.getType()==2){
  1246. order.setAdminProportion(proportion.getAdminProportion());
  1247. order.setAgencyProportion(proportion.getAgencyProportion());
  1248. order.setAgencyId(proportion.getAgencyId());
  1249. order.setMerchantProportion(proportion.getMerchantProportion());
  1250. order.setMerchantId(proportion.getMerchantId());
  1251. order.setPersonageProportion(proportion.getPersonageProportion());
  1252. order.setPersonageId(proportion.getPersonageId());
  1253. }
  1254. order.setPayPlatform(payPlatform);
  1255. if(StringUtils.isNotEmpty(payPlatform)&&payPlatform.equals("1")){
  1256. order.setIsSettlement("0");
  1257. }
  1258. order.setNote(note.toString());
  1259. TOrder order2 = new TOrder();
  1260. order2.setId(IDGenerator.orderID());
  1261. order2.setCreateDate(new Date());
  1262. order2.setModifyDate(new Date());
  1263. order2.setAdminId(admin.getId());
  1264. order2.setSn(sn2);
  1265. order2.setProductName(productName.toString());
  1266. order2.setProductNo(productNo);
  1267. order2.setPrice(price);
  1268. order2.setClientId(equipment.getClientId());
  1269. order2.setEquipmentId(equipmentId);
  1270. order2.setFrpCode(frpCode2);
  1271. order2.setAltInfo(altInfo.toString());
  1272. order2.setStatus(0);
  1273. order2.setProductNumber(productNumber);
  1274. order2.setType(admin.getType());
  1275. order2.setCompanyType(companyType);
  1276. order2.setMachineType(equipment.getMachineType());
  1277. if(admin.getType()==0){
  1278. order2.setAdminProportion(proportion.getAdminProportion());
  1279. order2.setAgencyProportion(proportion.getAgencyProportion());
  1280. order2.setAgencyId(proportion.getAgencyId());
  1281. }
  1282. if(admin.getType()==1){
  1283. order2.setAdminProportion(proportion.getAdminProportion());
  1284. order2.setAgencyProportion(proportion.getAgencyProportion());
  1285. order2.setAgencyId(proportion.getAgencyId());
  1286. order2.setMerchantProportion(proportion.getMerchantProportion());
  1287. order2.setMerchantId(proportion.getMerchantId());
  1288. }
  1289. if(admin.getType()==2){
  1290. order2.setAdminProportion(proportion.getAdminProportion());
  1291. order2.setAgencyProportion(proportion.getAgencyProportion());
  1292. order2.setAgencyId(proportion.getAgencyId());
  1293. order2.setMerchantProportion(proportion.getMerchantProportion());
  1294. order2.setMerchantId(proportion.getMerchantId());
  1295. order2.setPersonageProportion(proportion.getPersonageProportion());
  1296. order2.setPersonageId(proportion.getPersonageId());
  1297. }
  1298. order2.setPayPlatform(payPlatform);
  1299. if(StringUtils.isNotEmpty(payPlatform)&&payPlatform.equals("1")){
  1300. order2.setIsSettlement("0");
  1301. }
  1302. order2.setNote(note.toString());
  1303. // 订单明细表
  1304. TOrderDetails orderDetails = new TOrderDetails();
  1305. orderDetails.setAdminId(admin.getId());
  1306. orderDetails.setEquipmentId(equipmentId);
  1307. orderDetails.setCreateDate(new Date());
  1308. orderDetails.setCompanyType(companyType);
  1309. orderDetails.setRefundStatus("0");
  1310. orderDetails.setMachineType(equipment.getMachineType());
  1311. // String client6 = clientId.substring(clientId.length() - 6);
  1312. String productName1 = "";
  1313. if(productNumber > 1) {
  1314. if(equipment.getMachineType() == null || equipment.getMachineType().equals("0")) {
  1315. productName1 = "棉花糖-" + equipment.getName() + "-" + client6;
  1316. } else {
  1317. productName1 = "爆米花-" + equipment.getName() + "-" + client6;
  1318. }
  1319. } else {
  1320. productName1 = productName + "-" + equipment.getName() + "-" + client6;
  1321. }
  1322. String result = null;
  1323. if(StringUtils.isNotEmpty(payPlatform)&&payPlatform.equals("1")){
  1324. // 汇付分账参数
  1325. order.setAcctSplitBunch(acctSplitBunch.toString());
  1326. order2.setAcctSplitBunch(acctSplitBunch.toString());
  1327. //添加到汇付临时表,微信
  1328. THuifuTempOrder tHuifuTempOrder = new THuifuTempOrder();
  1329. String[] ignoreProperties = {"id"};
  1330. String id = HuifuUtils.inithuifuTempId();
  1331. tHuifuTempOrder.setId(id);
  1332. BeanUtil.copyProperties(order,tHuifuTempOrder,ignoreProperties);
  1333. huifuTempOrderService.save(tHuifuTempOrder);
  1334. String result1 = "";
  1335. if (StrUtil.isNotEmpty(companyType)&&companyType.equals("1")) {
  1336. result1 = HuifuConstant.SC_JUMP_URL+ "/#/weChatPay?id=" + id;
  1337. } else {
  1338. result1 = HuifuConstant.SZ_JUMP_URL+ "/#/weChatPay?id=" + id;
  1339. }
  1340. // 支付宝
  1341. try {
  1342. result = huifuMchService.uniPay(
  1343. sn2, amount, productName1, productDesc,
  1344. notifyUrl, frpCode2, openId, appid, acctSplitBunch, companyType
  1345. );
  1346. } catch (Exception e) {
  1347. e.printStackTrace();
  1348. return JsonMessage.error("申请支付失败");
  1349. }
  1350. if (StrUtil.isEmpty(result)) {
  1351. return JsonMessage.error("找不到支付图片");
  1352. }
  1353. // 汇聚支付支付申请返回支付二维码图片
  1354. String code1 = "http://sz.sunzee.com.cn/PAY-SERVER/tOrder/aliPay?zfb="+result+"&wx="+result1;
  1355. String qrcode = toQrcode(code1);
  1356. JSONObject kindData = new JSONObject();
  1357. kindData.put("sn", sn1+"-"+sn2);
  1358. kindData.put("rd_Pic", qrcode);
  1359. for (String key : productMap.keySet()) {
  1360. Integer productNum = productMap.get(key);
  1361. TProduct product = R.getDataIfSuccess(szwlFeign.getProduct(String.valueOf(equipment.getId()), key));
  1362. orderDetails.setId(HuifuUtils.initDetailsId());
  1363. orderDetails.setProductNo(product.getNo());
  1364. orderDetails.setProductName(key);
  1365. orderDetails.setPrice(product.getRmbPrice());
  1366. orderDetails.setProductNumber(productNum);
  1367. orderDetails.setAmount(product.getRmbPrice().multiply(new BigDecimal(productNum)));
  1368. orderDetails.setOrderSn(sn2);
  1369. orderDetailsService.save(orderDetails);
  1370. }
  1371. orderService.save(order2);
  1372. return JsonMessage.success(kindData.toString());
  1373. }else{
  1374. try {
  1375. result = joinpayMchService.uniPay(
  1376. orderNo1, amount, productName1, productDesc,
  1377. commonParameter, returnUrl, notifyUrl, frpCode1,
  1378. isShowPic, openId, authCode, appid, transactionModel, tradeMerchantNo,
  1379. buyerId, isAlt, altType, altInfo, altUrl, marketingAmount
  1380. );
  1381. } catch (UnsupportedEncodingException e) {
  1382. e.printStackTrace();
  1383. return JsonMessage.error("申请支付失败");
  1384. }
  1385. JSONObject resultJson = JSONObject.parseObject(result);
  1386. String rc_Result1 = resultJson.getString("rc_Result");
  1387. String result1 = null;
  1388. try {
  1389. result1 = joinpayMchService.uniPay(
  1390. orderNo2, amount, productName1, productDesc,
  1391. commonParameter, returnUrl, notifyUrl, frpCode2,
  1392. isShowPic, openId, authCode, appid, transactionModel, tradeMerchantNo,
  1393. buyerId, isAlt, altType, altInfo, altUrl, marketingAmount
  1394. );
  1395. } catch (UnsupportedEncodingException e) {
  1396. e.printStackTrace();
  1397. return JsonMessage.error("申请支付失败");
  1398. }
  1399. JSONObject resultJson1 = JSONObject.parseObject(result1);
  1400. String rc_Result2 = resultJson1.getString("rc_Result");
  1401. // 汇聚支付支付申请返回支付二维码图片
  1402. String code1 = "http://app.sunzee.com.cn/"+"/api/order/aliPay.htm?wx="+rc_Result1+"&zfb="+rc_Result2;
  1403. String rd_Pic = toQrcode(code1);
  1404. JSONObject kindData = new JSONObject();
  1405. kindData.put("sn", sn1+"-"+sn2);
  1406. kindData.put("rd_Pic", rd_Pic);
  1407. // 添加到订单明细表
  1408. for (String key : productMap.keySet()) {
  1409. Integer productNum = productMap.get(key);
  1410. TProduct product = R.getDataIfSuccess(szwlFeign.getProduct(String.valueOf(equipment.getId()), key));
  1411. orderDetails.setProductNo(product.getNo());
  1412. orderDetails.setProductName(key);
  1413. orderDetails.setPrice(product.getRmbPrice());
  1414. orderDetails.setProductNumber(productNum);
  1415. orderDetails.setAmount(product.getRmbPrice().multiply(new BigDecimal(productNum)));
  1416. orderDetails.setId(HuifuUtils.initDetailsId());
  1417. orderDetails.setOrderSn(sn1);
  1418. orderDetailsService.save(orderDetails);
  1419. orderDetails.setId(HuifuUtils.initDetailsId());
  1420. orderDetails.setOrderSn(sn2);
  1421. orderDetailsService.save(orderDetails);
  1422. }
  1423. orderService.save(order);
  1424. orderService.save(order2);
  1425. return JsonMessage.success(kindData.toString());
  1426. }
  1427. }
  1428. @ApiOperation(value = "购物车 微信小程序支付")
  1429. @PostMapping("/carsPayXCX")
  1430. public Object carsPayXCX(String clientId,String id,String coupons, @RequestBody Map<String,String> productNameMap) {
  1431. if(productNameMap==null){
  1432. return JsonMessage.error("数据出错");
  1433. }
  1434. StringBuffer note = new StringBuffer();
  1435. StringBuffer productName = new StringBuffer();
  1436. String productNo = "";
  1437. Map<String, Integer> productMap = new HashMap<>();
  1438. Integer productNumber = 0;
  1439. TEquipment equipment = R.getDataIfSuccess(szwlFeign.findEquipmentByClientId(clientId));
  1440. if(equipment==null||equipment.getId()==null){
  1441. return JsonMessage.error("找不到设备");
  1442. }
  1443. BigDecimal price = new BigDecimal("0.00");
  1444. List<TPromoCode> promoCodeAll = new ArrayList<>();
  1445. TEquipmentDesc equipmentDesc = R.getDataIfSuccess(szwlFeign.findEquipmentById(equipment.getId()));
  1446. if(StringUtils.isNotEmpty(equipmentDesc.getStatus())){
  1447. if(equipmentDesc.getStatus().equals("1")){
  1448. return JsonMessage.error("设备在做糖,请稍后再进行支付");
  1449. }
  1450. }
  1451. if(equipmentDesc==null||equipmentDesc.getEquipmentId()==null||equipmentDesc.getPayType()==null||equipmentDesc.getPayType().equals("0")){
  1452. //1,type=0,原始设定,没有第二件半价
  1453. for (String key : productNameMap.keySet()) {
  1454. String entryValue = String.valueOf(productNameMap.get(key));
  1455. List<String> value = JSON.parseArray(entryValue, String.class);
  1456. List<TPromoCode> promoCodeList = new ArrayList<>();
  1457. //1,校验优惠码
  1458. if(value.size()>0){
  1459. for(String code:value){
  1460. if(!code.equals("0")){
  1461. TPromoCode promoCode = new TPromoCode();
  1462. ResponseModel<TPromoCode> promoCode1 = szwlFeign.getPromoCode(code + "-" + equipment.getAdminId().toString());
  1463. TPromoCode tPromoCode = promoCode1.getData();
  1464. promoCode = tPromoCode;
  1465. if (tPromoCode == null||tPromoCode.getId()==null) {
  1466. //不存在
  1467. return JsonMessage.success("1");
  1468. }
  1469. //
  1470. Date lastUseDate = null;
  1471. if (promoCode != null) {
  1472. lastUseDate = promoCode.getLastUseDate();
  1473. }
  1474. if (lastUseDate != null && lastUseDate.getTime() < ((new Date()).getTime())) {
  1475. promoCode.setIsUse("2");
  1476. szwlFeign.updatePromoCode(String.valueOf(promoCode.getId()),"2");
  1477. //过期
  1478. return JsonMessage.success("4");
  1479. }
  1480. if (promoCode.getId() == null) {
  1481. //不存在
  1482. return JsonMessage.success("1");
  1483. }
  1484. if (promoCode.getIsUse().equals("1")) {
  1485. //被使用
  1486. return JsonMessage.success("2");
  1487. }
  1488. if(!promoCode.getAdminId().equals("1")){
  1489. if (String.valueOf(equipment.getAdminId()).equals(promoCode.getAdminId())) {
  1490. } else {
  1491. //不是本机
  1492. return JsonMessage.success("3");
  1493. }
  1494. }
  1495. if (promoCode.getDiscount() == null) {
  1496. //旧优惠码
  1497. return JsonMessage.success("0");
  1498. }
  1499. if (promoCode.getDiscount() != null && promoCode.getDiscount() == 0) {
  1500. //0折
  1501. return JsonMessage.success("0");
  1502. }
  1503. promoCodeList.add(promoCode);
  1504. promoCodeAll.add(promoCode);
  1505. }
  1506. }
  1507. }
  1508. String[] productNum = key.split("-");
  1509. String productNamea = productNum[0];
  1510. String num = productNum[1];
  1511. productMap.put(productNamea,Integer.valueOf(num));
  1512. productNumber += Integer.valueOf(num);
  1513. TProduct product = R.getDataIfSuccess(szwlFeign.getProduct(String.valueOf(equipment.getId()), productNamea));
  1514. productNo = product.getNo();
  1515. if (product == null) {
  1516. return JsonMessage.error("找不到商品");
  1517. }
  1518. productName.append(productNamea).append("x").append(num).append(",");
  1519. String no = product.getNo();
  1520. if(StringUtils.isEmpty(no)){
  1521. no="A00";
  1522. }
  1523. note.append(no).append("-").append(productNamea).append("-").append(num).append(":");
  1524. //确定价格,然后叠加R.getDataIfSuccess(szwlFeign.getProduct(String.valueOf(equipmentId), productName));
  1525. BigDecimal productPrice = product.getRmbPrice();
  1526. if(promoCodeList.size()>0){
  1527. //有优惠码
  1528. BigDecimal onePrice = new BigDecimal("0.00");
  1529. int i;
  1530. for( i = 0;i<promoCodeList.size();i++){
  1531. TPromoCode tPromoCode = promoCodeList.get(i);
  1532. BigDecimal discount = BigDecimal.valueOf(tPromoCode.getDiscount()).setScale(2, RoundingMode.HALF_DOWN);
  1533. if(StringUtils.isEmpty(tPromoCode.getType())||tPromoCode.getType().equals("0")){
  1534. //折扣优惠码
  1535. BigDecimal price1 = productPrice.multiply(discount).divide(new BigDecimal(10)).setScale(2, RoundingMode.HALF_DOWN);
  1536. price = price.add(price1);
  1537. onePrice = onePrice.add(price1);
  1538. }else {
  1539. //折扣价优惠码
  1540. BigDecimal price1 = productPrice.subtract(discount).setScale(2, RoundingMode.HALF_DOWN);
  1541. price = price.add(price1);
  1542. onePrice = onePrice.add(price1);
  1543. }
  1544. }
  1545. if(i<Integer.valueOf(num)){
  1546. int a = Integer.valueOf(num)-i;
  1547. BigDecimal price2 = productPrice.multiply(new BigDecimal(a)).setScale(2, RoundingMode.HALF_DOWN);
  1548. price =price.add(price2);
  1549. onePrice = onePrice.add(price2);
  1550. }
  1551. note.append(onePrice).append(",");
  1552. }else {
  1553. //1,type=0,原始设定,没有第二件半价
  1554. int a = Integer.valueOf(num);
  1555. BigDecimal price2 = productPrice.multiply(new BigDecimal(a)).setScale(2, RoundingMode.HALF_DOWN);
  1556. price =price.add(price2);
  1557. note.append(price2).append(",");
  1558. }
  1559. }
  1560. }else {
  1561. //2,type=1,第二件半价
  1562. //2.1 判定是否有第二件,有多少个第二件半价
  1563. for (String key : productNameMap.keySet()) {
  1564. String entryValue = String.valueOf(productNameMap.get(key));
  1565. List<String> value = JSON.parseArray(entryValue, String.class);
  1566. String[] productNum = key.split("-");
  1567. String productNamea = productNum[0];
  1568. String num = productNum[1];
  1569. productMap.put(productNamea,Integer.valueOf(num));
  1570. TProduct product = R.getDataIfSuccess(szwlFeign.getProduct(String.valueOf(equipment.getId()), productNamea));
  1571. productNo = product.getNo();
  1572. BigDecimal productPrice = product.getRmbPrice();
  1573. productName.append(productNamea).append("x").append(num).append(",");
  1574. note.append(productNamea).append(num).append(":");
  1575. switch (num) {
  1576. case "1":
  1577. //1个 0个半价
  1578. price =price.add(productPrice);
  1579. note.append(price).append(",");
  1580. break;
  1581. case "2":
  1582. //2个 1个半价
  1583. price =price.add(productPrice);
  1584. price =price.add(productPrice.multiply(new BigDecimal(5)).divide(new BigDecimal(10)).setScale(2, RoundingMode.HALF_DOWN));
  1585. note.append(price).append(",");
  1586. break;
  1587. case "3":
  1588. //3个 1个半价
  1589. price =price.add(productPrice);
  1590. price =price.add(productPrice.multiply(new BigDecimal(5)).divide(new BigDecimal(10)).setScale(2, RoundingMode.HALF_DOWN));
  1591. price =price.add(productPrice);
  1592. note.append(price).append(",");
  1593. break;
  1594. case "4":
  1595. //4个 2个半价
  1596. price =price.add(productPrice);
  1597. price =price.add(productPrice.multiply(new BigDecimal(5)).divide(new BigDecimal(10)).setScale(2, RoundingMode.HALF_DOWN));
  1598. price =price.add(productPrice);
  1599. price =price.add(productPrice.multiply(new BigDecimal(5)).divide(new BigDecimal(10)).setScale(2, RoundingMode.HALF_DOWN));
  1600. note.append(price).append(",");
  1601. break;
  1602. case "5":
  1603. //5个 2个半价
  1604. price =price.add(productPrice);
  1605. price =price.add(productPrice.multiply(new BigDecimal(5)).divide(new BigDecimal(10)).setScale(2, RoundingMode.HALF_DOWN));
  1606. price =price.add(productPrice);
  1607. price =price.add(productPrice.multiply(new BigDecimal(5)).divide(new BigDecimal(10)).setScale(2, RoundingMode.HALF_DOWN));
  1608. price =price.add(productPrice);
  1609. note.append(price).append(",");
  1610. break;
  1611. }
  1612. }
  1613. }
  1614. if (equipment == null) {
  1615. return JsonMessage.error("找不到设备");
  1616. }
  1617. Long equipmentId = equipment.getId();
  1618. //查找商品
  1619. Long client6 = Long.parseLong(clientId.substring(clientId.length() - 6));
  1620. String sn1 = orderService.initSn(client6);
  1621. // 获取关联设备的分销
  1622. TProportion proportion = R.getDataIfSuccess(szwlFeign.getProportion(String.valueOf(equipmentId)));
  1623. if (proportion == null || proportion.getId()==null) {
  1624. return JsonMessage.error("设备商家未完成分销设置");
  1625. }
  1626. TAdmin admin = R.getDataIfSuccess(szwlFeign.getAdmin(String.valueOf(equipment.getAdminId())));
  1627. if (admin == null) {
  1628. return JsonMessage.error("找不到设备商家");
  1629. }
  1630. // 支付方式:0或null为汇聚,1为汇付支付
  1631. String payPlatform = admin.getPayPlatform();
  1632. String companyType = admin.getCompanyType();
  1633. // 汇聚分账参数
  1634. JSONArray altInfo = new JSONArray();
  1635. // 汇付分账明细参数
  1636. JSONObject acctSplitBunch = new JSONObject();
  1637. if(StringUtils.isNotEmpty(payPlatform)&&payPlatform.equals("1")){
  1638. acctSplitBunch = getAcctSplitBunch(proportion, price, companyType);
  1639. if (!(acctSplitBunch.size() > 0)) {
  1640. return JsonMessage.error("设备商家未注册结算账号");
  1641. }
  1642. } else {
  1643. altInfo = getAltInfo(proportion, price);
  1644. if (!(altInfo.size() > 0)) {
  1645. return JsonMessage.error("设备商家未注册结算账号");
  1646. }
  1647. }
  1648. String orderNo1 = sn1;
  1649. String productDesc = "";
  1650. String commonParameter = "";
  1651. String returnUrl = null;
  1652. String notifyUrl = JoinpayConstant.Notify_Url;
  1653. String isShowPic = "1";
  1654. TSzsmWx szsmWx = szsmWxService.getById(id);
  1655. String openId = szsmWx.getOpenId();
  1656. String authCode = null;
  1657. String appid = "wx5071443e63295c29";
  1658. String transactionModel = null;
  1659. String tradeMerchantNo = "777114600391409";
  1660. String buyerId = null;
  1661. String isAlt = "1";
  1662. String altType = "11";
  1663. String altUrl = null;
  1664. BigDecimal priceTemp = price.setScale(2, RoundingMode.HALF_DOWN);
  1665. List<TPromoCode> yhjList = new ArrayList<>();
  1666. if(StringUtils.isNotEmpty(coupons)){
  1667. String[] cous = coupons.split(",");
  1668. if(cous.length>0){
  1669. for(int i = 0 ;i<cous.length ; i++){
  1670. if(StringUtils.isNotEmpty(cous[i])){
  1671. TPromoCode yhj = R.getDataIfSuccess(szwlFeign.getYHJ(cous[i]));
  1672. if(yhj!=null&&yhj.getId()!=null){
  1673. String isUse = yhj.getIsUse();
  1674. if(isUse.equals("1")){
  1675. return R.ok(yhj.getCode(),"优惠券已使用");
  1676. }
  1677. Date lastUseDate = yhj.getLastUseDate();
  1678. Date date = new Date();
  1679. if(date.getTime()>lastUseDate.getTime()){
  1680. return R.ok(yhj.getCode(),"优惠券已过期");
  1681. }
  1682. yhjList.add(yhj);
  1683. }
  1684. }
  1685. }
  1686. }
  1687. }
  1688. List<String> couponList = new ArrayList<>();
  1689. //营销金额
  1690. BigDecimal marketingAmount = null;
  1691. if(yhjList.size()>0){
  1692. for(TPromoCode yhj:yhjList){
  1693. if(yhj!=null&&yhj.getId()!=null){
  1694. if(marketingAmount==null){
  1695. marketingAmount = new BigDecimal("0.00");
  1696. }
  1697. marketingAmount =marketingAmount.add(new BigDecimal(yhj.getDiscount()).setScale(2, RoundingMode.HALF_DOWN));
  1698. couponList.add(String.valueOf(yhj.getCode()));
  1699. if(marketingAmount.compareTo(price)>=0){
  1700. marketingAmount = priceTemp.subtract(new BigDecimal("0.01"));
  1701. price = new BigDecimal("0.01").setScale(2, RoundingMode.HALF_DOWN);
  1702. break;
  1703. }else {
  1704. price = price.subtract(marketingAmount);
  1705. }
  1706. }
  1707. }
  1708. }
  1709. String frpCode1 = "WEIXIN_XCX";
  1710. BigDecimal amount = price.setScale(2, RoundingMode.HALF_DOWN);
  1711. TOrder order = new TOrder();
  1712. order.setId(IDGenerator.orderID());
  1713. order.setCreateDate(new Date());
  1714. order.setModifyDate(new Date());
  1715. order.setAdminId(admin.getId());
  1716. order.setSn(sn1);
  1717. order.setProductDesc(JSONArray.toJSONString(productNameMap));
  1718. // 去掉后面的逗号
  1719. order.setProductNo(productNo);
  1720. if(productNumber == 1) {
  1721. productName.delete(productName.length() - 3, productName.length());
  1722. } else {
  1723. productName.deleteCharAt(productName.length() - 1);
  1724. }
  1725. order.setProductName(productName.toString());
  1726. order.setPrice(price);
  1727. order.setClientId(equipment.getClientId());
  1728. order.setEquipmentId(equipmentId);
  1729. order.setFrpCode(frpCode1);
  1730. order.setAltInfo(altInfo.toString());
  1731. order.setStatus(0);
  1732. order.setType(admin.getType());
  1733. order.setMarketingAmount(marketingAmount);
  1734. order.setProductNumber(productNumber);
  1735. order.setCompanyType(companyType);
  1736. order.setMachineType(equipment.getMachineType());
  1737. if(couponList.size()>0){
  1738. order.setCoupons(couponList.toString());
  1739. }
  1740. if(admin.getType()==0){
  1741. order.setAdminProportion(proportion.getAdminProportion());
  1742. order.setAgencyProportion(proportion.getAgencyProportion());
  1743. order.setAgencyId(proportion.getAgencyId());
  1744. }
  1745. if(admin.getType()==1){
  1746. order.setAdminProportion(proportion.getAdminProportion());
  1747. order.setAgencyProportion(proportion.getAgencyProportion());
  1748. order.setAgencyId(proportion.getAgencyId());
  1749. order.setMerchantProportion(proportion.getMerchantProportion());
  1750. order.setMerchantId(proportion.getMerchantId());
  1751. }
  1752. if(admin.getType()==2){
  1753. order.setAdminProportion(proportion.getAdminProportion());
  1754. order.setAgencyProportion(proportion.getAgencyProportion());
  1755. order.setAgencyId(proportion.getAgencyId());
  1756. order.setMerchantProportion(proportion.getMerchantProportion());
  1757. order.setMerchantId(proportion.getMerchantId());
  1758. order.setPersonageProportion(proportion.getPersonageProportion());
  1759. order.setPersonageId(proportion.getPersonageId());
  1760. }
  1761. order.setPayPlatform(payPlatform);
  1762. if(StringUtils.isNotEmpty(payPlatform)&&payPlatform.equals("1")){
  1763. order.setIsSettlement("0");
  1764. }
  1765. order.setNote(note.toString());
  1766. if(StringUtils.isNotEmpty(id)){
  1767. order.setWxId(Long.valueOf(id));
  1768. }
  1769. if (BigDecimal.ZERO.compareTo(price) >= 0) {
  1770. order.setStatus(1);
  1771. orderService.updateById(order);
  1772. //修改优惠码状态
  1773. if(promoCodeAll.size()>0){
  1774. for(TPromoCode code:promoCodeAll){
  1775. szwlFeign.updatePromoCode(String.valueOf(code.getId()),"2");
  1776. }
  1777. }
  1778. //发送远程命令到机器那边,让机器做糖
  1779. equipmentService.sentMessage(equipment.getClientId(),PushUtils.buildJson("cardPayXCX", String.valueOf(productNameMap)).toString());
  1780. return JsonMessage.success("支付成功");
  1781. }
  1782. // 订单明细表
  1783. TOrderDetails orderDetails = new TOrderDetails();
  1784. orderDetails.setAdminId(admin.getId());
  1785. orderDetails.setEquipmentId(equipmentId);
  1786. orderDetails.setCreateDate(new Date());
  1787. orderDetails.setCompanyType(companyType);
  1788. orderDetails.setRefundStatus("0");
  1789. orderDetails.setMachineType(equipment.getMachineType());
  1790. // String client6 = clientId.substring(clientId.length() - 6);
  1791. String productName1 = "";
  1792. if(productNumber > 1) {
  1793. if(equipment.getMachineType() == null || equipment.getMachineType().equals("0")) {
  1794. productName1 = "棉花糖-" + equipment.getName() + "-" + client6;
  1795. } else {
  1796. productName1 = "爆米花-" + equipment.getName() + "-" + client6;
  1797. }
  1798. } else {
  1799. productName1 = productName + "-" + equipment.getName() + "-" + client6;
  1800. }
  1801. String result = null;
  1802. if(StringUtils.isNotEmpty(payPlatform)&&payPlatform.equals("1")){
  1803. order.setAcctSplitBunch(acctSplitBunch.toString());
  1804. try {
  1805. result = huifuMchService.uniPay(
  1806. orderNo1, amount, productName1, productDesc,
  1807. notifyUrl, frpCode1, openId, appid, acctSplitBunch, companyType
  1808. );
  1809. } catch (Exception e) {
  1810. e.printStackTrace();
  1811. return JsonMessage.error("申请支付失败");
  1812. }
  1813. // 返回支付二维码图片
  1814. if (result == null || StringUtils.isBlank(result)) {
  1815. return JsonMessage.error("申请支付失败");
  1816. }
  1817. Map<String,String> map = new HashMap<>();
  1818. map.put("sn", sn1);
  1819. map.put("rc_Result", result);
  1820. for (String key : productMap.keySet()) {
  1821. Integer productNum = productMap.get(key);
  1822. TProduct product = R.getDataIfSuccess(szwlFeign.getProduct(String.valueOf(equipment.getId()), key));
  1823. orderDetails.setId(HuifuUtils.initDetailsId());
  1824. orderDetails.setProductNo(product.getNo());
  1825. orderDetails.setProductName(key);
  1826. orderDetails.setPrice(product.getRmbPrice());
  1827. orderDetails.setProductNumber(productNum);
  1828. orderDetails.setAmount(product.getRmbPrice().multiply(new BigDecimal(productNum)));
  1829. orderDetails.setOrderSn(sn1);
  1830. orderDetailsService.save(orderDetails);
  1831. }
  1832. orderService.save(order);
  1833. return JsonMessage.success(map);
  1834. }else{
  1835. try {
  1836. result = joinpayMchService.uniPay(
  1837. orderNo1, amount, productName1, productDesc,
  1838. commonParameter, returnUrl, notifyUrl, frpCode1,
  1839. isShowPic, openId, authCode, appid, transactionModel, tradeMerchantNo,
  1840. buyerId, isAlt, altType, altInfo, altUrl, marketingAmount
  1841. );
  1842. } catch (UnsupportedEncodingException e) {
  1843. e.printStackTrace();
  1844. return JsonMessage.error("申请支付失败");
  1845. }
  1846. JSONObject resultJson = JSONObject.parseObject(result);
  1847. // 汇聚支付支付申请返回支付二维码图片
  1848. String rc_Result = resultJson.getString("rc_Result");
  1849. if (resultJson == null || StringUtils.isBlank(rc_Result)) {
  1850. return JsonMessage.error("申请支付失败");
  1851. }
  1852. Map<String,String> map = new HashMap<>();
  1853. map.put("sn", sn1);
  1854. map.put("rc_Result", rc_Result);
  1855. for (String key : productMap.keySet()) {
  1856. Integer productNum = productMap.get(key);
  1857. TProduct product = R.getDataIfSuccess(szwlFeign.getProduct(String.valueOf(equipment.getId()), key));
  1858. orderDetails.setId(HuifuUtils.initDetailsId());
  1859. orderDetails.setProductNo(product.getNo());
  1860. orderDetails.setProductName(key);
  1861. orderDetails.setPrice(product.getRmbPrice());
  1862. orderDetails.setProductNumber(productNum);
  1863. orderDetails.setAmount(product.getRmbPrice().multiply(new BigDecimal(productNum)));
  1864. orderDetails.setOrderSn(sn1);
  1865. orderDetailsService.save(orderDetails);
  1866. }
  1867. orderService.save(order);
  1868. return JsonMessage.success(map);
  1869. }
  1870. }
  1871. @ApiOperation(value = "汇付:获取code回调跳转到支付页面")
  1872. @GetMapping("/callback")
  1873. public void oauthCallback(HttpServletRequest request, HttpServletResponse response) throws IOException {
  1874. // 获取code
  1875. String code = request.getParameter("code");
  1876. String id = request.getParameter("state");
  1877. LambdaQueryWrapper<THuifuTempOrder> wrapper = new LambdaQueryWrapper<>();
  1878. wrapper.eq(THuifuTempOrder::getId,id);
  1879. THuifuTempOrder huifuTempOrder = huifuTempOrderService.getOne(wrapper);
  1880. // 从临时表中获取订单信息
  1881. String result = null;
  1882. if(huifuTempOrder != null) {
  1883. String companyType = huifuTempOrder.getCompanyType();
  1884. if(huifuTempOrder.getStatus() == 1) {
  1885. if(StrUtil.isNotEmpty(companyType) && companyType.equals("1")) {
  1886. result = HuifuConstant.SC_JUMP_URL+"/#/popPayment?status=1";
  1887. } else {
  1888. result = HuifuConstant.SZ_JUMP_URL+"/#/popPayment?status=1";
  1889. }
  1890. } else {
  1891. String openid;
  1892. if(StrUtil.isNotEmpty(companyType) && companyType.equals("1")) {
  1893. // 获取openid
  1894. try {
  1895. String openUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?" +
  1896. "appid=" + HuifuConstant.SC_WX_SUB_APP_ID +
  1897. "&secret=" + HuifuConstant.SC_WX_APP_SECRET +
  1898. "&code=" + code +
  1899. "&grant_type=authorization_code";
  1900. log.info("获取openID:{}", openUrl);
  1901. org.json.JSONObject jsonObject = HttpClientUtils.get(openUrl);
  1902. log.info("获取openID结果:{}", jsonObject);
  1903. openid = jsonObject.getString("openid");
  1904. result = huifuMchService.wetchatPay(huifuTempOrder,openid);
  1905. } catch (Exception e) {
  1906. e.printStackTrace();
  1907. }
  1908. } else {
  1909. // 获取openid
  1910. try {
  1911. String openUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?" +
  1912. "appid=" + HuifuConstant.SZ_WX_SUB_APP_ID +
  1913. "&secret=" + HuifuConstant.SZ_WX_APP_SECRET +
  1914. "&code=" + code +
  1915. "&grant_type=authorization_code";
  1916. log.info("获取openID:{}", openUrl);
  1917. org.json.JSONObject jsonObject = HttpClientUtils.get(openUrl);
  1918. log.info("获取openID结果:{}", jsonObject);
  1919. openid = jsonObject.getString("openid");
  1920. result = huifuMchService.wetchatPay(huifuTempOrder,openid);
  1921. } catch (Exception e) {
  1922. e.printStackTrace();
  1923. }
  1924. }
  1925. }
  1926. }
  1927. log.info("支付链接:{}",result);
  1928. response.sendRedirect(result);
  1929. }
  1930. /**
  1931. * 支付成功回调 汇付
  1932. *
  1933. */
  1934. @RequestMapping(value = "/huifuNotify", method = RequestMethod.POST)
  1935. @ResponseBody
  1936. public ResponseModel<?> huifuNotify(HttpServletRequest request) {
  1937. String respData = request.getParameter("resp_data");
  1938. JSONObject data = JSONObject.parseObject(respData);
  1939. String transStat = data.getString("trans_stat");
  1940. // 订单号
  1941. String sn = data.getString("req_seq_id");
  1942. if(StrUtil.hasEmpty(sn)) {
  1943. return R.fail(ResponseCodesEnum.A0001,"订单号为空!");
  1944. }
  1945. // 查到指定订单
  1946. LambdaQueryWrapper<TOrder> wrapper = new LambdaQueryWrapper<>();
  1947. wrapper.eq(TOrder::getSn,sn);
  1948. TOrder order = orderService.getOne(wrapper);
  1949. if(HuifuConstant.TRANS_STAT_S.equals(transStat)) {
  1950. // 查到对应订单明细表
  1951. LambdaQueryWrapper<TOrderDetails> lambdaQueryWrapper = new LambdaQueryWrapper<>();
  1952. lambdaQueryWrapper.eq(TOrderDetails::getOrderSn,sn);
  1953. List<TOrderDetails> orderDetails = orderDetailsService.list(lambdaQueryWrapper);
  1954. for (TOrderDetails orderDetail : orderDetails) {
  1955. // 修改状态订单明细状态
  1956. orderDetail.setRefundStatus("1");
  1957. orderDetailsService.updateById(orderDetail);
  1958. }
  1959. // 已支付
  1960. order.setStatus(1);
  1961. // 支付平台产生的全局流水号:hf_seq_id
  1962. String hfSeqId = data.getString("hf_seq_id");
  1963. order.setTrxNo(hfSeqId);
  1964. // 支付时间,格式:YYYY-MM-DD HH:mm:ss
  1965. String payTime = data.getString("end_time");
  1966. SimpleDateFormat inputFormat = new SimpleDateFormat("yyyyMMddHHmmss");
  1967. SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  1968. // 临时表修改支付状态
  1969. String id = data.getString("remark");
  1970. if(StrUtil.isNotEmpty(id)) {
  1971. LambdaQueryWrapper<THuifuTempOrder> queryWrapper = new LambdaQueryWrapper<>();
  1972. queryWrapper.eq(THuifuTempOrder::getId,id);
  1973. THuifuTempOrder huifuTempOrder = huifuTempOrderService.getOne(queryWrapper);
  1974. huifuTempOrder.setStatus(1);
  1975. huifuTempOrderService.updateById(huifuTempOrder);
  1976. }
  1977. try {
  1978. Date date = inputFormat.parse(URLDecoder.decode(payTime, "UTF-8"));
  1979. String formattedDate = outputFormat.format(date);
  1980. Date payDate = outputFormat.parse(URLDecoder.decode(formattedDate, "UTF-8"));
  1981. order.setPayDate(payDate);
  1982. } catch (ParseException e) {
  1983. e.printStackTrace();
  1984. } catch (UnsupportedEncodingException e) {
  1985. e.printStackTrace();
  1986. }
  1987. // 修改订单信息
  1988. orderService.updateById(order);
  1989. //
  1990. JSONObject kindData = new JSONObject();
  1991. kindData.put("sn", order.getSn());
  1992. kindData.put("productName", order.getProductName());
  1993. TEquipment equipment = R.getDataIfSuccess(szwlFeign.findEquipmentByClientId(order.getClientId()));
  1994. if(order.getFrpCode().equals("WEIXIN_XCX")){
  1995. JSONObject kindData1 = new JSONObject();
  1996. kindData1.put("sn", order.getSn());
  1997. kindData1.put("productDesc", order.getProductDesc());
  1998. String s = order.getId().toString();
  1999. if(s.length()>6){
  2000. s = s.substring(s.length()-6,s.length());
  2001. }
  2002. kindData1.put("mealCode", s);
  2003. order.setOrderStatus("-1");
  2004. equipmentService.sentMessage(equipment.getClientId(), PushUtils.buildJson("cardPayXCX", kindData1.toString()).toString());
  2005. if(StringUtils.isNotEmpty(equipment.getApkVersion())){
  2006. //发送两次mq
  2007. if(equipment.getApkVersion().equals("1.0.131-jz11")||equipment.getApkVersion().equals("1.0.131-jz12")||equipment.getApkVersion().equals("1.0.131-jz13")||equipment.getApkVersion().equals("1.0.131-jz14")||equipment.getApkVersion().equals("1.0.131-jz15")){
  2008. sendMqTwo(equipment.getClientId(),kindData1.toString());
  2009. }
  2010. }
  2011. TSzsmWx szsmWx = szsmWxService.getById(order.getWxId());
  2012. BigDecimal integral = szsmWx.getIntegral();
  2013. if(integral!=null){
  2014. integral = order.getPrice().add(integral);
  2015. }else {
  2016. integral = order.getPrice();
  2017. }
  2018. szsmWx.setIntegral(integral);
  2019. szsmWxService.updateById(szsmWx);
  2020. if(StringUtils.isNotEmpty(order.getCoupons())){
  2021. String coupons = order.getCoupons();
  2022. coupons= coupons.substring(1,coupons.length()-1);
  2023. List<String> list1 = Arrays.asList(coupons);
  2024. if(list1.size()>0){
  2025. for(String code:list1){
  2026. szwlFeign.useYHJ(code, String.valueOf(order.getAdminId()),order.getClientId());
  2027. }
  2028. }
  2029. }
  2030. }else {
  2031. if (!order.getIsNotified().equals("1")) {
  2032. String paySuccess = equipmentService.sentMessage(equipment.getClientId(), PushUtils.buildJson("pay_success", kindData.toString()).toString());
  2033. if (paySuccess.equals("success")) {
  2034. order.setIsNotified("1");
  2035. }
  2036. } }
  2037. orderService.updateById(order);
  2038. // equipmentService.sentMessage(equipment.getClientId(), PushUtils.buildJson("pay_success", kindData.toString()).toString());
  2039. try {
  2040. if(order.getProductNumber()==null||order.getProductNumber()==0){
  2041. order.setProductNumber(1);
  2042. }
  2043. orderFeign.saveToEs(order);
  2044. }catch (Exception e) {
  2045. e.printStackTrace();
  2046. }
  2047. // try {
  2048. LambdaQueryWrapper<TOrderTask> queryWrapper = new LambdaQueryWrapper<>();
  2049. queryWrapper.eq(TOrderTask::getSn, sn);
  2050. TOrderTask oldOrderTask = orderTaskService.getOne(queryWrapper);
  2051. if (oldOrderTask == null) {
  2052. TOrderTask orderTask = new TOrderTask();
  2053. BeanUtils.copyPropertiesIgnoreNull(order, orderTask, true);
  2054. orderTask.setAgencyId(order.getId());
  2055. orderTaskService.save(orderTask);
  2056. }
  2057. // }catch (Exception e) {
  2058. // e.printStackTrace();
  2059. // }
  2060. //传送数据
  2061. sentJingZhun(order);
  2062. return R.ok("支付成功");
  2063. } else {
  2064. TEquipment equipment = R.getDataIfSuccess(szwlFeign.findEquipmentByClientId(order.getClientId()));
  2065. String gtClientId = equipment.getGtClientId();
  2066. JSONObject kindData = new JSONObject();
  2067. kindData.put("sn", order.getSn());
  2068. if (!order.getIsNotified().equals("2")) {
  2069. String payFail = equipmentService.sentMessage(equipment.getClientId(), PushUtils.buildJson("pay_faile", kindData.toString()).toString());
  2070. if (payFail.equals("success")) {
  2071. order.setIsNotified("2");
  2072. }
  2073. } order.setNote(gtClientId + "支付失败");
  2074. orderService.updateById(order);
  2075. return R.fail(ResponseCodesEnum.A0001,"支付失败");
  2076. }
  2077. }
  2078. /**
  2079. * 支付成功回调 汇聚
  2080. *
  2081. * @return
  2082. */
  2083. @RequestMapping(value = "/notify", method = RequestMethod.GET)
  2084. @ResponseBody
  2085. public String notify(HttpServletRequest request) {
  2086. String r6_Status = request.getParameter("r6_Status");
  2087. // 订单号
  2088. String sn = request.getParameter("r2_OrderNo");
  2089. LambdaQueryWrapper<TOrder> query = Wrappers.lambdaQuery();
  2090. query.eq(TOrder::getSn,sn);
  2091. if(StringUtils.isEmpty(sn)){
  2092. return "error";
  2093. }
  2094. TOrder order = orderService.getOne(query);
  2095. if(order == null) {
  2096. return "error";
  2097. }
  2098. if (JoinpayConstant.r6_Status_100.equals(r6_Status)) {
  2099. // 查到对应订单明细表
  2100. LambdaQueryWrapper<TOrderDetails> lambdaQueryWrapper = new LambdaQueryWrapper<>();
  2101. lambdaQueryWrapper.eq(TOrderDetails::getOrderSn,sn);
  2102. List<TOrderDetails> orderDetails = orderDetailsService.list(lambdaQueryWrapper);
  2103. for (TOrderDetails orderDetail : orderDetails) {
  2104. // 修改状态订单明细状态
  2105. orderDetail.setRefundStatus("1");
  2106. orderDetailsService.updateById(orderDetail);
  2107. }
  2108. // 已支付
  2109. order.setStatus(1);
  2110. // 支付平台产生的流水号
  2111. String r7_TrxNo = request.getParameter("r7_TrxNo");
  2112. order.setTrxNo(r7_TrxNo);
  2113. // 格式:YYYY-MM-DD HH:mm:ss
  2114. String ra_PayTime = request.getParameter("ra_PayTime");
  2115. try {
  2116. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  2117. Date payDate = sdf.parse(URLDecoder.decode(ra_PayTime, "UTF-8"));
  2118. order.setPayDate(payDate);
  2119. } catch (ParseException e) {
  2120. e.printStackTrace();
  2121. } catch (UnsupportedEncodingException e) {
  2122. e.printStackTrace();
  2123. }
  2124. orderService.updateById(order);
  2125. JSONObject kindData = new JSONObject();
  2126. kindData.put("sn", order.getSn());
  2127. kindData.put("productName", order.getProductName());
  2128. TEquipment byClientId = R.getDataIfSuccess(szwlFeign.findEquipmentByClientId(order.getClientId()));
  2129. if(order.getFrpCode().equals("WEIXIN_XCX")){
  2130. JSONObject kindData1 = new JSONObject();
  2131. kindData1.put("sn", order.getSn());
  2132. kindData1.put("productDesc", order.getProductDesc());
  2133. String s = order.getId().toString();
  2134. if(s.length()>6){
  2135. s = s.substring(s.length()-6,s.length());
  2136. }
  2137. kindData1.put("mealCode", s);
  2138. order.setOrderStatus("-1");
  2139. equipmentService.sentMessage(byClientId.getClientId(), PushUtils.buildJson("cardPayXCX", kindData1.toString()).toString());
  2140. if(StringUtils.isNotEmpty(byClientId.getApkVersion())){
  2141. //发送两次mq
  2142. if(byClientId.getApkVersion().equals("1.0.131-jz11")||byClientId.getApkVersion().equals("1.0.131-jz12")||byClientId.getApkVersion().equals("1.0.131-jz13")||byClientId.getApkVersion().equals("1.0.131-jz14")||byClientId.getApkVersion().equals("1.0.131-jz15")){
  2143. sendMqTwo(byClientId.getClientId(),kindData1.toString());
  2144. }
  2145. }
  2146. TSzsmWx szsmWx = szsmWxService.getById(order.getWxId());
  2147. BigDecimal integral = szsmWx.getIntegral();
  2148. if(integral!=null){
  2149. integral = order.getPrice().add(integral);
  2150. }else {
  2151. integral = order.getPrice();
  2152. }
  2153. szsmWx.setIntegral(integral);
  2154. szsmWxService.updateById(szsmWx);
  2155. if(StringUtils.isNotEmpty(order.getCoupons())){
  2156. String coupons = order.getCoupons();
  2157. coupons= coupons.substring(1,coupons.length()-1);
  2158. List<String> list1 = Arrays.asList(coupons);
  2159. if(list1.size()>0){
  2160. for(String code:list1){
  2161. szwlFeign.useYHJ(code, String.valueOf(order.getAdminId()),order.getClientId());
  2162. }
  2163. }
  2164. }
  2165. }else {
  2166. if (!order.getIsNotified().equals("1")) {
  2167. String paySuccess = equipmentService.sentMessage(byClientId.getClientId(), PushUtils.buildJson("pay_success", kindData.toString()).toString());
  2168. if (paySuccess.equals("success")) {
  2169. order.setIsNotified("1");
  2170. }
  2171. }
  2172. }
  2173. orderService.updateById(order);
  2174. try {
  2175. if(order.getProductNumber()==null||order.getProductNumber()==0){
  2176. order.setProductNumber(1);
  2177. }
  2178. orderFeign.saveToEs(order);
  2179. }catch (Exception e) {
  2180. e.printStackTrace();
  2181. }
  2182. // try {
  2183. LambdaQueryWrapper<TOrderTask> queryWrapper = new LambdaQueryWrapper<>();
  2184. queryWrapper.eq(TOrderTask::getSn, sn);
  2185. TOrderTask oldOrderTask = orderTaskService.getOne(queryWrapper);
  2186. if (oldOrderTask == null) {
  2187. TOrderTask orderTask = new TOrderTask();
  2188. BeanUtils.copyPropertiesIgnoreNull(order, orderTask, true);
  2189. orderTask.setAgencyId(order.getId());
  2190. orderTaskService.save(orderTask);
  2191. }
  2192. // }catch (Exception e) {
  2193. // e.printStackTrace();
  2194. // }
  2195. //传送数据
  2196. sentJingZhun(order);
  2197. return "success";
  2198. } else {
  2199. TEquipment byClientId = R.getDataIfSuccess(szwlFeign.findEquipmentByClientId(order.getClientId()));
  2200. String gtClientId = byClientId.getGtClientId();
  2201. JSONObject kindData = new JSONObject();
  2202. kindData.put("sn", order.getSn());
  2203. if (!order.getIsNotified().equals("2")) {
  2204. String payFail = equipmentService.sentMessage(byClientId.getClientId(), PushUtils.buildJson("pay_faile", kindData.toString()).toString());
  2205. if (payFail.equals("success")) {
  2206. order.setIsNotified("2");
  2207. }
  2208. }
  2209. order.setNote(gtClientId + "支付失败");
  2210. orderService.updateById(order);
  2211. return "success";
  2212. }
  2213. }
  2214. //发送两次mq
  2215. private void sendMqTwo(String clientId, String kind) {
  2216. // 定时任务1
  2217. TimerTask timerTask = new TimerTask() {
  2218. @Override
  2219. public void run() {
  2220. equipmentService.sentMessage(clientId, PushUtils.buildJson("cardPayXCX", kind).toString());
  2221. System.out.println("运行定时任务1:" + clientId);
  2222. }
  2223. };
  2224. // 定时任务2
  2225. TimerTask timerTask1 = new TimerTask() {
  2226. @Override
  2227. public void run() {
  2228. equipmentService.sentMessage(clientId, PushUtils.buildJson("cardPayXCX", kind).toString());
  2229. System.out.println("运行定时任务1:" + clientId);
  2230. }
  2231. };
  2232. // 计时器
  2233. Timer timer = new Timer();
  2234. // 添加执行任务(延迟 5s 执行)
  2235. timer.schedule(timerTask, 5000);
  2236. Timer timer1 = new Timer();
  2237. timer1.schedule(timerTask1, 10000);
  2238. }
  2239. private void sentJingZhun( TOrder order) {
  2240. order = orderService.getById(order.getId());
  2241. int i = 0;
  2242. TAdmin admin = R.getDataIfSuccess(szwlFeign.getAdmin(String.valueOf(order.getAdminId())));
  2243. if(admin.getId().toString().equals("372")||admin.getRelationAdminId().equals("372")||admin.getId().toString().equals("639")||admin.getId().toString().equals("238")){
  2244. //发送数据到鲸准
  2245. String url = "http://47.106.13.109:3000/api/listen";
  2246. Map<String,String> map = new HashMap<>();
  2247. map.put("clientId",order.getClientId());
  2248. map.put("price", String.valueOf(order.getPrice()));
  2249. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  2250. map.put("pay_date",sdf.format(order.getPayDate()));
  2251. if(order.getWxId()!=null){
  2252. //小程序下单
  2253. TSzsmWx szsmWx = szsmWxService.getById(order.getWxId());
  2254. map.put("sourceChannel","1");
  2255. map.put("phone",szsmWx.getPhone());
  2256. if(order.getMarketingAmount()!=null){
  2257. map.put("amount",order.getMarketingAmount().toString());
  2258. }else{
  2259. map.put("amount","0");
  2260. }
  2261. map.put("nickName","1");
  2262. map.put("uninoId",szsmWx.getOpenId());
  2263. }else {
  2264. //机器下单
  2265. map.put("sourceChannel","0");
  2266. map.put("phone","");
  2267. map.put("amount","0");
  2268. map.put("nickName","");
  2269. map.put("uninoId","");
  2270. }
  2271. TEquipment byClientId = R.getDataIfSuccess(szwlFeign.findEquipmentByClientId(order.getClientId()));
  2272. map.put("latitude", String.valueOf(byClientId.getLatitude()));
  2273. map.put("longitude", String.valueOf(byClientId.getLongitude()));
  2274. String data = JSON.toJSONString(map);
  2275. try {
  2276. String s = HttpClientSslUtils.doPost(url, data);
  2277. System.out.println("发送鲸准:"+s);
  2278. } catch (Exception e) {
  2279. e.printStackTrace();
  2280. }
  2281. }
  2282. }
  2283. /**
  2284. * 支付成功回调 杉德
  2285. *
  2286. * @return
  2287. */
  2288. @RequestMapping(value = "/shandeRefund", method = RequestMethod.GET)
  2289. @ResponseBody
  2290. public Object shandeRefund(HttpServletRequest request) {
  2291. //订单号
  2292. String order_no = request.getParameter("out_order_no");
  2293. //交易状态
  2294. String trade_status = request.getParameter("trade_status");
  2295. String r6_Status = request.getParameter("r6_Status");
  2296. LambdaQueryWrapper<TOrder> query = Wrappers.lambdaQuery();
  2297. query.eq(TOrder::getSn,order_no);
  2298. if(StringUtils.isEmpty(order_no)){
  2299. return "error";
  2300. }
  2301. List<TOrder> list =orderService.list(query);
  2302. TOrder order = new TOrder();
  2303. if(list.size()>0){
  2304. order = list.get(0);
  2305. }
  2306. if (trade_status.equals("SUCCESS")) {
  2307. // 已支付
  2308. order.setStatus(1);
  2309. // 支付平台产生的流水号
  2310. String bank_order_no = request.getParameter("bank_order_no");
  2311. order.setTrxNo(bank_order_no);
  2312. // 格式:YYYY-MM-DD HH:mm:ss
  2313. String pay_success_time = request.getParameter("pay_success_time");
  2314. try {
  2315. SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
  2316. Date payDate = sdf.parse(URLDecoder.decode(pay_success_time, "UTF-8"));
  2317. order.setPayDate(payDate);
  2318. } catch (ParseException e) {
  2319. e.printStackTrace();
  2320. } catch (UnsupportedEncodingException e) {
  2321. e.printStackTrace();
  2322. }
  2323. orderService.updateById(order);
  2324. JSONObject kindData = new JSONObject();
  2325. kindData.put("sn", order.getSn());
  2326. kindData.put("productName", order.getProductName());
  2327. TEquipment byClientId = R.getDataIfSuccess(szwlFeign.findEquipmentByClientId(order.getClientId()));
  2328. String gtClientId = byClientId.getGtClientId();
  2329. equipmentService.sentMessage(byClientId.getClientId(),PushUtils.buildJson("pay_success", kindData.toString()).toString());
  2330. // PushUtils.push(gtClientId, "支付成功", "您的订单支付成功", PushUtils.buildJson("pay_success", kindData.toString()).toString());
  2331. order.setNote(gtClientId + "已推送");
  2332. try {
  2333. if(order.getProductNumber()==null||order.getProductNumber()==0){
  2334. order.setProductNumber(1);
  2335. }
  2336. esTOrderService.insertData(order);
  2337. orderFeign.saveToEs(order);
  2338. }catch (Exception e) {
  2339. e.printStackTrace();
  2340. }
  2341. //对金额表的当日数额进行修改
  2342. //1,获取商家的等级
  2343. BigDecimal price = order.getPrice();
  2344. TAdmin admin = R.getDataIfSuccess(szwlFeign.getAdmin(String.valueOf(byClientId.getAdminId())));
  2345. //获取分销 关联设备
  2346. TProportion proportion = R.getDataIfSuccess(szwlFeign.getProportion(String.valueOf(byClientId.getId())));
  2347. Integer type = proportion.getType();
  2348. switch (type) {
  2349. case 0:
  2350. BigDecimal agencyProportion = proportion.getProportion();
  2351. // 代理分销获得利润
  2352. BigDecimal agencyPrice = price.multiply(agencyProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  2353. //修改金额表的今日金额
  2354. TShandeMch shandeMch = R.getDataIfSuccess(szwlFeign.getShandeMch(String.valueOf(admin.getId())));
  2355. BigDecimal todayBalance = shandeMch.getTodayBalance();
  2356. todayBalance = todayBalance.add(agencyPrice).setScale(2, RoundingMode.HALF_DOWN);
  2357. shandeMch.setTodayBalance(todayBalance);
  2358. szwlFeign.updateShandeMch(shandeMch);
  2359. break;
  2360. case 1:
  2361. BigDecimal agencyProportion1 = proportion.getAgencyProportion();
  2362. BigDecimal merchantProportion1 = proportion.getProportion();
  2363. // 代理分销获得利润
  2364. BigDecimal agencyPrice1 = price.multiply(agencyProportion1.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  2365. // 经销商分销获得利润
  2366. BigDecimal merchantAmount1 = price.multiply(merchantProportion1.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  2367. //修改代理金额表的今日金额
  2368. // ArrayList<Filter> filters1 = new ArrayList<>();
  2369. // filters1.add(Filter.eq("adminId", admin.getAgencyId()));
  2370. TShandeMch shandeMch2 = R.getDataIfSuccess(szwlFeign.getShandeMch(String.valueOf(proportion.getAgencyId())));
  2371. BigDecimal todayBalance2 = shandeMch2.getTodayBalance();
  2372. todayBalance2 = todayBalance2.add(agencyPrice1).setScale(2, RoundingMode.HALF_DOWN);
  2373. shandeMch2.setTodayBalance(todayBalance2);
  2374. szwlFeign.updateShandeMch(shandeMch2);
  2375. //修改经销商金额表的今日金额
  2376. // ArrayList<Filter> filters2 = new ArrayList<>();
  2377. // filters2.add(Filter.eq("adminId", admin.getId()));
  2378. TShandeMch shandeMch3 = R.getDataIfSuccess(szwlFeign.getShandeMch(String.valueOf(admin.getId())));
  2379. BigDecimal todayBalance3 = shandeMch3.getTodayBalance();
  2380. todayBalance3 = todayBalance3.add(merchantAmount1).setScale(2, RoundingMode.HALF_DOWN);
  2381. shandeMch3.setTodayBalance(todayBalance3);
  2382. szwlFeign.updateShandeMch(shandeMch3);
  2383. break;
  2384. case 2:
  2385. BigDecimal agencyProportion2 = proportion.getAgencyProportion();
  2386. BigDecimal merchantProportion2 = proportion.getMerchantProportion();
  2387. BigDecimal personageProportion2 = proportion.getProportion();
  2388. // 代理分销获得利润
  2389. BigDecimal agencyAmount = price.multiply(agencyProportion2.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  2390. // 经销商分销获得利润
  2391. BigDecimal merchantAmount = price.multiply(merchantProportion2.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  2392. // 经销商分销获得利润
  2393. BigDecimal personageAmount = price.multiply(personageProportion2.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  2394. //修改代理金额表的今日金额
  2395. // ArrayList<Filter> filters3 = new ArrayList<>();
  2396. // filters3.add(Filter.eq("adminId", admin.getAgencyId()));
  2397. // List<ShandeMch> list3 = shandeMchService.findList(null,filters3,null);
  2398. TShandeMch shandeMch4 = R.getDataIfSuccess(szwlFeign.getShandeMch(String.valueOf(proportion.getAgencyId())));
  2399. BigDecimal todayBalance4 = shandeMch4.getTodayBalance();
  2400. todayBalance4 = todayBalance4.add(agencyAmount).setScale(2, RoundingMode.HALF_DOWN);
  2401. shandeMch4.setTodayBalance(todayBalance4);
  2402. szwlFeign.updateShandeMch(shandeMch4);
  2403. //修改经销商金额表的今日金额
  2404. TShandeMch shandeMch5 = R.getDataIfSuccess(szwlFeign.getShandeMch(String.valueOf(proportion.getMerchantId())));
  2405. BigDecimal todayBalance5 = shandeMch5.getTodayBalance();
  2406. todayBalance5 = todayBalance5.add(merchantAmount).setScale(2, RoundingMode.HALF_DOWN);
  2407. shandeMch5.setTodayBalance(todayBalance5);
  2408. szwlFeign.updateShandeMch(shandeMch5);
  2409. //修改终端金额表的今日金额
  2410. TShandeMch shandeMch6 = R.getDataIfSuccess(szwlFeign.getShandeMch(String.valueOf(admin.getId())));
  2411. BigDecimal todayBalance6 = shandeMch6.getTodayBalance();
  2412. todayBalance6 = todayBalance6.add(personageAmount).setScale(2, RoundingMode.HALF_DOWN);
  2413. shandeMch6.setTodayBalance(todayBalance6);
  2414. szwlFeign.updateShandeMch(shandeMch6);
  2415. break;
  2416. }
  2417. return "SUCCESS";
  2418. } else {
  2419. TEquipment byClientId = R.getDataIfSuccess(szwlFeign.findEquipmentByClientId(order.getClientId()));
  2420. String gtClientId = byClientId.getGtClientId();
  2421. JSONObject kindData = new JSONObject();
  2422. kindData.put("sn", order.getSn());
  2423. equipmentService.sentMessage(byClientId.getClientId(),PushUtils.buildJson("pay_faile", kindData.toString()).toString());
  2424. // PushUtils.push(gtClientId, "支付成功", "您的订单支付成功", PushUtils.buildJson("pay_faile", kindData.toString()).toString());
  2425. order.setNote(gtClientId + "支付失败");
  2426. orderService.updateById(order);
  2427. }
  2428. return "SUCCESS";
  2429. }
  2430. /**
  2431. * MG280支付成功回调
  2432. *
  2433. * @return
  2434. */
  2435. @RequestMapping(value = "/mg280Notify", method = RequestMethod.GET)
  2436. @ResponseBody
  2437. public Object mg280Notify(HttpServletRequest request) {
  2438. String r6_Status = request.getParameter("r6_Status");
  2439. // 订单号
  2440. String sn = request.getParameter("r2_OrderNo");
  2441. LambdaQueryWrapper<TOrder> query = Wrappers.lambdaQuery();
  2442. query.eq(TOrder::getSn,sn);
  2443. if(StringUtils.isEmpty(sn)){
  2444. return "error";
  2445. }
  2446. List<TOrder> list = orderService.list(query);
  2447. TOrder order = list.get(0);
  2448. if (JoinpayConstant.r6_Status_100.equals(r6_Status)) {
  2449. // 已支付
  2450. order.setStatus(1);
  2451. // 支付平台产生的流水号
  2452. String r7_TrxNo = request.getParameter("r7_TrxNo");
  2453. order.setTrxNo(r7_TrxNo);
  2454. // 格式:YYYY-MM-DD HH:mm:ss
  2455. String ra_PayTime = request.getParameter("ra_PayTime");
  2456. try {
  2457. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  2458. Date payDate = sdf.parse(URLDecoder.decode(ra_PayTime, "UTF-8"));
  2459. order.setPayDate(payDate);
  2460. } catch (ParseException e) {
  2461. e.printStackTrace();
  2462. } catch (UnsupportedEncodingException e) {
  2463. e.printStackTrace();
  2464. }
  2465. orderService.updateById(order);
  2466. JSONObject kindData = new JSONObject();
  2467. kindData.put("sn", order.getSn());
  2468. kindData.put("productName", order.getProductName());
  2469. TEquipment byClientId = R.getDataIfSuccess(szwlFeign.findEquipmentByClientId(order.getClientId()));
  2470. String gtClientId = byClientId.getGtClientId();
  2471. Date endDate = byClientId.getEndDate();
  2472. Date date = new Date();
  2473. Date time = null;
  2474. double price1Price = 0;
  2475. BigDecimal price = order.getPrice();
  2476. // List<Price> priceList = priceService.findAll();
  2477. // for (Price price1 : priceList) {
  2478. // if (price1.getName().equals("MG280")) {
  2479. // price1Price = price1.getPrice();
  2480. // }
  2481. // }
  2482. TPrice price1 = R.getDataIfSuccess(szwlFeign.getPrice("MG280"));
  2483. price1Price = price1.getPrice();
  2484. BigDecimal city = price.divide(BigDecimal.valueOf(price1Price));
  2485. int ci = city.intValue();
  2486. if (endDate.getTime() < date.getTime()) {
  2487. Calendar c = Calendar.getInstance();
  2488. c.setTime(date);
  2489. int i = c.get(Calendar.MONTH);
  2490. c.set(Calendar.MONTH, i - 1);
  2491. //上个月有多少天
  2492. int actualMaximum = c.getActualMaximum(Calendar.DAY_OF_MONTH);
  2493. long t = date.getTime() - endDate.getTime();
  2494. long m = actualMaximum * 1000 * 60 * 60 * 24l;
  2495. if (t <= m) {
  2496. Calendar calendar = Calendar.getInstance();
  2497. calendar.setTime(endDate);
  2498. calendar.set(Calendar.DAY_OF_MONTH, 10);
  2499. //将小时至0
  2500. calendar.set(Calendar.HOUR_OF_DAY, 0);
  2501. //将分钟至0
  2502. calendar.set(Calendar.MINUTE, 0);
  2503. //将秒至0
  2504. calendar.set(Calendar.SECOND, 0);
  2505. //将毫秒至0
  2506. calendar.set(Calendar.MILLISECOND, 0);
  2507. calendar.add(Calendar.MONTH, ci);
  2508. time = calendar.getTime();
  2509. }
  2510. if (t > m) {
  2511. Calendar calendar = Calendar.getInstance();
  2512. calendar.setTime(date);
  2513. calendar.set(Calendar.DAY_OF_MONTH, 10);
  2514. //将小时至0
  2515. calendar.set(Calendar.HOUR_OF_DAY, 0);
  2516. //将分钟至0
  2517. calendar.set(Calendar.MINUTE, 0);
  2518. //将秒至0
  2519. calendar.set(Calendar.SECOND, 0);
  2520. //将毫秒至0
  2521. calendar.set(Calendar.MILLISECOND, 0);
  2522. calendar.add(Calendar.MONTH, ci);
  2523. time = calendar.getTime();
  2524. }
  2525. } else {
  2526. Calendar calendar = Calendar.getInstance();
  2527. calendar.setTime(endDate);
  2528. calendar.set(Calendar.DAY_OF_MONTH, 10);
  2529. //将小时至0
  2530. calendar.set(Calendar.HOUR_OF_DAY, 0);
  2531. //将分钟至0
  2532. calendar.set(Calendar.MINUTE, 0);
  2533. //将秒至0
  2534. calendar.set(Calendar.SECOND, 0);
  2535. //将毫秒至0
  2536. calendar.set(Calendar.MILLISECOND, 0);
  2537. calendar.add(Calendar.MONTH, ci);
  2538. time = calendar.getTime();
  2539. }
  2540. byClientId.setEndDate(time);
  2541. //修改机器
  2542. szwlFeign.updateByEquipment(byClientId);
  2543. order.setNote(gtClientId + "已推送");
  2544. orderService.updateById(order);
  2545. equipmentService.sentMessage(byClientId.getClientId(),PushUtils.buildJson("endDate", String.valueOf(byClientId.getEndDate().getTime())).toString());
  2546. // PushUtils.push(byClientId.getGtClientId(), "", "", PushUtils.buildJson("endDate", String.valueOf(byClientId.getEndDate().getTime())).toString());
  2547. return "success";
  2548. } else {
  2549. TEquipment byClientId = R.getDataIfSuccess(szwlFeign.findEquipmentByClientId(order.getClientId()));
  2550. String gtClientId = byClientId.getGtClientId();
  2551. JSONObject kindData = new JSONObject();
  2552. kindData.put("sn", order.getSn());
  2553. order.setNote(gtClientId + "支付失败");
  2554. orderService.updateById(order);
  2555. }
  2556. return "success";
  2557. }
  2558. //生成二维码,并返回base64格式
  2559. private String toQrcode(String code) {
  2560. String imgtext = null;
  2561. QRCodeWriter qrCodeWriter = new QRCodeWriter();
  2562. HashMap<EncodeHintType, Object> hints = new HashMap<>();
  2563. hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
  2564. BitMatrix bitMatrix = null;
  2565. try {
  2566. bitMatrix = qrCodeWriter.encode(code, BarcodeFormat.QR_CODE, 600, 600, hints);
  2567. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  2568. MatrixToImageWriter.writeToStream(bitMatrix, "PNG", outputStream);
  2569. Base64.Encoder encoder = Base64.getEncoder();
  2570. String text = encoder.encodeToString(outputStream.toByteArray());
  2571. imgtext = "data:image/png;base64,"+text;
  2572. } catch (Exception e) {
  2573. e.printStackTrace();
  2574. }
  2575. return imgtext;
  2576. }
  2577. /**
  2578. * 汇付退款成功回调
  2579. *
  2580. * @param request
  2581. * @return
  2582. */
  2583. @RequestMapping(value = "/refundNotify", method = RequestMethod.POST)
  2584. @ResponseBody
  2585. public ResponseModel<?> refundNotify(HttpServletRequest request) {
  2586. String respData = request.getParameter("resp_data");
  2587. JSONObject data = JSONObject.parseObject(respData);
  2588. String transStat = data.getString("trans_stat");
  2589. // 订单号
  2590. String sn = data.getString("org_req_seq_id");
  2591. if(StrUtil.hasEmpty(sn)) {
  2592. return R.fail(ResponseCodesEnum.A0001,"订单号为空!");
  2593. }
  2594. LambdaQueryWrapper<TOrder> wrapper = new LambdaQueryWrapper<>();
  2595. wrapper.eq(TOrder::getSn,sn);
  2596. TOrder order = orderService.getOne(wrapper);
  2597. if(order == null) {
  2598. return R.fail(ResponseCodesEnum.A0001,"订单不存在");
  2599. }
  2600. if(HuifuConstant.TRANS_STAT_S.equals(transStat)) {
  2601. if(order.getRefundQuantity() < order.getProductNumber()) {
  2602. order.setStatus(1);
  2603. } else {
  2604. order.setStatus(3);
  2605. }
  2606. String refundOrderNo = data.getString("req_seq_id");
  2607. String ordAmt = data.getString("ord_amt");
  2608. String refundTrxNo = data.getString("hf_seq_id");
  2609. order.setRefundOrderNo(refundOrderNo);
  2610. order.setRefundDate(new Date());
  2611. order.setRefundTrxNo(refundTrxNo);
  2612. // 更改订单明细表
  2613. LambdaQueryWrapper<TOrderDetails> queryWrapper = new LambdaQueryWrapper<>();
  2614. queryWrapper.eq(TOrderDetails::getOrderSn, sn);
  2615. // 处理退款中状态的明细
  2616. queryWrapper.eq(TOrderDetails::getRefundStatus, "4");
  2617. List<TOrderDetails> tOrderDetails = orderDetailsService.list(queryWrapper);
  2618. for (TOrderDetails tOrderDetail : tOrderDetails) {
  2619. Integer refundQuantity = tOrderDetail.getRefundQuantity();
  2620. Integer productNumber = tOrderDetail.getProductNumber();
  2621. if(refundQuantity < productNumber) {
  2622. // 部分退款
  2623. tOrderDetail.setRefundStatus("2");
  2624. orderDetailsService.updateById(tOrderDetail);
  2625. } else {
  2626. // 全部退款
  2627. tOrderDetail.setRefundStatus("3");
  2628. orderDetailsService.updateById(tOrderDetail);
  2629. }
  2630. }
  2631. String acctSplitBunch = "";
  2632. // 部分退款或全部退款
  2633. TProportion proportion = R.getDataIfSuccess(szwlFeign.getProportion(String.valueOf(order.getEquipmentId())));
  2634. if(order.getRefundAmount() != null) {
  2635. // 退款总额
  2636. BigDecimal newRefundAmount = order.getRefundAmount().add(new BigDecimal(ordAmt).setScale(2, RoundingMode.HALF_UP));
  2637. order.setRefundAmount(newRefundAmount);
  2638. // 如果小于订单金额
  2639. if(newRefundAmount.compareTo(order.getPrice()) < 0) {
  2640. acctSplitBunch = getAcctSplitBunch(proportion,order.getPrice().subtract(newRefundAmount), order.getCompanyType()).toString();
  2641. order.setAcctSplitBunch(acctSplitBunch);
  2642. } else {
  2643. acctSplitBunch = getAcctSplitBunch(proportion,order.getPrice(), order.getCompanyType()).toString();
  2644. order.setAcctSplitBunch(acctSplitBunch);
  2645. }
  2646. } else {
  2647. order.setRefundAmount(new BigDecimal(ordAmt).setScale(2,RoundingMode.HALF_UP));
  2648. if(new BigDecimal(ordAmt).setScale(2, RoundingMode.HALF_UP).compareTo(order.getPrice()) < 0) {
  2649. // 重新设置分销
  2650. acctSplitBunch = getAcctSplitBunch(proportion,order.getPrice()
  2651. .subtract(new BigDecimal(ordAmt).setScale(2,RoundingMode.HALF_UP)), order.getCompanyType()).toString();
  2652. order.setAcctSplitBunch(acctSplitBunch);
  2653. }
  2654. }
  2655. try {
  2656. esTOrderService.updateDataById(order);
  2657. }catch (Exception e) {
  2658. e.printStackTrace();
  2659. }finally {
  2660. orderService.updateById(order);
  2661. }
  2662. return R.ok("退款成功");
  2663. }
  2664. return R.fail(ResponseCodesEnum.A0001,"退款失败");
  2665. }
  2666. /**
  2667. * 汇聚退款成功回调
  2668. *
  2669. * @param request
  2670. * @return
  2671. */
  2672. @RequestMapping(value = "/notifyRefund", method = RequestMethod.GET)
  2673. @ResponseBody
  2674. public Object notifyRefund(HttpServletRequest request) {
  2675. String r6_Status = request.getParameter("ra_Status");
  2676. // 订单号
  2677. String sn = request.getParameter("r2_OrderNo");
  2678. // 退款订单号
  2679. // String refundSn = request.getParameter("r3_RefundOrderNo");
  2680. // String num = refundSn.substring(refundSn.length() - 1);
  2681. //// Integer refundNumber = Integer.valueOf(num);
  2682. if(StringUtils.isEmpty(sn)){
  2683. return "error";
  2684. }
  2685. LambdaQueryWrapper<TOrder> query = Wrappers.lambdaQuery();
  2686. query.eq(TOrder::getSn,sn);
  2687. TOrder order = orderService.getOne(query);
  2688. if (order == null) {
  2689. return "error";
  2690. }
  2691. if (order.getStatus() == 3) {
  2692. return "success";
  2693. }
  2694. // 判断退款版本号是否存在
  2695. // String refundOrderNo = order.getRefundOrderNo();
  2696. // if(StringUtils.isNotEmpty(refundOrderNo)) {
  2697. // if(refundSn.equals(refundOrderNo)) {
  2698. // try {
  2699. // esTOrderService.updateDataById(order);
  2700. // }catch (Exception e) {
  2701. // e.printStackTrace();
  2702. // }
  2703. // return "success";
  2704. // }
  2705. // }
  2706. // 订单商品数量
  2707. // Integer productNumber = order.getProductNumber();
  2708. if (JoinpayConstant.r6_Status_100.equals(r6_Status)) {
  2709. if(order.getRefundQuantity() < order.getProductNumber()) {
  2710. order.setStatus(1);
  2711. } else {
  2712. order.setStatus(3);
  2713. }
  2714. // // 设置退款版本号
  2715. // order.setRefundOrderNo(refundSn);
  2716. // // 已退款
  2717. // if(productNumber >= refundNumber){
  2718. // productNumber -= refundNumber;
  2719. // order.setProductNumber(productNumber);
  2720. // }
  2721. // 更改订单明细表
  2722. LambdaQueryWrapper<TOrderDetails> queryWrapper = new LambdaQueryWrapper<>();
  2723. queryWrapper.eq(TOrderDetails::getOrderSn, sn);
  2724. // 处理退款中状态的明细
  2725. queryWrapper.eq(TOrderDetails::getRefundStatus, "4");
  2726. List<TOrderDetails> tOrderDetails = orderDetailsService.list(queryWrapper);
  2727. for (TOrderDetails tOrderDetail : tOrderDetails) {
  2728. Integer refundQuantity = tOrderDetail.getRefundQuantity();
  2729. Integer productNumber = tOrderDetail.getProductNumber();
  2730. if(refundQuantity < productNumber) {
  2731. // 部分退款
  2732. tOrderDetail.setRefundStatus("2");
  2733. orderDetailsService.updateById(tOrderDetail);
  2734. } else {
  2735. // 全部退款
  2736. tOrderDetail.setRefundStatus("3");
  2737. orderDetailsService.updateById(tOrderDetail);
  2738. }
  2739. }
  2740. // r4_refundAmount: 退款金额
  2741. BigDecimal r4_refundAmount = new BigDecimal(request.getParameter("r4_RefundAmount_str"));
  2742. if(order.getRefundMarketingAmount()!=null){
  2743. r4_refundAmount =r4_refundAmount.add(order.getRefundMarketingAmount());
  2744. }
  2745. // price: 订单金额, refundAmount:已退款的金额
  2746. BigDecimal price = order.getPrice();
  2747. BigDecimal refundAmount = order.getRefundAmount();
  2748. TProportion proportion = R.getDataIfSuccess(szwlFeign.getProportion(String.valueOf(order.getEquipmentId())));
  2749. if(order.getRefundAmount() != null) {
  2750. BigDecimal newRefundAmount = refundAmount.add(r4_refundAmount);
  2751. order.setRefundAmount(refundAmount.add(r4_refundAmount));
  2752. // 如果小于订单金额
  2753. if(newRefundAmount.compareTo(price) < 0) {
  2754. JSONArray altInfo = getAltInfo(proportion, price.subtract(newRefundAmount));
  2755. order.setAltInfo(altInfo.toString());
  2756. } else {
  2757. JSONArray altInfo = getAltInfo(proportion, price);
  2758. order.setAltInfo(altInfo.toString());
  2759. }
  2760. } else {
  2761. order.setRefundAmount(r4_refundAmount);
  2762. if(r4_refundAmount.compareTo(price) < 0) {
  2763. JSONArray altInfo = getAltInfo(proportion, price.subtract(r4_refundAmount));
  2764. order.setAltInfo(altInfo.toString());
  2765. }
  2766. }
  2767. // 判断退款金额是否小于订单余额
  2768. // if(r4_refundAmount.compareTo(price)==-1){
  2769. // // refundAmount: 已退款金额
  2770. // BigDecimal refundAmount = order.getRefundAmount();
  2771. // if(refundAmount!=null){
  2772. // // 如果已退款金额不为空,则加上退款金额
  2773. // order.setRefundAmount(refundAmount.add(r4_refundAmount));
  2774. // }else {
  2775. // // 已退款金额
  2776. // order.setRefundAmount(r4_refundAmount);
  2777. // }
  2778. // // 订单设置为支付状态
  2779. // order.setStatus(1);
  2780. // // 改变订单的金额,原先金额减去退款金额
  2781. //// order.setPrice(price.subtract(r4_refundAmount));
  2782. // // 重置分销
  2783. // if(order.getAltInfo()!=null) {
  2784. // TProportion proportion = R.getDataIfSuccess(szwlFeign.getProportion(String.valueOf(order.getEquipmentId())));
  2785. // JSONArray altInfo = getAltInfo(proportion, price.subtract(r4_refundAmount));
  2786. // order.setAltInfo(altInfo.toString());
  2787. // }
  2788. // }
  2789. // 判断退款金额是否等于订单余额
  2790. // if(r4_refundAmount.compareTo(price)==0){
  2791. // BigDecimal refundAmount = order.getRefundAmount();
  2792. //// order.setProductNumber(0);
  2793. // if(refundAmount!=null){
  2794. // order.setRefundAmount(refundAmount.add(r4_refundAmount));
  2795. // // 全部退完款重新加回已退款的金额
  2796. //// order.setPrice(order.getPrice().add(refundAmount));
  2797. // }else {
  2798. // // 退款金额
  2799. // order.setRefundAmount(r4_refundAmount);
  2800. // }
  2801. // order.setStatus(3);
  2802. // if(order.getAltInfo()!=null) {
  2803. // TProportion proportion = R.getDataIfSuccess(szwlFeign.getProportion(String.valueOf(order.getEquipmentId())));
  2804. // JSONArray altInfo = getAltInfo(proportion, order.getRefundAmount());
  2805. // order.setAltInfo(altInfo.toString());
  2806. // }
  2807. // }
  2808. // 支付平台产生的退款流水号
  2809. String r5_RefundTrxNo = request.getParameter("r5_RefundTrxNo");
  2810. order.setRefundTrxNo(r5_RefundTrxNo);
  2811. order.setRefundDate(new Date());
  2812. order.setModifyDate(new Date());
  2813. // orderService.updateById(order);
  2814. String orderStatus = order.getOrderStatus();
  2815. if(StringUtils.isNotEmpty(orderStatus)&&orderStatus.equals("0")){
  2816. if(order.getStatus().toString().equals("3")){
  2817. TEquipment byClientId = R.getDataIfSuccess(szwlFeign.findEquipmentByClientId(order.getClientId()));
  2818. JSONObject kindData = new JSONObject();
  2819. String s = order.getId().toString();
  2820. if(s.length()>6){
  2821. s = s.substring(s.length()-6,s.length());
  2822. }
  2823. kindData.put("mealCode", s);
  2824. equipmentService.sentMessage(byClientId.getClientId(), PushUtils.buildJson("refuse_success", kindData.toString()).toString());
  2825. }
  2826. }
  2827. try {
  2828. esTOrderService.updateDataById(order);
  2829. }catch (Exception e) {
  2830. e.printStackTrace();
  2831. }finally {
  2832. orderService.updateById(order);
  2833. }
  2834. return "success";
  2835. }
  2836. return "success";
  2837. }
  2838. @ApiOperation(value = "添加国内订单")
  2839. @PostMapping("/addOrder")
  2840. public ResponseModel<?> addOrder(@RequestBody TOrder order) {
  2841. boolean save = orderService.save(order);
  2842. return R.ok(save);
  2843. }
  2844. @ApiOperation(value = "修改订单")
  2845. @PostMapping("/updateOrder")
  2846. public ResponseModel<?> updateOrder(@RequestBody TOrder order) {
  2847. orderService.updateById(order);
  2848. LambdaQueryWrapper<TOrderTask> query = Wrappers.lambdaQuery();
  2849. query.eq(TOrderTask::getSn,order.getSn());
  2850. TOrderTask orderTask = orderTaskService.getOne(query);
  2851. orderTask.setStatus(order.getStatus());
  2852. orderTaskService.updateById(orderTask);
  2853. return R.ok();
  2854. }
  2855. @ApiOperation(value = "获取订单")
  2856. @GetMapping("/getOrder")
  2857. public ResponseModel<?> getOrder(@RequestParam String id) {
  2858. TOrder order = orderService.getById(id);
  2859. return R.ok(order);
  2860. }
  2861. @ApiOperation(value = "获取订单")
  2862. @GetMapping("/getOrderBySn")
  2863. public ResponseModel<?> getOrderBySn(@RequestParam String sn) {
  2864. LambdaQueryWrapper<TOrder> wrapper = new LambdaQueryWrapper<>();
  2865. wrapper.eq(TOrder::getSn,sn);
  2866. TOrder order = orderService.getOne(wrapper);
  2867. LambdaQueryWrapper<TOrderDetails> queryWrapper = new LambdaQueryWrapper<>();
  2868. queryWrapper.eq(TOrderDetails::getOrderSn,sn);
  2869. List<TOrderDetails> orderDetails = orderDetailsService.list(queryWrapper);
  2870. OrderDetailsDTO orderDetailsDTO = new OrderDetailsDTO();
  2871. BeanUtil.copyProperties(order,orderDetailsDTO);
  2872. orderDetailsDTO.setOrderDetails(orderDetails);
  2873. return R.ok(orderDetailsDTO);
  2874. }
  2875. /**
  2876. * 优惠码支付成功回调
  2877. *
  2878. * @return
  2879. */
  2880. @RequestMapping(value = "/promoCodeNotify", method = RequestMethod.GET)
  2881. @ResponseBody
  2882. public Object promoCoderefund(HttpServletRequest request) {
  2883. String r6_Status = request.getParameter("r6_Status");
  2884. // 订单号
  2885. String sn = request.getParameter("r2_OrderNo");
  2886. //价格
  2887. String price = request.getParameter("r3_Amount");
  2888. Double money = null;
  2889. TPrice tPrice = R.getDataIfSuccess(szwlFeign.getPrice("优惠码"));
  2890. money = tPrice.getPrice();
  2891. //生成优惠码的个数
  2892. double num = Double.parseDouble(price) / money;
  2893. int number = (int) num;
  2894. LambdaQueryWrapper<TOrder> query = Wrappers.lambdaQuery();
  2895. query.eq(TOrder::getSn,sn);
  2896. List<TOrder> list = new ArrayList<>();
  2897. if(StringUtils.isNotEmpty(sn)){
  2898. list = orderService.list(query);
  2899. }
  2900. TOrder order = list.get(0);
  2901. if (order.getStatus() != 0) {
  2902. return "success";
  2903. }
  2904. if (JoinpayConstant.r6_Status_100.equals(r6_Status)) {
  2905. // 已支付
  2906. order.setStatus(1);
  2907. // 支付平台产生的流水号
  2908. String r7_TrxNo = request.getParameter("r7_TrxNo");
  2909. order.setTrxNo(r7_TrxNo);
  2910. // 格式:YYYY-MM-DD HH:mm:ss
  2911. String ra_PayTime = request.getParameter("ra_PayTime");
  2912. try {
  2913. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  2914. Date payDate = sdf.parse(URLDecoder.decode(ra_PayTime, "UTF-8"));
  2915. order.setPayDate(payDate);
  2916. } catch (ParseException e) {
  2917. e.printStackTrace();
  2918. } catch (UnsupportedEncodingException e) {
  2919. e.printStackTrace();
  2920. }
  2921. orderService.updateById(order);
  2922. JSONObject kindData = new JSONObject();
  2923. kindData.put("sn", order.getSn());
  2924. kindData.put("productName", order.getProductName());
  2925. String mont = order.getRefundTrxNo();
  2926. Long data = Calendar.getInstance().getTimeInMillis();
  2927. Float month = Float.valueOf(mont);
  2928. Float mm = month*10;
  2929. String m = String.valueOf(mm);
  2930. String[] split = m.split("\\.");
  2931. String th = split[0];
  2932. Long t = Long.valueOf(th);
  2933. Long mon = 30L*24L*60L*60L*1000L;
  2934. Long d = data + ((mon*t)/10);
  2935. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  2936. String sd = sdf.format(new Date(Long.parseLong(String.valueOf(d))));
  2937. Date lastUseDate = new Date(Long.parseLong(String.valueOf(d)));
  2938. szwlFeign.addPromoCode(String.valueOf(order.getAdminId()),lastUseDate,number);
  2939. return "支付成功";
  2940. }
  2941. return "success";
  2942. }
  2943. /**
  2944. * 查询订单是否付款
  2945. *
  2946. * @param
  2947. * @param
  2948. * @return
  2949. */
  2950. @RequestMapping(value = "/getOrderStatus", method = RequestMethod.GET)
  2951. @ResponseBody
  2952. public JsonMessage getOrderStatus(String sn) {
  2953. LambdaQueryWrapper<TOrder> query = Wrappers.lambdaQuery();
  2954. query.eq(TOrder::getSn,sn);
  2955. List<TOrder> list = orderService.list(query);
  2956. TOrder order = list.get(0);
  2957. Integer status = order.getStatus();
  2958. String sta = status.toString();
  2959. if(status==1){
  2960. sta="pay";
  2961. }
  2962. return JsonMessage.success(sta);
  2963. }
  2964. /**
  2965. * 上传订单的做糖状态
  2966. *
  2967. * @param
  2968. * @param
  2969. * @return
  2970. */
  2971. @RequestMapping(value = "/sendOrderStatus", method = RequestMethod.GET)
  2972. @ResponseBody
  2973. public JsonMessage sendOrderStatus(String sn,String orderStatus) {
  2974. if(StringUtils.isEmpty(sn)||StringUtils.isEmpty(orderStatus)){
  2975. return JsonMessage.error("数据有空");
  2976. }
  2977. LambdaQueryWrapper<TOrder> query = Wrappers.lambdaQuery();
  2978. query.eq(TOrder::getSn,sn);
  2979. List<TOrder> list = orderService.list(query);
  2980. TOrder order = list.get(0);
  2981. order.setOrderStatus(orderStatus);
  2982. orderService.updateById(order);
  2983. if(orderStatus.equals("1")){
  2984. String s = order.getId().toString();
  2985. String clientId = order.getClientId();
  2986. szsmWxService.subscribeMessage(order.getWxId(),s.substring(s.length()-6,s.length()),clientId.substring(clientId.length()-6,clientId.length()));
  2987. }
  2988. LambdaQueryWrapper<TOrderTask> query1 = Wrappers.lambdaQuery();
  2989. query1.eq(TOrderTask::getSn,sn);
  2990. TOrderTask orderTask = orderTaskService.getOne(query1);
  2991. orderTask.setOrderStatus(orderStatus);
  2992. orderTaskService.updateById(orderTask);
  2993. return JsonMessage.success("success");
  2994. }
  2995. /**
  2996. * 删除订单
  2997. *
  2998. * @param
  2999. * @param
  3000. * @return
  3001. */
  3002. @RequestMapping(value = "/deleteOrder", method = RequestMethod.GET)
  3003. @ResponseBody
  3004. public JsonMessage deleteOrder(String sn) {
  3005. if(StringUtils.isEmpty(sn)){
  3006. return JsonMessage.error(sn + "删除失败"); }
  3007. LambdaQueryWrapper<TOrder> query = Wrappers.lambdaQuery();
  3008. query.eq(TOrder::getSn,sn);
  3009. List<TOrder> list = orderService.list(query);
  3010. TOrder order = list.get(0);
  3011. Integer status = order.getStatus();
  3012. String ss = status.toString();
  3013. if (status==0) {
  3014. orderService.removeById(order.getId());
  3015. return JsonMessage.success(sn + "删除成功");
  3016. }
  3017. // return JsonMessage.success(sn + "删除失败");
  3018. return JsonMessage.success(sn + "删除成功");
  3019. }
  3020. /**
  3021. * 添加订单
  3022. *
  3023. * @param clientId 设备client id
  3024. * @param productName 商品名称
  3025. * @param
  3026. * @return
  3027. */
  3028. @RequestMapping(value = "/addOrder", method = RequestMethod.GET)
  3029. @ResponseBody
  3030. public Object addOrder(String clientId, String productName, String pri,String sn) {
  3031. TEquipment equipment = R.getDataIfSuccess(szwlFeign.findEquipmentByClientId(clientId));
  3032. if (equipment == null) {
  3033. return JsonMessage.error("找不到设备");
  3034. }
  3035. Long equipmentId = equipment.getId();
  3036. BigDecimal price = new BigDecimal(pri);
  3037. TAdmin admin = R.getDataIfSuccess(szwlFeign.getAdmin(String.valueOf(equipment.getAdminId())));
  3038. if (admin == null) {
  3039. return JsonMessage.error("找不到设备商家");
  3040. }
  3041. JSONArray altInfo = new JSONArray();
  3042. String orderType = null;
  3043. TProportion proportion = R.getDataIfSuccess(szwlFeign.getProportion(String.valueOf(equipment.getId())));
  3044. Integer type = proportion.getType();
  3045. switch (type) {
  3046. case 0:
  3047. orderType = "1";
  3048. break;
  3049. case 1:
  3050. orderType = "2";
  3051. break;
  3052. case 2:
  3053. orderType = "3";
  3054. break;
  3055. }
  3056. TProduct product = R.getDataIfSuccess(szwlFeign.getProduct(String.valueOf(equipmentId), productName));
  3057. TOrder order = new TOrder();
  3058. order.setId(IDGenerator.orderID());
  3059. order.setCreateDate(new Date());
  3060. order.setModifyDate(new Date());
  3061. order.setAdminId(admin.getId());
  3062. order.setSn(sn);
  3063. order.setProductId(product.getId());
  3064. order.setProductNo(product.getNo());
  3065. order.setProductName(productName);
  3066. order.setPrice(price);
  3067. order.setClientId(equipment.getClientId());
  3068. order.setEquipmentId(equipmentId);
  3069. order.setAltInfo(altInfo.toString());
  3070. order.setStatus(1);
  3071. order.setType(type);
  3072. orderService.save(order);
  3073. try {
  3074. if(order.getProductNumber()==null||order.getProductNumber()==0){
  3075. order.setProductNumber(1);
  3076. }
  3077. esTOrderService.insertData(order);
  3078. orderFeign.saveToEs(order);
  3079. }catch (Exception e) {
  3080. e.printStackTrace();
  3081. }
  3082. return JsonMessage.success("success");
  3083. }
  3084. /**
  3085. * 汇聚分销明细
  3086. * @param proportion
  3087. * @param price
  3088. * @return
  3089. */
  3090. public JSONArray getAltInfo(TProportion proportion, BigDecimal price) {
  3091. // 获取分账有几方:0:分账方2个,1:分账方3个,2:分账方4个,3:分账方超4个
  3092. Integer type = proportion.getType();
  3093. // 汇聚分账参数
  3094. JSONArray altInfo = new JSONArray();
  3095. BigDecimal agencyProportion = null,
  3096. merchantProportion = null,
  3097. selfProportion = null;
  3098. BigDecimal agencyAmount = null;
  3099. BigDecimal merchantAmount = null;
  3100. BigDecimal selfAmount = null;
  3101. TJoinpayMch agencyMch = null;
  3102. TJoinpayMch merchantMch = null;
  3103. TJoinpayMch selfMch = null;
  3104. JSONObject agencyJson = null;
  3105. JSONObject merchantJson = null;
  3106. JSONObject selfJson = null;
  3107. //取消平台扣手续费
  3108. BigDecimal cutPrice = price.setScale(2, RoundingMode.HALF_UP);
  3109. BigDecimal refuseAmount = new BigDecimal(0.00);
  3110. switch (type) {
  3111. case 0:
  3112. // 商家自己的分销
  3113. selfProportion = proportion.getProportion();
  3114. // 商家分销获得利润
  3115. selfAmount = cutPrice.multiply(selfProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  3116. // 汇聚平台
  3117. selfMch = R.getDataIfSuccess(szwlFeign.getMch(String.valueOf(proportion.getAdminId())));
  3118. if (selfMch == null) {
  3119. return altInfo;
  3120. }
  3121. selfJson = new JSONObject();
  3122. selfJson.put("altMchNo", selfMch.getAltMchNo());
  3123. selfJson.put("altAmount", selfAmount.toString());
  3124. selfJson.put("isGuar", "12");
  3125. altInfo.add(selfJson);
  3126. break;
  3127. case 1:
  3128. // 商家自己的分销
  3129. selfProportion = proportion.getProportion();
  3130. // 一级分销
  3131. agencyProportion = proportion.getAgencyProportion();
  3132. // 商家分销获得利润
  3133. selfAmount = cutPrice.multiply(selfProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  3134. // 汇聚平台
  3135. agencyMch = R.getDataIfSuccess(szwlFeign.getMch(String.valueOf(proportion.getAgencyId())));
  3136. selfMch = R.getDataIfSuccess(szwlFeign.getMch(String.valueOf(proportion.getAdminId())));
  3137. if (selfMch == null || agencyMch == null) {
  3138. return altInfo;
  3139. }
  3140. if(!(agencyProportion.compareTo(refuseAmount) == 0||agencyProportion==null)){
  3141. // 一级分销获得利润
  3142. agencyAmount = cutPrice.multiply(agencyProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  3143. agencyJson = new JSONObject();
  3144. agencyJson.put("altMchNo", agencyMch.getAltMchNo());
  3145. agencyJson.put("altAmount", agencyAmount.toString());
  3146. agencyJson.put("isGuar", "12");
  3147. altInfo.add(agencyJson);
  3148. }
  3149. // 商家自己分销获得利润
  3150. selfJson = new JSONObject();
  3151. selfJson.put("altMchNo", selfMch.getAltMchNo());
  3152. selfJson.put("altAmount", selfAmount.toString());
  3153. selfJson.put("isGuar", "12");
  3154. altInfo.add(selfJson);
  3155. break;
  3156. case 2:
  3157. // 商家自己的分销
  3158. selfProportion = proportion.getProportion();
  3159. // 一级分销
  3160. agencyProportion = proportion.getAgencyProportion();
  3161. // 二级分销
  3162. merchantProportion = proportion.getMerchantProportion();
  3163. // 三级分销
  3164. // personageProportion = proportion.getPersonageProportion();
  3165. // 商家分销获得利润
  3166. selfAmount = cutPrice.multiply(selfProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  3167. // 汇聚
  3168. agencyMch = R.getDataIfSuccess(szwlFeign.getMch(String.valueOf(proportion.getAgencyId())));
  3169. merchantMch = R.getDataIfSuccess(szwlFeign.getMch(String.valueOf(proportion.getMerchantId())));
  3170. selfMch = R.getDataIfSuccess(szwlFeign.getMch(String.valueOf(proportion.getAdminId())));
  3171. if (selfMch == null || agencyMch == null || merchantMch == null) {
  3172. return altInfo;
  3173. }
  3174. if(!(agencyProportion.compareTo(refuseAmount) == 0||agencyProportion==null)){
  3175. // 代理分销获得利润
  3176. agencyAmount = cutPrice.multiply(agencyProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  3177. agencyJson = new JSONObject();
  3178. agencyJson.put("altMchNo", agencyMch.getAltMchNo());
  3179. agencyJson.put("altAmount", agencyAmount.toString());
  3180. agencyJson.put("isGuar", "12");
  3181. altInfo.add(agencyJson);
  3182. }
  3183. if(!(merchantProportion.compareTo(refuseAmount) == 0||merchantProportion==null)){
  3184. // 经销商分销获得利润
  3185. merchantAmount = cutPrice.multiply(merchantProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  3186. merchantJson = new JSONObject();
  3187. merchantJson.put("altMchNo", merchantMch.getAltMchNo());
  3188. merchantJson.put("altAmount", merchantAmount.toString());
  3189. merchantJson.put("isGuar", "12");
  3190. altInfo.add(merchantJson);
  3191. }
  3192. // 商家自己分销获得利润
  3193. merchantJson = new JSONObject();
  3194. merchantJson.put("altMchNo", selfMch.getAltMchNo());
  3195. merchantJson.put("altAmount", selfAmount.toString());
  3196. merchantJson.put("isGuar", "12");
  3197. altInfo.add(merchantJson);
  3198. break;
  3199. }
  3200. Iterator<Object> iterator = altInfo.iterator();
  3201. while (iterator.hasNext()) {
  3202. JSONObject jsonObject = (JSONObject) iterator.next();
  3203. String altAmount = jsonObject.getString("altAmount");
  3204. if (altAmount.equals("0.00")) {
  3205. iterator.remove();
  3206. }
  3207. }
  3208. return altInfo;
  3209. }
  3210. /**
  3211. * 汇付分销明细
  3212. * @param proportion
  3213. * @param price
  3214. * @return
  3215. */
  3216. public JSONObject getAcctSplitBunch(TProportion proportion, BigDecimal price, String companyType) {
  3217. // 获取分账有几方:0:分账方2个,1:分账方3个,2:分账方4个,3:分账方超4个
  3218. Integer type = proportion.getType();
  3219. // 汇付分账明细参数
  3220. JSONObject acctSplitBunch = new JSONObject();
  3221. JSONArray acctInfos = new JSONArray();
  3222. BigDecimal adminProportion = null,
  3223. agencyProportion = null,
  3224. merchantProportion = null,
  3225. selfProportion = null;
  3226. BigDecimal agencyAmount = null;
  3227. BigDecimal merchantAmount = null;
  3228. BigDecimal selfAmount = null;
  3229. BigDecimal adminAmount = null;
  3230. THuifuMch agencyHuifu = null;
  3231. THuifuMch merchantHuifu = null;
  3232. THuifuMch selfHuifu = null;
  3233. JSONObject agencyJson = null;
  3234. JSONObject merchantJson = null;
  3235. JSONObject selfJson = null;
  3236. JSONObject adminJson = null;
  3237. //取消平台扣手续费
  3238. BigDecimal cutPrice = price.setScale(2, RoundingMode.HALF_UP);
  3239. BigDecimal refuseAmount = new BigDecimal(0.00);
  3240. switch (type) {
  3241. case 0:
  3242. // 公司平台分销
  3243. adminProportion = proportion.getAdminProportion();
  3244. // 商家自己的分销
  3245. selfProportion = proportion.getProportion();
  3246. // 平台分销获得利润
  3247. adminAmount = cutPrice.multiply(adminProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_UP);
  3248. // 商家分销获得利润
  3249. selfAmount = cutPrice.multiply(selfProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  3250. // 汇付平台
  3251. // 商家汇付信息:agencyHuifu
  3252. selfHuifu = R.getDataIfSuccess(szwlFeign.getHuifuId(proportion.getAdminId()));
  3253. if (selfHuifu == null) {
  3254. return acctSplitBunch;
  3255. }
  3256. // 公司平台分销
  3257. adminJson = new JSONObject();
  3258. adminJson.put("div_amt", adminAmount.toString());
  3259. // 判断为申泽还是七云
  3260. if(StrUtil.isNotEmpty(companyType) && companyType.equals("1")) {
  3261. adminJson.put("huifu_id", HuifuConstant.SC_DIV_HUIFU_ID);
  3262. } else {
  3263. adminJson.put("huifu_id", HuifuConstant.SZ_DIV_HUIFU_ID);
  3264. }
  3265. acctInfos.add(adminJson);
  3266. // 商家分销获得利润
  3267. selfJson = new JSONObject();
  3268. selfJson.put("div_amt", selfAmount.toString());
  3269. selfJson.put("huifu_id", selfHuifu.getHuifuId());
  3270. acctInfos.add(selfJson);
  3271. break;
  3272. case 1:
  3273. // 公司平台分销
  3274. adminProportion = proportion.getAdminProportion();
  3275. // 商家自己的分销
  3276. selfProportion = proportion.getProportion();
  3277. // 一级分销
  3278. agencyProportion = proportion.getAgencyProportion();
  3279. // 平台分销获得利润
  3280. adminAmount = cutPrice.multiply(adminProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_UP);
  3281. // 商家分销获得利润
  3282. selfAmount = cutPrice.multiply(selfProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  3283. // 汇付平台
  3284. selfHuifu = R.getDataIfSuccess(szwlFeign.getHuifuId(proportion.getAdminId()));
  3285. agencyHuifu = R.getDataIfSuccess(szwlFeign.getHuifuId(proportion.getAgencyId()));
  3286. if (selfHuifu == null || agencyHuifu == null) {
  3287. return acctSplitBunch;
  3288. }
  3289. // 公司平台分销
  3290. adminJson = new JSONObject();
  3291. adminJson.put("div_amt", adminAmount.toString());
  3292. // 判断为申泽还是七云
  3293. if(StrUtil.isNotEmpty(companyType) && companyType.equals("1")) {
  3294. adminJson.put("huifu_id", HuifuConstant.SC_DIV_HUIFU_ID);
  3295. } else {
  3296. adminJson.put("huifu_id", HuifuConstant.SZ_DIV_HUIFU_ID);
  3297. }
  3298. acctInfos.add(adminJson);
  3299. // 商家分销获得利润
  3300. selfJson = new JSONObject();
  3301. selfJson.put("div_amt", selfAmount.toString());
  3302. selfJson.put("huifu_id", selfHuifu.getHuifuId());
  3303. acctInfos.add(selfJson);
  3304. if(!(agencyProportion.compareTo(refuseAmount) == 0||agencyProportion==null)){
  3305. // 一级分销获得利润
  3306. agencyAmount = cutPrice.multiply(agencyProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  3307. agencyJson = new JSONObject();
  3308. agencyJson.put("div_amt", agencyAmount.toString());
  3309. agencyJson.put("huifu_id", agencyHuifu.getHuifuId());
  3310. acctInfos.add(agencyJson);
  3311. }
  3312. break;
  3313. case 2:
  3314. // 公司平台分销
  3315. adminProportion = proportion.getAdminProportion();
  3316. // 商家自己的分销
  3317. selfProportion = proportion.getProportion();
  3318. // 一级分销
  3319. agencyProportion = proportion.getAgencyProportion();
  3320. // 二级分销
  3321. merchantProportion = proportion.getMerchantProportion();
  3322. // 平台分销获得利润
  3323. adminAmount = cutPrice.multiply(adminProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_UP);
  3324. // 商家分销获得利润
  3325. selfAmount = cutPrice.multiply(selfProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  3326. selfHuifu = R.getDataIfSuccess(szwlFeign.getHuifuId(proportion.getAdminId()));
  3327. agencyHuifu = R.getDataIfSuccess(szwlFeign.getHuifuId(proportion.getAgencyId()));
  3328. merchantHuifu = R.getDataIfSuccess(szwlFeign.getHuifuId(proportion.getMerchantId()));
  3329. if (selfHuifu == null || agencyHuifu == null || merchantHuifu == null) {
  3330. return acctSplitBunch;
  3331. }
  3332. // 公司平台分销
  3333. adminJson = new JSONObject();
  3334. adminJson.put("div_amt", adminAmount.toString());
  3335. // 判断为申泽还是七云
  3336. if(StrUtil.isNotEmpty(companyType) && companyType.equals("1")) {
  3337. adminJson.put("huifu_id", HuifuConstant.SC_DIV_HUIFU_ID);
  3338. } else {
  3339. adminJson.put("huifu_id", HuifuConstant.SZ_DIV_HUIFU_ID);
  3340. }
  3341. acctInfos.add(adminJson);
  3342. // 商家分销获得利润
  3343. selfJson = new JSONObject();
  3344. selfJson.put("div_amt", selfAmount.toString());
  3345. selfJson.put("huifu_id", selfHuifu.getHuifuId());
  3346. acctInfos.add(selfJson);
  3347. if(!(agencyProportion.compareTo(refuseAmount) == 0||agencyProportion==null)){
  3348. // 一级分销获得利润
  3349. agencyAmount = cutPrice.multiply(agencyProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  3350. agencyJson = new JSONObject();
  3351. agencyJson.put("div_amt", agencyAmount.toString());
  3352. agencyJson.put("huifu_id", agencyHuifu.getHuifuId());
  3353. acctInfos.add(agencyJson);
  3354. }
  3355. if(!(merchantProportion.compareTo(refuseAmount) == 0||merchantProportion==null)){
  3356. // 二级分销获得利润
  3357. merchantAmount = cutPrice.multiply(merchantProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  3358. merchantJson = new JSONObject();
  3359. merchantJson.put("div_amt", merchantAmount.toString());
  3360. merchantJson.put("huifu_id", merchantHuifu.getHuifuId());
  3361. acctInfos.add(merchantJson);
  3362. }
  3363. break;
  3364. }
  3365. // 计算div_amt的总和
  3366. BigDecimal totalAmount = BigDecimal.ZERO;
  3367. for (int i = 0; i < acctInfos.size(); i++) {
  3368. JSONObject jsonObject = acctInfos.getJSONObject(i);
  3369. BigDecimal divAmt = new BigDecimal(jsonObject.getString("div_amt"));
  3370. totalAmount = totalAmount.add(divAmt);
  3371. }
  3372. // 如果总和超过原本金额,调整div_amt
  3373. if (totalAmount.compareTo(price) > 0) {
  3374. BigDecimal diffAmount = totalAmount.subtract(price);
  3375. for (int i = 0; i < acctInfos.size(); i++) {
  3376. JSONObject jsonObject = acctInfos.getJSONObject(i);
  3377. String huifuId = jsonObject.getString("huifu_id");
  3378. if (huifuId.equals(HuifuConstant.SZ_DIV_HUIFU_ID) || huifuId.equals(HuifuConstant.SC_DIV_HUIFU_ID)) {
  3379. BigDecimal currentDivAmt = new BigDecimal(jsonObject.getString("div_amt"));
  3380. BigDecimal adjustedDivAmt = currentDivAmt.subtract(diffAmount).max(BigDecimal.ZERO);
  3381. jsonObject.put("div_amt", adjustedDivAmt.toString());
  3382. break;
  3383. }
  3384. }
  3385. }
  3386. acctSplitBunch.put("acct_infos", acctInfos);
  3387. return acctSplitBunch;
  3388. }
  3389. /**
  3390. * 旧版退款成功回调
  3391. *
  3392. * @param request
  3393. * @return
  3394. */
  3395. @RequestMapping(value = "/oldNotifyRefund", method = RequestMethod.GET)
  3396. @ResponseBody
  3397. public Object oldNotifyRefund(HttpServletRequest request) {
  3398. String r6_Status = request.getParameter("ra_Status");
  3399. // 订单号
  3400. String sn = request.getParameter("r2_OrderNo");
  3401. // 退款订单号
  3402. String refundSn = request.getParameter("r3_RefundOrderNo");
  3403. String num = refundSn.substring(refundSn.length() - 1);
  3404. Integer refundNumber = Integer.valueOf(num);
  3405. // LambdaQueryWrapper<TOrder> query = Wrappers.lambdaQuery();
  3406. LambdaQueryWrapper<TOrder> query = new LambdaQueryWrapper<>();
  3407. query.eq(TOrder::getSn,sn);
  3408. if(StringUtils.isEmpty(sn)){
  3409. return "error";
  3410. }
  3411. // List<TOrder> list = orderService.list(query);
  3412. // TOrder order = list.get(0);
  3413. TOrder order = orderService.getOne(query);
  3414. if (order == null) {
  3415. return "success";
  3416. }
  3417. if (order.getStatus() == 3) {
  3418. return "success";
  3419. }
  3420. // 判断退款版本号是否存在
  3421. String refundOrderNo = order.getRefundOrderNo();
  3422. if(StringUtils.isNotEmpty(refundOrderNo)) {
  3423. if(refundSn.equals(refundOrderNo)) {
  3424. try {
  3425. esTOrderService.updateDataById(order);
  3426. }catch (Exception e) {
  3427. e.printStackTrace();
  3428. }
  3429. return "success";
  3430. }
  3431. }
  3432. // 订单商品数量
  3433. Integer productNumber = order.getProductNumber();
  3434. if (JoinpayConstant.r6_Status_100.equals(r6_Status)) {
  3435. // 设置退款版本号
  3436. order.setRefundOrderNo(refundSn);
  3437. // 已退款
  3438. if(productNumber >= refundNumber){
  3439. productNumber -= refundNumber;
  3440. order.setProductNumber(productNumber);
  3441. }
  3442. // r4_refundAmount: 退款金额
  3443. BigDecimal r4_refundAmount = new BigDecimal(request.getParameter("r4_RefundAmount_str"));
  3444. if(order.getRefundMarketingAmount()!=null){
  3445. r4_refundAmount =r4_refundAmount.add(order.getRefundMarketingAmount());
  3446. }
  3447. // price: 当前订单退完款剩余的金额
  3448. BigDecimal price = order.getPrice();
  3449. // 判断退款金额是否小于订单余额
  3450. if(r4_refundAmount.compareTo(price)==-1){
  3451. // refundAmount: 已退款金额
  3452. BigDecimal refundAmount = order.getRefundAmount();
  3453. if(refundAmount!=null){
  3454. // 如果已退款金额不为空,则加上退款金额
  3455. order.setRefundAmount(refundAmount.add(r4_refundAmount));
  3456. }else {
  3457. // 已退款金额
  3458. order.setRefundAmount(r4_refundAmount);
  3459. }
  3460. // 订单设置为支付状态
  3461. order.setStatus(1);
  3462. // 改变订单的金额,原先金额减去退款金额
  3463. order.setPrice(price.subtract(r4_refundAmount));
  3464. // 重置分销
  3465. if(order.getAltInfo()!=null) {
  3466. TEquipment tEquipment = R.getDataIfSuccess(szwlFeign.findEquipmentByClientId(order.getClientId()));
  3467. String altInfo = altInfoReset(tEquipment.getId(), price.subtract(r4_refundAmount));
  3468. order.setAltInfo(altInfo);
  3469. }
  3470. }
  3471. // 判断退款金额是否等于订单余额
  3472. if(r4_refundAmount.compareTo(price)==0){
  3473. BigDecimal refundAmount = order.getRefundAmount();
  3474. // order.setAltInfo("");
  3475. order.setProductNumber(1);
  3476. if(refundAmount!=null){
  3477. order.setRefundAmount(refundAmount.add(r4_refundAmount));
  3478. // 全部退完款重新加回已退款的金额
  3479. order.setPrice(order.getPrice().add(refundAmount));
  3480. }else {
  3481. // 退款金额
  3482. order.setRefundAmount(r4_refundAmount);
  3483. }
  3484. order.setStatus(3);
  3485. if(order.getAltInfo()!=null) {
  3486. TEquipment tEquipment = R.getDataIfSuccess(szwlFeign.findEquipmentByClientId(order.getClientId()));
  3487. String altInfo = altInfoReset(tEquipment.getId(), order.getRefundAmount());
  3488. order.setAltInfo(altInfo);
  3489. }
  3490. }
  3491. // 支付平台产生的退款流水号
  3492. String r5_RefundTrxNo = request.getParameter("r5_RefundTrxNo");
  3493. order.setRefundTrxNo(r5_RefundTrxNo);
  3494. order.setRefundDate(new Date());
  3495. order.setModifyDate(new Date());
  3496. // orderService.updateById(order);
  3497. String orderStatus = order.getOrderStatus();
  3498. if(StringUtils.isNotEmpty(orderStatus)&&orderStatus.equals("0")){
  3499. if(order.getStatus().toString().equals("3")){
  3500. TEquipment byClientId = R.getDataIfSuccess(szwlFeign.findEquipmentByClientId(order.getClientId()));
  3501. JSONObject kindData = new JSONObject();
  3502. String s = order.getId().toString();
  3503. if(s.length()>6){
  3504. s = s.substring(s.length()-6,s.length());
  3505. }
  3506. kindData.put("mealCode", s);
  3507. equipmentService.sentMessage(byClientId.getClientId(), PushUtils.buildJson("refuse_success", kindData.toString()).toString());
  3508. }
  3509. }
  3510. try {
  3511. esTOrderService.updateDataById(order);
  3512. }catch (Exception e) {
  3513. e.printStackTrace();
  3514. }finally {
  3515. orderService.updateById(order);
  3516. }
  3517. return "success";
  3518. }
  3519. return "success";
  3520. }
  3521. public String altInfoReset(Long equipmentId, BigDecimal price) {
  3522. //获取分销 关联设备
  3523. TProportion proportion = R.getDataIfSuccess(szwlFeign.getProportion(String.valueOf(equipmentId)));
  3524. Integer type = proportion.getType();
  3525. JSONArray altInfo = new JSONArray();
  3526. JSONArray altNewInfo = new JSONArray();
  3527. BigDecimal adminProportion = null, agencyProportion = null, merchantProportion = null, personageProportion = null, selfProportion = null;
  3528. Long agencyId = proportion.getAgencyId();
  3529. Long merchantId = proportion.getMerchantId();
  3530. Long personageId = proportion.getPersonageId();
  3531. String orderType = null;
  3532. BigDecimal agencyAmount = null;
  3533. BigDecimal merchantAmount = null;
  3534. BigDecimal personageAmount = null;
  3535. BigDecimal selfAmount = null;
  3536. TJoinpayMch agencyMch = null;
  3537. TJoinpayMch merchantMch = null;
  3538. TJoinpayMch personageMch = null;
  3539. TJoinpayMch selfMch = null;
  3540. JSONObject agencyJson = null;
  3541. JSONObject merchantJson = null;
  3542. JSONObject personageJson = null;
  3543. JSONObject selfJson = null;
  3544. // 砍掉千6手续费
  3545. // BigDecimal cutPrice = price.multiply(new BigDecimal(99.4)).divide(new BigDecimal(100));
  3546. //取消平台扣手续费
  3547. BigDecimal cutPrice = price.multiply(new BigDecimal(100)).divide(new BigDecimal(100));
  3548. switch (type) {
  3549. case 0:
  3550. agencyMch = R.getDataIfSuccess(szwlFeign.getMch(String.valueOf(proportion.getAdminId())));
  3551. agencyProportion = proportion.getProportion();
  3552. adminProportion = proportion.getAdminProportion();
  3553. orderType = "1";
  3554. // 代理分销获得利润
  3555. agencyAmount = cutPrice.multiply(agencyProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  3556. agencyJson = new JSONObject();
  3557. agencyJson.put("altMchNo", agencyMch.getAltMchNo());
  3558. agencyJson.put("altAmount", agencyAmount.toString());
  3559. agencyJson.put("isGuar", "12");
  3560. altInfo.add(agencyJson);
  3561. break;
  3562. case 1:
  3563. agencyMch = R.getDataIfSuccess(szwlFeign.getMch(String.valueOf(proportion.getAgencyId())));
  3564. selfMch = R.getDataIfSuccess(szwlFeign.getMch(String.valueOf(proportion.getAdminId())));
  3565. agencyProportion = proportion.getAgencyProportion();
  3566. adminProportion = proportion.getAdminProportion();
  3567. selfProportion = proportion.getProportion();
  3568. orderType = "2";
  3569. BigDecimal refuseAmount = new BigDecimal(0.00);
  3570. if(agencyProportion.compareTo(refuseAmount) == 0||agencyProportion==null){
  3571. // System.out.println("a等于b");
  3572. }else {
  3573. // 一级分销获得利润
  3574. agencyAmount = cutPrice.multiply(agencyProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  3575. agencyJson = new JSONObject();
  3576. agencyJson.put("altMchNo", agencyMch.getAltMchNo());
  3577. agencyJson.put("altAmount", agencyAmount.toString());
  3578. agencyJson.put("isGuar", "12");
  3579. altInfo.add(agencyJson);
  3580. }
  3581. // 商家自己分销获得利润
  3582. selfAmount = cutPrice.multiply(selfProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  3583. merchantJson = new JSONObject();
  3584. merchantJson.put("altMchNo", selfMch.getAltMchNo());
  3585. merchantJson.put("altAmount", selfAmount.toString());
  3586. merchantJson.put("isGuar", "12");
  3587. altInfo.add(merchantJson);
  3588. break;
  3589. case 2:
  3590. agencyMch = R.getDataIfSuccess(szwlFeign.getMch(String.valueOf(proportion.getAgencyId())));
  3591. merchantMch = R.getDataIfSuccess(szwlFeign.getMch(String.valueOf(proportion.getMerchantId())));
  3592. selfMch = R.getDataIfSuccess(szwlFeign.getMch(String.valueOf(proportion.getAdminId())));
  3593. agencyProportion = proportion.getAgencyProportion();
  3594. adminProportion = proportion.getAdminProportion();
  3595. merchantProportion = proportion.getMerchantProportion();
  3596. personageProportion = proportion.getPersonageProportion();
  3597. selfProportion = proportion.getProportion();
  3598. orderType = "3";
  3599. BigDecimal refuseAmount1 = new BigDecimal(0.00);
  3600. if(agencyProportion.compareTo(refuseAmount1) == 0||agencyProportion==null){
  3601. // System.out.println("a等于b");
  3602. }else {
  3603. // 代理分销获得利润
  3604. agencyAmount = cutPrice.multiply(agencyProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  3605. agencyJson = new JSONObject();
  3606. agencyJson.put("altMchNo", agencyMch.getAltMchNo());
  3607. agencyJson.put("altAmount", agencyAmount.toString());
  3608. agencyJson.put("isGuar", "12");
  3609. altInfo.add(agencyJson);
  3610. }
  3611. if(merchantProportion.compareTo(refuseAmount1) == 0||merchantProportion==null){
  3612. // System.out.println("a等于b");
  3613. }else {
  3614. // 经销商分销获得利润
  3615. merchantAmount = cutPrice.multiply(merchantProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  3616. merchantJson = new JSONObject();
  3617. merchantJson.put("altMchNo", merchantMch.getAltMchNo());
  3618. merchantJson.put("altAmount", merchantAmount.toString());
  3619. merchantJson.put("isGuar", "12");
  3620. altInfo.add(merchantJson);
  3621. }
  3622. // 商家自己分销获得利润
  3623. selfAmount = cutPrice.multiply(selfProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
  3624. merchantJson = new JSONObject();
  3625. merchantJson.put("altMchNo", selfMch.getAltMchNo());
  3626. merchantJson.put("altAmount", selfAmount.toString());
  3627. merchantJson.put("isGuar", "12");
  3628. altInfo.add(merchantJson);
  3629. break;
  3630. }
  3631. return altInfo.toString();
  3632. }
  3633. }