RecordDocument.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. namespace app\admin\controller;
  3. use think\App;
  4. use think\Db;
  5. class RecordDocument extends Auth
  6. {
  7. public function index(){
  8. if(request()->isAjax()) {
  9. //分页参数
  10. $length = input('rows', 10, 'intval'); //每页条数
  11. $page = input('page', 1, 'intval'); //第几页
  12. $start = ($page - 1) * $length; //分页开始位置
  13. $map1 = [];
  14. $map = [];
  15. $b = input('begin','','trim');
  16. $e = input('end','','trim');
  17. $title = input('title','','trim');//护工
  18. if($title) {
  19. $map1[] = ['title', 'like', '%' . $title . '%'];
  20. }
  21. if($b && $e){
  22. if($b <= $e){
  23. $b = date('Ymd',strtotime($b));
  24. $e = date('Ymd',strtotime($e));
  25. $map[] = ['create_yyyymmdd','>=',$b];
  26. $map[] = ['create_yyyymmdd','<=',$e];
  27. }
  28. }
  29. if($b && $e==''){
  30. $b = date('Ymd',strtotime($b));
  31. $map[] = ['create_yyyymmdd','>=',$b];
  32. }
  33. if($b=='' && $e){
  34. $e = date('Ymd',strtotime($e));
  35. $map[] = ['create_yyyymmdd','<=',$e];
  36. }
  37. $cateId = input('cate_id','','trim');
  38. if($cateId){
  39. $map1[] = ['cate_id','=',$cateId];
  40. }
  41. $map1[] = ['org_id','=',$this->orgId];
  42. $map[] = ['org_id','=',$this->orgId];
  43. $ret = $this->get_document_data($map,$map1,$start,$length);
  44. $totalCount = Db::name('article')
  45. ->where('del',0)
  46. ->where('enable',1)
  47. ->where($map1)
  48. ->count();
  49. $totalPage = ceil($totalCount/$length);
  50. $result['page'] = $page;
  51. $result['total'] = $totalPage;
  52. $result['records'] = $totalCount;
  53. $result['rows'] = $ret;
  54. return json($result);
  55. }else{
  56. $cate = (new \app\common\model\ArticleCate())->getList();
  57. $this->assign('cate',$cate);
  58. return $this->fetch();
  59. }
  60. }
  61. public function get_document_data($map,$map1,$start,$length){
  62. $ret = Db::name('article')
  63. ->where('del',0)
  64. ->where('enable',1)
  65. ->where($map1)
  66. ->limit($start,$length)
  67. ->select();
  68. $count = $this->get_workers_count(array(),$this->orgId);
  69. $read_user_num = 0;
  70. foreach ($ret as $k=>$v){
  71. $ret[$k]['cate_name'] = db('article_cate')
  72. ->where('id',$v['cate_id'])
  73. ->value('title');
  74. $query = Db::name('record')
  75. // ->where('org_id', $this->orgId)
  76. ->where($map)
  77. ->where('article_id',$v['id']);
  78. $db = clone $query;
  79. $read_total = $db
  80. ->field('SUM(view) as views')
  81. ->find();
  82. $ret[$k]['read_total'] = $read_total['views']?$read_total['views']:0;
  83. $db1 = clone $query;
  84. $ret[$k]['read_user_num'] = $db1
  85. ->group('user_id')
  86. // ->distinct(true)
  87. ->count();
  88. $read_user_num += $ret[$k]['read_user_num'];
  89. $db2= clone $query;
  90. $time = $db2
  91. ->field('SUM(time) as times')
  92. ->find();
  93. $ret[$k]['read_time'] = $time['times']?$time['times']:0;
  94. if ($v['limit'] > 0) {
  95. $roles = empty($v['roles_ids'])?array():explode(',',$v['roles_ids']);
  96. $roles_ids = $this->get_worker_roles($roles,$this->orgId);
  97. if(!$roles_ids){
  98. $ret[$k]['unread_users'] = 0;
  99. }else{
  100. $db3 = clone $query;
  101. $db3->where('roles_id', 'in',$roles_ids);
  102. $finishuser = $db3
  103. ->field('SUM(time) as times,user_id')
  104. ->group('user_id')
  105. ->having('times >= '.$v['limit'])
  106. ->select();
  107. if($v['id'] == 3){
  108. // $finishuser = $db3
  109. // ->field('SUM(time) as times,user_id')
  110. // ->group('user_id')
  111. // ->having('times > '.$v['limit'])
  112. // ->fetchSql()
  113. // ->select();
  114. // var_dump($finishuser);
  115. }
  116. if($roles){
  117. $ucount = $this->get_workers_count($roles,$this->orgId);
  118. $ret[$k]['unread_users'] = $ucount - count($finishuser);
  119. }else{
  120. $ret[$k]['unread_users'] = $count - count($finishuser);
  121. }
  122. }
  123. } else {
  124. $ret[$k]['unread_users'] = 0;
  125. }
  126. }
  127. return $ret;
  128. }
  129. public function get_worker_roles($roles,$org){
  130. $roles_ids = array();
  131. if (!$roles) {
  132. $roles_ids = Db::name('roles')
  133. ->where('enable',1)
  134. ->where('del',0)
  135. ->where('org_id',$org)
  136. ->where('parent_id','>',0)
  137. ->column('id');
  138. }else{
  139. $roles_ids = $roles;
  140. }
  141. return $roles_ids;
  142. }
  143. public function get_workers_count($roles,$org){
  144. if (!$roles) {
  145. $roles_ids = Db::name('roles')
  146. ->where('enable',1)
  147. ->where('del',0)
  148. ->where('del',0)
  149. ->where('org_id',$org)
  150. ->where('parent_id','>',0)
  151. ->column('id');
  152. }else{
  153. $roles_ids = Db::name('roles')
  154. ->where('enable',1)
  155. ->where('del',0)
  156. ->where('org_id',$org)
  157. ->where('parent_id','in',$roles)
  158. ->column('id');
  159. }
  160. if($roles_ids){
  161. $query = Db::name('user')
  162. ->alias('a')
  163. ->where('b.roles_id','in',$roles_ids);
  164. }else{
  165. $query = Db::name('user')
  166. ->alias('a')
  167. ->where('b.roles_id','=',-1);
  168. }
  169. $query->join('user_roles b','a.id = b.user_id');
  170. $query->join('user_org c','a.id = c.user_id');
  171. $query->where('c.org_id',$org);
  172. $query->where('a.type',0);
  173. $query->where('a.del',0);
  174. $count = $query->count('a.id');
  175. return $count;
  176. }
  177. public function recordLog($id){
  178. if(request()->isAjax()){
  179. //分页参数
  180. $length = input('rows',10,'intval'); //每页条数
  181. $page = input('page',1,'intval'); //第几页
  182. $start = ($page - 1) * $length; //分页开始位置
  183. $res = $this->get_document_record($this->orgId,$id,$start,$length);
  184. $totalPage = $res['total'];
  185. $result['page'] = $page;
  186. $result['total'] = $totalPage;
  187. $result['records'] = $res['count'];
  188. $result['rows'] = $res['data'];
  189. return json($result);
  190. }else{
  191. $article = Db::name('article')
  192. ->where('id',$id)
  193. ->find();
  194. if($article['limit'] > 0){
  195. $m_name = '文档名称:'.$article['title'].'| 最低时长限制:'.$article['limit'].'分钟';
  196. }else{
  197. $m_name = '文档名称:'.$article['title'].'| 最低时长限制:不限制';
  198. }
  199. $this->assign('m_name',$m_name);
  200. $this->assign('id',$id);
  201. return $this->fetch();
  202. }
  203. }
  204. public function get_document_record($org,$id,$start,$length){
  205. $article = Db::name('article')
  206. ->where('id',$id)
  207. ->find();
  208. $roles = $article['roles_ids']?explode(',',$article['roles_ids']):array();
  209. $user_ids = $this->get_workers($roles,$org); //所有人的id
  210. if ($article['limit'] > 0) {
  211. $roles_ids = $this->get_worker_roles($roles,$org);
  212. $finishuser = Db::name('record')
  213. ->where('org_id', $org)
  214. ->where('article_id', $id)
  215. ->where('roles_id', 'in',$roles_ids)
  216. ->field('SUM(time) as times,user_id')
  217. ->group('user_id')
  218. // ->distinct(true)
  219. ->having('times >='.$article['limit'])
  220. ->select();
  221. $finishuserids = array(); // 达标的人
  222. if($finishuser){
  223. foreach ($finishuser as $k=>$v){
  224. $finishuserids[] = $v['user_id'];
  225. }
  226. }
  227. $user_ids = array_diff($user_ids, $finishuserids);
  228. } else {
  229. $user_ids = array();
  230. }
  231. $ret = $this->get_user_lists($user_ids,$start,$length);
  232. return $ret;
  233. }
  234. public function get_workers($roles,$org){
  235. $roles_ids = array();
  236. if ($roles) {
  237. $roles_ids = Db::name('roles')
  238. ->where('enable',1)
  239. ->where('del',0)
  240. ->where('org_id',$org)
  241. ->where('parent_id','in',$roles)
  242. ->column('id');
  243. }else{
  244. // $roles_ids = Db::name('roles')
  245. // ->where('enable',1)
  246. // ->where('del',0)
  247. // ->where('org_id',$org)
  248. // ->where('parent_id','in',[3,4,5,6,7,8,9,10,11])
  249. // ->column('id');
  250. }
  251. $this->db = Db::name('user')
  252. ->alias('a');
  253. if($roles){
  254. if($roles_ids){
  255. $this->db->where('b.roles_id','in',$roles_ids);
  256. }else{
  257. $this->db->where('b.roles_id',-1);
  258. }
  259. }
  260. $this->db->where('c.org_id',$org);
  261. $this->db->where('a.del',0);
  262. $this->db->join('user_roles b','a.id = b.user_id');
  263. $this->db->join('user_org c','a.id = c.user_id');
  264. $userIds = $this->db->column('a.id');
  265. return $userIds;
  266. }
  267. public function get_user_lists($user_ids,$start,$length, $type = 0)
  268. {
  269. $pageSize = 10;
  270. $total = 0;
  271. $list = array();
  272. if ($user_ids) {
  273. $this->db = Db::name('user')
  274. ->alias('a');
  275. $this->db->where('a.id','in', $user_ids);
  276. $this->db->where('a.type', $type);
  277. $this->db->where('a.del', 0);
  278. $this->db->join('user_roles b', 'b.user_id = a.id');
  279. $db = clone $this->db;
  280. $this->db->field('count(*) as count');
  281. $count = $this->db->find();
  282. $this->db = $db;
  283. $this->db->limit($start, $length);
  284. $this->db->field('a.*,b.roles_id');
  285. $list = $this->db->select();
  286. $list = $list?$list:array();
  287. foreach ($list as $k=>$v){
  288. $list[$k]['roles_name'] = Db::name('roles')
  289. ->where('id',$v['roles_id'])->value('name');
  290. }
  291. }
  292. $count = empty($count) ? 0 : $count['count'];
  293. return array('data' => $list, 'total' => $count, 'count' => $count);
  294. }
  295. }