StoreOrderDao.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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'];
  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. $isDel = isset($where['is_del']) && $where['is_del'] !== '' && $where['is_del'] != -1;
  41. $realName = $where['real_name'] ?? '';
  42. $fieldKey = $where['field_key'] ?? '';
  43. $fieldKey = $fieldKey == 'all' ? '' : $fieldKey;
  44. return parent::search($where)->when($isDel, function ($query) use ($where) {
  45. $query->where('is_del', $where['is_del']);
  46. })->when(isset($where['is_system_del']), function ($query) {
  47. $query->where('is_system_del', 0);
  48. })->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
  49. switch ((int)$where['status']) {
  50. case 0://未支付
  51. $query->where('paid', 0)->where('status', 0)->where('refund_status', 0)->where('is_del', 0);
  52. break;
  53. case 1://已支付 未发货
  54. $query->where('paid', 1)->where('status', 0)->where('refund_status', 0)->when(isset($where['shipping_type']), function ($query) {
  55. $query->where('shipping_type', 1);
  56. })->where('is_del', 0);
  57. break;
  58. case 2://已支付 待收货
  59. $query->where('paid', 1)->where('status', 1)->where('refund_status', 0)->where('is_del', 0);
  60. break;
  61. case 3:// 已支付 已收货 待评价
  62. $query->where('paid', 1)->where('status', 2)->where('refund_status', 0)->where('is_del', 0);
  63. break;
  64. case 4:// 交易完成
  65. $query->where('paid', 1)->where('status', 3)->where('refund_status', 0)->where('is_del', 0);
  66. break;
  67. case 5://已支付 待核销
  68. $query->where('paid', 1)->where('status', 0)->where('refund_status', 0)->where('shipping_type', 2)->where('is_del', 0);
  69. break;
  70. case 6://已支付 已核销 没有退款
  71. $query->where('paid', 1)->where('status', 2)->where('refund_status', 0)->where('shipping_type', 2)->where('is_del', 0);
  72. break;
  73. case -1://退款中
  74. $query->where('paid', 1)->where('refund_status', 1)->where('is_del', 0);
  75. break;
  76. case -2://已退款
  77. $query->where('paid', 1)->where('refund_status', 2)->where('is_del', 0);
  78. break;
  79. case -3://退款
  80. $query->where('paid', 1)->where('refund_status', 'in', '1,2')->where('is_del', 0);
  81. break;
  82. case -4://已删除
  83. $query->where('is_del', 1);
  84. break;
  85. }
  86. })->when(isset($where['type']), function ($query) use ($where) {
  87. switch ($where['type']) {
  88. case 1:
  89. $query->where('combination_id', 0)->where('seckill_id', 0)->where('bargain_id', 0);
  90. break;
  91. case 2:
  92. $query->where('pink_id|combination_id', ">", 0);
  93. break;
  94. case 3:
  95. $query->where('seckill_id', ">", 0);
  96. break;
  97. case 4:
  98. $query->where('bargain_id', ">", 0);
  99. break;
  100. }
  101. })->when(isset($where['pay_type']), function ($query) use ($where) {
  102. switch ($where['pay_type']) {
  103. case 1:
  104. $query->where('pay_type', 'weixin');
  105. break;
  106. case 2:
  107. $query->where('pay_type', 'yue');
  108. break;
  109. case 3:
  110. $query->where('pay_type', 'offline');
  111. break;
  112. case 4:
  113. $query->where('pay_type', 'alipay');
  114. break;
  115. }
  116. })->when($realName && $fieldKey && in_array($fieldKey, $this->withField), function ($query) use ($where, $realName, $fieldKey) {
  117. if ($fieldKey !== 'title') {
  118. $query->where(trim($fieldKey), trim($realName));
  119. } else {
  120. $query->where('id', 'in', function ($que) use ($where) {
  121. $que->name('store_order_cart_info')->whereIn('product_id', function ($q) use ($where) {
  122. $q->name('store_product')->whereLike('store_name|keyword', '%' . $where['real_name'] . '%')->field(['id'])->select();
  123. })->field(['oid'])->select();
  124. });
  125. }
  126. })->when($realName && !$fieldKey, function ($query) use ($where) {
  127. $query->where(function ($que) use ($where) {
  128. $que->whereLike('order_id|real_name', '%' . $where['real_name'] . '%')->whereOr('uid', 'in', function ($q) use ($where) {
  129. $q->name('user')->whereLike('nickname|uid|phone', '%' . $where['real_name'] . '%')->field(['uid'])->select();
  130. })->whereOr('id', 'in', function ($que) use ($where) {
  131. $que->name('store_order_cart_info')->whereIn('product_id', function ($q) use ($where) {
  132. $q->name('store_product')->whereLike('store_name|keyword', '%' . $where['real_name'] . '%')->field(['id'])->select();
  133. })->field(['oid'])->select();
  134. });
  135. });
  136. })->when(isset($where['store_id']) && $where['store_id'], function ($query) use ($where) {
  137. $query->where('store_id', $where['store_id']);
  138. })->when(isset($where['unique']), function ($query) use ($where) {
  139. $query->where('unique', $where['unique']);
  140. })->when(isset($where['is_remind']), function ($query) use ($where) {
  141. $query->where('is_remind', $where['is_remind']);
  142. });
  143. }
  144. /**
  145. * 订单搜索列表
  146. * @param array $where
  147. * @param array $field
  148. * @param int $page
  149. * @param int $limit
  150. * @param array $with
  151. * @return array
  152. * @throws \think\db\exception\DataNotFoundException
  153. * @throws \think\db\exception\DbException
  154. * @throws \think\db\exception\ModelNotFoundException
  155. */
  156. public function getOrderList(array $where, array $field, int $page, int $limit, array $with = [])
  157. {
  158. return $this->search($where)->field($field)->with(array_merge(['user', 'spread'], $with))->page($page, $limit)->order('add_time DESC,id DESC')->select()->toArray();
  159. }
  160. /**
  161. * 获取订单总数
  162. * @param array $where
  163. * @return int
  164. */
  165. public function count(array $where = []): int
  166. {
  167. return $this->search($where)->count();
  168. }
  169. /**
  170. * 聚合查询
  171. * @param array $where
  172. * @param string $field
  173. * @param string $together
  174. * @return int
  175. */
  176. public function together(array $where, string $field, string $together = 'sum')
  177. {
  178. if (!in_array($together, ['sum', 'max', 'min', 'avg'])) {
  179. return 0;
  180. }
  181. return $this->search($where)->{$together}($field);
  182. }
  183. /**
  184. * 查找指定条件下的订单数据以数组形式返回
  185. * @param array $where
  186. * @param string $field
  187. * @param string $key
  188. * @param string $group
  189. * @return array
  190. */
  191. public function column(array $where, string $field, string $key = '', string $group = '')
  192. {
  193. return $this->search($where)->when($group, function ($query) use ($group) {
  194. $query->group($group);
  195. })->column($field, $key);
  196. }
  197. /**
  198. * 获取订单id下没有删除的订单数量
  199. * @param array $ids
  200. * @return int
  201. */
  202. public function getOrderIdsCount(array $ids)
  203. {
  204. return $this->getModel()->whereIn('id', $ids)->where('is_del', 0)->count();
  205. }
  206. /**
  207. * 获取一段时间内订单列表
  208. * @param $datebefor
  209. * @param $dateafter
  210. * @return mixed
  211. */
  212. public function orderAddTimeList($datebefor, $dateafter, $timeType = "week")
  213. {
  214. return $this->getModel()->where('add_time', 'between time', [$datebefor, $dateafter])
  215. ->when($timeType, function ($query) use ($timeType) {
  216. switch ($timeType) {
  217. case "week" :
  218. $timeUnix = "%w";
  219. break;
  220. case "month" :
  221. $timeUnix = "%d";
  222. break;
  223. case "year" :
  224. $timeUnix = "%m";
  225. break;
  226. case "30" :
  227. $timeUnix = "%m-%d";
  228. break;
  229. }
  230. $query->field("FROM_UNIXTIME(add_time,'$timeUnix') as day,count(*) as count,sum(pay_price) as price");
  231. $query->group("FROM_UNIXTIME(add_time, '$timeUnix')");
  232. })
  233. ->order('add_time asc')
  234. ->select()->toArray();
  235. }
  236. /**
  237. * 统计总数上期
  238. * @param $pre_datebefor
  239. * @param $pre_dateafter
  240. * @return array|\think\Model|null
  241. * @throws \think\db\exception\DataNotFoundException
  242. * @throws \think\db\exception\DbException
  243. * @throws \think\db\exception\ModelNotFoundException
  244. */
  245. public function preTotalFind($pre_datebefor, $pre_dateafter)
  246. {
  247. return $this->getModel()->where('add_time', 'between time', [$pre_datebefor, $pre_dateafter])
  248. ->field("count(*) as count,sum(pay_price) as price")
  249. ->find();
  250. }
  251. /**
  252. * 获取一段时间内订单列表
  253. * @param $now_datebefor
  254. * @param $now_dateafter
  255. * @return mixed
  256. */
  257. public function nowOrderList($now_datebefor, $now_dateafter, $timeType = "week")
  258. {
  259. return $this->getModel()->where('add_time', 'between time', [$now_datebefor, $now_dateafter])
  260. ->when($timeType, function ($query) use ($timeType) {
  261. switch ($timeType) {
  262. case "week" :
  263. $timeUnix = "%w";
  264. break;
  265. case "month" :
  266. $timeUnix = "%d";
  267. break;
  268. case "year" :
  269. $timeUnix = "%m";
  270. break;
  271. }
  272. $query->field("FROM_UNIXTIME(add_time,'$timeUnix') as day,count(*) as count,sum(pay_price) as price");
  273. $query->group("FROM_UNIXTIME(add_time, '$timeUnix')");
  274. })
  275. ->order('add_time asc')
  276. ->select()->toArray();
  277. }
  278. /**
  279. * 获取订单数量
  280. * @return int
  281. */
  282. public function storeOrderCount()
  283. {
  284. return $this->search(['paid' => 1, 'is_del' => 0, 'refund_status' => 0, 'status' => 1, 'shipping_type' => 1])->count();
  285. }
  286. /**
  287. * 获取特定时间内订单总价
  288. * @param $time
  289. * @return float
  290. */
  291. public function todaySales($time)
  292. {
  293. return $this->search(['paid' => 1, 'is_del' => 0, 'refund_status' => 0, 'time' => $time ?: 'today', 'timekey' => 'pay_time'])->sum('pay_price');
  294. }
  295. /**
  296. * 获取特定时间内订单总价
  297. * @param $time
  298. * @return float
  299. */
  300. public function thisWeekSales($time)
  301. {
  302. return $this->search(['paid' => 1, 'is_del' => 0, 'refund_status' => 0, 'time' => $time ?: 'week', 'timeKey' => 'pay_time'])->sum('pay_price');
  303. }
  304. /**
  305. * 总销售额
  306. * @return float
  307. */
  308. public function totalSales($time)
  309. {
  310. return $this->search(['paid' => 1, 'is_del' => 0, 'refund_status' => 0, 'time' => $time ?: 'today', 'timekey' => 'pay_time'])->sum('pay_price');
  311. }
  312. public function newOrderUpdates($newOrderId)
  313. {
  314. return $this->getModel()->where('order_id', 'in', $newOrderId)->update(['is_remind' => 1]);
  315. }
  316. /**
  317. * 获取特定时间内订单量
  318. * @param $time
  319. * @return float
  320. */
  321. public function todayOrderVisit($time, $week)
  322. {
  323. switch ($week) {
  324. case 1:
  325. return $this->search(['time' => $time ?: 'today', 'timeKey' => 'add_time'])->count();
  326. case 2:
  327. return $this->search(['time' => $time ?: 'week', 'timeKey' => 'add_time'])->count();
  328. }
  329. }
  330. /**
  331. * 获取订单详情
  332. * @param $uid
  333. * @param $key
  334. * @return array|\think\Model|null
  335. * @throws \think\db\exception\DataNotFoundException
  336. * @throws \think\db\exception\DbException
  337. * @throws \think\db\exception\ModelNotFoundException
  338. */
  339. public function getUserOrderDetail(string $key, int $uid)
  340. {
  341. return $this->getOne(['order_id|unique' => $key, 'uid' => $uid, 'is_del' => 0]);
  342. }
  343. /**
  344. * 获取用户推广订单
  345. * @param array $where
  346. * @param string $field
  347. * @param int $page
  348. * @param int $limit
  349. * @param array $with
  350. * @return array
  351. * @throws \think\db\exception\DataNotFoundException
  352. * @throws \think\db\exception\DbException
  353. * @throws \think\db\exception\ModelNotFoundException
  354. */
  355. public function getStairOrderList(array $where, string $field, int $page, int $limit, array $with = [])
  356. {
  357. return $this->search($where)->with($with)->field($field)->page($page, $limit)->order('id DESC')->select()->toArray();
  358. }
  359. /**
  360. * 订单每月统计数据
  361. * @param int $page
  362. * @param int $limit
  363. * @return array
  364. */
  365. public function getOrderDataPriceCount(array $field, int $page, int $limit)
  366. {
  367. return $this->search(['is_del' => 0, 'paid' => 1, 'refund_status' => 0])
  368. ->field($field)->group("FROM_UNIXTIME(add_time, '%Y-%m-%d')")
  369. ->order('add_time DESC')->page($page, $limit)->select()->toArray();
  370. }
  371. /**
  372. * 获取当前时间到指定时间的支付金额 管理员
  373. * @param $start 开始时间
  374. * @param $stop 结束时间
  375. * @return mixed
  376. */
  377. public function chartTimePrice($start, $stop)
  378. {
  379. return $this->search(['is_del' => 0, 'paid' => 1, 'refund_status' => 0])
  380. ->where('add_time', '>=', $start)
  381. ->where('add_time', '<', $stop)
  382. ->field('sum(pay_price) as num,FROM_UNIXTIME(add_time, \'%Y-%m-%d\') as time')
  383. ->group("FROM_UNIXTIME(add_time, '%Y-%m-%d')")
  384. ->order('add_time ASC')->select()->toArray();
  385. }
  386. /**
  387. * 获取当前时间到指定时间的支付订单数 管理员
  388. * @param $start 开始时间
  389. * @param $stop 结束时间
  390. * @return mixed
  391. */
  392. public function chartTimeNumber($start, $stop)
  393. {
  394. return $this->search(['is_del' => 0, 'paid' => 1, 'refund_status' => 0])
  395. ->where('add_time', '>=', $start)
  396. ->where('add_time', '<', $stop)
  397. ->field('count(id) as num,FROM_UNIXTIME(add_time, \'%Y-%m-%d\') as time')
  398. ->group("FROM_UNIXTIME(add_time, '%Y-%m-%d')")
  399. ->order('add_time ASC')->select()->toArray();
  400. }
  401. /**
  402. * 获取用户已购买此活动商品的个数
  403. * @param $uid
  404. * @param $type
  405. * @param $typeId
  406. * @return int
  407. */
  408. public function getBuyCount($uid, $type, $typeId): int
  409. {
  410. return $this->getModel()
  411. ->where('uid', $uid)
  412. ->where($type, $typeId)
  413. ->where(function ($query) {
  414. $query->where('paid', 1)->whereOr(function ($query1) {
  415. $query1->where('paid', 0)->where('is_del', 0);
  416. });
  417. })->value('sum(total_num)') ?? 0;
  418. }
  419. /**
  420. * 获取没有支付的订单列表
  421. * @param array|string[] $field
  422. * @return array
  423. * @throws \think\db\exception\DataNotFoundException
  424. * @throws \think\db\exception\DbException
  425. * @throws \think\db\exception\ModelNotFoundException
  426. */
  427. public function getOrderUnPaidList(array $field = ['*'])
  428. {
  429. return $this->getModel()->where(['paid' => 0, 'is_del' => 0, 'status' => 0, 'refund_status' => 0])
  430. ->where('pay_type', '<>', 'offline')->field($field)->select();
  431. }
  432. /** 根据时间获取营业额
  433. * @param array $where
  434. * @return float|int
  435. */
  436. public function getOrderMoneyByTime(array $where)
  437. {
  438. if (isset($where['day'])) {
  439. return $this->getModel()->where(['refund_status' => 0, 'paid' => 1])->whereDay('add_time', date("Y-m-d", strtotime($where['day'])))->sum('pay_price');
  440. }
  441. return 0;
  442. }
  443. /**
  444. * 用户趋势数据
  445. * @param $time
  446. * @param $type
  447. * @param $timeType
  448. * @return mixed
  449. */
  450. public function getTrendData($time, $type, $timeType, $str)
  451. {
  452. return $this->getModel()->when($type != '', function ($query) use ($type) {
  453. $query->where('channel_type', $type);
  454. })->where(function ($query) use ($time) {
  455. if ($time[0] == $time[1]) {
  456. $query->whereDay('pay_time', $time[0]);
  457. } else {
  458. $time[1] = date('Y/m/d', strtotime($time[1]) + 86400);
  459. $query->whereTime('pay_time', 'between', $time);
  460. }
  461. })->field("FROM_UNIXTIME(pay_time,'$timeType') as days,$str as num")
  462. ->group('days')->select()->toArray();
  463. }
  464. /**
  465. * 用户地域数据
  466. * @param $time
  467. * @param $userType
  468. * @return mixed
  469. */
  470. public function getRegion($time, $userType)
  471. {
  472. return $this->getModel()->when($userType != '', function ($query) use ($userType) {
  473. $query->where('channel_type', $userType);
  474. })->where(function ($query) use ($time) {
  475. if ($time[0] == $time[1]) {
  476. $query->whereDay('pay_time', $time[0]);
  477. } else {
  478. $time[1] = date('Y/m/d', strtotime($time[1]) + 86400);
  479. $query->whereTime('pay_time', 'between', $time);
  480. }
  481. })->field('sum(pay_price) as payPrice,province')
  482. ->group('province')->select()->toArray();
  483. }
  484. /**
  485. * 商品趋势
  486. * @param $time
  487. * @param $timeType
  488. * @param $field
  489. * @param $str
  490. * @return mixed
  491. */
  492. public function getProductTrend($time, $timeType, $field, $str)
  493. {
  494. return $this->getModel()->where(function ($query) use ($field) {
  495. if ($field == 'pay_time') {
  496. $query->where('paid', 1);
  497. } elseif ($field == 'refund_reason_time') {
  498. $query->where('paid', 1)->where('refund_status', '>', 0);
  499. }
  500. })->where(function ($query) use ($time, $field) {
  501. if ($time[0] == $time[1]) {
  502. $query->whereDay($field, $time[0]);
  503. } else {
  504. $time[1] = date('Y/m/d', strtotime($time[1]) + 86400);
  505. $query->whereTime($field, 'between', $time);
  506. }
  507. })->field("FROM_UNIXTIME($field,'$timeType') as days,$str as num")->group('days')->select()->toArray();
  508. }
  509. /** 按照支付时间统计支付金额
  510. * @param array $where
  511. * @param string $sumField
  512. * @return mixed
  513. */
  514. public function getDayTotalMoney(array $where, string $sumField)
  515. {
  516. return $this->search($where)
  517. ->when(isset($where['timeKey']), function ($query) use ($where) {
  518. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  519. })
  520. ->sum($sumField);
  521. }
  522. /**时间段订单数统计
  523. * @param array $where
  524. * @param string $countField
  525. * @return int
  526. */
  527. public function getDayOrderCount(array $where, string $countField = "*")
  528. {
  529. return $this->search($where)
  530. ->when(isset($where['timeKey']), function ($query) use ($where) {
  531. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  532. })
  533. ->count($countField);
  534. }
  535. /** 时间分组订单付款金额统计
  536. * @param array $where
  537. * @param string $sumField
  538. * @return mixed
  539. */
  540. public function getDayGroupMoney(array $where, string $sumField, string $group)
  541. {
  542. return $this->search($where)
  543. ->when(isset($where['timeKey']), function ($query) use ($where, $sumField, $group) {
  544. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  545. if ($where['timeKey']['days'] == 1) {
  546. $timeUinx = "%H";
  547. } elseif ($where['timeKey']['days'] == 30) {
  548. $timeUinx = "%Y-%m-%d";
  549. } elseif ($where['timeKey']['days'] == 365) {
  550. $timeUinx = "%Y-%m";
  551. } elseif ($where['timeKey']['days'] > 1 && $where['timeKey']['days'] < 30) {
  552. $timeUinx = "%Y-%m-%d";
  553. } elseif ($where['timeKey']['days'] > 30 && $where['timeKey']['days'] < 365) {
  554. $timeUinx = "%Y-%m";
  555. }
  556. $query->field("sum($sumField) as number,FROM_UNIXTIME($group, '$timeUinx') as time");
  557. $query->group("FROM_UNIXTIME($group, '$timeUinx')");
  558. })
  559. ->order('pay_time ASC')->select()->toArray();
  560. // echo $this->getModel()->getLastSql();
  561. }
  562. /**时间分组订单数统计
  563. * @param array $where
  564. * @param string $sumField
  565. * @return mixed
  566. */
  567. public function getOrderGroupCount(array $where, string $sumField = "*")
  568. {
  569. return $this->search($where)
  570. ->when(isset($where['timeKey']), function ($query) use ($where, $sumField) {
  571. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  572. if ($where['timeKey']['days'] == 1) {
  573. $timeUinx = "%H";
  574. } elseif ($where['timeKey']['days'] == 30) {
  575. $timeUinx = "%Y-%m-%d";
  576. } elseif ($where['timeKey']['days'] == 365) {
  577. $timeUinx = "%Y-%m";
  578. } elseif ($where['timeKey']['days'] > 1 && $where['timeKey']['days'] < 30) {
  579. $timeUinx = "%Y-%m-%d";
  580. } elseif ($where['timeKey']['days'] > 30 && $where['timeKey']['days'] < 365) {
  581. $timeUinx = "%Y-%m";
  582. }
  583. $query->field("count($sumField) as number,FROM_UNIXTIME(pay_time, '$timeUinx') as time");
  584. $query->group("FROM_UNIXTIME(pay_time, '$timeUinx')");
  585. })
  586. ->order('pay_time ASC')->select()->toArray();
  587. // echo $this->getModel()->getLastSql();die;
  588. }
  589. /**时间段支付订单人数
  590. * @param $where
  591. * @return mixed
  592. */
  593. public function getPayOrderPeople($where)
  594. {
  595. return $this->search($where)
  596. ->when(isset($where['timeKey']), function ($query) use ($where) {
  597. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  598. })
  599. ->field('uid')
  600. ->distinct(true)
  601. ->select()->toArray();
  602. //echo $this->getModel()->getLastSql();die;
  603. }
  604. /**时间段分组统计支付订单人数
  605. * @param $where
  606. * @return mixed
  607. */
  608. public function getPayOrderGroupPeople($where)
  609. {
  610. return $this->search($where)
  611. ->when(isset($where['timeKey']), function ($query) use ($where) {
  612. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  613. if ($where['timeKey']['days'] == 1) {
  614. $timeUinx = "%H";
  615. } elseif ($where['timeKey']['days'] == 30) {
  616. $timeUinx = "%Y-%m-%d";
  617. } elseif ($where['timeKey']['days'] == 365) {
  618. $timeUinx = "%Y-%m";
  619. } elseif ($where['timeKey']['days'] > 1 && $where['timeKey']['days'] < 30) {
  620. $timeUinx = "%Y-%m-%d";
  621. } elseif ($where['timeKey']['days'] > 30 && $where['timeKey']['days'] < 365) {
  622. $timeUinx = "%Y-%m";
  623. }
  624. $query->field("count(distinct uid) as number,FROM_UNIXTIME(pay_time, '$timeUinx') as time");
  625. $query->group("FROM_UNIXTIME(pay_time, '$timeUinx')");
  626. })
  627. ->order('pay_time ASC')->select()->toArray();
  628. //echo $this->getModel()->getLastSql();die;
  629. }
  630. }