order_processing.dart 18 KB

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