Statistics.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * @title:
  4. * @user: zwq
  5. * @date: 2025-02-17 19:28
  6. */
  7. namespace app\admin\controller\statistics;
  8. use app\admin\controller\Base;
  9. use app\admin\extend\screen\TimeHandle;
  10. use app\common\model\base\org\Org;
  11. use app\common\model\base\org\OrgType;
  12. use app\common\model\base\user\UserRole;
  13. use app\common\model\enroll\EnrollStudents;
  14. use app\common\model\enroll\JdfEnrollVolunteer;
  15. use daorui\platform\platformAuth;
  16. class Statistics extends Base
  17. {
  18. protected $noNeedLogin = [];
  19. protected $noNeedAuth = [];
  20. public function initialize()
  21. {
  22. parent::initialize(); // TODO: Change the autogenerated stub
  23. }
  24. /**
  25. * Notes:获取组织列表
  26. * User: zhang
  27. * Date: 2025/2/18
  28. * Time: 16:16
  29. */
  30. public function getOrgList()
  31. {
  32. $userinfo = $this->userinfo;
  33. $SchoolCode = (new platformAuth())->interfaceRequest('getSchoolCode', []);
  34. $codes = array_column($SchoolCode['data'], 'code');
  35. if (!$userinfo['is_developer']) {
  36. $org_ids =$this->userinfo['org_id'];
  37. $where = [];
  38. $where[] = ['type', 'IN', $codes];
  39. if($org_ids!==1){
  40. $where[] = ['org_id', 'IN', $org_ids];
  41. }
  42. $list = Org::where($where)->field("org_id,name,type")->select();
  43. } else {
  44. $list = Org::where('type', 'IN', $codes)->field("org_id,name,type")->select();
  45. }
  46. return res(1, '获取成功', $list);
  47. }
  48. /**
  49. * Notes:预约人数统计
  50. * User: zhang
  51. * Date: 2025/2/17
  52. * Time: 19:58
  53. */
  54. public function orderNumCount()
  55. {
  56. $data = $this->request->param();
  57. $where = [];
  58. $SchoolCode = (new platformAuth())->interfaceRequest('getSchoolCode', []);
  59. $codes = array_column($SchoolCode['data'], 'code');
  60. $timeMode = 'Y-m-d H:i:s';//需要的时间格式
  61. $x = [];
  62. $userinfo = $this->userinfo;
  63. if (empty($data['type'])) {
  64. $data['type'] = 1;
  65. }
  66. switch ($data['type']) {
  67. case 1:
  68. //近3天
  69. $timeData = TimeHandle::getSrceenSE("", 1, $timeMode);
  70. $x = TimeHandle::getChartX($timeData['s'], $timeData['e'], 2)['x'];
  71. // 构造条件
  72. $where[] = ['b.create_at', '>=', $timeData['s']];
  73. $where[] = ['b.create_at', '<=', $timeData['e']];
  74. break;
  75. case 2:
  76. //近7天
  77. $timeData = TimeHandle::getSrceenSE("", 2, $timeMode);
  78. $x = TimeHandle::getChartX($timeData['s'], $timeData['e'], 2)['x'];
  79. // 构造条件
  80. $where[] = ['b.create_at', '>=', $timeData['s']];
  81. $where[] = ['b.create_at', '<=', $timeData['e']];
  82. break;
  83. }
  84. if (!$userinfo['is_developer']) {
  85. $org_ids = UserRole::where('user_id', $this->userinfo['user_id'])->column("org_id");
  86. $types = Org::where('org_id', 'IN', $org_ids)->field("org_id,name,type")->column("type");
  87. //去重
  88. $types = array_unique($types);
  89. $where[] = ['b.stage_code', 'IN', $types];
  90. if (!in_array(1, $org_ids)) {
  91. $where[] = ['a.org_id', 'IN', $org_ids];
  92. }
  93. } else {
  94. $where[] = ['b.stage_code', 'IN', $codes];
  95. }
  96. if (!empty($data['org_id'])) {
  97. $where[] = ['a.org_id', '=', $data['org_id']];
  98. }
  99. $list = JdfEnrollVolunteer::alias('a')
  100. ->join('jdf_enroll_students b', 'a.enroll_id=b.enroll_id')
  101. ->where($where)
  102. ->field('a.org_id,b.semester_date,b.create_at,b.stage_code,count(*) as value')
  103. ->group('DATE(b.create_at),b.stage_code')->select()->toArray();
  104. // $list = EnrollStudents::where($where)->field('semester_date,stage_code,count(*) as value')->group('DATE(create_at),stage_code')->select()->toArray();
  105. $arr_code = array_column($list, 'stage_code');
  106. $arr_code = array_unique($arr_code);
  107. $typeList = OrgType::where('code', 'IN', $arr_code)->field("org_type_name as name,code")->select();
  108. // 初始化数据数组,默认为0
  109. $initialData = [];
  110. foreach ($x as $date) {
  111. foreach ($arr_code as $code) {
  112. if (!isset($initialData[$code])) {
  113. $initialData[$code] = [];
  114. }
  115. $initialData[$code][$date] = 0; // 初始化为0
  116. }
  117. }
  118. // 遍历已有数据并填充
  119. foreach ($list as $item) {
  120. $date = date('Y-m-d', strtotime($item['semester_date']));
  121. $date = TimeHandle::beginToday(2, $date, 'd') . '号';// 将日期格式化为Y-m-d,与$x中的格式一致
  122. $stageCode = $item['stage_code'];
  123. $value = $item['value'];
  124. $initialData[$stageCode][$date] = $value; // 填充已有数据
  125. }
  126. // 转换数据格式,符合之前的返回结构
  127. $typeData = [];
  128. foreach ($initialData as $stageCode => $dailyData) {
  129. $values = array_values($dailyData); // 将日期作为键的数组转换为值数组,保持顺序
  130. $typeData[$stageCode] = $values;
  131. }
  132. return res(1, '获取成功', ['x' => $x, 'data' => $typeData, 'type' => $typeList]);
  133. }
  134. /**
  135. * Desc :预约性别统计
  136. * User : zwq
  137. * Date : 2025-02-17 19:31
  138. */
  139. public function orderSexCount()
  140. {
  141. $org_id = input('org_id/d', 0);
  142. $where = [];
  143. $userinfo = $this->userinfo;
  144. if (!$userinfo['is_developer']) {
  145. $org_ids = UserRole::where('user_id', $userinfo['user_id'])->column("org_id");
  146. if (!in_array(1, $org_ids)) {// 非集团
  147. $where[] = ['a.org_id', 'IN', $org_ids];
  148. }
  149. }
  150. if (!empty($org_id)) {
  151. $where[] = ['a.org_id', '=', $org_id];
  152. }
  153. $list = JdfEnrollVolunteer::alias('a')
  154. ->join('jdf_enroll_students b', 'a.enroll_id=b.enroll_id')
  155. ->where($where)
  156. ->field('a.org_id,b.sex,count(*) as value')
  157. ->group('b.sex')
  158. ->select()
  159. ->toArray();
  160. // $list = EnrollStudents::where($where)->field('sex,count(*) as value')->group('sex')->select()->toArray();
  161. if ($list) {
  162. // 计算总人数
  163. $total = array_sum(array_column($list, 'value'));
  164. foreach ($list as $k => &$v) {
  165. $list[$k]['name'] = $v['sex'] == 1 ? '男' : '女';
  166. $v['percentage'] = round(($v['value'] / $total) * 100, 2); // 保留两位小数
  167. }
  168. // 移除引用,因为我们在foreach中使用了引用
  169. unset($v);
  170. } else {
  171. $list = [];
  172. }
  173. return res(1, '获取成功', $list);
  174. }
  175. }