OtherOrderDao.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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\OtherOrder;
  14. class OtherOrderDao extends BaseDao
  15. {
  16. /** 设置模型
  17. * @return string
  18. */
  19. protected function setModel(): string
  20. {
  21. // TODO: Implement setModel() method.
  22. return OtherOrder::class;
  23. }
  24. /**
  25. * 获取某个时间点一共有多少用户是付费会员状态
  26. * @param $time
  27. * @param string $channel_type
  28. * @return int|mixed
  29. * @throws \think\db\exception\DataNotFoundException
  30. * @throws \think\db\exception\DbException
  31. * @throws \think\db\exception\ModelNotFoundException
  32. */
  33. public function getPayUserCount(int $time, string $channel_type = '')
  34. {
  35. return $this->getModel()->when($channel_type != '', function ($query) use ($channel_type) {
  36. $query->where('channel_type', $channel_type);
  37. })->field('distinct(uid),add_time')
  38. ->group('uid')->having('add_time < ' . $time)
  39. ->order('add_time desc')
  40. ->select()->toArray();
  41. }
  42. /**
  43. * 获取VIP曲线
  44. * @param $time
  45. * @param $type
  46. * @param $timeType
  47. * @return mixed
  48. */
  49. public function getTrendData($time, $type, $timeType)
  50. {
  51. return $this->getModel()->when($type != '', function ($query) use ($type) {
  52. $query->where('channel_type', $type);
  53. })->where(function ($query) use ($time) {
  54. if ($time[0] == $time[1]) {
  55. $query->whereDay('add_time', $time[0]);
  56. } else {
  57. $time[1] = date('Y/m/d', strtotime($time[1]) + 86400);
  58. $query->whereTime('add_time', 'between', $time);
  59. }
  60. })->field("FROM_UNIXTIME(add_time,'$timeType') as days,count(uid) as num")
  61. ->group('days')->select()->toArray();
  62. }
  63. /**合计某字段值
  64. * @param array $where
  65. * @param string $sumField
  66. * @return float
  67. */
  68. public function getWhereSumField(array $where, string $sumField)
  69. {
  70. return $this->search($where)
  71. ->when(isset($where['timeKey']), function ($query) use ($where) {
  72. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  73. })
  74. ->sum($sumField);
  75. }
  76. /**根据某字段分组查询
  77. * @param array $where
  78. * @param string $field
  79. * @param string $group
  80. * @return mixed
  81. */
  82. public function getGroupField(array $where, string $field, string $group)
  83. {
  84. return $this->search($where)
  85. ->when(isset($where['timeKey']), function ($query) use ($where, $field, $group) {
  86. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  87. if ($where['timeKey']['days'] == 1) {
  88. $timeUinx = "%H";
  89. } elseif ($where['timeKey']['days'] == 30) {
  90. $timeUinx = "%Y-%m-%d";
  91. } elseif ($where['timeKey']['days'] == 365) {
  92. $timeUinx = "%Y-%m";
  93. } elseif ($where['timeKey']['days'] > 1 && $where['timeKey']['days'] < 30) {
  94. $timeUinx = "%Y-%m-%d";
  95. } elseif ($where['timeKey']['days'] > 30 && $where['timeKey']['days'] < 365) {
  96. $timeUinx = "%Y-%m";
  97. }
  98. $query->field("sum($field) as number,FROM_UNIXTIME($group, '$timeUinx') as time");
  99. $query->group("FROM_UNIXTIME($group, '$timeUinx')");
  100. })
  101. ->order('add_time ASC')->select()->toArray();
  102. }
  103. /**根据条件获取单条信息
  104. * @param array $where
  105. * @return array|\think\Model|null
  106. * @throws \think\db\exception\DataNotFoundException
  107. * @throws \think\db\exception\DbException
  108. * @throws \think\db\exception\ModelNotFoundException
  109. */
  110. public function getOneByWhere(array $where)
  111. {
  112. return $this->getModel()->where($where)->find();
  113. }
  114. /**收银订单
  115. * @param array $where
  116. * @param int $page
  117. * @param int $limit
  118. * @param string $order
  119. * @return array
  120. * @throws \think\db\exception\DataNotFoundException
  121. * @throws \think\db\exception\DbException
  122. * @throws \think\db\exception\ModelNotFoundException
  123. */
  124. public function getScanOrderList(array $where = [], int $page = 0, int $limit = 0, string $order = '')
  125. {
  126. foreach ($where as $k => $v) {
  127. if ($v == "") unset($where[$k]);
  128. }
  129. return $this->search($where)
  130. ->order(($order ? $order . ' ,' : '') . 'id desc')
  131. ->page($page, $limit)->select()->toArray();
  132. }
  133. /**获取会员记录
  134. * @param array $where
  135. * @param int $page
  136. * @param int $limit
  137. * @param string $order
  138. * @return array
  139. * @throws \think\db\exception\DataNotFoundException
  140. * @throws \think\db\exception\DbException
  141. * @throws \think\db\exception\ModelNotFoundException
  142. */
  143. public function getMemberRecord(array $where = [], int $page = 0, int $limit = 0, string $order = '')
  144. {
  145. return $this->search($where)
  146. ->with(['user'])
  147. ->order(($order ? $order . ' ,' : '') . 'id desc')
  148. ->page($page, $limit)->select()->toArray();
  149. }
  150. public function search(array $where = [])
  151. {
  152. return parent::search($where)->when(isset($where['name']) && $where['name'], function ($query) use($where){
  153. $query->where('uid', 'in', function ($que) use($where){
  154. $nickname = trim($where['name']);
  155. $que->name('user')->where('nickname', 'like', $nickname.'%')->field(['uid'])->select();
  156. });
  157. });
  158. }
  159. public function count(array $where = []): int
  160. {
  161. return $this->search($where)->count();
  162. }
  163. }