2
0

RelationController.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Relation;
  4. use Illuminate\Http\Request;
  5. use App\Http\Resources\RelationResource;
  6. use App\Http\Api\AuthApi;
  7. use Illuminate\Support\Facades\App;
  8. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  9. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  10. class RelationController extends Controller
  11. {
  12. /**
  13. * Display a listing of the resource.
  14. *
  15. * @return \Illuminate\Http\Response
  16. */
  17. public function index(Request $request)
  18. {
  19. //
  20. $table = Relation::select(['id','name','case','from','to','editor_id','updated_at','created_at']);
  21. if(($request->has('case'))){
  22. $table = $table->whereIn('case', explode(",",$request->get('case')) );
  23. }
  24. if(($request->has('search'))){
  25. $table = $table->where('name', 'like', $request->get('search')."%");
  26. }
  27. $table = $table->orderBy($request->get('order','updated_at'),$request->get('dir','desc'));
  28. $count = $table->count();
  29. $table = $table->skip($request->get("offset",0))
  30. ->take($request->get('limit',1000));
  31. $result = $table->get();
  32. return $this->ok(["rows"=>RelationResource::collection($result),"count"=>$count]);
  33. }
  34. /**
  35. * Store a newly created resource in storage.
  36. *
  37. * @param \Illuminate\Http\Request $request
  38. * @return \Illuminate\Http\Response
  39. */
  40. public function store(Request $request)
  41. {
  42. //
  43. $user = AuthApi::current($request);
  44. if(!$user){
  45. return $this->error(__('auth.failed'),[],401);
  46. }
  47. //TODO 判断权限
  48. $validated = $request->validate([
  49. 'name' => 'required',
  50. ]);
  51. $case = $request->get('case','');
  52. $new = new Relation;
  53. $new->name = $validated['name'];
  54. if($request->has('case')){
  55. $new->case = $request->get('case');
  56. }else{
  57. $new->case = null;
  58. }
  59. if($request->has('from')){
  60. $new->from = json_encode($request->get('from'),JSON_UNESCAPED_UNICODE);
  61. }else{
  62. $new->from = null;
  63. }
  64. if($request->has('to')){
  65. $new->to = json_encode($request->get('to'),JSON_UNESCAPED_UNICODE);
  66. }else{
  67. $new->to = null;
  68. }
  69. $new->editor_id = $user['user_uid'];
  70. $new->save();
  71. return $this->ok(new RelationResource($new));
  72. }
  73. /**
  74. * Display the specified resource.
  75. *
  76. * @param \App\Models\Relation $relation
  77. * @return \Illuminate\Http\Response
  78. */
  79. public function show(Relation $relation)
  80. {
  81. //
  82. return $this->ok(new RelationResource($relation));
  83. }
  84. /**
  85. * Update the specified resource in storage.
  86. *
  87. * @param \Illuminate\Http\Request $request
  88. * @param \App\Models\Relation $relation
  89. * @return \Illuminate\Http\Response
  90. */
  91. public function update(Request $request, Relation $relation)
  92. {
  93. //
  94. $user = AuthApi::current($request);
  95. if(!$user){
  96. return $this->error(__('auth.failed'));
  97. }
  98. $relation->name = $request->get('name');
  99. if($request->has('case')){
  100. $relation->case = $request->get('case');
  101. }else{
  102. $relation->case = null;
  103. }
  104. if($request->has('from')){
  105. $relation->from = json_encode($request->get('from'),JSON_UNESCAPED_UNICODE);
  106. }else{
  107. $relation->from = null;
  108. }
  109. if($request->has('to')){
  110. $relation->to = json_encode($request->get('to'),JSON_UNESCAPED_UNICODE);
  111. }else{
  112. $relation->to = null;
  113. }
  114. $relation->editor_id = $user['user_uid'];
  115. $relation->save();
  116. return $this->ok(new RelationResource($relation));
  117. }
  118. /**
  119. * Remove the specified resource from storage.
  120. *
  121. * @param \Illuminate\Http\Request $request
  122. * @param \App\Models\Relation $relation
  123. * @return \Illuminate\Http\Response
  124. */
  125. public function destroy(Request $request,Relation $relation)
  126. {
  127. //
  128. $user = AuthApi::current($request);
  129. if(!$user){
  130. return $this->error(__('auth.failed'));
  131. }
  132. //TODO 判断当前用户是否有权限
  133. $delete = 0;
  134. $delete = $relation->delete();
  135. return $this->ok($delete);
  136. }
  137. public function export(){
  138. $spreadsheet = new Spreadsheet();
  139. $activeWorksheet = $spreadsheet->getActiveSheet();
  140. $activeWorksheet->setCellValue('A1', 'id');
  141. $activeWorksheet->setCellValue('B1', 'name');
  142. $activeWorksheet->setCellValue('C1', 'case');
  143. $activeWorksheet->setCellValue('D1', 'to');
  144. $nissaya = Relation::cursor();
  145. $currLine = 2;
  146. foreach ($nissaya as $key => $row) {
  147. # code...
  148. $activeWorksheet->setCellValue("A{$currLine}", $row->id);
  149. $activeWorksheet->setCellValue("B{$currLine}", $row->name);
  150. $activeWorksheet->setCellValue("C{$currLine}", $row->case);
  151. $activeWorksheet->setCellValue("D{$currLine}", $row->to);
  152. $currLine++;
  153. }
  154. $writer = new Xlsx($spreadsheet);
  155. header('Content-Type: application/vnd.ms-excel');
  156. header('Content-Disposition: attachment; filename="relation.xlsx"');
  157. $writer->save("php://output");
  158. }
  159. public function import(Request $request){
  160. $user = AuthApi::current($request);
  161. if(!$user){
  162. return $this->error(__('auth.failed'));
  163. }
  164. $filename = $request->get('filename');
  165. $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
  166. $reader->setReadDataOnly(true);
  167. $spreadsheet = $reader->load($filename);
  168. $activeWorksheet = $spreadsheet->getActiveSheet();
  169. $currLine = 2;
  170. $countFail = 0;
  171. $error = "";
  172. do {
  173. # code...
  174. $id = $activeWorksheet->getCell("A{$currLine}")->getValue();
  175. $name = $activeWorksheet->getCell("B{$currLine}")->getValue();
  176. $case = $activeWorksheet->getCell("C{$currLine}")->getValue();
  177. $to = $activeWorksheet->getCell("D{$currLine}")->getValue();
  178. if(!empty($name)){
  179. //查询是否有冲突数据
  180. //查询此id是否有旧数据
  181. if(!empty($id)){
  182. $oldRow = Relation::find($id);
  183. }
  184. //查询是否跟已有数据重复
  185. $row = Relation::where(['name'=>$name,'case'=>$case])->first();
  186. if(!$row){
  187. //不重复
  188. if(isset($oldRow) && $oldRow){
  189. //有旧的记录-修改旧数据
  190. $row = $oldRow;
  191. }else{
  192. //没找到旧的记录-新建
  193. $row = new Relation();
  194. }
  195. }else{
  196. //重复-如果与旧的id不同旧报错
  197. if(isset($oldRow) && $oldRow && $row->id !== $id){
  198. $error .= "重复的数据:{$id} - {$word}\n";
  199. $currLine++;
  200. $countFail++;
  201. continue;
  202. }
  203. }
  204. $row->name = $name;
  205. $row->case = $case;
  206. $row->to = $to;
  207. $row->editor_id = $user['user_uid'];
  208. $row->save();
  209. }else{
  210. break;
  211. }
  212. $currLine++;
  213. } while (true);
  214. return $this->ok(["success"=>$currLine-2-$countFail,'fail'=>($countFail)],$error);
  215. }
  216. }