NissayaEndingController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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)
  133. ->where('word',$relation['name'])
  134. ->first();
  135. if(empty($relation->from)){
  136. $cardData['row'][] = ["relation"=>$relation->name];
  137. continue;
  138. }
  139. $from = json_decode($relation->from);
  140. if(isset($from->case)){
  141. $cases = $from->case;
  142. $localCase =[];
  143. foreach ($cases as $case) {
  144. $localCase[] = ['label'=>__("grammar.".$case),
  145. 'link'=>env('DASHBOARD_URL').'/term/list/'.$case
  146. ];
  147. }
  148. # 格位
  149. $newLine['case'] = $localCase;
  150. }
  151. if(isset($from->spell)){
  152. $newLine['spell'] = $from->spell;
  153. }
  154. //含义
  155. if($relationInTerm){
  156. $newLine['other_meaning'] = $relationInTerm->other_meaning;
  157. $newLine['note'] = $relationInTerm->note;
  158. if(!empty($relationInTerm->note)){
  159. $newLine['summary'] = explode("\n",$relationInTerm->note)[0];
  160. }
  161. }
  162. //翻译建议
  163. $localEnding = '';
  164. $localEndingRecord = NissayaEnding::where('relation',$relation['name'])
  165. ->where('lang',$request->get('lang'));
  166. if(!empty($case)){
  167. $localEndingRecord = $localEndingRecord->where('case',$case);
  168. }
  169. $localLangs = $localEndingRecord->get();
  170. foreach ($localLangs as $localLang) {
  171. # code...
  172. $localEnding .= $localLang->ending.",";
  173. }
  174. $newLine['local_ending'] = $localEnding;
  175. //本地语言 关系名称
  176. if($relationInTerm){
  177. $newLine['local_relation'] = $relationInTerm->meaning;
  178. }
  179. //关系名称
  180. $newLine['relation'] = $relation['name'];
  181. $newLine['relation_link'] = env('DASHBOARD_URL').'/term/list/'.$relation['name'];
  182. $cardData['row'][] = $newLine;
  183. }
  184. }
  185. $m = new \Mustache_Engine(array('entity_flags'=>ENT_QUOTES));
  186. $tpl = file_get_contents(resource_path("mustache/nissaya_ending_card.tpl"));
  187. $md = $m->render($tpl,$cardData);
  188. return $this->ok($md);
  189. }
  190. /**
  191. * Update the specified resource in storage.
  192. *
  193. * @param \Illuminate\Http\Request $request
  194. * @param \App\Models\NissayaEnding $nissayaEnding
  195. * @return \Illuminate\Http\Response
  196. */
  197. public function update(Request $request, NissayaEnding $nissayaEnding)
  198. {
  199. //
  200. $user = AuthApi::current($request);
  201. if(!$user){
  202. return $this->error(__('auth.failed'));
  203. }
  204. //查询是否重复
  205. if(NissayaEnding::where('ending',$request->get('ending'))
  206. ->where('lang',$request->get('lang'))
  207. ->where('relation',$request->get('relation'))
  208. ->where('case',$request->get('case'))
  209. ->exists()){
  210. return $this->error(__('validation.exists',['name']));
  211. }
  212. $nissayaEnding->ending = $request->get('ending');
  213. $nissayaEnding->strlen = mb_strlen($request->get('ending'),"UTF-8") ;
  214. $nissayaEnding->lang = $request->get('lang');
  215. $nissayaEnding->relation = $request->get('relation');
  216. $nissayaEnding->case = $request->get('case');
  217. $nissayaEnding->editor_id = $user['user_uid'];
  218. $nissayaEnding->save();
  219. return $this->ok(new NissayaEndingResource($nissayaEnding));
  220. }
  221. /**
  222. * Remove the specified resource from storage.
  223. *
  224. * @param \Illuminate\Http\Request $request
  225. * @param \App\Models\NissayaEnding $nissayaEnding
  226. * @return \Illuminate\Http\Response
  227. */
  228. public function destroy(Request $request,NissayaEnding $nissayaEnding)
  229. {
  230. //
  231. $user = AuthApi::current($request);
  232. if(!$user){
  233. return $this->error(__('auth.failed'));
  234. }
  235. //TODO 判断当前用户是否有权限
  236. $delete = 0;
  237. $delete = $nissayaEnding->delete();
  238. return $this->ok($delete);
  239. }
  240. public function export(){
  241. $spreadsheet = new Spreadsheet();
  242. $activeWorksheet = $spreadsheet->getActiveSheet();
  243. $activeWorksheet->setCellValue('A1', 'id');
  244. $activeWorksheet->setCellValue('B1', 'ending');
  245. $activeWorksheet->setCellValue('C1', 'lang');
  246. $activeWorksheet->setCellValue('D1', 'relation');
  247. $nissaya = NissayaEnding::cursor();
  248. $currLine = 2;
  249. foreach ($nissaya as $key => $row) {
  250. # code...
  251. $activeWorksheet->setCellValue("A{$currLine}", $row->id);
  252. $activeWorksheet->setCellValue("B{$currLine}", $row->ending);
  253. $activeWorksheet->setCellValue("C{$currLine}", $row->lang);
  254. $activeWorksheet->setCellValue("D{$currLine}", $row->relation);
  255. $activeWorksheet->setCellValue("E{$currLine}", $row->case);
  256. $currLine++;
  257. }
  258. $writer = new Xlsx($spreadsheet);
  259. header('Content-Type: application/vnd.ms-excel');
  260. header('Content-Disposition: attachment; filename="nissaya-ending.xlsx"');
  261. $writer->save("php://output");
  262. }
  263. public function import(Request $request){
  264. $user = AuthApi::current($request);
  265. if(!$user){
  266. return $this->error(__('auth.failed'));
  267. }
  268. $filename = $request->get('filename');
  269. $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
  270. $reader->setReadDataOnly(true);
  271. $spreadsheet = $reader->load($filename);
  272. $activeWorksheet = $spreadsheet->getActiveSheet();
  273. $currLine = 2;
  274. $countFail = 0;
  275. $error = "";
  276. do {
  277. # code...
  278. $id = $activeWorksheet->getCell("A{$currLine}")->getValue();
  279. $ending = $activeWorksheet->getCell("B{$currLine}")->getValue();
  280. $lang = $activeWorksheet->getCell("C{$currLine}")->getValue();
  281. $relation = $activeWorksheet->getCell("D{$currLine}")->getValue();
  282. $case = $activeWorksheet->getCell("E{$currLine}")->getValue();
  283. if(!empty($ending)){
  284. //查询是否有冲突数据
  285. //查询此id是否有旧数据
  286. if(!empty($id)){
  287. $oldRow = NissayaEnding::find($id);
  288. }
  289. //查询是否跟已有数据重复
  290. $row = NissayaEnding::where(['ending'=>$ending,'relation'=>$relation,'case'=>$case])->first();
  291. if(!$row){
  292. //不重复
  293. if(isset($oldRow) && $oldRow){
  294. //有旧的记录-修改旧数据
  295. $row = $oldRow;
  296. }else{
  297. //没找到旧的记录-新建
  298. $row = new NissayaEnding();
  299. }
  300. }else{
  301. //重复-如果与旧的id不同旧报错
  302. if(isset($oldRow) && $oldRow && $row->id !== $id){
  303. $error .= "重复的数据:{$id} - {$word}\n";
  304. $currLine++;
  305. $countFail++;
  306. continue;
  307. }
  308. }
  309. $row->ending = $ending;
  310. $row->strlen = mb_strlen($ending,"UTF-8") ;
  311. $row->lang = $lang;
  312. $row->relation = $relation;
  313. $row->case = $case;
  314. $row->editor_id = $user['user_uid'];
  315. $row->save();
  316. }else{
  317. break;
  318. }
  319. $currLine++;
  320. } while (true);
  321. return $this->ok(["success"=>$currLine-2-$countFail,'fail'=>($countFail)],$error);
  322. }
  323. }