OtherOrderDao.php 7.2 KB

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