StoreOrderDao.php 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  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. });
  238. }
  239. /**
  240. * 获取某一个月订单数量
  241. * @param array $where
  242. * @param string $month
  243. * @return int
  244. */
  245. public function getMonthCount(array $where, string $month)
  246. {
  247. return $this->search($where)->whereMonth('add_time', $month)->count();
  248. }
  249. /**
  250. * 获取某一个月订单金额
  251. * @param array $where
  252. * @param string $month
  253. * @param string $field
  254. * @return float
  255. */
  256. public function getMonthMoneyCount(array $where, string $month, string $field)
  257. {
  258. return $this->search($where)->whereMonth('add_time', $month)->sum($field);
  259. }
  260. /**
  261. * 获取购买历史用户
  262. * @param int $storeId
  263. * @param int $staffId
  264. * @param int $limit
  265. * @return mixed
  266. */
  267. public function getOrderHistoryList(int $storeId, int $staffId, array $uid = [], int $limit = 20)
  268. {
  269. return $this->search(['store_id' => $storeId, 'staff_id' => $staffId])->when($uid, function ($query) use ($uid) {
  270. $query->whereNotIn('uid', $uid);
  271. })->where('uid', '<>', 0)->with('user')->limit($limit)
  272. ->group('uid')->order('add_time', 'desc')->field(['uid', 'store_id', 'staff_id'])->select()->toArray();
  273. }
  274. /**
  275. * 订单搜索列表
  276. * @param array $where
  277. * @param array $field
  278. * @param int $page
  279. * @param int $limit
  280. * @param array $with
  281. * @return array
  282. * @throws \think\db\exception\DataNotFoundException
  283. * @throws \think\db\exception\DbException
  284. * @throws \think\db\exception\ModelNotFoundException
  285. */
  286. public function getList(array $where, array $field, int $page = 0, int $limit = 0, array $with = [])
  287. {
  288. return $this->search($where)->field($field)->with($with)->when($page && $limit, function ($query) use ($page, $limit) {
  289. $query->page($page, $limit);
  290. })->order('pay_time DESC,id DESC')->select()->toArray();
  291. }
  292. /**
  293. * 获取待核销的订单列表
  294. * @param array $where
  295. * @param array|string[] $field
  296. * @return mixed
  297. */
  298. public function getUnWirteOffList(array $where, array $field = ['*'])
  299. {
  300. 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)
  301. ->where(function ($query) {
  302. $query->where('shipping_type', 2)->whereOr('delivery_type', 'send');
  303. })->order('pay_time DESC,id DESC')->select()->toArray();
  304. }
  305. /**
  306. * 订单搜索列表
  307. * @param array $where
  308. * @param array $field
  309. * @param int $page
  310. * @param int $limit
  311. * @param array $with
  312. * @param string $order
  313. * @return array
  314. * @throws \think\db\exception\DataNotFoundException
  315. * @throws \think\db\exception\DbException
  316. * @throws \think\db\exception\ModelNotFoundException
  317. */
  318. public function getOrderList(array $where, array $field, int $page = 0, int $limit = 0, array $with = [], string $order = 'add_time DESC,id DESC')
  319. {
  320. return $this->search($where)->field($field)->with(array_merge(['user', 'spread', 'refund'], $with))->when($page && $limit, function ($query) use ($page, $limit) {
  321. $query->page($page, $limit);
  322. })->when(!$page && $limit, function ($query) use ($limit) {
  323. $query->limit($limit);
  324. })->order($order)->select()->toArray();
  325. }
  326. /**
  327. * 聚合查询
  328. * @param array $where
  329. * @param string $field
  330. * @param string $together
  331. * @return int
  332. */
  333. public function together(array $where, string $field, string $together = 'sum')
  334. {
  335. if (!in_array($together, ['sum', 'max', 'min', 'avg'])) {
  336. return 0;
  337. }
  338. return $this->search($where)->{$together}($field);
  339. }
  340. /**
  341. * 查找指定条件下的订单数据以数组形式返回
  342. * @param array $where
  343. * @param string $field
  344. * @param string $key
  345. * @param string $group
  346. * @return array
  347. */
  348. public function column(array $where, string $field, string $key = '', string $group = '')
  349. {
  350. return $this->search($where)->when($group, function ($query) use ($group) {
  351. $query->group($group);
  352. })->column($field, $key);
  353. }
  354. /**
  355. * 获取订单id下没有删除的订单数量
  356. * @param array $ids
  357. * @return int
  358. */
  359. public function getOrderIdsCount(array $ids)
  360. {
  361. return $this->getModel()->whereIn('id', $ids)->where('is_del', 0)->count();
  362. }
  363. /**
  364. * 获取一段时间订单统计数量、金额
  365. * @param array $where
  366. * @param array $time
  367. * @param string $timeType
  368. * @param bool $is_pid
  369. * @param string $countField
  370. * @param string $sumField
  371. * @param string $groupField
  372. * @return array
  373. * @throws \think\db\exception\DataNotFoundException
  374. * @throws \think\db\exception\DbException
  375. * @throws \think\db\exception\ModelNotFoundException
  376. */
  377. public function orderAddTimeList(array $where, array $time, string $timeType = "week", bool $is_pid = true, string $countField = '*', string $sumField = 'pay_price', string $groupField = 'add_time')
  378. {
  379. return $this->getModel()->where($where)->when($is_pid, function ($query) {
  380. $query->where('pid', '>=', 0);
  381. })->when(!isset($where['refund_status']), function ($query) {
  382. $query->whereIn('refund_status', [0, 3]);
  383. })->where('paid', 1)->where('is_del', 0)->where('is_system_del', 0)
  384. ->where(isset($where['timekey']) && $where['timekey'] ? $where['timekey'] : 'add_time', 'between time', $time)
  385. ->when($timeType, function ($query) use ($timeType, $countField, $sumField, $groupField) {
  386. switch ($timeType) {
  387. case "hour":
  388. $timeUnix = "%H";
  389. break;
  390. case "day" :
  391. $timeUnix = "%Y-%m-%d";
  392. break;
  393. case "week" :
  394. $timeUnix = "%w";
  395. break;
  396. case "month" :
  397. $timeUnix = "%d";
  398. break;
  399. case "weekly" :
  400. $timeUnix = "%W";
  401. break;
  402. case "year" :
  403. $timeUnix = "%Y-%m";
  404. break;
  405. default:
  406. $timeUnix = "%Y-%m-%d";
  407. break;
  408. }
  409. $query->field("FROM_UNIXTIME(`" . $groupField . "`,'$timeUnix') as day,count(" . $countField . ") as count,sum(`" . $sumField . "`) as price");
  410. $query->group('day');
  411. })->order('add_time asc')->select()->toArray();
  412. }
  413. /**
  414. * 统计总数上期
  415. * @param array $where
  416. * @param array $time
  417. * @return array|\think\Model|null
  418. * @throws \think\db\exception\DataNotFoundException
  419. * @throws \think\db\exception\DbException
  420. * @throws \think\db\exception\ModelNotFoundException
  421. */
  422. public function preTotalFind(array $where, array $time, string $sumField = 'pay_price', string $groupField = 'add_time')
  423. {
  424. 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)
  425. ->where($groupField, 'between time', $time)
  426. ->field("count(*) as count,sum(`" . $sumField . "`) as price")
  427. ->find();
  428. }
  429. /**
  430. * 新订单ID
  431. * @param $status
  432. * @param int $store_id
  433. * @return array
  434. */
  435. public function newOrderId($status, int $store_id = 0)
  436. {
  437. return $this->search(['status' => $status, 'is_remind' => 0])->where('store_id', $store_id)->column('order_id', 'id');
  438. }
  439. /**
  440. * 获取订单数量
  441. * @param int $store_id
  442. * @param int $type
  443. * @param string $field
  444. * @return int
  445. */
  446. public function storeOrderCount(int $val = 0, int $type = -1, string $field = 'store_id')
  447. {
  448. $where = ['pid' => 0, 'status' => 1];
  449. if ($type != -1) $where['type'] = $type;
  450. return $this->search($where)->when($field && $val > 0, function ($query) use ($field, $val) {
  451. $query->where($field, $val);
  452. })->count();
  453. }
  454. /**
  455. * 总销售额
  456. * @param $time
  457. * @param int $store_id
  458. * @return float
  459. */
  460. public function totalSales($time, int $store_id = 0)
  461. {
  462. 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');
  463. }
  464. /**
  465. * 获取特定时间内订单量
  466. * @param $time
  467. * @return int
  468. */
  469. public function totalOrderCount($time)
  470. {
  471. return $this->search(['pid' => 0, 'time' => $time ?: 'today', 'timeKey' => 'add_time'])->where('paid', 1)->whereIn('refund_status', [0, 3])->where('store_id', 0)->count();
  472. }
  473. /**
  474. * 获取订单详情
  475. * @param string $key
  476. * @param int $uid
  477. * @param array $with
  478. * @return array|\think\Model|null
  479. * @throws \think\db\exception\DataNotFoundException
  480. * @throws \think\db\exception\DbException
  481. * @throws \think\db\exception\ModelNotFoundException
  482. */
  483. public function getUserOrderDetail(string $key, int $uid, array $with = [])
  484. {
  485. return $this->getOne(['order_id|id|unique' => $key, 'uid' => $uid, 'is_del' => 0], '*', $with);
  486. }
  487. /**
  488. * 获取用户推广订单
  489. * @param array $where
  490. * @param string $field
  491. * @param int $page
  492. * @param int $limit
  493. * @param array $with
  494. * @return array
  495. * @throws \think\db\exception\DataNotFoundException
  496. * @throws \think\db\exception\DbException
  497. * @throws \think\db\exception\ModelNotFoundException
  498. */
  499. public function getStairOrderList(array $where, string $field, int $page, int $limit, array $with = [])
  500. {
  501. return $this->search($where)->with($with)->field($field)->when($page && $limit, function ($query) use ($page, $limit) {
  502. $query->page($page, $limit);
  503. })->order('id DESC')->select()->toArray();
  504. }
  505. /**
  506. * 订单每月统计数据
  507. * @param int $page
  508. * @param int $limit
  509. * @return array
  510. */
  511. public function getOrderDataPriceCount(array $where, array $field, int $page, int $limit)
  512. {
  513. return $this->search($where)
  514. ->field($field)->group("FROM_UNIXTIME(add_time, '%Y-%m-%d')")
  515. ->order('add_time DESC')->page($page, $limit)->select()->toArray();
  516. }
  517. /**
  518. * 获取当前时间到指定时间的支付金额 管理员
  519. * @param $start
  520. * @param $stop
  521. * @param int $store_id
  522. * @return mixed
  523. */
  524. public function chartTimePrice($start, $stop, int $store_id = 0)
  525. {
  526. return $this->search(['pid' => 0, 'is_del' => 0, 'paid' => 1, 'refund_status' => [0, 3], 'is_system_del' => 0])
  527. ->where('store_id', $store_id)
  528. ->where('add_time', '>=', $start)
  529. ->where('add_time', '<', $stop)
  530. ->field('sum(pay_price) as num,FROM_UNIXTIME(add_time, \'%Y-%m-%d\') as time')
  531. ->group("FROM_UNIXTIME(add_time, '%Y-%m-%d')")
  532. ->order('add_time ASC')->select()->toArray();
  533. }
  534. /**
  535. * 获取当前时间到指定时间的支付订单数 管理员
  536. * @param $start
  537. * @param $stop
  538. * @param int $store_id
  539. * @return mixed
  540. */
  541. public function chartTimeNumber($start, $stop, int $store_id = 0)
  542. {
  543. return $this->search(['pid' => 0, 'is_del' => 0, 'paid' => 1, 'refund_status' => [0, 3]])
  544. ->where('store_id', $store_id)
  545. ->where('add_time', '>=', $start)
  546. ->where('add_time', '<', $stop)
  547. ->field('count(id) as num,FROM_UNIXTIME(add_time, \'%Y-%m-%d\') as time')
  548. ->group("FROM_UNIXTIME(add_time, '%Y-%m-%d')")
  549. ->order('add_time ASC')->select()->toArray();
  550. }
  551. /**
  552. * 获取用户已购买此活动商品的个数
  553. * @param $uid
  554. * @param $type
  555. * @param $activity_id
  556. * @return int
  557. */
  558. public function getBuyCount($uid, $type, $activity_id): int
  559. {
  560. return $this->getModel()
  561. ->where('uid', $uid)
  562. ->where('type', $type)
  563. ->where('activity_id', $activity_id)
  564. ->whereIn('pid', [0, -1])
  565. ->where(function ($query) {
  566. $query->where('paid', 1)->whereOr(function ($query1) {
  567. $query1->where('paid', 0)->where('is_del', 0);
  568. });
  569. })->value('sum(total_num)') ?? 0;
  570. }
  571. /**
  572. * 获取没有支付的订单列表
  573. * @param int $store_id
  574. * @param int $page
  575. * @param int $limit
  576. * @return \crmeb\basic\BaseModel
  577. */
  578. public function getOrderUnPaid(int $store_id = 0, int $page = 0, int $limit = 0)
  579. {
  580. return $this->getModel()
  581. ->where(['pid' => 0, 'paid' => 0, 'is_del' => 0, 'status' => 0, 'refund_status' => 0])
  582. ->where('store_id', $store_id)
  583. ->where('pay_type', '<>', 'offline')
  584. ->when($page && $limit, function ($query) use ($page, $limit) {
  585. $query->page($page, $limit);
  586. });
  587. }
  588. /**
  589. * 用户趋势数据
  590. * @param $time
  591. * @param $type
  592. * @param $timeType
  593. * @return mixed
  594. */
  595. public function getTrendData($time, $type, $timeType, $str)
  596. {
  597. return $this->getModel()->when($type != '', function ($query) use ($type) {
  598. $query->where('channel_type', $type);
  599. })->where('paid', 1)->where('pid', '>=', 0)->where(function ($query) use ($time) {
  600. if ($time[0] == $time[1]) {
  601. $query->whereDay('pay_time', $time[0]);
  602. } else {
  603. $time[1] = date('Y/m/d', strtotime($time[1]) + 86400);
  604. $query->whereTime('pay_time', 'between', $time);
  605. }
  606. })->field("FROM_UNIXTIME(pay_time,'$timeType') as days,$str as num")
  607. ->group('days')->select()->toArray();
  608. }
  609. /**
  610. * 用户地域数据
  611. * @param $time
  612. * @param $userType
  613. * @return mixed
  614. */
  615. public function getRegion($time, $userType)
  616. {
  617. return $this->getModel()->when($userType != '', function ($query) use ($userType) {
  618. $query->where('channel_type', $userType);
  619. })->where('pid', '>=', 0)->where('store_id', 0)->where(function ($query) use ($time) {
  620. if ($time[0] == $time[1]) {
  621. $query->whereDay('pay_time', $time[0]);
  622. } else {
  623. $time[1] = date('Y/m/d', strtotime($time[1]) + 86400);
  624. $query->whereTime('pay_time', 'between', $time);
  625. }
  626. })->field('pay_price as payPrice,substring_index(user_address, " ", 1) as province')->select()->toArray();
  627. }
  628. /**
  629. * 商品趋势
  630. * @param $time
  631. * @param $timeType
  632. * @param $field
  633. * @param $str
  634. * @return mixed
  635. */
  636. public function getProductTrend($time, $timeType, $field, $str, $orderStatus = '')
  637. {
  638. return $this->getModel()->where(function ($query) use ($field, $orderStatus) {
  639. if ($field == 'pay_time') {
  640. $query->where('paid', 1);
  641. } elseif ($field == 'refund_reason_time') {
  642. $query->where('paid', 1)->where('refund_status', '>', 0);
  643. } elseif ($field == 'add_time') {
  644. if ($orderStatus == 'pay') {
  645. $query->where('paid', 1)->where('pid', '>=', 0)->where('refund_status', 0);
  646. } elseif ($orderStatus == 'refund') {
  647. $query->where('paid', 1)->where('pid', '>=', 0)->where('refund_status', '>', 0);
  648. } elseif ($orderStatus == 'coupon') {
  649. $query->where('paid', 1)->where('pid', '>=', 0)->where('coupon_id', '>', 0);
  650. }
  651. }
  652. })->where('pid', '>=', 0)->where('store_id', 0)->where(function ($query) use ($time, $field) {
  653. if ($time[0] == $time[1]) {
  654. $query->whereDay($field, $time[0]);
  655. } else {
  656. $time[1] = date('Y/m/d', strtotime($time[1]) + 86400);
  657. $query->whereTime($field, 'between', $time);
  658. }
  659. })->field("FROM_UNIXTIME($field,'$timeType') as days,$str as num")->group('days')->select()->toArray();
  660. }
  661. /**
  662. * 按照支付时间统计支付金额
  663. * @param array $where
  664. * @param string $sumField
  665. * @return mixed
  666. */
  667. public function getDayTotalMoney(array $where, string $sumField)
  668. {
  669. return $this->search($where)
  670. ->when(isset($where['timeKey']), function ($query) use ($where) {
  671. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  672. })
  673. ->sum($sumField);
  674. }
  675. /**
  676. * 时间段订单数统计
  677. * @param array $where
  678. * @param string $countField
  679. * @return int
  680. */
  681. public function getDayOrderCount(array $where, string $countField = "*")
  682. {
  683. return $this->search($where)
  684. ->when(isset($where['timeKey']), function ($query) use ($where) {
  685. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  686. })
  687. ->count($countField);
  688. }
  689. /**
  690. * 时间分组订单付款金额统计
  691. * @param array $where
  692. * @param string $sumField
  693. * @return mixed
  694. */
  695. public function getDayGroupMoney(array $where, string $sumField, string $group)
  696. {
  697. return $this->search($where)
  698. ->when(isset($where['timeKey']), function ($query) use ($where, $sumField, $group) {
  699. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  700. if ($where['timeKey']['days'] == 1) {
  701. $timeUinx = "%H";
  702. } elseif ($where['timeKey']['days'] == 30) {
  703. $timeUinx = "%Y-%m-%d";
  704. } elseif ($where['timeKey']['days'] == 365) {
  705. $timeUinx = "%Y-%m";
  706. } elseif ($where['timeKey']['days'] > 1 && $where['timeKey']['days'] < 30) {
  707. $timeUinx = "%Y-%m-%d";
  708. } elseif ($where['timeKey']['days'] > 30 && $where['timeKey']['days'] < 365) {
  709. $timeUinx = "%Y-%m";
  710. } else {
  711. $timeUinx = "%Y-%m";
  712. }
  713. $query->field("sum($sumField) as number,FROM_UNIXTIME($group, '$timeUinx') as time");
  714. $query->group("FROM_UNIXTIME($group, '$timeUinx')");
  715. })
  716. ->order('pay_time ASC')->select()->toArray();
  717. }
  718. /**
  719. * 时间分组订单数统计
  720. * @param array $where
  721. * @param string $sumField
  722. * @return mixed
  723. */
  724. public function getOrderGroupCount(array $where, string $sumField = "*")
  725. {
  726. return $this->search($where)
  727. ->when(isset($where['timeKey']), function ($query) use ($where, $sumField) {
  728. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  729. if ($where['timeKey']['days'] == 1) {
  730. $timeUinx = "%H";
  731. } elseif ($where['timeKey']['days'] == 30) {
  732. $timeUinx = "%Y-%m-%d";
  733. } elseif ($where['timeKey']['days'] == 365) {
  734. $timeUinx = "%Y-%m";
  735. } elseif ($where['timeKey']['days'] > 1 && $where['timeKey']['days'] < 30) {
  736. $timeUinx = "%Y-%m-%d";
  737. } elseif ($where['timeKey']['days'] > 30 && $where['timeKey']['days'] < 365) {
  738. $timeUinx = "%Y-%m";
  739. } else {
  740. $timeUinx = "%Y-%m";
  741. }
  742. $query->field("count($sumField) as number,FROM_UNIXTIME(pay_time, '$timeUinx') as time");
  743. $query->group("FROM_UNIXTIME(pay_time, '$timeUinx')");
  744. })
  745. ->order('pay_time ASC')->select()->toArray();
  746. }
  747. /**
  748. * 时间段支付订单人数
  749. * @param $where
  750. * @return mixed
  751. */
  752. public function getPayOrderPeople($where)
  753. {
  754. return $this->search($where)
  755. ->when(isset($where['timeKey']), function ($query) use ($where) {
  756. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  757. })
  758. ->field('uid')
  759. ->distinct(true)
  760. ->select()->toArray();
  761. }
  762. /**
  763. * 时间段分组统计支付订单人数
  764. * @param $where
  765. * @return mixed
  766. */
  767. public function getPayOrderGroupPeople($where)
  768. {
  769. return $this->search($where)
  770. ->when(isset($where['timeKey']), function ($query) use ($where) {
  771. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  772. if ($where['timeKey']['days'] == 1) {
  773. $timeUinx = "%H";
  774. } elseif ($where['timeKey']['days'] == 30) {
  775. $timeUinx = "%Y-%m-%d";
  776. } elseif ($where['timeKey']['days'] == 365) {
  777. $timeUinx = "%Y-%m";
  778. } elseif ($where['timeKey']['days'] > 1 && $where['timeKey']['days'] < 30) {
  779. $timeUinx = "%Y-%m-%d";
  780. } elseif ($where['timeKey']['days'] > 30 && $where['timeKey']['days'] < 365) {
  781. $timeUinx = "%Y-%m";
  782. } else {
  783. $timeUinx = "%Y-%m";
  784. }
  785. $query->field("count(distinct uid) as number,FROM_UNIXTIME(pay_time, '$timeUinx') as time");
  786. $query->group("FROM_UNIXTIME(pay_time, '$timeUinx')");
  787. })
  788. ->order('pay_time ASC')->select()->toArray();
  789. }
  790. /**
  791. * 获取批量打印电子面单数据
  792. * @param array $where
  793. * @param array $ids
  794. * @param string $filed
  795. * @param int $store_id
  796. * @return array
  797. * @throws \think\db\exception\DataNotFoundException
  798. * @throws \think\db\exception\DbException
  799. * @throws \think\db\exception\ModelNotFoundException
  800. */
  801. public function getOrderDumpData(array $where, array $ids = [], $filed = "*", int $store_id = 0)
  802. {
  803. $where['pid'] = 0;
  804. $where['status'] = 1;
  805. $where['refund_status'] = 0;
  806. $where['paid'] = 1;
  807. $where['is_del'] = 0;
  808. $where['shipping_type'] = 1;
  809. $where['is_system_del'] = 0;
  810. $where['store_id'] = $store_id;
  811. return $this->search($where)->when($ids, function ($query) use ($ids) {
  812. $query->whereIn('id', $ids);
  813. })->field($filed)->with(['pink'])->select()->toArray();
  814. }
  815. /**
  816. * @param array $where
  817. * @param string $field
  818. * @return array
  819. * @throws \think\db\exception\DataNotFoundException
  820. * @throws \think\db\exception\DbException
  821. * @throws \think\db\exception\ModelNotFoundException
  822. */
  823. public function getOrderListByWhere(array $where, $field = "*")
  824. {
  825. return $this->search($where)->field($field)->select()->toArray();
  826. }
  827. /**
  828. * 批量修改订单
  829. * @param array $ids
  830. * @param array $data
  831. * @param string|null $key
  832. * @return \crmeb\basic\BaseModel
  833. */
  834. public function batchUpdateOrder(array $ids, array $data, ?string $key = null)
  835. {
  836. return $this->getModel()::whereIn(is_null($key) ? $this->getPk() : $key, $ids)->update($data);
  837. }
  838. /**
  839. * 获取拆单之后的子订单
  840. * @param int $id
  841. * @param string $field
  842. * @param int $type 1:不包含自己2:包含自己
  843. * @return array
  844. * @throws \think\db\exception\DataNotFoundException
  845. * @throws \think\db\exception\DbException
  846. * @throws \think\db\exception\ModelNotFoundException
  847. */
  848. public function getSonOrder(int $id, string $field = '*', int $type = 1)
  849. {
  850. return in_array($type, [1, 2]) ? $this->getModel()::field($field)->when($type, function ($query) use ($id, $type) {
  851. if ($type == 1) {
  852. $query->where('pid', $id);
  853. } else {
  854. $query->where('pid', $id)->whereOr('id', $id);
  855. }
  856. })->select()->toArray() : [];
  857. }
  858. /**
  859. * 查询退款订单
  860. * @param $where
  861. * @param $page
  862. * @param $limit
  863. * @return array
  864. * @throws \think\db\exception\DataNotFoundException
  865. * @throws \think\db\exception\DbException
  866. * @throws \think\db\exception\ModelNotFoundException
  867. */
  868. public function getRefundList($where, $page = 0, $limit = 0)
  869. {
  870. $model = $this->getModel()
  871. ->where('is_system_del', 0)
  872. ->where('paid', 1)
  873. ->when(isset($where['store_id']), function ($query) use ($where) {
  874. $query->where('store_id', $where['store_id']);
  875. })->when(isset($where['refund_type']) && $where['refund_type'] !== '', function ($query) use ($where) {
  876. if ($where['refund_type'] == 0) {
  877. $query->where('refund_type', '>', 0);
  878. } else {
  879. $query->where('refund_type', $where['refund_type']);
  880. }
  881. })->when(isset($where['not_pid']), function ($query) {
  882. $query->where('pid', '<>', -1);
  883. })->when($where['order_id'] != '', function ($query) use ($where) {
  884. $query->where('order_id', $where['order_id']);
  885. })->when(is_array($where['refund_reason_time']), function ($query) use ($where) {
  886. $query->whereBetween('refund_reason_time', [strtotime($where['refund_reason_time'][0]), strtotime($where['refund_reason_time'][1]) + 86400]);
  887. })->with(array_merge(['user', 'spread']));
  888. $count = $model->count();
  889. $list = $model->when($page != 0 && $limit != 0, function ($query) use ($page, $limit) {
  890. $query->page($page, $limit);
  891. })->order('refund_reason_time desc')->select()->toArray();
  892. return compact('list', 'count');
  893. }
  894. /**
  895. * 门店线上支付订单详情
  896. * @param int $store_id
  897. * @param int $uid
  898. * @return array|\think\Model|null
  899. * @throws \think\db\exception\DataNotFoundException
  900. * @throws \think\db\exception\DbException
  901. * @throws \think\db\exception\ModelNotFoundException
  902. */
  903. public function payCashierOrder(int $store_id, int $uid)
  904. {
  905. return $this->getModel()->where('uid', $uid)->where('store_id', $store_id)->where('paid', 0)->where('is_del', 0)->where('is_system_del', 0)
  906. ->where('shipping_type', 4)
  907. ->order('add_time desc,id desc')
  908. ->find();
  909. }
  910. /**
  911. * 商品趋势
  912. * @param $time
  913. * @param $timeType
  914. * @param $field
  915. * @param $str
  916. * @param $orderStatus
  917. * @return mixed
  918. */
  919. public function getOrderStatistics($where, $time, $timeType, $field, $str, $orderStatus = '')
  920. {
  921. return $this->getModel()->where($where)->where(function ($query) use ($field, $orderStatus) {
  922. if ($field == 'pay_time') {
  923. $query->where('paid', 1);
  924. } elseif ($field == 'refund_reason_time') {
  925. $query->where('paid', 1)->where('refund_status', '>', 0);
  926. } elseif ($field == 'add_time') {
  927. if ($orderStatus == 'pay') {
  928. $query->where('paid', 1)->where('pid', '>=', 0)->whereIn('refund_status', [0, 3]);
  929. } elseif ($orderStatus == 'refund') {
  930. $query->where('paid', 1)->where('pid', '>=', 0)->where('refund_type', 6);
  931. }
  932. }
  933. })->where(function ($query) use ($time, $field) {
  934. if ($time[0] == $time[1]) {
  935. $query->whereDay($field, $time[0]);
  936. } else {
  937. $time[1] = date('Y/m/d', (!is_numeric($time[1]) ? strtotime($time[1]) : $time[1]) + 86400);
  938. $query->whereTime($field, 'between', $time);
  939. }
  940. })->where('is_del', 0)->where('is_system_del', 0)
  941. ->field("FROM_UNIXTIME($field,'$timeType') as days,$str as num")->group('days')->select()->toArray();
  942. }
  943. /**
  944. * 获取活动订单列表:
  945. * @param int $id
  946. * @param int $type 0:普通、1:秒杀、2:砍价、3:拼团、4:积分、5:套餐、6:预售、7:新人礼
  947. * @param array $where
  948. * @param int $page
  949. * @param int $limit
  950. * @return array
  951. * @throws \think\db\exception\DataNotFoundException
  952. * @throws \think\db\exception\DbException
  953. * @throws \think\db\exception\ModelNotFoundException
  954. */
  955. public function activityStatisticsOrder(int $id, int $type = 1,array $where = [], int $page = 0, int $limit = 0)
  956. {
  957. return $this->search($where)->where('pid', 'in', [0, -1])->where('paid', 1)->where('type', $type)->where('activity_id', $id)
  958. ->when($page && $limit, function ($query) use ($page, $limit) {
  959. $query->page($page, $limit);
  960. })->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();
  961. }
  962. /**
  963. * 秒杀参与人统计
  964. * @param int $id
  965. * @param string $keyword
  966. * @param int $page
  967. * @param int $limit
  968. * @return mixed
  969. */
  970. public function seckillPeople(int $id, string $keyword, int $page = 0, int $limit = 0)
  971. {
  972. return $this->getModel()
  973. ->when($id != 0, function ($query) use ($id) {
  974. $query->where('type', 1)->where('activity_id', $id);
  975. })->when($keyword != '', function ($query) use ($keyword) {
  976. $query->where('real_name|uid|user_phone', 'like', '%' . $keyword . '%');
  977. })->where('pid', 'in', [0, -1])->where('paid', 1)->field([
  978. 'real_name',
  979. 'uid',
  980. 'SUM(total_num) as goods_num',
  981. 'COUNT(id) as order_num',
  982. 'SUM(pay_price) as total_price',
  983. 'add_time'
  984. ])->group('uid')->order("add_time desc")->when($page && $limit, function ($query) use ($page, $limit) {
  985. $query->page($page, $limit);
  986. })->select()->toArray();
  987. }
  988. /**
  989. * @param int $pid
  990. * @return array
  991. * @throws \think\db\exception\DataNotFoundException
  992. * @throws \think\db\exception\DbException
  993. * @throws \think\db\exception\ModelNotFoundException
  994. */
  995. public function getSubOrderNotSendList(int $pid)
  996. {
  997. return $this->getModel()->where('pid', $pid)->where('status', 1)->select()->toArray();
  998. }
  999. /**
  1000. * @param int $pid
  1001. * @param int $order_id
  1002. * @return int
  1003. * @throws \think\db\exception\DbException
  1004. */
  1005. public function getSubOrderNotSend(int $pid, int $order_id)
  1006. {
  1007. return $this->getModel()->where('pid', $pid)->where('status', 0)->where('id', '<>', $order_id)->count();
  1008. }
  1009. /**
  1010. * @param int $pid
  1011. * @param int $order_id
  1012. * @return int
  1013. * @throws \think\db\exception\DbException
  1014. */
  1015. public function getSubOrderNotTake(int $pid, int $order_id)
  1016. {
  1017. return $this->getModel()->where('pid', $pid)->where('status', 1)->where('id', '<>', $order_id)->count();
  1018. }
  1019. }