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.

190 lines
6.6 KiB

7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 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\Logic;
  3. use App\Common\ReturnData;
  4. use App\Http\Model\UserBonus;
  5. use App\Http\Requests\UserBonusRequest;
  6. use Validator;
  7. class UserBonusLogic extends BaseLogic
  8. {
  9. public function __construct()
  10. {
  11. parent::__construct();
  12. }
  13. public function getModel()
  14. {
  15. return new UserBonus();
  16. }
  17. public function getValidate($data, $scene_name)
  18. {
  19. //数据验证
  20. $validate = new UserBonusRequest();
  21. return Validator::make($data, $validate->getSceneRules($scene_name), $validate->getSceneRulesMessages());
  22. }
  23. //列表
  24. public function getList($where = array(), $order = '', $field = '*', $offset = '', $limit = '')
  25. {
  26. $res = $this->getModel()->getList($where, $order, $field, $offset, $limit);
  27. if($res['count'] > 0)
  28. {
  29. foreach($res['list'] as $k=>$v)
  30. {
  31. $res['list'][$k] = $this->getDataView($v);
  32. $res['list'][$k]->bonus = model('Bonus')->getOne(['id'=>$v->bonus_id]);
  33. }
  34. }
  35. return $res;
  36. }
  37. //分页html
  38. public function getPaginate($where = array(), $order = '', $field = '*', $limit = '')
  39. {
  40. $res = $this->getModel()->getPaginate($where, $order, $field, $limit);
  41. if($res->count() > 0)
  42. {
  43. foreach($res as $k=>$v)
  44. {
  45. $res[$k] = $this->getDataView($v);
  46. }
  47. }
  48. return $res;
  49. }
  50. //全部列表
  51. public function getAll($where = array(), $order = '', $field = '*', $limit = '')
  52. {
  53. $res = $this->getModel()->getAll($where, $order, $field, $limit);
  54. if($res)
  55. {
  56. foreach($res as $k=>$v)
  57. {
  58. $res[$k] = $this->getDataView($v);
  59. }
  60. }
  61. return $res;
  62. }
  63. //详情
  64. public function getOne($where = array(), $field = '*')
  65. {
  66. $res = $this->getModel()->getOne($where, $field);
  67. if(!$res){return false;}
  68. $res = $this->getDataView($res);
  69. $res->bonus = model('Bonus')->getOne(['id'=>$res->bonus_id]);
  70. return $res;
  71. }
  72. //用户获取优惠券
  73. public function add($data = array(), $type=0)
  74. {
  75. if(empty($data)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  76. $validator = $this->getValidate($data, 'add');
  77. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  78. $data['get_time'] = time(); //优惠券获取时间
  79. $bonus = model('Bonus')->getOne(['id'=>$data['bonus_id']]);
  80. if(!$bonus){return ReturnData::create(ReturnData::PARAMS_ERROR,null,'亲,您来晚了啦,已被抢光了');}
  81. if($bonus->num==-1 || $bonus->num>0){}else{return ReturnData::create(ReturnData::PARAMS_ERROR,null,'亲,您来晚了啦,已被抢光了');}
  82. if($this->getModel()->getOne(['bonus_id'=>$data['bonus_id'],'user_id'=>$data['user_id']])){return ReturnData::create(ReturnData::PARAMS_ERROR,null,'亲,您已获取!');}
  83. $res = $this->getModel()->add($data,$type);
  84. if($res)
  85. {
  86. if($bonus->num>0){model('Bonus')->getDb()->where(array('id'=>$data['bonus_id']))->decrement('num', 1);}
  87. return ReturnData::create(ReturnData::SUCCESS,$res);
  88. }
  89. return ReturnData::create(ReturnData::FAIL);
  90. }
  91. //修改
  92. public function edit($data, $where = array())
  93. {
  94. if(empty($data)){return ReturnData::create(ReturnData::SUCCESS);}
  95. $validator = $this->getValidate($data, 'edit');
  96. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  97. $res = $this->getModel()->edit($data,$where);
  98. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  99. return ReturnData::create(ReturnData::FAIL);
  100. }
  101. //删除
  102. public function del($where)
  103. {
  104. if(empty($where)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  105. $validator = $this->getValidate($where,'del');
  106. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  107. $res = $this->getModel()->del($where);
  108. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  109. return ReturnData::create(ReturnData::FAIL);
  110. }
  111. /**
  112. * 数据获取器
  113. * @param array $data 要转化的数据
  114. * @return array
  115. */
  116. private function getDataView($data = array())
  117. {
  118. return getDataAttr($this->getModel(),$data);
  119. }
  120. /**
  121. * 商品结算时,获取优惠券列表
  122. * @param float $param['min_amount'] 最小金额可以用的优惠券
  123. * @return array
  124. */
  125. public function getAvailableBonusList(array $param)
  126. {
  127. $where['user_bonus.user_id'] = $param['user_id'];
  128. if(isset($status)){$where['bonus.status'] = 0;}
  129. $model = model('UserBonus')->getDb();
  130. if(isset($param['min_amount'])){$model = $model->where('bonus.min_amount', '<=', $param['min_amount'])->where('bonus.money', '<=', $param['min_amount']);} //满多少使用
  131. $model = $model->where('bonus.end_time', '>=', date('Y-m-d H:i:s')); //有效期
  132. $bonus_list = $model->join('bonus', 'bonus.id', '=', 'user_bonus.bonus_id')->where($where)
  133. ->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')
  134. ->orderBy('bonus.money','desc')->get();
  135. $res['list'] = $bonus_list;
  136. return $res;
  137. }
  138. public function getUserBonusByid(array $param)
  139. {
  140. $where['user_bonus.user_id'] = $param['user_id'];
  141. $where['bonus.status'] = 0;
  142. $where['user_bonus.id'] = $param['user_bonus_id'];
  143. $model = model('UserBonus')->getDb();
  144. if(isset($param['min_amount'])){$model = $model->where('bonus.min_amount', '<=', $param['min_amount'])->where('bonus.money', '<=', $param['min_amount']);} //满多少使用
  145. $model = $model->where('bonus.end_time', '>=', date('Y-m-d H:i:s')); //有效期
  146. $bonus = $model->join('bonus', 'bonus.id', '=', 'user_bonus.bonus_id')->where($where)
  147. ->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')
  148. ->first();
  149. return $bonus;
  150. }
  151. }