SentenceInfoController.php 14 KB

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