package com.pqs9900.controller.business;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.njcn.enums.LogTypeEnum;
import com.njcn.pojo.data.DatabaseRunning;
import com.njcn.service.data.DatabaseRunningService;
import com.njcn.service.log.UserLogDetailService;
import com.njcn.utils.PubUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

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

@Controller
@RequestMapping(value = "database")
public class DatabaseRunningController {

    @Autowired
    DatabaseRunningService databaseRunningService;

    @Autowired
    UserLogDetailService userLogDetailService;

    ObjectMapper objectMapper = new ObjectMapper();
    // 日志记录
    private static final Logger logger = LoggerFactory.getLogger(DatabaseRunning.class);

    /**
     * 获取数据库表空间等
     *
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping(value = "getDatabaseRunning", method = RequestMethod.POST)
    @ResponseBody
    public String getDatabaseRunning(HttpServletRequest request) throws JsonProcessingException {
        List<DatabaseRunning> result = new ArrayList<>();
        List<List> s = new ArrayList<>();
        try {
            result = databaseRunningService.getDatabaseRunning();
            if (!result.isEmpty()) {
                for (DatabaseRunning databaseRunning : result) {
                    List<String> str = new ArrayList<String>();
                    str.add(databaseRunning.getTablespacename());
                    str.add(databaseRunning.getConten());
                    str.add(String.valueOf(databaseRunning.getTotals()));
                    str.add(String.valueOf(databaseRunning.getUses()));
                    str.add(String.valueOf(databaseRunning.getFrees()));
                    str.add(String.valueOf(databaseRunning.getPonts()));
                    s.add(str);
                }
            }
            userLogDetailService.getLog("查询数据库运行情况", "成功", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),0);
        } catch (Exception e) {
            userLogDetailService.getLog("查询数据库运行情况", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),0);
            logger.error("获取数据表空间异常，异常是" + e.toString());
        }
        return objectMapper.writeValueAsString(s);
    }
}
