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.

143 lines
4.5 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
  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. $data['get_time'] = time(); //优惠券获取时间
  57. if ($id = self::insertGetId($data))
  58. {
  59. DB::table('bonus')->where(array('id'=>$data['bonus_id']))->decrement('num', 1);
  60. return ReturnData::create(ReturnData::SUCCESS,$id);
  61. }
  62. return ReturnData::create(ReturnData::SYSTEM_FAIL);
  63. }
  64. public static function modify($where, array $data)
  65. {
  66. if (self::where($where)->update($data))
  67. {
  68. return true;
  69. }
  70. return false;
  71. }
  72. //删除一条记录
  73. public static function remove($id)
  74. {
  75. if (!self::whereIn('id', explode(',', $id))->delete())
  76. {
  77. return false;
  78. }
  79. return true;
  80. }
  81. //商品结算时,获取优惠券列表
  82. public static function getAvailableBonusList(array $param)
  83. {
  84. extract($param);
  85. $where['user_bonus.user_id'] = $user_id;
  86. if(isset($status)){$where['bonus.status'] = 0;}
  87. $model = new UserBonus;
  88. if(isset($min_amount)){$model = $model->where('bonus.min_amount', '<=', $min_amount)->where('bonus.money', '<=', $min_amount);} //满多少使用
  89. if(isset($bonus_end_time)){$model = $model->where('bonus.end_time', '>=', date('Y-m-d H:i:s'));} //有效期
  90. $bonus_list = $model->join('bonus', 'bonus.id', '=', 'user_bonus.bonus_id')->where($where)
  91. ->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')
  92. ->orderBy('bonus.money','desc')->get();
  93. $res['list'] = $bonus_list;
  94. return $res;
  95. }
  96. public static function getUserBonusByid(array $param)
  97. {
  98. extract($param);
  99. $where['user_bonus.user_id'] = $user_id;
  100. $where['bonus.status'] = 0;
  101. $where['user_bonus.id'] = $user_bonus_id;
  102. $model = new UserBonus;
  103. if(isset($min_amount)){$model = $model->where('bonus.min_amount', '<=', $min_amount)->where('bonus.money', '<=', $min_amount);} //满多少使用
  104. $model = $model->where('bonus.end_time', '>=', date('Y-m-d H:i:s')); //有效期
  105. $bonus = $model->join('bonus', 'bonus.id', '=', 'user_bonus.bonus_id')->where($where)
  106. ->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')
  107. ->first();
  108. return $bonus;
  109. }
  110. }