StoreCartDao.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. 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 think\Collection;
  16. use think\db\exception\DataNotFoundException;
  17. use think\db\exception\DbException;
  18. use think\db\exception\ModelNotFoundException;
  19. use think\model\Relation;
  20. class StoreCartDao extends BaseDao
  21. {
  22. protected function getModel(): string
  23. {
  24. return StoreCart::class;
  25. }
  26. public function test()
  27. {
  28. return StoreCart::getDB()->with(['product' => function (Relation $query) {
  29. $query->where('store_name', '儿童节礼物');
  30. }])->select();
  31. }
  32. /**
  33. * @param array $ids
  34. * @param $uid
  35. * @param int|null $merId
  36. * @return array
  37. * @author xaboy
  38. * @day 2020/6/5
  39. */
  40. public function validIntersection(array $ids, $uid, int $merId = null): array
  41. {
  42. return StoreCart::getDB()->whereIn('cart_id', $ids)
  43. ->when($merId, function ($query, $merId) {
  44. $query->where('mer_id', $merId);
  45. })
  46. ->where('is_del', 0)->where('is_fail', 0)->where('is_pay', 0)->where('uid', $uid)->column('cart_id');
  47. }
  48. /**
  49. * @Author:Qinii
  50. * @Date: 2020/6/1
  51. * @param int $uid
  52. * @return mixed
  53. */
  54. public function getAll(int $uid)
  55. {
  56. $query = ($this->getModel())::where(['uid' => $uid, 'is_del' => 0, 'is_new' => 0, 'is_pay' => 0])
  57. ->with([
  58. 'product' => function ($query) {
  59. $query->field('product_id,image,store_name,is_show,status,is_del,unit_name,price,mer_status,is_used,product_type');
  60. },
  61. 'productAttr' => function ($query) {
  62. $query->field('product_id,stock,price,unique,sku,image');
  63. },
  64. 'merchant' => function ($query) {
  65. $query->field('mer_id,mer_name,mer_state,mer_avatar,is_trader');
  66. }
  67. ])->select();
  68. return $query;
  69. }
  70. public function cartIbByData(array $ids, int $uid, ?int $cityId, bool $confirm = true)
  71. {
  72. return StoreCart::getDb()->where('uid', $uid)->with(['product' => function (Relation $query) use ($cityId) {
  73. $query->field('product_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');
  74. if ($cityId) {
  75. $query->with(['temp' => ['region' => function (Relation $query) use ($cityId) {
  76. $query->where(function ($query) use ($cityId) {
  77. $query->whereLike('city_id', "%/{$cityId}/%")->whereOr('city_id', '0');
  78. })->order('shipping_template_region_id DESC')->withLimit(1);
  79. }, 'undelives' => function ($query) use ($cityId) {
  80. $query->whereLike('city_id', "%/{$cityId}/%");
  81. }, 'free' => function (Relation $query) use ($cityId) {
  82. $query->whereLike('city_id', "%/{$cityId}/%")->order('shipping_template_free_id DESC')->withLimit(1);
  83. }]]);
  84. }
  85. }, 'productAttr' => function (Relation $query) use ($confirm) {
  86. $query->field('image,extension_one,extension_two,product_id,stock,price,unique,sku,volume,weight,ot_price ' . ($confirm ? '' : ',cost'))
  87. ->append(['bc_extension_one', 'bc_extension_two']);
  88. }, 'merchant' => function (Relation $query) use ($uid) {
  89. $query->field('mer_id,mer_name,mer_state,mer_avatar')->with(['coupon' => function ($query) use ($uid) {
  90. $query->where('uid', $uid);
  91. }]);
  92. }])->whereIn('cart_id', $ids)->order('product_type DESC,cart_id DESC')->select();
  93. }
  94. /**
  95. * @param array $cartIds
  96. * @param int $uid
  97. * @author Qinii
  98. */
  99. public function batchDelete(array $cartIds, int $uid)
  100. {
  101. return ($this->getModel()::getDB())->where('uid', $uid)->whereIn('cart_id', $cartIds)->delete();
  102. }
  103. /**
  104. * @param int $uid
  105. * @return mixed
  106. * @author Qinii
  107. */
  108. public function getCartCount(int $uid)
  109. {
  110. $data = ($this->getModel()::getDB())->where(['uid' => $uid, 'is_del' => 0, 'is_new' => 0, 'is_pay' => 0])->field('SUM(cart_num) as count')->select();
  111. $data[0]['count'] = $data[0]['count'] ? $data[0]['count'] : 0;
  112. return $data;
  113. }
  114. /**
  115. * @param $source
  116. * @param array|null $ids
  117. * @author xaboy
  118. * @day 2020/8/31
  119. */
  120. public function getSourcePayInfo($source, ?array $ids = null)
  121. {
  122. return StoreCart::getDB()->alias('A')->where('A.source', $source)->where('A.is_pay', 1)->when($ids, function ($query, $ids) {
  123. $query->whereIn('A.source_id', $ids);
  124. })->leftJoin('StoreOrderProduct B', 'A.cart_id = B.cart_id')
  125. ->field('sum(B.product_num) as pay_num,sum(B.product_price) as pay_price,A.source_id')->group('A.source_id')->select();
  126. }
  127. }