package com.iamberry.wechat.service.wechat;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iamberry.wechat.core.entity.AdminUtils;
import com.iamberry.wechat.core.entity.PaperBean;
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.service.wechat.dao.ImageTextInfoDAO;
import com.iamberry.wechat.service.wechat.dao.ImageTextMoreDAO;
import com.iamberry.wechat.tools.NameUtils;

@Service
public class ImageTextInfoServiceImpl implements ImageTextInfoService {
	@Autowired
	private AdminUtils adminUtils;
	public void setAdminUtils(AdminUtils adminUtils) {
		this.adminUtils = adminUtils;
	}
	
	@Autowired
	private ImageTextInfoDAO imageTextInfoDao;
	public void setImageTextInfoDao(ImageTextInfoDAO imageTextInfoDao) {
		this.imageTextInfoDao = imageTextInfoDao;
	}
	@Autowired
	private ImageTextMoreDAO imageTextMoreDAO;
	public void setImageTextMoreDAO(ImageTextMoreDAO imageTextMoreDAO) {
		this.imageTextMoreDAO = imageTextMoreDAO;
	}

	@Override
	public int addImageTextInfo(ImageTextInfo imageTextInfo) {
		return imageTextInfoDao.addImageTextInfo(imageTextInfo);
	}

	@Override
	public int delImageTextInfo(Long id) {
		return imageTextInfoDao.delImageTextInfo(id);
	}

	@Override
	public int updateImageTextInfo(ImageTextInfo imageTextInfo) {
		return imageTextInfoDao.updateImageTextInfo(imageTextInfo);
	}

	@Override
	public ImageTextInfo findImageTextInfoById(Long id) {
		return imageTextInfoDao.findImageTextInfoById(id);
	}

	@Override
	public List<ImageTextInfo> findImageTextInfoBywxPublicNo(String wxPublicNo) {
		return imageTextInfoDao.findImageTextInfoBywxPublicNo(wxPublicNo);
	}

	@Override
	public List<Map<Object, Object>> findImageTextInfo(String wxPublicNo,
			Integer pageNo, Integer selectNum) {
		List<Map<Object, Object>> imageTextList = new ArrayList<Map<Object, Object>>();
		List<ImageTextInfo> list = imageTextInfoDao.findImageTextInfo(wxPublicNo, pageNo, selectNum);
		for (ImageTextInfo info : list) {
			Map<Object, Object> tempMap = new HashMap<Object, Object>();
			tempMap.put("title", info.getTitle());
			tempMap.put("digest", info.getDigest());
			tempMap.put("imageurl", info.getImageUrl());
			tempMap.put("sourceurl", info.getSourceUrl());
			tempMap.put("imagetextno", info.getImageTextNo());
			tempMap.put("clickouturl", info.getClickOutUrl());
			tempMap.put("imagetexttype", info.getImageTextType());
			tempMap.put("createtime", info.getCreateTime());
			//子图文
			if("2".equals(info.getImageTextType())){
				tempMap.put("subVoList", imageTextMoreDAO.findImageTextmore(info.getImageTextNo()));
			}
			imageTextList.add(tempMap);
		}
		return imageTextList;
	}

	@Override
	public int findImageTextInfoNo(String wxPublicNo, Integer pageSize) {
		int count = imageTextInfoDao.findImageTextInfoNo(wxPublicNo);
		count = count % pageSize == 0? count/pageSize:(count/pageSize)+1;
		return count;
	}
	
	public List<Map<Object, Object>> selectImageInfoList(String wxPublicNo, Integer pageNO, Integer pageSize) throws Exception {
		PaperBean bean = new PaperBean();
		bean.setMinNum((pageNO-1)*pageSize);
		bean.setMaxNum(pageSize);
		bean.setStr(wxPublicNo);
		List<Map<Object, Object>> imageTextList = new ArrayList<Map<Object, Object>>();
		List<ImageTextInfo> list = imageTextInfoDao.selectImageTextInfoList(bean);
		for (ImageTextInfo info : list) {
			Map<Object, Object> tempMap = new HashMap<Object, Object>();
			tempMap.put("imageTextMD5", adminUtils.encryptObject(info.getImageTextNo(), "imageTextNo"));//主图文的ID加密
			tempMap.put("title", info.getTitle());
			tempMap.put("digest", info.getDigest());
			tempMap.put("imageurl", info.getImageUrl());
			tempMap.put("sourceurl", info.getSourceUrl());
			tempMap.put("imagetextno", info.getImageTextNo());
			tempMap.put("clickouturl", info.getClickOutUrl());
			tempMap.put("imagetexttype", info.getImageTextType());
			tempMap.put("createtime", info.getCreateTime());
			//子图文
			if("2".equals(info.getImageTextType())){
				List<ImageTextMore> lists = imageTextMoreDAO.findImageTextmore(info.getImageTextNo());
				List<ImageTextMore> imageTextMores = new ArrayList<ImageTextMore>();
				for(ImageTextMore imageTextMore : lists) {
					//多图文的ID加密
					String md5MoreImageTextNo = adminUtils.encryptObject(imageTextMore.getMoreImageTextNo(), "moreImageTextNo");
					imageTextMore.setMoreImageTextMD5(md5MoreImageTextNo);
					imageTextMores.add(imageTextMore);
				}
				tempMap.put("subVoList", imageTextMores);
			}
			imageTextList.add(tempMap);
		}
		return imageTextList;
	}
	
