2
0

SentenceInfoController.php 14 KB

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