Examine.php 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 审批统计
  4. // +----------------------------------------------------------------------
  5. // | Author: Michael_xu | gengxiaoxu@5kcrm.com
  6. // +----------------------------------------------------------------------
  7. namespace app\bi\model;
  8. use think\Db;
  9. use app\admin\model\Common;
  10. use app\admin\model\User as UserModel;
  11. use app\bi\model\Examine as OaExamineModel;
  12. use think\Request;
  13. class Examine extends Common
  14. {
  15. /**
  16. * 为了数据库的整洁,同时又不影响Model和Controller的名称
  17. * 我们约定每个模块的数据表都加上相同的前缀,比如CRM模块用crm作为数据表前缀
  18. */
  19. protected $name = 'oa_examine';
  20. protected $createTime = 'create_time';
  21. protected $updateTime = 'update_time';
  22. protected $autoWriteTimestamp = true;
  23. private $statusArr = ['0'=>'待审核','1'=>'审核中','2'=>'审核通过','3'=>'已拒绝','4'=>'已撤回'];
  24. /**
  25. * [getSortByExamine 排序]
  26. * @author zhi
  27. * @param
  28. * @return
  29. */
  30. public function getSortByExamine($whereArr)
  31. {
  32. $count = OaExamineModel::where($whereArr)
  33. ->group('create_user_id')
  34. ->field('create_user_id,count(*) as count')
  35. ->order('count desc')
  36. ->select();
  37. return $count;
  38. }
  39. /**
  40. * [getStatistics 审批统计]
  41. *
  42. * @param $param
  43. * @return array
  44. * @throws \think\db\exception\DataNotFoundException
  45. * @throws \think\db\exception\ModelNotFoundException
  46. * @throws \think\exception\DbException
  47. */
  48. public function getStatistics($param)
  49. {
  50. $userModel = new \app\admin\model\User();
  51. $adminModel = new \app\admin\model\Admin();
  52. $perUserIds = []; //权限范围内userIds
  53. $whereData = $adminModel->getWhere($param, '', $perUserIds); //统计条件
  54. $userIds = $whereData['userIds'];
  55. //时间
  56. $category_list = db('oa_examine_category')
  57. ->where(['status' => 1,'is_deleted' => ['neq',1]])
  58. ->field('category_id,title')
  59. ->select();
  60. $fields = ['create_user_id'];
  61. foreach ($category_list as $key=>$val) {
  62. // 拼接表头标题
  63. $category_list[$key]['title'] = strstr($val['title'],'审批') ? str_replace('审批','次数',$val['title']) : $val['title'].'次数';
  64. $fields['SUM(CASE WHEN category_id = ' . $val['category_id'] . ' THEN 1 ELSE 0 END)'] = 'count_' . $val['category_id'];
  65. }
  66. $sql = OaExamineModel::field($fields)
  67. ->where([
  68. 'create_time' => ['BETWEEN', $whereData['between_time']],
  69. 'create_user_id' => ['IN', $userIds],
  70. 'check_status' => ['neq', 4]
  71. ])
  72. ->group('create_user_id')
  73. ->fetchSql()
  74. ->select();
  75. $list = queryCache($sql);
  76. // $list = array_column($list, null, 'create_user_id');
  77. foreach ($list as $key => $val) {
  78. $val['realname'] = $userModel->getUserById($val['create_user_id'])['realname'];
  79. $val['id'] = $val['create_user_id'];
  80. $list[$key] = $val;
  81. }
  82. $data = [
  83. 'category_list' => $category_list,
  84. 'userList' => $list
  85. ];
  86. return $data ? : [];
  87. }
  88. }