DROP TABLE IF EXISTS `app_log`;
CREATE TABLE `app_log` (
  `id` varchar(64) NOT NULL,
  `appcode` varchar(10) DEFAULT NULL COMMENT 'app编码',
  `appversion` varchar(10) DEFAULT NULL COMMENT 'app版本',
  `device` varchar(10) DEFAULT NULL COMMENT '设备类型',
  `sysversion` varchar(10) DEFAULT NULL COMMENT '系统版本',
  `service` varchar(32) DEFAULT NULL COMMENT '服务',
  `action` varchar(32) DEFAULT NULL COMMENT '操作',
  `method` varchar(10) DEFAULT NULL COMMENT '请求方式',
  `status` varchar(10) DEFAULT NULL COMMENT '请求状态',
  `reqheader` text COMMENT '请求头',
  `reqbody` text COMMENT '请求业务数据',
  `exception` longtext COMMENT '异常',
  `create_date` datetime DEFAULT NULL COMMENT '请求时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;


SELECT a.raceId , a.raceName , a.startTime , a.endTime , 
	case when c.joinTotal is null then 0 else c.joinTotal end as joinTotal , 
	case when b.uid is null 	  then 0 else b.uid end as uid  , 
	case when b.score is null 	  then 0 else b.score end as score  , 
	case when d.truename is null  then '' else d.truename end as truename 
FROM ( 
	select raceId ,raceName , startTime , endTime  
	from Race 
	where  (status = 0  or current_timestamp > endTime) 
	order by raceId desc limit :limit offset :offset 
	) a  
left join  (
	SELECT DISTINCT ON (raceId)	uid, raceId, score  
	FROM raceScore  
	ORDER BY  raceId , score DESC 
	) b  on  a.raceId = b.raceId  
left join  ( 
	SELECT raceId , count(*) as joinTotal  
	FROM raceScore  GROUP BY raceId 
	) c  ON a.raceId = c.raceId  
LEFT JOIN V_baseuser d  on b.uid = d.studentId  
order by a.endTime desc 

@Cache(prefix=_3ECacheContants.CACHE_PREFIX_QWHSYB,key="#page.limit,#page.offset",duration=60*60*4)
