StoreCartDao.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. namespace app\common\dao\store\order;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\BaseModel;
  14. use app\common\model\store\order\StoreCart;
  15. use app\common\model\user\UserAddress;
  16. use think\Collection;
  17. use think\db\exception\DataNotFoundException;
  18. use think\db\exception\DbException;
  19. use think\db\exception\ModelNotFoundException;
  20. use think\model\Relation;
  21. class StoreCartDao extends BaseDao
  22. {
  23. protected function getModel(): string
  24. {
  25. return StoreCart::class;
  26. }
  27. public function test()
  28. {
  29. return StoreCart::getDB()->with(['product' => function (Relation $query) {
  30. $query->where('store_name', '儿童节礼物');
  31. }])->select();
  32. }
  33. /**
  34. * @param array $ids
  35. * @param $uid
  36. * @param int|null $merId
  37. * @return array
  38. * @author xaboy
  39. * @day 2020/6/5
  40. */
  41. public function validIntersection(array $ids, $uid, int $merId = null): array
  42. {
  43. return StoreCart::getDB()->whereIn('cart_id', $ids)
  44. ->when($merId, function ($query, $merId) {
  45. $query->where('mer_id', $merId);
  46. })
  47. ->where('is_del', 0)->where('is_fail', 0)->where('is_pay', 0)->where('uid', $uid)->column('cart_id');
  48. }
  49. /**
  50. * @Author:Qinii
  51. * @Date: 2020/6/1
  52. * @param int $uid
  53. * @return mixed
  54. */
  55. public function getAll(int $uid)
  56. {
  57. $query = ($this->getModel())::where(['uid' => $uid, 'is_del' => 0, 'is_new' => 0, 'is_pay' => 0])
  58. ->with([
  59. 'product' => function ($query) {
  60. $query->field('product_id,image,store_name,is_show,status,is_del,unit_name,price,mer_status,is_used,product_type,once_max_count,once_min_count,pay_limit,mer_svip_status,svip_price_type');
  61. },
  62. 'productAttr' => function ($query) {
  63. $query->field('product_id,stock,price,unique,sku,image,svip_price');
  64. },
  65. 'merchant' => function ($query) {
  66. $query->field('mer_id,mer_name,mer_state,mer_avatar,is_trader,type_id')->with(['type_name']);
  67. }
  68. ])->select();
  69. return $query;
  70. }
  71. public function cartIbByData(array $ids, int $uid, ?UserAddress $address)
  72. {
  73. return StoreCart::getDb()->where('uid', $uid)->with([
  74. 'product' => function (Relation $query) use ($address) {
  75. $query->field('product_id,cate_id,image,store_name,is_show,status,is_del,unit_name,price,mer_status,temp_id,give_coupon_ids,is_gift_bag,is_used,product_type,old_product_id,integral_rate,delivery_way,delivery_free,type,extend,pay_limit,once_max_count,once_min_count,mer_svip_status,svip_price_type');
  76. if ($address) {
  77. $cityIds = array_filter([$address->province_id, $address->city_id, $address->district_id, $address->street_id]);
  78. $query->with(['temp' => ['region' => function (Relation $query) use ($cityIds) {
  79. $query->where(function ($query) use ($cityIds) {
  80. foreach ($cityIds as $v) {
  81. $query->whereOr('city_id', 'like', "%/{$v}/%");
  82. }
  83. $query->whereOr('city_id', '0');
  84. })->order('shipping_template_region_id DESC')->withLimit(1);
  85. }, 'undelives' => function ($query) use ($cityIds) {
  86. foreach ($cityIds as $v) {
  87. $query->whereOr('city_id', 'like', "%/{$v}/%");
  88. }
  89. }, 'free' => function (Relation $query) use ($cityIds) {
  90. foreach ($cityIds as $v) {
  91. $query->whereOr('city_id', 'like', "%/{$v}/%");
  92. }
  93. $query->order('shipping_template_free_id DESC')->withLimit(1);
  94. }]]);
  95. }
  96. },
  97. 'productAttr' => function (Relation $query) {
  98. $query->field('image,extension_one,extension_two,product_id,stock,price,unique,sku,volume,weight,ot_price,cost,svip_price')
  99. ->append(['bc_extension_one', 'bc_extension_two']);
  100. },
  101. 'merchant' => function (Relation $query) use ($uid) {
  102. $query->field('mer_id,mer_name,mer_state,mer_avatar,delivery_way,commission_rate,category_id')->with(['coupon' => function ($query) use ($uid) {
  103. $query->where('uid', $uid);
  104. },
  105. 'config' => function ($query) {
  106. $query->whereIn('config_key', ['mer_integral_status', 'mer_integral_rate', 'mer_store_stock', 'mer_take_status', 'mer_take_name', 'mer_take_phone', 'mer_take_address', 'mer_take_location', 'mer_take_day', 'mer_take_time']);
  107. },
  108. 'merchantCategory'
  109. ]);
  110. }])->whereIn('cart_id', $ids)->order('product_type DESC,cart_id DESC')->select();
  111. }
  112. /**
  113. * @param array $cartIds
  114. * @param int $uid
  115. * @author Qinii
  116. */
  117. public function batchDelete(array $cartIds, int $uid)
  118. {
  119. return ($this->getModel()::getDB())->where('uid', $uid)->whereIn('cart_id', $cartIds)->delete();
  120. }
  121. /**
  122. * @param int $uid
  123. * @return mixed
  124. * @author Qinii
  125. */
  126. public function getCartCount(int $uid)
  127. {
  128. $data = ($this->getModel()::getDB())->where(['uid' => $uid, 'is_del' => 0, 'is_new' => 0, 'is_pay' => 0])->field('SUM(cart_num) as count')->select();
  129. $data[0]['count'] = $data[0]['count'] ? $data[0]['count'] : 0;
  130. return $data;
  131. }
  132. /**
  133. * @param $source
  134. * @param array|null $ids
  135. * @author xaboy
  136. * @day 2020/8/31
  137. */
  138. public function getSourcePayInfo($source, ?array $ids = null)
  139. {
  140. return StoreCart::getDB()->alias('A')->where('A.source', $source)->where('A.is_pay', 1)->when($ids, function ($query, $ids) {
  141. $query->whereIn('A.source_id', $ids);
  142. })->leftJoin('StoreOrderProduct B', 'A.cart_id = B.cart_id')
  143. ->field('sum(B.product_num) as pay_num,sum(B.product_price) as pay_price,A.source_id')->group('A.source_id')->select();
  144. }
  145. }