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.

123 lines
3.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
  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. if(!Bonus::where(['id'=>$data['bonus_id']])->where('num',-1)->first() && !Bonus::where(['id'=>$data['bonus_id']])->where('num','>',0)->first())
  52. {
  53. return ReturnData::create(ReturnData::PARAMS_ERROR,null,'亲,您来晚了啦,已被抢光了');
  54. }
  55. if(self::where(['bonus_id'=>$data['bonus_id'],'user_id'=>$data['user_id']])->first()){return ReturnData::create(ReturnData::PARAMS_ERROR,null,'亲,您已获取!');}
  56. if ($id = self::insertGetId($data))
  57. {
  58. DB::table('bonus')->where(array('id'=>$data['bonus_id']))->decrement('num', 1);
  59. return ReturnData::create(ReturnData::SUCCESS,$id);
  60. }
  61. return ReturnData::create(ReturnData::SYSTEM_FAIL);
  62. }
  63. public static function modify($where, array $data)
  64. {
  65. if (self::where($where)->update($data))
  66. {
  67. return true;
  68. }
  69. return false;
  70. }
  71. //删除一条记录
  72. public static function remove($id)
  73. {
  74. if (!self::whereIn('id', explode(',', $id))->delete())
  75. {
  76. return false;
  77. }
  78. return true;
  79. }
  80. //商品结算时,获取优惠券列表
  81. public static function getAvailableBonusList(array $param)
  82. {
  83. extract($param);
  84. $where['user_bonus.user_id'] = $user_id;
  85. if(isset($status)){$where['bonus.status'] = 0;}
  86. $model = new UserBonus;
  87. if(isset($min_amount)){$model = $model->where('bonus.min_amount', '<=', $min_amount)->where('bonus.money', '<=', $min_amount);} //满多少使用
  88. if(isset($bonus_end_time)){$model = $model->where('bonus.end_time', '>=', date('Y-m-d H:i:s'));} //有效期
  89. $bonus_list = $model->join('bonus', 'bonus.id', '=', 'user_bonus.bonus_id')->where($where)
  90. ->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')
  91. ->orderBy('bonus.money','desc')->get();
  92. $res['list'] = $bonus_list;
  93. return $res;
  94. }
  95. }