StoreCouponUserCouponDao.php 3.8 KB

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