package com.sunyard.ssp.modules.monitor.log.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sunyard.ssp.common.PageVo;
import com.sunyard.ssp.common.Result;
import com.sunyard.ssp.modules.monitor.log.entity.AuditLog;
import com.sunyard.ssp.modules.monitor.log.service.IAuditLogService;
import com.sunyard.ssp.modules.user.entity.ScUser;
import com.sunyard.ssp.utils.PageUtil;
import com.sunyard.ssp.utils.ResultUtil;
import com.sunyard.ssp.utils.SecurityUtil;
import com.sunyard.ssp.vo.SearchVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fyc
 * @since 2020-03-18
 */
@Controller
@Slf4j
@Api(description = "监控管理-审计日志",tags ="监控管理-审计日志")
@RequestMapping("/monitor/auditLog")
@Transactional
public class AuditLogController {

    @Autowired
    private IAuditLogService iAuditLogService;

    @Autowired
    private SecurityUtil securityUtil;

    @RequestMapping(value = "/getById",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "通过id获取")
    public Result<AuditLog> get(@RequestParam Integer id){

        AuditLog entity = iAuditLogService.getById(id);
        return new ResultUtil<AuditLog>().setData(entity);
    }


    @RequestMapping(value = "/queryList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取列表")
    public Result<IPage<AuditLog>> queryList(@RequestParam Integer pageNumber,
                                             @RequestParam Integer pageSize,
                                             String operateContent, String operateType,
                                             String requestMothed, String readUserName,
                                             String startTime,
                                             String endTime){
        IPage<AuditLog> iPage =  iAuditLogService.queryList(pageNumber,pageSize,
                operateContent,operateType,requestMothed,readUserName,startTime,endTime);
        return new ResultUtil<IPage<AuditLog>>().setData(iPage);
    }

    @RequestMapping(value = "/queryListWithOutPage",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取列表")
    public Result<List<AuditLog>> queryListWithOutPage(String operateContent, String operateType,
                                             String requestMothed, String readUserName,
                                             String startTime,
                                             String endTime){
        List<AuditLog> auditLogs =  iAuditLogService.queryList(operateContent,operateType,requestMothed,readUserName,startTime,endTime);
        return new ResultUtil<List<AuditLog>>().setData(auditLogs);
    }

    @RequestMapping(value = "/user/queryList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取列表")
    public Result<IPage<AuditLog>> userLogQueryList(@RequestParam Integer pageNumber,
                                             @RequestParam Integer pageSize,
                                              String readUserName,
                                             String startTime,
                                             String endTime){
        IPage<AuditLog> iPage =  iAuditLogService.userLogQueryList(pageNumber,pageSize,
                null,"登录",null,readUserName,startTime,endTime);
        return new ResultUtil<IPage<AuditLog>>().setData(iPage);
    }

    @RequestMapping(value = "/user/queryListWithOutPage",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取列表")
    public Result<List<AuditLog>> userLogQueryListWithOutPage(
                                                       String readUserName,
                                                       String startTime,
                                                       String endTime){
        List<AuditLog> auditLogs =  iAuditLogService.userLogQueryListWithOutPage(null,"登录",null,readUserName,startTime,endTime);
        return new ResultUtil<List<AuditLog>>().setData(auditLogs);
    }

    @RequestMapping(value = "/queryNeedSignLog",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "分页条件查询可以签名的日志")
    public Result<org.springframework.data.domain.Page<AuditLog>> queryNeedSignLog(@ModelAttribute AuditLog auditLog,
                                                                                   @ModelAttribute SearchVo searchVo,
                                                                                   @ModelAttribute PageVo pageVo){
        org.springframework.data.domain.Page<AuditLog> iPage =  iAuditLogService.queryNeedSignLog(auditLog,searchVo, PageUtil.initPage(pageVo));
        return new ResultUtil<org.springframework.data.domain.Page<AuditLog>>().setData(iPage);
    }

    @RequestMapping(value = "/user/queryNeedSignLog",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "分页条件查询可以签名的日志")
    public Result<org.springframework.data.domain.Page<AuditLog>> userLoginQueryNeedSignLog(@ModelAttribute AuditLog auditLog,
                                                                                   @ModelAttribute SearchVo searchVo,
                                                                                   @ModelAttribute PageVo pageVo){
        org.springframework.data.domain.Page<AuditLog> iPage =  iAuditLogService.userLoginQueryNeedSignLog(auditLog,searchVo, PageUtil.initPage(pageVo));
        return new ResultUtil<org.springframework.data.domain.Page<AuditLog>>().setData(iPage);
    }

    @RequestMapping(value = "/updateReadStatus",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "更新审批状态")
    public Result<Object> updateReadStatus(@RequestParam Integer[] ids){
        ScUser scUser = securityUtil.getCurrUser();
        boolean result = iAuditLogService.updateReadStatus(ids,scUser);
        if(result){
            return new ResultUtil<Object>().setSuccessMsg("审阅日志成功");
        }
        return new ResultUtil<Object>().setErrorMsg("审阅日志失败");
    }


    @RequestMapping(value = "/deleteByIds",method = RequestMethod.DELETE)
    @ResponseBody
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@RequestParam Integer[] ids){

        for(Integer id:ids){
            iAuditLogService.removeById(id);
        }
        return new ResultUtil<Object>().setSuccessMsg("批量通过id删除数据成功");
    }

    @RequestMapping(value = "/truncateTable",method = RequestMethod.DELETE)
    @ResponseBody
    @ApiOperation(value = "清空表")
    public Result<Object> truncateTable(){

        boolean result = iAuditLogService.truncateTable();
        if(result){
            return new ResultUtil<Object>().setSuccessMsg("清空表成功");
        }
        return new ResultUtil<Object>().setErrorMsg("清空表失败");
    }


}
