package cn.com.capcloud.api;

import cn.com.capcloud.api.action.NoSessionAction;
import cn.com.capcloud.api.action.service.ApiService;
import cn.com.capcloud.ccui.core.common.json.JackJSON;
import cn.com.capcloud.core.util.ReadProperty;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.joda.time.DateTime;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;

@CrossOrigin//支持跨域请求
@RestController
@RequestMapping("/nosession")
public class NoSessionController {

    @Resource
    private NoSessionAction noSessionAction;
    @Resource
    private ReadProperty readProperty;
    @Resource
    private ApiService apiService;

    @RequestMapping("/data/myAction")
    @ResponseBody
    public KHCResponseEntity myAction(@RequestBody String jsonStr, HttpServletRequest request) {
        String retStr= null;
        try {
            retStr = noSessionAction.myAction(jsonStr,request);
        } catch (Exception e) {
            e.printStackTrace();
        }
        KHCResponseEntity KHCResponseEntity = JackJSON.parseObject(retStr, KHCResponseEntity.class);
        return KHCResponseEntity;
    }
    @RequestMapping("/file/downFileYZM")
    public void downFileYZM(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String fileDiskName = "/"+request.getParameter("fileDiskName");
        String suffix = "jpg";

        if (suffix != null && suffix.length() > 1) {
            suffix = suffix.substring(1, suffix.length());
        }
        String fileDir=readProperty.getCaptchaFilePath();
        fileDiskName=fileDiskName.replace("..", "");
        File file = new File(fileDir+fileDiskName);
        FileInputStream inputStream = new FileInputStream(file);
        byte[] data = new byte[(int) file.length()];
        inputStream.read(data);
        inputStream.close();
        apiService.setContentType(response,suffix);
        String downloadFileName = request.getParameter("downFileName");
        if (downloadFileName == null || downloadFileName.length() == 0) {
            downloadFileName = fileDiskName;
        }
        response.addHeader("Content-Disposition",
                "attachment; filename=" + URLEncoder.encode(downloadFileName, "utf-8"));
        OutputStream stream = response.getOutputStream();
        stream.write(data);
        stream.flush();
        stream.close();
    }
    //模板示例：
    /*
    @RequestMapping("/test/ip")
    @ResponseBody
    public ResponseEntity testIp(HttpServletRequest request) {
        String ip= IPUtil.getIpAddress(request);
        Map<String, Object> retMap=apiService.getReturnDataByCode("ok");
        retMap.put("message",ip);
        String retStr=JSONObject.fromObject(retMap).toString();
        ResponseEntity responseEntity= JSON.parseObject(retStr, ResponseEntity.class);
        return responseEntity;
    }*/
}
