package com.yihu.jw.base.endpoint.methodlog;

import com.yihu.jw.base.methlog.BaseMethodLogDO;
import com.yihu.jw.base.methlog.BaseMethodLogService;
import com.yihu.jw.entity.base.doctor.BaseDoctorDO;
import com.yihu.jw.hospital.httplog.service.WlyyHttpLogService;
import com.yihu.jw.restmodel.base.doctor.BaseDoctorVO;
import com.yihu.jw.restmodel.web.MixEnvelop;
import com.yihu.jw.restmodel.web.PageEnvelop;
import com.yihu.jw.restmodel.web.endpoint.EnvelopRestEndpoint;
import com.yihu.jw.rm.base.BaseRequestMapping;
import com.yihu.jw.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统日志
 * @author huangwenjie
 * @date 2020/4/11 10:16
 */
@RestController
@RequestMapping(value = BaseRequestMapping.BaseMethodLog.PREFIX)
@Api(value = "系统日志管理", description = "系统日志管理接口", tags = {"基础服务 - 系统日志管理管理服务接口"})
public class MethodLogEndpoint extends EnvelopRestEndpoint {
	
	@Autowired
	private BaseMethodLogService baseMethodLogService;
	@Autowired
	private WlyyHttpLogService wlyyHttpLogService;
	
	@GetMapping(value = BaseRequestMapping.BaseMethodLog.getLog)
	@ApiOperation(value = "获取分页")
	public PageEnvelop<BaseMethodLogDO> page(
			@ApiParam(name = "fields", value = "返回的字段，为空返回全部字段")
			@RequestParam(value = "fields", required = false) String fields,
			@ApiParam(name = "filters", value = "过滤器，为空检索所有条件")
			@RequestParam(value = "filters", required = false) String filters,
			@ApiParam(name = "sorts", value = "排序，规则参见说明文档")
			@RequestParam(value = "sorts", required = false) String sorts,
			@ApiParam(name = "page", value = "分页大小", required = true, defaultValue = "1")
			@RequestParam(value = "page") int page,
			@ApiParam(name = "size", value = "页码", required = true, defaultValue = "15")
			@RequestParam(value = "size") int size) throws Exception {

				if(StringUtil.isBlank(sorts)){
					sorts = "-createTime";
				}

		List<BaseMethodLogDO> baseDoctors = baseMethodLogService.search(fields, filters, sorts, page, size);
		int count = (int) baseMethodLogService.getCount(filters);
		return success(baseDoctors, count, page, size);
	}

	@PostMapping(value = "/findHttpLog")
	@ApiOperation(value = "查询接口操作日志")
	public MixEnvelop findHttpLog(@ApiParam(name = "startTime", value = "startTime")
								  @RequestParam(value = "startTime", required = false) String startTime,
								  @ApiParam(name = "endTime", value = "endTime")
								  @RequestParam(value = "endTime", required = false) String endTime,
								  @ApiParam(name = "interfaceName", value = "interfaceName")
								  @RequestParam(value = "interfaceName", required = false) String interfaceName,
								  @ApiParam(name = "code", value = "code")
                                  @RequestParam(value = "code", required = false) String code,
								  @ApiParam(name = "patient", value = "patient")
								  @RequestParam(value = "patient", required = false) String patient,
								  @ApiParam(name = "doctor", value = "doctor")
								  @RequestParam(value = "doctor", required = false) String doctor,
								  @ApiParam(name = "responseKeyWord", value = "返回值关键字")
									  @RequestParam(value = "responseKeyWord", required = false) String responseKeyWord,
								  @ApiParam(name = "requestKeyWord", value = "请求参数关键字")
									  @RequestParam(value = "requestKeyWord", required = false) String requestKeyWord,
								  @ApiParam(name = "page", value = "page")
								  @RequestParam(value = "page", required = false) Integer page,
								  @ApiParam(name = "pageSize", value = "pageSize")
								  @RequestParam(value = "pageSize", required = false) Integer pageSize
	) {

		return wlyyHttpLogService.findWlyyHttpLogInBase(startTime,endTime,code,interfaceName,patient,doctor,responseKeyWord,requestKeyWord,page,pageSize);
	}
	@GetMapping(value = "/findLoginLog")
	@ApiOperation(value = "查询接口操作日志")
	public MixEnvelop findLoginLog(@ApiParam(name = "startTime", value = "startTime")
								  @RequestParam(value = "startTime", required = false) String startTime,
								  @ApiParam(name = "endTime", value = "endTime")
								  @RequestParam(value = "endTime", required = false) String endTime,
								   @ApiParam(name = "opreateStart", value = "opreateStart")
									   @RequestParam(value = "opreateStart", required = false) String opreateStart,
								   @ApiParam(name = "opreateEnd", value = "opreateEnd")
									   @RequestParam(value = "opreateEnd", required = false) String opreateEnd,
								  @ApiParam(name = "userId", value = "userId")
								  @RequestParam(value = "userId", required = false) String userId,
								  @ApiParam(name = "userName", value = "userName")
								  @RequestParam(value = "userName", required = false) String userName,
								  @ApiParam(name = "openId", value = "openId")
								  @RequestParam(value = "openId", required = false) String openId,
								  @ApiParam(name = "userAgent", value = "userAgent")
								  @RequestParam(value = "userAgent", required = false) String userAgent,
								  @ApiParam(name = "loginType", value = "2医生 3 患者 4 授权")
								  @RequestParam(value = "loginType", required = false) String loginType,
								  @ApiParam(name = "page", value = "page")
								  @RequestParam(value = "page", required = false) Integer page,
								  @ApiParam(name = "pageSize", value = "pageSize")
								  @RequestParam(value = "pageSize", required = false) Integer pageSize
	) {

		return wlyyHttpLogService.findLoginLog(startTime,endTime,userId,userName,openId,userAgent, loginType, opreateStart, opreateEnd,page,pageSize);
	}

}
