package com.th.demo.controller.develop;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.th.demo.model.system.Type;
import com.th.demo.service.develop.DevTablePropertyService;
import com.th.demo.service.system.SysDicService;
import com.th.demo.tools.ExportExcelTools;
import com.th.demo.tools.Tools;
import org.springframework.beans.factory.annotation.Autowired;
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.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

/**
 * Created by dell on 2017-07-13.
 */
@RestController
@RequestMapping(value = "/DevTableProperty")
public class DevTablePropertyController {
    @Autowired
    public DevTablePropertyService devTablePropertyService;

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


    //——————————————————————————【Action】————————————————————————————————
    @RequestMapping(value = "/show.do")//这个方法必须写    用于点击框架左侧目录   右侧显示界面
    public ModelAndView show() throws IOException {
        return new ModelAndView("system/sysdic");//jsp相对路径  jsp页面的名字
    }

    @RequestMapping(value = "/query.do")//查询方法 前两个参数是分页参数
    public String query(int page,
                             int rows,
                             String tableLabel,
                             HttpServletResponse response) throws IOException {
        result = devTablePropertyService.query(page, rows, tableLabel);
        return result;
    }

    @RequestMapping(value = "/save.do", method = RequestMethod.POST)//Post方法
    public void save(String id,
                        String tableName,
                        String tableField,
                        String tableType,
                        String tableLabel,
                        String tableProp,
                        String tableWidth,
                        String isForm,
                        String formType,
                        String formSort,
                        String isQuery,
                        String queryType,
                        String querySort,
                        String userId,
                        HttpServletResponse response) throws IOException {
//调用service业务逻辑
        result = devTablePropertyService.save(id,tableName,tableField,tableType,tableLabel,tableProp,
                tableWidth,isForm,formType,formSort,isQuery,queryType,querySort,userId);
        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 = "/remove.do", method = RequestMethod.POST)
    public void remove(String id,
                          String userId,
                          HttpServletResponse response) throws IOException {

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