ManyController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. Db::startTrans();
  72. $many = Many::where('id', $data['id'])->lock(true)->find();
  73. $surplus = $this->surplus($data['id'], $request->uid(),1);
  74. $user = User::where('uid', $request->uid())->find();
  75. if (!$many) return app('json')->fail('场次不存在');
  76. if(!preg_match("/^[1-9][0-9]*$/" ,$data['price'])) return app('json')->fail('请投注整数');
  77. if ($many['number'] >= $many['money']) return app('json')->fail('已完成无法种树');
  78. if ($many['status'] == 0) return app('json')->fail('未开启');
  79. if ($many['end_time'] < time()) return app('json')->fail('已结束');
  80. if (($many['number']+$data['price']) > $many['money']) return app('json')->fail('还能最大种树'.($many['money']-$many['number']));
  81. if ($data['price'] > $many['single']) return app('json')->fail('单次最大可以种树'.$many['single']);
  82. if ($surplus < $data['price']) return app('json')->fail('超过最大可种树额度');
  83. if (ManyOrder::where('many_id', $many['id'])->where('uid', $user['uid'])->where('stage', $many['stage'])->count() >= 3) return app('json')->fail('超过单场最大抢购数');
  84. if ($many['add_time'] > time()){
  85. if ($many['add_time'] - 600 > time()) return app('json')->fail('只能提前十分钟进行,提前种树');
  86. $advance = SystemConfigService::get('advance');
  87. $integral = $data['price']* ($advance/100);
  88. if ($user['integral'] < $integral) return app('json')->fail('阳光不足无法提前投注');
  89. $user['integral'] -= $integral;
  90. UserBill::expend('扣除阳光积分', $user['uid'], 'integral', 'ad_integral', $integral, 0, $user['integral'], '提前投注扣除'.$integral.'阳光');
  91. }
  92. if ($user['white_integral'] < $data['price']) return app('json')->fail('肥料不够');
  93. try {
  94. $many['number'] += round($data['price'], 2);
  95. $user['white_integral'] -= $data['price'];
  96. UserBill::expend('扣除肥料', $user['uid'], 'white_integral', 'bet_white_integral', $data['price'], 0,$user['white_integral'],'使用肥料,参与种树-》'.$many['name'].'期数-》'.$many['stage'].'-成功');
  97. $user->save();
  98. ManyOrder::create([
  99. 'order_id' => StoreOrder::getNewOrderId(),
  100. 'many_id' => $many['id'],
  101. 'uid' => $user['uid'],
  102. 'stage' => $many['stage'],
  103. 'price' => $data['price'],
  104. ]);
  105. if ($many['number'] >= $many['money']){
  106. $many['suc'] = 1;// 众筹成功
  107. $many['status'] = 0;// 众筹成功
  108. ManyDiscipline::create(['many_id' => $many['id'], 'stage' => $many['stage'], 'status' => 1]);// 成功记录
  109. ManyOrder::order_return($many);
  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. $many->save();
  117. Db::commit();
  118. return app('json')->success('种树成功');
  119. } catch (\Exception $e) {
  120. Db::rollback();
  121. return app('json')->fail($e->getMessage());
  122. }
  123. }
  124. /**
  125. * 还有多少可投注
  126. * @param $id
  127. * @param $type
  128. * @return float|mixed
  129. * @throws \think\db\exception\DataNotFoundException
  130. * @throws \think\db\exception\DbException
  131. * @throws \think\db\exception\ModelNotFoundException
  132. */
  133. public function surplus($id, $uid,$type = 0)
  134. {
  135. if ($type > 0){
  136. $many = Many::where('id', $id)->find();
  137. if (!$many) return app('json')->fail('场次不存在');
  138. $many_order = ManyOrder::where('many_id', $id)->where('uid', $uid)->where('stage', $many['stage'])->sum('price');
  139. $price = ($many['upper_limit'] - $many_order); // 还可以投注额度
  140. return $price;
  141. }else{
  142. $many = Many::where('id', $id)->find();
  143. if (!$many) return app('json')->fail('场次不存在');
  144. $many_order = ManyOrder::where('many_id', $id)->where('uid', $uid)->where('stage', $many['stage'])->sum('price');
  145. $data['price'] = ($many['upper_limit'] - $many_order); // 还可以投注额度
  146. return app('json')->fail($data);
  147. }
  148. }
  149. /**
  150. * 添加收款方式
  151. * @param Request $request
  152. * @return void
  153. */
  154. public function pay(Request $request)
  155. {
  156. $data = UtilService::postMore([
  157. ['payment'],
  158. ['image'],
  159. ['bank'],
  160. ['name'],
  161. ['type'],
  162. ['phone'],
  163. ['bank_name']
  164. ], $request);
  165. if (!$data['type']) return app('json')->fail('数据传入错误');
  166. $data['uid'] =$request->uid();
  167. $model = new AuctionPay();
  168. $pay = $model->where([['uid', '=', $request->uid()], ['type', '=', $data['type']]])->find();
  169. $res = Validate::rule([
  170. 'phone' => 'mobile'
  171. ]);
  172. $res->message([
  173. 'phone.mobile' => '请填写正确手机格式'
  174. ]);
  175. if (!$res->check($data)){
  176. return app('json')->fail($res->getError());
  177. }
  178. if (!empty($pay)){
  179. if ($data['type'] == 1 ){
  180. // 微信收款方式
  181. if (!$data['payment']) return app('json')->fail('微信账号不能为空');
  182. if (!$data['image']) return app('json')->fail('二维码不能为空');
  183. if (!$data['name']) return app('json')->fail('姓名不能为空');
  184. $pay['payment'] = $data['payment'];
  185. $pay['image'] = $data['image'];
  186. $pay['name'] = $data['name'];
  187. $pay['phone'] = $data['phone'];
  188. }elseif ($data['type'] == 2){
  189. // 支付宝收款方式
  190. if (!$data['payment']) return app('json')->fail('支付宝账号不能为空');
  191. if (!$data['name']) return app('json')->fail('姓名不能为空');
  192. $pay['payment'] = $data['payment'];
  193. $pay['name'] = $data['name'];
  194. }elseif ($data['type'] == 3){
  195. // 银行卡收款方式
  196. if (!$data['payment']) return app('json')->fail('银行卡号不能为空');
  197. if (!$data['name']) return app('json')->fail('姓名不能为空');
  198. if (!$data['bank']) return app('json')->fail('开户行不能为空');
  199. $pay['payment'] = $data['payment'];
  200. $pay['bank'] = $data['bank'];
  201. $pay['name'] = $data['name'];
  202. }
  203. $res = $pay->save();
  204. if ($res) return app('json')->successful('修改成功');
  205. return app('json')->fail('修改失败');
  206. }else{
  207. if ($data['type'] == 1 ){
  208. // 微信收款方式
  209. if (!$data['payment']) return app('json')->fail('微信账号不能为空');
  210. if (!$data['image']) return app('json')->fail('二维码不能为空');
  211. if (!$data['name']) return app('json')->fail('姓名不能为空');
  212. if (!$data['phone']) return app('json')->fail('请填写手机号');
  213. }elseif ($data['type'] == 2){
  214. // 支付宝收款方式
  215. if (!$data['payment']) return app('json')->fail('支付宝账号不能为空');
  216. if (!$data['name']) return app('json')->fail('姓名不能为空');
  217. }elseif ($data['type'] == 3){
  218. // 银行卡收款方式
  219. if (!$data['payment']) return app('json')->fail('银行卡号不能为空');
  220. if (!$data['name']) return app('json')->fail('姓名不能为空');
  221. if (!$data['bank']) return app('json')->fail('开户行不能为空');
  222. }
  223. $res = $model->save($data);
  224. if ($res) return app('json')->successful('添加成功');
  225. return app('json')->fail('添加失败');
  226. }
  227. }
  228. /**
  229. * 收款方式详情
  230. * @param Request $request
  231. * @return mixed
  232. * @throws \think\db\exception\DataNotFoundException
  233. * @throws \think\db\exception\DbException
  234. * @throws \think\db\exception\ModelNotFoundException
  235. */
  236. public function pay_list(Request $request)
  237. {
  238. $model = new AuctionPay();
  239. $list = $model->where('uid', $request->uid())->select();
  240. $list = empty($list)? []: $list->toArray();
  241. $data['wx'] = [];
  242. $data['zfb'] = [];
  243. $data['bank'] = [];
  244. foreach ($list as $k => $v){
  245. if ($v['type'] == 1){
  246. $data['wx'] = $v;
  247. }elseif ($v['type'] == 2){
  248. $data['zfb'] = $v;
  249. }elseif ($v['type'] == 3){
  250. $data['bank'] = $v;
  251. }
  252. }
  253. return app('json')->successful($data);
  254. }
  255. }