RechargeLogic.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. <?php
  2. namespace App\Http\logic\admin;
  3. use App\Facade\TableName;
  4. use App\Http\logic\api\BaseApiLogic;
  5. use App\Models\Platform\MonthCard;
  6. use Illuminate\Support\Facades\DB;
  7. class RechargeLogic extends BaseApiLogic
  8. {
  9. public function config($method)
  10. {
  11. $list = DB::connection('write')->table('agent.dbo.admin_configs as ac')
  12. ->leftJoin('agent.dbo.admin_users as u', 'ac.admin_id', '=', 'u.id')
  13. ->select('ac.*', 'u.account')
  14. ->where('ac.type', 'pay')
  15. ->where('ac.new_pay_type', $method)
  16. ->get();
  17. // 支付渠道列表
  18. $pay_list = DB::connection('write')->table('agent.dbo.admin_configs')
  19. ->where('status', 1)
  20. ->where('type', 'pay')
  21. ->pluck('name', 'id');
  22. // 开关状态
  23. $first = DB::connection('write')->table('QPAccountsDB.dbo.SystemStatusInfo')
  24. ->where('StatusName', 'FirstChargeGiftBagStatus')
  25. ->first();
  26. // 首充
  27. $firstCharge = DB::connection('write')->table('QPAccountsDB.dbo.SystemStatusInfo')
  28. ->where('StatusName', 'FirstChargeGiftBag')
  29. ->first();
  30. $poll = DB::connection('write')->table('QPAccountsDB.dbo.SystemStatusInfo')
  31. ->where('StatusName', 'PollSwitch')
  32. ->first();
  33. return compact('list', 'pay_list', 'first', 'firstCharge', 'poll');
  34. }
  35. // 控制开关
  36. public function switch_control($id, $type, $admin_id)
  37. {
  38. switch ($type) {
  39. case 'off': // 关闭
  40. // 不能全部关闭,至少保留一个
  41. $query = DB::table('agent.dbo.admin_configs')->where('type', 'pay')->where('status', 1)->count();
  42. if ($query <= 1) {
  43. $this->error = '至少开启一个支付渠道';
  44. return false;
  45. }
  46. $res = DB::table('agent.dbo.admin_configs')->where('id', $id)->update([
  47. 'status' => -1,
  48. 'updated_at' => date('Y-m-d H:i:s'),
  49. 'admin_id' => $admin_id
  50. ]);
  51. $list = DB::table('agent.dbo.recharge_gear')->get();
  52. foreach ($list as &$value) {
  53. try {
  54. $gear = \GuzzleHttp\json_decode($value->gear, true);
  55. foreach ($gear as &$val) {
  56. if ($val['id'] == $id) {
  57. $val['status'] = -1;
  58. }
  59. }
  60. $gear = \GuzzleHttp\json_encode($gear);
  61. DB::table('agent.dbo.recharge_gear')->where('id', $value->id)->update(['gear' => $gear]);
  62. }catch (\Exception $e) {
  63. }
  64. }
  65. break;
  66. case 'on': // 开启
  67. $res = DB::table('agent.dbo.admin_configs')->where('id', $id)->update([
  68. 'status' => 1,
  69. 'updated_at' => date('Y-m-d H:i:s'),
  70. 'admin_id' => $admin_id
  71. ]);
  72. $list = DB::table('agent.dbo.recharge_gear')->get();
  73. foreach ($list as &$value) {
  74. try {
  75. $gear = \GuzzleHttp\json_decode($value->gear, true);
  76. foreach ($gear as &$val) {
  77. if ($val['id'] == $id) {
  78. $val['status'] = 1;
  79. }
  80. }
  81. $gear = \GuzzleHttp\json_encode($gear);
  82. DB::table('agent.dbo.recharge_gear')->where('id', $value->id)->update(['gear' => $gear]);
  83. }catch (\Exception $e){
  84. }
  85. }
  86. break;
  87. default:
  88. $this->error = '类型错误';
  89. return false;
  90. }
  91. if (isset($res) && $res) {
  92. return true;
  93. } else {
  94. $this->error = '操作失败';
  95. return false;
  96. }
  97. }
  98. // 充值档位
  99. public function gear_list($first_pay_status = 0)
  100. {
  101. $where = [];
  102. $where[] = ['first_pay', $first_pay_status];
  103. $list = DB::table('agent.dbo.recharge_gear')
  104. ->where($where)
  105. ->orderBy('money')
  106. ->get();
  107. $names = DB::table('agent.dbo.admin_configs')->where('type', 'pay')->pluck('name', 'id');
  108. $payMethodsNames = [
  109. 'cashapp' => 'CashApp',
  110. 'paypal' => 'PayPal',
  111. 'applepay' => 'ApplePay',
  112. 'googlepay' => 'GooglePay',
  113. 'usdt-trc20' => 'USDT-TRC20',
  114. 'usdt-erc20' => 'USDT-ERC20',
  115. 'usdc-trc20' => 'USDC-TRC20',
  116. 'usdc-erc20' => 'USDC-ERC20'
  117. ];
  118. foreach ($list as &$value) {
  119. $name = [];
  120. if (!empty($value->gear)) {
  121. $gear = \GuzzleHttp\json_decode($value->gear, true);
  122. if (is_array($gear)) {
  123. foreach ($gear as $val) {
  124. // 新格式:支付方式位掩码
  125. if (isset($val['name']) && isset($val['status']) && $val['status'] == 1) {
  126. $name[] = $payMethodsNames[$val['name']] ?? $val['name'];
  127. }
  128. // 旧格式:支付渠道ID(兼容)
  129. elseif (isset($val['id']) && ($val['status'] ?? 0) == 1) {
  130. $name[] = isset($names[$val['id']]) ? $names[$val['id']] : '';
  131. }
  132. }
  133. }
  134. }
  135. $value->gear = implode(', ', $name);
  136. }
  137. // dd($list);
  138. // // 首充档位配置
  139. // $first_pay = DB::table('agent.dbo.recharge_gear')
  140. // ->where('first_pay', 1)
  141. // ->first();
  142. // $name = [];
  143. // if (!empty($first_pay->gear)) {
  144. // $gear = \GuzzleHttp\json_decode($first_pay->gear, true);
  145. // foreach ($gear as $val) {
  146. // if ($val['status'] == 1) {
  147. // $name[] = isset($names[$val['id']]) ? $names[$val['id']] : '';
  148. // }
  149. // }
  150. // }
  151. // $first_pay->gear = implode(',', $name);
  152. $first_pay = [];
  153. $RecomendarRecharge = [];
  154. $monthCard = [];
  155. // 推荐充值档位
  156. // $RecomendarRecharge = DB::table(TableName::QPAccountsDB() . 'SystemStatusInfo')
  157. // ->where('StatusName', 'RecomendarRecharge')
  158. // ->value('StatusValue');
  159. // // 周卡
  160. // $channels = DB::table('agent.dbo.admin_configs')->where([
  161. // 'type' => 'pay',
  162. // ])->pluck('id', 'name')->toArray();
  163. // $monthCard = MonthCard::query()->where('CardType', 1)->get();
  164. // foreach ($monthCard as $k => $v) {
  165. // $monthCard[$k]->FirstReward = $v->FirstReward/100;
  166. // $monthCard[$k]->DayReward = $v->DayReward/100;
  167. // $cardChannels = array_filter($v->gear, function ($item) {
  168. // return $item['status'] == 1;
  169. // });
  170. // $ids = array_column($cardChannels, 'id');
  171. // $v['channels'] = array_filter($channels, function ($item) use ($ids) {
  172. // return in_array($item, $ids);
  173. // });
  174. // }
  175. return compact('list', 'first_pay', 'RecomendarRecharge', 'monthCard');
  176. }
  177. public function channel_switch($id, $type)
  178. {
  179. $first_pay_status = DB::table('agent.dbo.recharge_gear')->where('id', $id)->value('first_pay');
  180. switch ($type) {
  181. case 'off': // 关闭
  182. $query = DB::table('agent.dbo.recharge_gear')->where('status', 1)->count();
  183. if ($query <= 1) {
  184. $this->error = '至少开启一个充值档位';
  185. return false;
  186. }
  187. if ($first_pay_status == 2) { // 引导付费 共用一个开关状态
  188. $res = DB::table('agent.dbo.recharge_gear')->where('first_pay', 2)->update([
  189. 'status' => -1,
  190. ]);
  191. }else{
  192. $res = DB::table('agent.dbo.recharge_gear')->where('id', $id)->update([
  193. 'status' => -1,
  194. ]);
  195. }
  196. break;
  197. case 'on': // 开启
  198. if ($first_pay_status == 2) { // 引导付费 共用一个开关状态
  199. $res = DB::table('agent.dbo.recharge_gear')->where('first_pay', 2)->update([
  200. 'status' => 1,
  201. ]);
  202. }else{
  203. $res = DB::table('agent.dbo.recharge_gear')->where('id', $id)->update([
  204. 'status' => 1,
  205. ]);
  206. }
  207. break;
  208. default:
  209. $this->error = '类型错误';
  210. return false;
  211. }
  212. if (isset($res) && $res) {
  213. return true;
  214. } else {
  215. $this->error = '操作失败';
  216. return false;
  217. }
  218. }
  219. public function add($money, $status, $config_ids, $favorable_price, $give, $pay_methods = 0, $in_shop = 1)
  220. {
  221. // 构建支付方式数组
  222. $gearData = [];
  223. $payMethodsMap = [
  224. 1 => 'cashapp',
  225. 2 => 'paypal',
  226. 4 => 'applepay',
  227. 8 => 'googlepay',
  228. 64 => 'usdt-trc20',
  229. 128 => 'usdt-erc20',
  230. 256 => 'usdc-trc20',
  231. 512 => 'usdc-erc20'
  232. ];
  233. foreach ($payMethodsMap as $value => $name) {
  234. if ($pay_methods & $value) {
  235. $gearData[] = [
  236. 'name' => $name,
  237. 'value' => $value,
  238. 'status' => 1
  239. ];
  240. }
  241. }
  242. $add_data = [
  243. 'money' => round($money, 2),
  244. 'status' => $status,
  245. 'in_shop' => (int)$in_shop,
  246. 'gear' => \GuzzleHttp\json_encode($gearData), // 存储到gear字段
  247. 'created_at' => date('Y-m-d H:i:s'),
  248. 'favorable_price' => round($favorable_price, 2),
  249. 'give' => round($give, 2)
  250. ];
  251. DB::table('agent.dbo.recharge_gear')->insert($add_data);
  252. return true;
  253. }
  254. public function update($id)
  255. {
  256. $info = DB::table('agent.dbo.recharge_gear')->where('id', $id)->first();
  257. $gear = \GuzzleHttp\json_decode($info->gear, true);
  258. $names = DB::table('agent.dbo.admin_configs')->where('type', 'pay')->pluck('name', 'id');
  259. $new_arr = [];
  260. foreach ($gear as &$value) {
  261. if ($value['status'] == 1) {
  262. $new_arr[] = $value['id'];
  263. }
  264. }
  265. $info->gear = $new_arr;
  266. return compact('info', 'names');
  267. }
  268. public function gear_switch($id, $config_id, $type)
  269. {
  270. $query = DB::table('agent.dbo.recharge_gear')->where('id', $id)->first();
  271. $gear = \GuzzleHttp\json_decode($query->gear, true);
  272. $off = 0;
  273. $data = [];
  274. $new_arr = [];
  275. switch ($type) {
  276. case 'on':
  277. foreach ($gear as &$value) {
  278. $data[] = $value['id'];
  279. if ($value['id'] == $config_id) {
  280. $value['status'] = 1;
  281. }
  282. }
  283. if (!in_array($config_id, $data)) {
  284. $new_arr['id'] = $config_id;
  285. $new_arr['status'] = 1;
  286. }
  287. break;
  288. case 'off':
  289. foreach ($gear as &$value) {
  290. $data[] = $value['id'];
  291. if ($value['id'] == $config_id) {
  292. $value['status'] = -1;
  293. }
  294. if ($value['status'] == 1) {
  295. $off += 1;
  296. }
  297. }
  298. if (!in_array($config_id, $data)) {
  299. $new_arr['id'] = $config_id;
  300. $new_arr['status'] = -1;
  301. }
  302. if ($off < 1) {
  303. $this->error = '至少开启一个通道';
  304. return false;
  305. }
  306. break;
  307. default:
  308. $this->error = '类型错误';
  309. return false;
  310. }
  311. !empty($new_arr) && $gear[] = $new_arr;
  312. $gear = \GuzzleHttp\json_encode($gear);
  313. DB::table('agent.dbo.recharge_gear')->where('id', $id)->update(['gear' => $gear]);
  314. }
  315. // 首充礼包
  316. public function gift()
  317. {
  318. $where = [];
  319. $where[] = ['first_pay', '>=', 1];
  320. $list = DB::table('agent.dbo.recharge_gear')
  321. ->where($where)
  322. ->orderByDesc('created_at')
  323. ->get();
  324. $names = DB::table('agent.dbo.admin_configs')->where('type', 'pay')->pluck('name', 'id');
  325. foreach ($list as &$value) {
  326. $name = [];
  327. if (!empty($value->gear)) {
  328. $gear = \GuzzleHttp\json_decode($value->gear, true);
  329. foreach ($gear as $val) {
  330. if ($val['status'] == 1) {
  331. $name[] = isset($names[$val['id']]) ? $names[$val['id']] : '';
  332. }
  333. }
  334. }
  335. $value->gear = implode(',', $name);
  336. }
  337. // 旧首充
  338. // 开关状态
  339. $oldRechargeStatus = DB::connection('write')->table('QPAccountsDB.dbo.SystemStatusInfo')
  340. ->where('StatusName', 'FirstChargeGiftBagStatus')
  341. ->first();
  342. // 首充
  343. $oldRecharge = DB::connection('write')->table('QPAccountsDB.dbo.SystemStatusInfo')
  344. ->where('StatusName', 'FirstChargeGiftBag')
  345. ->first();
  346. $oldRechargeChannel = $names[$oldRecharge->StatusValue] ?? '';
  347. $pay_list = $names;
  348. return compact('list', 'oldRecharge', 'oldRechargeStatus','oldRechargeChannel','pay_list');
  349. }
  350. }