EnergyPlatformPackageController.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. namespace App\Http\Controllers\Admin\Energy;
  3. use Illuminate\Http\Request;
  4. use App\Services\AipHttpClient;
  5. use App\Http\Controllers\Controller;
  6. use Illuminate\Support\Facades\DB;
  7. use App\Models\Energy\EnergyPlatform;
  8. use App\Models\Energy\EnergyPlatformPackage;
  9. use App\Models\Telegram\TelegramBot;
  10. use App\Http\Controllers\Admin\Setting\ConfigController;
  11. class EnergyPlatformPackageController extends Controller
  12. {
  13. public $Status = ['开启','关闭'];
  14. public $EnergyDay = ['0' => '1小时','1' => '1天','3' => '3天'];
  15. public $PackageType = ['1' => '能量'];
  16. public function index(Request $request)
  17. {
  18. $Status = $this->Status;
  19. $EnergyDay = $this->EnergyDay;
  20. $PackageType = $this->PackageType;
  21. $botData = TelegramBot::pluck('bot_username','rid');
  22. return view('admin.energy.package.index',compact("botData","Status","EnergyDay","PackageType"));
  23. }
  24. //列表
  25. public function getData(Request $request)
  26. {
  27. $model = EnergyPlatformPackage::from('energy_platform_package as a')
  28. ->leftJoin('telegram_bot as b','a.bot_rid','b.rid')
  29. ->where(function($query) use ($request){
  30. if ($request->package_name != '') {
  31. $query->where('a.package_name', 'like' ,"%" . $request->package_name ."%");
  32. }
  33. if ($request->bot_rid != '') {
  34. $query->where('a.bot_rid', $request->bot_rid);
  35. }
  36. });
  37. $count = $model->count();
  38. $limit = $request->limit ?? 15;
  39. $offset = $request->page ? ($request->page - 1) * $limit : 0;
  40. $data = $model->limit($limit)->offset($offset)->select('a.*','b.bot_token','b.bot_firstname','b.bot_username')->orderBy('a.rid','desc')->get();
  41. $EnergyDay = $this->EnergyDay;
  42. $PackageType = $this->PackageType;
  43. $data = $data->map(function($query) use ($EnergyDay,$PackageType){
  44. $query->energy_day_val = $EnergyDay[$query->energy_day];
  45. $query->package_type_val = $PackageType[$query->package_type];
  46. return $query;
  47. });
  48. return ['code' => '0', 'data' => $data, 'count' => $count];
  49. }
  50. //添加
  51. public function add(Request $request)
  52. {
  53. if(empty($request->trx_price) || $request->trx_price <= 0){
  54. return $this->responseData(400, 'trx售价不能小于0');
  55. }
  56. if(empty($request->energy_amount) || $request->energy_amount < 32000){
  57. return $this->responseData(400, '能量数量不能低于32000');
  58. }
  59. if(empty($request->package_name)){
  60. return $this->responseData(400, '套餐名称不能为空');
  61. }
  62. $res = EnergyPlatformPackage::create([
  63. 'bot_rid' => $request->bot_rid,
  64. 'package_type' => $request->package_type,
  65. 'package_name' => $request->package_name,
  66. 'energy_amount' => $request->energy_amount,
  67. 'energy_day' => $request->energy_day,
  68. 'trx_price' => $request->trx_price ?? 0.1,
  69. 'usdt_price' => $request->usdt_price ?? 0.1,
  70. 'seq_sn' => $request->seq_sn ?? 0,
  71. 'callback_data' => 'energy_'.md5(nowDate()),
  72. 'show_notes' => $request->show_notes ?? '',
  73. 'create_time' => nowDate()
  74. ]);
  75. return $res ? $this->responseData(200, '添加成功') : $this->responseData(400, '添加失败');
  76. }
  77. //删除
  78. public function delete(Request $request)
  79. {
  80. $res = EnergyPlatformPackage::where('rid', $request->rid)->delete();
  81. return $res ? $this->responseData(200, '删除成功') : $this->responseData(400, '删除失败');
  82. }
  83. //批量删除
  84. public function batchdelete(Request $request)
  85. {
  86. $res = EnergyPlatformPackage::where('bot_rid', $request->bot_rid)->delete();
  87. return $res ? $this->responseData(200, '删除成功') : $this->responseData(400, '删除失败');
  88. }
  89. //编辑
  90. public function update(Request $request, ConfigController $upload)
  91. {
  92. if(empty($request->trx_price) || $request->trx_price <= 0){
  93. return $this->responseData(400, 'trx售价不能小于0');
  94. }
  95. if(empty($request->energy_amount) || $request->energy_amount < 32000){
  96. return $this->responseData(400, '能量数量不能低于32000');
  97. }
  98. if(empty($request->package_name)){
  99. return $this->responseData(400, '套餐名称不能为空');
  100. }
  101. $packageData = EnergyPlatformPackage::where('rid', $request->rid)->first();
  102. if(empty($packageData)){
  103. return $this->responseData(400, '数据不存在');
  104. }
  105. if(!empty($request->file('thumb'))){
  106. $filedata = $upload->uploadfile($request->file('thumb'), 'news');
  107. $fileurl = $filedata['data']['url'];
  108. }else{
  109. $fileurl = $packageData->reply_photo;
  110. }
  111. DB::beginTransaction();
  112. try {
  113. $packageData->bot_rid = $request->bot_rid;
  114. $packageData->package_type = $request->package_type;
  115. $packageData->package_name = $request->package_name;
  116. $packageData->energy_amount = $request->energy_amount;
  117. $packageData->energy_day = $request->energy_day;
  118. $packageData->trx_price = $request->trx_price ?? 0.1;
  119. $packageData->usdt_price = $request->usdt_price ?? 0.1;
  120. $packageData->seq_sn = $request->seq_sn ?? 0;
  121. $packageData->show_notes = $request->show_notes ?? '';
  122. $packageData->package_pic = $fileurl;
  123. $packageData->update_time = nowDate();
  124. $packageData->save();
  125. DB::commit();
  126. return $this->responseData(200, '更新成功');
  127. } catch (\Exception $e) {
  128. DB::rollBack();
  129. return $this->responseData(400, '更新失败'.$e->getMessage());
  130. }
  131. }
  132. //编辑状态
  133. public function change_status(Request $request)
  134. {
  135. DB::beginTransaction();
  136. try {
  137. $data = EnergyPlatformPackage::where('rid', $request->rid)->first();
  138. $data->status = $request->status == 1 ? 0 : 1;
  139. $data->save();
  140. DB::commit();
  141. return $this->responseData(200, '更新成功');
  142. } catch (\Exception $e) {
  143. DB::rollBack();
  144. return $this->responseData(400, '更新失败'.$e->getMessage());
  145. }
  146. }
  147. // 编辑页面查看
  148. public function show(Request $request)
  149. {
  150. $Status = $this->Status;
  151. $EnergyDay = $this->EnergyDay;
  152. $PackageType = $this->PackageType;
  153. $data = EnergyPlatformPackage::from('energy_platform_package as a')
  154. ->leftJoin('telegram_bot as b','a.bot_rid','b.rid')
  155. ->where('a.rid',$request->rid)
  156. ->select('a.*','b.bot_token','b.bot_firstname','b.bot_username')
  157. ->first();
  158. return view('admin.energy.package.edit',compact("Status","EnergyDay","PackageType","data"));
  159. }
  160. //复制
  161. public function copyPaste(Request $request)
  162. {
  163. if(empty($request->paste_bot_rid) || empty($request->copy_bot_rid)){
  164. return $this->responseData(400, '覆盖和来源机器人必填');
  165. }
  166. if($request->paste_bot_rid == $request->copy_bot_rid){
  167. return $this->responseData(400, '覆盖和来源机器人不能一致');
  168. }
  169. $data = EnergyPlatformPackage::where('bot_rid', $request->copy_bot_rid)->get();
  170. if($data->count() == 0){
  171. return $this->responseData(400, '来源机器人无数据可复制');
  172. }
  173. DB::beginTransaction();
  174. try {
  175. EnergyPlatformPackage::where('bot_rid', $request->paste_bot_rid)->delete();
  176. EnergyPlatformPackage::insertUsing([
  177. 'bot_rid', 'package_type', 'package_name','energy_amount','energy_day','trx_price','usdt_price','status','seq_sn','create_time','callback_data','show_notes','package_pic'
  178. ], EnergyPlatformPackage::selectRaw(
  179. "$request->paste_bot_rid, package_type, package_name, energy_amount, energy_day, trx_price, usdt_price, status, seq_sn, sysdate(),concat('energy_',md5(rand())), show_notes, package_pic"
  180. )->where('bot_rid', $request->copy_bot_rid));
  181. DB::commit();
  182. return $this->responseData(200, '复制成功');
  183. } catch (\Exception $e) {
  184. DB::rollBack();
  185. return $this->responseData(400, '复制失败'.$e->getMessage());
  186. }
  187. }
  188. }