2
0

RelationController.php 9.9 KB

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