SentenceInfoController.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 = $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=page
  148. */
  149. public function showprogress(Request $request)
  150. {
  151. ob_clean();
  152. ob_start();
  153. $resulte = $this->getSentProgress($request);
  154. $img = imagecreate(strlen($resulte)*10,22) or die('create image fail ');
  155. imagecolorallocate($img,255,255,255);
  156. $color = imagecolorallocate($img,0,0,0);
  157. imagestring($img,5,0,0,$resulte,$color);
  158. imagegif($img);
  159. imagedestroy($img);
  160. $content = ob_get_clean();
  161. return response($content,200,[
  162. 'Content-Type'=>'image/gif'
  163. ]);
  164. }
  165. //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
  166. public function showprogressdaily(Request $request)
  167. {
  168. $imgWidth = 300;
  169. $imgHeight = 100;
  170. $xAxisOffset = 16;
  171. $yAxisOffset = 16;
  172. $maxDay = 10;
  173. $maxPage = 20;
  174. $yLineSpace = 5;
  175. #默认完成度显示字符数
  176. # strlen
  177. # page
  178. # percent
  179. $view = 'strlen';
  180. if(!empty($request->get('view'))){
  181. $view =$request->get('view');
  182. }
  183. if(!empty($request->get('type'))){
  184. $view =$request->get('type');
  185. }
  186. $pagePix = ($imgHeight-$xAxisOffset)/$maxPage;
  187. $dayPix = ($imgWidth-$yAxisOffset)/$maxDay;
  188. ob_clean();
  189. ob_start();
  190. $channel = $request->get('channel');
  191. $from = $request->get('from');
  192. $to = $request->get('to');
  193. $img = imagecreate($imgWidth,$imgHeight) or die('create image fail ');
  194. #颜色定义
  195. //background color
  196. imagecolorallocate($img,255,255,255);
  197. $color = imagecolorallocate($img,0,0,0);
  198. $gray = imagecolorallocate($img,180,180,180);
  199. $dataLineColor = imagecolorallocate($img,50,50,255);
  200. //绘制坐标轴
  201. imageline($img,0,$imgHeight-$xAxisOffset,$imgWidth,$imgHeight-$xAxisOffset,$color);
  202. imageline($img,$yAxisOffset,$imgHeight,$yAxisOffset,0,$color);
  203. //绘制y轴网格线
  204. for($i=1;$i<$maxPage/$yLineSpace;$i++){
  205. $space= ($imgHeight-$xAxisOffset)/$maxPage*$yLineSpace;
  206. $y=$imgHeight-$yAxisOffset-$i*$space;
  207. imageline($img,$yAxisOffset,$y,$imgWidth,$y,$gray);
  208. imagestring($img,5,0,$y-5,$i*$yLineSpace,$color);
  209. }
  210. //绘制x轴网格线
  211. for($i=0; $i<$maxDay; $i++){
  212. $space= ($imgWidth-$yAxisOffset)/$maxDay;
  213. $x=$imgWidth-$yAxisOffset-$i*$space;
  214. $dayOffset = $maxDay-$i;
  215. $date = strtotime("today -{$i} day");
  216. $day = date("d",$date);
  217. imageline($img,$x,($imgHeight-$xAxisOffset),$x,($imgHeight-$xAxisOffset+5),$gray);
  218. imagestring($img,5,$x,($imgHeight-$xAxisOffset),$day,$color);
  219. }
  220. $last=0;
  221. for($i = 1; $i <= $maxDay; $i++){
  222. $day = strtotime("today -{$i} day");
  223. $date = date("Y-m-d",$day);
  224. $resulte = $this->getSentProgress($request,$date)*$pagePix;
  225. if($i>0){
  226. imageline($img,($imgWidth-$i*$dayPix),$imgHeight-$xAxisOffset-$resulte,($imgWidth-($i-1)*$dayPix),$imgHeight-$xAxisOffset-$last,$dataLineColor);
  227. }
  228. $last = $resulte;
  229. }
  230. imagegif($img);
  231. imagedestroy($img);
  232. $content = ob_get_clean();
  233. return response($content,200,[
  234. 'Content-Type'=>'image/gif'
  235. ]);
  236. }
  237. /**
  238. * Display the specified resource.
  239. *
  240. * @param \App\Models\Sentence $sentence
  241. * @return \Illuminate\Http\Response
  242. */
  243. public function show(Sentence $sentence)
  244. {
  245. //
  246. }
  247. /**
  248. * Show the form for editing the specified resource.
  249. *
  250. * @param \App\Models\Sentence $sentence
  251. * @return \Illuminate\Http\Response
  252. */
  253. public function edit(Sentence $sentence)
  254. {
  255. //
  256. }
  257. /**
  258. * Update the specified resource in storage.
  259. *
  260. * @param \Illuminate\Http\Request $request
  261. * @param \App\Models\Sentence $sentence
  262. * @return \Illuminate\Http\Response
  263. */
  264. public function update(Request $request, Sentence $sentence)
  265. {
  266. //
  267. }
  268. /**
  269. * Remove the specified resource from storage.
  270. *
  271. * @param \App\Models\Sentence $sentence
  272. * @return \Illuminate\Http\Response
  273. */
  274. public function destroy(Sentence $sentence)
  275. {
  276. //
  277. }
  278. }