package com.th.demo.controller.system;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.th.demo.model.system.SysDepartment;
import com.th.demo.model.system.TreeNode;
import com.th.demo.model.system.Type;

import com.th.demo.service.system.SysDepartmentService;
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.bind.annotation.RestController;
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.Date;
import java.util.List;

/**
 * Created by dell on 2017-07-07. lee test
 */
@RestController
@RequestMapping(value = "/SysDepartment")
public class SysDepartmentController {


    @Autowired
    public SysDepartmentService sysDepartmentService;

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



    @RequestMapping(value = "/queryTree.do")
    public String queryTree() throws IOException {
        result = sysDepartmentService.query("");
        return result;
    }

    @RequestMapping(value = "/queryDeptTree.do")
    public String queryDeptTree() throws IOException {
        result = sysDepartmentService.queryDeptTree();
        return result;
    }


    @RequestMapping(value = "/queryByFid.do" )
    public String queryByFid( String fid,
                          HttpServletResponse response) throws IOException {

        List<SysDepartment> list = sysDepartmentService.queryByFid(fid);

        result =   gson.toJson(list)  ;
        return result;
    }
    @RequestMapping(value = "/queryById.do" )
    public String queryById( String id ) throws IOException {

         SysDepartment dept = sysDepartmentService.queryById(id);

        result =   dept.getDeptName()  ;
        return result;
    }

    @RequestMapping(value = "/save.do", method = RequestMethod.POST)//Post方法
    public String save(String id,
                        String fid,
                        String deptName,
                        String deptDesc,
                        String userId,
                        HttpServletResponse response) throws IOException {

        result = sysDepartmentService.save(id,fid,deptName,deptDesc,userId);
        return result;
    }

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

        result = sysDepartmentService.remove(id,userId);
        return result;
    }

}
