| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553 |
- import 'dart:convert';
- import 'package:bbyyy/beans/my_shop_bean_entity.dart';
- import 'package:bbyyy/beans/template_bean_entity.dart';
- import 'package:bbyyy/https/MyDio.dart';
- import 'package:bbyyy/https/url.dart';
- import 'package:bbyyy/my_tools/easy_loading/easy_loading.dart';
- import 'package:flustars/flustars.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter/services.dart';
- import '../const.dart';
- import '../dims.dart';
- import '../event_bus.dart';
- import '../my_colors.dart';
- import '../my_cookie.dart';
- import '../my_tools.dart';
- import '../my_views.dart';
- import '../order.dart';
- import '../pop_up_queue.dart';
- class OrderProcessing {
- OrderProcessing._internal();
- static OrderProcessing _singleton = OrderProcessing._internal();
- factory OrderProcessing() => _singleton;
- List<MyShopBeanDataData> shops = []; //所属货帮
- List<TemplateBeanData> orderTemplate = []; //订单模板
- String clipboardData = ''; //剪切板数据
- MyShopBeanDataData shop; //选中的店铺
- TemplateBeanData template; //匹配的模板
- //最大金额赢家数量 0---发送订单的人不是最大赢家 1---发订单的人是最大赢家 2---发订单的人是最大赢家且有两个一样的金额
- int commissionPayerNum = -1;
- String orderHash = ''; //订单hash值
- StateSetter ss;
- List<OrderInfo2> order; //订单
- BuildContext buildContext;
- int amountCompleted = 0;
- bool saving = false;
- //初始化变量
- void initialization() {
- amountCompleted = 0;
- shops.clear();
- orderTemplate.clear();
- clipboardData = '';
- shop = null;
- template = null;
- commissionPayerNum = -1;
- orderHash = '';
- order.clear();
- EasyLoading.dismiss();
- showToast('发送成功');
- saving = false;
- EventBus().off('saveOrder');
- PopUpQueue().onShow = false;
- PopUpQueue().showNext();
- }
- //查询订单模板
- queryOrderTemplate(String clipboardData, BuildContext buildContext) {
- this.clipboardData = clipboardData;
- this.buildContext = buildContext;
- print('=======queryOrderTemplate========');
- MyDio().query(
- {'key': 'order_template', "dims": orderTemplateDims, "filters": {}},
- (response, hasError) {
- if (!hasError) {
- TemplateBeanEntity entity = TemplateBeanEntity()
- .fromJson(json.decode(response.data.toString()));
- orderTemplate = entity.data;
- queryTheStore();
- } else {
- PopUpQueue().onShow = false;
- PopUpQueue().showNext();
- }
- }, (error) {
- PopUpQueue().onShow = false;
- PopUpQueue().showNext();
- });
- }
- //查询所属店铺
- queryTheStore() {
- print('=======queryTheStore========');
- MyDio().query({
- "key": "shop_user",
- "filters": {
- "or": true,
- "conditions": [
- "role!=0",
- "user_uid==${MyCookie().getUID()}",
- "review_state==1"
- ],
- "filters": [
- {
- "conditions": ["role==0", "user_uid==${MyCookie().getUID()}"]
- }
- ]
- },
- "dims": shopUserDims,
- "paging": [1, 20000]
- }, (response, hasError) {
- if (!hasError) {
- MyShopBeanEntity entity =
- MyShopBeanEntity().fromJson(json.decode(response.data.toString()));
- if (entity.data.data == null || entity.data.data.isEmpty) {
- PopUpQueue().onShow = false;
- PopUpQueue().showNext();
- return;
- }
- shops = entity.data.data;
- shops.removeWhere((element) => !element.privateShop);
- shops.removeWhere((element) => !element.innerTrade);
- x:
- for (int i = 0; i < shops.length; i++) {
- shop = shops[i];
- matchTemplate();
- if (orderHash != '') {
- showView();
- break x;
- }
- }
- } else {
- PopUpQueue().onShow = false;
- PopUpQueue().showNext();
- }
- }, (error) {
- PopUpQueue().onShow = false;
- PopUpQueue().showNext();
- });
- }
- //优先匹配店铺模板
- matchTemplate() {
- print('=======================|matchTemplate|=======================');
- w:
- for (int i = 0; i < orderTemplate.length; i++) {
- print(
- '=======================|${orderTemplate[i].ownerUid}||${shop.shopUid}|=======================');
- if (orderTemplate[i].ownerUid == shop.shopUid) {
- if (parseOrder(orderTemplate[i])) {
- break w;
- }
- }
- }
- if (orderHash != '') {
- } else {
- v:
- for (int i = 0; i < orderTemplate.length; i++) {
- if (orderTemplate[i].ownerUid == 0) {
- if (parseOrder(orderTemplate[i])) {
- break v;
- }
- }
- }
- }
- }
- bool parseOrder(TemplateBeanData element) {
- print('=======parseOrder========');
- print(
- '=========================|${element.template}|=============================');
- print(
- '=========================|$clipboardData|=============================');
- var res;
- try {
- res = parseString2(
- element.template.replaceAll('\r', ''), '''$clipboardData''');
- } catch (e) {}
- print(res.toString());
- if (res != null) {
- print('-----------data[i].template-----------');
- print(element.template);
- print('json.encode(res) =============\n${json.encode(res)}');
- var order = parse2(res);
- print('order.toString()=============\n${order.toString()}');
- var maxAmount = 0.0;
- int maxAmountNum = 0;
- List<String> maxAmountOwner = [];
- res.forEach((key, value) {
- if (key.contains('amount')) {
- try {
- if (double.parse(value) > maxAmount) {
- maxAmount = double.parse(value);
- maxAmountNum = 1;
- maxAmountOwner.clear();
- maxAmountOwner.add(res[key.replaceAll('amount', 'person')]);
- } else if (double.parse(value) == maxAmount) {
- maxAmountNum++;
- maxAmountOwner.add(res[key.replaceAll('amount', 'person')]);
- }
- } catch (e) {}
- }
- });
- if (maxAmountOwner.isNotEmpty) {
- maxAmountOwner.forEach((element) {
- if (MyCookie().getName().contains(element) ||
- element.contains(MyCookie().getName())) {
- commissionPayerNum = maxAmountNum;
- }
- });
- }
- print('maxAmount============\n$maxAmount\n==============');
- print('maxAmountNum==============\n$maxAmountNum\n==============');
- print(
- 'maxAmountOwner==============\n${maxAmountOwner.toString()}\n==============');
- print(
- 'commissionPayerNum================ \n$commissionPayerNum\n==============');
- order.removeWhere((element) =>
- !element.seller.name
- .contains(MyCookie().loginInformation.data.extra.name) &&
- !MyCookie()
- .loginInformation
- .data
- .extra
- .name
- .contains(element.seller.name));
- if (order.length > 0) {
- orderHash = computeOrderHash(res);
- this.order = order;
- return true;
- } else {
- return false;
- }
- } else {
- return false;
- }
- }
- showView() {
- print('=======|showView|========');
- Clipboard.setData(ClipboardData(text: ''));
- EasyLoading.instance
- ..contentPadding = EdgeInsets.symmetric(horizontal: 0, vertical: 12)
- ..alignment = Alignment.bottomCenter
- ..loadingStyle = EasyLoadingStyle.light
- ..contentMargin = EdgeInsets.fromLTRB(20, 20, 20, 65);
- EasyLoading.show(
- indicator: Material(
- color: Colors.white,
- child: StatefulBuilder(
- builder: (c, s) {
- ss = s;
- return Column(
- children: [
- Container(
- child: Stack(
- children: [
- Container(
- child: MyViews().myText('智能订单', MyColors.c333333, 14),
- alignment: Alignment.center,
- height: 22,
- ),
- Positioned(
- right: 0,
- child: GestureDetector(
- onTap: () {
- initialization();
- PopUpQueue().onShow = false;
- PopUpQueue().showNext();
- },
- behavior: HitTestBehavior.translucent,
- child: Container(
- child: Icon(
- Icons.close,
- size: 20,
- color: Colors.white,
- ),
- decoration: BoxDecoration(
- color: MyColors.cB6B6B6,
- borderRadius: BorderRadius.circular(11)),
- height: 22,
- width: 22,
- ),
- ),
- )
- ],
- alignment: Alignment.center,
- ),
- height: 30,
- padding: EdgeInsets.symmetric(horizontal: 12),
- ),
- Container(
- height: 5,
- color: MyColors.cF7F7F7,
- margin: EdgeInsets.only(top: 10),
- ),
- Column(
- children: items(order),
- ),
- Container(
- height: 5,
- color: MyColors.cF7F7F7,
- margin: EdgeInsets.only(top: 10),
- ),
- Visibility(
- child: Column(
- children: [
- Container(
- child: MyViews().myText('货帮选择', MyColors.c333333, 13),
- margin: EdgeInsets.only(left: 7, top: 11),
- ),
- Container(
- margin:
- EdgeInsets.symmetric(horizontal: 7, vertical: 10),
- height: (shops.length * 1.0 / 2).ceil() *
- (double.parse(
- '${MediaQuery.of(buildContext).size.width - 74}') /
- 2 /
- 3.8 +
- 10) -
- 10,
- child: GridView(
- gridDelegate:
- SliverGridDelegateWithFixedCrossAxisCount(
- crossAxisCount: 2, //横轴三个子widget
- childAspectRatio: 3.8,
- crossAxisSpacing: 10,
- mainAxisSpacing: 8 //宽高比为1时,子widget
- ),
- children: shopItem(shops),
- padding: EdgeInsets.all(0),
- ),
- ),
- ],
- crossAxisAlignment: CrossAxisAlignment.start,
- ),
- visible: shops.length != 1,
- ),
- Container(
- height: 0.5,
- color: MyColors.cF7F7F7,
- margin: EdgeInsets.only(bottom: 10),
- ),
- Container(
- child: GestureDetector(
- onTap: () {
- if (saving) {
- return;
- } else {
- saving = true;
- EventBus().on('saveOrder', (arg) {
- amountCompleted++;
- if (amountCompleted == order.length) {
- initialization();
- }
- });
- order.forEach((element) {
- sendOrder(element);
- });
- }
- },
- behavior: HitTestBehavior.translucent,
- child: Container(
- decoration: BoxDecoration(
- color: MyColors.cFF4233,
- borderRadius: BorderRadius.circular(20),
- ),
- height: 40,
- width: 150,
- child: MyViews().myText('发送订单', Colors.white, 14),
- alignment: Alignment.center,
- ),
- ),
- alignment: Alignment.center,
- )
- ],
- mainAxisAlignment: MainAxisAlignment.start,
- crossAxisAlignment: CrossAxisAlignment.start,
- );
- },
- ),
- ),
- );
- }
- //订单视图
- items(List<OrderInfo2> order) {
- List<Widget> items = [];
- order.forEach((element) {
- items.add(Container(
- child: Column(
- children: [
- Row(
- children: [
- Container(
- margin: EdgeInsets.only(left: 8, right: 7),
- child: ClipRRect(
- child: MyViews().netImg(
- imgURL(MyCookie().loginInformation.data.extra.picture),
- 35,
- 35),
- borderRadius: BorderRadius.circular(17.5),
- ),
- ),
- Expanded(
- child: Container(
- height: 35,
- child: Column(
- children: [
- Text(
- '付款人:${element.payer.name}',
- style: TextStyle(
- color: MyColors.c333333,
- fontSize: 13,
- decoration: TextDecoration.none,
- ),
- ),
- Text(
- '订单时间:${element.time}',
- style: TextStyle(
- color: MyColors.c666666,
- fontSize: 10,
- decoration: TextDecoration.none,
- ),
- ),
- ],
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- crossAxisAlignment: CrossAxisAlignment.start,
- ),
- ),
- ),
- Container(
- margin: EdgeInsets.only(right: 10),
- child: Text(
- '¥${(element.payer.amount * element.factor).toStringAsFixed(2)}',
- style: TextStyle(
- color: MyColors.cFF4233,
- fontSize: 14,
- decoration: TextDecoration.none,
- ),
- ),
- ),
- ],
- crossAxisAlignment: CrossAxisAlignment.center,
- ),
- Container(
- height: 0.5,
- margin: EdgeInsets.only(right: 10, left: 50, top: 5),
- color: MyColors.cE7E7E7,
- )
- ],
- ),
- margin: EdgeInsets.only(top: 5),
- ));
- });
- return items;
- }
- //店铺视图
- shopItem(List<MyShopBeanDataData> shops) {
- List<Widget> items = [];
- for (int i = 0; i < shops.length; i++) {
- items.add(GestureDetector(
- onTap: () {
- MyShopBeanDataData oldShop = shop;
- shop = shops[i];
- orderHash = '';
- matchTemplate();
- if (orderHash == '') {
- showToast('店铺选择错误');
- shop = oldShop;
- matchTemplate();
- } else {
- ss(() {});
- }
- },
- behavior: HitTestBehavior.translucent,
- child: Container(
- child: Row(
- children: [
- Container(
- margin: EdgeInsets.only(right: 5),
- child: ClipRRect(
- child: MyViews().netImg(imgURL(shops[i].shopPic), 30, 30),
- borderRadius: BorderRadius.circular(15),
- ),
- ),
- Expanded(
- child: Column(
- children: [
- Text(
- shops[i].shopName,
- style: TextStyle(
- color: shop.shopUid == shops[i].shopUid
- ? MyColors.cFF4233
- : MyColors.c666666,
- fontSize: 12,
- ),
- maxLines: 1,
- overflow: TextOverflow.ellipsis,
- softWrap: true,
- ),
- Text(
- 'ID:${shops[i].shopUid}',
- style: TextStyle(
- color: shop.shopUid == shops[i].shopUid
- ? MyColors.cFF4233
- : MyColors.c666666,
- fontSize: 10,
- ),
- maxLines: 1,
- overflow: TextOverflow.ellipsis,
- softWrap: true,
- ),
- ],
- crossAxisAlignment: CrossAxisAlignment.start,
- ),
- ),
- ],
- ),
- decoration: BoxDecoration(
- color: shop.shopUid == shops[i].shopUid
- ? MyColors.cFFECEB
- : MyColors.cEDEDED,
- borderRadius: BorderRadius.circular(4),
- ),
- constraints: BoxConstraints(maxWidth: 150),
- padding: EdgeInsets.symmetric(horizontal: 6, vertical: 6),
- ),
- ));
- }
- return items;
- }
- //发送订单
- void sendOrder(OrderInfo2 element) {
- MyDio().save({
- 'key': 'order',
- 'object': {
- 'uuid': orderHash,
- 'shop_uid': shop.shopUid,
- 'shop_pic': shop.shopPic,
- 'shop_name': shop.shopName,
- 'seller_pic': MyCookie().loginInformation.data.extra.picture,
- 'seller_uid': MyCookie().getUID(),
- 'seller_name': MyCookie().loginInformation.data.extra.name,
- 'buyer_name': element.payer.name,
- 'type': orderTypeThirdPlatform,
- 'amount': NumUtil.multiply(element.payer.amount, element.factor),
- 'commission_payer_num': commissionPayerNum
- }
- }, (response, hasError) {
- if (!hasError) {
- EventBus().emit('saveOrder');
- }
- }, (error) {});
- }
- }
|