Examine.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. $time_array = getTimeArray();
  57. $category_list = db('oa_examine_category')
  58. ->where(['status' => 1,'is_deleted' => ['neq',1]])
  59. ->field('category_id,title')
  60. ->select();
  61. $fields = ['create_user_id'];
  62. foreach ($category_list as $key=>$val) {
  63. // 拼接表头标题
  64. $category_list[$key]['title'] = strstr($val['title'],'审批') ? str_replace('审批','次数',$val['title']) : $val['title'].'次数';
  65. $fields['SUM(CASE WHEN category_id = ' . $val['category_id'] . ' THEN 1 ELSE 0 END)'] = 'count_' . $val['category_id'];
  66. }
  67. $sql = OaExamineModel::field($fields)
  68. ->where([
  69. 'create_time' => ['BETWEEN', $time_array['between']],
  70. 'create_user_id' => ['IN', $userIds],
  71. 'check_status' => ['neq', 4]
  72. ])
  73. ->group('create_user_id')
  74. ->fetchSql()
  75. ->select();
  76. $list = queryCache($sql);
  77. // $list = array_column($list, null, 'create_user_id');
  78. foreach ($list as $key => $val) {
  79. $val['realname'] = $userModel->getUserById($val['create_user_id'])['realname'];
  80. $val['id'] = $val['create_user_id'];
  81. $list[$key] = $val;
  82. }
  83. $data = [
  84. 'category_list' => $category_list,
  85. 'userList' => $list
  86. ];
  87. return $data ? : [];
  88. }
  89. }