123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 商业智能-审核统计
  4. // +----------------------------------------------------------------------
  5. // | Author: zhi | zhijunfu@5kcrm.com
  6. // +----------------------------------------------------------------------
  7. namespace app\bi\controller;
  8. use app\admin\controller\ApiCommon;
  9. use think\Hook;
  10. use think\Request;
  11. use think\Db;
  12. class Examine extends ApiCommon
  13. {
  14. /**
  15. * 用于判断权限
  16. * @permission 无限制
  17. * @allow 登录用户可访问
  18. * @other 其他根据系统设置
  19. **/
  20. public function _initialize()
  21. {
  22. $action = [
  23. 'permission'=>[''],
  24. 'allow'=>['statistics','index','excelexport']
  25. ];
  26. Hook::listen('check_auth',$action);
  27. $request = Request::instance();
  28. $a = strtolower($request->action());
  29. if (!in_array($a, $action['permission'])) {
  30. parent::_initialize();
  31. }
  32. if (!checkPerByAction('bi', 'oa', 'read')) {
  33. header('Content-Type:application/json; charset=utf-8');
  34. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  35. }
  36. }
  37. /**
  38. * 审核统计列表
  39. *
  40. * @return \think\response\Json
  41. * @throws \think\db\exception\DataNotFoundException
  42. * @throws \think\db\exception\ModelNotFoundException
  43. * @throws \think\exception\DbException
  44. */
  45. public function statistics()
  46. {
  47. $param = $this->param;
  48. if ($param['type']) {
  49. $timeArr = getTimeByType($param['type']);
  50. $param['start_time'] = $timeArr[0];
  51. $param['end_time'] = $timeArr[1];
  52. } else {
  53. if (!empty($param['start_time'])) $param['start_time'] = strtotime($param['start_time'] . ' 00:00:00');
  54. if (!empty($param['end_time'])) $param['end_time'] = strtotime($param['end_time'] . ' 23:59:59');
  55. }
  56. $examineModel = new \app\bi\model\Examine();
  57. $list = $examineModel->getStatistics($param) ? : [];
  58. return resultArray(['data'=>$list]);
  59. }
  60. /**
  61. * 审核统计详情列表
  62. * @return
  63. */
  64. public function index()
  65. {
  66. $examineModel = new \app\oa\model\Examine();
  67. $param = $this->param;
  68. $user_id = $param['user_id'];
  69. $category_id = $param['category_id'];
  70. $type = $param['type'];
  71. if (!$user_id || !$category_id) {
  72. return resultArray(['error'=>'参数错误']);
  73. }
  74. //时间
  75. if ($type) {
  76. $timeArr = getTimeByType($type);
  77. $start_time = $timeArr[0];
  78. $end_time = $timeArr[1];
  79. } else {
  80. $start_time = $param['start_time'] ? : strtotime(date('Y-m-d',time()));
  81. $end_time = $param['end_time'] ? : strtotime(date('Y-m-d',time()))+86399;
  82. }
  83. $create_time = array('between',array($start_time,$end_time));
  84. $where = [];
  85. $where['create_user_id'] = $user_id;
  86. $where['check_status'] = 2;
  87. $where['create_time'] = $create_time;
  88. $where['category_id'] = $category_id;
  89. $sumData = 0;
  90. $categoryName = '普通审批';
  91. switch ($category_id) {
  92. case '2' :
  93. $sumData = db('oa_examine')->where($where)->sum('duration');
  94. $categoryName = '请假审批';
  95. break;
  96. case '3' :
  97. $sumData = db('oa_examine')->where($where)->sum('duration');
  98. $categoryName = '出差审批';
  99. break;
  100. case '4' :
  101. $sumData = db('oa_examine')->where($where)->sum('duration');
  102. $categoryName = '加班审批';
  103. break;
  104. case '5' :
  105. $sumData = db('oa_examine')->where($where)->sum('money');
  106. $categoryName = '差旅报销';
  107. break;
  108. case '6' :
  109. $sumData = db('oa_examine')->where($where)->sum('money');
  110. $categoryName = '借款申请';
  111. break;
  112. default :
  113. $categoryName = db('oa_examine_category')->where(['category_id' => $category_id])->value('title');
  114. break;
  115. }
  116. unset($where['create_time']);
  117. unset($where['create_user_id']);
  118. unset($where['create_user_id']);
  119. $where['check_status'] = 'all';
  120. $where['page'] = $param['page'];
  121. $where['limit'] = $param['limit'];
  122. $where['user_id'] = $user_id;
  123. $where['between_time'] = array($start_time,$end_time);
  124. $list = $examineModel->getDataList($where);
  125. $data = [];
  126. $data['list'] = $list ? : [];
  127. $data['sumData'] = $sumData;
  128. $data['categoryName'] = $categoryName;
  129. return resultArray(['data'=>$data]);
  130. }
  131. /**
  132. * 统计导出
  133. * @author Michael_xu
  134. * @param
  135. * @return
  136. */
  137. public function excelExport()
  138. {
  139. $param = $this->param;
  140. $excelModel = new \app\admin\model\Excel();
  141. // 导出的字段列表
  142. $category_list = db('oa_examine_category')->where(['status' => 1,'is_deleted' => ['neq',1]])->field('title,category_id')->select();
  143. $field_list = [];
  144. $field_list[0]['name'] = '员工';
  145. $field_list[0]['field'] = 'realname';
  146. $i = 1;
  147. foreach ($category_list as $k=>$v) {
  148. $field_list[$i]['name'] = strstr($v['title'],'审批') ? str_replace('审批','次数',$v['title']) : $v['title'].'次数';
  149. $field_list[$i]['field'] = 'count_'.$v['category_id'];
  150. $i++;
  151. }
  152. // 文件名
  153. $file_name = '5kcrm_examine_'.date('Ymd');
  154. $excelModel->dataExportCsv($file_name, $field_list, function($list) use ($param){
  155. $examineModel = new \app\bi\model\Examine();
  156. if ($param['type']) {
  157. $timeArr = getTimeByType($param['type']);
  158. $param['start_time'] = $timeArr[0];
  159. $param['end_time'] = $timeArr[1];
  160. }
  161. $list = $examineModel->getStatistics($param);
  162. return $list['userList'];
  163. });
  164. }
  165. }