You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
4.6 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. <?php
  2. namespace App\Http\Model;
  3. use App\Common\ReturnData;
  4. use DB;
  5. class UserBonus extends BaseModel
  6. {
  7. //用户优惠券
  8. protected $table = 'user_bonus';
  9. public $timestamps = false;
  10. /**
  11. * 不能被批量赋值的属性
  12. *
  13. * @var array
  14. */
  15. protected $guarded = array();
  16. //获取列表
  17. public static function getList(array $param)
  18. {
  19. extract($param); //参数:limit,offset
  20. $where['user_id'] = $user_id;
  21. $limit = isset($limit) ? $limit : 10;
  22. $offset = isset($offset) ? $offset : 0;
  23. $model = new UserBonus;
  24. if(isset($status)){$where['status'] = $status;}
  25. $model = $model->where($where);
  26. $res['count'] = $model->count();
  27. $res['list'] = array();
  28. if($res['count']>0)
  29. {
  30. $bonus_list = $model->skip($offset)->take($limit)->orderBy('id','desc')->get();
  31. foreach($bonus_list as $k=>$v)
  32. {
  33. $bonus_list[$k]->bonus = Bonus::where('id',$v->bonus_id)->first();
  34. }
  35. $res['list'] = $bonus_list;
  36. }
  37. else
  38. {
  39. return false;
  40. }
  41. return $res;
  42. }
  43. public static function getOne($where)
  44. {
  45. $res = self::where($where)->first();
  46. if($res){$res->bonus = Bonus::where('id',$res->bonus_id)->first();}
  47. return $res;
  48. }
  49. public static function add(array $data)
  50. {
  51. $bonus1 = Bonus::where(['id'=>$data['bonus_id']])->where('num',-1)->first();
  52. $bonus2 = Bonus::where(['id'=>$data['bonus_id']])->where('num','>',0)->first();
  53. if(!$bonus1 && !$bonus2)
  54. {
  55. return ReturnData::create(ReturnData::PARAMS_ERROR,null,'亲,您来晚了啦,已被抢光了');
  56. }
  57. if(self::where(['bonus_id'=>$data['bonus_id'],'user_id'=>$data['user_id']])->first()){return ReturnData::create(ReturnData::PARAMS_ERROR,null,'亲,您已获取!');}
  58. $data['get_time'] = time(); //优惠券获取时间
  59. if ($id = self::insertGetId($data))
  60. {
  61. if(!$bonus1){DB::table('bonus')->where(array('id'=>$data['bonus_id']))->decrement('num', 1);}
  62. return ReturnData::create(ReturnData::SUCCESS,$id);
  63. }
  64. return ReturnData::create(ReturnData::SYSTEM_FAIL);
  65. }
  66. public static function modify($where, array $data)
  67. {
  68. if (self::where($where)->update($data))
  69. {
  70. return true;
  71. }
  72. return false;
  73. }
  74. //删除一条记录
  75. public static function remove($id)
  76. {
  77. if (!self::whereIn('id', explode(',', $id))->delete())
  78. {
  79. return false;
  80. }
  81. return true;
  82. }
  83. //商品结算时,获取优惠券列表
  84. public static function getAvailableBonusList(array $param)
  85. {
  86. extract($param);
  87. $where['user_bonus.user_id'] = $user_id;
  88. if(isset($status)){$where['bonus.status'] = 0;}
  89. $model = new UserBonus;
  90. if(isset($min_amount)){$model = $model->where('bonus.min_amount', '<=', $min_amount)->where('bonus.money', '<=', $min_amount);} //满多少使用
  91. if(isset($bonus_end_time)){$model = $model->where('bonus.end_time', '>=', date('Y-m-d H:i:s'));} //有效期
  92. $bonus_list = $model->join('bonus', 'bonus.id', '=', 'user_bonus.bonus_id')->where($where)
  93. ->select('bonus.*', 'user_bonus.user_id', 'user_bonus.used_time', 'user_bonus.get_time', 'user_bonus.status as user_bonus_status', 'user_bonus.id as user_bonus_id')
  94. ->orderBy('bonus.money','desc')->get();
  95. $res['list'] = $bonus_list;
  96. return $res;
  97. }
  98. public static function getUserBonusByid(array $param)
  99. {
  100. extract($param);
  101. $where['user_bonus.user_id'] = $user_id;
  102. $where['bonus.status'] = 0;
  103. $where['user_bonus.id'] = $user_bonus_id;
  104. $model = new UserBonus;
  105. if(isset($min_amount)){$model = $model->where('bonus.min_amount', '<=', $min_amount)->where('bonus.money', '<=', $min_amount);} //满多少使用
  106. $model = $model->where('bonus.end_time', '>=', date('Y-m-d H:i:s')); //有效期
  107. $bonus = $model->join('bonus', 'bonus.id', '=', 'user_bonus.bonus_id')->where($where)
  108. ->select('bonus.*', 'user_bonus.user_id', 'user_bonus.used_time', 'user_bonus.get_time', 'user_bonus.status as user_bonus_status', 'user_bonus.id as user_bonus_id')
  109. ->first();
  110. return $bonus;
  111. }
  112. }