RechargeLogic.php 13 KB

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