package com.th.demo.controller.common;

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.common.CommonService;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping(value = "/Common")
public class CommonController {
    @Autowired
    CommonService commonService;
    String result = "";

    @RequestMapping(value = "/queryPrimaryKey.do")
    public String queryPrimaryKey() throws IOException {
        try {
            result = Type.FAIL;
            result = commonService.queryPrimaryKey();
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSON.toJSONString(new ResultJSON(ResponseCodeMsg.CODE_EX, ex.getMessage(), null));
        } finally {
            return result;
        }

    }


    @RequestMapping(value = "/doProcQuery.do")
    public String doProcQuery(String procName,
                                  String param) {

        try {
            result = Type.FAIL;
            result = commonService.doProcQuery(procName,  param);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSON.toJSONString(new ResultJSON(ResponseCodeMsg.CODE_EX, ex.getMessage(), null));
        } finally {
            return result;
        }

    }



    @RequestMapping(value = "/doProcQueryPage.do")
    public String doProcQueryPage(String procName,
                              int page,
                              int rows,
                              String param) {

        try {
            result = Type.FAIL;
            result = commonService.doProcQueryPage(procName, page, rows, param);

        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSON.toJSONString(new ResultJSON(ResponseCodeMsg.CODE_EX, ex.getMessage(), null));
        } finally {
            return result;
        }

    }


    @RequestMapping(value = "/doProc.do", method = RequestMethod.POST)
    public String doProc(String procName,
                         String param) {
        try {
            result = Type.FAIL;
            result = commonService.doProc(procName, param);
            result = JSON.toJSONString(new ResultJSON(ResponseCodeMsg.CODE_OK, ResponseCodeMsg.MSG_OK, result));
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSON.toJSONString(new ResultJSON(ResponseCodeMsg.CODE_EX, ex.getMessage(), null));
        } finally {
            return result;
        }
    }



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

    }


    @RequestMapping(value = "/queryImage.do")
    public String queryImage(String fid) {
        String result = "";
        try {
            result = commonService.queryImage(fid);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSON.toJSONString(new ResultJSON(ResponseCodeMsg.CODE_EX, ex.getMessage(), null));
        } finally {
            return result;
        }

    }

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




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



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

    @RequestMapping(value = "/queryFile.do")
    public String queryFile(String fid) {
        String result = "";
        try {
            result = commonService.queryFile(fid);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSON.toJSONString(new ResultJSON(ResponseCodeMsg.CODE_EX, ex.getMessage(), null));
        } finally {
            return result;
        }
    }


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



    @RequestMapping(value = "/querySelector.do")
    public String querySelector(String type,HttpServletRequest request) {
        String result = "";
        try {
            result = commonService.querySelector(type);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSON.toJSONString(new ResultJSON(ResponseCodeMsg.CODE_EX, ex.getMessage(), null));
        } finally {
            return result;
        }
    }


    @RequestMapping(value = "/downloadByStream.do")
    public void downloadByStream(String id,HttpServletRequest request,HttpServletResponse response) {
        String result = "";
        try {
            commonService.downloadByStream(id,request,response);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}
