Coupon.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <?php
  2. namespace app\common\models;
  3. use app\framework\Database\Eloquent\Builder;
  4. use Carbon\Carbon;
  5. use Illuminate\Database\Eloquent\SoftDeletes;
  6. /**
  7. * Class Coupon
  8. * @package app\common\models
  9. * @property int coupon_method
  10. * @property int use_type
  11. * @property int status
  12. * @property int get_type
  13. * @property int time_limit
  14. * @property string name
  15. * @property string suppliernames
  16. * @property array supplierids
  17. * @property int getsupplier
  18. * @property string storenames
  19. * @property array storeids
  20. * @property int getstore
  21. * @property string category_ids
  22. * @property int is_complex
  23. * @property array goods_ids
  24. * @property int id
  25. * @property int plugin_id
  26. * @property int total
  27. * @property int time_days
  28. * @property float discount
  29. * @property float enough
  30. * @property float deduct
  31. * @property int get_max
  32. * @property int level_limit
  33. * @property Carbon time_start
  34. * @property Carbon time_end
  35. * @method Builder memberLevel($memberLevel)
  36. * @method Builder unexpired($time)
  37. */
  38. class Coupon extends BaseModel
  39. {
  40. use SoftDeletes;
  41. protected $dates = ['deleted_at', 'time_start', 'time_end'];
  42. const COUPON_SHOP_USE = 0; //适用范围 - 商城通用
  43. const COUPON_CATEGORY_USE = 1; //适用范围 - 指定分类
  44. const COUPON_GOODS_USE = 2; //适用范围 - 指定商品
  45. const COUPON_SUPPLIER_USE = 3; //适用范围 - 指定供应商single
  46. const COUPON_STORE_USE = 4; //适用范围 - 指定门店
  47. const COUPON_SINGLE_STORE_USE = 5; //适用范围 - 指定单个门店
  48. const COUPON_ONE_HOTEL_USE = 6; //适用范围 - 单个酒店
  49. const COUPON_MORE_HOTEL_USE = 7; //适用范围 - 多个酒店
  50. const COUPON_EXCHANGE_USE = 8; //兑换券
  51. const COUPON_GOODS_AND_STORE_USE = 9; //指定商品+指定门店
  52. const COUPON_MONEY_OFF = 1; //优惠方式- 立减
  53. const COUPON_DISCOUNT = 2; //优惠方式- 折扣
  54. const COUPON_EXCHANGE = 3; //优惠方式- 兑换
  55. const COUPON_DATE_TIME_RANGE = 1;//有效期 - 时间范围
  56. const COUPON_SINCE_RECEIVE = 0;//有效期 - 领取后n天
  57. const NO_LIMIT = -1; //不限
  58. public $table = 'yz_coupon';
  59. protected $guarded = [];
  60. protected $casts = [
  61. 'goods_ids' => 'json',
  62. 'category_ids' => 'json',
  63. 'goods_names' => 'json',
  64. 'categorynames' => 'json',
  65. 'supplierids' => 'json',
  66. 'storeids' => 'json',
  67. 'storenames' => 'json',
  68. 'member_tags_ids' => 'json',
  69. 'member_tags_names' => 'json',
  70. ];
  71. /**
  72. * @var array
  73. */
  74. protected $appends = ['coupon_type_name'];
  75. public function getCouponTypeNameAttribute()
  76. {
  77. switch ($this->attributes['use_type']) {
  78. case Coupon::COUPON_SHOP_USE:
  79. $name = '平台商品';
  80. break;
  81. case Coupon::COUPON_STORE_USE:
  82. $name = '门店商品';
  83. break;
  84. case Coupon::COUPON_SINGLE_STORE_USE:
  85. $name = '门店商品';
  86. break;
  87. case Coupon::COUPON_ONE_HOTEL_USE:
  88. $name = '酒店商品';
  89. break;
  90. case Coupon::COUPON_MORE_HOTEL_USE:
  91. $name = '酒店商品';
  92. break;
  93. case Coupon::COUPON_GOODS_USE:
  94. $name = '指定商品';
  95. break;
  96. case Coupon::COUPON_CATEGORY_USE:
  97. $name = '分类商品';
  98. break;
  99. case Coupon::COUPON_EXCHANGE_USE:
  100. $name = '兑换券';
  101. break;
  102. case Coupon::COUPON_GOODS_AND_STORE_USE:
  103. $name = '商品和门店';
  104. break;
  105. default:
  106. $name = '未知类型';
  107. }
  108. return $name;
  109. }
  110. // protected $hidden = ['storeids'];
  111. // protected $hidden = ['uniacid', 'cat_id', 'get_type', 'level_limit', 'use_type', 'return_type', 'coupon_type'
  112. // , 'coupon_method','back_type','supplier_uid','cashiersids','cashiersnames','category_ids','goods_ids',
  113. // 'storeids','supplierids','is_complex','getcashier','getstore','getsupplier','back_money','back_credit',
  114. // 'back_redpack','back_when','descnoset','deleted_at'];
  115. public function hasManyMemberCoupon()
  116. {
  117. return $this->hasMany('app\common\models\MemberCoupon','coupon_id','id');
  118. }
  119. public static function getValidCoupon($MemberModel)
  120. {
  121. return MemberCoupon::getMemberCoupon($MemberModel);
  122. }
  123. public static function getUsageCount($couponId)
  124. {
  125. return static::uniacid()
  126. ->select(['id'])
  127. ->where('id', '=', $couponId)
  128. ->withCount(['hasManyMemberCoupon' => function ($query) {
  129. return $query->where('used', '=', 0);
  130. }]);
  131. }
  132. public static function getCouponById($couponId)
  133. {
  134. return static::uniacid()
  135. ->where('id', '=', $couponId)
  136. // ->with(['hasManyMemberCoupon'])(这个查询导致数据量太大崩了)
  137. ->first();
  138. }
  139. //获取优惠券优惠方式
  140. public static function getPromotionMethod($couponId)
  141. {
  142. $useType = static::uniacid()->where('id', '=', $couponId)->value('coupon_method');
  143. switch ($useType) {
  144. case self::COUPON_MONEY_OFF:
  145. return [
  146. 'type' => self::COUPON_MONEY_OFF,
  147. 'mode' => static::uniacid()->where('id', '=', $couponId)->value('deduct'),
  148. ];
  149. break;
  150. case self::COUPON_DISCOUNT:
  151. return [
  152. 'type' => self::COUPON_DISCOUNT,
  153. 'mode' => static::uniacid()->where('id', '=', $couponId)->value('discount'),
  154. ];
  155. break;
  156. default:
  157. return [
  158. 'type' => self::COUPON_SHOP_USE,
  159. ];
  160. break;
  161. }
  162. }
  163. //获取优惠券适用期限
  164. public static function getTimeLimit($couponId)
  165. {
  166. $time_limit = static::uniacid()->where('id', '=', $couponId)->value('time_limit');
  167. switch ($time_limit) {
  168. case self::COUPON_SINCE_RECEIVE:
  169. return [
  170. 'type' => self::COUPON_SINCE_RECEIVE,
  171. 'time_end' => static::uniacid()->where('id', '=', $couponId)->value('time_days'),
  172. ];
  173. break;
  174. case self::COUPON_DATE_TIME_RANGE:
  175. return [
  176. 'type' => self::COUPON_DATE_TIME_RANGE,
  177. 'time_end' => static::uniacid()->where('id', '=', $couponId)->value('time_end'),
  178. ];
  179. break;
  180. default:
  181. return [
  182. 'type' => self::COUPON_SHOP_USE,
  183. ];
  184. break;
  185. }
  186. }
  187. //获取优惠券的适用范围
  188. public static function getApplicableScope($couponId)
  189. {
  190. $useType = static::uniacid()
  191. ->where('id', '=', $couponId)
  192. ->value('use_type');
  193. switch ($useType) {
  194. case 8:
  195. $goodIds = self::getApplicalbeGoodIds($couponId);
  196. return [
  197. 'type' => self::COUPON_GOODS_USE,
  198. 'scope' => $goodIds,
  199. ];
  200. break;
  201. case self::COUPON_GOODS_USE:
  202. $goodIds = self::getApplicalbeGoodIds($couponId);
  203. return [
  204. 'type' => self::COUPON_GOODS_USE,
  205. 'scope' => $goodIds,
  206. ];
  207. break;
  208. case self::COUPON_CATEGORY_USE:
  209. $categoryIds = self::getApplicalbeCategoryIds($couponId);
  210. return [
  211. 'type' => self::COUPON_CATEGORY_USE,
  212. 'scope' => $categoryIds,
  213. ];
  214. break;
  215. case self::COUPON_STORE_USE:
  216. $categoryIds = self::getApplicalbeCategoryIds($couponId);
  217. return [
  218. 'type' => self::COUPON_STORE_USE,
  219. 'scope' => $categoryIds,
  220. ];
  221. break;
  222. case self::COUPON_SINGLE_STORE_USE:
  223. $categoryIds = self::getApplicalbeCategoryIds($couponId);
  224. return [
  225. 'type' => self::COUPON_SINGLE_STORE_USE,
  226. 'scope' => $categoryIds,
  227. ];
  228. break;
  229. case self::COUPON_GOODS_AND_STORE_USE:
  230. return [
  231. 'type' => self::COUPON_GOODS_AND_STORE_USE,
  232. ];
  233. break;
  234. default:
  235. return [
  236. 'type' => self::COUPON_SHOP_USE,
  237. ];
  238. break;
  239. }
  240. }
  241. //获取优惠券的适用商品ID
  242. public static function getApplicalbeGoodIds($couponId)
  243. {
  244. return static::uniacid()
  245. ->where('id', '=', $couponId)
  246. ->value('goods_ids');
  247. }
  248. //获取优惠券的适用商品分类ID
  249. public static function getApplicalbeCategoryIds($couponId)
  250. {
  251. return static::uniacid()
  252. ->where('id', '=', $couponId)
  253. ->value('category_ids');
  254. }
  255. /**
  256. * 筛选某会员等级可领
  257. * @param Builder $query
  258. * @param $memberLevel
  259. * @return Builder
  260. */
  261. public function scopeMemberLevel(Builder $query, $memberLevel)
  262. {
  263. return $query->where(function ($query) use ($memberLevel) {
  264. $query->where('level_limit', '<=', $memberLevel)
  265. ->orWhere(function ($query) {
  266. $query->where('level_limit', -1);
  267. });
  268. });
  269. }
  270. /**
  271. * 筛选未过期
  272. * @param Builder $query
  273. * @param null $time
  274. * @return Builder
  275. */
  276. public function scopeUnexpired(Builder $query, $time = null)
  277. {
  278. if (!isset($time)) {
  279. $time = time();
  280. }
  281. return $query->where(function ($query) use ($time) {
  282. // 不限时间
  283. $query->where('time_limit', 1)->where('time_end', '>', $time-86400)
  284. ->orWhere(function ($query) {
  285. // 未结束的优惠券
  286. $query->where('time_limit', 0);
  287. });
  288. });
  289. }
  290. /**
  291. * todo 为什么会出现负数
  292. * 可领取张数
  293. * @param $receivedCount
  294. * @return int
  295. */
  296. public function availableCount($receivedCount)
  297. {
  298. if ($this->get_max == self::NO_LIMIT) {
  299. return 999;
  300. }
  301. return max($this->get_max - $receivedCount,0);
  302. }
  303. /**
  304. * todo 应在优惠券表添加这个字段
  305. * 获取已领取数量
  306. * @return int
  307. */
  308. public function getReceiveCount()
  309. {
  310. return $this->hasManyMemberCoupon()->count();
  311. }
  312. /**
  313. * 是否可领取
  314. * @return bool
  315. */
  316. public function available($special_type = 0)
  317. {
  318. $res = $this->status == 1 && ($this->total == -1 || $this->total > 0);
  319. if ($special_type != 1){
  320. $res = $res && $this->get_type == 1;
  321. }
  322. return $res;
  323. // return $this->status == 1 && $this->get_type == 1 && ($this->total == -1 || $this->total > 0);
  324. }
  325. }