RelationController.php 10.0 KB

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