TOrderController.java 149 KB

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