order_processing.dart 18 KB

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