SentenceInfoController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Sentence;
  4. use App\Models\PaliSentence;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\Cache;
  7. use Illuminate\Support\Facades\DB;
  8. class SentenceInfoController extends Controller
  9. {
  10. /**
  11. * Display a listing of the resource.
  12. *
  13. * @return \Illuminate\Http\Response
  14. */
  15. public function index()
  16. {
  17. //
  18. }
  19. /**
  20. * Show the form for creating a new resource.
  21. *
  22. * @return \Illuminate\Http\Response
  23. */
  24. public function create()
  25. {
  26. //
  27. }
  28. /**
  29. * Store a newly created resource in storage.
  30. *
  31. * @param \Illuminate\Http\Request $request
  32. * @return \Illuminate\Http\Response
  33. */
  34. public function store(Request $request)
  35. {
  36. //
  37. }
  38. private function getSentProgress(Request $request,$date=''){
  39. $channel = $request->get('channel');
  40. $from = $request->get('from');
  41. $to = $request->get('to');
  42. #默认完成度显示字符数
  43. # strlen
  44. # page
  45. # percent
  46. $view = 'strlen';
  47. if(!empty($request->get('view'))){
  48. $view =$request->get('view');
  49. }
  50. if(!empty($request->get('type'))){
  51. $view =$request->get('type');
  52. }
  53. #一页书中的字符数
  54. $pageStrLen = 2000;
  55. if(!empty($request->get('strlen'))){
  56. $pageStrLen =$request->get('strlen');
  57. }
  58. if(!empty($request->get('pagelen'))){
  59. $pageStrLen =$request->get('pagelen');
  60. }
  61. # 页数
  62. $pageNumber = 300;
  63. if(!empty($request->get('pages'))){
  64. $pageNumber =$request->get('pages');
  65. }
  66. $db = Sentence::where('channel_uid',$request->get('channel'))
  67. ->where('book_id','>=',$request->get('book'))
  68. ->where('paragraph','>=',$request->get('from'))
  69. ->where('paragraph','<=',$request->get('to'));
  70. if(!empty($date)){
  71. $db = $db->whereDate('created_at','=',$date);
  72. }
  73. $strlen =$db->sum('strlen');
  74. if(is_null($strlen) || $strlen===0){
  75. return 0;
  76. }
  77. #计算已完成百分比
  78. $percent = 0;
  79. if(($view==='page' && !empty($request->get('pages'))) || $view==='percent' ){
  80. #计算完成的句子在巴利语句子表中的字符串长度百分比
  81. $db = Sentence::select(['book_id','paragraph','word_start','word_end'])
  82. ->where('channel_uid',$request->get('channel'))
  83. ->where('book_id','>=',$request->get('book'))
  84. ->where('paragraph','>=',$request->get('from'))
  85. ->where('paragraph','<=',$request->get('to'));
  86. if(!empty($date)){
  87. $db = $db->whereDate('created_at','=',$date);
  88. }
  89. $sentFinished = $db->get();
  90. #查询这些句子的总共等效巴利语字符数
  91. $allStrLen = PaliSentence::where('book',$request->get('book'))
  92. ->where('paragraph','>=',$request->get('from'))
  93. ->where('paragraph','<=',$request->get('to'))
  94. ->sum('length');
  95. $para_strlen = 0;
  96. foreach ($sentFinished as $sent) {
  97. # code...
  98. if($request->get('cache')=="1"){
  99. $key_sent_id = $sent->book_id.'-'.$sent->paragraph.'-'.$sent->word_start.'-'.$sent->word_end;
  100. $para_strlen += Cache::get(env('REDIS_NAMESPACE').'pali-sent/strlen/'.$key_sent_id, function() use($sent) {
  101. return PaliSentence::where('book',$sent->book_id)
  102. ->where('paragraph',$sent->paragraph)
  103. ->where('word_begin',$sent->word_start)
  104. ->where('word_end',$sent->word_end)
  105. ->value('length');
  106. });
  107. }else{
  108. $para_strlen += PaliSentence::where('book',$request->get('book'))
  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. $percent = $para_strlen / $allStrLen;
  116. }
  117. switch ($view) {
  118. case 'page':
  119. # 输出已经完成的页数
  120. if(!empty($request->get('pages'))){
  121. #给了页码,用百分比计算
  122. $resulte = $percent * $request->get('pages');
  123. }else{
  124. #没给页码,用每页字符数计算
  125. $resulte = $strlen / $pageStrLen;
  126. }
  127. break;
  128. case 'percent': //百分比
  129. $resulte = sprintf('%.2f',$percent);
  130. break;
  131. case 'strlen':
  132. default:
  133. # code...
  134. $resulte = $strlen;
  135. break;
  136. }
  137. #保留小数点后两位
  138. $resulte = sprintf('%.2f',$resulte);
  139. return $resulte;
  140. }
  141. /**
  142. * 输出一张图片显示进度
  143. * Display the specified resource.
  144. *
  145. * @param \App\Models\Sentence $sentence
  146. * @return \Illuminate\Http\Response
  147. * http://127.0.0.1:8000/api/sentence/progress/image?channel=00ae2c48-c204-4082-ae79-79ba2740d506&&book=168&from=916&to=926&view=percent
  148. */
  149. public function showprogress(Request $request)
  150. {
  151. $resulte = $this->getSentProgress($request);
  152. $svg = "<svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' viewBox='0 0 100 25'>";
  153. switch ($request->get('view')) {
  154. case 'percent':
  155. # code...
  156. $resulte = $resulte*100;
  157. $svg .= "<rect id='frontground' x='0' y='0' width='100' height='25' fill='#cccccc' ></rect>";
  158. $svg .= "<text id='bg_text' x='5' y='21' fill='#006600' style='font-size:25px;'>$resulte%</text>";
  159. $svg .= "<rect id='background' x='0' y='0' width='100' height='25' fill='#006600' clip-path='url(#textClipPath)'></rect>";
  160. $svg .= "<text id='bg_text' x='5' y='21' fill='#ffffff' style='font-size:25px;' clip-path='url(#textClipPath)'>$resulte%</text>";
  161. $svg .= "<clipPath id='textClipPath'>";
  162. $svg .= " <rect x='0' y='0' width='$resulte' height='25'></rect>";
  163. $svg .= "</clipPath>";
  164. break;
  165. case 'strlen':
  166. case 'page':
  167. default:
  168. $svg .= "<text id='bg_text' x='5' y='21' fill='#006600' style='font-size:25px;'>$resulte</text>";
  169. break;
  170. }
  171. $svg .= "</svg>";
  172. return response($svg,200,[
  173. 'Content-Type'=>'image/svg+xml'
  174. ]);
  175. }
  176. //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
  177. public function showprogressdaily(Request $request)
  178. {
  179. $imgWidth = 300;
  180. $imgHeight = 100;
  181. $xAxisOffset = 16;
  182. $yAxisOffset = 16;
  183. $maxDay = 10;
  184. $maxPage = 20;
  185. $yLineSpace = 5;
  186. #默认完成度显示字符数
  187. # strlen
  188. # page
  189. # percent
  190. $view = 'strlen';
  191. if(!empty($request->get('view'))){
  192. $view =$request->get('view');
  193. }
  194. if(!empty($request->get('type'))){
  195. $view =$request->get('type');
  196. }
  197. $pagePix = ($imgHeight-$xAxisOffset)/$maxPage;
  198. $dayPix = ($imgWidth-$yAxisOffset)/$maxDay;
  199. ob_clean();
  200. ob_start();
  201. $channel = $request->get('channel');
  202. $from = $request->get('from');
  203. $to = $request->get('to');
  204. $img = imagecreate($imgWidth,$imgHeight) or die('create image fail ');
  205. #颜色定义
  206. //background color
  207. imagecolorallocate($img,255,255,255);
  208. $color = imagecolorallocate($img,0,0,0);
  209. $gray = imagecolorallocate($img,180,180,180);
  210. $dataLineColor = imagecolorallocate($img,50,50,255);
  211. //绘制坐标轴
  212. imageline($img,0,$imgHeight-$xAxisOffset,$imgWidth,$imgHeight-$xAxisOffset,$color);
  213. imageline($img,$yAxisOffset,$imgHeight,$yAxisOffset,0,$color);
  214. //绘制y轴网格线
  215. for($i=1;$i<$maxPage/$yLineSpace;$i++){
  216. $space= ($imgHeight-$xAxisOffset)/$maxPage*$yLineSpace;
  217. $y=$imgHeight-$yAxisOffset-$i*$space;
  218. imageline($img,$yAxisOffset,$y,$imgWidth,$y,$gray);
  219. imagestring($img,5,0,$y-5,$i*$yLineSpace,$color);
  220. }
  221. //绘制x轴网格线
  222. for($i=0; $i<$maxDay; $i++){
  223. $space= ($imgWidth-$yAxisOffset)/$maxDay;
  224. $x=$imgWidth-$yAxisOffset-$i*$space;
  225. $dayOffset = $maxDay-$i;
  226. $date = strtotime("today -{$i} day");
  227. $day = date("d",$date);
  228. imageline($img,$x,($imgHeight-$xAxisOffset),$x,($imgHeight-$xAxisOffset+5),$gray);
  229. imagestring($img,5,$x,($imgHeight-$xAxisOffset),$day,$color);
  230. }
  231. $last=0;
  232. for($i = 1; $i <= $maxDay; $i++){
  233. $day = strtotime("today -{$i} day");
  234. $date = date("Y-m-d",$day);
  235. $resulte = $this->getSentProgress($request,$date)*$pagePix;
  236. if($i>0){
  237. imageline($img,($imgWidth-$i*$dayPix),$imgHeight-$xAxisOffset-$resulte,($imgWidth-($i-1)*$dayPix),$imgHeight-$xAxisOffset-$last,$dataLineColor);
  238. }
  239. $last = $resulte;
  240. }
  241. imagegif($img);
  242. imagedestroy($img);
  243. $content = ob_get_clean();
  244. return response($content,200,[
  245. 'Content-Type'=>'image/gif'
  246. ]);
  247. }
  248. /**
  249. * Display the specified resource.
  250. *
  251. * @param \App\Models\Sentence $sentence
  252. * @return \Illuminate\Http\Response
  253. */
  254. public function show(Sentence $sentence)
  255. {
  256. //
  257. }
  258. /**
  259. * Show the form for editing the specified resource.
  260. *
  261. * @param \App\Models\Sentence $sentence
  262. * @return \Illuminate\Http\Response
  263. */
  264. public function edit(Sentence $sentence)
  265. {
  266. //
  267. }
  268. /**
  269. * Update the specified resource in storage.
  270. *
  271. * @param \Illuminate\Http\Request $request
  272. * @param \App\Models\Sentence $sentence
  273. * @return \Illuminate\Http\Response
  274. */
  275. public function update(Request $request, Sentence $sentence)
  276. {
  277. //
  278. }
  279. /**
  280. * Remove the specified resource from storage.
  281. *
  282. * @param \App\Models\Sentence $sentence
  283. * @return \Illuminate\Http\Response
  284. */
  285. public function destroy(Sentence $sentence)
  286. {
  287. //
  288. }
  289. }