StorePinkDao.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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)->when($where['k_id'] != 0, function ($query) use ($where) {
  52. $query->whereOr('id', $where['k_id']);
  53. })->with('getProduct')->when($page != 0, function ($query) use ($page, $limit) {
  54. $query->page($page, $limit);
  55. })->order('add_time desc')->select()->toArray();
  56. }
  57. /**
  58. * 获取正在拼团中的人,取最早写入的一条
  59. * @param array $where
  60. * @return array|\think\Model|null
  61. * @throws \think\db\exception\DataNotFoundException
  62. * @throws \think\db\exception\DbException
  63. * @throws \think\db\exception\ModelNotFoundException
  64. */
  65. public function getPinking(array $where)
  66. {
  67. return $this->search($where)->order('add_time asc')->find();
  68. }
  69. /**
  70. * 获取拼团列表
  71. * @param array $where
  72. * @return array
  73. * @throws \think\db\exception\DataNotFoundException
  74. * @throws \think\db\exception\DbException
  75. * @throws \think\db\exception\ModelNotFoundException
  76. */
  77. public function pinkList(array $where)
  78. {
  79. return $this->search($where)
  80. ->where('stop_time', '>', time())
  81. ->order('add_time desc')
  82. ->select()->toArray();
  83. }
  84. /**
  85. * 获取正在拼团的人数
  86. * @param int $kid
  87. * @return int
  88. */
  89. public function getPinkPeople(int $kid)
  90. {
  91. return $this->count(['k_id' => $kid, 'is_refund' => 0]) + 1;
  92. }
  93. /**
  94. * 获取正在拼团的人数
  95. * @param array $kids
  96. * @return int
  97. */
  98. public function getPinkPeopleCount(array $kids)
  99. {
  100. $count = $this->getModel()->whereIn('k_id', $kids)->where('is_refund', 0)->group('k_id')->column('COUNT(id) as count', 'k_id');
  101. $counts = [];
  102. foreach ($kids as &$item) {
  103. if (isset($count[$item])) {
  104. $counts[$item] = $count[$item] + 1;
  105. } else {
  106. $counts[$item] = 1;
  107. }
  108. }
  109. return $counts;
  110. }
  111. /**
  112. * 获取拼团成功的列表
  113. * @param int $uid
  114. * @return array
  115. * @throws \think\db\exception\DataNotFoundException
  116. * @throws \think\db\exception\DbException
  117. * @throws \think\db\exception\ModelNotFoundException
  118. */
  119. public function successList(int $uid)
  120. {
  121. return $this->search(['status' => 2, 'is_refund' => 0])
  122. ->where('uid', '<>', $uid)
  123. ->select()->toArray();
  124. }
  125. /**
  126. * 获取拼团完成的个数
  127. * @return float
  128. */
  129. public function getPinkOkSumTotalNum()
  130. {
  131. return $this->sum(['status' => 2, 'is_refund' => 0], 'total_num');
  132. }
  133. /**
  134. * 是否能继续拼团
  135. * @param int $id
  136. * @param int $uid
  137. * @return int
  138. */
  139. public function isPink(int $id, int $uid)
  140. {
  141. return $this->getModel()->where('k_id|id', $id)->where('uid', $uid)->where('is_refund', 0)->count();
  142. }
  143. /**
  144. * 获取一条拼团信息
  145. * @param int $id
  146. * @return array|\think\Model|null
  147. * @throws \think\db\exception\DataNotFoundException
  148. * @throws \think\db\exception\DbException
  149. * @throws \think\db\exception\ModelNotFoundException
  150. */
  151. public function getPinkUserOne(int $id)
  152. {
  153. return $this->search()->with('getProduct')->find($id);
  154. }
  155. /**
  156. * 获取拼团信息
  157. * @param array $where
  158. * @return array|\think\Model|null
  159. * @throws \think\db\exception\DataNotFoundException
  160. * @throws \think\db\exception\DbException
  161. * @throws \think\db\exception\ModelNotFoundException
  162. */
  163. public function getPinkUserList(array $where)
  164. {
  165. return $this->getModel()->where($where)->with('getProduct')->select()->toArray();
  166. }
  167. /**
  168. * 获取拼团结束的列表
  169. * @return array
  170. * @throws \think\db\exception\DataNotFoundException
  171. * @throws \think\db\exception\DbException
  172. * @throws \think\db\exception\ModelNotFoundException
  173. */
  174. public function pinkListEnd()
  175. {
  176. return $this->getModel()->where('stop_time', '<=', time())
  177. ->where('status', 1)
  178. ->where('k_id', 0)
  179. ->where('is_refund', 0)
  180. ->field('id,people,k_id,uid,stop_time')->select()->toArray();
  181. }
  182. }