WbwController.php 7.2 KB

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