NissayaEndingController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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','from','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. if($request->has('from')){
  82. $new->from = json_encode($request->get('from'),JSON_UNESCAPED_UNICODE);
  83. }else{
  84. $new->from = null;
  85. }
  86. $new->editor_id = $user['user_uid'];
  87. $new->save();
  88. return $this->ok(new NissayaEndingResource($new));
  89. }
  90. /**
  91. * Display the specified resource.
  92. *
  93. * @param \App\Models\NissayaEnding $nissayaEnding
  94. * @return \Illuminate\Http\Response
  95. */
  96. public function show(NissayaEnding $nissayaEnding)
  97. {
  98. //
  99. return $this->ok(new NissayaEndingResource($nissayaEnding));
  100. }
  101. public function nissaya_card(Request $request)
  102. {
  103. //
  104. $cardData = [];
  105. App::setLocale($request->get('lang'));
  106. $localTerm = ChannelApi::getSysChannel(
  107. "_System_Grammar_Term_".strtolower($request->get('lang'))."_",
  108. "_System_Grammar_Term_en_"
  109. );
  110. if(!$localTerm){
  111. return $this->error('no term channel');
  112. }
  113. $termTable = DhammaTerm::where('channal',$localTerm);
  114. $cardData['ending'] = $request->get('ending');
  115. $endingTerm = $termTable->where('word',$request->get('ending'))->first();
  116. if($endingTerm){
  117. $cardData['ending_tag'] = $endingTerm->tag;
  118. $cardData['ending_meaning'] = $endingTerm->meaning;
  119. $cardData['ending_note'] = $endingTerm->note;
  120. }
  121. $myEnding = NissayaEnding::where('ending',$request->get('ending'))
  122. ->groupBy('relation')
  123. ->select('relation')->get();
  124. if(count($myEnding) === 0){
  125. if(!isset($cardData['ending_note'])){
  126. $cardData['ending_note'] = "no record\n";
  127. }
  128. }
  129. $relations = Relation::whereIn('name',$myEnding)->get();
  130. if(count($relations) > 0){
  131. $cardData['title_case'] = "本词";
  132. $cardData['title_relation'] = "关系";
  133. $cardData['title_local_relation'] = "关系";
  134. $cardData['title_local_link_to'] = "目标词特征";
  135. $cardData['title_content'] = "含义";
  136. $cardData['title_local_ending'] = "翻译建议";
  137. foreach ($relations as $key => $relation) {
  138. $relationInTerm = DhammaTerm::where('channal',$localTerm)
  139. ->where('word',$relation['name'])
  140. ->first();
  141. if(empty($relation->from)){
  142. $cardData['row'][] = ["relation"=>$relation->name];
  143. continue;
  144. }
  145. $from = json_decode($relation->from);
  146. if(isset($from->case)){
  147. $cases = $from->case;
  148. $localCase =[];
  149. foreach ($cases as $case) {
  150. $localCase[] = ['label'=>__("grammar.".$case),
  151. 'link'=>config('mint.server.dashboard_base_path').'/term/list/'.$case
  152. ];
  153. }
  154. # 格位
  155. $newLine['case'] = $localCase;
  156. }
  157. if(isset($from->spell)){
  158. $newLine['spell'] = $from->spell;
  159. }
  160. //连接到
  161. $linkTos = json_decode($relation->to);
  162. if(count($linkTos)>0){
  163. $localTo =[];
  164. foreach ($linkTos as $to) {
  165. $localTo[] = ['label'=>__("grammar.".$to),
  166. 'link'=>config('mint.server.dashboard_base_path').'/term/list/'.$case
  167. ];
  168. }
  169. # 格位
  170. $newLine['to'] = $localTo;
  171. }
  172. //含义 用分类字段的term 数据
  173. if(isset($relation['category']) && !empty($relation['category'])){
  174. $localCategory = DhammaTerm::where('channal',$localTerm)
  175. ->where('word',$relation['category'])
  176. ->first();
  177. if($localCategory){
  178. $newLine['category_note'] = $localCategory->note;
  179. }
  180. }
  181. /**
  182. * 翻译建议
  183. * relation 和 from 都匹配成功
  184. * from 为空 只匹配 relation
  185. */
  186. $arrLocalEnding = array();
  187. $localEndings = NissayaEnding::where('relation',$relation['name'])
  188. ->where('lang',$request->get('lang'))
  189. ->get();
  190. foreach ($localEndings as $localEnding) {
  191. if(empty($localEnding->from) || $localEnding->from===$relation->from){
  192. $arrLocalEnding[]=$localEnding->ending;
  193. }
  194. }
  195. $newLine['local_ending'] = implode(';',$arrLocalEnding);
  196. //本地语言 关系名称
  197. if($relationInTerm){
  198. $newLine['local_relation'] = $relationInTerm->meaning;
  199. }
  200. //关系名称
  201. $newLine['relation'] = $relation['name'];
  202. $newLine['relation_link'] = config('mint.server.dashboard_base_path').'/term/list/'.$relation['name'];
  203. $cardData['row'][] = $newLine;
  204. }
  205. }
  206. $m = new \Mustache_Engine(array('entity_flags'=>ENT_QUOTES));
  207. $tpl = file_get_contents(resource_path("mustache/nissaya_ending_card.tpl"));
  208. $md = $m->render($tpl,$cardData);
  209. return $this->ok($md);
  210. }
  211. /**
  212. * Update the specified resource in storage.
  213. *
  214. * @param \Illuminate\Http\Request $request
  215. * @param \App\Models\NissayaEnding $nissayaEnding
  216. * @return \Illuminate\Http\Response
  217. */
  218. public function update(Request $request, NissayaEnding $nissayaEnding)
  219. {
  220. //
  221. $user = AuthApi::current($request);
  222. if(!$user){
  223. return $this->error(__('auth.failed'));
  224. }
  225. //查询是否重复
  226. /*
  227. $table = NissayaEnding::where('ending',$request->get('ending'))
  228. ->where('lang',$request->get('lang'))
  229. ->where('relation',$request->get('relation'));
  230. $from = json_encode($request->get('from'),JSON_UNESCAPED_UNICODE);
  231. if(empty($from)){
  232. $table = $table->whereNull('from');
  233. }else{
  234. $json = $request->get('from');
  235. $table = $table->whereJsonContains('from',['case'=>$json['case']]);
  236. }
  237. if($table->exists()){
  238. return $this->error(__('validation.exists',['name']));
  239. }
  240. */
  241. $nissayaEnding->ending = $request->get('ending');
  242. $nissayaEnding->strlen = mb_strlen($request->get('ending'),"UTF-8") ;
  243. $nissayaEnding->lang = $request->get('lang');
  244. $nissayaEnding->relation = $request->get('relation');
  245. if($request->has('from') && !empty($request->get('from'))){
  246. $nissayaEnding->from = json_encode($request->get('from'),JSON_UNESCAPED_UNICODE);
  247. }else{
  248. $nissayaEnding->from = null;
  249. }
  250. $nissayaEnding->editor_id = $user['user_uid'];
  251. $nissayaEnding->save();
  252. return $this->ok(new NissayaEndingResource($nissayaEnding));
  253. }
  254. /**
  255. * Remove the specified resource from storage.
  256. *
  257. * @param \Illuminate\Http\Request $request
  258. * @param \App\Models\NissayaEnding $nissayaEnding
  259. * @return \Illuminate\Http\Response
  260. */
  261. public function destroy(Request $request,NissayaEnding $nissayaEnding)
  262. {
  263. //
  264. $user = AuthApi::current($request);
  265. if(!$user){
  266. return $this->error(__('auth.failed'));
  267. }
  268. //TODO 判断当前用户是否有权限
  269. $delete = 0;
  270. $delete = $nissayaEnding->delete();
  271. return $this->ok($delete);
  272. }
  273. public function export(){
  274. $spreadsheet = new Spreadsheet();
  275. $activeWorksheet = $spreadsheet->getActiveSheet();
  276. $activeWorksheet->setCellValue('A1', 'id');
  277. $activeWorksheet->setCellValue('B1', 'ending');
  278. $activeWorksheet->setCellValue('C1', 'lang');
  279. $activeWorksheet->setCellValue('D1', 'relation');
  280. $nissaya = NissayaEnding::cursor();
  281. $currLine = 2;
  282. foreach ($nissaya as $key => $row) {
  283. # code...
  284. $activeWorksheet->setCellValue("A{$currLine}", $row->id);
  285. $activeWorksheet->setCellValue("B{$currLine}", $row->ending);
  286. $activeWorksheet->setCellValue("C{$currLine}", $row->lang);
  287. $activeWorksheet->setCellValue("D{$currLine}", $row->relation);
  288. $activeWorksheet->setCellValue("E{$currLine}", $row->case);
  289. $currLine++;
  290. }
  291. $writer = new Xlsx($spreadsheet);
  292. header('Content-Type: application/vnd.ms-excel');
  293. header('Content-Disposition: attachment; filename="nissaya-ending.xlsx"');
  294. $writer->save("php://output");
  295. }
  296. public function import(Request $request){
  297. $user = AuthApi::current($request);
  298. if(!$user){
  299. return $this->error(__('auth.failed'));
  300. }
  301. $filename = $request->get('filename');
  302. $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
  303. $reader->setReadDataOnly(true);
  304. $spreadsheet = $reader->load($filename);
  305. $activeWorksheet = $spreadsheet->getActiveSheet();
  306. $currLine = 2;
  307. $countFail = 0;
  308. $error = "";
  309. do {
  310. # code...
  311. $id = $activeWorksheet->getCell("A{$currLine}")->getValue();
  312. $ending = $activeWorksheet->getCell("B{$currLine}")->getValue();
  313. $lang = $activeWorksheet->getCell("C{$currLine}")->getValue();
  314. $relation = $activeWorksheet->getCell("D{$currLine}")->getValue();
  315. $case = $activeWorksheet->getCell("E{$currLine}")->getValue();
  316. if(!empty($ending)){
  317. //查询是否有冲突数据
  318. //查询此id是否有旧数据
  319. if(!empty($id)){
  320. $oldRow = NissayaEnding::find($id);
  321. }
  322. //查询是否跟已有数据重复
  323. $row = NissayaEnding::where(['ending'=>$ending,'relation'=>$relation,'case'=>$case])->first();
  324. if(!$row){
  325. //不重复
  326. if(isset($oldRow) && $oldRow){
  327. //有旧的记录-修改旧数据
  328. $row = $oldRow;
  329. }else{
  330. //没找到旧的记录-新建
  331. $row = new NissayaEnding();
  332. }
  333. }else{
  334. //重复-如果与旧的id不同旧报错
  335. if(isset($oldRow) && $oldRow && $row->id !== $id){
  336. $error .= "重复的数据:{$id} - {$word}\n";
  337. $currLine++;
  338. $countFail++;
  339. continue;
  340. }
  341. }
  342. $row->ending = $ending;
  343. $row->strlen = mb_strlen($ending,"UTF-8") ;
  344. $row->lang = $lang;
  345. $row->relation = $relation;
  346. $row->case = $case;
  347. $row->editor_id = $user['user_uid'];
  348. $row->save();
  349. }else{
  350. break;
  351. }
  352. $currLine++;
  353. } while (true);
  354. return $this->ok(["success"=>$currLine-2-$countFail,'fail'=>($countFail)],$error);
  355. }
  356. }