NissayaEndingController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\NissayaEnding;
  4. use App\Models\Relation;
  5. use App\Models\DhammaTerm;
  6. use Illuminate\Http\Request;
  7. use App\Http\Resources\NissayaEndingResource;
  8. use App\Http\Api\AuthApi;
  9. use App\Http\Api\ChannelApi;
  10. use Illuminate\Support\Facades\App;
  11. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  12. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  13. use mustache\mustache;
  14. class NissayaEndingController extends Controller
  15. {
  16. /**
  17. * Display a listing of the resource.
  18. *
  19. * @return \Illuminate\Http\Response
  20. */
  21. public function index(Request $request)
  22. {
  23. //
  24. $table = NissayaEnding::select(['id','ending','lang','relation','case','count','editor_id','updated_at']);
  25. if(($request->has('case'))){
  26. $table->whereIn('case', explode(",",$request->get('case')) );
  27. }
  28. if(($request->has('lang'))){
  29. $table->whereIn('lang', explode(",",$request->get('lang')) );
  30. }
  31. if(($request->has('relation'))){
  32. $table->where('relation', $request->get('relation'));
  33. }
  34. if(($request->has('case'))){
  35. $table->where('case', $request->get('case'));
  36. }
  37. if(($request->has('search'))){
  38. $table->where('ending', 'like', "%".$request->get('search')."%");
  39. }
  40. $count = $table->count();
  41. $table->orderBy($request->get('order','updated_at'),
  42. $request->get('dir','desc'));
  43. $table->skip($request->get("offset",0))
  44. ->take($request->get('limit',1000));
  45. $result = $table->get();
  46. return $this->ok(["rows"=>NissayaEndingResource::collection($result),"count"=>$count]);
  47. }
  48. public function vocabulary(Request $request){
  49. $result = NissayaEnding::select(['ending'])
  50. ->where('lang', $request->get('lang') )
  51. ->groupBy('ending')
  52. ->get();
  53. return $this->ok(["rows"=>$result,"count"=>count($result)]);
  54. }
  55. /**
  56. * Store a newly created resource in storage.
  57. *
  58. * @param \Illuminate\Http\Request $request
  59. * @return \Illuminate\Http\Response
  60. */
  61. public function store(Request $request)
  62. {
  63. //
  64. $user = AuthApi::current($request);
  65. if(!$user){
  66. return $this->error(__('auth.failed'));
  67. }
  68. //TODO 判断权限
  69. $validated = $request->validate([
  70. 'ending' => 'required',
  71. 'lang' => 'required',
  72. ]);
  73. $new = new NissayaEnding;
  74. $new->ending = $validated['ending'];
  75. $new->strlen = mb_strlen($validated['ending'],"UTF-8") ;
  76. $new->lang = $validated['lang'];
  77. $new->relation = $request->get('relation');
  78. $new->case = $request->get('case');
  79. $new->editor_id = $user['user_uid'];
  80. $new->save();
  81. return $this->ok(new NissayaEndingResource($new));
  82. }
  83. /**
  84. * Display the specified resource.
  85. *
  86. * @param \App\Models\NissayaEnding $nissayaEnding
  87. * @return \Illuminate\Http\Response
  88. */
  89. public function show(NissayaEnding $nissayaEnding)
  90. {
  91. //
  92. return $this->ok(new NissayaEndingResource($nissayaEnding));
  93. }
  94. public function nissaya_card(Request $request)
  95. {
  96. //
  97. $cardData = [];
  98. App::setLocale($request->get('lang'));
  99. $localTerm = ChannelApi::getSysChannel(
  100. "_System_Grammar_Term_".strtolower($request->get('lang'))."_",
  101. "_System_Grammar_Term_en_"
  102. );
  103. if(!$localTerm){
  104. return $this->error('no term channel');
  105. }
  106. $termTable = DhammaTerm::where('channal',$localTerm);
  107. $cardData['ending'] = $request->get('ending');
  108. $endingTerm = $termTable->where('word',$request->get('ending'))->first();
  109. if($endingTerm){
  110. $cardData['ending_tag'] = $endingTerm->tag;
  111. $cardData['ending_meaning'] = $endingTerm->meaning;
  112. $cardData['ending_note'] = $endingTerm->note;
  113. }
  114. $myEnding = NissayaEnding::where('ending',$request->get('ending'))
  115. ->groupBy('relation')
  116. ->select('relation')->get();
  117. if(count($myEnding) === 0){
  118. if(!isset($cardData['ending_note'])){
  119. $cardData['ending_note'] = "no record\n";
  120. }
  121. }
  122. $relations = Relation::whereIn('name',$myEnding)->get();
  123. if(count($relations) > 0){
  124. $cardData['title_case'] = "格位";
  125. $cardData['title_content'] = "含义";
  126. $cardData['title_local_ending'] = "翻译建议";
  127. $cardData['title_local_relation'] = "关系";
  128. $cardData['title_relation'] = "关系";
  129. foreach ($relations as $key => $relation) {
  130. $relationInTerm = DhammaTerm::where('channal',$localTerm)->where('word',$relation['name'])->first();
  131. if(empty($relation->from)){
  132. $cardData['row'][] = ["relation"=>$relation->name];
  133. continue;
  134. }
  135. $from = json_decode($relation->from);
  136. if(isset($from->case)){
  137. $cases = $from->case;
  138. $localCase =[];
  139. foreach ($cases as $case) {
  140. $localCase[] = __("grammar.".$case);
  141. }
  142. # 格位
  143. $newLine['case'] = implode(';',$localCase);
  144. }
  145. if(isset($from->spell)){
  146. $newLine['spell'] = $from->spell;
  147. }
  148. //含义
  149. if($relationInTerm){
  150. $newLine['other_meaning'] = $relationInTerm->other_meaning;
  151. $newLine['note'] = $relationInTerm->note;
  152. if(!empty($relationInTerm->note)){
  153. $newLine['summary'] = explode("\n",$relationInTerm->note)[0];
  154. }
  155. }
  156. //翻译建议
  157. $localEnding = '';
  158. $localEndingRecord = NissayaEnding::where('relation',$relation['name'])
  159. ->where('lang',$request->get('lang'));
  160. if(!empty($case)){
  161. $localEndingRecord = $localEndingRecord->where('case',$case);
  162. }
  163. $localLangs = $localEndingRecord->get();
  164. foreach ($localLangs as $localLang) {
  165. # code...
  166. $localEnding .= $localLang->ending.",";
  167. }
  168. $newLine['local_ending'] = $localEnding;
  169. //本地语言 关系名称
  170. if($relationInTerm){
  171. $newLine['local_relation'] = $relationInTerm->meaning;
  172. }
  173. //关系名称
  174. $newLine['relation'] = strtoupper($relation['name']);
  175. $cardData['row'][] = $newLine;
  176. }
  177. }
  178. $m = new \Mustache_Engine(array('entity_flags'=>ENT_QUOTES));
  179. $tpl = file_get_contents(resource_path("mustache/nissaya_ending_card.tpl"));
  180. $md = $m->render($tpl,$cardData);
  181. return $this->ok($md);
  182. }
  183. /**
  184. * Update the specified resource in storage.
  185. *
  186. * @param \Illuminate\Http\Request $request
  187. * @param \App\Models\NissayaEnding $nissayaEnding
  188. * @return \Illuminate\Http\Response
  189. */
  190. public function update(Request $request, NissayaEnding $nissayaEnding)
  191. {
  192. //
  193. $user = AuthApi::current($request);
  194. if(!$user){
  195. return $this->error(__('auth.failed'));
  196. }
  197. //查询是否重复
  198. if(NissayaEnding::where('ending',$request->get('ending'))
  199. ->where('lang',$request->get('lang'))
  200. ->where('relation',$request->get('relation'))
  201. ->where('case',$request->get('case'))
  202. ->exists()){
  203. return $this->error(__('validation.exists',['name']));
  204. }
  205. $nissayaEnding->ending = $request->get('ending');
  206. $nissayaEnding->strlen = mb_strlen($request->get('ending'),"UTF-8") ;
  207. $nissayaEnding->lang = $request->get('lang');
  208. $nissayaEnding->relation = $request->get('relation');
  209. $nissayaEnding->case = $request->get('case');
  210. $nissayaEnding->editor_id = $user['user_uid'];
  211. $nissayaEnding->save();
  212. return $this->ok(new NissayaEndingResource($nissayaEnding));
  213. }
  214. /**
  215. * Remove the specified resource from storage.
  216. *
  217. * @param \Illuminate\Http\Request $request
  218. * @param \App\Models\NissayaEnding $nissayaEnding
  219. * @return \Illuminate\Http\Response
  220. */
  221. public function destroy(Request $request,NissayaEnding $nissayaEnding)
  222. {
  223. //
  224. $user = AuthApi::current($request);
  225. if(!$user){
  226. return $this->error(__('auth.failed'));
  227. }
  228. //TODO 判断当前用户是否有权限
  229. $delete = 0;
  230. $delete = $nissayaEnding->delete();
  231. return $this->ok($delete);
  232. }
  233. public function export(){
  234. $spreadsheet = new Spreadsheet();
  235. $activeWorksheet = $spreadsheet->getActiveSheet();
  236. $activeWorksheet->setCellValue('A1', 'id');
  237. $activeWorksheet->setCellValue('B1', 'ending');
  238. $activeWorksheet->setCellValue('C1', 'lang');
  239. $activeWorksheet->setCellValue('D1', 'relation');
  240. $nissaya = NissayaEnding::cursor();
  241. $currLine = 2;
  242. foreach ($nissaya as $key => $row) {
  243. # code...
  244. $activeWorksheet->setCellValue("A{$currLine}", $row->id);
  245. $activeWorksheet->setCellValue("B{$currLine}", $row->ending);
  246. $activeWorksheet->setCellValue("C{$currLine}", $row->lang);
  247. $activeWorksheet->setCellValue("D{$currLine}", $row->relation);
  248. $activeWorksheet->setCellValue("E{$currLine}", $row->case);
  249. $currLine++;
  250. }
  251. $writer = new Xlsx($spreadsheet);
  252. header('Content-Type: application/vnd.ms-excel');
  253. header('Content-Disposition: attachment; filename="nissaya-ending.xlsx"');
  254. $writer->save("php://output");
  255. }
  256. public function import(Request $request){
  257. $user = AuthApi::current($request);
  258. if(!$user){
  259. return $this->error(__('auth.failed'));
  260. }
  261. $filename = $request->get('filename');
  262. $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
  263. $reader->setReadDataOnly(true);
  264. $spreadsheet = $reader->load($filename);
  265. $activeWorksheet = $spreadsheet->getActiveSheet();
  266. $currLine = 2;
  267. $countFail = 0;
  268. $error = "";
  269. do {
  270. # code...
  271. $id = $activeWorksheet->getCell("A{$currLine}")->getValue();
  272. $ending = $activeWorksheet->getCell("B{$currLine}")->getValue();
  273. $lang = $activeWorksheet->getCell("C{$currLine}")->getValue();
  274. $relation = $activeWorksheet->getCell("D{$currLine}")->getValue();
  275. $case = $activeWorksheet->getCell("E{$currLine}")->getValue();
  276. if(!empty($ending)){
  277. //查询是否有冲突数据
  278. //查询此id是否有旧数据
  279. if(!empty($id)){
  280. $oldRow = NissayaEnding::find($id);
  281. }
  282. //查询是否跟已有数据重复
  283. $row = NissayaEnding::where(['ending'=>$ending,'relation'=>$relation,'case'=>$case])->first();
  284. if(!$row){
  285. //不重复
  286. if(isset($oldRow) && $oldRow){
  287. //有旧的记录-修改旧数据
  288. $row = $oldRow;
  289. }else{
  290. //没找到旧的记录-新建
  291. $row = new NissayaEnding();
  292. }
  293. }else{
  294. //重复-如果与旧的id不同旧报错
  295. if(isset($oldRow) && $oldRow && $row->id !== $id){
  296. $error .= "重复的数据:{$id} - {$word}\n";
  297. $currLine++;
  298. $countFail++;
  299. continue;
  300. }
  301. }
  302. $row->ending = $ending;
  303. $row->strlen = mb_strlen($ending,"UTF-8") ;
  304. $row->lang = $lang;
  305. $row->relation = $relation;
  306. $row->case = $case;
  307. $row->editor_id = $user['user_uid'];
  308. $row->save();
  309. }else{
  310. break;
  311. }
  312. $currLine++;
  313. } while (true);
  314. return $this->ok(["success"=>$currLine-2-$countFail,'fail'=>($countFail)],$error);
  315. }
  316. }