Order.php 11 KB

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