NissayaEndingController.php 9.0 KB

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