SentenceInfoController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. if($request->get('cache')=="1"){
  116. $key_sent_id = $sent->book_id.'-'.$sent->paragraph.'-'.$sent->word_start.'-'.$sent->word_end;
  117. $para_strlen += Cache::get(env('REDIS_NAMESPACE').'pali-sent/strlen/'.$key_sent_id, function() use($sent) {
  118. return PaliSentence::where('book',$sent->book_id)
  119. ->where('paragraph',$sent->paragraph)
  120. ->where('word_begin',$sent->word_start)
  121. ->where('word_end',$sent->word_end)
  122. ->value('length');
  123. });
  124. }else{
  125. $para_strlen += PaliSentence::where('book',$request->get('book'))
  126. ->where('paragraph',$sent->paragraph)
  127. ->where('word_begin',$sent->word_start)
  128. ->where('word_end',$sent->word_end)
  129. ->value('length');
  130. }
  131. }
  132. $percent = $para_strlen / $allStrLen;
  133. }
  134. switch ($view) {
  135. case 'page':
  136. # 输出已经完成的页数
  137. if(!empty($request->get('pages'))){
  138. #给了页码,用百分比计算
  139. $resulte = $percent * $request->get('pages');
  140. }else{
  141. #没给页码,用每页字符数计算
  142. $resulte = $strlen / $pageStrLen;
  143. }
  144. break;
  145. case 'percent': //百分比
  146. $resulte = sprintf('%.2f',$percent);
  147. break;
  148. case 'strlen':
  149. default:
  150. # code...
  151. $resulte = $strlen;
  152. break;
  153. }
  154. #保留小数点后两位
  155. $resulte = sprintf('%.2f',$resulte);
  156. return $resulte;
  157. }
  158. /**
  159. * 输出一张图片显示进度
  160. * Display the specified resource.
  161. *
  162. * @param \App\Models\Sentence $sentence
  163. * @return \Illuminate\Http\Response
  164. * http://127.0.0.1:8000/api/sentence/progress/image?channel=00ae2c48-c204-4082-ae79-79ba2740d506&&book=168&from=916&to=926&view=percent
  165. */
  166. public function showprogress(Request $request)
  167. {
  168. $resulte = $this->getSentProgress($request);
  169. $svg = "<svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' viewBox='0 0 100 25'>";
  170. switch ($request->get('view')) {
  171. case 'percent':
  172. # code...
  173. $resulte = $resulte*100;
  174. $svg .= "<rect id='frontground' x='0' y='0' width='100' height='25' fill='#cccccc' ></rect>";
  175. $svg .= "<text id='bg_text' x='5' y='21' fill='#006600' style='font-size:25px;'>$resulte%</text>";
  176. $svg .= "<rect id='background' x='0' y='0' width='100' height='25' fill='#006600' clip-path='url(#textClipPath)'></rect>";
  177. $svg .= "<text id='bg_text' x='5' y='21' fill='#ffffff' style='font-size:25px;' clip-path='url(#textClipPath)'>$resulte%</text>";
  178. $svg .= "<clipPath id='textClipPath'>";
  179. $svg .= " <rect x='0' y='0' width='$resulte' height='25'></rect>";
  180. $svg .= "</clipPath>";
  181. break;
  182. case 'strlen':
  183. case 'page':
  184. default:
  185. $svg .= "<text id='bg_text' x='5' y='21' fill='#006600' style='font-size:25px;'>$resulte</text>";
  186. break;
  187. }
  188. $svg .= "</svg>";
  189. return response($svg,200,[
  190. 'Content-Type'=>'image/svg+xml'
  191. ]);
  192. }
  193. //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
  194. public function showprogressdaily(Request $request)
  195. {
  196. $imgWidth = 300;
  197. $imgHeight = 100;
  198. $xAxisOffset = 16;
  199. $yAxisOffset = 25;
  200. $maxDay = 20;
  201. $maxPage = 20;
  202. $yLineSpace = 5;
  203. $yMin = 20; //y轴满刻度数值 最小
  204. #默认完成度显示字符数
  205. # strlen
  206. # page
  207. # percent
  208. $view = 'strlen';
  209. if($request->has('view')){
  210. $view =$request->get('view');
  211. }
  212. if($request->has('type')){
  213. $view =$request->get('type');
  214. }
  215. $pagePix = ($imgHeight-$xAxisOffset)/$maxPage;
  216. $dayPix = ($imgWidth-$yAxisOffset)/$maxDay;
  217. ob_clean();
  218. ob_start();
  219. $channel = $request->get('channel');
  220. $from = $request->get('from');
  221. if ($request->has('to')){
  222. $to = $request->get('to');
  223. }else{
  224. $chapterLen = PaliText::where('book',$request->get('book'))->where('paragraph',$from)->value('chapter_len');
  225. $to = $from + $chapterLen - 1;
  226. $this->_endParagraph = $to;
  227. }
  228. $img = imagecreate($imgWidth,$imgHeight) or die('create image fail ');
  229. #颜色定义
  230. //background color
  231. imagecolorallocate($img,255,255,255);
  232. $color = imagecolorallocate($img,0,0,0);
  233. $gray = imagecolorallocate($img,180,180,180);
  234. $dataLineColor = imagecolorallocate($img,50,50,255);
  235. $max=0;
  236. $values = [];
  237. #按天获取数据
  238. for($i = 1; $i <= $maxDay; $i++){
  239. $day = strtotime("today -{$i} day");
  240. $date = date("Y-m-d",$day);
  241. $current = $this->getSentProgress($request,$date);
  242. $values[] = $current;
  243. if($max < $current){
  244. $max = $current;
  245. }
  246. }
  247. /*
  248. * 计算Y 轴满刻度值
  249. * 算法 不足 20 按 20 算 小于100 满刻度是是50的整倍数
  250. * 小于1000 满刻度是是500的整倍数
  251. */
  252. if($max < $yMin){
  253. $yMax = $yMin;
  254. }else{
  255. $len = strlen($max);
  256. $yMax = pow(10,$len);
  257. if($max < $yMax/2){
  258. $yMax = $yMax / 2;
  259. }
  260. }
  261. //根据满刻度像素数 计算缩放比例
  262. $yPix = $imgHeight - $xAxisOffset;//y轴实际像素数
  263. $rate = $yPix / $yMax;
  264. $svg = "<svg xmlns=\"http://www.w3.org/2000/svg\" fill=\"currentColor\" class=\"bi bi-alarm-fill\" viewBox=\"0 0 $imgWidth $imgHeight\">";
  265. //绘制坐标轴
  266. imageline($img,0,$imgHeight-$xAxisOffset,$imgWidth,$imgHeight-$xAxisOffset,$color);
  267. imageline($img,$yAxisOffset,$imgHeight,$yAxisOffset,0,$color);
  268. // x 轴
  269. $y=$imgHeight-$xAxisOffset+1;
  270. $svg .= "<line x1='$yAxisOffset' y1='$y' x2='$imgWidth' y2='$y' style='stroke:#666666;'></line>";
  271. // y 轴
  272. $x = $yAxisOffset - 1;
  273. $svg .= "<line x1='$x' y1='0' x2='$x' y2='".($imgHeight-$xAxisOffset)."' style='stroke:#666666;'></line>";
  274. //绘制x轴刻度线
  275. for($i=0; $i<$maxDay; $i++){
  276. $space= ($imgWidth-$yAxisOffset)/$maxDay;
  277. $x=$imgWidth-$i*$space - $space/2;
  278. $dayOffset = $maxDay-$i;
  279. $date = strtotime("today -{$i} day");
  280. $day = date("d",$date);
  281. imageline($img,$x,($imgHeight-$xAxisOffset),$x,($imgHeight-$xAxisOffset+5),$gray);
  282. imagestring($img,5,$x,($imgHeight-$xAxisOffset-2),$day,$color);
  283. $y = $imgHeight-$xAxisOffset+1;
  284. $height = 5;
  285. $svg .= "<line x1='$x' y1='$y' x2='$x' y2='".($y+$height)."' style='stroke:#666666;'></line>";
  286. $svg .= "<text x='".($x-5)."' y='".($y+12)."' style='font-size:8px;'>$day</text>";
  287. }
  288. //绘制y轴刻度线 将y轴五等分
  289. $step = $yMax / 5 * $rate;
  290. for ($i=1; $i < 5; $i++) {
  291. # code...
  292. $yValue = $yMax / 5 * $i;
  293. if($yValue>=1000000){
  294. $yValue = ($yValue / 1000000 ).'m';
  295. }else if($yValue>=1000){
  296. $yValue = ($yValue / 1000 ).'k';
  297. }
  298. $x = $yAxisOffset;
  299. $y = $imgHeight-$yAxisOffset-$i*$step;
  300. $svg .= "<line x1='$x' y1='$y' x2='".($x - 5)."' y2='$y' style='stroke:#666666;'></line>";
  301. $svg .= "<text x='".($x-18)."' y='".($y+4)."' style='font-size:8px;'>$yValue</text>";
  302. }
  303. for($i=1;$i < $maxPage/$yLineSpace;$i++){
  304. $space= ($imgHeight-$xAxisOffset)/$maxPage*$yLineSpace;
  305. $y=$imgHeight-$yAxisOffset-$i*$space;
  306. imageline($img,$yAxisOffset,$y,$imgWidth,$y,$gray);
  307. imagestring($img,5,0,$y-5,$i*$yLineSpace,$color);
  308. }
  309. // 绘制柱状图
  310. $rectWidth = $dayPix*0.9;
  311. $last = 0;
  312. foreach ($values as $key => $value) {
  313. # code...
  314. $value = $value*$rate;
  315. $x = $imgWidth - ($dayPix * $key + $yAxisOffset);
  316. $y = $imgHeight - $xAxisOffset - $value;
  317. $svg .= "<rect x='$x' y='$y' height='{$value}' width='{$rectWidth}' style='stroke:#006600; fill: #006600'/>";
  318. if($key>0){
  319. imageline($img,($imgWidth - $key * $dayPix),$imgHeight - $xAxisOffset - $value,($imgWidth-($key - 1)*$dayPix),$imgHeight-$xAxisOffset-$last,$dataLineColor);
  320. }
  321. $last = $value;
  322. }
  323. $svg .= "</svg>";
  324. imagegif($img);
  325. imagedestroy($img);
  326. $content = ob_get_clean();
  327. return response($svg,200,[
  328. 'Content-Type'=>'image/svg+xml'
  329. ]);
  330. }
  331. /**
  332. * Display the specified resource.
  333. *
  334. * @param \App\Models\Sentence $sentence
  335. * @return \Illuminate\Http\Response
  336. */
  337. public function show(Sentence $sentence)
  338. {
  339. //
  340. }
  341. /**
  342. * Show the form for editing the specified resource.
  343. *
  344. * @param \App\Models\Sentence $sentence
  345. * @return \Illuminate\Http\Response
  346. */
  347. public function edit(Sentence $sentence)
  348. {
  349. //
  350. }
  351. /**
  352. * Update the specified resource in storage.
  353. *
  354. * @param \Illuminate\Http\Request $request
  355. * @param \App\Models\Sentence $sentence
  356. * @return \Illuminate\Http\Response
  357. */
  358. public function update(Request $request, Sentence $sentence)
  359. {
  360. //
  361. }
  362. /**
  363. * Remove the specified resource from storage.
  364. *
  365. * @param \App\Models\Sentence $sentence
  366. * @return \Illuminate\Http\Response
  367. */
  368. public function destroy(Sentence $sentence)
  369. {
  370. //
  371. }
  372. }