order_processing.dart 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. import 'dart:convert';
  2. import 'package:bbyyy/beans/my_shop_bean_entity.dart';
  3. import 'package:bbyyy/beans/template_bean_entity.dart';
  4. import 'package:bbyyy/https/MyDio.dart';
  5. import 'package:bbyyy/https/url.dart';
  6. import 'package:bbyyy/my_tools/easy_loading/easy_loading.dart';
  7. import 'package:flustars/flustars.dart';
  8. import 'package:flutter/material.dart';
  9. import 'package:flutter/services.dart';
  10. import '../const.dart';
  11. import '../dims.dart';
  12. import '../event_bus.dart';
  13. import '../my_colors.dart';
  14. import '../my_cookie.dart';
  15. import '../my_tools.dart';
  16. import '../my_views.dart';
  17. import '../order.dart';
  18. import '../pop_up_queue.dart';
  19. class OrderProcessing {
  20. OrderProcessing._internal();
  21. static OrderProcessing _singleton = OrderProcessing._internal();
  22. factory OrderProcessing() => _singleton;
  23. List<MyShopBeanDataData> shops = []; //所属货帮
  24. List<TemplateBeanData> orderTemplate = []; //订单模板
  25. String clipboardData = ''; //剪切板数据
  26. MyShopBeanDataData shop; //选中的店铺
  27. TemplateBeanData template; //匹配的模板
  28. //最大金额赢家数量 0---发送订单的人不是最大赢家 1---发订单的人是最大赢家 2---发订单的人是最大赢家且有两个一样的金额
  29. int commissionPayerNum = -1;
  30. String orderHash = ''; //订单hash值
  31. StateSetter ss;
  32. List<OrderInfo2> order; //订单
  33. BuildContext buildContext;
  34. int amountCompleted = 0;
  35. bool saving = false;
  36. //初始化变量
  37. void initialization() {
  38. amountCompleted = 0;
  39. shops.clear();
  40. orderTemplate.clear();
  41. clipboardData = '';
  42. shop = null;
  43. template = null;
  44. commissionPayerNum = -1;
  45. orderHash = '';
  46. order.clear();
  47. EasyLoading.dismiss();
  48. showToast('发送成功');
  49. saving = false;
  50. EventBus().off('saveOrder');
  51. PopUpQueue().onShow = false;
  52. PopUpQueue().showNext();
  53. }
  54. //查询订单模板
  55. queryOrderTemplate(String clipboardData, BuildContext buildContext) {
  56. this.clipboardData = clipboardData;
  57. this.buildContext = buildContext;
  58. print('=======queryOrderTemplate========');
  59. MyDio().query(
  60. {'key': 'order_template', "dims": orderTemplateDims, "filters": {}},
  61. (response, hasError) {
  62. if (!hasError) {
  63. TemplateBeanEntity entity = TemplateBeanEntity()
  64. .fromJson(json.decode(response.data.toString()));
  65. orderTemplate = entity.data;
  66. queryTheStore();
  67. } else {
  68. PopUpQueue().onShow = false;
  69. PopUpQueue().showNext();
  70. }
  71. }, (error) {
  72. PopUpQueue().onShow = false;
  73. PopUpQueue().showNext();
  74. });
  75. }
  76. //查询所属店铺
  77. queryTheStore() {
  78. print('=======queryTheStore========');
  79. MyDio().query({
  80. "key": "shop_user",
  81. "filters": {
  82. "or": true,
  83. "conditions": [
  84. "role!=0",
  85. "user_uid==${MyCookie().getUID()}",
  86. "review_state==1"
  87. ],
  88. "filters": [
  89. {
  90. "conditions": ["role==0", "user_uid==${MyCookie().getUID()}"]
  91. }
  92. ]
  93. },
  94. "dims": shopUserDims,
  95. "paging": [1, 20000]
  96. }, (response, hasError) {
  97. if (!hasError) {
  98. MyShopBeanEntity entity =
  99. MyShopBeanEntity().fromJson(json.decode(response.data.toString()));
  100. if (entity.data.data == null || entity.data.data.isEmpty) {
  101. PopUpQueue().onShow = false;
  102. PopUpQueue().showNext();
  103. return;
  104. }
  105. shops = entity.data.data;
  106. shops.removeWhere((element) => !element.privateShop);
  107. shops.removeWhere((element) => !element.innerTrade);
  108. bool hasMatch = false;
  109. x:
  110. for (int i = 0; i < shops.length; i++) {
  111. shop = shops[i];
  112. matchTemplate();
  113. if (orderHash != '') {
  114. hasMatch = true;
  115. showView();
  116. break x;
  117. }
  118. }
  119. if(!hasMatch){
  120. print('noMatch');
  121. PopUpQueue().onShow = false;
  122. PopUpQueue().showNext();
  123. }
  124. } else {
  125. PopUpQueue().onShow = false;
  126. PopUpQueue().showNext();
  127. }
  128. }, (error) {
  129. PopUpQueue().onShow = false;
  130. PopUpQueue().showNext();
  131. });
  132. }
  133. //优先匹配店铺模板
  134. matchTemplate() {
  135. print('=======================|matchTemplate|=======================');
  136. w:
  137. for (int i = 0; i < orderTemplate.length; i++) {
  138. print(
  139. '=======================|${orderTemplate[i].shopUid}||${shop.shopUid}|=======================');
  140. if (orderTemplate[i].shopUid == shop.shopUid) {
  141. if (parseOrder(orderTemplate[i])) {
  142. break w;
  143. }
  144. }
  145. }
  146. if (orderHash != '') {
  147. } else {
  148. v:
  149. for (int i = 0; i < orderTemplate.length; i++) {
  150. if (orderTemplate[i].shopUid == 0) {
  151. if (parseOrder(orderTemplate[i])) {
  152. break v;
  153. }
  154. }
  155. }
  156. }
  157. }
  158. bool parseOrder(TemplateBeanData element) {
  159. print('=======parseOrder========');
  160. print(
  161. '=========================|${element.template}|=============================');
  162. print(
  163. '=========================|$clipboardData|=============================');
  164. var res;
  165. try {
  166. res = parseString2(
  167. element.template.replaceAll('\r', ''), '''$clipboardData''');
  168. } catch (e) {}
  169. print(res.toString());
  170. if (res != null) {
  171. print('-----------data[i].template-----------');
  172. print(element.template);
  173. print('json.encode(res) =============\n${json.encode(res)}');
  174. var order = parse2(res);
  175. print('order.toString()=============\n${order.toString()}');
  176. var maxAmount = 0.0;
  177. int maxAmountNum = 0;
  178. List<String> maxAmountOwner = [];
  179. res.forEach((key, value) {
  180. if (key.contains('amount')) {
  181. try {
  182. if (double.parse(value) > maxAmount) {
  183. maxAmount = double.parse(value);
  184. maxAmountNum = 1;
  185. maxAmountOwner.clear();
  186. maxAmountOwner.add(res[key.replaceAll('amount', 'person')]);
  187. } else if (double.parse(value) == maxAmount) {
  188. maxAmountNum++;
  189. maxAmountOwner.add(res[key.replaceAll('amount', 'person')]);
  190. }
  191. } catch (e) {}
  192. }
  193. });
  194. if (maxAmountOwner.isNotEmpty) {
  195. maxAmountOwner.forEach((element) {
  196. if (MyCookie().getName().contains(element) ||
  197. element.contains(MyCookie().getName())) {
  198. commissionPayerNum = maxAmountNum;
  199. }
  200. });
  201. }
  202. print('maxAmount============\n$maxAmount\n==============');
  203. print('maxAmountNum==============\n$maxAmountNum\n==============');
  204. print(
  205. 'maxAmountOwner==============\n${maxAmountOwner.toString()}\n==============');
  206. print(
  207. 'commissionPayerNum================ \n$commissionPayerNum\n==============');
  208. order.removeWhere((element) =>
  209. !element.seller.name
  210. .contains(MyCookie().loginInformation.data.extra.name) &&
  211. !MyCookie()
  212. .loginInformation
  213. .data
  214. .extra
  215. .name
  216. .contains(element.seller.name));
  217. if (order.length > 0) {
  218. orderHash = computeOrderHash(res);
  219. this.order = order;
  220. return true;
  221. } else {
  222. return false;
  223. }
  224. } else {
  225. return false;
  226. }
  227. }
  228. showView() {
  229. print('=======|showView|========');
  230. Clipboard.setData(ClipboardData(text: ''));
  231. EasyLoading.instance
  232. ..contentPadding = EdgeInsets.symmetric(horizontal: 0, vertical: 12)
  233. ..alignment = Alignment.bottomCenter
  234. ..loadingStyle = EasyLoadingStyle.light
  235. ..contentMargin = EdgeInsets.fromLTRB(20, 20, 20, 65);
  236. EasyLoading.show(
  237. indicator: Material(
  238. color: Colors.white,
  239. child: StatefulBuilder(
  240. builder: (c, s) {
  241. ss = s;
  242. return Column(
  243. children: [
  244. Container(
  245. child: Stack(
  246. children: [
  247. Container(
  248. child: MyViews().myText('智能订单', MyColors.c333333, 14),
  249. alignment: Alignment.center,
  250. height: 22,
  251. ),
  252. Positioned(
  253. right: 0,
  254. child: GestureDetector(
  255. onTap: () {
  256. initialization();
  257. PopUpQueue().onShow = false;
  258. PopUpQueue().showNext();
  259. },
  260. behavior: HitTestBehavior.translucent,
  261. child: Container(
  262. child: Icon(
  263. Icons.close,
  264. size: 20,
  265. color: Colors.white,
  266. ),
  267. decoration: BoxDecoration(
  268. color: MyColors.cB6B6B6,
  269. borderRadius: BorderRadius.circular(11)),
  270. height: 22,
  271. width: 22,
  272. ),
  273. ),
  274. )
  275. ],
  276. alignment: Alignment.center,
  277. ),
  278. height: 30,
  279. padding: EdgeInsets.symmetric(horizontal: 12),
  280. ),
  281. Container(
  282. height: 5,
  283. color: MyColors.cF7F7F7,
  284. margin: EdgeInsets.only(top: 10),
  285. ),
  286. Column(
  287. children: items(order),
  288. ),
  289. Container(
  290. height: 5,
  291. color: MyColors.cF7F7F7,
  292. margin: EdgeInsets.only(top: 10),
  293. ),
  294. Visibility(
  295. child: Column(
  296. children: [
  297. Container(
  298. child: MyViews().myText('货帮选择', MyColors.c333333, 13),
  299. margin: EdgeInsets.only(left: 7, top: 11),
  300. ),
  301. Container(
  302. margin:
  303. EdgeInsets.symmetric(horizontal: 7, vertical: 10),
  304. height: (shops.length * 1.0 / 2).ceil() *
  305. (double.parse(
  306. '${MediaQuery.of(buildContext).size.width - 74}') /
  307. 2 /
  308. 3.8 +
  309. 10) -
  310. 10,
  311. child: GridView(
  312. gridDelegate:
  313. SliverGridDelegateWithFixedCrossAxisCount(
  314. crossAxisCount: 2, //横轴三个子widget
  315. childAspectRatio: 3.8,
  316. crossAxisSpacing: 10,
  317. mainAxisSpacing: 8 //宽高比为1时,子widget
  318. ),
  319. children: shopItem(shops),
  320. padding: EdgeInsets.all(0),
  321. ),
  322. ),
  323. ],
  324. crossAxisAlignment: CrossAxisAlignment.start,
  325. ),
  326. visible: shops.length != 1,
  327. ),
  328. Container(
  329. height: 0.5,
  330. color: MyColors.cF7F7F7,
  331. margin: EdgeInsets.only(bottom: 10),
  332. ),
  333. Container(
  334. child: GestureDetector(
  335. onTap: () {
  336. if (saving) {
  337. return;
  338. } else {
  339. saving = true;
  340. EventBus().on('saveOrder', (arg) {
  341. amountCompleted++;
  342. if (amountCompleted == order.length) {
  343. initialization();
  344. }
  345. });
  346. order.forEach((element) {
  347. sendOrder(element);
  348. });
  349. }
  350. },
  351. behavior: HitTestBehavior.translucent,
  352. child: Container(
  353. decoration: BoxDecoration(
  354. color: MyColors.cFF4233,
  355. borderRadius: BorderRadius.circular(20),
  356. ),
  357. height: 40,
  358. width: 150,
  359. child: MyViews().myText('发送订单', Colors.white, 14),
  360. alignment: Alignment.center,
  361. ),
  362. ),
  363. alignment: Alignment.center,
  364. )
  365. ],
  366. mainAxisAlignment: MainAxisAlignment.start,
  367. crossAxisAlignment: CrossAxisAlignment.start,
  368. );
  369. },
  370. ),
  371. ),
  372. );
  373. }
  374. //订单视图
  375. items(List<OrderInfo2> order) {
  376. List<Widget> items = [];
  377. order.forEach((element) {
  378. items.add(Container(
  379. child: Column(
  380. children: [
  381. Row(
  382. children: [
  383. Container(
  384. margin: EdgeInsets.only(left: 8, right: 7),
  385. child: ClipRRect(
  386. child: MyViews().netImg(
  387. imgURL(MyCookie().loginInformation.data.extra.picture),
  388. 35,
  389. 35),
  390. borderRadius: BorderRadius.circular(17.5),
  391. ),
  392. ),
  393. Expanded(
  394. child: Container(
  395. height: 35,
  396. child: Column(
  397. children: [
  398. Text(
  399. '付款人:${element.payer.name}',
  400. style: TextStyle(
  401. color: MyColors.c333333,
  402. fontSize: 13,
  403. decoration: TextDecoration.none,
  404. ),
  405. ),
  406. Text(
  407. '订单时间:${element.time}',
  408. style: TextStyle(
  409. color: MyColors.c666666,
  410. fontSize: 10,
  411. decoration: TextDecoration.none,
  412. ),
  413. ),
  414. ],
  415. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  416. crossAxisAlignment: CrossAxisAlignment.start,
  417. ),
  418. ),
  419. ),
  420. Container(
  421. margin: EdgeInsets.only(right: 10),
  422. child: Text(
  423. '¥${(element.payer.amount * element.factor).toStringAsFixed(2)}',
  424. style: TextStyle(
  425. color: MyColors.cFF4233,
  426. fontSize: 14,
  427. decoration: TextDecoration.none,
  428. ),
  429. ),
  430. ),
  431. ],
  432. crossAxisAlignment: CrossAxisAlignment.center,
  433. ),
  434. Container(
  435. height: 0.5,
  436. margin: EdgeInsets.only(right: 10, left: 50, top: 5),
  437. color: MyColors.cE7E7E7,
  438. )
  439. ],
  440. ),
  441. margin: EdgeInsets.only(top: 5),
  442. ));
  443. });
  444. return items;
  445. }
  446. //店铺视图
  447. shopItem(List<MyShopBeanDataData> shops) {
  448. List<Widget> items = [];
  449. for (int i = 0; i < shops.length; i++) {
  450. items.add(GestureDetector(
  451. onTap: () {
  452. MyShopBeanDataData oldShop = shop;
  453. shop = shops[i];
  454. orderHash = '';
  455. matchTemplate();
  456. if (orderHash == '') {
  457. // showToast('店铺选择错误');
  458. shop = oldShop;
  459. matchTemplate();
  460. } else {
  461. ss(() {});
  462. }
  463. },
  464. behavior: HitTestBehavior.translucent,
  465. child: Container(
  466. child: Row(
  467. children: [
  468. Container(
  469. margin: EdgeInsets.only(right: 5),
  470. child: ClipRRect(
  471. child: MyViews().netImg(imgURL(shops[i].shopPic), 30, 30),
  472. borderRadius: BorderRadius.circular(15),
  473. ),
  474. ),
  475. Expanded(
  476. child: Column(
  477. children: [
  478. Text(
  479. shops[i].shopName,
  480. style: TextStyle(
  481. color: shop.shopUid == shops[i].shopUid
  482. ? MyColors.cFF4233
  483. : MyColors.c666666,
  484. fontSize: 12,
  485. ),
  486. maxLines: 1,
  487. overflow: TextOverflow.ellipsis,
  488. softWrap: true,
  489. ),
  490. Text(
  491. 'ID:${shops[i].shopUid}',
  492. style: TextStyle(
  493. color: shop.shopUid == shops[i].shopUid
  494. ? MyColors.cFF4233
  495. : MyColors.c666666,
  496. fontSize: 10,
  497. ),
  498. maxLines: 1,
  499. overflow: TextOverflow.ellipsis,
  500. softWrap: true,
  501. ),
  502. ],
  503. crossAxisAlignment: CrossAxisAlignment.start,
  504. ),
  505. ),
  506. ],
  507. ),
  508. decoration: BoxDecoration(
  509. color: shop.shopUid == shops[i].shopUid
  510. ? MyColors.cFFECEB
  511. : MyColors.cEDEDED,
  512. borderRadius: BorderRadius.circular(4),
  513. ),
  514. constraints: BoxConstraints(maxWidth: 150),
  515. padding: EdgeInsets.symmetric(horizontal: 6, vertical: 6),
  516. ),
  517. ));
  518. }
  519. return items;
  520. }
  521. //发送订单
  522. void sendOrder(OrderInfo2 element) {
  523. MyDio().save({
  524. 'key': 'order',
  525. 'object': {
  526. 'uuid': orderHash,
  527. 'shop_uid': shop.shopUid,
  528. 'shop_pic': shop.shopPic,
  529. 'shop_name': shop.shopName,
  530. 'seller_pic': MyCookie().loginInformation.data.extra.picture,
  531. 'seller_uid': MyCookie().getUID(),
  532. 'seller_name': MyCookie().loginInformation.data.extra.name,
  533. 'buyer_name': element.payer.name,
  534. 'type': orderTypeThirdPlatform,
  535. 'amount': NumUtil.multiply(element.payer.amount, element.factor),
  536. 'commission_payer_num': commissionPayerNum
  537. }
  538. }, (response, hasError) {
  539. if (!hasError) {
  540. EventBus().emit('saveOrder');
  541. }
  542. }, (error) {});
  543. }
  544. }