order_processing.dart 18 KB

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