package com.th.demo.controller.system;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.th.demo.model.system.SysLog;
import com.th.demo.model.system.SysLogClient;
import com.th.demo.model.system.SysUser;
import com.th.demo.model.system.Type;
import com.th.demo.service.impl.system.SysLogServiceImpl;
import com.th.demo.tools.Tools;
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.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "/SysLog")
public class SysLogController {
    @Autowired
    public SysLogServiceImpl sysLogServiceImpl;

    Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd hh:mm:ss").create();
    String result = Type.FAIL;

    @RequestMapping(value = "/query.do", method = RequestMethod.POST)
    public void query(
            String startDate,
            String endDate,
            String logUserName,
            String logUserDesc,
            String logIp,
            String logHostName,
            String logJmId,
            String logType,
            HttpServletResponse response) throws IOException {

        List<SysLog> list = new ArrayList<SysLog>();

        list = sysLogServiceImpl.query(startDate, endDate, logUserName, logUserDesc, logIp, logHostName, logJmId, logType);
        result = gson.toJson(list);
        //System.out.println(result);

        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-type", "text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.print(result);
        out.flush();
        out.close();
    }


    @RequestMapping(value = "/add.do", method = RequestMethod.POST)
    public void add(@RequestParam(value = "param1") String userId,
                    @RequestParam(value = "param2") String ip,
                    @RequestParam(value = "param3") String hostName,
                    @RequestParam(value = "param4") String jmId,
                    @RequestParam(value = "param5") String logType,
                    @RequestParam(value = "param6") String logInfo,
                    HttpServletResponse response) throws IOException {

        SysLog sysLog = new SysLog();
        SysUser user = new SysUser();
        user.setId(userId);
        sysLog.setUser(user);
        sysLog.setLogIp(ip);
        sysLog.setLogHostname(hostName);
        sysLog.setLogJmId(jmId);
        sysLog.setLogType(logType);
        sysLog.setLogInfo(logInfo);
        sysLog.setAddTime(new Date());

        result = Tools.moreThanZero(sysLogServiceImpl.add(sysLog));

        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-type", "text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.print(result);
        out.flush();
        out.close();
    }

    @RequestMapping(value = "/addLogClient.do", method = RequestMethod.POST)
    public void addLogClient(@RequestParam(value = "param1") String str,
                             HttpServletResponse response
    ) throws IOException {

        SysLogClient sysLogClient = gson.fromJson(str, SysLogClient.class);
        sysLogClient.setCreateTime(new Date());
        result = Tools.moreThanZero(sysLogServiceImpl.addLogClient(sysLogClient));

        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-type", "text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.print(result);
        out.flush();
        out.close();
    }

    @RequestMapping(value = "/show.do")
    public ModelAndView show() throws IOException {
        return new ModelAndView("system/syslog");
    }

}
