package com.iamberry.wechat.handles.wechat.imagetext;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

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

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.iamberry.app.tool.log.RatFWLogger;
import com.iamberry.wechat.core.entity.AdminUtils;
import com.iamberry.wechat.core.entity.wx.ImageTextInfo;
import com.iamberry.wechat.core.entity.wx.ImageTextMore;
import com.iamberry.wechat.face.wechat.ImageTextInfoService;
import com.iamberry.wechat.face.wechat.ImageTextMoreService;
import com.iamberry.wechat.tools.NameUtils;
/**
 * @company	深圳爱贝源科技有限公司
 * @website www.iamberry.com
 * @author 	献
 * @tel		18271840547
 * @date	2016年11月3日
 * @explain	微信后台管理-单图文
 */
@Controller
@RequestMapping("/admin/wx")
public class ImageTextInfoHandler {
	@Autowired
	private AdminUtils adminUtils;
	public void setAdminUtils(AdminUtils adminUtils) {
		this.adminUtils = adminUtils;
	}
	
	@Autowired
	private ImageTextInfoService imageTextInfoService;
	public void setImageTextInfoService(ImageTextInfoService imageTextInfoService) {
		this.imageTextInfoService = imageTextInfoService;
	}
	@Autowired
	private ImageTextMoreService imageTextMoreService;
	public void setImageTextMoreService(ImageTextMoreService imageTextMoreService) {
		this.imageTextMoreService = imageTextMoreService;
	}
	@Autowired
	private RatFWLogger ratFWLogger;
	public void setRatFWLogger(RatFWLogger ratFWLogger) {
		this.ratFWLogger = ratFWLogger;
	}
	/**
	 * 创建单图文页面
	 * @return
	 */
	@RequestMapping("/showOneImageTextEdit")
	public ModelAndView imginfoUI(HttpSession session, HttpServletRequest request){
		ModelAndView mav = new ModelAndView();
		//创建单图文表单token
		String oneImageTextToken = adminUtils.byte2hex(adminUtils.createRandomByte());
		session.setAttribute("oneImageTextFormToken", oneImageTextToken);
		mav.addObject("token", oneImageTextToken);
		mav.addObject("operationType", "add");//操作类型为：添加单图文操作
		mav.addObject("currDate", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));//单图文创建日期
		mav.setViewName("admin/wx/imagetext/oneimagetext");
		return mav;
	}
	
	/**
	 * 添加、修改单图文
	 */
	@RequestMapping("/addOrUpdateOneImageText/{operateType}/{token}")
	@ResponseBody
	public JSONObject addOrUpdateOneImageText(ImageTextInfo imageTextInfo,@PathVariable(value="operateType") String operateType, @PathVariable(value="token") String token, HttpSession session, HttpServletRequest request) {
		String errInfo = "";
		String sessionToken = (String) session.getAttribute("oneImageTextFormToken");
		session.removeAttribute("oneImageTextFormToken");
		if(token != null && sessionToken != null && sessionToken.equals(token)) {
			ratFWLogger.info("token表单验证成功！");
			//formToken 验证成功
			if(imageTextInfo != null && adminUtils.parseFieldLength(imageTextInfo.getTitle(), 1, 64) && 
					imageTextInfo.getImageUrl() != null && 
					(adminUtils.parseFieldLength(imageTextInfo.getMainText(), 1, 10000) || imageTextInfo.getClickOutUrl() != null)) {
				//验证成功
				if(operateType.equals("add")) {
					//添加操作
					ImageTextInfo vo = new ImageTextInfo();
					vo.setTitle(imageTextInfo.getTitle());
					vo.setDigest(imageTextInfo.getDigest());
					vo.setImageTextType("1");//1、单图文类型， 2、多图文类型 
					vo.setImageUrl(imageTextInfo.getImageUrl());
					vo.setClickOutUrl(imageTextInfo.getClickOutUrl());
					vo.setMainText(imageTextInfo.getMainText());
					vo.setSourceUrl(imageTextInfo.getSourceUrl());
					vo.setCreateTime(new Date());
					vo.setWxpublicNo(NameUtils.pubNo);
					imageTextInfoService.addImageTextInfo(vo);
					ratFWLogger.info("添加成功");
					errInfo = "{'msg':'success'}";
					
				} else if(operateType.equals("update")) {
					if(imageTextInfo.getImageTextMD5() != null) {
						String imageTextID = adminUtils.decryptObject(imageTextInfo.getImageTextMD5(), "imageTextNO");
						//修改操作
						ImageTextInfo vo = imageTextInfoService.findImageTextInfoById(imageTextID!=null?Long.parseLong(imageTextID):0);
						if (vo != null) {
							vo.setTitle(imageTextInfo.getTitle());
							vo.setDigest(imageTextInfo.getDigest());
							vo.setImageUrl(imageTextInfo.getImageUrl());
							vo.setClickOutUrl(imageTextInfo.getClickOutUrl());
							vo.setMainText(imageTextInfo.getMainText());
							vo.setSourceUrl(imageTextInfo.getSourceUrl());
							imageTextInfoService.updateImageTextInfo(vo);
							ratFWLogger.info("图文修改成功！");
							errInfo = "{'msg':'success'}";
						} else {
							//修改失败
							errInfo = "{'msg':'修改失败，刷新后重试'}";
						}
					}
				} else {
					//操作类型有误，可能：恶意请求
					ratFWLogger.info("操作有误！");
					errInfo = "{'msg':'操作流程有误，请刷新后重新操作!'}";
				}
			} else {
				//数据验证失败
				ratFWLogger.info("数据验证失败！");
				errInfo = "{'msg':'数据校验失败!刷新后重试'}";
			}
		} else {
			//系统繁忙，请勿重复提交
			ratFWLogger.info("token表单验证失败！");
			errInfo = "{'msg':'token表单验证失败！刷新后重试'}";
		}
		JSONObject json = JSONObject.fromObject(errInfo);
		return json;
	}
	
	
	/**
	 * 图文素材列表
	 * @throws Exception 
	 */
	@RequestMapping("/showMaterialList")
	public ModelAndView showMaterialList(@RequestParam(value="pageNO", defaultValue="1") Integer pageNO, HttpSession session) throws Exception {
		ModelAndView mv = new ModelAndView("admin/wx/material/materiallist");
		int pageSize = 3;//每页显示的数量
		List<Map<Object, Object>> resultLists = imageTextInfoService.selectImageInfoList(NameUtils.pubNo, pageNO, pageSize);
		String oneImageTextToken = adminUtils.byte2hex(adminUtils.createRandomByte());
		session.setAttribute("delTextToken", oneImageTextToken);
		mv.addObject("token", oneImageTextToken);
		mv.addObject("resultLists", resultLists);
		mv.addObject("pageNO", pageNO);
		mv.addObject("pageSize", imageTextInfoService.findImageTextInfoNo(NameUtils.pubNo, pageSize));
		mv.addObject("url", "/admin/wx/showMaterialList?pageNO=");
		return mv;
	}
	
	
	/**
	 * 进入多图文页面
	 * @throws Exception 
	 */
	@RequestMapping("/showMoreImageTextEdit")
	public ModelAndView showMoreImageTextEdit(HttpSession session) throws Exception {
		ModelAndView mv = new ModelAndView("admin/wx/imagetext/moreimagetext");
		//创建单图文表单token
		String oneImageTextToken = adminUtils.byte2hex(adminUtils.createRandomByte());
		session.setAttribute("oneImageTextFormToken", oneImageTextToken);
		mv.addObject("token", oneImageTextToken);
		mv.addObject("currDate", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
		return mv;
	}
	
	
	/**
	 * 创建多图文
	 * @throws Exception
	 */
	@RequestMapping("/addOrUpdateMoreImageText/{token}")
	@ResponseBody
	public JSONObject addOrUpdateImageMoreAjax(HttpServletRequest request,
											HttpSession session,
											@PathVariable(value="token") String token) throws Exception {
		String errInfo = "";
		String oneImageTextFormToken = (String) session.getAttribute("oneImageTextFormToken");
		session.removeAttribute("oneImageTextFormToken");
		if(token != null && oneImageTextFormToken != null && oneImageTextFormToken.equals(token)) {
			String jsonData = request.getParameter("jsonData");
			if(validateImageTextInfo(jsonData)) {
					imageTextInfoService.addImageTextMore(jsonData);
					errInfo = "{'msg':'success'}";
			} else {
				//校验不通过
				errInfo = "{'msg':'数据校验失败，请刷新后重新添加！'}";
			}
		} else {
			//token验证失败
			errInfo = "{'msg':'操作有误，请刷新后重试!'}";
		}
		JSONObject json = JSONObject.fromObject(errInfo);
		return json;
	}

	
	/**
	 * 删除多图文
	 *    根据单图文ID和多图文主图文ID
	 */
	@RequestMapping("/deleteImageTextByMainId/{imageTextNo}/{token}")
	@ResponseBody
	public JSONObject deleteImageTextByMainId(@PathVariable(value="imageTextNo") String imageTextId,
												@PathVariable(value="token") String token,
												HttpSession session) {
		String errInfo = null;
		String sessionToken = (String) session.getAttribute("delTextToken");
		session.removeAttribute("delTextToken");
		if(token != null && sessionToken != null && sessionToken.equals(token)) {
			if(imageTextId != null) {
				//解析加密ID
				 imageTextId = adminUtils.decryptObject(imageTextId, "imageTextNo");
				 if(imageTextId != null) {
					 ImageTextInfo imageTextInfo = imageTextInfoService.findImageTextInfoById(Long.parseLong(imageTextId));
					 if(imageTextInfo != null){
						if(imageTextInfoService.selectImageTextOtherTableCount(Integer.parseInt(imageTextId)) == 0) {
							imageTextInfoService.delImageTextInfo(imageTextInfo.getImageTextNo());
							//根据朱图文ID,删除附属图文
							List<ImageTextMore> imageTextMoreList = imageTextMoreService.findImageTextmore(imageTextInfo.getImageTextNo());
							if(imageTextMoreList != null && imageTextMoreList.size() > 0) {
								imageTextMoreService.delImageTextMoreByImageNo(imageTextInfo.getImageTextNo());
							}
							errInfo = "{'msg':'删除成功!'}";
						} else {
							errInfo = "{'msg':'该素材正在被使用，禁止删除!'}";
						}
					 } else {
						errInfo = "{'msg':'删除失败!'}";
					 }
				 } else {
					 errInfo = "{'msg':'删除失败!'}";
				 }
			} else {
				errInfo = "{'msg':'删除失败!'}";
			}
		} else {
			//token 验证失败
			errInfo = "{'msg':'操作流程有误,请刷新后重新操作！'}";
		}
		JSONObject json = JSONObject.fromObject(errInfo);
		return json;
		
	}
	
	/**
	 * 进入单图文或多图文修改页面
	 * @throws Exception 
	 */
	@RequestMapping("/toOneImageTextOrMoreImageText/{imageTextID}")
	public ModelAndView toOneImageTextOrMoreImageText(@PathVariable("imageTextID") String imageTextId, HttpSession session) throws Exception {
		ModelAndView mv = new ModelAndView();
		String oneImageTextFormToken = adminUtils.byte2hex(adminUtils.createRandomByte());
		session.setAttribute("oneImageTextFormToken", oneImageTextFormToken);
		if(imageTextId != null) {
			imageTextId = adminUtils.decryptObject(imageTextId, "imageTextNo");
			if(imageTextId != null) {
				ImageTextInfo imageTextInfo = imageTextInfoService.findImageTextInfoById(Long.parseLong(imageTextId));
				if(imageTextInfo != null) {
					if("1".equals(imageTextInfo.getImageTextType())) {
						imageTextInfo.setImageTextMD5(adminUtils.encryptObject(Long.parseLong(imageTextId), "imageTextNO"));
						//单图文
						mv.addObject("imageTextInfo", imageTextInfo);
						mv.addObject("token", oneImageTextFormToken);
						mv.addObject("operationType", "update");//操作类型为：添加单图文操作
						mv.setViewName("admin/wx/imagetext/editoneimagetext");
					} else if("2".equals(imageTextInfo.getImageTextType())) {
						//多图文
						Map<Object, Object> imgtxtMore = imageTextInfoService.findImageTextMore(Long.parseLong(imageTextId));
						mv.addObject("imgtxtMore", imgtxtMore);
						mv.addObject("token", oneImageTextFormToken);
						mv.setViewName("admin/wx/imagetext/editmoreimagetext");
					}
				}
			}
		}
		return mv;
	}
	
	/**
	 * 修改多图文
	 */
	@RequestMapping("/updateMoreImageText/{token}")
	@ResponseBody
	public JSONObject updateMoreImageText(@PathVariable("token") String token, HttpSession session, HttpServletRequest request) {
		String errInfo = "";
		String sessionToken = (String) session.getAttribute("oneImageTextFormToken");
		session.removeAttribute("oneImageTextFormToken");
		if(token != null && sessionToken != null && token.equals(sessionToken)) {
			String jsonData = request.getParameter("jsonData");
			String jsonDelData = request.getParameter("jsonDelData");
			List<ImageTextMore> subVoList = new ArrayList<ImageTextMore>();
			Date now = new Date();
			try {
				ImageTextInfo mainVo = new ImageTextInfo();
				JSONArray array = JSONArray.fromObject(jsonData);
				for (int i = 0; array !=null && i < array.size(); i++) {
					JSONObject obj = array.getJSONObject(i);
					if(obj != null){
						if (i == 0) { // 主图文
							String rid = obj.getString("rid"); 
							if(rid != null && !"".equals(rid.trim())) {
								rid = adminUtils.decryptObject(rid, "mainImageTextNO");//主图文ID的解密
								mainVo = imageTextInfoService.findImageTextInfoById(Long.parseLong(rid));
							}else{
								mainVo.setWxpublicNo(NameUtils.pubNo);
							}
							//修改或新增
							mainVo.setCreateTime(now);
							mainVo.setImageTextType("2");
							mainVo.setTitle(obj.getString("title"));
							mainVo.setImageUrl(obj.getString("cover"));
							mainVo.setMainText(obj.getString("content"));
							mainVo.setClickOutUrl(obj.getString("chain"));
							mainVo.setSourceUrl(obj.getString("sourceurl"));
						} else {      // 子图文
							String rid = obj.getString("rid");
							ImageTextMore subVo = new ImageTextMore();
							if(rid != null && !"".equals(rid.trim())) {
								rid = adminUtils.decryptObject(rid, "moreImageTextNO");//子图文ID的解密
								subVo = imageTextMoreService.getImageTextmore(Long.parseLong(rid));
								if(subVo == null) {
									subVo.setWxpublicNo(NameUtils.pubNo);
								}
							}else{
								subVo.setWxpublicNo(NameUtils.pubNo);
							}
//System.out.println(subVo);
							//修改或新增
							subVo.setTitle(obj.getString("title"));
							subVo.setImageUrl(obj.getString("cover"));
							subVo.setMainText(obj.getString("content"));
							subVo.setClickOutUrl(obj.getString("chain"));
							subVo.setSourceUrl(obj.getString("sourceurl"));
							subVo.setCreateTime(now);
							subVoList.add(subVo);
						}
					}
				}
				List<String> delVoList = new ArrayList<String>();
				JSONArray delArray = JSONArray.fromObject(jsonDelData);
				delVoList = JSONArray.toList(delArray);
				// delResId
				imageTextInfoService.doUpdateMore(mainVo, subVoList, delVoList);
				errInfo = "{'msg':'success'}";
			} catch (Exception e) {
				errInfo = "{'msg':'修改失败!, 请刷新后重试！'}";
				ratFWLogger.info("修改时出现异常");
				e.printStackTrace();
			}
			
		} else {
			//token验证失败
			errInfo = "{'msg':'修改失败， 请刷新后重试！'}";
			ratFWLogger.info("token验证失败");
		}
		JSONObject json = JSONObject.fromObject(errInfo);
		return json;
	}
	
	/**
	 * 验证多图文的方法
	 */
	public boolean validateImageTextInfo(String jsonData) {
		if(jsonData != null && !"".equals(jsonData)) {
			JSONArray array = JSONArray.fromObject(jsonData);
			ratFWLogger.info("添加多图文的JSON数据:"+array);
			int mainMsg = 1;
			int msg = 1;
			if(array.size() >= 2 && array.size() <=8) {
				for(int i = 0; i < array.size(); i++) {
					JSONObject jsonObject = array.getJSONObject(i);
//System.out.println(jsonObject);
					if(i == 0) {
						//主素材的验证
						String title = jsonObject.getString("title");
						String cover = jsonObject.getString("cover");
						String content = jsonObject.getString("content");
						String chain = jsonObject.getString("chain");
						if(adminUtils.parseFieldLength(title, 1, 64) && cover !=null && (adminUtils.parseFieldLength(content, 1, 10000) || chain != null) ) {
							mainMsg = 3;
							ratFWLogger.info("主图文的信息验证通过!");
						} else {
							//提交的主图文信息不完善
							mainMsg = 2;
							ratFWLogger.info("多图文添加过程中，主图文的信息验证不通过!");
						}
					} else {
						//附属素材的验证
						//主素材的验证
						String title = jsonObject.getString("title");
						String cover = jsonObject.getString("cover");
						String content = jsonObject.getString("content");
						String chain = jsonObject.getString("chain");
						if(adminUtils.parseFieldLength(title, 1, 64) && cover !=null && (adminUtils.parseFieldLength(content, 1, 10000) || chain != null) ) {
							msg = 3;
							ratFWLogger.info("所有的副图文的信息验证通过!");
						} else {
							//提交的副图文信息不完善
							msg = 2;
							ratFWLogger.info("多图文添加过程中，某个副图文的信息验证不通过!");
						}
					}
				}
			} else {
				//最少添加两个图文素材
				ratFWLogger.info("添加多图文的个数不对");
				return false;
			}
			if(msg == 3 && mainMsg == 3) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
	
	
	/*一下代码都是以前的代码，一般删除没影响，有些代码可用，所以留着*/
	
	
	/**
	 * 查看详细页面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/detail")
	public ModelAndView doDetail(HttpServletRequest request) throws Exception {
		String id = request.getParameter("id");
		String type = request.getParameter("type");
		ModelAndView mav = new ModelAndView();
		if(id != null && !"".equals(id.trim())) {
			ImageTextInfo imgtxtinfo = null;
			if("1".equals(type)) {
				imgtxtinfo = imageTextInfoService.findImageTextInfoById(Long.parseLong(id));
			} else {
				ImageTextMore imageTextMore = imageTextMoreService.getImageTextmore(Long.parseLong(id));
				if(imageTextMore != null){
					imgtxtinfo = new ImageTextInfo();
					imgtxtinfo.setTitle(imageTextMore.getTitle());
					imgtxtinfo.setDigest(imageTextMore.getDigest());
					imgtxtinfo.setClickUrl(imageTextMore.getClickUrl());
					imgtxtinfo.setImageUrl(imageTextMore.getImageUrl());
					imgtxtinfo.setMainText(imageTextMore.getMainText());
					imgtxtinfo.setSourceUrl(imageTextMore.getSourceUrl());
					imgtxtinfo.setCreateTime(imageTextMore.getCreateTime());
					imgtxtinfo.setClickOutUrl(imageTextMore.getClickOutUrl());
				}
			}
			mav.addObject("imgtxtinfo", imgtxtinfo);
		}
		mav.setViewName("forward:/main/material/imagetextinfo/detail.jsp");
		return mav;
	}
	
	/**
	 * 修改多图文
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public void updateImageMore(HttpServletRequest request,HttpServletResponse response) throws Exception {
		// 解析jsonData
		String jsonData = request.getParameter("jsonData");
		String delResId = request.getParameter("delResId");//删除图文的id
		List<ImageTextMore> subVoList = new ArrayList<ImageTextMore>();
		Date now = new Date();
		try {
			ImageTextInfo mainVo = new ImageTextInfo();
			JSONArray array = JSONArray.fromObject(jsonData);
			for (int i = 0; array !=null && i < array.size(); i++) {
				JSONObject obj = array.getJSONObject(i);
				if(obj != null){
					if (i == 0) { // 主图文
						String rid = obj.getString("rid"); 
						if(rid != null && !"".equals(rid.trim())) {
							mainVo = imageTextInfoService.findImageTextInfoById(Long.parseLong(rid));
						}else{
							mainVo.setWxpublicNo(NameUtils.pubNo);
						}
						//修改或新增
						mainVo.setCreateTime(now);
						mainVo.setImageTextType("2");
						mainVo.setTitle(obj.getString("title"));
						mainVo.setImageUrl(obj.getString("cover"));
						mainVo.setMainText(obj.getString("content"));
						mainVo.setClickOutUrl(obj.getString("chain"));
						mainVo.setSourceUrl(obj.getString("sourceurl"));
					} else {      // 子图文
						String rid = obj.getString("rid");
						ImageTextMore subVo = new ImageTextMore();
						if(rid != null && !"".equals(rid.trim())) {
							subVo = imageTextMoreService.getImageTextmore(Long.parseLong(rid));
						}else{
							subVo.setWxpublicNo(NameUtils.pubNo);
						}
						//修改或新增
						subVo.setCreateTime(now);
						subVo.setTitle(obj.getString("title"));
						subVo.setImageUrl(obj.getString("cover"));
						subVo.setMainText(obj.getString("content"));
						subVo.setClickOutUrl(obj.getString("chain"));
						subVo.setSourceUrl(obj.getString("sourceurl"));
						subVoList.add(subVo);
					}
				}
			}
			List<String> delVoList = new ArrayList<String>();
			JSONArray delArray = JSONArray.fromObject(delResId);
			delVoList = JSONArray.toList(delArray);
			// delResId
			imageTextInfoService.doUpdateMore(mainVo, subVoList, delVoList);
			response.getWriter().write("true");
			response.getWriter().close();
		} catch (JSONException e) {// 抛错 说明JSON字符不是数组或根本就不是JSON
			try {
				e.printStackTrace();
			} catch (JSONException e2) {// 抛错 说明JSON字符根本就不是JSON
				System.out.println("非法的JSON字符串");
			}
		}
	}
	
	/**
	 * 图文预览
	 * @throws Exception
	 */
	@RequestMapping("/preview")
	public void doPreViewAjax(HttpServletRequest request,HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		if (id != null && !"".equals(id.trim())) {
			Map<Object, Object> tempMap = imageTextInfoService.findImageTextMore(Long.parseLong(id));
			JSONObject json = new JSONObject();
			json.put("success", true);
			json.put("tempMap", tempMap);
			response.setCharacterEncoding("UTF-8");
			response.getWriter().write(json.toString());
//System.out.println(json.toString());
			response.getWriter().close();
		}
	}
	
	/**
	 * 选择图文消息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/sltlist")
	public ModelAndView doSelectList(HttpServletRequest request) throws Exception {
		String start = request.getParameter("start"); // 分页起始
		String limit = request.getParameter("limit"); // 每页数量，适用mysql
		if (start == null || "".equals(start.trim())) start = "0";
		if (limit == null || "".equals(limit.trim())) limit = "10";
		List<Map<Object, Object>> resultList = imageTextInfoService
				.findImageTextInfo(NameUtils.pubNo, Integer.parseInt(start),
						Integer.parseInt(limit));
		ModelAndView mav = new ModelAndView();
		mav.addObject("start", start);
		mav.addObject("totalCount", resultList.size());
		mav.addObject("resultList", resultList);
		mav.setViewName("admin/wx/imagetext/article");
		return mav;
	}
	
	/**
	 *  删除图文
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/delimgtxt")
	public void doRemoveAjax(HttpServletRequest request,HttpServletResponse response) throws Exception {
		String rid = request.getParameter("rid");
		if (rid != null && !"".equals(rid.trim())) {
			ImageTextInfo imageTextInfo = imageTextInfoService.findImageTextInfoById(Long.parseLong(rid));
			if(imageTextInfo != null){
				imageTextInfoService.delImageTextInfo(imageTextInfo.getImageTextNo());
				response.getWriter().write("true");
			}
		}else{
			response.getWriter().write("false");
		}
		response.getWriter().close();
	}
	
	/**
	 * 保存单图文素材
	 * @throws IOException
	 */
	@RequestMapping(value="/saveOneImageTextInfo")
	public void saveOneImageTextInfo(HttpServletRequest request, BindException errors) throws IOException {
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		MultipartFile file = multipartRequest.getFile("file");
		//获取文件名
		String realFileName = file.getOriginalFilename();
		//获取文件保存路径
		String ctxPath = request.getSession().getServletContext().getRealPath("/")+"images\\";
		//创建文件文件夹
		File dirPath = new File(ctxPath);
		if(!dirPath.exists()) {
			dirPath.mkdir();
		}
		File uploadFile = new File(ctxPath + realFileName);
		FileCopyUtils.copy(file.getBytes(), uploadFile);
	}
	
}