	@Override
	public void addImageTextMore(String jsonData) {
		ImageTextInfo mainVo = new ImageTextInfo();
		List<ImageTextMore> subVoList = new ArrayList<ImageTextMore>();
		Date now = new Date();
		// 解析jsonData
		JSONArray array = JSONArray.fromObject(jsonData);
		for (int i = 0; i < array.size(); i++) {
			JSONObject obj = array.getJSONObject(i);
			if (i == 0) { // 主图文
				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"));
				mainVo.setWxpublicNo(NameUtils.pubNo);
			} else {      // 子图文
				ImageTextMore subVo = new ImageTextMore();
				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"));
				subVo.setWxpublicNo(NameUtils.pubNo);
				subVoList.add(subVo);
			}
		}
		// 创建多图文，先创建主图文再创建子图文
		imageTextInfoDao.addImageTextInfo(mainVo);
		for (ImageTextMore subVo : subVoList) {
			subVo.setImageTextNo(mainVo.getImageTextNo());
			imageTextMoreDAO.addImageTextMore(subVo);
		}
	}

	@Override
	public Map<Object, Object> findImageTextMore(Long imageTextNo) throws Exception {
		ImageTextInfo info = imageTextInfoDao.findImageTextInfoById(imageTextNo);
		if(info == null) {
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Map<Object, Object> tempMap = new HashMap<Object, Object>();
		tempMap.put("title", info.getTitle());
		tempMap.put("content", info.getMainText());
		tempMap.put("imageurl", info.getImageUrl());
		tempMap.put("sourceurl", info.getSourceUrl());
		tempMap.put("imagetextno", adminUtils.encryptObject(info.getImageTextNo(), "mainImageTextNO"));
		tempMap.put("clickouturl", info.getClickOutUrl());
		tempMap.put("imagetexttype", info.getImageTextType());
		tempMap.put("createtime", sdf.format(info.getCreateTime()));
		//子图文
		if("2".equals(info.getImageTextType())){
			List<ImageTextMore> lists = imageTextMoreDAO.findImageTextmore(info.getImageTextNo());
			//为ID加密
			List<ImageTextMore> arrayLists = new ArrayList<>();
			for(ImageTextMore imageTextMore : lists) {
				String moreImageTextNO = adminUtils.encryptObject(imageTextMore.getMoreImageTextNo(), "moreImageTextNO");
				imageTextMore.setMoreImageTextMD5(moreImageTextNO);
				arrayLists.add(imageTextMore);
			}
			tempMap.put("subVoList", arrayLists);
		}
		return tempMap;
	}

	@Override
	public void doUpdateMore(ImageTextInfo mainVo,
			List<ImageTextMore> subVoList, List<String> delVoList) {
		imageTextInfoDao.updateImageTextInfo(mainVo);
		for(ImageTextMore subVo : subVoList) {
			if(subVo !=null && subVo.getMoreImageTextNo() != null) {
				imageTextMoreDAO.updateImageTextMore(subVo);
			} else {
				subVo.setImageTextNo(mainVo.getImageTextNo());
				imageTextMoreDAO.addImageTextMore(subVo);
			}
		}
		for (String rid : delVoList) {
			if(rid != null && !"".equals(rid)) {
				rid = adminUtils.decryptObject(rid, "moreImageTextNO");//删除ID的解密
				ImageTextMore ImageTextMore = imageTextMoreDAO.getImageTextmore(Long.parseLong(rid));
				if(ImageTextMore !=null)imageTextMoreDAO.delImageTextMore(ImageTextMore.getMoreImageTextNo());
			}
		}
	}

	@Override
	public List<Map<Object, Object>> selectAllImageTextList(String publicWxNo) throws Exception {
		List<Map<Object, Object>> imageTextList = new ArrayList<Map<Object, Object>>();
		List<ImageTextInfo> list = imageTextInfoDao.selectAllImageTextList(publicWxNo);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		for (ImageTextInfo info : list) {
			Map<Object, Object> tempMap = new HashMap<Object, Object>();
			tempMap.put("imageTextMD5", adminUtils.encryptObject(info.getImageTextNo(), "imageTextNo"));//主图文的ID加密
			tempMap.put("title", info.getTitle());
			tempMap.put("digest", info.getDigest());
			tempMap.put("imageurl", info.getImageUrl());
			tempMap.put("sourceurl", info.getSourceUrl());
			tempMap.put("imagetextno", info.getImageTextNo());
			tempMap.put("clickouturl", info.getClickOutUrl());
			tempMap.put("imagetexttype", info.getImageTextType());
			tempMap.put("createtime", sdf.format(info.getCreateTime()));
			//子图文
			if("2".equals(info.getImageTextType())){
				List<ImageTextMore> lists = imageTextMoreDAO.findImageTextmore(info.getImageTextNo());
				List<ImageTextMore> imageTextMores = new ArrayList<ImageTextMore>();
				for(ImageTextMore imageTextMore : lists) {
					//多图文的ID加密
					String md5MoreImageTextNo = adminUtils.encryptObject(imageTextMore.getMoreImageTextNo(), "moreImageTextNo");
					imageTextMore.setMoreImageTextMD5(md5MoreImageTextNo);
					imageTextMores.add(imageTextMore);
				}
				tempMap.put("subVoList", imageTextMores);
			} else {
				tempMap.put("subVoList", "");
			}
			imageTextList.add(tempMap);
		}
		return imageTextList;
	}
	
	/**
	 * 扫描素材是否在其他表中使用
	 */
	public Integer selectImageTextOtherTableCount(Integer imageTextId) {
		Integer fCount = imageTextInfoDao.selectFirstAnswerByID(imageTextId);
		Integer kCount = imageTextInfoDao.selectKeyWordByID(imageTextId);
		Integer mCount = imageTextInfoDao.selectMenuKeyByID(imageTextId);
		return fCount + kCount + mCount;
	}

}
