Order.php 11 KB

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