OtherOrderDao.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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\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. $timeUinx = "%H";
  88. if ($where['timeKey']['days'] == 1) {
  89. $timeUinx = "%H";
  90. } elseif ($where['timeKey']['days'] == 30) {
  91. $timeUinx = "%Y-%m-%d";
  92. } elseif ($where['timeKey']['days'] == 365) {
  93. $timeUinx = "%Y-%m";
  94. } elseif ($where['timeKey']['days'] > 1 && $where['timeKey']['days'] < 30) {
  95. $timeUinx = "%Y-%m-%d";
  96. } elseif ($where['timeKey']['days'] > 30 && $where['timeKey']['days'] < 365) {
  97. $timeUinx = "%Y-%m";
  98. }
  99. $query->field("sum($field) as number,FROM_UNIXTIME($group, '$timeUinx') as time");
  100. $query->group("FROM_UNIXTIME($group, '$timeUinx')");
  101. })
  102. ->order('add_time ASC')->select()->toArray();
  103. }
  104. /**根据条件获取单条信息
  105. * @param array $where
  106. * @return array|\think\Model|null
  107. * @throws \think\db\exception\DataNotFoundException
  108. * @throws \think\db\exception\DbException
  109. * @throws \think\db\exception\ModelNotFoundException
  110. */
  111. public function getOneByWhere(array $where)
  112. {
  113. return $this->getModel()->where($where)->find();
  114. }
  115. /**收银订单
  116. * @param array $where
  117. * @param int $page
  118. * @param int $limit
  119. * @param string $order
  120. * @return array
  121. * @throws \think\db\exception\DataNotFoundException
  122. * @throws \think\db\exception\DbException
  123. * @throws \think\db\exception\ModelNotFoundException
  124. */
  125. public function getScanOrderList(array $where = [], int $page = 0, int $limit = 0, string $order = '')
  126. {
  127. foreach ($where as $k => $v) {
  128. if ($v == "") unset($where[$k]);
  129. }
  130. return $this->search($where)
  131. ->order(($order ? $order . ' ,' : '') . 'id desc')
  132. ->page($page, $limit)->select()->toArray();
  133. }
  134. /**获取会员记录
  135. * @param array $where
  136. * @param int $page
  137. * @param int $limit
  138. * @param string $order
  139. * @return array
  140. * @throws \think\db\exception\DataNotFoundException
  141. * @throws \think\db\exception\DbException
  142. * @throws \think\db\exception\ModelNotFoundException
  143. */
  144. public function getMemberRecord(array $where = [], int $page = 0, int $limit = 0, string $order = '')
  145. {
  146. return $this->search($where)
  147. ->with(['user'])
  148. ->order(($order ? $order . ' ,' : '') . 'id desc')
  149. ->page($page, $limit)->select()->toArray();
  150. }
  151. /**
  152. * 其他订单搜索
  153. * @param array $where
  154. * @param bool $search
  155. * @return \crmeb\basic\BaseModel
  156. * @throws \ReflectionException
  157. * @author 吴汐
  158. * @email 442384644@qq.com
  159. * @date 2023/03/20
  160. */
  161. public function search(array $where = [], bool $search = false)
  162. {
  163. return parent::search($where, $search)->when(isset($where['name']) && $where['name'], function ($query) use ($where) {
  164. $query->where('uid', 'in', function ($que) use ($where) {
  165. $nickname = trim($where['name']);
  166. $que->name('user')->where('nickname', 'like', $nickname . '%')->field(['uid'])->select();
  167. });
  168. });
  169. }
  170. /**
  171. * @param array $where
  172. * @param bool $search
  173. * @return int
  174. * @throws \ReflectionException
  175. * @author 吴汐
  176. * @email 442384644@qq.com
  177. * @date 2023/04/11
  178. */
  179. public function count(array $where = [], bool $search = true)
  180. {
  181. return $this->search($where, $search)->count();
  182. }
  183. }