StoreCouponUser.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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\model\activity\coupon;
  12. use app\model\user\User;
  13. use crmeb\basic\BaseModel;
  14. use crmeb\traits\ModelTrait;
  15. use think\Model;
  16. /**
  17. * TODO 优惠券发放Model
  18. * Class StoreCouponUser
  19. * @package app\model\coupon
  20. */
  21. class StoreCouponUser extends BaseModel
  22. {
  23. use ModelTrait;
  24. /**
  25. * 数据表主键
  26. * @var string
  27. */
  28. protected $pk = 'id';
  29. /**
  30. * 模型名称
  31. * @var string
  32. */
  33. protected $name = 'store_coupon_user';
  34. /**
  35. * 获取类型
  36. * @var string[]
  37. */
  38. protected $gainType = ['send' => '后台发放', 'get' => '手动领取'];
  39. /**
  40. * 类型获取器
  41. * @param $value
  42. * @return string
  43. */
  44. public function getTypeAttr($value)
  45. {
  46. return $this->gainType[$value];
  47. }
  48. /**
  49. * 使用状态
  50. * @var string[]
  51. */
  52. protected $statusType = [0 => '未使用', 1 => '已使用', 2 => '已过期'];
  53. /**
  54. * 状态获取器
  55. * @param $value
  56. * @return string
  57. */
  58. public function getStatusAttr($value)
  59. {
  60. return $this->statusType[$value];
  61. }
  62. /**
  63. * @return \think\model\relation\HasOne
  64. */
  65. public function issue()
  66. {
  67. return $this->hasOne(StoreCouponIssue::class, 'id', 'cid')->field(['id', 'end_use_time', 'start_use_time', 'type', 'coupon_time', 'product_id', 'category_id', 'receive_type'])->bind([
  68. 'applicable_type' => 'type',
  69. 'coupon_time' => 'coupon_time',
  70. 'product_id',
  71. 'category_id',
  72. 'receive_type',
  73. 'start_use_time',
  74. 'end_use_time'
  75. ]);
  76. }
  77. /**
  78. * 获取领取人名称头像
  79. * @return \think\model\relation\HasOne
  80. */
  81. public function userInfo()
  82. {
  83. return $this->hasOne(User::class, 'uid', 'uid')->field('uid,nickname,avatar')->bind(['nickname', 'avatar']);
  84. }
  85. /**
  86. * 优惠券ID搜索器
  87. * @param Model $query
  88. * @param $value
  89. * @param $data
  90. */
  91. public function searchCidAttr($query, $value, $data)
  92. {
  93. if (is_array($value)) {
  94. $query->where('cid', 'IN', $value);
  95. } else {
  96. $query->where('cid', $value);
  97. }
  98. }
  99. /**
  100. * 用户ID搜索器
  101. * @param Model $query
  102. * @param $value
  103. * @param $data
  104. */
  105. public function searchUidAttr($query, $value, $data)
  106. {
  107. $query->where('uid', $value);
  108. }
  109. /**
  110. * 优惠券名称搜索器
  111. * @param Model $query
  112. * @param $value
  113. * @param $data
  114. */
  115. public function searchCouponTitleAttr($query, $value, $data)
  116. {
  117. $query->where('coupon_title', 'like', '%' . $value . '%');
  118. }
  119. /**
  120. * 获取方式搜索器
  121. * @param Model $query
  122. * @param $value
  123. * @param $data
  124. */
  125. public function searchTypeAttr($query, $value, $data)
  126. {
  127. $query->where('type', $value);
  128. }
  129. /**
  130. * 是否失效
  131. * @param Model $query
  132. * @param $value
  133. * @param $data
  134. */
  135. public function searchIsFailAttr($query, $value, $data)
  136. {
  137. $query->where('is_fail', $value);
  138. }
  139. /**
  140. * 是否在使用期限内搜索器
  141. * @param Model $query
  142. * @param $value
  143. * @param $data
  144. */
  145. public function searchTimeAttr($query, $value, $data)
  146. {
  147. $query->whereTime('add_time', '>=', $value)->whereTime('end_time', '<=', $value);
  148. }
  149. }