WbwController.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Api\ChannelApi;
  4. use App\Http\Api\Mq;
  5. use App\Http\Api\ShareApi;
  6. use App\Models\Channel;
  7. use App\Models\PaliSentence;
  8. use App\Models\Sentence;
  9. use App\Models\Wbw;
  10. use App\Models\WbwBlock;
  11. use App\Services\AuthService;
  12. use App\Tools\Tools;
  13. use Illuminate\Http\Request;
  14. use Illuminate\Http\Response;
  15. use Illuminate\Support\Str;
  16. class WbwController extends Controller
  17. {
  18. /**
  19. * Display a listing of the resource.
  20. *
  21. * @return Response
  22. */
  23. public function index()
  24. {
  25. //
  26. }
  27. /**
  28. * Store a newly created resource in storage.
  29. * 新建多个
  30. * 如果存在,修改
  31. *
  32. * @return Response
  33. */
  34. public function store(Request $request)
  35. {
  36. //
  37. // 鉴权
  38. $user = AuthService::current($request);
  39. if (! $user) {
  40. // 未登录用户
  41. return $this->error(__('auth.failed'), [], 401);
  42. }
  43. $channel = Channel::where('uid', $request->input('channel_id'))->first();
  44. if (! $channel) {
  45. return $this->error(__('auth.failed'));
  46. }
  47. if ($channel->owner_uid !== $user['user_uid']) {
  48. // 判断是否为协作
  49. $power = ShareApi::getResPower($user['user_uid'], $channel->uid);
  50. if ($power < 20) {
  51. return $this->error(__('auth.failed'), [], 403);
  52. }
  53. }
  54. // 查看WbwBlock是否已经建立
  55. $wbwBlockId = WbwBlock::where('book_id', $request->input('book'))
  56. ->where('paragraph', $request->input('para'))
  57. ->where('channel_uid', $request->input('channel_id'))
  58. ->value('uid');
  59. if (! Str::isUuid($wbwBlockId)) {
  60. $wbwBlock = new WbwBlock;
  61. $wbwBlockId = Str::uuid();
  62. $wbwBlock->id = app('snowflake')->id();
  63. $wbwBlock->uid = $wbwBlockId;
  64. $wbwBlock->creator_uid = $user['user_uid'];
  65. $wbwBlock->editor_id = $user['user_id'];
  66. $wbwBlock->book_id = $request->input('book');
  67. $wbwBlock->paragraph = $request->input('para');
  68. $wbwBlock->channel_uid = $request->input('channel_id');
  69. $wbwBlock->lang = $channel->lang;
  70. $wbwBlock->status = $channel->status;
  71. $wbwBlock->create_time = time() * 1000;
  72. $wbwBlock->modify_time = time() * 1000;
  73. $wbwBlock->save();
  74. }
  75. $wbw = Wbw::where('block_uid', $wbwBlockId)
  76. ->where('wid', $request->input('sn'))
  77. ->first();
  78. $sent = PaliSentence::where('book', $request->input('book'))
  79. ->where('paragraph', $request->input('para'))
  80. ->where('word_begin', '<=', $request->input('sn'))
  81. ->where('word_end', '>=', $request->input('sn'))
  82. ->first();
  83. if (! $wbw) {
  84. // 建立一个句子的逐词解析数据
  85. // 找到句子
  86. $channelId = ChannelApi::getSysChannel('_System_Wbw_VRI_');
  87. $wbwContent = Sentence::where('book_id', $sent->book)
  88. ->where('paragraph', $sent->paragraph)
  89. ->where('word_start', $sent->word_begin)
  90. ->where('word_end', $sent->word_end)
  91. ->where('channel_uid', $channelId)
  92. ->value('content');
  93. $words = json_decode($wbwContent);
  94. foreach ($words as $word) {
  95. // code...
  96. $xmlObj = simplexml_load_string('<word></word>');
  97. $xmlObj->addChild('id', "{$sent->book}-{$sent->paragraph}-{$word->sn[0]}");
  98. $xmlObj->addChild('pali', $word->word->value)->addAttribute('status', 0);
  99. $xmlObj->addChild('real', $word->real->value)->addAttribute('status', 0);
  100. $xmlObj->addChild('type', $word->type->value)->addAttribute('status', 0);
  101. $xmlObj->addChild('gramma', $word->grammar->value)->addAttribute('status', 0);
  102. $xmlObj->addChild('case', $word->case->value)->addAttribute('status', 0);
  103. $xmlObj->addChild('style', $word->style->value)->addAttribute('status', 0);
  104. $xmlObj->addChild('org', $word->factors->value)->addAttribute('status', 0);
  105. $xmlObj->addChild('om', $word->factorMeaning->value)->addAttribute('status', 0);
  106. $xmlObj->addChild('status', 1);
  107. $xml = $xmlObj->asXml();
  108. $xml = str_replace('<?xml version="1.0"?>', '', $xml);
  109. $newWbw = new Wbw;
  110. $newWbw->id = app('snowflake')->id();
  111. $newWbw->uid = Str::uuid();
  112. $newWbw->creator_uid = $channel->owner_uid;
  113. $newWbw->editor_id = $user['user_id'];
  114. $newWbw->book_id = $request->input('book');
  115. $newWbw->paragraph = $request->input('para');
  116. $newWbw->wid = $word->sn[0];
  117. $newWbw->block_uid = $wbwBlockId;
  118. $newWbw->data = $xml;
  119. $newWbw->word = $word->real->value;
  120. $newWbw->status = 0;
  121. $newWbw->create_time = time() * 1000;
  122. $newWbw->modify_time = time() * 1000;
  123. $newWbw->save();
  124. if ($word->sn[0] === $request->input('sn')) {
  125. $wbw = $newWbw;
  126. }
  127. }
  128. }
  129. $count = 0;
  130. $wbwId = [];
  131. foreach ($request->input('data') as $row) {
  132. $wbw = Wbw::where('block_uid', $wbwBlockId)
  133. ->where('wid', $row['sn'])
  134. ->first();
  135. if ($wbw) {
  136. $wbwData = '';
  137. foreach ($row['words'] as $word) {
  138. $xml = Tools::JsonToXml($word);
  139. $xml = str_replace('<?xml version="1.0"?>', '', $xml);
  140. $wbwData .= $xml;
  141. }
  142. $wbw->data = $wbwData;
  143. $wbw->status = 5;
  144. $wbw->save();
  145. $wbwId[] = $wbw->id;
  146. $count++;
  147. }
  148. }
  149. // 获取整个句子数据
  150. $corpus = new CorpusController;
  151. $wbwString = $corpus->getWbw(
  152. $request->input('book'),
  153. $request->input('para'),
  154. $sent->word_begin,
  155. $sent->word_end,
  156. $request->input('channel_id')
  157. );
  158. if ($wbwString) {
  159. $wbwSentence = json_decode($wbwString);
  160. } else {
  161. $wbwSentence = [];
  162. }
  163. if (count($wbwId) > 0) {
  164. Mq::publish('wbw-analyses', $wbwId);
  165. }
  166. return $this->ok(['rows' => $wbwSentence, 'count' => $count]);
  167. }
  168. /**
  169. * Display the specified resource.
  170. *
  171. * @return Response
  172. */
  173. public function show(Wbw $wbw)
  174. {
  175. //
  176. }
  177. /**
  178. * Update the specified resource in storage.
  179. *
  180. * @return Response
  181. */
  182. public function update(Request $request, Wbw $wbw)
  183. {
  184. //
  185. }
  186. /**
  187. * Remove the specified resource from storage.
  188. *
  189. * @return Response
  190. */
  191. public function destroy(Wbw $wbw)
  192. {
  193. //
  194. }
  195. }