Coupon.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. namespace app\admin\model\shopro;
  3. use app\admin\model\shopro\Common;
  4. use app\admin\model\shopro\user\Coupon as UserCouponModel;
  5. use app\admin\model\shopro\goods\Goods;
  6. use app\admin\model\shopro\Category;
  7. use traits\model\SoftDelete;
  8. class Coupon extends Common
  9. {
  10. use SoftDelete;
  11. protected $deleteTime = 'deletetime';
  12. protected $name = 'shopro_coupon';
  13. protected $type = [
  14. 'get_start_time' => 'timestamp',
  15. 'get_end_time' => 'timestamp',
  16. // 'use_start_time' => 'timestamp',
  17. // 'use_end_time' => 'timestamp',
  18. ];
  19. // 追加属性
  20. protected $append = [
  21. 'type_text',
  22. 'status_text',
  23. 'use_scope_text',
  24. 'amount_text',
  25. 'get_time_status',
  26. 'get_time_text'
  27. ];
  28. /**
  29. * 默认类型列表
  30. *
  31. * @return array
  32. */
  33. public function typeList()
  34. {
  35. return [
  36. 'reduce' => '满减券',
  37. 'discount' => '折扣券'
  38. ];
  39. }
  40. /**
  41. * 可用范围列表
  42. *
  43. * @return array
  44. */
  45. public function useScopeList()
  46. {
  47. return [
  48. 'all_use' => '全场通用',
  49. 'goods' => '指定商品可用',
  50. 'disabled_goods' => '指定商品不可用',
  51. 'category' => '指定分类可用',
  52. ];
  53. }
  54. /**
  55. * 默认状态列表
  56. *
  57. * @return array
  58. */
  59. public function statusList()
  60. {
  61. return [
  62. 'normal' => '公开发放',
  63. 'hidden' => '后台发放',
  64. 'disabled' => '禁止使用',
  65. ];
  66. }
  67. /**
  68. * 优惠券领取状态
  69. *
  70. * @return void
  71. */
  72. public function getStatusList()
  73. {
  74. return [
  75. 'can_get' => '立即领取',
  76. 'cannot_get' => '已领取',
  77. 'get_over' => '已领完',
  78. // 用户优惠券的状态
  79. 'used' => '已使用',
  80. 'can_use' => '立即使用',
  81. 'expired' => '已过期',
  82. 'cannot_use' => '暂不可用'
  83. ];
  84. }
  85. public function scopeCanGet($query)
  86. {
  87. return $query->where('get_start_time', '<=', time())
  88. ->where('get_end_time', '>=', time());
  89. }
  90. /**
  91. * 查询指定商品满足的优惠券
  92. *
  93. * @param [type] $query
  94. * @param [type] $goods
  95. * @return void
  96. */
  97. public function scopeGoods($query, $goods)
  98. {
  99. $goods_id = $goods['id'];
  100. $category_ids = $goods['category_ids'];
  101. // 查询符合商品的优惠券
  102. return $query->where(function ($query) use ($goods_id, $category_ids) {
  103. $query->where('use_scope', 'all_use')
  104. ->whereOr(function ($query) use ($goods_id) {
  105. $query->where('use_scope', 'goods')->whereRaw("find_in_set($goods_id, items)");
  106. })
  107. ->whereOr(function ($query) use ($goods_id) {
  108. $query->where('use_scope', 'disabled_goods')->whereRaw("not find_in_set($goods_id, items)");
  109. })
  110. ->whereOr(function ($query) use ($goods_id, $category_ids) {
  111. $query->where('use_scope', 'category')->where(function ($query) use ($category_ids) {
  112. $category_ids = array_filter(explode(',', $category_ids));
  113. foreach ($category_ids as $key => $category_id) {
  114. $query->whereOrRaw("find_in_set($category_id, items)");
  115. }
  116. });
  117. });
  118. });
  119. }
  120. /**
  121. * 开始使用时间获取器
  122. *
  123. * @param string $value
  124. * @param array $data
  125. * @return int
  126. */
  127. public function setUseStartTimeAttr($value, $data)
  128. {
  129. return $value ? strtotime($value) : (isset($data['use_start_time']) ? strtotime($data['use_start_time']) : 0);
  130. }
  131. /**
  132. * 结束使用时间获取器
  133. *
  134. * @param string $value
  135. * @param array $data
  136. * @return int
  137. */
  138. public function setUseEndTimeAttr($value, $data)
  139. {
  140. return $value ? strtotime($value) : (isset($data['use_end_time']) ? strtotime($data['use_end_time']) : 0);
  141. }
  142. /**
  143. * 可用范围获取器
  144. *
  145. * @param string $value
  146. * @param array $data
  147. * @return string
  148. */
  149. public function getUseScopeTextAttr($value, $data)
  150. {
  151. $value = $value ?: ($data['use_scope'] ?? null);
  152. $list = $this->useScopeList();
  153. return isset($list[$value]) ? $list[$value] : '';
  154. }
  155. public function getAmountTextAttr($value, $data)
  156. {
  157. return '满' . $data['enough'] . '元,' . ($data['type'] == 'reduce' ? '减' . floatval($data['amount']) . '元' : '打' . floatval($data['amount']) . '折');
  158. }
  159. public function getItemsValueAttr($value, $data)
  160. {
  161. if (in_array($data['use_scope'], ['goods', 'disabled_goods'])) {
  162. $items_value = Goods::whereIn('id', $data['items'])->select();
  163. $items_value = collection($items_value);
  164. $items_value = $items_value->each(function ($goods) {
  165. // 前端要显示活动标签
  166. $goods->promos = $goods->promos;
  167. });
  168. } else {
  169. $items_value = Category::whereIn('id', $data['items'])->select();
  170. }
  171. return $items_value ?? [];
  172. }
  173. public function getGetStatusAttr($value, $data)
  174. {
  175. $limit_num = $data['limit_num'] ?? 0;
  176. // 不限制领取次数,或者限制次数,领取数量还没达到最大值
  177. $get_status = (!$limit_num || ($limit_num && $limit_num > count($this->user_coupons))) ? 'can_get' : 'cannot_get';
  178. if ($get_status == 'can_get' && $data['stock'] <= 0) {
  179. $get_status = 'get_over'; // 已领完
  180. }
  181. $user_coupon_id = request()->param('user_coupon_id', 0);
  182. if ($user_coupon_id) {
  183. // 从我领取的优惠券进详情,覆盖 状态
  184. $user = auth_user();
  185. $userCoupon = UserCouponModel::where('user_id', ($user ? $user->id : 0))->find($user_coupon_id);
  186. if ($userCoupon) {
  187. $get_status = $userCoupon->status;
  188. }
  189. }
  190. return $get_status;
  191. }
  192. public function getGetStatusTextAttr($value, $data)
  193. {
  194. $list = $this->getStatusList();
  195. return isset($list[$this->get_status]) ? $list[$this->get_status] : '';
  196. }
  197. /**
  198. * 后端发放状态
  199. *
  200. * @return string
  201. */
  202. public function getGetTimeStatusAttr($value, $data) {
  203. if ($data['get_start_time'] > time()) {
  204. $time_text = 'nostart'; // 未开始
  205. } else if ($data['get_start_time'] <= time() && $data['get_end_time'] >= time()) {
  206. $time_text = 'ing';
  207. } else if ($data['get_end_time'] < time()) {
  208. $time_text = 'ended';
  209. }
  210. return $time_text;
  211. }
  212. /**
  213. * 后端发放状态
  214. *
  215. * @return string
  216. */
  217. public function getGetTimeTextAttr($value, $data)
  218. {
  219. if ($this->get_time_status == 'nostart') {
  220. $time_text = '未开始'; // 未开始
  221. } else if ($this->get_time_status == 'ing') {
  222. $time_text = '发放中';
  223. } else if ($this->get_time_status == 'ended') {
  224. $time_text = '已结束';
  225. }
  226. return $time_text;
  227. }
  228. public function getGetNumAttr($value, $data)
  229. {
  230. return UserCouponModel::where('coupon_id', $data['id'])->count();
  231. }
  232. public function getUseNumAttr($value, $data)
  233. {
  234. return UserCouponModel::where('coupon_id', $data['id'])->whereNotNull('use_time')->count();
  235. }
  236. public function getUseStartTimeAttr($value, $data)
  237. {
  238. $use_start_time = $value ? date('Y-m-d H:i:s', $value) : null;
  239. $user_coupon_id = request()->param('user_coupon_id', 0);
  240. if ($user_coupon_id && $data['use_time_type'] == 'days') {
  241. // 从我领取的优惠券进详情,覆盖 状态
  242. $user = auth_user();
  243. $userCoupon = UserCouponModel::cache(60)->where('user_id', ($user ? $user->id : 0))->find($user_coupon_id);
  244. if ($userCoupon) {
  245. $use_start_time = date('Y-m-d H:i:s', $userCoupon->getData('createtime') + ($this->start_days * 86400));
  246. }
  247. }
  248. return $use_start_time;
  249. }
  250. public function getUseEndTimeAttr($value, $data)
  251. {
  252. $use_end_time = $value ? date('Y-m-d H:i:s', $value) : null;
  253. $user_coupon_id = request()->param('user_coupon_id', 0);
  254. if ($user_coupon_id && $data['use_time_type'] == 'days') {
  255. // 从我领取的优惠券进详情,覆盖 状态
  256. $user = auth_user();
  257. $userCoupon = UserCouponModel::cache(60)->where('user_id', ($user ? $user->id : 0))->find($user_coupon_id);
  258. if ($userCoupon) {
  259. $use_end_time = date('Y-m-d H:i:s', $userCoupon->getData('createtime') + (($this->start_days + $this->days) * 86400));
  260. }
  261. }
  262. return $use_end_time;
  263. }
  264. public function userCoupons()
  265. {
  266. $user = auth_user();
  267. return $this->hasMany(UserCouponModel::class, 'coupon_id')->where('user_id', ($user ? $user->id : 0));
  268. }
  269. }