UserCoupon.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. use app\common\library\IntCode;
  5. use app\common\model\OrderAction;
  6. class UserCoupon extends Model
  7. {
  8. // 表名
  9. protected $name = 'shop_user_coupon';
  10. // 自动写入时间戳字段
  11. protected $autoWriteTimestamp = 'int';
  12. // 定义时间戳字段名
  13. protected $createTime = 'createtime';
  14. protected $updateTime = 'updatetime';
  15. protected $deleteTime = false;
  16. // 追加属性
  17. protected $append = [
  18. 'is_used_text'
  19. ];
  20. public function getIsUsedList()
  21. {
  22. return ['1' => __('Is_used 1'), '2' => __('Is_used 2')];
  23. }
  24. public function getIsUsedTextAttr($value, $data)
  25. {
  26. $value = $value ?: ($data['is_used'] ?? '');
  27. $list = $this->getIsUsedList();
  28. return $list[$value] ?? '';
  29. }
  30. /**
  31. * 检查优惠券的归属用户和是否已使用
  32. *
  33. * @param [type] $user_coupon_id
  34. * @param [type] $user_id
  35. * @return Object
  36. */
  37. public function checkUserOrUse($user_coupon_id, $user_id)
  38. {
  39. $row = $this->where('id', $user_coupon_id)->where('user_id', $user_id)->find();
  40. if (!$row) {
  41. throw new \Exception('未领取该优惠券或不可用!');
  42. }
  43. if ($row['is_used'] == 2) {
  44. throw new \Exception('该优惠券已使用!');
  45. }
  46. return $row;
  47. }
  48. //我的优惠券列表
  49. public static function tableList($param)
  50. {
  51. $pageNum = 10;
  52. if (!empty($param['num'])) {
  53. $pageNum = $param['num'];
  54. }
  55. return self::field('id,coupon_id,user_id,is_used,expire_time,begin_time,createtime')->with(['Coupon' => function ($query) {
  56. $query->field('id,name,result,result_data,allow_num,begintime,endtime,mode,use_times');
  57. }])->where(function ($query) use ($param) {
  58. $time = time();
  59. if (!empty($param['is_used'])) {
  60. $query->where('is_used', $param['is_used'])->where('begin_time', '<', $time);
  61. }
  62. if (isset($param['user_id']) && $param['user_id'] != '') {
  63. $query->where('user_id', $param['user_id']);
  64. }
  65. if (!empty($param['begin_time'])) {
  66. $query->where('begin_time', '>', $time);
  67. }
  68. if (!empty($param['expire_time'])) {
  69. $query->where('expire_time', '<', $time);
  70. }
  71. })->order('createtime desc')->paginate($pageNum);
  72. }
  73. //我的可以使用的优惠券【过滤掉条件不符合的】
  74. public static function myGoodsCoupon($user_id, $goods_ids, $category_ids, $brand_ids)
  75. {
  76. $order = (new Order())->where('user_id', $user_id)->where('pay_time', '>', 0)->count();
  77. if ($order) {
  78. $type = 3; //老用户
  79. } else {
  80. $type = 2; //新用户
  81. }
  82. $conditions = CouponCondition::getGoodsCondition($goods_ids, $category_ids, $brand_ids, $type);
  83. $sql = "condition_ids IS NULL OR condition_ids=''";
  84. foreach ($conditions as $key => $item) {
  85. $sql .= " OR FIND_IN_SET('{$item['id']}',condition_ids)";
  86. }
  87. $time = time();
  88. //我的所有未使用的优惠券
  89. $list = self::field('*')->with(['Coupon' => function ($query) use ($sql) {
  90. $query->where($sql);
  91. }])->where('user_id', $user_id)
  92. ->where('is_used', 1)
  93. ->where('begin_time', '<', $time)
  94. ->where('expire_time', '>', $time)
  95. ->select();
  96. $data = [];
  97. foreach ($list as $item) {
  98. if (!empty($item['coupon'])) {
  99. $coupon = $item['coupon'];
  100. $coupon['id'] = is_numeric($coupon['id']) ? IntCode::encode($coupon['id']) : $coupon['id'];
  101. $coupon['user_coupon_id'] = $item['id'];
  102. $coupon['expire_time'] = $item['expire_time'];
  103. $data[] = $coupon->toArray();
  104. }
  105. }
  106. return $data;
  107. }
  108. //恢复优惠券
  109. public static function resetUserCoupon($user_coupon_id, $order_sn)
  110. {
  111. if ($user_coupon_id) {
  112. self::where('id', $user_coupon_id)->update(['is_used' => 1]);
  113. //记录操作
  114. OrderAction::push($order_sn, '系统', '订单取消恢复优惠券');
  115. }
  116. return true;
  117. }
  118. public function Coupon()
  119. {
  120. return $this->hasOne('Coupon', 'id', 'coupon_id');
  121. }
  122. }