SentenceInfoController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Sentence;
  4. use App\Models\PaliSentence;
  5. use App\Models\PaliText;
  6. use Illuminate\Http\Request;
  7. use App\Http\Resources\SentResource;
  8. use Illuminate\Support\Facades\Cache;
  9. class SentenceInfoController extends Controller
  10. {
  11. protected $_endParagraph;
  12. protected $_startParagraph;
  13. /**
  14. * Display a listing of the resource.
  15. *
  16. * @return \Illuminate\Http\Response
  17. */
  18. public function index(Request $request) {}
  19. /**
  20. * Store a newly created resource in storage.
  21. *
  22. * @param \Illuminate\Http\Request $request
  23. * @return \Illuminate\Http\Response
  24. */
  25. public function store(Request $request)
  26. {
  27. //
  28. }
  29. private function getSentProgress(Request $request, $date = '')
  30. {
  31. $channel = $request->get('channel');
  32. $from = $request->get('from');
  33. if ($request->has('to')) {
  34. $to = $request->get('to');
  35. } else {
  36. $to = $this->_endParagraph;
  37. }
  38. #默认完成度显示字符数
  39. # strlen
  40. # palistrlen 巴利语等效字符数
  41. # page
  42. # percent
  43. $view = 'strlen';
  44. if ($request->has('view')) {
  45. $view = $request->get('view');
  46. } else if ($request->has('type')) {
  47. $view = $request->get('type');
  48. }
  49. #一页书中的字符数
  50. $pageStrLen = 2000;
  51. if ($request->has('strlen')) {
  52. $pageStrLen = $request->get('strlen');
  53. }
  54. if ($request->has('pagelen')) {
  55. $pageStrLen = $request->get('pagelen');
  56. }
  57. # 页数
  58. $pageNumber = 300;
  59. if ($request->has('pages')) {
  60. $pageNumber = $request->get('pages');
  61. }
  62. $db = Sentence::where('sentences.channel_uid', $request->get('channel'))
  63. ->where('sentences.book_id', '>=', $request->get('book'))
  64. ->where('sentences.paragraph', '>=', $request->get('from'))
  65. ->where('sentences.paragraph', '<=', $to);
  66. if ($view === "palistrlen") {
  67. $db = $db->leftJoin('pali_texts', function ($join) {
  68. $join->on('sentences.book_id', '=', 'pali_texts.book');
  69. $join->on('sentences.paragraph', '=', 'pali_texts.paragraph');
  70. });
  71. }
  72. if (!empty($date)) {
  73. $db = $db->whereDate('sentences.created_at', '=', $date);
  74. }
  75. if ($view === "palistrlen") {
  76. return $db->sum('pali_texts.lenght');
  77. }
  78. $strlen = $db->sum('sentences.strlen');
  79. if (is_null($strlen) || $strlen === 0) {
  80. return 0;
  81. }
  82. #计算已完成百分比
  83. $percent = 0;
  84. if (($view === 'page' && !empty($request->get('pages'))) || $view === 'percent') {
  85. #计算完成的句子在巴利语句子表中的字符串长度百分比
  86. $db = Sentence::select(['book_id', 'paragraph', 'word_start', 'word_end'])
  87. ->where('channel_uid', $request->get('channel'))
  88. ->where('book_id', '>=', $request->get('book'))
  89. ->where('paragraph', '>=', $request->get('from'))
  90. ->where('paragraph', '<=', $to);
  91. if (!empty($date)) {
  92. $db = $db->whereDate('created_at', '=', $date);
  93. }
  94. $sentFinished = $db->get();
  95. #查询这些句子的总共等效巴利语字符数
  96. $allStrLen = PaliSentence::where('book', $request->get('book'))
  97. ->where('paragraph', '>=', $request->get('from'))
  98. ->where('paragraph', '<=', $to)
  99. ->sum('length');
  100. $para_strlen = 0;
  101. foreach ($sentFinished as $sent) {
  102. # code...
  103. $key_sent_id = $sent->book_id . '-' . $sent->paragraph . '-' . $sent->word_start . '-' . $sent->word_end;
  104. $para_strlen += Cache::remember(
  105. 'pali-sent/strlen/' . $key_sent_id,
  106. config('mint.cache.expire'),
  107. function () use ($sent) {
  108. return PaliSentence::where('book', $sent->book_id)
  109. ->where('paragraph', $sent->paragraph)
  110. ->where('word_begin', $sent->word_start)
  111. ->where('word_end', $sent->word_end)
  112. ->value('length');
  113. }
  114. );
  115. }
  116. $percent = $para_strlen / $allStrLen;
  117. }
  118. switch ($view) {
  119. case 'page':
  120. # 输出已经完成的页数
  121. if (!empty($request->get('pages'))) {
  122. #给了页码,用百分比计算
  123. $resulte = $percent * $request->get('pages');
  124. } else {
  125. #没给页码,用每页字符数计算
  126. $resulte = $strlen / $pageStrLen;
  127. }
  128. break;
  129. case 'percent': //百分比
  130. $resulte = sprintf('%.2f', $percent);
  131. break;
  132. case 'strlen':
  133. default:
  134. # code...
  135. $resulte = $strlen;
  136. break;
  137. }
  138. #保留小数点后两位
  139. $resulte = sprintf('%.2f', $resulte);
  140. return $resulte;
  141. }
  142. /**
  143. * 输出一张图片显示进度
  144. * Display the specified resource.
  145. *
  146. * @param \App\Models\Sentence $sentence
  147. * @return \Illuminate\Http\Response
  148. * http://127.0.0.1:8000/api/sentence/progress/image?channel=00ae2c48-c204-4082-ae79-79ba2740d506&&book=168&from=916&to=926&view=page&pages=400
  149. */
  150. public function showprogress(Request $request)
  151. {
  152. $resulte = $this->getSentProgress($request);
  153. $svg = "<svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' viewBox='0 0 100 25'>";
  154. switch ($request->get('view')) {
  155. case 'percent':
  156. # code...
  157. $resulte = $resulte * 100;
  158. $svg .= "<rect id='frontground' x='0' y='0' width='100' height='25' fill='#cccccc' ></rect>";
  159. $svg .= "<text id='bg_text' x='5' y='21' fill='#006600' style='font-size:25px;'>$resulte%</text>";
  160. $svg .= "<rect id='background' x='0' y='0' width='100' height='25' fill='#006600' clip-path='url(#textClipPath)'></rect>";
  161. $svg .= "<text id='bg_text' x='5' y='21' fill='#ffffff' style='font-size:25px;' clip-path='url(#textClipPath)'>$resulte%</text>";
  162. $svg .= "<clipPath id='textClipPath'>";
  163. $svg .= " <rect x='0' y='0' width='$resulte' height='25'></rect>";
  164. $svg .= "</clipPath>";
  165. break;
  166. case 'strlen':
  167. case 'page':
  168. default:
  169. $svg .= "<text id='bg_text' x='5' y='21' fill='#006600' style='font-size:25px;'>$resulte</text>";
  170. break;
  171. }
  172. $svg .= "</svg>";
  173. return response($svg, 200, [
  174. 'Content-Type' => 'image/svg+xml'
  175. ]);
  176. }
  177. //http://127.0.0.1:8000/api/sentence/progress/daily/image?channel=00ae2c48-c204-4082-ae79-79ba2740d506&&book=168&from=916&to=926&view=page
  178. public function showprogressdaily(Request $request)
  179. {
  180. $imgWidth = 300;
  181. $imgHeight = 100;
  182. $xAxisOffset = 16;
  183. $yAxisOffset = 25;
  184. $maxDay = 20;
  185. $maxPage = 20;
  186. $yLineSpace = 5;
  187. $yMin = 20; //y轴满刻度数值 最小
  188. #默认完成度显示字符数
  189. # strlen
  190. # page
  191. # percent
  192. $view = 'strlen';
  193. if ($request->has('view')) {
  194. $view = $request->get('view');
  195. }
  196. if ($request->has('type')) {
  197. $view = $request->get('type');
  198. }
  199. $pagePix = ($imgHeight - $xAxisOffset) / $maxPage;
  200. $dayPix = ($imgWidth - $yAxisOffset) / $maxDay;
  201. ob_clean();
  202. ob_start();
  203. $channel = $request->get('channel');
  204. $from = $request->get('from');
  205. if ($request->has('to')) {
  206. $to = $request->get('to');
  207. } else {
  208. $chapterLen = PaliText::where('book', $request->get('book'))->where('paragraph', $from)->value('chapter_len');
  209. $to = $from + $chapterLen - 1;
  210. $this->_endParagraph = $to;
  211. }
  212. $img = imagecreate($imgWidth, $imgHeight) or die('create image fail ');
  213. #颜色定义
  214. //background color
  215. imagecolorallocate($img, 255, 255, 255);
  216. $color = imagecolorallocate($img, 0, 0, 0);
  217. $gray = imagecolorallocate($img, 180, 180, 180);
  218. $dataLineColor = imagecolorallocate($img, 50, 50, 255);
  219. $max = 0;
  220. $values = [];
  221. #按天获取数据
  222. for ($i = 1; $i <= $maxDay; $i++) {
  223. $day = strtotime("today -{$i} day");
  224. $date = date("Y-m-d", $day);
  225. $current = $this->getSentProgress($request, $date);
  226. $values[] = $current;
  227. if ($max < $current) {
  228. $max = $current;
  229. }
  230. }
  231. /*
  232. * 计算Y 轴满刻度值
  233. * 算法 不足 20 按 20 算 小于100 满刻度是是50的整倍数
  234. * 小于1000 满刻度是是500的整倍数
  235. */
  236. if ($max < $yMin) {
  237. $yMax = $yMin;
  238. } else {
  239. $len = strlen($max);
  240. $yMax = pow(10, $len);
  241. if ($max < $yMax / 2) {
  242. $yMax = $yMax / 2;
  243. }
  244. }
  245. //根据满刻度像素数 计算缩放比例
  246. $yPix = $imgHeight - $xAxisOffset; //y轴实际像素数
  247. $rate = $yPix / $yMax;
  248. $svg = "<svg xmlns=\"http://www.w3.org/2000/svg\" fill=\"currentColor\" class=\"bi bi-alarm-fill\" viewBox=\"0 0 $imgWidth $imgHeight\">";
  249. //绘制坐标轴
  250. imageline($img, 0, $imgHeight - $xAxisOffset, $imgWidth, $imgHeight - $xAxisOffset, $color);
  251. imageline($img, $yAxisOffset, $imgHeight, $yAxisOffset, 0, $color);
  252. // x 轴
  253. $y = $imgHeight - $xAxisOffset + 1;
  254. $svg .= "<line x1='$yAxisOffset' y1='$y' x2='$imgWidth' y2='$y' style='stroke:#666666;'></line>";
  255. // y 轴
  256. $x = $yAxisOffset - 1;
  257. $svg .= "<line x1='$x' y1='0' x2='$x' y2='" . ($imgHeight - $xAxisOffset) . "' style='stroke:#666666;'></line>";
  258. //绘制x轴刻度线
  259. for ($i = 0; $i < $maxDay; $i++) {
  260. $space = ($imgWidth - $yAxisOffset) / $maxDay;
  261. $x = $imgWidth - $i * $space - $space / 2;
  262. $dayOffset = $maxDay - $i;
  263. $date = strtotime("today -{$i} day");
  264. $day = date("d", $date);
  265. imageline($img, $x, ($imgHeight - $xAxisOffset), $x, ($imgHeight - $xAxisOffset + 5), $gray);
  266. imagestring($img, 5, $x, ($imgHeight - $xAxisOffset - 2), $day, $color);
  267. $y = $imgHeight - $xAxisOffset + 1;
  268. $height = 5;
  269. $svg .= "<line x1='$x' y1='$y' x2='$x' y2='" . ($y + $height) . "' style='stroke:#666666;'></line>";
  270. $svg .= "<text x='" . ($x - 5) . "' y='" . ($y + 12) . "' style='font-size:8px;'>$day</text>";
  271. }
  272. //绘制y轴刻度线 将y轴五等分
  273. $step = $yMax / 5 * $rate;
  274. for ($i = 1; $i < 5; $i++) {
  275. # code...
  276. $yValue = $yMax / 5 * $i;
  277. if ($yValue >= 1000000) {
  278. $yValue = ($yValue / 1000000) . 'm';
  279. } else if ($yValue >= 1000) {
  280. $yValue = ($yValue / 1000) . 'k';
  281. }
  282. $x = $yAxisOffset;
  283. $y = $imgHeight - $yAxisOffset - $i * $step;
  284. $svg .= "<line x1='$x' y1='$y' x2='" . ($x - 5) . "' y2='$y' style='stroke:#666666;'></line>";
  285. $svg .= "<text x='" . ($x - 18) . "' y='" . ($y + 4) . "' style='font-size:8px;'>$yValue</text>";
  286. }
  287. for ($i = 1; $i < $maxPage / $yLineSpace; $i++) {
  288. $space = ($imgHeight - $xAxisOffset) / $maxPage * $yLineSpace;
  289. $y = $imgHeight - $yAxisOffset - $i * $space;
  290. imageline($img, $yAxisOffset, $y, $imgWidth, $y, $gray);
  291. imagestring($img, 5, 0, $y - 5, $i * $yLineSpace, $color);
  292. }
  293. // 绘制柱状图
  294. $rectWidth = $dayPix * 0.9;
  295. $last = 0;
  296. foreach ($values as $key => $value) {
  297. # code...
  298. $value = $value * $rate;
  299. $x = $imgWidth - ($dayPix * $key + $yAxisOffset);
  300. $y = $imgHeight - $xAxisOffset - $value;
  301. $svg .= "<rect x='$x' y='$y' height='{$value}' width='{$rectWidth}' style='stroke:#006600; fill: #006600'/>";
  302. if ($key > 0) {
  303. imageline($img, ($imgWidth - $key * $dayPix), $imgHeight - $xAxisOffset - $value, ($imgWidth - ($key - 1) * $dayPix), $imgHeight - $xAxisOffset - $last, $dataLineColor);
  304. }
  305. $last = $value;
  306. }
  307. $svg .= "</svg>";
  308. imagegif($img);
  309. imagedestroy($img);
  310. $content = ob_get_clean();
  311. return response($svg, 200, [
  312. 'Content-Type' => 'image/svg+xml'
  313. ]);
  314. }
  315. /**
  316. * Display the specified resource.
  317. * @param \Illuminate\Http\Request $request
  318. * @param string $sentenceId
  319. * @return \Illuminate\Http\Response
  320. */
  321. public function show(Request $request, string $sentenceId)
  322. {
  323. //
  324. $sentence = Sentence::where('book_id', $request->get('book'))
  325. ->where('paragraph', $request->get('par'))
  326. ->where('word_start', $request->get('start'))
  327. ->where('word_end', $request->get('end'))
  328. ->where('channel_uid', $request->get('channel'))
  329. ->firstOrFail();
  330. return $this->ok(new SentResource($sentence));
  331. }
  332. /**
  333. * Update the specified resource in storage.
  334. *
  335. * @param \Illuminate\Http\Request $request
  336. * @param \App\Models\Sentence $sentence
  337. * @return \Illuminate\Http\Response
  338. */
  339. public function update(Request $request, Sentence $sentence)
  340. {
  341. //
  342. }
  343. /**
  344. * Remove the specified resource from storage.
  345. *
  346. * @param \App\Models\Sentence $sentence
  347. * @return \Illuminate\Http\Response
  348. */
  349. public function destroy(Sentence $sentence)
  350. {
  351. //
  352. }
  353. }