StoreCartDao.php 6.2 KB

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