TOrderController.java 148 KB

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