123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- <?php
- /**
- * @title:
- * @user: zwq
- * @date: 2025-02-17 19:28
- */
- namespace app\admin\controller\statistics;
- use app\admin\controller\Base;
- use app\admin\extend\screen\TimeHandle;
- use app\common\model\base\org\Org;
- use app\common\model\base\org\OrgType;
- use app\common\model\base\user\UserRole;
- use app\common\model\enroll\EnrollStudents;
- use app\common\model\enroll\JdfEnrollVolunteer;
- use daorui\platform\platformAuth;
- class Statistics extends Base
- {
- protected $noNeedLogin = [];
- protected $noNeedAuth = [];
- public function initialize()
- {
- parent::initialize(); // TODO: Change the autogenerated stub
- }
- /**
- * Notes:获取组织列表
- * User: zhang
- * Date: 2025/2/18
- * Time: 16:16
- */
- public function getOrgList()
- {
- $userinfo = $this->userinfo;
- $SchoolCode = (new platformAuth())->interfaceRequest('getSchoolCode', []);
- $codes = array_column($SchoolCode['data'], 'code');
- if (!$userinfo['is_developer']) {
- $org_ids =$this->userinfo['org_id'];
- $where = [];
- $where[] = ['type', 'IN', $codes];
- if($org_ids!==1){
- $where[] = ['org_id', 'IN', $org_ids];
- }
- $list = Org::where($where)->field("org_id,name,type")->select();
- } else {
- $list = Org::where('type', 'IN', $codes)->field("org_id,name,type")->select();
- }
- return res(1, '获取成功', $list);
- }
- /**
- * Notes:预约人数统计
- * User: zhang
- * Date: 2025/2/17
- * Time: 19:58
- */
- public function orderNumCount()
- {
- $data = $this->request->param();
- $where = [];
- $SchoolCode = (new platformAuth())->interfaceRequest('getSchoolCode', []);
- $codes = array_column($SchoolCode['data'], 'code');
- $timeMode = 'Y-m-d H:i:s';//需要的时间格式
- $x = [];
- $userinfo = $this->userinfo;
- if (empty($data['type'])) {
- $data['type'] = 1;
- }
- switch ($data['type']) {
- case 1:
- //近3天
- $timeData = TimeHandle::getSrceenSE("", 1, $timeMode);
- $x = TimeHandle::getChartX($timeData['s'], $timeData['e'], 2)['x'];
- // 构造条件
- $where[] = ['b.create_at', '>=', $timeData['s']];
- $where[] = ['b.create_at', '<=', $timeData['e']];
- break;
- case 2:
- //近7天
- $timeData = TimeHandle::getSrceenSE("", 2, $timeMode);
- $x = TimeHandle::getChartX($timeData['s'], $timeData['e'], 2)['x'];
- // 构造条件
- $where[] = ['b.create_at', '>=', $timeData['s']];
- $where[] = ['b.create_at', '<=', $timeData['e']];
- break;
- }
- if (!$userinfo['is_developer']) {
- $org_ids = UserRole::where('user_id', $this->userinfo['user_id'])->column("org_id");
- $types = Org::where('org_id', 'IN', $org_ids)->field("org_id,name,type")->column("type");
- //去重
- $types = array_unique($types);
- $where[] = ['b.stage_code', 'IN', $types];
- if (!in_array(1, $org_ids)) {
- $where[] = ['a.org_id', 'IN', $org_ids];
- }
- } else {
- $where[] = ['b.stage_code', 'IN', $codes];
- }
- if (!empty($data['org_id'])) {
- $where[] = ['a.org_id', '=', $data['org_id']];
- }
- $list = JdfEnrollVolunteer::alias('a')
- ->join('jdf_enroll_students b', 'a.enroll_id=b.enroll_id')
- ->where($where)
- ->field('a.org_id,b.semester_date,b.create_at,b.stage_code,count(*) as value')
- ->group('DATE(b.create_at),b.stage_code')->select()->toArray();
- // $list = EnrollStudents::where($where)->field('semester_date,stage_code,count(*) as value')->group('DATE(create_at),stage_code')->select()->toArray();
- $arr_code = array_column($list, 'stage_code');
- $arr_code = array_unique($arr_code);
- $typeList = OrgType::where('code', 'IN', $arr_code)->field("org_type_name as name,code")->select();
- // 初始化数据数组,默认为0
- $initialData = [];
- foreach ($x as $date) {
- foreach ($arr_code as $code) {
- if (!isset($initialData[$code])) {
- $initialData[$code] = [];
- }
- $initialData[$code][$date] = 0; // 初始化为0
- }
- }
- // 遍历已有数据并填充
- foreach ($list as $item) {
- $date = date('Y-m-d', strtotime($item['semester_date']));
- $date = TimeHandle::beginToday(2, $date, 'd') . '号';// 将日期格式化为Y-m-d,与$x中的格式一致
- $stageCode = $item['stage_code'];
- $value = $item['value'];
- $initialData[$stageCode][$date] = $value; // 填充已有数据
- }
- // 转换数据格式,符合之前的返回结构
- $typeData = [];
- foreach ($initialData as $stageCode => $dailyData) {
- $values = array_values($dailyData); // 将日期作为键的数组转换为值数组,保持顺序
- $typeData[$stageCode] = $values;
- }
- return res(1, '获取成功', ['x' => $x, 'data' => $typeData, 'type' => $typeList]);
- }
- /**
- * Desc :预约性别统计
- * User : zwq
- * Date : 2025-02-17 19:31
- */
- public function orderSexCount()
- {
- $org_id = input('org_id/d', 0);
- $where = [];
- $userinfo = $this->userinfo;
- if (!$userinfo['is_developer']) {
- $org_ids = UserRole::where('user_id', $userinfo['user_id'])->column("org_id");
- if (!in_array(1, $org_ids)) {// 非集团
- $where[] = ['a.org_id', 'IN', $org_ids];
- }
- }
- if (!empty($org_id)) {
- $where[] = ['a.org_id', '=', $org_id];
- }
- $list = JdfEnrollVolunteer::alias('a')
- ->join('jdf_enroll_students b', 'a.enroll_id=b.enroll_id')
- ->where($where)
- ->field('a.org_id,b.sex,count(*) as value')
- ->group('b.sex')
- ->select()
- ->toArray();
- // $list = EnrollStudents::where($where)->field('sex,count(*) as value')->group('sex')->select()->toArray();
- if ($list) {
- // 计算总人数
- $total = array_sum(array_column($list, 'value'));
- foreach ($list as $k => &$v) {
- $list[$k]['name'] = $v['sex'] == 1 ? '男' : '女';
- $v['percentage'] = round(($v['value'] / $total) * 100, 2); // 保留两位小数
- }
- // 移除引用,因为我们在foreach中使用了引用
- unset($v);
- } else {
- $list = [];
- }
- return res(1, '获取成功', $list);
- }
- }
|