Recruitment.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php namespace App\Http\Controllers\Api\Lottery;
  2. use App\Http\Controllers\Api\Api;
  3. use App\Models\Lottery\Recruitment as Model;
  4. use App\Models\Lottery\RecruitmentUsable;
  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\RecruitmentRecord;
  10. use App\Models\Lottery\RecruitmentReward as RecruitmentReward;
  11. use Illuminate\Support\Facades\DB;
  12. use App\Models\WeiBan\Tags as WeiBanTags;
  13. /**
  14. * 积分抽奖
  15. *
  16. * @author 刘相欣
  17. *
  18. * */
  19. class Recruitment extends Api{
  20. /**
  21. * 获取抽奖配置 /api/lottery_recruitment/get_detail
  22. *
  23. *
  24. * */
  25. public function get_detail(Model $Model,Custom $Custom,RecruitmentReward $RecruitmentReward,RecruitmentUsable $RecruitmentUsable,WeiBanTags $WeiBanTags){
  26. // 接口验签
  27. // $this->verify_sign();
  28. // 检查登录
  29. $uid = $this->checkLogin();
  30. // 获取客户信息
  31. $custom = $Custom->getOne($uid);
  32. // 如果存在的话
  33. if( !$custom ) return json_send(['code'=>'no_login','msg'=>'请登录','data'=>['error'=>'无对应客户']]);
  34. // 接收参数
  35. $id = request('id',0);
  36. // 获取活动
  37. $data = $Model->getOne($id);
  38. // 如果存在的话
  39. if( !$data ) return json_send(['code'=>'error','msg'=>'暂无活动','data'=>$data]);
  40. // 默认可以参加活动
  41. $data['allow_join'] = 1;
  42. // 判断是不是可以参与
  43. if( $data['tag_scope'] ) {
  44. // 解析数组
  45. $data['tag_scope'] = explode(',',$data['tag_scope']);
  46. // 查询用户标签
  47. $tags = $WeiBanTags->getListByWeibanExtid($custom['weiban_extid']);
  48. // 标签范围限定时,默认不能参与
  49. $data['allow_join'] = 0;
  50. // 判断标签是不是存在
  51. foreach ($tags as $value) {
  52. // 标签范围内,允许参加
  53. if( in_array($value['name'],$data['tag_scope']) ) $data['allow_join'] = 1;
  54. }
  55. // 如果不能参与
  56. if( !$data['allow_join'] ) return json_send(['code'=>'error','msg'=>'账号不在标签范围','data'=>['error'=>'不在标签范围内']]);
  57. }
  58. // 判断是不是可以参与
  59. if( $data['city_ids'] ) {
  60. // 解析数组
  61. $data['city_ids'] = explode(',',$data['city_ids']);
  62. // 如果不在城市范围
  63. if( !in_array($custom['city_id'],$data['city_ids']) ) $data['allow_join'] = 0;
  64. // 如果不能参与
  65. if( !$data['allow_join'] ) return json_send(['code'=>'error','msg'=>'账号不在活动城市','data'=>['error'=>'账号不在活动城市']]);
  66. }
  67. // 奖品
  68. $reward = $RecruitmentReward->getListByLottery($data['id']);
  69. // 活动暂无奖品
  70. if( !$reward ) return json_send(['code'=>'error','msg'=>'活动暂未配置奖品','data'=>$data]);
  71. // logo
  72. $data['logo'] = $data['logo'] ? path_compat($data['logo']) : '';
  73. // 通过活动ID,查询奖品
  74. $data['reward_list'] = [];
  75. // 奖品数据
  76. foreach ($reward as $value) {
  77. // 奖项
  78. $data['reward_list'][] = ['id'=>$value['id'],'name'=>$value['reward_name'],'img'=>$value['reward_thumb'],'reward_type'=>$value['reward_type']];
  79. }
  80. // 查询用户可用抽奖次数
  81. $number = $RecruitmentUsable->query()->where([['custom_uid','=',$uid],['lottery_id','=',$id]])->value('number');
  82. // 最少为0,避免显示异常
  83. $data['number'] = $number < 0 ? 0 : $number;
  84. // 时间处理
  85. $data['start_date'] = date('Y/m/d H:i',$data['start_time']);
  86. // 时间处理
  87. $data['end_date'] = date('Y/m/d H:i',$data['end_time']);
  88. // 返回结果
  89. return json_send(['code'=>'success','msg'=>'获取成功','data'=>$data]);
  90. }
  91. /**
  92. * 抽奖 /api/lottery_recruitment/get_reward
  93. *
  94. * */
  95. public function get_reward(Model $Model,Custom $Custom,RecruitmentRecord $RecruitmentRecord,RecruitmentReward $RecruitmentReward,CustomCoupon $CustomCoupon,CustomScore $CustomScore,CustomAmount $CustomAmount,WeiBanTags $WeiBanTags){
  96. // 接口验签
  97. // $this->verify_sign();
  98. // 检查登录
  99. $uid = $this->checkLogin();
  100. // 获取活动
  101. $lotteryId = request('lottery_id',0);
  102. // 如果存在的话
  103. if( !$lotteryId ) return json_send(['code'=>'error','msg'=>'请选择参与的活动','data'=>['error'=>"抽奖活动ID有误"]]);
  104. // 获取客户信息
  105. $custom = $Custom->getOne($uid);
  106. // 如果存在的话
  107. if( !$custom ) return json_send(['code'=>'no_login','msg'=>'请登录','data'=>['error'=>'无对应客户']]);
  108. // 通过活动ID,查询奖品
  109. $data = $Model->getOne($lotteryId);
  110. // 如果存在的话
  111. if( !$data ) return json_send(['code'=>'error','msg'=>'活动不存在或未开始','data'=>$data]);
  112. // 活动时间判断
  113. if( $data['start_time'] > time() ) return json_send(['code'=>'error','msg'=>'活动暂未开始','data'=>$data]);
  114. // 活动时间判断
  115. if( $data['end_time'] < time() ) return json_send(['code'=>'error','msg'=>'活动已结束','data'=>$data]);
  116. // 默认可以参加活动
  117. $data['allow_join'] = 1;
  118. // 判断是不是可以参与
  119. if( $data['tag_scope'] ) {
  120. // 解析数组
  121. $data['tag_scope'] = explode(',',$data['tag_scope']);
  122. // 查询用户标签
  123. $tags = $WeiBanTags->getListByWeibanExtid($custom['weiban_extid']);
  124. // 标签范围限定时,默认不能参与
  125. $data['allow_join'] = 0;
  126. // 判断标签是不是存在
  127. foreach ($tags as $value) {
  128. // 标签范围内,允许参加
  129. if( in_array($value['name'],$data['tag_scope']) ) $data['allow_join'] = 1;
  130. }
  131. // 如果不能参与
  132. if( !$data['allow_join'] ) return json_send(['code'=>'error','msg'=>'不符合参与条件','data'=>['error'=>'不符合参与条件']]);
  133. }
  134. // 判断是不是可以参与
  135. if( $data['city_ids'] ) {
  136. // 解析数组
  137. $data['city_ids'] = explode(',',$data['city_ids']);
  138. // 如果不在城市范围
  139. if( !in_array($custom['city_id'],$data['city_ids']) ) $data['allow_join'] = 0;
  140. // 如果不能参与
  141. if( !$data['allow_join'] ) return json_send(['code'=>'error','msg'=>'账号不在活动城市','data'=>['error'=>'账号不在活动城市']]);
  142. }
  143. // 奖品
  144. $reward = $RecruitmentReward->getListByLottery($data['id']);
  145. // 活动暂无奖品
  146. if( !$reward ) return json_send(['code'=>'error','msg'=>'活动暂未配置奖品','data'=>$data]);
  147. // 查询用户已参与次数
  148. $data['join_num'] = (int)$RecruitmentRecord->query()->where([['custom_uid','=',$uid],['lottery_id','=',$lotteryId]])->count();
  149. // 计算剩余次数
  150. $data['join_num'] = $data['lucky_num'] - $data['join_num'];
  151. // 如果次数不够
  152. if( $data['join_num'] <= 0 ) return json_send(['code'=>'error','msg'=>'抽奖次数已用完','data'=>['error'=>'抽奖次数已用完']]);
  153. // 抽奖记录
  154. $record = ['custom_uid'=>$uid,'lottery_id'=>$lotteryId,'reward_id'=>0,'reward_name'=>0,'status'=>1];
  155. // 开启事务
  156. DB::beginTransaction();
  157. try{
  158. // 查询条件
  159. $map = [];
  160. // 判断周期
  161. if( !empty($data['freq']) ) {
  162. if( $data['freq'] == 1 ) $map = [['insert_time','>=',now()->startOfDay()->getTimestamp()],['insert_time','<=',now()->endOfDay()->getTimestamp()]];
  163. if( $data['freq'] == 2 ) $map = [['insert_time','>=',now()->startOfWeek()->getTimestamp()],['insert_time','<=',now()->endOfWeek()->getTimestamp()]];
  164. if( $data['freq'] == 3 ) $map = [['insert_time','>=',now()->startOfMonth()->getTimestamp()],['insert_time','<=',now()->endOfMonth()->getTimestamp()]];
  165. }
  166. // 是否限制中奖次数字段,没有默认中奖一次
  167. $data['max_reward'] = isset($data['max_reward']) ? 1 : $data['max_reward'];
  168. // 限制中奖则获取中奖次数
  169. $rewarTotal = $data['max_reward'] ? $RecruitmentRecord->query()->where([['lottery_id','=',$data['id']],['custom_uid','=',$uid],['reward_id','>',0]])->where($map)->count() : 0;
  170. // 中奖上限以后不再中奖
  171. $rewardIndex = ($data['max_reward'] && $rewarTotal >= $data['max_reward']) ? 0 : $RecruitmentReward->getRewardResult($reward);
  172. // 如果中奖,下标不是0
  173. if( $rewardIndex ) {
  174. // 获取奖品
  175. $rewardResult = $reward[$rewardIndex];
  176. // 奖品记录ID
  177. if( !empty($rewardResult['id']) ){
  178. // 如果是积分
  179. if( $rewardResult['reward_type'] == 1 ){
  180. // 积分大于0
  181. if( $rewardResult['reward_info'] > 0 ){
  182. // 积分发放
  183. $result = $CustomScore->trade($uid,$lotteryId,$rewardResult['reward_info'],7,5);
  184. // 发放失败,改为未中奖
  185. if( isset($result['error']) ) $rewardIndex = 0;
  186. // 发放成功,状态为已完成
  187. $record['status']= 8;
  188. }
  189. }
  190. // 优惠券,先进行发放
  191. if( $rewardResult['reward_type'] == 2 ){
  192. // 优惠券存在ID
  193. if( $rewardResult['reward_info'] > 0 ){
  194. // 积分给与
  195. $result = $CustomCoupon->giveCoupon($rewardResult['reward_info'],$uid);
  196. // 发放失败,改为未中奖
  197. if( !$result ) $rewardIndex = 0;
  198. // 发放成功,状态为已完成
  199. $record['status']= 8;
  200. }
  201. }
  202. // 红包
  203. if( $rewardResult['reward_type'] == 3 ){
  204. // 积分大于0
  205. if( $rewardResult['reward_info'] > 0 ){
  206. // 积分发放
  207. $result = $CustomAmount->trade($uid,$lotteryId,$rewardResult['reward_info'],4,1,'灯谜抽奖');
  208. // 发放失败,改为未中奖
  209. if( isset($result['error']) ) $rewardIndex = 0;
  210. // 发放成功,状态为已完成
  211. $record['status']= 8;
  212. }
  213. }
  214. // 如果是实物,要求填写地址,状态设置为0
  215. if( $rewardResult['reward_type'] == 5 ) $record['status'] = 0;
  216. }
  217. }
  218. // 中奖记录奖品
  219. if( $rewardIndex ) {
  220. // 记录,默认状态为1,进行中
  221. $record['reward_id'] = $reward[$rewardIndex]['id'];
  222. $record['reward_name'] = $reward[$rewardIndex]['reward_name'];
  223. // 扣减次数
  224. $RecruitmentReward->edit($rewardResult['id'],['reward_total'=>DB::raw('reward_total+-1')]);
  225. }
  226. // 写入记录
  227. $result = $RecruitmentRecord->add($record);
  228. // 写入失败
  229. if( !$result ) {
  230. // 回退数据
  231. DB::rollBack();
  232. // 提示
  233. return json_send(['code'=>'error','msg'=>'抽奖记录失败,请重试']);
  234. }
  235. // 提交事务
  236. DB::commit();
  237. // 通过活动ID,查询奖品
  238. $rewardList = [];
  239. // 奖品数据
  240. foreach ($reward as $value) {
  241. $rewardList[] = ['id'=>$value['id'],'name'=>$value['reward_name'],'img'=>$value['reward_thumb'],'reward_type'=>$value['reward_type']];
  242. }
  243. // 返回结果
  244. return json_send(['code'=>'success','msg'=>'抽奖成功','data'=>['reward_list'=>$rewardList,'reward_index'=>$rewardIndex,'join_num'=>$data['join_num']-1]]);
  245. // 异常处理
  246. } catch (\Throwable $th) {
  247. // 回退数据
  248. DB::rollBack();
  249. // 下单失败提示
  250. return json_send(['code'=>'error','msg'=>'抽奖失败,请重试','data'=>['error'=>$th->getMessage().$th->getLine()]]);
  251. }
  252. }
  253. }