StoreOrder.php 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\model\order;
  12. use app\admin\model\user\User;
  13. use app\admin\model\user\UserBill;
  14. use app\admin\model\wechat\WechatUser;
  15. use app\admin\model\ump\StorePink;
  16. use app\admin\model\store\StoreProduct;
  17. use app\wap\model\store\StoreCart;
  18. use traits\ModelTrait;
  19. use basic\ModelBasic;
  20. use service\WechatTemplateService;
  21. use think\Url;
  22. use think\Db;
  23. use service\HookService;
  24. use EasyWeChat\Core\Exception;
  25. use service\SystemConfigService;
  26. use app\wap\model\routine\RoutineTemplate;
  27. use behavior\wap\StoreProductBehavior;
  28. use service\PhpSpreadsheetService;
  29. /**
  30. * 订单管理Model
  31. * Class StoreOrder
  32. * @package app\admin\model\order
  33. */
  34. class StoreOrder extends ModelBasic
  35. {
  36. use ModelTrait;
  37. public static function getOrderList($where)
  38. {
  39. $model = UserBill::where('u.uid', $where['uid'])->alias('u')->join('__STORE_ORDER__ a', 'a.id=u.link_id')
  40. ->where('u.category', 'now_money')->where('u.type', 'brokerage')
  41. ->where(['a.paid' => 1, 'a.is_gift' => 0, 'a.is_receive_gift' => 0])->order('a.add_time desc')->field('a.*');
  42. if ($where['start_date'] && $where['end_date']) $model = $model->where('a.add_time', 'between', [strtotime($where['start_date']), strtotime($where['end_date'])]);
  43. if ($where['excel']) {
  44. $list = $model->select();
  45. $excel = [];
  46. foreach ($list as $item) {
  47. $item['title'] = self::get_title($item);
  48. $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  49. $item['nickname'] = self::getDb('user')->where('uid', $item['uid'])->value('nickname');
  50. $excel[] = [$item['add_time'], $item['order_id'], $item['nickname'], $item['title'], $item['pay_price']];
  51. }
  52. $filename = '直推订单导出' . time() . '.xlsx';
  53. $head = ['时间', '订单号', '用户名', '名称', '订单金额'];
  54. PhpSpreadsheetService::outdata($filename, $excel, $head);
  55. } else
  56. $list = $model->page((int)$where['page'], (int)$where['limit'])->select();
  57. $list = count($list) > 0 ? $list->toArray() : [];
  58. foreach ($list as &$item) {
  59. $item['title'] = self::get_title($item);
  60. $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  61. $item['nickname'] = self::getDb('user')->where('uid', $item['uid'])->value('nickname');
  62. }
  63. return $list;
  64. }
  65. public static function get_title($item)
  66. {
  67. switch ($item['type']) {
  68. case 0:
  69. return self::getDb('special')->where('id', $item['cart_id'])->value('title');
  70. break;
  71. case 1:
  72. return self::getDb('member_ship')->where('id', $item['member_id'])->value('title');
  73. break;
  74. case 2:
  75. $product_id = self::getDb('store_order_cart_info')->where('oid', $item['id'])->value('product_id');
  76. return self::getDb('store_product')->where('id', $product_id)->value('store_name');
  77. break;
  78. }
  79. }
  80. public static function orderCount($type = 0)
  81. {
  82. $data['wz'] = self::statusByWhere(0, new self(), '', $type)->count();
  83. $data['wf'] = self::statusByWhere(1, new self(), '', $type)->count();
  84. $data['sh'] = self::statusByWhere(2, new self(), '', $type)->count();
  85. $data['pj'] = self::statusByWhere(3, new self(), '', $type)->count();
  86. $data['wc'] = self::statusByWhere(4, new self(), '', $type)->count();
  87. $data['pt'] = self::statusByWhere(5, new self(), '', $type)->count();
  88. $data['pu'] = self::statusByWhere(6, new self(), '', $type)->count();
  89. $data['lw'] = self::statusByWhere(7, new self(), '', $type)->count();
  90. $data['sp'] = self::statusByWhere(8, new self(), '', $type)->count();
  91. $data['vip'] = self::statusByWhere(9, new self(), '', $type)->count();
  92. $data['tk'] = self::statusByWhere(-1, new self(), '', $type)->count();
  93. $data['yt'] = self::statusByWhere(-2, new self(), '', $type)->count();
  94. return $data;
  95. }
  96. public static function OrderList($where)
  97. {
  98. $model = self::getOrderWhere($where, self::alias('a')->join('User r', 'r.uid=a.uid', 'LEFT'), 'a.', 'r')->field('a.*,r.nickname,r.phone');
  99. if ($where['order'] != '') {
  100. $model = $model->order(self::setOrder($where['order']));
  101. } else {
  102. $model = $model->order('a.id desc');
  103. }
  104. if (isset($where['mer_id']) && $where['mer_id']) $model->where('mer_id', $where['mer_id']);
  105. if (isset($where['excel']) && $where['excel'] == 1) {
  106. $data = ($data = $model->select()) && count($data) ? $data->toArray() : [];
  107. } else {
  108. $data = ($data = $model->page((int)$where['page'], (int)$where['limit'])->select()) && count($data) ? $data->toArray() : [];
  109. }
  110. foreach ($data as &$item) {
  111. switch ($item['type']) {
  112. case 0:
  113. $item['_info'] = db('special')->where('id', $item['cart_id'])->find();
  114. if ($item['pink_id']) {
  115. $item['pink_name'] = '[拼团订单]';
  116. $item['color'] = '#895612';
  117. } else if ($item['is_gift'] && !$item['pink_id']) {
  118. $item['pink_name'] = '[送礼物订单]';
  119. $item['color'] = '#895612';
  120. } else if (!$item['is_gift'] && !$item['pink_id'] && $item['gift_order_id']) {
  121. $item['pink_name'] = '[领礼物订单]';
  122. $item['color'] = '#895612';
  123. } else {
  124. $item['pink_name'] = '[普通订单]';
  125. $item['color'] = '#895612';
  126. }
  127. if (!$item['_info']) {
  128. $item['_info']['title'] = '专题被删除';
  129. } else {
  130. $len = strlen($item['_info']['title']);
  131. $item['_info']['title'] = $len > 16 ? mb_substr($item['_info']['title'], 0, 16) . '...' : $item['_info']['title'];
  132. }
  133. break;
  134. case 1:
  135. $item['_info'] = db('member_ship')->where('id', $item['member_id'])->find();
  136. $item['pink_name'] = '[会员订单]';
  137. $item['color'] = '#895612';
  138. if (!$item['_info']) $item['_info']['title'] = '会员被删除';
  139. break;
  140. case 2:
  141. $_info = Db::name('store_order_cart_info')->where('oid', $item['id'])->field('cart_info')->select();
  142. foreach ($_info as $k => &$v) {
  143. $cart_info = json_decode($v['cart_info'], true);
  144. if (!isset($cart_info['productInfo'])) $cart_info['productInfo'] = [];
  145. $len = strlen($cart_info['productInfo']['store_name']);
  146. $cart_info['productInfo']['store_name'] = $len > 30 ? mb_substr($cart_info['productInfo']['store_name'], 0, 30) . '...' : $cart_info['productInfo']['store_name'];
  147. $v['cart_info'] = $cart_info;
  148. unset($cart_info);
  149. }
  150. $item['_info'] = $_info;
  151. $item['pink_name'] = '[商品订单]';
  152. $item['color'] = '#895612';
  153. break;
  154. case 3:
  155. $item['_info'] = Db::name('plan')->where('id', $item['cart_id'])->find();
  156. if (!$item['_info']) {
  157. $item['_info']['plan_name'] = '学习计划被删除';
  158. } else {
  159. $len = strlen($item['_info']['plan_name']);
  160. $item['_info']['plan_name'] = $len > 16 ? mb_substr($item['_info']['plan_name'], 0, 16) . '...' : $item['_info']['plan_name'];
  161. }
  162. break;
  163. }
  164. if ($item['paid'] == 1) {
  165. switch ($item['pay_type']) {
  166. case 'weixin':
  167. $item['pay_type_name'] = '微信支付';
  168. break;
  169. case 'yue':
  170. $item['pay_type_name'] = '余额支付';
  171. break;
  172. case 'offline':
  173. $item['pay_type_name'] = '线下支付';
  174. break;
  175. case 'zhifubao':
  176. $item['pay_type_name'] = '支付宝支付';
  177. break;
  178. default:
  179. $item['pay_type_name'] = '其他支付';
  180. break;
  181. }
  182. } else {
  183. switch ($item['pay_type']) {
  184. case 'offline':
  185. $item['pay_type_name'] = '线下支付';
  186. $item['pay_type_info'] = 1;
  187. break;
  188. default:
  189. $item['pay_type_name'] = '未支付';
  190. break;
  191. }
  192. }
  193. if ($item['paid'] == 0 && $item['status'] == 0) {
  194. $item['status_name'] = '未支付';
  195. } else if ($item['paid'] == 1 && $item['status'] == 0 && $item['refund_status'] == 0 && $item['type'] != 2) {
  196. $item['status_name'] = '已支付';
  197. } else if ($item['paid'] == 1 && $item['status'] == 0 && $item['refund_status'] == 0 && $item['type'] == 2) {
  198. $item['status_name'] = '待发货';
  199. } else if ($item['paid'] == 1 && $item['status'] == 1 && $item['refund_status'] == 0 && $item['type'] == 2) {
  200. $item['status_name'] = '待收货';
  201. } else if ($item['paid'] == 1 && $item['status'] == 2 && $item['refund_status'] == 0 && $item['type'] == 2) {
  202. $item['status_name'] = '待评价';
  203. } else if ($item['paid'] == 1 && $item['status'] == 3 && $item['refund_status'] == 0 && $item['type'] == 2) {
  204. $item['status_name'] = '已完成';
  205. } else if ($item['paid'] == 1 && $item['refund_status'] == 1) {
  206. $item['status_name'] = <<<HTML
  207. <b style="color:#f124c7">申请退款</b><br/>
  208. HTML;
  209. } else if ($item['paid'] == 1 && $item['refund_status'] == 2) {
  210. $item['status_name'] = '已退款';
  211. }
  212. if ($item['paid'] == 0 && $item['status'] == 0 && $item['refund_status'] == 0) {
  213. $item['_status'] = 1;
  214. } else if ($item['paid'] == 1 && $item['status'] == 0 && $item['refund_status'] == 0) {
  215. $item['_status'] = 2;
  216. } else if ($item['paid'] == 1 && $item['refund_status'] == 1) {
  217. $item['_status'] = 3;
  218. } else if ($item['paid'] == 1 && $item['status'] == 1 && $item['refund_status'] == 0) {
  219. $item['_status'] = 4;
  220. } else if ($item['paid'] == 1 && $item['status'] == 2 && $item['refund_status'] == 0) {
  221. $item['_status'] = 5;
  222. } else if ($item['paid'] == 1 && $item['status'] == 3 && $item['refund_status'] == 0) {
  223. $item['_status'] = 6;
  224. } else if ($item['paid'] == 1 && $item['refund_status'] == 2) {
  225. $item['_status'] = 7;
  226. }
  227. $item['spread_name'] = '';
  228. $item['spread_name_two'] = '';
  229. if ($item['type'] == 0 || $item['type'] == 1) {
  230. if ($item['link_pay_uid']) {
  231. $spread_name = User::where('uid', $item['link_pay_uid'])->value('nickname');
  232. $item['spread_name'] = $spread_name ? $spread_name . '/' . $item['link_pay_uid'] : '无';
  233. $spread_uid_two = User::where('uid', $item['link_pay_uid'])->value('spread_uid');
  234. if ($spread_uid_two) {
  235. $spread_name_two = User::where('uid', $spread_uid_two)->value('nickname');
  236. $item['spread_name_two'] = $spread_name_two ? $spread_name_two . '/' . $spread_uid_two : '无';
  237. } else {
  238. $item['spread_name_two'] = '无';
  239. }
  240. } else if ($item['spread_uid']) {
  241. $spread_name = User::where('uid', $item['spread_uid'])->value('nickname');
  242. $item['spread_name'] = $spread_name ? $spread_name . '/' . $item['spread_uid'] : '无';
  243. $spread_uid_two = User::where('uid', $item['spread_uid'])->value('spread_uid');
  244. if ($spread_uid_two) {
  245. $spread_name_two = User::where('uid', $spread_uid_two)->value('nickname');
  246. $item['spread_name_two'] = $spread_name_two ? $spread_name_two . '/' . $spread_uid_two : '无';
  247. } else {
  248. $item['spread_name_two'] = '无';
  249. }
  250. } else {
  251. $item['spread_name'] = '无';
  252. $item['spread_name_two'] = '无';
  253. }
  254. } else {
  255. $item['spread_name'] = '不参与分销';
  256. $item['spread_name_two'] = '不参与分销';
  257. }
  258. }
  259. if (isset($where['excel']) && $where['excel'] == 1) {
  260. self::SaveExcel($data, $where['types']);
  261. }
  262. $count = self::getOrderWhere($where, self::alias('a')->join('user r', 'r.uid=a.uid', 'LEFT'), 'a.', 'r')->count();
  263. return compact('count', 'data');
  264. }
  265. /*
  266. * 保存并下载excel
  267. * $list array
  268. * return
  269. */
  270. public static function SaveExcel($list, $type)
  271. {
  272. $export = [];
  273. foreach ($list as $index => $item) {
  274. if ($item['paid'] == 0 && $item['status'] == 0) {
  275. $item['status_name'] = '未支付';
  276. } else if ($item['paid'] == 1 && $item['status'] == 0 && $item['refund_status'] == 0 && $item['type'] != 2) {
  277. $item['status_name'] = '已支付';
  278. } else if ($item['paid'] == 1 && $item['status'] == 0 && $item['refund_status'] == 0 && $item['type'] == 2) {
  279. $item['status_name'] = '待发货';
  280. } else if ($item['paid'] == 1 && $item['status'] == 1 && $item['refund_status'] == 0 && $item['type'] == 2) {
  281. $item['status_name'] = '待收货';
  282. } else if ($item['paid'] == 1 && $item['status'] == 2 && $item['refund_status'] == 0 && $item['type'] == 2) {
  283. $item['status_name'] = '已完成';
  284. } else if ($item['paid'] == 1 && $item['refund_status'] == 1) {
  285. $item['status_name'] = '退款中';
  286. } else if ($item['paid'] == 1 && $item['refund_status'] == 2) {
  287. $item['status_name'] = '已退款';
  288. }
  289. if ($type == 0) {
  290. $special = self::getDb('special')->where('id', $item['cart_id'])->field('title,money')->find();
  291. if ($special) {
  292. $goodsName = $special['title'] . '| ' . $special['money'];
  293. } else {
  294. $goodsName = '专题被删除';
  295. }
  296. $export[] = [
  297. $item['order_id'], $item['pay_type_name'],
  298. $item['total_num'], $item['total_price'], $item['pay_price'], $item['refund_price'],
  299. $goodsName,
  300. $item['spread_name'],
  301. $item['spread_name_two'],
  302. $item['paid'] == 1 ? '已支付' : '未支付' . '/支付时间: ' . ($item['pay_time'] > 0 ? date('Y/m/d H:i', $item['pay_time']) : '暂无'),
  303. $item['status_name'],
  304. $item['nickname'] . '/' . $item['uid'],
  305. $item['phone']
  306. ];
  307. } else if ($type == 2) {
  308. $_info = Db::name('store_order_cart_info')->where('oid', $item['id'])->column('cart_info');
  309. $goodsName = [];
  310. foreach ($_info as $k => $v) {
  311. $v = json_decode($v, true);
  312. $goodsName = implode(
  313. [$v['productInfo']['store_name'],
  314. isset($v['productInfo']['attrInfo']) ? '(' . $v['productInfo']['attrInfo']['suk'] . ')' : '',
  315. "[{$v['cart_num']} * {$v['truePrice']}]"
  316. ], ' ');
  317. }
  318. $item['cartInfo'] = $_info;
  319. $export[] = [
  320. $item['order_id'], $item['pay_type_name'],
  321. $item['total_num'], $item['total_price'], $item['total_postage'], $item['pay_price'], $item['refund_price'],
  322. $goodsName,
  323. $item['spread_name'],
  324. $item['spread_name_two'],
  325. $item['paid'] == 1 ? '已支付' : '未支付' . '/支付时间: ' . ($item['pay_time'] > 0 ? date('Y/m/d H:i', $item['pay_time']) : '暂无'),
  326. $item['status_name'],
  327. $item['nickname'] . '/' . $item['uid'],
  328. '收货人:' . $item['real_name'],
  329. '联系电话:' . $item['user_phone'],
  330. '收货地址:' . $item['user_address']
  331. ];
  332. } else if ($type == 1) {
  333. $_info = db('member_ship')->where('id', $item['member_id'])->find();
  334. if ($_info) {
  335. $goodsName = $_info['title'] . '会员| ' . $_info['price'];
  336. } else {
  337. $goodsName = '会员信息被删除';
  338. }
  339. $export[] = [
  340. $item['order_id'], $item['pay_type_name'],
  341. $item['total_price'], $item['pay_price'], $item['refund_price'],
  342. $goodsName,
  343. $item['spread_name'],
  344. $item['spread_name_two'],
  345. $item['paid'] == 1 ? '已支付' : '未支付' . '/支付时间: ' . ($item['pay_time'] > 0 ? date('Y/m/d H:i', $item['pay_time']) : '暂无'),
  346. $item['status_name'],
  347. $item['nickname'] . '/' . $item['uid'],
  348. $item['phone']
  349. ];
  350. }
  351. }
  352. switch ($type) {
  353. case 0:
  354. $filename = '专题课程订单导出' . time() . '.xlsx';
  355. $head = ['订单号', '支付方式', '专题总数', '专题总价', '支付金额', '退款金额', '专题信息', '推广人', '推广人上级', '支付状态', '订单状态', '微信昵称/UID', '手机号'];
  356. break;
  357. case 1:
  358. $filename = '会员订单导出' . time() . '.xlsx';
  359. $head = ['订单号', '支付方式', '会员总价', '支付金额', '退款金额', '会员信息', '推广人', '推广人上级', '支付状态', '订单状态', '微信昵称/UID', '手机号'];
  360. break;
  361. case 2:
  362. $filename = '商品订单导出' . time() . '.xlsx';
  363. $head = ['订单号', '支付方式', '商品总数', '商品总价', '邮费', '支付金额', '退款金额', '商品信息', '推广人', '推广人上级', '支付状态', '订单状态', '收货人', '联系电话', '收货地址'];
  364. break;
  365. }
  366. PhpSpreadsheetService::outdata($filename, $export, $head);
  367. }
  368. public static function statusByWhere($status, $model = null, $alert = '', $type = 0)
  369. {
  370. if ($model == null) $model = new self;
  371. switch ($type) {
  372. case 0:
  373. $model = $model->where($alert . 'type', 0);
  374. break;
  375. case 1:
  376. $model = $model->where($alert . 'type', 1);
  377. break;
  378. case 2:
  379. $model = $model->where($alert . 'type', 2);
  380. break;
  381. }
  382. if ('' === $status)
  383. return $model;
  384. else if ($status == 0)//未支付
  385. return $model->where($alert . 'paid', 0)->where($alert . 'is_system_del', 0)->where($alert . 'status', 0)->where($alert . 'refund_status', 0);
  386. else if ($status == 1)//已支付 待发货
  387. return $model->where($alert . 'paid', 1)->where($alert . 'is_system_del', 0)->where($alert . 'status', 0)->where($alert . 'refund_status', 0);
  388. else if ($status == 2)//已支付 待收货
  389. return $model->where($alert . 'paid', 1)->where($alert . 'is_system_del', 0)->where($alert . 'status', 1)->where($alert . 'refund_status', 0);
  390. else if ($status == 3)//已支付 待评价
  391. return $model->where($alert . 'paid', 1)->where($alert . 'is_system_del', 0)->where($alert . 'status', 2)->where($alert . 'refund_status', 0);
  392. else if ($status == 4)//已支付 已完成
  393. return $model->where($alert . 'paid', 1)->where($alert . 'is_system_del', 0)->where($alert . 'status', 3)->where($alert . 'refund_status', 0);
  394. else if ($status == 5)//课程订单
  395. return $model->where($alert . 'combination_id', 0)->where($alert . 'is_system_del', 0)->where($alert . 'is_gift', 0);
  396. else if ($status == 6)// 拼团订单
  397. return $model->where($alert . 'combination_id', '>', 0)->where($alert . 'is_system_del', 0)->where($alert . 'is_gift', 0);
  398. else if ($status == 7)// 礼物订单
  399. return $model->where($alert . 'combination_id', 0)->where($alert . 'is_system_del', 0)->where($alert . 'is_gift', '>', 0);
  400. else if ($status == 8)//商品订单
  401. return $model->where($alert . 'combination_id', 0)->where($alert . 'is_system_del', 0)->where($alert . 'is_gift', 0);
  402. else if ($status == 9)//会员订单
  403. return $model->where($alert . 'combination_id', 0)->where($alert . 'is_system_del', 0)->where($alert . 'is_gift', 0);
  404. else if ($status == -1)//退款中
  405. return $model->where($alert . 'paid', 1)->where($alert . 'is_system_del', 0)->where($alert . 'refund_status', 1);
  406. else if ($status == -2)//已退款
  407. return $model->where($alert . 'paid', 1)->where($alert . 'is_system_del', 0)->where($alert . 'refund_status', 2);
  408. else
  409. return $model;
  410. }
  411. /**退金币
  412. * @param $data
  413. * @param $oid
  414. */
  415. public static function returnGoldCoin($oid)
  416. {
  417. $gold_name = SystemConfigService::get('gold_name');//虚拟币名称
  418. $order = self::where('id', $oid)->field('id,order_id,gain_gold_num,uid,type')->find();
  419. if ($order['type'] != 2) return true;
  420. if ($order['gain_gold_num'] <= 0) return true;
  421. $uill = UserBill::where(['link_id' => $order['id'], 'pm' => 1, 'category' => 'gold_num', 'type' => 'gain'])->find();
  422. if (!$uill) return true;
  423. $userInfo = User::where('uid', $order['uid'])->find();
  424. if (bcsub($userInfo['gold_num'], $order['gain_gold_num'], 2) > 0) {
  425. $gold_num = bcsub($userInfo['gold_num'], $order['gain_gold_num'], 2);
  426. } else {
  427. $gold_num = 0;
  428. }
  429. $res1 = User::where('uid', $order['uid'])->update(['gold_num' => $gold_num]);
  430. $res2 = UserBill::expend($gold_name . '返还', $order['uid'], 'gold_num', 'return', $order['gain_gold_num'], $order['id'], $gold_num, '用户退款返还' . floatval($order['gain_gold_num']) . $gold_name);
  431. $res = $res1 && $res2;
  432. return $res;
  433. }
  434. /**退佣金 一级
  435. * @param $data
  436. * @param $oid
  437. */
  438. public static function returnCommissionOne($oid)
  439. {
  440. $order = self::where('id', $oid)->find();
  441. $userInfo = User::where('uid', $order['uid'])->find();
  442. if (!$userInfo || !$userInfo['spread_uid']) return true;
  443. $storeBrokerageStatu = SystemConfigService::get('store_brokerage_statu') ?: 1;//获取后台分销类型
  444. if ($storeBrokerageStatu == 1) {
  445. if (!User::be(['uid' => $userInfo['spread_uid'], 'is_promoter' => 1])) return true;
  446. }
  447. $userInfoOne = User::where('uid', $userInfo['spread_uid'])->find();
  448. $uill = UserBill::where(['link_id' => $order['id'], 'pm' => 1, 'uid' => $userInfoOne['uid'], 'category' => 'now_money', 'type' => 'brokerage'])->find();
  449. if (!$uill) return true;
  450. if (bcsub($userInfoOne['brokerage_price'], $uill['number'], 2) > 0) {
  451. $brokerage_price = bcsub($userInfoOne['brokerage_price'], $uill['number'], 2);
  452. } else {
  453. $brokerage_price = 0;
  454. }
  455. $res1 = User::where('uid', $userInfoOne['uid'])->update(['brokerage_price' => $brokerage_price]);
  456. $res2 = UserBill::expend('佣金返还', $userInfoOne['uid'], 'now_money', 'brokerage_return', $uill['number'], $order['id'], $brokerage_price, '用户退款返还佣金' . floatval($uill['number']) . '元');
  457. $res = $res1 && $res2;
  458. if ($res) {
  459. return self::returnCommissionTwo($userInfoOne, $order);
  460. }
  461. }
  462. /**退佣金 二级
  463. * @param $data
  464. * @param $oid
  465. */
  466. public static function returnCommissionTwo($userInfoOne, $order)
  467. {
  468. if (!$userInfoOne || !$userInfoOne['spread_uid']) return true;
  469. $storeBrokerageStatu = SystemConfigService::get('store_brokerage_statu') ?: 1;//获取后台分销类型
  470. if ($storeBrokerageStatu == 1) {
  471. if (!User::be(['uid' => $userInfoOne['spread_uid'], 'is_promoter' => 1])) return true;
  472. }
  473. $userInfoTwo = User::where('uid', $userInfoOne['spread_uid'])->find();
  474. $uill = UserBill::where(['link_id' => $order['id'], 'pm' => 1, 'uid' => $userInfoTwo['uid'], 'category' => 'now_money', 'type' => 'brokerage'])->find();
  475. if (!$uill) return true;
  476. if (bcsub($userInfoTwo['brokerage_price'], $uill['number'], 2) > 0) {
  477. $brokerage_price = bcsub($userInfoTwo['brokerage_price'], $uill['number'], 2);
  478. } else {
  479. $brokerage_price = 0;
  480. }
  481. $res1 = User::where('uid', $userInfoTwo['uid'])->update(['brokerage_price' => $brokerage_price]);
  482. $res2 = UserBill::expend('佣金返还', $userInfoTwo['uid'], 'now_money', 'brokerage_return', $uill['number'], $order['id'], $brokerage_price, '用户退款返还佣金' . floatval($uill['number']) . '元');
  483. $res = $res1 && $res2;
  484. return $res;
  485. }
  486. public static function timeQuantumWhere($startTime = null, $endTime = null, $model = null)
  487. {
  488. if ($model === null) $model = new self;
  489. if ($startTime != null && $endTime != null)
  490. $model = $model->where('add_time', '>', strtotime($startTime))->where('add_time', '<', strtotime($endTime));
  491. return $model;
  492. }
  493. public static function changeOrderId($orderId)
  494. {
  495. $ymd = substr($orderId, 2, 8);
  496. $key = substr($orderId, 16);
  497. return 'wx' . $ymd . date('His') . $key;
  498. }
  499. /**
  500. * 线下付款
  501. * @param $id
  502. * @return $this
  503. */
  504. public static function updateOffline($id)
  505. {
  506. $orderId = self::where('id', $id)->value('order_id');
  507. $res = self::where('order_id', $orderId)->update(['paid' => 1, 'pay_time' => time()]);
  508. return $res;
  509. }
  510. /**
  511. * 退款发送模板消息
  512. * @param $oid
  513. * $oid 订单id key
  514. */
  515. public static function refundTemplate($data, $oid)
  516. {
  517. $order = self::where('id', $oid)->find();
  518. $wechat_notification_message = SystemConfigService::get('wechat_notification_message');
  519. if ($wechat_notification_message == 1) {
  520. WechatTemplateService::sendTemplate(WechatUser::uidToOpenid($order['uid']), WechatTemplateService::ORDER_REFUND_STATUS, [
  521. 'first' => '亲,您的订单已退款,本次退款' . $data['refund_price'] . '金额',
  522. 'keyword1' => $order['order_id'],
  523. 'keyword2' => $data['refund_price'],
  524. 'keyword3' => date('Y-m-d H:i:s', $order['add_time']),
  525. 'remark' => '请查看账单'
  526. ], '');
  527. } else {
  528. $dataAdmin['character_string7']['value'] = $order['order_id'];
  529. $dataAdmin['time8']['value'] = date('Y-m-d H:i:s', $order['add_time']);
  530. $dataAdmin['amount3']['value'] = $data['refund_price'];
  531. RoutineTemplate::sendOrderRefundSuccess($dataAdmin, $order['uid'], Url::build('wap/special/order', ['uni' => $order['order_id']], true, true));
  532. }
  533. }
  534. /**
  535. * 处理where条件
  536. * @param $where
  537. * @param $model
  538. * @return mixed
  539. */
  540. public static function getOrderWhere($where, $model, $aler = '', $join = '')
  541. {
  542. if ($where['status'] != '') {
  543. $model = self::statusByWhere($where['status'], $model, $aler, $where['types']);
  544. } else {
  545. $model = $model->where($aler . 'is_system_del', 0);
  546. }
  547. if ($where['real_name'] != '') {
  548. $model = $model->where($aler . 'order_id|' . $aler . 'real_name|' . $aler . 'user_phone' . ($join ? '|' . $join . '.nickname|' . $join . '.uid|' . $join . '.phone' : ''), 'LIKE', "%$where[real_name]%");
  549. }
  550. $model = $model->where($aler . 'type', $where['types']);
  551. if ($where['type'] != '') {
  552. switch ($where['type']) {
  553. case 5:
  554. $model = $model->where($aler . 'combination_id', 0)->where($aler . 'is_gift', 0);
  555. break;
  556. case 6:
  557. $model = $model->where($aler . 'combination_id', '>', 0)->where($aler . 'is_gift', 0);
  558. break;
  559. case 7:
  560. $model = $model->where($aler . 'combination_id', 0)->where($aler . 'is_gift', '>', 0);
  561. break;
  562. case 8:
  563. $model = $model->where($aler . 'combination_id', 0)->where($aler . 'is_gift', 0);
  564. break;
  565. case 9:
  566. $model = $model->where($aler . 'combination_id', 0)->where($aler . 'is_gift', 0);
  567. break;
  568. }
  569. }
  570. if ($where['data'] !== '') {
  571. $model = self::getModelTime($where, $model, $aler . 'add_time');
  572. }
  573. return $model;
  574. }
  575. public static function getBadge($where)
  576. {
  577. $price = self::getOrderPrice($where);
  578. switch ($where['types']) {
  579. case 0:
  580. $name = '售出专题';
  581. break;
  582. case 1:
  583. $name = '售出会员';
  584. break;
  585. case 2:
  586. $name = '售出商品';
  587. break;
  588. }
  589. return [
  590. [
  591. 'name' => '订单数量',
  592. 'field' => '件',
  593. 'count' => $price['order_sum'],
  594. 'background_color' => 'layui-bg-blue',
  595. 'col' => 3
  596. ],
  597. [
  598. 'name' => $name,
  599. 'field' => '件',
  600. 'count' => $price['total_num'],
  601. 'background_color' => 'layui-bg-blue',
  602. 'col' => 3
  603. ],
  604. [
  605. 'name' => '订单金额',
  606. 'field' => '元',
  607. 'count' => $price['pay_price'],
  608. 'background_color' => 'layui-bg-blue',
  609. 'col' => 3
  610. ],
  611. [
  612. 'name' => '退款金额',
  613. 'field' => '元',
  614. 'count' => $price['refund_price'],
  615. 'background_color' => 'layui-bg-blue',
  616. 'col' => 3
  617. ],
  618. [
  619. 'name' => '微信支付金额',
  620. 'field' => '元',
  621. 'count' => $price['pay_price_wx'],
  622. 'background_color' => 'layui-bg-blue',
  623. 'col' => 3
  624. ],
  625. // [
  626. // 'name' => '余额支付金额',
  627. // 'field' => '元',
  628. // 'count' => $price['pay_price_yue'],
  629. // 'background_color' => 'layui-bg-blue',
  630. // 'col' => 3
  631. // ],
  632. [
  633. 'name' => '支付宝支付金额',
  634. 'field' => '元',
  635. 'count' => $price['pay_price_zhifubao'],
  636. 'background_color' => 'layui-bg-blue',
  637. 'col' => 3
  638. ]
  639. ];
  640. }
  641. /**
  642. * 处理订单金额
  643. * @param $where
  644. * @return array
  645. */
  646. public static function getOrderPrice($where)
  647. {
  648. $price = array();
  649. $price['pay_price'] = 0;//支付金额
  650. $price['refund_price'] = 0;//退款金额
  651. $price['pay_price_wx'] = 0;//微信支付金额
  652. $price['pay_price_yue'] = 0;//余额支付金额
  653. $price['pay_price_offline'] = 0;//线下支付金额
  654. $price['pay_price_zhifubao'] = 0;//支付宝支付金额
  655. $price['pay_price_other'] = 0;//其他支付金额
  656. $list = self::getOrderWhere($where, self::alias('a')->join('User r', 'r.uid=a.uid', 'LEFT'), 'a.', 'r')->field([
  657. 'sum(a.total_num) as total_num',
  658. 'sum(a.pay_price) as pay_price',
  659. 'sum(a.refund_price) as refund_price'])->find()->toArray();
  660. $price['total_num'] = $list['total_num'];//商品总数
  661. $price['pay_price'] = $list['pay_price'];//支付金额
  662. $price['refund_price'] = $list['refund_price'];//退款金额
  663. $list = self::getOrderWhere($where, self::alias('a')->join('User r', 'r.uid=a.uid', 'LEFT'), 'a.', 'r')->field('sum(a.pay_price) as pay_price,a.pay_type')->group('a.pay_type')->select()->toArray();
  664. foreach ($list as $v) {
  665. if ($v['pay_type'] == 'weixin') {
  666. $price['pay_price_wx'] = $v['pay_price'];
  667. } elseif ($v['pay_type'] == 'yue') {
  668. $price['pay_price_yue'] = $v['pay_price'];
  669. } elseif ($v['pay_type'] == 'offline') {
  670. $price['pay_price_offline'] = $v['pay_price'];
  671. } elseif ($v['pay_type'] == 'zhifubao') {
  672. $price['pay_price_zhifubao'] = $v['pay_price'];
  673. } else {
  674. $price['pay_price_other'] = $v['pay_price'];
  675. }
  676. }
  677. $price['order_sum'] = self::getOrderWhere($where, self::alias('a')->join('User r', 'r.uid=a.uid', 'LEFT'), 'a.', 'r')->count();
  678. return $price;
  679. }
  680. public static function systemPagePink($where)
  681. {
  682. $model = new self;
  683. $model = self::getOrderWherePink($where, $model);
  684. $model = $model->order('id desc');
  685. if ($where['export'] == 1) {
  686. $list = $model->select()->toArray();
  687. $export = [];
  688. foreach ($list as $index => $item) {
  689. if ($item['pay_type'] == 'weixin') {
  690. $payType = '微信支付';
  691. } elseif ($item['pay_type'] == 'yue') {
  692. $payType = '余额支付';
  693. } elseif ($item['pay_type'] == 'offline') {
  694. $payType = '线下支付';
  695. } else {
  696. $payType = '其他支付';
  697. }
  698. $_info = db('store_order_cart_info')->where('oid', $item['id'])->column('cart_info');
  699. $goodsName = [];
  700. foreach ($_info as $k => $v) {
  701. $v = json_decode($v, true);
  702. $goodsName[] = implode(
  703. [$v['productInfo']['store_name'],
  704. isset($v['productInfo']['attrInfo']) ? '(' . $v['productInfo']['attrInfo']['suk'] . ')' : '',
  705. "[{$v['cart_num']} * {$v['truePrice']}]"
  706. ], ' ');
  707. }
  708. $item['cartInfo'] = $_info;
  709. $export[] = [
  710. $item['order_id'], $payType,
  711. $item['total_num'], $item['total_price'], $item['total_postage'], $item['pay_price'], $item['refund_price'],
  712. $item['mark'], $item['remark'],
  713. $item['real_name'] . '/' . $item['user_phone'] . '/' . $item['user_address'],
  714. $goodsName,
  715. $item['paid'] == 1 ? '已支付' : '未支付' . '/支付时间: ' . ($item['pay_time'] > 0 ? date('Y/md H:i', $item['pay_time']) : '暂无')
  716. ];
  717. $list[$index] = $item;
  718. }
  719. $filename = '订单导出' . time() . '.xlsx';
  720. $head = ['订单号', '支付方式', '商品总数', '商品总价', '邮费', '支付金额', '退款金额', '用户备注', '管理员备注', '收货人信息', '商品信息', '支付状态'];
  721. PhpSpreadsheetService::outdata($filename, $export, $head);
  722. }
  723. return self::page($model, function ($item) {
  724. $item['nickname'] = WechatUser::where('uid', $item['uid'])->value('nickname');
  725. $_info = db('store_order_cart_info')->where('oid', $item['id'])->field('cart_info')->select();
  726. foreach ($_info as $k => $v) {
  727. $_info[$k]['cart_info'] = json_decode($v['cart_info'], true);
  728. }
  729. $item['_info'] = $_info;
  730. }, $where);
  731. }
  732. /**
  733. * 处理where条件
  734. * @param $where
  735. * @param $model
  736. * @return mixed
  737. */
  738. public static function getOrderWherePink($where, $model)
  739. {
  740. if ($where['status'] != '') $model = self::statusByWhere($where['status']);
  741. $model = $model->where('combination_id', 'GT', 0);
  742. if ($where['real_name'] != '') {
  743. $model = $model->where('order_id|real_name|user_phone', 'LIKE', "%$where[real_name]%");
  744. }
  745. if ($where['data'] !== '') {
  746. list($startTime, $endTime) = explode(' - ', $where['data']);
  747. $model = $model->where('add_time', '>', strtotime($startTime));
  748. $model = $model->where('add_time', '<', strtotime($endTime));
  749. }
  750. return $model;
  751. }
  752. /**
  753. * 处理订单金额
  754. * @param $where
  755. * @return array
  756. */
  757. public static function getOrderPricePink($where)
  758. {
  759. $model = new self;
  760. $price = array();
  761. $price['pay_price'] = 0;//支付金额
  762. $price['refund_price'] = 0;//退款金额
  763. $price['pay_price_wx'] = 0;//微信支付金额
  764. $price['pay_price_yue'] = 0;//余额支付金额
  765. $price['pay_price_offline'] = 0;//线下支付金额
  766. $price['pay_price_other'] = 0;//其他支付金额
  767. $price['use_integral'] = 0;//用户使用积分
  768. $price['back_integral'] = 0;//退积分总数
  769. $price['deduction_price'] = 0;//抵扣金额
  770. $price['total_num'] = 0; //商品总数
  771. $model = self::getOrderWherePink($where, $model);
  772. $list = $model->select()->toArray();
  773. foreach ($list as $v) {
  774. $price['total_num'] = bcadd($price['total_num'], $v['total_num'], 0);
  775. $price['pay_price'] = bcadd($price['pay_price'], $v['pay_price'], 2);
  776. $price['refund_price'] = bcadd($price['refund_price'], $v['refund_price'], 2);
  777. $price['use_integral'] = bcadd($price['use_integral'], $v['use_integral'], 2);
  778. $price['back_integral'] = bcadd($price['back_integral'], $v['back_integral'], 2);
  779. $price['deduction_price'] = bcadd($price['deduction_price'], $v['deduction_price'], 2);
  780. if ($v['pay_type'] == 'weixin') {
  781. $price['pay_price_wx'] = bcadd($price['pay_price_wx'], $v['pay_price'], 2);
  782. } elseif ($v['pay_type'] == 'yue') {
  783. $price['pay_price_yue'] = bcadd($price['pay_price_yue'], $v['pay_price'], 2);
  784. } elseif ($v['pay_type'] == 'offline') {
  785. $price['pay_price_offline'] = bcadd($price['pay_price_offline'], $v['pay_price'], 2);
  786. } else {
  787. $price['pay_price_other'] = bcadd($price['pay_price_other'], $v['pay_price'], 2);
  788. }
  789. }
  790. return $price;
  791. }
  792. /**
  793. * 获取昨天的订单 首页在使用
  794. * @param int $preDay
  795. * @param int $day
  796. * @return $this|StoreOrder
  797. */
  798. public static function isMainYesterdayCount($preDay = 0, $day = 0)
  799. {
  800. $model = new self();
  801. $model = $model->where('add_time', 'gt', $preDay);
  802. $model = $model->where('add_time', 'lt', $day);
  803. return $model;
  804. }
  805. /**
  806. * 获取用户购买次数
  807. * @param int $uid
  808. * @return int|string
  809. */
  810. public static function getUserCountPay($uid = 0)
  811. {
  812. if (!$uid) return 0;
  813. return self::where('uid', $uid)->where('paid', 1)->count();
  814. }
  815. /**
  816. * 获取单个用户购买列表
  817. * @param array $where
  818. * @return array
  819. */
  820. public static function getOneorderList($where)
  821. {
  822. return self::where(['uid' => $where['uid'], 'paid' => 1])
  823. ->order('add_time desc')
  824. ->page((int)$where['page'], (int)$where['limit'])
  825. ->field(['order_id', 'total_num', 'total_price', 'pay_price',
  826. 'FROM_UNIXTIME(pay_time,"%Y-%m-%d") as pay_time', 'paid', 'pay_type',
  827. 'pink_id'
  828. ])->select()
  829. ->toArray();
  830. }
  831. /*
  832. * 设置订单统计图搜索
  833. * $where array 条件
  834. * return object
  835. */
  836. public static function setEchatWhere($where, $status = null, $time = null)
  837. {
  838. $model = self::statusByWhere($where['status']);
  839. if ($status !== null) $where['type'] = $status;
  840. if ($time === true) $where['data'] = '';
  841. switch ($where['type']) {
  842. case 1:
  843. //普通商品
  844. $model = $model->where('combination_id', 0)->where('seckill_id', 0);
  845. break;
  846. case 2:
  847. //拼团商品
  848. $model = $model->where('combination_id', ">", 0)->where('pink_id', ">", 0);
  849. break;
  850. case 3:
  851. //秒杀商品
  852. $model = $model->where('seckill_id', ">", 0);
  853. break;
  854. case 4:
  855. //砍价商品
  856. $model = $model->where('bargain_id', '>', 0);
  857. break;
  858. }
  859. return self::getModelTime($where, $model);
  860. }
  861. /*
  862. * 获取订单数据统计图
  863. * $where array
  864. * $limit int
  865. * return array
  866. */
  867. public static function getEchartsOrder($where, $limit = 20)
  868. {
  869. $orderlist = self::setEchatWhere($where)->field([
  870. 'FROM_UNIXTIME(add_time,"%Y-%m-%d") as _add_time',
  871. 'sum(total_num) total_num',
  872. 'count(*) count',
  873. 'sum(total_price) total_price',
  874. 'sum(refund_price) refund_price',
  875. 'group_concat(cart_id SEPARATOR "|") cart_ids'
  876. ])->group('_add_time')->order('_add_time asc')->select();
  877. count($orderlist) && $orderlist = $orderlist->toArray();
  878. $legend = ['商品数量', '订单数量', '订单金额', '退款金额'];
  879. $seriesdata = [
  880. [
  881. 'name' => $legend[0],
  882. 'type' => 'line',
  883. 'data' => [],
  884. ],
  885. [
  886. 'name' => $legend[1],
  887. 'type' => 'line',
  888. 'data' => []
  889. ],
  890. [
  891. 'name' => $legend[2],
  892. 'type' => 'line',
  893. 'data' => []
  894. ],
  895. [
  896. 'name' => $legend[3],
  897. 'type' => 'line',
  898. 'data' => []
  899. ]
  900. ];
  901. $xdata = [];
  902. $zoom = '';
  903. foreach ($orderlist as $item) {
  904. $xdata[] = $item['_add_time'];
  905. $seriesdata[0]['data'][] = $item['total_num'];
  906. $seriesdata[1]['data'][] = $item['count'];
  907. $seriesdata[2]['data'][] = $item['total_price'];
  908. $seriesdata[3]['data'][] = $item['refund_price'];
  909. }
  910. count($xdata) > $limit && $zoom = $xdata[$limit - 5];
  911. $badge = self::getOrderBadge($where);
  912. $bingpaytype = self::setEchatWhere($where)->group('pay_type')->field(['count(*) as count', 'pay_type'])->select();
  913. count($bingpaytype) && $bingpaytype = $bingpaytype->toArray();
  914. $bing_xdata = ['微信支付', '余额支付', '其他支付'];
  915. $color = ['#ffcccc', '#99cc00', '#fd99cc', '#669966'];
  916. $bing_data = [];
  917. foreach ($bingpaytype as $key => $item) {
  918. if ($item['pay_type'] == 'weixin') {
  919. $value['name'] = $bing_xdata[0];
  920. } else if ($item['pay_type'] == 'yue') {
  921. $value['name'] = $bing_xdata[1];
  922. } else {
  923. $value['name'] = $bing_xdata[2];
  924. }
  925. $value['value'] = $item['count'];
  926. $value['itemStyle']['color'] = isset($color[$key]) ? $color[$key] : $color[0];
  927. $bing_data[] = $value;
  928. }
  929. return compact('zoom', 'xdata', 'seriesdata', 'badge', 'legend', 'bing_data', 'bing_xdata');
  930. }
  931. public static function getOrderBadge($where)
  932. {
  933. return [
  934. [
  935. 'name' => '拼团订单数量',
  936. 'field' => '个',
  937. 'count' => self::setEchatWhere($where, 2)->count(),
  938. 'content' => '拼团总订单数量',
  939. 'background_color' => 'layui-bg-cyan',
  940. 'sum' => self::setEchatWhere($where, 2, true)->count(),
  941. 'class' => 'fa fa-line-chart',
  942. 'col' => 2
  943. ],
  944. [
  945. 'name' => '砍价订单数量',
  946. 'field' => '个',
  947. 'count' => self::setEchatWhere($where, 4)->count(),
  948. 'content' => '砍价总订单数量',
  949. 'background_color' => 'layui-bg-cyan',
  950. 'sum' => self::setEchatWhere($where, 4, true)->count(),
  951. 'class' => 'fa fa-line-chart',
  952. 'col' => 2
  953. ],
  954. [
  955. 'name' => '秒杀订单数量',
  956. 'field' => '个',
  957. 'count' => self::setEchatWhere($where, 3)->count(),
  958. 'content' => '秒杀总订单数量',
  959. 'background_color' => 'layui-bg-cyan',
  960. 'sum' => self::setEchatWhere($where, 3, true)->count(),
  961. 'class' => 'fa fa-line-chart',
  962. 'col' => 2
  963. ],
  964. [
  965. 'name' => '普通订单数量',
  966. 'field' => '个',
  967. 'count' => self::setEchatWhere($where, 1)->count(),
  968. 'content' => '普通总订单数量',
  969. 'background_color' => 'layui-bg-cyan',
  970. 'sum' => self::setEchatWhere($where, 1, true)->count(),
  971. 'class' => 'fa fa-line-chart',
  972. 'col' => 2,
  973. ],
  974. [
  975. 'name' => '使用优惠卷金额',
  976. 'field' => '元',
  977. 'count' => self::setEchatWhere($where)->sum('coupon_price'),
  978. 'content' => '普通总订单数量',
  979. 'background_color' => 'layui-bg-cyan',
  980. 'sum' => self::setEchatWhere($where, null, true)->sum('coupon_price'),
  981. 'class' => 'fa fa-line-chart',
  982. 'col' => 2
  983. ],
  984. [
  985. 'name' => '积分消耗数',
  986. 'field' => '个',
  987. 'count' => self::setEchatWhere($where)->sum('use_integral'),
  988. 'content' => '积分消耗总数',
  989. 'background_color' => 'layui-bg-cyan',
  990. 'sum' => self::setEchatWhere($where, null, true)->sum('use_integral'),
  991. 'class' => 'fa fa-line-chart',
  992. 'col' => 2
  993. ],
  994. [
  995. 'name' => '积分抵扣金额',
  996. 'field' => '个',
  997. 'count' => self::setEchatWhere($where)->sum('deduction_price'),
  998. 'content' => '积分抵扣总金额',
  999. 'background_color' => 'layui-bg-cyan',
  1000. 'sum' => self::setEchatWhere($where, null, true)->sum('deduction_price'),
  1001. 'class' => 'fa fa-money',
  1002. 'col' => 2
  1003. ],
  1004. [
  1005. 'name' => '在线支付金额',
  1006. 'field' => '元',
  1007. 'count' => self::setEchatWhere($where)->where('pay_type', 'weixin')->sum('pay_price'),
  1008. 'content' => '在线支付总金额',
  1009. 'background_color' => 'layui-bg-cyan',
  1010. 'sum' => self::setEchatWhere($where, null, true)->where('pay_type', 'weixin')->sum('pay_price'),
  1011. 'class' => 'fa fa-weixin',
  1012. 'col' => 2
  1013. ],
  1014. [
  1015. 'name' => '余额支付金额',
  1016. 'field' => '元',
  1017. 'count' => self::setEchatWhere($where)->where('pay_type', 'yue')->sum('pay_price'),
  1018. 'content' => '余额支付总金额',
  1019. 'background_color' => 'layui-bg-cyan',
  1020. 'sum' => self::setEchatWhere($where, null, true)->where('pay_type', 'yue')->sum('pay_price'),
  1021. 'class' => 'fa fa-balance-scale',
  1022. 'col' => 2
  1023. ],
  1024. [
  1025. 'name' => '赚取积分',
  1026. 'field' => '分',
  1027. 'count' => self::setEchatWhere($where)->sum('gain_integral'),
  1028. 'content' => '赚取总积分',
  1029. 'background_color' => 'layui-bg-cyan',
  1030. 'sum' => self::setEchatWhere($where, null, true)->sum('gain_integral'),
  1031. 'class' => 'fa fa-gg-circle',
  1032. 'col' => 2
  1033. ],
  1034. [
  1035. 'name' => '交易额',
  1036. 'field' => '元',
  1037. 'count' => self::setEchatWhere($where)->sum('pay_price'),
  1038. 'content' => '总交易额',
  1039. 'background_color' => 'layui-bg-cyan',
  1040. 'sum' => self::setEchatWhere($where, null, true)->sum('pay_price'),
  1041. 'class' => 'fa fa-jpy',
  1042. 'col' => 2
  1043. ],
  1044. [
  1045. 'name' => '订单商品数量',
  1046. 'field' => '元',
  1047. 'count' => self::setEchatWhere($where)->sum('total_num'),
  1048. 'content' => '订单商品总数量',
  1049. 'background_color' => 'layui-bg-cyan',
  1050. 'sum' => self::setEchatWhere($where, null, true)->sum('total_num'),
  1051. 'class' => 'fa fa-cube',
  1052. 'col' => 2
  1053. ]
  1054. ];
  1055. }
  1056. /**
  1057. * 获取订单总数
  1058. * @param int $uid
  1059. * @return int|string
  1060. */
  1061. public static function getOrderCount($uid = 0)
  1062. {
  1063. if (!$uid) return 0;
  1064. return self::where('uid', $uid)->where('paid', 1)->where('refund_status', 0)->where('status', 2)->count();
  1065. }
  1066. /**清除未支付商品订单归还库存
  1067. * @return bool
  1068. */
  1069. public static function goodsOrderReturnInventory()
  1070. {
  1071. $order_clear_time = SystemConfigService::get('order_clear_time');
  1072. if ($order_clear_time <= 0 || $order_clear_time == '') return true;
  1073. $return_time = bcsub(time(), bcmul($order_clear_time, 3600, 0), 0);
  1074. $list = self::where(['type' => 2, 'paid' => 0, 'is_del' => 0, 'is_system_del' => 0])->where('add_time', '<=', $return_time)->select();
  1075. $list = count($list) > 0 ? $list->toArray() : [];
  1076. if (count($list) <= 0) return true;
  1077. foreach ($list as $key => $item) {
  1078. $res = self::where('id', $item['id'])->update(['is_del' => 1, 'is_system_del' => 1]);
  1079. if (!$res) continue;
  1080. $cart_id = Db::name('store_order_cart_info')->where('oid', $item['id'])->value('cart_id');
  1081. if (!$cart_id) continue;
  1082. $cart = StoreCart::where('id', $cart_id)->field('product_id,cart_num')->find();
  1083. if ($cart) {
  1084. $res1 = StoreProduct::where('id', $cart['product_id'])->setInc('stock', $cart['cart_num']);
  1085. if (!$res1) continue;
  1086. } else {
  1087. continue;
  1088. }
  1089. }
  1090. return true;
  1091. }
  1092. /**
  1093. * 系统自动收货
  1094. */
  1095. public static function goodsAutomaticReceiving()
  1096. {
  1097. $automatic_receiving_time = SystemConfigService::get('automatic_receiving_time');
  1098. if ($automatic_receiving_time <= 0 || $automatic_receiving_time == '') return true;
  1099. $return_time = bcsub(time(), bcmul($automatic_receiving_time, 86400, 0), 0);
  1100. $list = self::where(['type' => 2, 'paid' => 1, 'is_del' => 0, 'status' => 1, 'refund_status' => 0, 'is_system_del' => 0])->where('add_time', '<=', $return_time)->select();
  1101. $list = count($list) > 0 ? $list->toArray() : [];
  1102. if (count($list) <= 0) return true;
  1103. foreach ($list as $key => $item) {
  1104. try {
  1105. $data['status'] = 2;
  1106. self::edit($data, $item['id']);
  1107. HookService::listen('store_product_order_take_delivery', $item, $item['id'], false, StoreProductBehavior::class);
  1108. } catch (Exception $e) {
  1109. continue;
  1110. }
  1111. StoreOrderStatus::setStatus($item['id'], 'system_take_delivery', '系统自动收货');
  1112. }
  1113. return true;
  1114. }
  1115. }