RechargeLogic.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. foreach ($list as &$value) {
  109. $name = [];
  110. if (!empty($value->gear)) {
  111. $gear = \GuzzleHttp\json_decode($value->gear, true);
  112. foreach ($gear as $val) {
  113. if ($val['status'] == 1) {
  114. $name[] = isset($names[$val['id']]) ? $names[$val['id']] : '';
  115. }
  116. }
  117. }
  118. $value->gear = implode(',', $name);
  119. }
  120. // dd($list);
  121. // // 首充档位配置
  122. // $first_pay = DB::table('agent.dbo.recharge_gear')
  123. // ->where('first_pay', 1)
  124. // ->first();
  125. // $name = [];
  126. // if (!empty($first_pay->gear)) {
  127. // $gear = \GuzzleHttp\json_decode($first_pay->gear, true);
  128. // foreach ($gear as $val) {
  129. // if ($val['status'] == 1) {
  130. // $name[] = isset($names[$val['id']]) ? $names[$val['id']] : '';
  131. // }
  132. // }
  133. // }
  134. // $first_pay->gear = implode(',', $name);
  135. $first_pay = [];
  136. $RecomendarRecharge = [];
  137. $monthCard = [];
  138. // 推荐充值档位
  139. // $RecomendarRecharge = DB::table(TableName::QPAccountsDB() . 'SystemStatusInfo')
  140. // ->where('StatusName', 'RecomendarRecharge')
  141. // ->value('StatusValue');
  142. // // 周卡
  143. // $channels = DB::table('agent.dbo.admin_configs')->where([
  144. // 'type' => 'pay',
  145. // ])->pluck('id', 'name')->toArray();
  146. // $monthCard = MonthCard::query()->where('CardType', 1)->get();
  147. // foreach ($monthCard as $k => $v) {
  148. // $monthCard[$k]->FirstReward = $v->FirstReward/100;
  149. // $monthCard[$k]->DayReward = $v->DayReward/100;
  150. // $cardChannels = array_filter($v->gear, function ($item) {
  151. // return $item['status'] == 1;
  152. // });
  153. // $ids = array_column($cardChannels, 'id');
  154. // $v['channels'] = array_filter($channels, function ($item) use ($ids) {
  155. // return in_array($item, $ids);
  156. // });
  157. // }
  158. return compact('list', 'first_pay', 'RecomendarRecharge', 'monthCard');
  159. }
  160. public function channel_switch($id, $type)
  161. {
  162. $first_pay_status = DB::table('agent.dbo.recharge_gear')->where('id', $id)->value('first_pay');
  163. switch ($type) {
  164. case 'off': // 关闭
  165. $query = DB::table('agent.dbo.recharge_gear')->where('status', 1)->count();
  166. if ($query <= 1) {
  167. $this->error = '至少开启一个充值档位';
  168. return false;
  169. }
  170. if ($first_pay_status == 2) { // 引导付费 共用一个开关状态
  171. $res = DB::table('agent.dbo.recharge_gear')->where('first_pay', 2)->update([
  172. 'status' => -1,
  173. ]);
  174. }else{
  175. $res = DB::table('agent.dbo.recharge_gear')->where('id', $id)->update([
  176. 'status' => -1,
  177. ]);
  178. }
  179. break;
  180. case 'on': // 开启
  181. if ($first_pay_status == 2) { // 引导付费 共用一个开关状态
  182. $res = DB::table('agent.dbo.recharge_gear')->where('first_pay', 2)->update([
  183. 'status' => 1,
  184. ]);
  185. }else{
  186. $res = DB::table('agent.dbo.recharge_gear')->where('id', $id)->update([
  187. 'status' => 1,
  188. ]);
  189. }
  190. break;
  191. default:
  192. $this->error = '类型错误';
  193. return false;
  194. }
  195. if (isset($res) && $res) {
  196. return true;
  197. } else {
  198. $this->error = '操作失败';
  199. return false;
  200. }
  201. }
  202. public function add($money, $status, $config_ids, $favorable_price, $give)
  203. {
  204. // 查询所有渠道
  205. $pluck = DB::table('agent.dbo.admin_configs')->where('type', 'pay')->pluck('id')->toArray();
  206. $data = [];
  207. foreach ($pluck as $val) {
  208. if (in_array($val, $config_ids)) {
  209. $data[] = [
  210. 'id' => $val,
  211. 'status' => 1
  212. ];
  213. } else {
  214. $data[] = [
  215. 'id' => $val,
  216. 'status' => -1
  217. ];
  218. }
  219. }
  220. $add_data = [
  221. 'money' => round($money, 2),
  222. 'status' => $status,
  223. 'gear' => \GuzzleHttp\json_encode($data),
  224. 'created_at' => date('Y-m-d H:i:s'),
  225. 'favorable_price' => round($favorable_price, 2),
  226. 'give' => round($give, 2)
  227. ];
  228. DB::table('agent.dbo.recharge_gear')->insert($add_data);
  229. return true;
  230. }
  231. public function update($id)
  232. {
  233. $info = DB::table('agent.dbo.recharge_gear')->where('id', $id)->first();
  234. $gear = \GuzzleHttp\json_decode($info->gear, true);
  235. $names = DB::table('agent.dbo.admin_configs')->where('type', 'pay')->pluck('name', 'id');
  236. $new_arr = [];
  237. foreach ($gear as &$value) {
  238. if ($value['status'] == 1) {
  239. $new_arr[] = $value['id'];
  240. }
  241. }
  242. $info->gear = $new_arr;
  243. return compact('info', 'names');
  244. }
  245. public function gear_switch($id, $config_id, $type)
  246. {
  247. $query = DB::table('agent.dbo.recharge_gear')->where('id', $id)->first();
  248. $gear = \GuzzleHttp\json_decode($query->gear, true);
  249. $off = 0;
  250. $data = [];
  251. $new_arr = [];
  252. switch ($type) {
  253. case 'on':
  254. foreach ($gear as &$value) {
  255. $data[] = $value['id'];
  256. if ($value['id'] == $config_id) {
  257. $value['status'] = 1;
  258. }
  259. }
  260. if (!in_array($config_id, $data)) {
  261. $new_arr['id'] = $config_id;
  262. $new_arr['status'] = 1;
  263. }
  264. break;
  265. case 'off':
  266. foreach ($gear as &$value) {
  267. $data[] = $value['id'];
  268. if ($value['id'] == $config_id) {
  269. $value['status'] = -1;
  270. }
  271. if ($value['status'] == 1) {
  272. $off += 1;
  273. }
  274. }
  275. if (!in_array($config_id, $data)) {
  276. $new_arr['id'] = $config_id;
  277. $new_arr['status'] = -1;
  278. }
  279. if ($off < 1) {
  280. $this->error = '至少开启一个通道';
  281. return false;
  282. }
  283. break;
  284. default:
  285. $this->error = '类型错误';
  286. return false;
  287. }
  288. !empty($new_arr) && $gear[] = $new_arr;
  289. $gear = \GuzzleHttp\json_encode($gear);
  290. DB::table('agent.dbo.recharge_gear')->where('id', $id)->update(['gear' => $gear]);
  291. }
  292. // 首充礼包
  293. public function gift()
  294. {
  295. $where = [];
  296. $where[] = ['first_pay', '>=', 1];
  297. $list = DB::table('agent.dbo.recharge_gear')
  298. ->where($where)
  299. ->orderByDesc('created_at')
  300. ->get();
  301. $names = DB::table('agent.dbo.admin_configs')->where('type', 'pay')->pluck('name', 'id');
  302. foreach ($list as &$value) {
  303. $name = [];
  304. if (!empty($value->gear)) {
  305. $gear = \GuzzleHttp\json_decode($value->gear, true);
  306. foreach ($gear as $val) {
  307. if ($val['status'] == 1) {
  308. $name[] = isset($names[$val['id']]) ? $names[$val['id']] : '';
  309. }
  310. }
  311. }
  312. $value->gear = implode(',', $name);
  313. }
  314. // 旧首充
  315. // 开关状态
  316. $oldRechargeStatus = DB::connection('write')->table('QPAccountsDB.dbo.SystemStatusInfo')
  317. ->where('StatusName', 'FirstChargeGiftBagStatus')
  318. ->first();
  319. // 首充
  320. $oldRecharge = DB::connection('write')->table('QPAccountsDB.dbo.SystemStatusInfo')
  321. ->where('StatusName', 'FirstChargeGiftBag')
  322. ->first();
  323. $oldRechargeChannel = $names[$oldRecharge->StatusValue] ?? '';
  324. $pay_list = $names;
  325. return compact('list', 'oldRecharge', 'oldRechargeStatus','oldRechargeChannel','pay_list');
  326. }
  327. }