StoreCouponUserCouponDao.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. namespace app\dao\activity\coupon;
  12. use app\dao\BaseDao;
  13. use app\model\activity\coupon\StoreCoupon;
  14. use app\model\activity\coupon\StoreCouponUser;
  15. /**
  16. * Class StoreCouponUserCouponDao
  17. * @package app\dao\coupon
  18. */
  19. class StoreCouponUserCouponDao extends BaseDao
  20. {
  21. /**
  22. * 主表别名
  23. * @var string
  24. */
  25. protected $alias = 'a';
  26. /**
  27. * 连表别名
  28. * @var string
  29. */
  30. protected $joinAlis = 'b';
  31. /**
  32. * 主表模型
  33. * @return string
  34. */
  35. public function setModel(): string
  36. {
  37. return StoreCouponUser::class;
  38. }
  39. /**
  40. * 连表表明
  41. * @return string
  42. */
  43. public function setJoinModel(): string
  44. {
  45. return StoreCoupon::class;
  46. }
  47. /**
  48. * 设置模型
  49. * @return \crmeb\basic\BaseModel
  50. */
  51. public function getModel()
  52. {
  53. /** @var StoreCoupon $joinModel */
  54. $joinModel = app()->make($this->setJoinModel());
  55. $name = $joinModel->getName();
  56. return parent::getModel()->alias($this->alias)->join($name . ' ' . $this->joinAlis, $this->joinAlis . '.id=' . $this->alias . '.cid');
  57. }
  58. /**
  59. * 根据下单金额获取用户能使用的优惠卷
  60. * @param int $uid
  61. * @param string $truePrice
  62. * @param int $productId
  63. * @return mixed
  64. * @throws \think\db\exception\DataNotFoundException
  65. * @throws \think\db\exception\DbException
  66. * @throws \think\db\exception\ModelNotFoundException
  67. */
  68. public function getUidCouponList(int $uid, string $truePrice, int $productId)
  69. {
  70. return $this->getModel()
  71. ->where($this->alias . '.uid', $uid)
  72. ->where($this->alias . '.is_fail', 0)
  73. ->where($this->alias . '.status', 0)
  74. ->where($this->alias . '.use_min_price', '<=', $truePrice)
  75. ->whereFindinSet($this->joinAlis . '.product_id', $productId)
  76. ->where($this->joinAlis . '.type', 2)
  77. ->field($this->alias . '.*,' . $this->joinAlis . '.type')
  78. ->order($this->alias . '.coupon_price', 'DESC')
  79. ->select()
  80. ->hidden(['status', 'is_fail'])
  81. ->toArray();
  82. }
  83. /**
  84. * 获取购买金额最小使用范围内的优惠卷
  85. * @param $uid
  86. * @param $price
  87. * @param $value
  88. * @return mixed
  89. * @throws \think\db\exception\DataNotFoundException
  90. * @throws \think\db\exception\DbException
  91. * @throws \think\db\exception\ModelNotFoundException
  92. */
  93. public function getUidCouponMinList($uid, $price, $value = '', int $type = 1)
  94. {
  95. return $this->getModel()->where($this->alias . '.uid', $uid)
  96. ->where($this->alias . '.is_fail', 0)
  97. ->where($this->alias . '.status', 0)
  98. ->where($this->alias . '.use_min_price', '<=', $price)
  99. ->when($value, function ($query) use ($value) {
  100. $query->whereFindinSet($this->joinAlis . '.category_id', $value);
  101. })
  102. ->where($this->joinAlis . '.type', $type)
  103. ->field($this->alias . '.*,' . $this->joinAlis . '.type')
  104. ->order($this->alias . '.coupon_price', 'DESC')
  105. ->select()
  106. ->hidden(['status', 'is_fail'])
  107. ->toArray();
  108. }
  109. }