MemberCoupon.php 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace app\frontend\modules\coupon\models;
  3. use app\common\exceptions\AppException;
  4. use app\common\exceptions\ShopException;
  5. class MemberCoupon extends \app\common\models\MemberCoupon
  6. {
  7. public $table = 'yz_member_coupon';
  8. const USED = 1;
  9. const NOT_USED = 0;
  10. //获取指定用户名下的优惠券
  11. public static function getCouponsOfMember($memberId,$search_type = '')
  12. {
  13. $coupons = static::uniacid()->with(['belongsToCoupon' => function ($query) {
  14. return $query->select(['id', 'name', 'coupon_method', 'deduct', 'discount', 'enough', 'use_type', 'category_ids', 'categorynames',
  15. 'goods_ids', 'goods_names', 'storeids', 'storenames', 'time_limit', 'time_days', 'time_start', 'time_end', 'total',
  16. 'money', 'credit', 'plugin_id', 'use_conditions']);
  17. }]);
  18. $coupons->whereHas('belongsToCoupon', function ($q) use ($search_type) {
  19. switch ($search_type) {
  20. case Coupon::TYPE_SHOP:
  21. $q->where('use_type', Coupon::COUPON_SHOP_USE);
  22. break;
  23. case Coupon::TYPE_STORE:
  24. if (app('plugins')->isEnabled('store-cashier')) {
  25. $q->whereIn('use_type', [Coupon::COUPON_STORE_USE, Coupon::COUPON_SINGLE_STORE_USE]);
  26. }
  27. break;
  28. case Coupon::TYPE_HOTEL:
  29. if (app('plugins')->isEnabled('hotel')) {
  30. $q->whereIn('use_type', [Coupon::COUPON_ONE_HOTEL_USE, Coupon::COUPON_MORE_HOTEL_USE]);
  31. }
  32. break;
  33. case Coupon::TYPE_GOODS:
  34. $q->where('use_type', Coupon::COUPON_GOODS_USE);
  35. break;
  36. case Coupon::TYPE_CATE:
  37. $q->where('use_type', Coupon::COUPON_CATEGORY_USE);
  38. break;
  39. case Coupon::TYPE_EXCHANGE:
  40. $q->where('use_type', Coupon::COUPON_EXCHANGE_USE);
  41. break;
  42. case Coupon::TYPE_GOOD_AND_STORE:
  43. $q->where('use_type', Coupon::COUPON_GOODS_AND_STORE_USE);
  44. break;
  45. case Coupon::TYPE_MONEY_OFF:
  46. $q->where('coupon_method', Coupon::COUPON_MONEY_OFF);
  47. break;
  48. case Coupon::TYPE_DISCOUNT:
  49. $q->where('coupon_method', Coupon::COUPON_DISCOUNT);
  50. break;
  51. case Coupon::TYPE_OVERDUE:
  52. $q->where('near_expiration', 1);
  53. break;
  54. }
  55. if (!app('plugins')->isEnabled('store-cashier')) {
  56. $q->whereNotIn('use_type', [Coupon::COUPON_STORE_USE, Coupon::COUPON_SINGLE_STORE_USE]);
  57. }
  58. if (!app('plugins')->isEnabled('hotel')) {
  59. $q->whereNotIn('use_type', [Coupon::COUPON_ONE_HOTEL_USE, Coupon::COUPON_MORE_HOTEL_USE]);
  60. }
  61. });
  62. $coupons->where('uid', $memberId)
  63. ->select(['id', 'coupon_id', 'used', 'use_time', 'get_time','near_expiration'])
  64. ->orderBy('get_time', 'desc');
  65. return $coupons;
  66. }
  67. public static function getExchange($memberId, $pluginId)
  68. {
  69. $coupons = static::uniacid()
  70. ->whereHas('belongsToCoupon',function ($query) use($pluginId) {
  71. return $query->where('plugin_id',$pluginId)->where('use_type',8);
  72. })
  73. ->with(['belongsToCoupon' => function ($query) {
  74. return $query->select(['id', 'name', 'coupon_method', 'deduct', 'discount', 'enough', 'use_type',
  75. 'goods_ids', 'goods_names','time_limit', 'time_days', 'time_start', 'time_end', 'total',
  76. 'money', 'credit', 'plugin_id']);
  77. }])
  78. ->where('used', '=', 0)
  79. ->where('is_member_deleted', 0)
  80. ->where('uid', $memberId)
  81. ->select(['id', 'coupon_id', 'used','use_time', 'get_time'])
  82. ->orderBy('get_time', 'desc');
  83. return $coupons;
  84. }
  85. }