You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StoreCouponUserCouponDao.php 3.2KB

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