StoreCartDao.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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\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. * 搜索
  32. * @param array $where
  33. * @return \crmeb\basic\BaseModel|mixed|\think\Model
  34. */
  35. public function search(array $where = [])
  36. {
  37. return parent::search($where)->when(isset($where['id']) && $where['id'], function ($query) use ($where) {
  38. $query->whereIn('id', $where['id']);
  39. })->when(isset($where['status']), function ($query) use ($where) {
  40. $query->where('status', $where['status']);
  41. });
  42. }
  43. /**
  44. * @param array $where
  45. * @param array $unique
  46. * @return array
  47. */
  48. public function getUserCartNums(array $where, array $unique)
  49. {
  50. return $this->search($where)->whereIn('product_attr_unique', $unique)->column('cart_num', 'product_attr_unique');
  51. }
  52. /**
  53. * 获取挂单数据
  54. * @param string $search
  55. * @param int $storeId
  56. * @param int $staffId
  57. * @param int $page
  58. * @param int $limit
  59. * @return mixed
  60. */
  61. public function getHangOrder(int $storeId, int $staffId = 0, string $search = '', int $page = 0, int $limit = 0)
  62. {
  63. return $this->getModel()->where([
  64. 'status' => 1,
  65. 'is_del' => 0,
  66. 'is_pay' => 0,
  67. 'is_new' => 0,
  68. 'store_id' => $storeId
  69. ])->when($staffId, function ($query) use ($staffId) {
  70. $query->where('staff_id', $staffId);
  71. })->when($search !== '', function ($query) use ($search) {
  72. $query->whereIn('uid', function ($query) use ($search) {
  73. $query->name('user')->where('uid|phone|nickname', 'like', "%" . $search . "%")->field('uid');
  74. });
  75. })->when($page && $limit, function ($query) use ($page, $limit) {
  76. $query->page($page, $limit);
  77. })->field(['tourist_uid', 'add_time', 'GROUP_CONCAT(id) as cart_id', 'store_id', 'staff_id', 'uid'])->with('user')
  78. ->group('uid,tourist_uid');
  79. }
  80. /**
  81. * 根据商品id获取购物车数量
  82. * @param array $ids
  83. * @param int $uid
  84. * @param int $staff_id
  85. * @return mixed
  86. */
  87. public function productIdByCartNum(array $ids, int $uid, int $staff_id = 0, int $touristUid = 0, int $storeId = 0)
  88. {
  89. return $this->search([
  90. 'product_id' => $ids,
  91. 'is_pay' => 0,
  92. 'is_del' => 0,
  93. 'is_new' => 0,
  94. 'tourist_uid' => $touristUid,
  95. 'uid' => $uid,
  96. 'store_id' => $storeId])->when($staff_id, function ($query) use ($staff_id) {
  97. $query->where('staff_id', $staff_id);
  98. })->group('product_attr_unique')->column('cart_num,product_id', 'product_attr_unique');
  99. }
  100. /**
  101. * 获取购物车列表
  102. * @param array $where
  103. * @param int $page
  104. * @param int $limit
  105. * @return array
  106. * @throws \think\db\exception\DataNotFoundException
  107. * @throws \think\db\exception\DbException
  108. * @throws \think\db\exception\ModelNotFoundException
  109. */
  110. public function getCartList(array $where, int $page = 0, int $limit = 0, array $with = [])
  111. {
  112. return $this->search($where)->when($page && $limit, function ($query) use ($page, $limit) {
  113. $query->page($page, $limit);
  114. })->when(count($with), function ($query) use ($with, $where) {
  115. $query->with($with);
  116. })->when(isset($where['is_cashier']), function ($query) use ($where) {
  117. $query->where('is_cashier', $where['is_cashier']);
  118. })->order('add_time DESC')->select()->toArray();
  119. }
  120. /**
  121. * 修改购物车数据未已删除
  122. * @param array $id
  123. * @param array $data
  124. * @return \crmeb\basic\BaseModel
  125. */
  126. public function updateDel(array $id)
  127. {
  128. return $this->getModel()->whereIn('id', $id)->update(['is_del' => 1]);
  129. }
  130. /**
  131. * 删除购物车
  132. * @param int $uid
  133. * @param array $ids
  134. * @return bool
  135. * @throws \Exception
  136. */
  137. public function removeUserCart(int $uid, array $ids)
  138. {
  139. return $this->getModel()->where('uid', $uid)->whereIn('id', $ids)->delete();
  140. }
  141. /**
  142. * 获取购物车数量
  143. * @param $uid
  144. * @param $type
  145. * @param $numType
  146. */
  147. public function getUserCartNum($uid, $type, $numType)
  148. {
  149. $model = $this->getModel()->where(['uid' => $uid, 'type' => $type, 'is_pay' => 0, 'is_new' => 0, 'is_del' => 0]);
  150. if ($numType) {
  151. return $model->count();
  152. } else {
  153. return $model->sum('cart_num');
  154. }
  155. }
  156. /**
  157. * 用户购物车统计数据
  158. * @param int $uid
  159. * @param string $field
  160. * @param array $with
  161. * @return array
  162. * @throws \think\db\exception\DataNotFoundException
  163. * @throws \think\db\exception\DbException
  164. * @throws \think\db\exception\ModelNotFoundException
  165. */
  166. public function getUserCartList(array $where, string $field = '*', array $with = [])
  167. {
  168. return $this->getModel()->where(array_merge($where, ['is_pay' => 0, 'is_new' => 0, 'is_del' => 0, 'type' => 0]))
  169. ->when(count($with), function ($query) use ($with) {
  170. $query->with($with);
  171. })->order('add_time DESC')->field($field)->select()->toArray();
  172. }
  173. /**
  174. * 修改购物车数量
  175. * @param $cartId
  176. * @param $cartNum
  177. * @param $uid
  178. */
  179. public function changeUserCartNum(array $where, int $carNum)
  180. {
  181. return $this->getModel()->update(['cart_num' => $carNum], $where);
  182. }
  183. /**
  184. * 修改购物车状态
  185. * @param array $product_ids
  186. * @param int $status
  187. * @return \crmeb\basic\BaseModel
  188. */
  189. public function changeStatus(array $product_ids, int $status = 0)
  190. {
  191. return $this->getModel()->where('product_id', 'IN', $product_ids)->update(['status' => $status]);
  192. }
  193. /**
  194. * 删除购物车
  195. * @param $cartIds
  196. * @return bool
  197. */
  198. public function deleteCartStatus($cartIds)
  199. {
  200. return $this->getModel()->where('id', 'IN', $cartIds)->delete();
  201. }
  202. /**
  203. * 获取购物车最大的id
  204. * @return mixed
  205. */
  206. public function getCartIdMax()
  207. {
  208. return $this->getModel()->max('id');
  209. }
  210. /**
  211. * 求和
  212. * @param $where
  213. * @param $field
  214. * @return float
  215. */
  216. public function getSum($where, $field)
  217. {
  218. return $this->search($where)->sum($field);
  219. }
  220. /**
  221. * 购物车趋势
  222. * @param $time
  223. * @param $timeType
  224. * @param $str
  225. * @return mixed
  226. */
  227. public function getProductTrend($time, $timeType, $str)
  228. {
  229. return $this->getModel()->where(function ($query) use ($time) {
  230. if ($time[0] == $time[1]) {
  231. $query->whereDay('add_time', $time[0]);
  232. } else {
  233. $time[1] = date('Y/m/d', strtotime($time[1]) + 86400);
  234. $query->whereTime('add_time', 'between', $time);
  235. }
  236. })->field("FROM_UNIXTIME(add_time,'$timeType') as days,$str as num")->group('days')->select()->toArray();
  237. }
  238. }