StoreOrderDao.php 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\dao\order;
  12. use app\dao\BaseDao;
  13. use app\model\order\StoreOrder;
  14. /**
  15. * 订单
  16. * Class StoreOrderDao
  17. * @package app\dao\order
  18. */
  19. class StoreOrderDao extends BaseDao
  20. {
  21. /**
  22. * 限制精确查询字段
  23. * @var string[]
  24. */
  25. protected $withField = ['uid', 'order_id', 'real_name', 'user_phone', 'title', 'total_num'];
  26. /**
  27. * @return string
  28. */
  29. protected function setModel(): string
  30. {
  31. return StoreOrder::class;
  32. }
  33. /**
  34. * 订单搜索
  35. * @param array $where
  36. * @return \crmeb\basic\BaseModel|mixed|\think\Model
  37. */
  38. public function search(array $where = [])
  39. {
  40. if (isset($where['real_name'])) {
  41. $where['real_name'] = trim($where['real_name']);
  42. }
  43. $isDel = isset($where['is_del']) && $where['is_del'] !== '' && $where['is_del'] != -1;
  44. $realName = $where['real_name'] ?? '';
  45. $fieldKey = $where['field_key'] ?? '';
  46. $fieldKey = $fieldKey == 'all' ? '' : $fieldKey;
  47. return parent::search($where)->when($isDel, function ($query) use ($where) {
  48. $query->where('is_del', $where['is_del']);
  49. })->when(isset($where['plat_type']) && in_array($where['plat_type'], [-1, 0, 1, 2]), function ($query) use ($where) {
  50. switch ($where['plat_type']) {
  51. case -1://所有
  52. break;
  53. case 0://平台
  54. $query->where('store_id', 0)->where('supplier_id', 0);
  55. break;
  56. case 1://门店
  57. $query->where('store_id', '>', 0);
  58. break;
  59. case 2://供应商
  60. $query->where('supplier_id', '>', 0);
  61. break;
  62. }
  63. })->when(isset($where['is_system_del']), function ($query) {
  64. $query->where('is_system_del', 0);
  65. })->when(isset($where['is_coupon']), function ($query) {
  66. $query->where('coupon_id', '>', 0);
  67. })->when(isset($where['staff_id']) && $where['staff_id'], function ($query) use ($where) {
  68. $query->where('staff_id', $where['staff_id']);
  69. })->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
  70. switch ((int)$where['status']) {
  71. case 0://未支付
  72. $query->where('paid', 0)->where('status', 0)->where('refund_status', 0)->where('is_del', 0);
  73. break;
  74. case 1://已支付 未发货
  75. $query->where('paid', 1)->whereIn('status', [0, 4])->whereIn('refund_status', [0, 3])->whereIn('shipping_type', [1, 3, 4])->where('is_del', 0);
  76. break;
  77. case 7://已支付 部分发货
  78. $query->where('paid', 1)->where('status', 4)->whereIn('refund_status', [0, 3])->where('is_del', 0);
  79. break;
  80. case 2://已支付 待收货
  81. $query->where('paid', 1)->whereIn('status', [1, 5])->whereIn('refund_status', [0, 3])->where('is_del', 0);
  82. break;
  83. case 3:// 已支付 已收货 待评价
  84. $query->where('paid', 1)->where('status', 2)->whereIn('refund_status', [0, 3])->where('is_del', 0);
  85. break;
  86. case 4:// 交易完成
  87. $query->where('paid', 1)->where('status', 3)->whereIn('refund_status', [0, 3])->where('is_del', 0);
  88. break;
  89. case 5://已支付 待核销
  90. $query->where('paid', 1)->whereIn('status', [0, 1, 5])->whereIn('refund_status', [0, 3])->where('shipping_type', 2)->where('is_del', 0);
  91. break;
  92. case 6://已支付 已核销 没有退款
  93. $query->where('paid', 1)->where('status', 2)->whereIn('refund_status', [0, 3])->where('shipping_type', 2)->where('is_del', 0);
  94. break;
  95. case 8://已支付 核销订单
  96. $query->where('paid', 1)->whereIn('status', [0, 1, 2, 5])->whereIn('refund_status', [0, 3])->where('shipping_type', 2)->where('is_del', 0);
  97. break;
  98. case 9://已配送
  99. $query->where('paid', 1)->whereIn('status', [2, 3])->whereIn('refund_status', [0, 3])->where('is_del', 0);
  100. break;
  101. case -1://退款中
  102. $query->where('paid', 1)->whereIn('refund_status', [1, 4])->where('is_del', 0);
  103. break;
  104. case -2://已退款
  105. $query->where('paid', 1)->where('refund_status', 2)->where('is_del', 0);
  106. break;
  107. case -3://退款
  108. $query->where('paid', 1)->whereIn('refund_status', [1, 2, 4])->where('is_del', 0);
  109. break;
  110. case -4://已删除
  111. $query->where('is_del', 1);
  112. break;
  113. }
  114. })->when(isset($where['type']) && $where['type'] !== '', function ($query) use ($where) {
  115. switch ($where['type']) {
  116. case 0://普通
  117. $query->where('type', 0);
  118. break;
  119. case 1://秒杀
  120. $query->where('type', 1);
  121. break;
  122. case 2://砍价
  123. $query->where('type', 2);
  124. break;
  125. case 3://拼团
  126. $query->where('type', 3);
  127. break;
  128. case 4://套餐
  129. $query->where('type', 5);
  130. break;
  131. case 5://核销订单
  132. $query->where('shipping_type', 2);
  133. break;
  134. case 6://收银台订单
  135. $query->where('shipping_type', 4);
  136. break;
  137. case 7://配送订单
  138. $query->whereIn('shipping_type', [1, 3]);
  139. break;
  140. case 8://预售
  141. $query->where('type', 6);
  142. break;
  143. case 9://新人专享
  144. $query->where('type', 7);
  145. break;
  146. case 10://抽奖
  147. $query->where('type', 8);
  148. break;
  149. case 11://拼单
  150. $query->where('type', 9);
  151. break;
  152. case 12://桌码
  153. $query->where('type', 10);
  154. break;
  155. }
  156. })->when(isset($where['order_type']) && $where['order_type'] !== '', function ($query) use ($where) {
  157. switch ($where['order_type']) {
  158. case 5://核销订单
  159. $query->where('shipping_type', 2);
  160. break;
  161. case 6://收银台订单
  162. $query->where('shipping_type', 4);
  163. break;
  164. case 7://配送订单
  165. $query->whereIn('shipping_type', [1, 3]);
  166. break;
  167. }
  168. })->when(isset($where['pay_type']), function ($query) use ($where) {
  169. switch ($where['pay_type']) {
  170. case 1:
  171. $query->where('pay_type', 'weixin');
  172. break;
  173. case 2:
  174. $query->where('pay_type', 'yue');
  175. break;
  176. case 3:
  177. $query->where('pay_type', 'offline');
  178. break;
  179. case 4:
  180. $query->where('pay_type', 'alipay');
  181. break;
  182. }
  183. })->when($realName && $fieldKey && in_array($fieldKey, $this->withField), function ($query) use ($where, $realName, $fieldKey) {
  184. if ($fieldKey !== 'title') {
  185. $query->where(trim($fieldKey), trim($realName));
  186. } else {
  187. $query->where('id', 'in', function ($que) use ($where) {
  188. $que->name('store_order_cart_info')->whereIn('product_id', function ($q) use ($where) {
  189. $q->name('store_product')->whereLike('store_name|keyword', '%' . $where['real_name'] . '%')->field(['id'])->select();
  190. })->field(['oid'])->select();
  191. });
  192. }
  193. })->when($realName && !$fieldKey, function ($query) use ($where) {
  194. $query->where(function ($que) use ($where) {
  195. $que->whereLike('order_id|real_name|user_phone|verify_code', '%' . $where['real_name'] . '%')
  196. ->whereOr('uid', 'in', function ($q) use ($where) {
  197. $q->name('user')->whereLike('nickname|uid|phone', '%' . $where['real_name'] . '%')->field(['uid'])->select();
  198. })->whereOr('uid', 'in', function ($q) use ($where) {
  199. $q->name('user_address')->whereLike('real_name|uid|phone', '%' . $where['real_name'] . '%')->field(['uid'])->select();
  200. })->whereOr('id', 'in', function ($que) use ($where) {
  201. $que->name('store_order_cart_info')->whereIn('product_id', function ($q) use ($where) {
  202. $q->name('store_product')->whereLike('store_name|keyword', '%' . $where['real_name'] . '%')->field(['id'])->select();
  203. })->field(['oid'])->select();
  204. })->whereOr('activity_id', 'in', function ($que) use ($where) {
  205. $que->name('store_seckill')->whereLike('title|info', '%' . $where['real_name'] . '%')->field(['id'])->select();
  206. })->whereOr('activity_id', 'in', function ($que) use ($where) {
  207. $que->name('store_bargain')->whereLike('title|info', '%' . $where['real_name'] . '%')->field(['id'])->select();
  208. })->whereOr('activity_id', 'in', function ($que) use ($where) {
  209. $que->name('store_combination')->whereLike('title|info', '%' . $where['real_name'] . '%')->field(['id'])->select();
  210. });
  211. });
  212. })->when(isset($where['unique']), function ($query) use ($where) {
  213. $query->where('unique', $where['unique']);
  214. })->when(isset($where['is_remind']), function ($query) use ($where) {
  215. $query->where('is_remind', $where['is_remind']);
  216. })->when(isset($where['refundTypes']) && $where['refundTypes'] != '', function ($query) use ($where) {
  217. switch ((int)$where['refundTypes']) {
  218. case 1:
  219. $query->where('refund_type', 'in', '1,2');
  220. break;
  221. case 2:
  222. $query->where('refund_type', 4);
  223. break;
  224. case 3:
  225. $query->where('refund_type', 5);
  226. break;
  227. case 4:
  228. $query->where('refund_type', 6);
  229. break;
  230. }
  231. })->when(isset($where['is_refund']) && $where['is_refund'] !== '', function ($query) use ($where) {
  232. if ($where['is_refund'] == 1) {
  233. $query->where('refund_status', 2);
  234. } else {
  235. $query->where('refund_status', 0);
  236. }
  237. })->when(isset($where['area']) && !empty($where['area']), function ($query) use ($where) {
  238. if (is_array($where['area'])) {
  239. if (($where['area']['order_province'] ?? '') != '') {
  240. $query->where('order_province', $where['area']['order_province']);
  241. if (($where['area']['order_city'] ?? '') != '') {
  242. $query->where('order_city', $where['area']['order_city']);
  243. if (($where['area']['order_district'] ?? '') != '') {
  244. $query->where('order_district', $where['area']['order_district']);
  245. }
  246. }
  247. }
  248. }
  249. });
  250. }
  251. /**
  252. * 获取某一个月订单数量
  253. * @param array $where
  254. * @param string $month
  255. * @return int
  256. */
  257. public function getMonthCount(array $where, string $month)
  258. {
  259. return $this->search($where)->whereMonth('add_time', $month)->count();
  260. }
  261. /**
  262. * 获取某一个月订单金额
  263. * @param array $where
  264. * @param string $month
  265. * @param string $field
  266. * @return float
  267. */
  268. public function getMonthMoneyCount(array $where, string $month, string $field)
  269. {
  270. return $this->search($where)->whereMonth('add_time', $month)->sum($field);
  271. }
  272. /**
  273. * 获取购买历史用户
  274. * @param int $storeId
  275. * @param int $staffId
  276. * @param int $limit
  277. * @return mixed
  278. */
  279. public function getOrderHistoryList(int $storeId, int $staffId, array $uid = [], int $limit = 20)
  280. {
  281. return $this->search(['store_id' => $storeId, 'staff_id' => $staffId])->when($uid, function ($query) use ($uid) {
  282. $query->whereNotIn('uid', $uid);
  283. })->where('uid', '<>', 0)->with('user')->limit($limit)
  284. ->group('uid')->order('add_time', 'desc')->field(['uid', 'store_id', 'staff_id'])->select()->toArray();
  285. }
  286. /**
  287. * 订单搜索列表
  288. * @param array $where
  289. * @param array $field
  290. * @param int $page
  291. * @param int $limit
  292. * @param array $with
  293. * @return array
  294. * @throws \think\db\exception\DataNotFoundException
  295. * @throws \think\db\exception\DbException
  296. * @throws \think\db\exception\ModelNotFoundException
  297. */
  298. public function getList(array $where, array $field, int $page = 0, int $limit = 0, array $with = [])
  299. {
  300. return $this->search($where)->field($field)->with($with)->when($page && $limit, function ($query) use ($page, $limit) {
  301. $query->page($page, $limit);
  302. })->order('pay_time DESC,id DESC')->select()->toArray();
  303. }
  304. /**
  305. * 获取待核销的订单列表
  306. * @param array $where
  307. * @param array|string[] $field
  308. * @return mixed
  309. */
  310. public function getUnWirteOffList(array $where, array $field = ['*'])
  311. {
  312. return $this->search($where)->field($field)->where('paid', 1)->whereIn('status', [0, 1, 5])->whereIn('refund_status', [0, 3])->where('is_del', 0)->where('is_system_del', 0)
  313. ->where(function ($query) {
  314. $query->where('shipping_type', 2)->whereOr('delivery_type', 'send');
  315. })->order('pay_time DESC,id DESC')->select()->toArray();
  316. }
  317. /**
  318. * 订单搜索列表
  319. * @param array $where
  320. * @param array $field
  321. * @param int $page
  322. * @param int $limit
  323. * @param array $with
  324. * @param string $order
  325. * @return array
  326. * @throws \think\db\exception\DataNotFoundException
  327. * @throws \think\db\exception\DbException
  328. * @throws \think\db\exception\ModelNotFoundException
  329. */
  330. public function getOrderList(array $where, array $field, int $page = 0, int $limit = 0, array $with = [], string $order = 'add_time DESC,id DESC')
  331. {
  332. return $this->search($where)->field($field)->with(array_merge(['user', 'spread', 'refund'], $with))->when($page && $limit, function ($query) use ($page, $limit) {
  333. $query->page($page, $limit);
  334. })->when(!$page && $limit, function ($query) use ($limit) {
  335. $query->limit($limit);
  336. })->order($order)->select()->toArray();
  337. }
  338. /**
  339. * 聚合查询
  340. * @param array $where
  341. * @param string $field
  342. * @param string $together
  343. * @return int
  344. */
  345. public function together(array $where, string $field, string $together = 'sum')
  346. {
  347. if (!in_array($together, ['sum', 'max', 'min', 'avg'])) {
  348. return 0;
  349. }
  350. return $this->search($where)->{$together}($field);
  351. }
  352. /**
  353. * 查找指定条件下的订单数据以数组形式返回
  354. * @param array $where
  355. * @param string $field
  356. * @param string $key
  357. * @param string $group
  358. * @return array
  359. */
  360. public function column(array $where, string $field, string $key = '', string $group = '')
  361. {
  362. return $this->search($where)->when($group, function ($query) use ($group) {
  363. $query->group($group);
  364. })->column($field, $key);
  365. }
  366. /**
  367. * 获取订单id下没有删除的订单数量
  368. * @param array $ids
  369. * @return int
  370. */
  371. public function getOrderIdsCount(array $ids)
  372. {
  373. return $this->getModel()->whereIn('id', $ids)->where('is_del', 0)->count();
  374. }
  375. /**
  376. * 获取一段时间订单统计数量、金额
  377. * @param array $where
  378. * @param array $time
  379. * @param string $timeType
  380. * @param bool $is_pid
  381. * @param string $countField
  382. * @param string $sumField
  383. * @param string $groupField
  384. * @return array
  385. * @throws \think\db\exception\DataNotFoundException
  386. * @throws \think\db\exception\DbException
  387. * @throws \think\db\exception\ModelNotFoundException
  388. */
  389. public function orderAddTimeList(array $where, array $time, string $timeType = "week", bool $is_pid = true, string $countField = '*', string $sumField = 'pay_price', string $groupField = 'add_time')
  390. {
  391. return $this->getModel()->where($where)->when($is_pid, function ($query) {
  392. $query->where('pid', '>=', 0);
  393. })->when(!isset($where['refund_status']), function ($query) {
  394. $query->whereIn('refund_status', [0, 3]);
  395. })->where('paid', 1)->where('is_del', 0)->where('is_system_del', 0)
  396. ->where(isset($where['timekey']) && $where['timekey'] ? $where['timekey'] : 'add_time', 'between time', $time)
  397. ->when($timeType, function ($query) use ($timeType, $countField, $sumField, $groupField) {
  398. switch ($timeType) {
  399. case "hour":
  400. $timeUnix = "%H";
  401. break;
  402. case "day" :
  403. $timeUnix = "%Y-%m-%d";
  404. break;
  405. case "week" :
  406. $timeUnix = "%w";
  407. break;
  408. case "month" :
  409. $timeUnix = "%d";
  410. break;
  411. case "weekly" :
  412. $timeUnix = "%W";
  413. break;
  414. case "year" :
  415. $timeUnix = "%Y-%m";
  416. break;
  417. default:
  418. $timeUnix = "%Y-%m-%d";
  419. break;
  420. }
  421. $query->field("FROM_UNIXTIME(`" . $groupField . "`,'$timeUnix') as day,count(" . $countField . ") as count,sum(`" . $sumField . "`) as price");
  422. $query->group('day');
  423. })->order('add_time asc')->select()->toArray();
  424. }
  425. /**
  426. * 统计总数上期
  427. * @param array $where
  428. * @param array $time
  429. * @return array|\think\Model|null
  430. * @throws \think\db\exception\DataNotFoundException
  431. * @throws \think\db\exception\DbException
  432. * @throws \think\db\exception\ModelNotFoundException
  433. */
  434. public function preTotalFind(array $where, array $time, string $sumField = 'pay_price', string $groupField = 'add_time')
  435. {
  436. return $this->getModel()->where($where)->where('pid', '>=', 0)->where('paid', 1)->whereIn('refund_status', [0, 3])->where('is_del', 0)->where('is_system_del', 0)
  437. ->where($groupField, 'between time', $time)
  438. ->field("count(*) as count,sum(`" . $sumField . "`) as price")
  439. ->find();
  440. }
  441. /**
  442. * 新订单ID
  443. * @param $status
  444. * @param int $store_id
  445. * @return array
  446. */
  447. public function newOrderId($status, int $store_id = 0)
  448. {
  449. return $this->search(['status' => $status, 'is_remind' => 0])->where('store_id', $store_id)->column('order_id', 'id');
  450. }
  451. /**
  452. * 获取订单数量
  453. * @param int $store_id
  454. * @param int $type
  455. * @param string $field
  456. * @return int
  457. */
  458. public function storeOrderCount(int $val = 0, int $type = -1, string $field = 'store_id')
  459. {
  460. $where = ['pid' => 0, 'status' => 1];
  461. if ($type != -1) $where['type'] = $type;
  462. return $this->search($where)->when($field && $val > 0, function ($query) use ($field, $val) {
  463. $query->where($field, $val);
  464. })->count();
  465. }
  466. /**
  467. * 总销售额
  468. * @param $time
  469. * @param int $store_id
  470. * @return float
  471. */
  472. public function totalSales($time, int $store_id = 0)
  473. {
  474. return $this->search(['pid' => 0, 'paid' => 1, 'is_del' => 0, 'refund_status' => [0, 3], 'time' => $time ?: 'today', 'timekey' => 'pay_time'])->where('store_id', $store_id)->sum('pay_price');
  475. }
  476. /**
  477. * 获取特定时间内订单量
  478. * @param $time
  479. * @return int
  480. */
  481. public function totalOrderCount($time)
  482. {
  483. return $this->search(['pid' => 0, 'time' => $time ?: 'today', 'timeKey' => 'add_time'])->where('paid', 1)->whereIn('refund_status', [0, 3])->where('store_id', 0)->count();
  484. }
  485. /**
  486. * 获取订单详情
  487. * @param string $key
  488. * @param int $uid
  489. * @param array $with
  490. * @return array|\think\Model|null
  491. * @throws \think\db\exception\DataNotFoundException
  492. * @throws \think\db\exception\DbException
  493. * @throws \think\db\exception\ModelNotFoundException
  494. */
  495. public function getUserOrderDetail(string $key, int $uid, array $with = [])
  496. {
  497. return $this->getOne(['order_id|id|unique' => $key, 'uid' => $uid, 'is_del' => 0], '*', $with);
  498. }
  499. /**
  500. * 获取用户推广订单
  501. * @param array $where
  502. * @param string $field
  503. * @param int $page
  504. * @param int $limit
  505. * @param array $with
  506. * @return array
  507. * @throws \think\db\exception\DataNotFoundException
  508. * @throws \think\db\exception\DbException
  509. * @throws \think\db\exception\ModelNotFoundException
  510. */
  511. public function getStairOrderList(array $where, string $field, int $page, int $limit, array $with = [])
  512. {
  513. return $this->search($where)->with($with)->field($field)->when($page && $limit, function ($query) use ($page, $limit) {
  514. $query->page($page, $limit);
  515. })->order('id DESC')->select()->toArray();
  516. }
  517. /**
  518. * 订单每月统计数据
  519. * @param int $page
  520. * @param int $limit
  521. * @return array
  522. */
  523. public function getOrderDataPriceCount(array $where, array $field, int $page, int $limit)
  524. {
  525. return $this->search($where)
  526. ->field($field)->group("FROM_UNIXTIME(add_time, '%Y-%m-%d')")
  527. ->order('add_time DESC')->page($page, $limit)->select()->toArray();
  528. }
  529. /**
  530. * 获取当前时间到指定时间的支付金额 管理员
  531. * @param $start
  532. * @param $stop
  533. * @param int $store_id
  534. * @return mixed
  535. */
  536. public function chartTimePrice($start, $stop, int $store_id = 0)
  537. {
  538. return $this->search(['pid' => 0, 'is_del' => 0, 'paid' => 1, 'refund_status' => [0, 3], 'is_system_del' => 0])
  539. ->where('store_id', $store_id)
  540. ->where('add_time', '>=', $start)
  541. ->where('add_time', '<', $stop)
  542. ->field('sum(pay_price) as num,FROM_UNIXTIME(add_time, \'%Y-%m-%d\') as time')
  543. ->group("FROM_UNIXTIME(add_time, '%Y-%m-%d')")
  544. ->order('add_time ASC')->select()->toArray();
  545. }
  546. /**
  547. * 获取当前时间到指定时间的支付订单数 管理员
  548. * @param $start
  549. * @param $stop
  550. * @param int $store_id
  551. * @return mixed
  552. */
  553. public function chartTimeNumber($start, $stop, int $store_id = 0)
  554. {
  555. return $this->search(['pid' => 0, 'is_del' => 0, 'paid' => 1, 'refund_status' => [0, 3]])
  556. ->where('store_id', $store_id)
  557. ->where('add_time', '>=', $start)
  558. ->where('add_time', '<', $stop)
  559. ->field('count(id) as num,FROM_UNIXTIME(add_time, \'%Y-%m-%d\') as time')
  560. ->group("FROM_UNIXTIME(add_time, '%Y-%m-%d')")
  561. ->order('add_time ASC')->select()->toArray();
  562. }
  563. /**
  564. * 获取用户已购买此活动商品的个数
  565. * @param $uid
  566. * @param $type
  567. * @param $activity_id
  568. * @return int
  569. */
  570. public function getBuyCount($uid, $type, $activity_id): int
  571. {
  572. return $this->getModel()
  573. ->where('uid', $uid)
  574. ->where('type', $type)
  575. ->where('activity_id', $activity_id)
  576. ->whereIn('pid', [0, -1])
  577. ->where(function ($query) {
  578. $query->where('paid', 1)->whereOr(function ($query1) {
  579. $query1->where('paid', 0)->where('is_del', 0);
  580. });
  581. })->value('sum(total_num)') ?? 0;
  582. }
  583. /**
  584. * 获取没有支付的订单列表
  585. * @param int $store_id
  586. * @param int $page
  587. * @param int $limit
  588. * @return \crmeb\basic\BaseModel
  589. */
  590. public function getOrderUnPaid(int $store_id = 0, int $page = 0, int $limit = 0)
  591. {
  592. return $this->getModel()
  593. ->where(['pid' => 0, 'paid' => 0, 'is_del' => 0, 'status' => 0, 'refund_status' => 0])
  594. ->where('store_id', $store_id)
  595. ->where('pay_type', '<>', 'offline')
  596. ->when($page && $limit, function ($query) use ($page, $limit) {
  597. $query->page($page, $limit);
  598. });
  599. }
  600. /**
  601. * 用户趋势数据
  602. * @param $time
  603. * @param $type
  604. * @param $timeType
  605. * @return mixed
  606. */
  607. public function getTrendData($time, $type, $timeType, $str)
  608. {
  609. return $this->getModel()->when($type != '', function ($query) use ($type) {
  610. $query->where('channel_type', $type);
  611. })->where('paid', 1)->where('pid', '>=', 0)->where(function ($query) use ($time) {
  612. if ($time[0] == $time[1]) {
  613. $query->whereDay('pay_time', $time[0]);
  614. } else {
  615. $time[1] = date('Y/m/d', strtotime($time[1]) + 86400);
  616. $query->whereTime('pay_time', 'between', $time);
  617. }
  618. })->field("FROM_UNIXTIME(pay_time,'$timeType') as days,$str as num")
  619. ->group('days')->select()->toArray();
  620. }
  621. /**
  622. * 用户地域数据
  623. * @param $time
  624. * @param $userType
  625. * @return mixed
  626. */
  627. public function getRegion($time, $userType)
  628. {
  629. return $this->getModel()->when($userType != '', function ($query) use ($userType) {
  630. $query->where('channel_type', $userType);
  631. })->where('pid', '>=', 0)->where('store_id', 0)->where(function ($query) use ($time) {
  632. if ($time[0] == $time[1]) {
  633. $query->whereDay('pay_time', $time[0]);
  634. } else {
  635. $time[1] = date('Y/m/d', strtotime($time[1]) + 86400);
  636. $query->whereTime('pay_time', 'between', $time);
  637. }
  638. })->field('pay_price as payPrice,substring_index(user_address, " ", 1) as province')->select()->toArray();
  639. }
  640. /**
  641. * 商品趋势
  642. * @param $time
  643. * @param $timeType
  644. * @param $field
  645. * @param $str
  646. * @param string $orderStatus
  647. * @param null $area
  648. * @return mixed
  649. */
  650. public function getProductTrend($time, $timeType, $field, $str, $orderStatus = '', $area = null)
  651. {
  652. return $this->getModel()->where(function ($query) use ($field, $orderStatus) {
  653. if ($field == 'pay_time') {
  654. $query->where('paid', 1);
  655. } elseif ($field == 'refund_reason_time') {
  656. $query->where('paid', 1)->where('refund_status', '>', 0);
  657. } elseif ($field == 'add_time') {
  658. if ($orderStatus == 'pay') {
  659. $query->where('paid', 1)->where('pid', '>=', 0)->where('refund_status', 0);
  660. } elseif ($orderStatus == 'refund') {
  661. $query->where('paid', 1)->where('pid', '>=', 0)->where('refund_status', '>', 0);
  662. } elseif ($orderStatus == 'coupon') {
  663. $query->where('paid', 1)->where('pid', '>=', 0)->where('coupon_id', '>', 0);
  664. }
  665. }
  666. })->where('pid', '>=', 0)->where('store_id', 0)->where(function ($query) use ($time, $field) {
  667. if ($time[0] == $time[1]) {
  668. $query->whereDay($field, $time[0]);
  669. } else {
  670. $time[1] = date('Y/m/d', strtotime($time[1]) + 86400);
  671. $query->whereTime($field, 'between', $time);
  672. }
  673. })->where(function ($query) use ($area) {
  674. if (is_array($area)) {
  675. if (($area['order_province'] ?? '') != '') {
  676. $query->where('order_province', $area['order_province']);
  677. if (($area['order_city'] ?? '') != '') {
  678. $query->where('order_city', $area['order_city']);
  679. if (($area['order_district'] ?? '') != '') {
  680. $query->where('order_district', $area['order_district']);
  681. }
  682. }
  683. }
  684. }
  685. })->field("FROM_UNIXTIME($field,'$timeType') as days,$str as num")->group('days')->select()->toArray();
  686. }
  687. /**
  688. * 按照支付时间统计支付金额
  689. * @param array $where
  690. * @param string $sumField
  691. * @return mixed
  692. */
  693. public function getDayTotalMoney(array $where, string $sumField)
  694. {
  695. return $this->search($where)
  696. ->when(isset($where['timeKey']), function ($query) use ($where) {
  697. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  698. })
  699. ->sum($sumField);
  700. }
  701. /**
  702. * 时间段订单数统计
  703. * @param array $where
  704. * @param string $countField
  705. * @return int
  706. */
  707. public function getDayOrderCount(array $where, string $countField = "*")
  708. {
  709. return $this->search($where)
  710. ->when(isset($where['timeKey']), function ($query) use ($where) {
  711. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  712. })
  713. ->count($countField);
  714. }
  715. /**
  716. * 时间分组订单付款金额统计
  717. * @param array $where
  718. * @param string $sumField
  719. * @return mixed
  720. */
  721. public function getDayGroupMoney(array $where, string $sumField, string $group)
  722. {
  723. return $this->search($where)
  724. ->when(isset($where['timeKey']), function ($query) use ($where, $sumField, $group) {
  725. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  726. if ($where['timeKey']['days'] == 1) {
  727. $timeUinx = "%H";
  728. } elseif ($where['timeKey']['days'] == 30) {
  729. $timeUinx = "%Y-%m-%d";
  730. } elseif ($where['timeKey']['days'] == 365) {
  731. $timeUinx = "%Y-%m";
  732. } elseif ($where['timeKey']['days'] > 1 && $where['timeKey']['days'] < 30) {
  733. $timeUinx = "%Y-%m-%d";
  734. } elseif ($where['timeKey']['days'] > 30 && $where['timeKey']['days'] < 365) {
  735. $timeUinx = "%Y-%m";
  736. } else {
  737. $timeUinx = "%Y-%m";
  738. }
  739. $query->field("sum($sumField) as number,FROM_UNIXTIME($group, '$timeUinx') as time");
  740. $query->group("FROM_UNIXTIME($group, '$timeUinx')");
  741. })
  742. ->order('pay_time ASC')->select()->toArray();
  743. }
  744. /**
  745. * 时间分组订单数统计
  746. * @param array $where
  747. * @param string $sumField
  748. * @return mixed
  749. */
  750. public function getOrderGroupCount(array $where, string $sumField = "*")
  751. {
  752. return $this->search($where)
  753. ->when(isset($where['timeKey']), function ($query) use ($where, $sumField) {
  754. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  755. if ($where['timeKey']['days'] == 1) {
  756. $timeUinx = "%H";
  757. } elseif ($where['timeKey']['days'] == 30) {
  758. $timeUinx = "%Y-%m-%d";
  759. } elseif ($where['timeKey']['days'] == 365) {
  760. $timeUinx = "%Y-%m";
  761. } elseif ($where['timeKey']['days'] > 1 && $where['timeKey']['days'] < 30) {
  762. $timeUinx = "%Y-%m-%d";
  763. } elseif ($where['timeKey']['days'] > 30 && $where['timeKey']['days'] < 365) {
  764. $timeUinx = "%Y-%m";
  765. } else {
  766. $timeUinx = "%Y-%m";
  767. }
  768. $query->field("count($sumField) as number,FROM_UNIXTIME(pay_time, '$timeUinx') as time");
  769. $query->group("FROM_UNIXTIME(pay_time, '$timeUinx')");
  770. })
  771. ->order('pay_time ASC')->select()->toArray();
  772. }
  773. /**
  774. * 时间段支付订单人数
  775. * @param $where
  776. * @return mixed
  777. */
  778. public function getPayOrderPeople($where)
  779. {
  780. return $this->search($where)
  781. ->when(isset($where['timeKey']), function ($query) use ($where) {
  782. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  783. })
  784. ->field('uid')
  785. ->distinct(true)
  786. ->select()->toArray();
  787. }
  788. /**
  789. * 时间段分组统计支付订单人数
  790. * @param $where
  791. * @return mixed
  792. */
  793. public function getPayOrderGroupPeople($where)
  794. {
  795. return $this->search($where)
  796. ->when(isset($where['timeKey']), function ($query) use ($where) {
  797. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  798. if ($where['timeKey']['days'] == 1) {
  799. $timeUinx = "%H";
  800. } elseif ($where['timeKey']['days'] == 30) {
  801. $timeUinx = "%Y-%m-%d";
  802. } elseif ($where['timeKey']['days'] == 365) {
  803. $timeUinx = "%Y-%m";
  804. } elseif ($where['timeKey']['days'] > 1 && $where['timeKey']['days'] < 30) {
  805. $timeUinx = "%Y-%m-%d";
  806. } elseif ($where['timeKey']['days'] > 30 && $where['timeKey']['days'] < 365) {
  807. $timeUinx = "%Y-%m";
  808. } else {
  809. $timeUinx = "%Y-%m";
  810. }
  811. $query->field("count(distinct uid) as number,FROM_UNIXTIME(pay_time, '$timeUinx') as time");
  812. $query->group("FROM_UNIXTIME(pay_time, '$timeUinx')");
  813. })
  814. ->order('pay_time ASC')->select()->toArray();
  815. }
  816. /**
  817. * 获取批量打印电子面单数据
  818. * @param array $where
  819. * @param array $ids
  820. * @param string $filed
  821. * @param int $store_id
  822. * @return array
  823. * @throws \think\db\exception\DataNotFoundException
  824. * @throws \think\db\exception\DbException
  825. * @throws \think\db\exception\ModelNotFoundException
  826. */
  827. public function getOrderDumpData(array $where, array $ids = [], $filed = "*", int $store_id = 0)
  828. {
  829. $where['pid'] = 0;
  830. $where['status'] = 1;
  831. $where['refund_status'] = 0;
  832. $where['paid'] = 1;
  833. $where['is_del'] = 0;
  834. $where['shipping_type'] = 1;
  835. $where['is_system_del'] = 0;
  836. $where['store_id'] = $store_id;
  837. return $this->search($where)->when($ids, function ($query) use ($ids) {
  838. $query->whereIn('id', $ids);
  839. })->field($filed)->with(['pink'])->select()->toArray();
  840. }
  841. /**
  842. * @param array $where
  843. * @param string $field
  844. * @return array
  845. * @throws \think\db\exception\DataNotFoundException
  846. * @throws \think\db\exception\DbException
  847. * @throws \think\db\exception\ModelNotFoundException
  848. */
  849. public function getOrderListByWhere(array $where, $field = "*")
  850. {
  851. return $this->search($where)->field($field)->select()->toArray();
  852. }
  853. /**
  854. * 批量修改订单
  855. * @param array $ids
  856. * @param array $data
  857. * @param string|null $key
  858. * @return \crmeb\basic\BaseModel
  859. */
  860. public function batchUpdateOrder(array $ids, array $data, ?string $key = null)
  861. {
  862. return $this->getModel()::whereIn(is_null($key) ? $this->getPk() : $key, $ids)->update($data);
  863. }
  864. /**
  865. * 获取拆单之后的子订单
  866. * @param int $id
  867. * @param string $field
  868. * @param int $type 1:不包含自己2:包含自己
  869. * @return array
  870. * @throws \think\db\exception\DataNotFoundException
  871. * @throws \think\db\exception\DbException
  872. * @throws \think\db\exception\ModelNotFoundException
  873. */
  874. public function getSonOrder(int $id, string $field = '*', int $type = 1)
  875. {
  876. return in_array($type, [1, 2]) ? $this->getModel()::field($field)->when($type, function ($query) use ($id, $type) {
  877. if ($type == 1) {
  878. $query->where('pid', $id);
  879. } else {
  880. $query->where('pid', $id)->whereOr('id', $id);
  881. }
  882. })->select()->toArray() : [];
  883. }
  884. /**
  885. * 查询退款订单
  886. * @param $where
  887. * @param $page
  888. * @param $limit
  889. * @return array
  890. * @throws \think\db\exception\DataNotFoundException
  891. * @throws \think\db\exception\DbException
  892. * @throws \think\db\exception\ModelNotFoundException
  893. */
  894. public function getRefundList($where, $page = 0, $limit = 0)
  895. {
  896. $model = $this->getModel()
  897. ->where('is_system_del', 0)
  898. ->where('paid', 1)
  899. ->when(isset($where['store_id']), function ($query) use ($where) {
  900. $query->where('store_id', $where['store_id']);
  901. })->when(isset($where['refund_type']) && $where['refund_type'] !== '', function ($query) use ($where) {
  902. if ($where['refund_type'] == 0) {
  903. $query->where('refund_type', '>', 0);
  904. } else {
  905. $query->where('refund_type', $where['refund_type']);
  906. }
  907. })->when(isset($where['not_pid']), function ($query) {
  908. $query->where('pid', '<>', -1);
  909. })->when($where['order_id'] != '', function ($query) use ($where) {
  910. $query->where('order_id', $where['order_id']);
  911. })->when(is_array($where['refund_reason_time']), function ($query) use ($where) {
  912. $query->whereBetween('refund_reason_time', [strtotime($where['refund_reason_time'][0]), strtotime($where['refund_reason_time'][1]) + 86400]);
  913. })->with(array_merge(['user', 'spread']));
  914. $count = $model->count();
  915. $list = $model->when($page != 0 && $limit != 0, function ($query) use ($page, $limit) {
  916. $query->page($page, $limit);
  917. })->order('refund_reason_time desc')->select()->toArray();
  918. return compact('list', 'count');
  919. }
  920. /**
  921. * 门店线上支付订单详情
  922. * @param int $store_id
  923. * @param int $uid
  924. * @return array|\think\Model|null
  925. * @throws \think\db\exception\DataNotFoundException
  926. * @throws \think\db\exception\DbException
  927. * @throws \think\db\exception\ModelNotFoundException
  928. */
  929. public function payCashierOrder(int $store_id, int $uid)
  930. {
  931. return $this->getModel()->where('uid', $uid)->where('store_id', $store_id)->where('paid', 0)->where('is_del', 0)->where('is_system_del', 0)
  932. ->where('shipping_type', 4)
  933. ->order('add_time desc,id desc')
  934. ->find();
  935. }
  936. /**
  937. * 商品趋势
  938. * @param $time
  939. * @param $timeType
  940. * @param $field
  941. * @param $str
  942. * @param $orderStatus
  943. * @return mixed
  944. */
  945. public function getOrderStatistics($where, $time, $timeType, $field, $str, $orderStatus = '')
  946. {
  947. return $this->getModel()->where($where)->where(function ($query) use ($field, $orderStatus) {
  948. if ($field == 'pay_time') {
  949. $query->where('paid', 1);
  950. } elseif ($field == 'refund_reason_time') {
  951. $query->where('paid', 1)->where('refund_status', '>', 0);
  952. } elseif ($field == 'add_time') {
  953. if ($orderStatus == 'pay') {
  954. $query->where('paid', 1)->where('pid', '>=', 0)->whereIn('refund_status', [0, 3]);
  955. } elseif ($orderStatus == 'refund') {
  956. $query->where('paid', 1)->where('pid', '>=', 0)->where('refund_type', 6);
  957. }
  958. }
  959. })->where(function ($query) use ($time, $field) {
  960. if ($time[0] == $time[1]) {
  961. $query->whereDay($field, $time[0]);
  962. } else {
  963. $time[1] = date('Y/m/d', (!is_numeric($time[1]) ? strtotime($time[1]) : $time[1]) + 86400);
  964. $query->whereTime($field, 'between', $time);
  965. }
  966. })->where('is_del', 0)->where('is_system_del', 0)
  967. ->field("FROM_UNIXTIME($field,'$timeType') as days,$str as num")->group('days')->select()->toArray();
  968. }
  969. /**
  970. * 获取活动订单列表:
  971. * @param int $id
  972. * @param int $type 0:普通、1:秒杀、2:砍价、3:拼团、4:积分、5:套餐、6:预售、7:新人礼
  973. * @param array $where
  974. * @param int $page
  975. * @param int $limit
  976. * @return array
  977. * @throws \think\db\exception\DataNotFoundException
  978. * @throws \think\db\exception\DbException
  979. * @throws \think\db\exception\ModelNotFoundException
  980. */
  981. public function activityStatisticsOrder(int $id, int $type = 1, array $where = [], int $page = 0, int $limit = 0)
  982. {
  983. return $this->search($where)->where('pid', 'in', [0, -1])->where('paid', 1)->where('type', $type)->where('activity_id', $id)
  984. ->when($page && $limit, function ($query) use ($page, $limit) {
  985. $query->page($page, $limit);
  986. })->field(['order_id', 'real_name', 'status', 'pay_price', 'total_num', 'add_time', 'pay_time', 'paid', 'shipping_type', 'refund_status', 'is_del', 'is_system_del'])->select()->toArray();
  987. }
  988. /**
  989. * 秒杀参与人统计
  990. * @param int $id
  991. * @param string $keyword
  992. * @param int $page
  993. * @param int $limit
  994. * @return mixed
  995. */
  996. public function seckillPeople(int $id, string $keyword, int $page = 0, int $limit = 0)
  997. {
  998. return $this->getModel()
  999. ->when($id != 0, function ($query) use ($id) {
  1000. $query->where('type', 1)->where('activity_id', $id);
  1001. })->when($keyword != '', function ($query) use ($keyword) {
  1002. $query->where('real_name|uid|user_phone', 'like', '%' . $keyword . '%');
  1003. })->where('pid', 'in', [0, -1])->where('paid', 1)->field([
  1004. 'real_name',
  1005. 'uid',
  1006. 'SUM(total_num) as goods_num',
  1007. 'COUNT(id) as order_num',
  1008. 'SUM(pay_price) as total_price',
  1009. 'add_time'
  1010. ])->group('uid')->order("add_time desc")->when($page && $limit, function ($query) use ($page, $limit) {
  1011. $query->page($page, $limit);
  1012. })->select()->toArray();
  1013. }
  1014. /**
  1015. * @param int $pid
  1016. * @return array
  1017. * @throws \think\db\exception\DataNotFoundException
  1018. * @throws \think\db\exception\DbException
  1019. * @throws \think\db\exception\ModelNotFoundException
  1020. */
  1021. public function getSubOrderNotSendList(int $pid)
  1022. {
  1023. return $this->getModel()->where('pid', $pid)->where('status', 1)->select()->toArray();
  1024. }
  1025. /**
  1026. * @param int $pid
  1027. * @param int $order_id
  1028. * @return int
  1029. * @throws \think\db\exception\DbException
  1030. */
  1031. public function getSubOrderNotSend(int $pid, int $order_id)
  1032. {
  1033. return $this->getModel()->where('pid', $pid)->where('status', 0)->where('id', '<>', $order_id)->count();
  1034. }
  1035. /**
  1036. * @param int $pid
  1037. * @param int $order_id
  1038. * @return int
  1039. * @throws \think\db\exception\DbException
  1040. */
  1041. public function getSubOrderNotTake(int $pid, int $order_id)
  1042. {
  1043. return $this->getModel()->where('pid', $pid)->where('status', 1)->where('id', '<>', $order_id)->count();
  1044. }
  1045. }