TOrderController.java 152 KB

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