ManyController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. namespace app\api\controller\many;
  3. use app\models\many\AuctionPay;
  4. use app\models\many\Many;
  5. use app\models\many\ManyDiscipline;
  6. use app\models\many\ManyGreen;
  7. use app\models\many\ManyOrder;
  8. use app\models\store\StoreOrder;
  9. use app\models\user\User;
  10. use app\models\user\UserBill;
  11. use app\Request;
  12. use crmeb\services\GroupDataService;
  13. use crmeb\services\QrcodeService;
  14. use crmeb\services\SystemConfigService;
  15. use crmeb\services\UtilService;
  16. use crmeb\services\upload\Upload;
  17. use think\facade\Db;
  18. use think\facade\Validate;
  19. /**
  20. * 账单类
  21. * Class UserBillController
  22. * @package app\api\controller\user
  23. */
  24. class ManyController
  25. {
  26. /**
  27. * 众筹列表
  28. * @param Request $request
  29. * @return mixed
  30. */
  31. public function many(Request $request)
  32. {
  33. $where = UtilService::getMore([
  34. ['page', 1],
  35. ['limit', 10],
  36. ['name']
  37. ]);
  38. $list = \app\models\many\Many::list($where);
  39. return app('json')->success($list);
  40. }
  41. /**
  42. * 众筹详情
  43. * @return mixed
  44. * @throws \think\db\exception\DataNotFoundException
  45. * @throws \think\db\exception\DbException
  46. * @throws \think\db\exception\ModelNotFoundException
  47. */
  48. public function details()
  49. {
  50. $where = UtilService::getMore([
  51. ['id'],
  52. ]);
  53. $many = Many::find($where['id']);
  54. if (!$many) return app('json')->fail('没有该场次');
  55. return app('json')->success($many->toArray());
  56. }
  57. /**
  58. * 投注
  59. * @param Request $request
  60. * @return mixed
  61. * @throws \think\db\exception\DataNotFoundException
  62. * @throws \think\db\exception\DbException
  63. * @throws \think\db\exception\ModelNotFoundException
  64. */
  65. public function purchase(Request $request)
  66. {
  67. $data = UtilService::postMore([
  68. ['id'],
  69. ['price']
  70. ]);
  71. $many = Many::where('id', $data['id'])->find();
  72. $surplus = $this->surplus($data['id'], $request->uid(),1);
  73. $user = User::where('uid', $request->uid())->find();
  74. if ($data['price'] < 10) return app('json')->fail('最小投注数10');
  75. if (!$many) return app('json')->fail('场次不存在');
  76. if ($many['number'] >= $many['money']) return app('json')->fail('已完成无法投注');
  77. if ($many['status'] == 0) return app('json')->fail('未开启');
  78. if ($many['end_time'] < time()) return app('json')->fail('已结束');
  79. if (($many['number']+$data['price']) > $many['money']) return app('json')->fail('还能最大投注'.($many['money']-$many['number']));
  80. if ($data['price'] > $many['single']) return app('json')->fail('单次最大可打怪'.$many['single']);
  81. if ($surplus < $data['price']) return app('json')->fail('超过最大可投注额度');
  82. Db::startTrans();
  83. if ($many['add_time'] > time()){
  84. $green = ManyGreen::where('uid', $user['uid'])->where('status', 0)->find();
  85. if (!$green) return app('json')->fail('你无法提前打怪');
  86. $green['status'] = 1;
  87. }
  88. $integral = $user['white_integral'] + $user['purple_integral'];// 白积分加紫积分的总积分
  89. if ($integral < $data['price']) return app('json')->fail('积分额度不够');
  90. try {
  91. if ($user['white_integral'] < $data['price']){
  92. $white = $user['white_integral'];
  93. if ($white > 0){
  94. User::where('uid', $request->uid())->dec('white_integral',$white)->update();
  95. UserBill::expend('扣除红积分', $user['uid'], 'white_integral', 'bet_white_integral', $white, 0,0,'使用红积分,参与打怪-》'.$many['name'].'期数-》'.$many['stage'].'-成功');
  96. }
  97. $user['purple_integral'] -= $data['price'] - $white;
  98. User::where('uid', $request->uid())->dec('purple_integral', $data['price']-$white)->update();
  99. UserBill::expend('扣除阳光积分', $user['uid'], 'purple_integral', 'bet_purple_integral', $data['price']-$white, 0,$user['purple_integral'],'使用阳光积分,参与打怪-》'.$many['name'].'期数-》'.$many['stage'].'-成功');
  100. }else{
  101. $user['white_integral'] -= $data['price'];
  102. User::where('uid', $request->uid())->dec('white_integral', $data['price'])->update();
  103. UserBill::expend('扣除红积分', $user['uid'], 'white_integral', 'bet_white_integral', $data['price'], 0,$user['white_integral'],'使用红积分,参与打怪-》'.$many['name'].'期数-》'.$many['stage'].'-成功');
  104. }
  105. $many['number'] += $data['price'];
  106. if ($many['number'] >= $many['money']){
  107. $many['suc'] = 1;// 众筹成功
  108. $many['status'] = 0;// 众筹成功
  109. ManyDiscipline::create(['many_id' => $many['id'], 'stage' => $many['stage'], 'status' => 1]);// 成功记录
  110. if ($many['stage'] >= 4){
  111. // 期数如果大于等于4
  112. $stage = $many['stage'] - 3;
  113. ManyOrder::where('many_id', $many['id'])->where('stage', $stage)->update(['is_return' => 1]);// 成功后添加返还状态
  114. }
  115. }
  116. ManyOrder::create([
  117. 'order_id' => StoreOrder::getNewOrderId(),
  118. 'many_id' => $many['id'],
  119. 'uid' => $user['uid'],
  120. 'stage' => $many['stage'],
  121. 'price' => $data['price'],
  122. ]);
  123. $many->save();
  124. if ($many['add_time'] > time()) $green->save();
  125. event('Many', ['many' => $many, 'data' => $data, 'uid' => $request->uid()]);
  126. Db::commit();
  127. return app('json')->success('投注成功');
  128. } catch (\Exception $e) {
  129. Db::rollback();
  130. return app('json')->fail($e->getMessage());
  131. }
  132. }
  133. /**
  134. * 还有多少可投注
  135. * @param $id
  136. * @param $type
  137. * @return float|mixed
  138. * @throws \think\db\exception\DataNotFoundException
  139. * @throws \think\db\exception\DbException
  140. * @throws \think\db\exception\ModelNotFoundException
  141. */
  142. public function surplus($id, $uid,$type = 0)
  143. {
  144. if ($type > 0){
  145. $many = Many::where('id', $id)->find();
  146. if (!$many) return app('json')->fail('场次不存在');
  147. $many_order = ManyOrder::where('many_id', $id)->where('uid', $uid)->where('stage', $many['stage'])->sum('price');
  148. $price = ($many['upper_limit'] - $many_order); // 还可以投注额度
  149. return $price;
  150. }else{
  151. $many = Many::where('id', $id)->find();
  152. if (!$many) return app('json')->fail('场次不存在');
  153. $many_order = ManyOrder::where('many_id', $id)->where('uid', $uid)->where('stage', $many['stage'])->sum('price');
  154. $data['price'] = ($many['upper_limit'] - $many_order); // 还可以投注额度
  155. return app('json')->fail($data);
  156. }
  157. }
  158. /**
  159. * 添加收款方式
  160. * @param Request $request
  161. * @return void
  162. */
  163. public function pay(Request $request)
  164. {
  165. $data = UtilService::postMore([
  166. ['payment'],
  167. ['image'],
  168. ['bank'],
  169. ['name'],
  170. ['type'],
  171. ['phone'],
  172. ['bank_name']
  173. ], $request);
  174. if (!$data['type']) return app('json')->fail('数据传入错误');
  175. $data['uid'] =$request->uid();
  176. $model = new AuctionPay();
  177. $pay = $model->where([['uid', '=', $request->uid()], ['type', '=', $data['type']]])->find();
  178. $res = Validate::rule([
  179. 'phone' => 'mobile'
  180. ]);
  181. $res->message([
  182. 'phone.mobile' => '请填写正确手机格式'
  183. ]);
  184. if (!$res->check($data)){
  185. return app('json')->fail($res->getError());
  186. }
  187. if (!empty($pay)){
  188. if ($data['type'] == 1 ){
  189. // 微信收款方式
  190. if (!$data['payment']) return app('json')->fail('微信账号不能为空');
  191. if (!$data['image']) return app('json')->fail('二维码不能为空');
  192. if (!$data['name']) return app('json')->fail('姓名不能为空');
  193. $pay['payment'] = $data['payment'];
  194. $pay['image'] = $data['image'];
  195. $pay['name'] = $data['name'];
  196. $pay['phone'] = $data['phone'];
  197. }elseif ($data['type'] == 2){
  198. // 支付宝收款方式
  199. if (!$data['payment']) return app('json')->fail('支付宝账号不能为空');
  200. if (!$data['name']) return app('json')->fail('姓名不能为空');
  201. $pay['payment'] = $data['payment'];
  202. $pay['name'] = $data['name'];
  203. $pay['phone'] = $data['phone'];
  204. }elseif ($data['type'] == 3){
  205. // 银行卡收款方式
  206. if (!$data['payment']) return app('json')->fail('银行卡号不能为空');
  207. if (!$data['name']) return app('json')->fail('姓名不能为空');
  208. if (!$data['bank']) return app('json')->fail('开户行不能为空');
  209. if (!$data['bank_name']) return app('json')->fail('开户支行不能为空');
  210. if (!$data['phone']) return app('json')->fail('请填写手机号');
  211. $pay['payment'] = $data['payment'];
  212. $pay['bank'] = $data['bank'];
  213. $pay['bank_name'] = $data['bank_name'];
  214. $pay['phone'] = $data['phone'];
  215. $pay['name'] = $data['name'];
  216. }
  217. $res = $pay->save();
  218. if ($res) return app('json')->successful('修改成功');
  219. return app('json')->fail('修改失败');
  220. }else{
  221. if ($data['type'] == 1 ){
  222. // 微信收款方式
  223. if (!$data['payment']) return app('json')->fail('微信账号不能为空');
  224. if (!$data['image']) return app('json')->fail('二维码不能为空');
  225. if (!$data['name']) return app('json')->fail('姓名不能为空');
  226. if (!$data['phone']) return app('json')->fail('请填写手机号');
  227. }elseif ($data['type'] == 2){
  228. // 支付宝收款方式
  229. if (!$data['payment']) return app('json')->fail('支付宝账号不能为空');
  230. if (!$data['name']) return app('json')->fail('姓名不能为空');
  231. if (!$data['phone']) return app('json')->fail('请填写手机号');
  232. }elseif ($data['type'] == 3){
  233. // 银行卡收款方式
  234. if (!$data['payment']) return app('json')->fail('银行卡号不能为空');
  235. if (!$data['name']) return app('json')->fail('姓名不能为空');
  236. if (!$data['bank']) return app('json')->fail('开户行不能为空');
  237. if (!$data['bank_name']) return app('json')->fail('开户支行不能为空');
  238. if (!$data['phone']) return app('json')->fail('请填写手机号');
  239. }
  240. $res = $model->save($data);
  241. if ($res) return app('json')->successful('添加成功');
  242. return app('json')->fail('添加失败');
  243. }
  244. }
  245. /**
  246. * 收款方式详情
  247. * @param Request $request
  248. * @return mixed
  249. * @throws \think\db\exception\DataNotFoundException
  250. * @throws \think\db\exception\DbException
  251. * @throws \think\db\exception\ModelNotFoundException
  252. */
  253. public function pay_list(Request $request)
  254. {
  255. $model = new AuctionPay();
  256. $list = $model->where('uid', $request->uid())->select();
  257. $list = empty($list)? []: $list->toArray();
  258. $data['wx'] = [];
  259. $data['zfb'] = [];
  260. $data['bank'] = [];
  261. foreach ($list as $k => $v){
  262. if ($v['type'] == 1){
  263. $data['wx'] = $v;
  264. }elseif ($v['type'] == 2){
  265. $data['zfb'] = $v;
  266. }elseif ($v['type'] == 3){
  267. $data['bank'] = $v;
  268. }
  269. }
  270. return app('json')->successful($data);
  271. }
  272. }