Riddle.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php namespace App\Http\Controllers\Api\Lottery;
  2. use App\Http\Controllers\Api\Api;
  3. use App\Models\Lottery\Riddle as Model;
  4. use App\Models\Lottery\RiddleUsable;
  5. use App\Models\Custom;
  6. use App\Models\CustomCoupon;
  7. use App\Models\CustomScore;
  8. use App\Models\CustomAmount;
  9. use App\Models\Lottery\RiddleRecord;
  10. use App\Models\Lottery\RiddleReward as RiddleReward;
  11. use Illuminate\Support\Facades\DB;
  12. /**
  13. * 积分抽奖
  14. *
  15. * @author 刘相欣
  16. *
  17. * */
  18. class Riddle extends Api{
  19. /**
  20. * 获取抽奖配置 /api/lottery_riddle/get_detail
  21. *
  22. *
  23. * */
  24. public function get_detail(Model $Model,Custom $Custom,RiddleReward $RiddleReward,RiddleUsable $RiddleUsable){
  25. // 接口验签
  26. // $this->verify_sign();
  27. // 检查登录
  28. $uid = $this->checkLogin();
  29. // 获取客户信息
  30. $custom = $Custom->getOne($uid);
  31. // 如果存在的话
  32. if( !$custom ) return json_send(['code'=>'no_login','msg'=>'请登录','data'=>['error'=>'无对应客户']]);
  33. // 接收参数
  34. $id = request('id',0);
  35. // 获取活动
  36. $data = $Model->getOne($id);
  37. // 如果存在的话
  38. if( !$data ) return json_send(['code'=>'error','msg'=>'暂无活动','data'=>$data]);
  39. // 奖品
  40. $reward = $RiddleReward->getListByLottery($data['id']);
  41. // 活动暂无奖品
  42. if( !$reward ) return json_send(['code'=>'error','msg'=>'活动暂未配置奖品','data'=>$data]);
  43. // logo
  44. $data['logo'] = $data['logo'] ? path_compat($data['logo']) : '';
  45. // 通过活动ID,查询奖品
  46. $data['reward_list'] = [];
  47. // 奖品数据
  48. foreach ($reward as $value) {
  49. // 奖项
  50. $data['reward_list'][] = ['id'=>$value['id'],'name'=>$value['reward_name'],'img'=>$value['reward_thumb'],'reward_type'=>$value['reward_type']];
  51. }
  52. // 查询用户可用抽奖次数
  53. $number = $RiddleUsable->query()->where([['custom_uid','=',$uid],['lottery_id','=',$id]])->value('number');
  54. // 最少为0,避免显示异常
  55. $data['number'] = $number < 0 ? 0 : $number;
  56. // 时间处理
  57. $data['start_date'] = date('Y/m/d H:i',$data['start_time']);
  58. // 时间处理
  59. $data['end_date'] = date('Y/m/d H:i',$data['end_time']);
  60. // 返回结果
  61. return json_send(['code'=>'success','msg'=>'获取成功','data'=>$data]);
  62. }
  63. /**
  64. * 抽奖 /api/lottery_riddle/get_reward
  65. *
  66. * */
  67. public function get_reward(Model $Model,Custom $Custom,RiddleRecord $RiddleRecord,RiddleReward $RiddleReward,CustomCoupon $CustomCoupon,OrderRecord $OrderRecord,CustomScore $CustomScore,RiddleUsable $RiddleUsable,CustomAmount $CustomAmount){
  68. // 接口验签
  69. // $this->verify_sign();
  70. // 检查登录
  71. $uid = $this->checkLogin();
  72. // 获取活动
  73. $lotteryId = request('lottery_id',0);
  74. // 如果存在的话
  75. if( !$lotteryId ) return json_send(['code'=>'error','msg'=>'请选择参与的活动','data'=>['error'=>"抽奖活动ID有误"]]);
  76. // 获取客户信息
  77. $custom = $Custom->getOne($uid);
  78. // 如果存在的话
  79. if( !$custom ) return json_send(['code'=>'no_login','msg'=>'请登录','data'=>['error'=>'无对应客户']]);
  80. // 通过活动ID,查询奖品
  81. $data = $Model->getOne($lotteryId);
  82. // 如果存在的话
  83. if( !$data ) return json_send(['code'=>'error','msg'=>'活动不存在或未开始','data'=>$data]);
  84. // 活动时间判断
  85. if( $data['start_time'] > time() ) return json_send(['code'=>'error','msg'=>'活动暂未开始','data'=>$data]);
  86. // 活动时间判断
  87. if( $data['end_time'] < time() ) return json_send(['code'=>'error','msg'=>'活动已结束','data'=>$data]);
  88. // 奖品
  89. $reward = $RiddleReward->getListByLottery($data['id']);
  90. // 活动暂无奖品
  91. if( !$reward ) return json_send(['code'=>'error','msg'=>'活动暂未配置奖品','data'=>$data]);
  92. // 查询用户可用抽奖次数
  93. $usable = $RiddleUsable->query()->where([['custom_uid','=',$uid],['lottery_id','=',$lotteryId]])->first();
  94. if (!$usable) return json_send(['code'=>'error','msg'=>'抽奖次数已用完','data'=>['error'=>'抽奖次数已用完']]);
  95. // 最少为0,避免显示异常
  96. $data['join_num'] = $usable['number'] < 0 ? 0 : $usable['number'];
  97. // 如果次数不够
  98. if( $data['join_num'] <= 0 ) return json_send(['code'=>'error','msg'=>'抽奖次数已用完','data'=>['error'=>'抽奖次数已用完']]);
  99. // 组合数据,写入订单表,子表
  100. DB::beginTransaction();
  101. try{
  102. // 扣减次数
  103. $result = $RiddleUsable->query()->where(['lottery_id'=>$data['id'],'custom_uid'=>$uid])->update(['number'=>$usable['number']-1,'use_number'=>$usable['use_number']+1]);
  104. // 如果积分扣减失败
  105. if( !$result ) {
  106. // 回退数据
  107. DB::rollBack();
  108. return json_send(['code'=>'error','msg'=>'抽奖次数扣减失败','data'=>['error'=>'抽奖次数扣减失败']]);
  109. }
  110. // 查询条件
  111. $map = [['insert_time','>=',$data['start_time']],['insert_time','<=',$data['end_time']]];
  112. // 判断周期
  113. if( !empty($data['freq']) ) {
  114. if( $data['freq'] == 1 ) $map = [['insert_time','>=',now()->startOfDay()->getTimestamp()],['insert_time','<=',now()->endOfDay()->getTimestamp()]];
  115. if( $data['freq'] == 2 ) $map = [['insert_time','>=',now()->startOfWeek()->getTimestamp()],['insert_time','<=',now()->endOfWeek()->getTimestamp()]];
  116. if( $data['freq'] == 3 ) $map = [['insert_time','>=',now()->startOfMonth()->getTimestamp()],['insert_time','<=',now()->endOfMonth()->getTimestamp()]];
  117. }
  118. // 限制中奖则获取中奖次数
  119. $rewarTotal = $data['max_reward'] ? $RiddleRecord->query()->where(['lottery_id'=>$data['id'],'custom_uid'=>$uid])->where($map)->count() : 0;
  120. // 中奖上限以后不再中奖
  121. $rewardIndex = ($data['max_reward'] && $rewarTotal >= $data['max_reward']) ? 0 : $RiddleReward->getRewardResult($reward);
  122. // 如果中奖,下标不是0
  123. if( $rewardIndex ) {
  124. // 获取奖品
  125. $rewardResult = $reward[$rewardIndex];
  126. // 奖品记录ID
  127. if( !empty($rewardResult['id']) ){
  128. // 记录,默认状态为1,进行中
  129. $record = ['custom_uid'=>$uid,'lottery_id'=>$lotteryId,'reward_id'=>$rewardResult['id'],'reward_name'=>$rewardResult['reward_name'],'status'=>1];
  130. // 如果是积分
  131. if( $rewardResult['reward_type'] == 1 ){
  132. // 积分大于0
  133. if( $rewardResult['reward_info'] > 0 ){
  134. // 积分发放
  135. $result = $CustomScore->trade($uid,$lotteryId,$rewardResult['reward_info'],7,4);
  136. // 发放失败,改为未中奖
  137. if( isset($result['error']) ) $rewardIndex = 0;
  138. // 发放成功,状态为已完成
  139. $record['status']= 8;
  140. }
  141. }
  142. // 优惠券,先进行发放
  143. if( $rewardResult['reward_type'] == 2 ){
  144. // 优惠券存在ID
  145. if( $rewardResult['reward_info'] > 0 ){
  146. // 积分给与
  147. $result = $CustomCoupon->giveCoupon($rewardResult['reward_info'],$uid);
  148. // 发放失败,改为未中奖
  149. if( !$result ) $rewardIndex = 0;
  150. // 发放成功,状态为已完成
  151. $record['status']= 8;
  152. }
  153. }
  154. // 红包
  155. if( $rewardResult['reward_type'] == 3 ){
  156. // 积分大于0
  157. if( $rewardResult['reward_info'] > 0 ){
  158. // 积分发放
  159. $result = $CustomAmount->trade($uid,$lotteryId,$rewardResult['reward_info'],4,1,'答题抽奖');
  160. // 发放失败,改为未中奖
  161. if( isset($result['error']) ) $rewardIndex = 0;
  162. // 发放成功,状态为已完成
  163. $record['status']= 8;
  164. }
  165. }
  166. // 如果是实物,要求填写地址,状态设置为0
  167. if( $rewardResult['reward_type'] == 5 ) $record['status'] = 0;
  168. // 中奖才进行记录
  169. if( $rewardIndex ) {
  170. // 奖品数量减少
  171. $RiddleReward->edit($rewardResult['id'],['reward_total'=>DB::raw('reward_total+-1')]);
  172. // 扣减数量
  173. $RiddleRecord->add($record);
  174. }
  175. }
  176. }
  177. // 提交事务
  178. DB::commit();
  179. // 通过活动ID,查询奖品
  180. $rewardList = [];
  181. // 奖品数据
  182. foreach ($reward as $value) {
  183. $rewardList[] = ['id'=>$value['id'],'name'=>$value['reward_name'],'img'=>$value['reward_thumb'],'reward_type'=>$value['reward_type']];
  184. }
  185. // 返回结果
  186. return json_send(['code'=>'success','msg'=>'抽奖成功','data'=>['reward_list'=>$rewardList,'reward_index'=>$rewardIndex,'join_num'=>$data['join_num']-1]]);
  187. // 异常处理
  188. } catch (\Throwable $th) {
  189. // 回退数据
  190. DB::rollBack();
  191. // 下单失败提示
  192. return json_send(['code'=>'error','msg'=>'抽奖失败,请重试','data'=>['error'=>$th->getMessage().$th->getLine()]]);
  193. }
  194. }
  195. }