StoreCartDao.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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\order;
  13. use app\dao\BaseDao;
  14. use app\model\order\StoreCart;
  15. /**
  16. *
  17. * Class StoreCartDao
  18. * @package app\dao\order
  19. */
  20. class StoreCartDao extends BaseDao
  21. {
  22. /**
  23. * 设置模型
  24. * @return string
  25. */
  26. protected function setModel(): string
  27. {
  28. return StoreCart::class;
  29. }
  30. /**
  31. * @param array $where
  32. * @param array $unique
  33. * @return array
  34. */
  35. public function getUserCartNums(array $where, array $unique)
  36. {
  37. return $this->search($where)->whereIn('product_attr_unique', $unique)->column('cart_num', 'product_attr_unique');
  38. }
  39. /**
  40. * 搜索
  41. * @param array $where
  42. * @param bool $search
  43. * @return \crmeb\basic\BaseModel|mixed|\think\Model
  44. * @throws \ReflectionException
  45. */
  46. public function search(array $where = [], bool $search = false)
  47. {
  48. return parent::search($where, $search)->when(isset($where['id']) && $where['id'], function ($query) use ($where) {
  49. $query->whereIn('id', $where['id']);
  50. })->when(isset($where['status']), function ($query) use ($where) {
  51. //兼容之前老用户 数据库默认值null
  52. if ($where['status'] == 1) {
  53. $query->where(function ($or) {
  54. $or->where('status', 1)->whereOr('status', 'exp', 'is null');
  55. });
  56. } else {
  57. $query->where('status', $where['status']);
  58. }
  59. });
  60. }
  61. /**
  62. * 根据商品id获取购物车数量
  63. * @param array $ids
  64. * @param int $uid
  65. * @return mixed
  66. */
  67. public function productIdByCartNum(array $ids, int $uid)
  68. {
  69. return $this->search(['product_id' => $ids, 'is_pay' => 0, 'is_del' => 0, 'is_new' => 0, 'uid' => $uid])->group('product_attr_unique')->column('cart_num,product_id', 'product_attr_unique');
  70. }
  71. /**
  72. * 获取购物车列表
  73. * @param array $where
  74. * @param int $page
  75. * @param int $limit
  76. * @return array
  77. * @throws \think\db\exception\DataNotFoundException
  78. * @throws \think\db\exception\DbException
  79. * @throws \think\db\exception\ModelNotFoundException
  80. */
  81. public function getCartList(array $where, int $page = 0, int $limit = 0, array $with = [])
  82. {
  83. return $this->search($where)->when($page && $limit, function ($query) use ($page, $limit) {
  84. $query->page($page, $limit);
  85. })->when(count($with), function ($query) use ($with) {
  86. $query->with($with);
  87. })->order('add_time DESC')->select()->toArray();
  88. }
  89. /**
  90. * 修改购物车数据未已删除
  91. * @param array $id
  92. * @param array $data
  93. * @return \crmeb\basic\BaseModel
  94. */
  95. public function updateDel(array $id)
  96. {
  97. return $this->getModel()->whereIn('id', $id)->update(['is_del' => 1]);
  98. }
  99. /**
  100. * 删除购物车
  101. * @param int $uid
  102. * @param array $ids
  103. * @return bool
  104. * @throws \Exception
  105. */
  106. public function removeUserCart(int $uid, array $ids)
  107. {
  108. return $this->getModel()->where('uid', $uid)->whereIn('id', $ids)->delete();
  109. }
  110. /**
  111. * 获取购物车数量
  112. * @param $uid
  113. * @param $type
  114. * @param $numType
  115. */
  116. public function getUserCartNum($uid, $type, $numType)
  117. {
  118. $model = $this->getModel()->where(['uid' => $uid, 'type' => $type, 'is_pay' => 0, 'is_new' => 0, 'is_del' => 0]);
  119. if ($numType) {
  120. return $model->count();
  121. } else {
  122. return $model->sum('cart_num');
  123. }
  124. }
  125. /**
  126. * 用户购物车统计数据
  127. * @param $uid
  128. * @param $type
  129. * @param string $field
  130. * @param array $with
  131. * @return array
  132. * @throws \think\db\exception\DataNotFoundException
  133. * @throws \think\db\exception\DbException
  134. * @throws \think\db\exception\ModelNotFoundException
  135. */
  136. public function getUserCartList($uid, string $field = '*', array $with = [])
  137. {
  138. return $this->getModel()->where(['uid' => $uid, 'is_pay' => 0, 'is_new' => 0, 'is_del' => 0])->when(count($with), function ($query) use ($with) {
  139. $query->with($with);
  140. })->order('add_time DESC')->field($field)->select()->toArray();
  141. }
  142. /**
  143. * 修改购物车数量
  144. * @param $cartId
  145. * @param $cartNum
  146. * @param $uid
  147. */
  148. public function changeUserCartNum(array $where, int $carNum)
  149. {
  150. return $this->getModel()->where($where)->update(['cart_num' => $carNum]);
  151. }
  152. /**
  153. * 修改购物车状态
  154. * @param $cartIds
  155. * @return \crmeb\basic\BaseModel
  156. */
  157. public function deleteCartStatus($cartIds)
  158. {
  159. return $this->getModel()->where('id', 'IN', $cartIds)->delete();
  160. }
  161. /**
  162. * 获取购物车最大的id
  163. * @return mixed
  164. */
  165. public function getCartIdMax()
  166. {
  167. return $this->getModel()->max('id');
  168. }
  169. /**
  170. * 求和
  171. * @param $where
  172. * @param $field
  173. * @return float
  174. */
  175. public function getSum($where, $field)
  176. {
  177. return $this->search($where)->sum($field);
  178. }
  179. /**
  180. * 购物车趋势
  181. * @param $time
  182. * @param $timeType
  183. * @param $str
  184. * @return mixed
  185. */
  186. public function getProductTrend($time, $timeType, $str)
  187. {
  188. return $this->getModel()->where(function ($query) use ($time) {
  189. if ($time[0] == $time[1]) {
  190. $query->whereDay('add_time', $time[0]);
  191. } else {
  192. $time[1] = date('Y/m/d', strtotime($time[1]) + 86400);
  193. $query->whereTime('add_time', 'between', $time);
  194. }
  195. })->field("FROM_UNIXTIME(add_time,'$timeType') as days,$str as num")->group('days')->select()->toArray();
  196. }
  197. }