StoreCouponUser.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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\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. * 优惠券发放Model
  18. * Class StoreCouponUser
  19. * @package app\model\activity\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 = [
  39. 'send' => '后台发放',
  40. 'get' => '手动领取',
  41. 'newcomer' => '新人礼赠送',
  42. 'activate_level' => '会员卡激活赠送',
  43. 'user_first' => '用户注册赠送',
  44. 'order' => '下单赠送',
  45. 'luck_lottery' => '抽奖赠送'
  46. ];
  47. /**
  48. * 类型获取器
  49. * @param $value
  50. * @return string
  51. */
  52. public function getTypeAttr($value)
  53. {
  54. return $this->gainType[$value];
  55. }
  56. /**
  57. * 使用状态
  58. * @var string[]
  59. */
  60. protected $statusType = [0 => '未使用', 1 => '已使用', 2 => '已过期'];
  61. /**
  62. * 状态获取器
  63. * @param $value
  64. * @return string
  65. */
  66. public function getStatusAttr($value)
  67. {
  68. return $this->statusType[$value];
  69. }
  70. /**
  71. * @return \think\model\relation\HasOne
  72. */
  73. public function issue()
  74. {
  75. return $this->hasOne(StoreCouponIssue::class, 'id', 'cid')->field(['id', 'end_use_time', 'start_use_time', 'coupon_type', 'type', 'coupon_time', 'product_id', 'category_id', 'brand_id', 'receive_type', 'applicable_type', 'applicable_store_id'])->bind([
  76. 'coupon_applicable_type' => 'type',
  77. 'coupon_time' => 'coupon_time',
  78. 'product_id',
  79. 'category_id',
  80. 'brand_id',
  81. 'receive_type',
  82. 'coupon_type',
  83. 'start_use_time',
  84. 'end_use_time',
  85. 'applicable_type',
  86. 'applicable_store_id'
  87. ]);
  88. }
  89. /**
  90. * 获取领取人名称头像
  91. * @return \think\model\relation\HasOne
  92. */
  93. public function userInfo()
  94. {
  95. return $this->hasOne(User::class, 'uid', 'uid')->field('uid,nickname,avatar')->bind(['nickname', 'avatar']);
  96. }
  97. /**
  98. * id
  99. * @param Model $query
  100. * @param $value
  101. */
  102. public function searchIdAttr($query, $value)
  103. {
  104. if (is_array($value))
  105. $query->whereIn('id', $value);
  106. else
  107. $query->where('id', $value);
  108. }
  109. /**
  110. * 优惠券ID搜索器
  111. * @param Model $query
  112. * @param $value
  113. */
  114. public function searchCidAttr($query, $value)
  115. {
  116. if (is_array($value))
  117. $query->whereIn('cid', $value);
  118. else
  119. $query->where('cid', $value);
  120. }
  121. /**
  122. * 用户ID搜索器
  123. * @param Model $query
  124. * @param $value
  125. * @param $data
  126. */
  127. public function searchUidAttr($query, $value, $data)
  128. {
  129. $query->where('uid', $value);
  130. }
  131. /**
  132. * 优惠券名称搜索器
  133. * @param Model $query
  134. * @param $value
  135. * @param $data
  136. */
  137. public function searchCouponTitleAttr($query, $value, $data)
  138. {
  139. $query->where('coupon_title', 'like', '%' . $value . '%');
  140. }
  141. /**
  142. * 获取方式搜索器
  143. * @param Model $query
  144. * @param $value
  145. * @param $data
  146. */
  147. public function searchTypeAttr($query, $value, $data)
  148. {
  149. $query->where('type', $value);
  150. }
  151. /**
  152. * 用户ID搜索器
  153. * @param Model $query
  154. * @param $value
  155. * @param $data
  156. */
  157. public function searchStatusAttr($query, $value, $data)
  158. {
  159. if ($value !== '' && $value != -1) {
  160. if (is_array($value)) {
  161. $query->whereIn('status', $value);
  162. } else {
  163. $query->where('status', $value);
  164. }
  165. }
  166. }
  167. /**
  168. * 是否失效
  169. * @param Model $query
  170. * @param $value
  171. * @param $data
  172. */
  173. public function searchIsFailAttr($query, $value, $data)
  174. {
  175. $query->where('is_fail', $value);
  176. }
  177. /**
  178. * 是否在使用期限内搜索器
  179. * @param Model $query
  180. * @param $value
  181. * @param $data
  182. */
  183. public function searchTimeAttr($query, $value, $data)
  184. {
  185. $query->whereTime('add_time', '>=', $value)->whereTime('end_time', '<=', $value);
  186. }
  187. }