StoreCartDao.php 5.3 KB

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