package com.ho.user.controller;


import com.ho.common.tools.annotation.LogAnnotation;
import com.ho.common.tools.constant.CommonConstant;
import com.ho.common.tools.constant.ContextConstant;
import com.ho.common.tools.constant.RedisKeyConstant;
import com.ho.common.tools.entity.UserDetailRespVO;
import com.ho.common.tools.exception.DataResult;
import com.ho.common.tools.service.RedisService;
import com.ho.common.tools.util.PageResult;
import com.ho.common.tools.entity.SimpleUser;
import com.ho.user.api.entity.SysLog;
import com.ho.user.service.LogService;
import com.ho.user.vo.req.SysLogPageReqVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName: SysLogController
 * TODO:类文件简单描述
 * @Author:
 * @CreateDate: 2019/9/23 16:15
 * @UpdateUser:
 * @UpdateDate: 2019/9/23 16:15
 * @Version: 0.0.1
 */
@RequestMapping(ContextConstant.SYS + "logs")
@Api(tags = "系统模块-系统操作日志管理")
@RestController
public class SysLogController {

    @Autowired
    private LogService logService;

    @Autowired
    private RedisService redisService;


    @PostMapping("page")
    @ApiOperation(value = "分页查询系统操作日志接口")
    //@LogAnnotation(title = "系统操作日志管理", action = "分页查询系统操作日志")
    //@RequiresPermissions("sys:log:list")
    public DataResult<PageResult<SysLog>> pageInfo(@RequestBody SysLogPageReqVO vo, HttpServletRequest request) {
        String token = request.getHeader(CommonConstant.ACCESS_TOKEN);
        SimpleUser simpleUser = redisService.getSimpleUserByToken(token);
        //对输入的时间进行处理
        if(vo.getStartTime() != null){
            String beginTime = vo.getStartTime()+CommonConstant.START_SUFFIX_TIMESTAMP;
            String time = vo.getEndTime()+CommonConstant.END_SUFFIX_TIMESTAMP;
            vo.setStartTime(beginTime);
            vo.setEndTime(time);
        }
        PageResult<SysLog> sysLogPageVO = logService.pageInfo(vo,simpleUser);
        DataResult<PageResult<SysLog>> result = DataResult.success();
        result.setData(sysLogPageVO);
        return result;
    }



    @DeleteMapping("delete")
    @ApiOperation(value = "删除日志接口")
    @LogAnnotation(title = "系统操作日志管理", action = "删除系统操作日志")
    @RequiresPermissions("sys:log:deleted")
    public DataResult deleted(@RequestBody @ApiParam(value = "日志id集合") List<Long> logIds) {
        logService.deleted(logIds);
        return DataResult.success();
    }
}
