RelationController.php 10 KB

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