RelationController.php 9.5 KB

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