package com.th.demo.controller.system;

import com.alibaba.fastjson.JSON;
import com.th.demo.model.system.ResponseCodeMsg;
import com.th.demo.model.system.ResultJSON;
import com.th.demo.model.system.Type;
import com.th.demo.service.system.SysUserService;
import com.th.demo.tools.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;


@RestController
@RequestMapping(value = "/SysUser")
public class SysUserController {
    @Autowired
    public SysUserService sysUserService;
    String result = Type.FAIL;

    @RequestMapping(value = "/query.do")//查询方法 前两个参数是分页参数
    public String query(int page,
                        int rows,
                        String userDesc,
                        HttpServletRequest request) {

        try {
            String userId = (String) request.getAttribute("userId");
            String belongId = (String) request.getAttribute("belongId");
            result = sysUserService.query(page, rows, userDesc,userId,belongId);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSON.toJSONString(new ResultJSON(ResponseCodeMsg.CODE_EX, ex.getMessage(), null));
        } finally {
            return result;
        }
    }


    @RequestMapping(value = "/queryByDept.do")//查询方法 前两个参数是分页参数
    public String queryByDept(String deptId,
                        HttpServletRequest request) {

        try {
            String userId = (String) request.getAttribute("userId");
            result = sysUserService.queryByDept(deptId);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSON.toJSONString(new ResultJSON(ResponseCodeMsg.CODE_EX, ex.getMessage(), null));
        } finally {
            return result;
        }
    }

    @RequestMapping(value = "/save.do", method = RequestMethod.POST)//Post方法
    public String save(String json,
                       HttpServletRequest request) {
        try {
            String userId = (String) request.getAttribute("userId");
            String belongId = (String) request.getAttribute("belongId");
            result = sysUserService.save(json, userId,belongId);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSON.toJSONString(new ResultJSON(ResponseCodeMsg.CODE_EX, ex.getMessage(), null));
        } finally {
            return result;
        }
    }

    @RequestMapping(value = "/remove.do", method = RequestMethod.POST)
    public String remove(String id,
                         HttpServletRequest request)  {
        try {
            String userId = (String) request.getAttribute("userId");
            result = sysUserService.remove(id, userId);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSON.toJSONString(new ResultJSON(ResponseCodeMsg.CODE_EX, ex.getMessage(), null));
        } finally {
            return result;
        }
    }


    @RequestMapping(value = "/updatePassword.do", method = RequestMethod.POST)
    public String updatePassword(
            String oldPassword,
            String newPassword,
            HttpServletRequest request) {
        try {
            String userId = (String) request.getAttribute("userId");
            result = sysUserService.updatePassword(userId, oldPassword, newPassword);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSON.toJSONString(new ResultJSON(ResponseCodeMsg.CODE_EX, ex.getMessage(), null));
        } finally {
            return result;
        }

    }


}
