package com.th.demo.controller.system;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.th.demo.model.system.SysUser;

import com.th.demo.model.system.SysUserDeptPost;
import com.th.demo.model.system.SysUserDeptPostShow;
import com.th.demo.model.system.Type;
import com.th.demo.service.impl.system.SysUserDeptPostServiceImpl;
import com.th.demo.service.impl.system.SysUserServiceImpl;
import com.th.demo.service.system.SysUserDeptPostService;
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.servlet.ModelAndView;

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

/**
 * Created by dell on 2017-07-10.
 */
@Controller
@RequestMapping(value = "/SysUserDeptPost")
public class SysUserDeptPostController {


    @Autowired
    public SysUserDeptPostServiceImpl sysUserDeptPostServiceImpl;

    @Autowired
    public SysUserServiceImpl sysUserServiceImpl;
    @Autowired
    public SysUserDeptPostService sysUserDeptPostService;

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


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


    @RequestMapping(value = "/query.do")
    public void query(
            String userId,
            HttpServletResponse response) throws IOException {

        List<SysUserDeptPostShow> list = sysUserDeptPostServiceImpl.query(userId);
        result = gson.toJson(list);
        Tools.out( response,result);
    }


    @RequestMapping(value = "/queryByDeptPost.do")
    public void queryByDeptPost(
            String deptId,
            String postId,
            HttpServletResponse response) throws IOException {
        List<SysUser> list = new ArrayList<SysUser>();
        if (deptId == null || postId == null || deptId.equals("") || postId.equals("")) {
            result = sysUserServiceImpl.query("", "");
        } else {
            list = sysUserDeptPostServiceImpl.queryByDeptPost(deptId, postId);
            result = gson.toJson(list);
        }

        Tools.out( response,result);
    }

    @RequestMapping(value = "/save.do", method = RequestMethod.POST)
    public void save(String userId,
                     String deptId,
                     String postId,
                     String addId,
                     HttpServletResponse response) throws IOException {
        result = sysUserDeptPostService.save(userId, deptId, postId, addId);
        Tools.out( response,result);
    }

    @RequestMapping(value = "/remove.do", method = RequestMethod.POST)
    public void remove(String id,
                       String userId,
                       HttpServletResponse response) throws IOException {

        result = sysUserDeptPostService.remove(id, userId);
        Tools.out( response,result);
    }
}
