StoreOrderDao.php 38 KB

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