TOrderController.java 151 KB

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