package com.cku.controller;

import java.io.IOException;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
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.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.cku.core.RESTResponse;
import com.cku.service.CkuImageServiceImpl;
import com.cku.upyun.ImageFtp;
import com.cku.util.ServletUtils;

/**
 *user chaixueteng
 *2016年4月29日
 */
@Controller
@RequestMapping("ckuImage")
public class CkuImageController {
	private static final Logger logger = Logger.getLogger(CkuMatchHotelController.class);
	@Autowired 
	public   CkuImageServiceImpl ckuImageServiceImpl;
	
/*	//修改上传图片
	@RequestMapping("/editImage")
	public void editImage(HttpServletResponse response,HttpServletRequest request,
			@RequestParam(value = "file", required = false) MultipartFile file) throws IOException{
		RESTResponse result = null;
		String fileName = file.getOriginalFilename();
		int lastIndex = fileName.lastIndexOf(".");
		String fileNameEnd = fileName.substring(lastIndex);
		String newFileName = UUID.randomUUID()+fileNameEnd;
		String resultOra = "";
		try {
		    resultOra = ImageFtp.uploadBytes(ImageFtp.AVATAR_PATH_TEST, newFileName, file.getBytes());
			int i = ckuImageServiceImpl.editImage(imageId);
			if (i>0) {
				result = new RESTResponse(0,"修改成功!");
			}else{
				result = new RESTResponse(1,"修改失败!");
			}
		} catch (Exception e) {
			result = new RESTResponse(e);
			logger.error(e);
		}
		ServletUtils.writeResponse(response, result);
	}*/
	//删除图片
	@RequestMapping("/del")
	public void delete(HttpServletResponse response,HttpServletRequest request)throws IOException{
		RESTResponse result = null;
		try {
			Integer imageId = ServletUtils.getParameterInt(request, "id");
			int i=ckuImageServiceImpl.delete(imageId);
			if (i>0) {
				result = new RESTResponse(0,"删除成功!");
			}else{
				result = new RESTResponse(1,"删除失败!");
			}
		} catch (Exception e) {
			result = new RESTResponse(e);
			logger.error(e);
		}
		ServletUtils.writeResponse(response, result);
	}
}
