Coupon.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace app\backend\modules\coupon\models;
  3. use app\common\observers\coupon\CouponObserver;
  4. use app\common\models\coupon\CouponIncreaseRecords;
  5. class Coupon extends \app\common\models\Coupon
  6. {
  7. static protected $needLog = true;
  8. public $table = 'yz_coupon';
  9. public $widgets = [];
  10. //类型转换
  11. protected $casts = [
  12. 'goods_ids' => 'json',
  13. 'category_ids' => 'json',
  14. 'storeids' => 'json',
  15. 'member_tags_ids' => 'json',
  16. 'goods_names' => 'json',
  17. 'categorynames' => 'json',
  18. 'storenames' => 'json',
  19. 'member_tags_names' => 'json',
  20. ];
  21. //默认值
  22. protected $attributes = [
  23. 'goods_ids' => '[]',
  24. 'category_ids' => '[]',
  25. 'storeids' => '[]',
  26. 'member_tags_ids' => '[]',
  27. 'display_order' => 0,
  28. 'plugin_id' => 0,
  29. ];
  30. /**
  31. * 定义字段名
  32. * 可使
  33. * @return array */
  34. public function atributeNames() {
  35. return [
  36. 'display_order'=> '排序',
  37. 'name'=> '优惠券名称',
  38. 'enough'=> '使用条件(消费金额前提)',
  39. 'time_days'=> '使用时间限制',
  40. 'deduct'=> '立减',
  41. 'discount'=> '折扣',
  42. 'get_max'=> '每个人的限领数量',
  43. 'get_limit_max' => '每人每日限领数量',
  44. 'total' => '发放总数',
  45. 'resp_title' => '推送标题',
  46. 'resp_desc' => '推送说明',
  47. 'resp_url' => '推送链接',
  48. ];
  49. }
  50. public function couponIncreaseRecords()
  51. {
  52. return $this->hasMany(CouponIncreaseRecords::class, 'coupon_id')->select('count', 'created_at')->orderBy('created_at', 'desc')->get();
  53. }
  54. /**
  55. * 字段规则
  56. * @return array */
  57. public function rules() {
  58. //因为 deduct 和 discount 的默认值都为 0, 为了让 0 通过验证, 所以需要如下判断
  59. if($this->coupon_method == 1){
  60. $deduct = '|min:1';
  61. // if($this->enough){
  62. // $deduct = '|between:1,'.$this->enough; //不能超过"订单金额"(如果 enough 为 0, 表示不限制消费金额, 则不限制"立减"金额)
  63. // } else{
  64. // $deduct = '|min:1';
  65. // }
  66. }elseif($this->coupon_method == 2){
  67. $discount = '|between:0,10';
  68. }else{
  69. $deduct = null;
  70. $discount = null;
  71. }
  72. return [
  73. 'display_order' => 'required|integer',
  74. 'name' => 'required',
  75. 'enough' => 'required|integer',
  76. 'time_days' => 'required|integer',
  77. 'deduct' => 'required|'.$deduct,
  78. 'discount' => 'required|'.$discount,
  79. // 'get_max' => 'required|integer',
  80. // 'get_limit_max' => 'required|integer',
  81. 'total' => 'required|integer',
  82. 'resp_title' => 'nullable|string',
  83. 'resp_desc' => 'nullable|string',
  84. 'resp_url' => 'nullable|url',
  85. ];
  86. }
  87. /**
  88. * @param $keyword
  89. * @return mixed
  90. */
  91. public static function getCouponsByName($keyword)
  92. {
  93. return static::uniacid()->select('id', 'name')
  94. ->where('name', 'like', '%' . $keyword . '%')
  95. ->get();
  96. }
  97. /**
  98. * @param $keyword
  99. * @return mixed
  100. */
  101. public static function getCouponsDataByName($keyword)
  102. {
  103. return static::uniacid()
  104. ->select(['id','display_order','name', 'enough', 'coupon_method', 'deduct', 'discount', 'get_type', 'created_at','status','money' , 'total'])
  105. ->where('name', 'like', '%' . $keyword . '%');
  106. }
  107. /**
  108. * @param $title 优惠券名称
  109. * @param $type 优惠券是否在领取中心显示
  110. * @param $timeSwitch 是否开启"创建时间"的搜索选项
  111. * @param $timeStart 起始时间
  112. * @param $timeEnd 结束时间
  113. * @return mixed
  114. */
  115. static public function getCouponsBySearch($title, $type=NULL, $timeSwitch=0, $timeStart=NULL, $timeEnd=NULL)
  116. {
  117. $CouponsModel = self::uniacid()
  118. ->select(['id','display_order','name', 'enough',
  119. 'coupon_method', 'deduct', 'discount', 'get_type', 'created_at','total']);
  120. if(!empty($title)){
  121. $CouponsModel = $CouponsModel->where('name', 'like', '%'.$title.'%');
  122. }
  123. if(!empty($type) || $type == '0'){
  124. $CouponsModel = $CouponsModel->where('get_type', '=', $type);
  125. }
  126. if($timeSwitch == 1 && !empty($timeStart) && !empty($timeEnd)){
  127. $CouponsModel = $CouponsModel->whereBetween('created_at', [$timeStart, $timeEnd]);
  128. }
  129. return $CouponsModel;
  130. }
  131. //删除优惠券
  132. public static function deleteCouponById($couponId)
  133. {
  134. return static::find($couponId)->delete();
  135. }
  136. }