NissayaEndingController.php 13 KB

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