2
0

SentenceInfoController.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. /*
  99. $para_strlen += PaliSentence::where('book',$request->get('book'))
  100. ->where('paragraph',$sent->paragraph)
  101. ->where('word_begin',$sent->word_start)
  102. ->where('word_end',$sent->word_end)
  103. ->value('length');
  104. */
  105. $key_sent_id = $sent->book_id.'-'.$sent->paragraph.'-'.$sent->word_start.'-'.$sent->word_end;
  106. $para_strlen += Cache::get(env('REDIS_NAMESPACE').'pali-sent/strlen/'.$key_sent_id, function() use($sent) {
  107. return PaliSentence::where('book',$sent->book_id)
  108. ->where('paragraph',$sent->paragraph)
  109. ->where('word_begin',$sent->word_start)
  110. ->where('word_end',$sent->word_end)
  111. ->value('length');
  112. });
  113. }
  114. $percent = $para_strlen / $allStrLen;
  115. }
  116. switch ($view) {
  117. case 'page':
  118. # 输出已经完成的页数
  119. if(!empty($request->get('pages'))){
  120. #给了页码,用百分比计算
  121. $resulte = $percent * $request->get('pages');
  122. }else{
  123. #没给页码,用每页字符数计算
  124. $resulte = $strlen / $pageStrLen;
  125. }
  126. break;
  127. case 'percent':
  128. $resulte = $percent;
  129. break;
  130. case 'strlen':
  131. default:
  132. # code...
  133. $resulte = $strlen;
  134. break;
  135. }
  136. #保留小数点后两位
  137. $resulte = sprintf('%.2f',$resulte);
  138. return $resulte;
  139. }
  140. /**
  141. * 输出一张图片显示进度
  142. * Display the specified resource.
  143. *
  144. * @param \App\Models\Sentence $sentence
  145. * @return \Illuminate\Http\Response
  146. * http://127.0.0.1:8000/api/sentence/progress/image?channel=00ae2c48-c204-4082-ae79-79ba2740d506&&book=168&from=916&to=926&view=page
  147. */
  148. public function showprogress(Request $request)
  149. {
  150. ob_clean();
  151. ob_start();
  152. $resulte = $this->getSentProgress($request);
  153. $img = imagecreate(strlen($resulte)*10,22) or die('create image fail ');
  154. imagecolorallocate($img,255,255,255);
  155. $color = imagecolorallocate($img,0,0,0);
  156. imagestring($img,5,0,0,$resulte,$color);
  157. imagegif($img);
  158. imagedestroy($img);
  159. $content = ob_get_clean();
  160. return response($content,200,[
  161. 'Content-Type'=>'image/gif'
  162. ]);
  163. }
  164. //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
  165. public function showprogressdaily(Request $request)
  166. {
  167. $imgWidth = 300;
  168. $imgHeight = 100;
  169. $xAxisOffset = 16;
  170. $yAxisOffset = 16;
  171. $maxDay = 10;
  172. $maxPage = 20;
  173. $yLineSpace = 5;
  174. #默认完成度显示字符数
  175. # strlen
  176. # page
  177. # percent
  178. $view = 'strlen';
  179. if(!empty($request->get('view'))){
  180. $view =$request->get('view');
  181. }
  182. if(!empty($request->get('type'))){
  183. $view =$request->get('type');
  184. }
  185. $pagePix = ($imgHeight-$xAxisOffset)/$maxPage;
  186. $dayPix = ($imgWidth-$yAxisOffset)/$maxDay;
  187. ob_clean();
  188. ob_start();
  189. $channel = $request->get('channel');
  190. $from = $request->get('from');
  191. $to = $request->get('to');
  192. $img = imagecreate($imgWidth,$imgHeight) or die('create image fail ');
  193. #颜色定义
  194. //background color
  195. imagecolorallocate($img,255,255,255);
  196. $color = imagecolorallocate($img,0,0,0);
  197. $gray = imagecolorallocate($img,180,180,180);
  198. $dataLineColor = imagecolorallocate($img,50,50,255);
  199. //绘制坐标轴
  200. imageline($img,0,$imgHeight-$xAxisOffset,$imgWidth,$imgHeight-$xAxisOffset,$color);
  201. imageline($img,$yAxisOffset,$imgHeight,$yAxisOffset,0,$color);
  202. //绘制y轴网格线
  203. for($i=1;$i<$maxPage/$yLineSpace;$i++){
  204. $space= ($imgHeight-$xAxisOffset)/$maxPage*$yLineSpace;
  205. $y=$imgHeight-$yAxisOffset-$i*$space;
  206. imageline($img,$yAxisOffset,$y,$imgWidth,$y,$gray);
  207. imagestring($img,5,0,$y-5,$i*$yLineSpace,$color);
  208. }
  209. //绘制x轴网格线
  210. for($i=0; $i<$maxDay; $i++){
  211. $space= ($imgWidth-$yAxisOffset)/$maxDay;
  212. $x=$imgWidth-$yAxisOffset-$i*$space;
  213. $dayOffset = $maxDay-$i;
  214. $date = strtotime("today -{$i} day");
  215. $day = date("d",$date);
  216. imageline($img,$x,($imgHeight-$xAxisOffset),$x,($imgHeight-$xAxisOffset+5),$gray);
  217. imagestring($img,5,$x,($imgHeight-$xAxisOffset),$day,$color);
  218. }
  219. $last=0;
  220. for($i = 1; $i <= $maxDay; $i++){
  221. $day = strtotime("today -{$i} day");
  222. $date = date("Y-m-d",$day);
  223. $resulte = $this->getSentProgress($request,$date)*$pagePix;
  224. if($i>0){
  225. imageline($img,($imgWidth-$i*$dayPix),$imgHeight-$xAxisOffset-$resulte,($imgWidth-($i-1)*$dayPix),$imgHeight-$xAxisOffset-$last,$dataLineColor);
  226. }
  227. $last = $resulte;
  228. }
  229. imagegif($img);
  230. imagedestroy($img);
  231. $content = ob_get_clean();
  232. return response($content,200,[
  233. 'Content-Type'=>'image/gif'
  234. ]);
  235. }
  236. /**
  237. * Display the specified resource.
  238. *
  239. * @param \App\Models\Sentence $sentence
  240. * @return \Illuminate\Http\Response
  241. */
  242. public function show(Sentence $sentence)
  243. {
  244. //
  245. }
  246. /**
  247. * Show the form for editing the specified resource.
  248. *
  249. * @param \App\Models\Sentence $sentence
  250. * @return \Illuminate\Http\Response
  251. */
  252. public function edit(Sentence $sentence)
  253. {
  254. //
  255. }
  256. /**
  257. * Update the specified resource in storage.
  258. *
  259. * @param \Illuminate\Http\Request $request
  260. * @param \App\Models\Sentence $sentence
  261. * @return \Illuminate\Http\Response
  262. */
  263. public function update(Request $request, Sentence $sentence)
  264. {
  265. //
  266. }
  267. /**
  268. * Remove the specified resource from storage.
  269. *
  270. * @param \App\Models\Sentence $sentence
  271. * @return \Illuminate\Http\Response
  272. */
  273. public function destroy(Sentence $sentence)
  274. {
  275. //
  276. }
  277. }