NissayaEndingController.php 12 KB

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