StorePinkDao.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. 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\combination
  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', 'getUser'])->when($page && $limit, 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. * @param string $field
  115. * @param int $page
  116. * @param int $limit
  117. * @return array
  118. * @throws \think\db\exception\DataNotFoundException
  119. * @throws \think\db\exception\DbException
  120. * @throws \think\db\exception\ModelNotFoundException
  121. */
  122. public function successList(int $uid, string $field = '*', int $page = 0, int $limit = 0)
  123. {
  124. return $this->search(['status' => 2, 'is_refund' => 0])->field($field)
  125. ->where('uid', '>', 0)
  126. ->when($page && $limit, function ($query) use ($page, $limit) {
  127. $query->page($page, $limit);
  128. })->when(!$page && $limit, function ($query) use ($limit) {
  129. $query->limit($limit);
  130. })->order('id desc')->select()->toArray();
  131. }
  132. /**
  133. * 获取拼团完成的个数
  134. * @return float
  135. */
  136. public function getPinkOkSumTotalNum()
  137. {
  138. return $this->sum(['status' => 2, 'is_refund' => 0], 'total_num');
  139. }
  140. /**
  141. * 是否能继续拼团
  142. * @param int $id
  143. * @param int $uid
  144. * @return int
  145. */
  146. public function isPink(int $id, int $uid)
  147. {
  148. return $this->getModel()->where('k_id|id', $id)->where('uid', $uid)->where('is_refund', 0)->count();
  149. }
  150. /**
  151. * 获取一条拼团信息
  152. * @param int $id
  153. * @return array|\think\Model|null
  154. * @throws \think\db\exception\DataNotFoundException
  155. * @throws \think\db\exception\DbException
  156. * @throws \think\db\exception\ModelNotFoundException
  157. */
  158. public function getPinkUserOne(int $id)
  159. {
  160. return $this->search()->with('getProduct')->find($id);
  161. }
  162. /**
  163. * 获取拼团信息
  164. * @param array $where
  165. * @return array|\think\Model|null
  166. * @throws \think\db\exception\DataNotFoundException
  167. * @throws \think\db\exception\DbException
  168. * @throws \think\db\exception\ModelNotFoundException
  169. */
  170. public function getPinkUserList(array $where)
  171. {
  172. return $this->getModel()->where($where)->with('getProduct')->select()->toArray();
  173. }
  174. /**
  175. * 获取拼团结束的列表
  176. * @return \crmeb\basic\BaseModel
  177. */
  178. public function pinkListEnd()
  179. {
  180. return $this->getModel()->where('stop_time', '<=', time())
  181. ->where('status', 1)
  182. ->where('k_id', 0)
  183. ->where('is_refund', 0)
  184. ->field('id,people,k_id,order_id_key,uid,stop_time');
  185. }
  186. }