StorePinkDao.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. declare (strict_types=1);
  12. namespace app\dao\activity\combination;
  13. use app\dao\BaseDao;
  14. use app\model\activity\combination\StorePink;
  15. /**
  16. *
  17. * Class StorePinkDao
  18. * @package app\dao\activity
  19. */
  20. class StorePinkDao extends BaseDao
  21. {
  22. /**
  23. * 设置模型
  24. * @return string
  25. */
  26. protected function setModel(): string
  27. {
  28. return StorePink::class;
  29. }
  30. /**
  31. * 获取拼团数量集合
  32. * @param array $where
  33. * @return array
  34. */
  35. public function getPinkCount(array $where = [])
  36. {
  37. return $this->getModel()->where($where)->group('cid')->column('count(*)', 'cid');
  38. }
  39. /**
  40. * 获取列表
  41. * @param array $where
  42. * @param int $page
  43. * @param int $limit
  44. * @return array
  45. * @throws \think\db\exception\DataNotFoundException
  46. * @throws \think\db\exception\DbException
  47. * @throws \think\db\exception\ModelNotFoundException
  48. */
  49. public function getList(array $where, int $page = 0, int $limit = 0)
  50. {
  51. return $this->search($where, false)->when($where['k_id'] != 0, function ($query) use ($where) {
  52. $query->whereOr('id', $where['k_id']);
  53. })->when(isset($where['keyword']) && $where['keyword'] != '', function ($query) use ($where) {
  54. $query->where('uid|nickname', 'like', '%' . $where['keyword'] . '%');
  55. })->with('getProduct')->when($page != 0, function ($query) use ($page, $limit) {
  56. $query->page($page, $limit);
  57. })->order('add_time desc')->select()->toArray();
  58. }
  59. /**
  60. * 获取正在拼团中的人,取最早写入的一条
  61. * @param array $where
  62. * @return array|\think\Model|null
  63. * @throws \think\db\exception\DataNotFoundException
  64. * @throws \think\db\exception\DbException
  65. * @throws \think\db\exception\ModelNotFoundException
  66. */
  67. public function getPinking(array $where)
  68. {
  69. return $this->search($where)->order('add_time asc')->find();
  70. }
  71. /**
  72. * 获取拼团列表
  73. * @param array $where
  74. * @return array
  75. * @throws \think\db\exception\DataNotFoundException
  76. * @throws \think\db\exception\DbException
  77. * @throws \think\db\exception\ModelNotFoundException
  78. */
  79. public function pinkList(array $where)
  80. {
  81. return $this->search($where)
  82. ->where('stop_time', '>', time())
  83. ->order('add_time desc')
  84. ->select()->toArray();
  85. }
  86. /**
  87. * 获取正在拼团的人数
  88. * @param int $kid
  89. * @return int
  90. */
  91. public function getPinkPeople(int $kid)
  92. {
  93. return $this->count(['k_id' => $kid, 'is_refund' => 0]) + 1;
  94. }
  95. /**
  96. * 获取正在拼团的人数
  97. * @param array $kids
  98. * @return int
  99. */
  100. public function getPinkPeopleCount(array $kids)
  101. {
  102. $count = $this->getModel()->whereIn('k_id', $kids)->where('is_refund', 0)->group('k_id')->column('COUNT(id) as count', 'k_id');
  103. $counts = [];
  104. foreach ($kids as &$item) {
  105. if (isset($count[$item])) {
  106. $counts[$item] = $count[$item] + 1;
  107. } else {
  108. $counts[$item] = 1;
  109. }
  110. }
  111. return $counts;
  112. }
  113. /**
  114. * 获取拼团成功的列表
  115. * @param int $uid
  116. * @return array
  117. * @throws \think\db\exception\DataNotFoundException
  118. * @throws \think\db\exception\DbException
  119. * @throws \think\db\exception\ModelNotFoundException
  120. */
  121. public function successList(int $uid)
  122. {
  123. return $this->search(['status' => 2, 'is_refund' => 0])
  124. // ->where('uid', '<>', $uid)
  125. ->select()->toArray();
  126. }
  127. /**
  128. * 获取拼团完成的个数
  129. * @return float
  130. * @throws \ReflectionException
  131. */
  132. public function getPinkOkSumTotalNum()
  133. {
  134. return $this->sum(['status' => 2, 'is_refund' => 0], 'total_num');
  135. }
  136. /**
  137. * 是否能继续拼团
  138. * @param int $id
  139. * @param int $uid
  140. * @return int
  141. */
  142. public function isPink(int $id, int $uid)
  143. {
  144. return $this->getModel()->where('k_id|id', $id)->where('uid', $uid)->where('is_refund', 0)->count();
  145. }
  146. /**
  147. * 获取一条拼团信息
  148. * @param int $id
  149. * @return array|\think\Model|null
  150. * @throws \think\db\exception\DataNotFoundException
  151. * @throws \think\db\exception\DbException
  152. * @throws \think\db\exception\ModelNotFoundException
  153. */
  154. public function getPinkUserOne(int $id)
  155. {
  156. return $this->search()->with('getProduct')->find($id);
  157. }
  158. /**
  159. * 获取拼团信息
  160. * @param array $where
  161. * @return array|\think\Model|null
  162. * @throws \think\db\exception\DataNotFoundException
  163. * @throws \think\db\exception\DbException
  164. * @throws \think\db\exception\ModelNotFoundException
  165. */
  166. public function getPinkUserList(array $where)
  167. {
  168. return $this->getModel()->where($where)->with('getProduct')->select()->toArray();
  169. }
  170. /**
  171. * 获取拼团结束的列表
  172. * @return array
  173. * @throws \think\db\exception\DataNotFoundException
  174. * @throws \think\db\exception\DbException
  175. * @throws \think\db\exception\ModelNotFoundException
  176. */
  177. public function pinkListEnd()
  178. {
  179. return $this->getModel()->where('stop_time', '<=', time())
  180. ->where('status', 1)
  181. ->where('k_id', 0)
  182. ->where('is_refund', 0)
  183. ->field('id,people,k_id,uid,stop_time')->select()->toArray();
  184. }
  185. }