VideoPoint.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\common\model\VideoPointUser as VPU;
  5. use app\common\model\Area;
  6. use think\db\Query;
  7. /**
  8. *
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class VideoPoint extends Backend
  13. {
  14. /**
  15. * VideoPoint模型对象
  16. * @var \app\admin\model\VideoPoint
  17. */
  18. protected $model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = new \app\admin\model\VideoPoint;
  23. }
  24. public function import()
  25. {
  26. parent::import();
  27. }
  28. /**
  29. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  30. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  31. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  32. */
  33. /**
  34. * 查看
  35. */
  36. public function index()
  37. {
  38. //当前是否为关联查询
  39. $this->relationSearch = true;
  40. //设置过滤方法
  41. $this->request->filter(['strip_tags', 'trim']);
  42. if ($this->request->isAjax()) {
  43. //如果发送的来源是Selectpage,则转发到Selectpage
  44. if ($this->request->request('keyField')) {
  45. return $this->selectpage();
  46. }
  47. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  48. $ids=input('ids');
  49. $list = $this->model
  50. ->with(['video'])
  51. ->where(function (Query $query)use ($ids){
  52. $ids && $query->where('video_id',$ids);
  53. })
  54. ->where($where)
  55. ->order($sort, $order)
  56. ->paginate($limit);
  57. foreach ($list as $row) {
  58. $row->visible(['id','type','second','title','items','item_type','key','key_desc','total_time','ave_time','title_img']);
  59. $row->visible(['video']);
  60. $row->getRelation('video')->visible(['title','src']);
  61. }
  62. $result = array("total" => $list->total(), "rows" => $list->items());
  63. return json($result);
  64. }
  65. return $this->view->fetch();
  66. }
  67. public function curve($ids){
  68. $point=$this->model->findOrFail($ids);
  69. if($point['type']=='wenda'){
  70. return '';
  71. }
  72. $this->assign('point',$point);
  73. $this->assign('x',array_keys($point['items']));
  74. $inputDate=input('time');
  75. if(!$inputDate){
  76. $inputDate=date('Y-m-01 00:00:00').' - '.date('Y-m-01 00:00:00',strtotime('+1month',strtotime(date('Y-m-01 00:00:00'))));
  77. }
  78. $this->assign('date',$inputDate);
  79. list($start,$end)=explode(' - ',$inputDate);
  80. $start=strtotime($start);
  81. $end=strtotime($end);
  82. $values=[];
  83. $total=$point->user()->day([$start,$end])->first()->sex(input('sex'))->filterUserArea(input('province_id'),input('city_id'))->filterUserAge(input('age_start'),input('age_end'))->count();
  84. foreach ($point['items'] as $key=>$value){
  85. if($total==0){
  86. $values[]=0;
  87. continue;
  88. }
  89. $count=$point->user()->day([$start,$end])->first()->sex(input('sex'))->filterUserArea(input('province_id'),input('city_id'))->filterUserAge(input('age_start'),input('age_end'))->whereRaw("FIND_IN_SET('{$key}',`key`)")->count();
  90. $values[]=bcmul(bcdiv($count,$total,2),100,2);
  91. }
  92. $this->assign('value',$values);
  93. $this->assign('province',Area::pro()->select());
  94. return view();
  95. }
  96. public function rate($ids){
  97. $point=$this->model->findOrFail($ids);
  98. if($point['type']=='wenda'){
  99. return '';
  100. }
  101. $this->assign('point',$point);
  102. $this->assign('x',['回答次数','正确率']);
  103. $inputDate=input('time');
  104. if(!$inputDate){
  105. $inputDate=date('Y-m-01 00:00:00').' - '.date('Y-m-01 00:00:00',strtotime('+1month',strtotime(date('Y-m-01 00:00:00'))));
  106. }
  107. $this->assign('date',$inputDate);
  108. list($start,$end)=explode(' - ',$inputDate);
  109. $start=strtotime($start);
  110. $end=strtotime($end);
  111. $values=[];
  112. $_total=$point->user()->day([$start,$end])->where('is_first',1)->column('is_right','id');
  113. $total=count($_total);
  114. $rightTotal=$_total?(array_count_values(array_values($_total))[1]??0):0;
  115. foreach ($point['items'] as $key=>$value){
  116. if($total==0){
  117. $values[]=0;
  118. continue;
  119. }
  120. $count=$point->user()->whereRaw("FIND_IN_SET('{$key}',`key`)")->count();
  121. $values[]=bcmul(bcdiv($count,$total,2),100,2);
  122. }
  123. $this->assign('value',[$total,$total?bcmul(bcdiv($rightTotal,$total,2),100,2):0]);
  124. return view();
  125. }
  126. public function time($ids){
  127. $point=$this->model->findOrFail($ids);
  128. $type=input('type','day');
  129. $this->assign('point',$point);
  130. $x=[];
  131. $values=[];
  132. $inputDate=input('time');
  133. if(!$inputDate){
  134. $inputDate=date('Y-m-01 00:00:00').' - '.date('Y-m-01 00:00:00',strtotime('+1month',strtotime(date('Y-m-01 00:00:00'))));
  135. }
  136. $this->assign('date',$inputDate);
  137. list($start,$end)=explode(' - ',$inputDate);
  138. $selectDay=strtotime($inputDate);
  139. $type='day';
  140. if($type=='day'){
  141. $temp=[];
  142. //$day=strtotime(date('Y-m-01',$start));
  143. //$month=strtotime('+1month',$day);
  144. $start=strtotime($start);
  145. $end=strtotime($end);
  146. for ($i=0;$i<($end-$start)/86400;$i++){
  147. $temp[]=$a=strtotime("+{$i}days",$start);
  148. $x[]=date('Y/m/d',$a);
  149. }
  150. $data=$point->user()->day([$start,$end])->first()->group('day')->column('avg(time)','day');
  151. foreach ($temp as $time){
  152. $values[]=bcadd($data[$time]??0,0);
  153. }
  154. }elseif ($type=='month'){
  155. $month=strtotime(date('Y-01-01',$selectDay));
  156. $temp=[];
  157. for ($i=0;$i<12;$i++){
  158. $temp[]=$a=strtotime("+{$i}month",$month);
  159. $x[]=date('Y/m',$a);
  160. }
  161. $data=$point->user()->where('month','in',$temp)->group('month')->column('avg(time)','month');
  162. foreach ($temp as $time){
  163. $values[]=$data[$time]??0;
  164. }
  165. }elseif ($type=='year'){
  166. $limit=7;
  167. $year=strtotime("-{$limit}year",strtotime(date('Y-01-01',$selectDay)));
  168. $temp=[];
  169. for ($i=0;$i<=$limit;$i++){
  170. $temp[]=$a=strtotime("+{$i}year",$year);
  171. $x[]=date('Y',$a);
  172. }
  173. $data=$point->user()->where('year','in',$temp)->group('year')->column('avg(time)','year');
  174. foreach ($temp as $time){
  175. $values[]=$data[$time]??0;
  176. }
  177. }
  178. $this->assign('x',$x);
  179. $this->assign('value',$values);
  180. return view();
  181. }
  182. public function map($ids){
  183. $point = $this->model->find($ids);
  184. if($this->request->isGet()) {
  185. $this->assign('point', $point);
  186. return $this->view->fetch();
  187. }else{
  188. $data=input('');
  189. $sex=$data['sex'];
  190. $ageFrom=$data['age_from'];
  191. $ageTo=$data['age_to'];
  192. $time=$data['time'];
  193. $query=$point
  194. ->user()
  195. ->first('a')
  196. ->alias('a')
  197. ->join('user b','a.user_id=b.id');
  198. if($time){
  199. list($timeStart,$timeEnd)=explode(' - ',$time);
  200. $query->where('a.created_at','>=',strtotime($timeStart))->where('a.created_at','<=',strtotime($timeEnd));
  201. }
  202. if($sex!==''){
  203. $query->where('b.gender',$sex);
  204. }
  205. if($ageFrom){
  206. $query->where('b.age','>=',$ageFrom);
  207. }
  208. if($ageTo){
  209. $query->where('b.age','<=',$ageTo);
  210. }
  211. if($data['level']==1){
  212. $query
  213. ->join('area c','b.province_id=c.id')
  214. ->group('c.id')
  215. ->field('c.shortname AS name,COUNT(a.id) AS value');
  216. $data['province']=json_decode($data['province'],true);
  217. $result=array_column($query->select()->toArray(),'value','name');
  218. foreach ($data['province'] as &$area){
  219. $area['value']=$result[$area['name']]??0;
  220. }
  221. $this->success('','',$data['province']);
  222. }elseif ($data['level']==2){
  223. $letter=$data['province_pinyin'];
  224. if(!$letter){
  225. $this->success();
  226. }
  227. $json_str=file_get_contents(ROOT_PATH."public/assets/js/map/json/{$letter}.json");
  228. $cities_temp=json_decode($json_str,true);
  229. $cities=[];
  230. foreach ($cities_temp['features'] as $feature){
  231. $cities[]=$feature['properties']['name'];
  232. }
  233. /*$province_id= Area::where('shortname',$data['province'])->value('id');
  234. if(!$province_id){
  235. $this->error('未找到省份');
  236. }*/
  237. //$cities=Area::where('pid',$province_id)->column('name','id');
  238. if(in_array($data['province'],['北京','上海','重庆','天津'])){
  239. $query
  240. ->group('c.id')
  241. ->join('area c','b.county_id=c.id')
  242. ->field('COUNT(b.county_id) as value,c.name')
  243. ->whereIn('c.name',$cities);
  244. $result=array_column($query->select()->toArray(),'value','name');
  245. $change=[];
  246. foreach ($cities as $city){
  247. $change[]=[
  248. 'name'=>$city,
  249. 'value'=>$result[$city]??0
  250. ];
  251. }
  252. }else{
  253. $query
  254. ->group('c.id')
  255. ->join('area c','b.city_id=c.id')
  256. ->field('COUNT(b.city_id) as value,c.name')
  257. ->whereIn('c.name',$cities);
  258. $result=array_column($query->select()->toArray(),'value','name');
  259. $change=[];
  260. foreach ($cities as $city){
  261. $change[]=[
  262. 'name'=>$city,
  263. 'value'=>$result[$city]??0
  264. ];
  265. }
  266. }
  267. $this->success('','',$change);
  268. }
  269. $this->success('','',[]);
  270. }
  271. }
  272. }