package com.iamberry.wechat.service.wechat;

import java.util.Date;
import java.util.List;

import net.sf.json.JSONArray;

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

import com.iamberry.wechat.core.entity.wx.MenuInfo;
import com.iamberry.wechat.core.entity.wx.MenuKeySet;
import com.iamberry.wechat.core.entity.wx.ZTreeBean;
import com.iamberry.wechat.face.wechat.MenuInfoService;
import com.iamberry.wechat.service.wechat.dao.MenuInfoDAO;
import com.iamberry.wechat.service.wechat.dao.MenuKeySetDAO;
import com.iamberry.wechat.tools.NameUtils;

@Service
public class MenuInfoServiceImpl implements MenuInfoService {
	@Autowired
	private MenuInfoDAO menuInfoDAO;
	public void setMenuInfoDAO(MenuInfoDAO menuInfoDAO) {
		this.menuInfoDAO = menuInfoDAO;
	}
	@Autowired
	private MenuKeySetDAO menuKeySetDAO;
	public void setMenuKeySetDAO(MenuKeySetDAO menuKeySetDAO) {
		this.menuKeySetDAO = menuKeySetDAO;
	}

	@Override
	public MenuInfo selectOne(String pubNo) {
		return menuInfoDAO.selectOne(pubNo);
	}

	@Override
	public MenuInfo selectByMenuIdAndPubNo(String menuId, String pubNo) {
		return menuInfoDAO.selectByMenuIdAndPubNo(menuId, pubNo);
	}

	@Override
	public int insert(MenuInfo menuInfo) {
		return menuInfoDAO.insert(menuInfo);
	}

	@Override
	public int update(MenuInfo menuInfo) {
		return menuInfoDAO.update(menuInfo);
	}

	@Override
	public int deleteByPubNo(String pubNo) {
		return menuInfoDAO.deleteByPubNo(pubNo);
	}

	@SuppressWarnings("unchecked")
	@Override
	public int saveMenu(String treedata, String str) {
		try {
			boolean flag = true;
			MenuInfo menuInfoVO = menuInfoDAO.selectOne(NameUtils.pubNo);
			if(menuInfoVO != null){
				//全部删除
				if(treedata != null && treedata.length()==53 && treedata.contains("id:0")){
					menuInfoDAO.deleteByPubNo(NameUtils.pubNo);
					menuKeySetDAO.deleteByPubNo(NameUtils.pubNo);
					flag = false;
					treedata = "[{id:0,pid:0,name:'自定义菜单列表',url:'',content:'',type:0}]";
				}else{
					if(!menuInfoVO.getVchar1().equals(treedata)){
						menuKeySetDAO.deleteByPubNo(NameUtils.pubNo);
					}else{//没有任何修改
						flag = false;
					}
				}
			}
			
			if(flag){
				String[] strArr = null;
				if(str != null && !"".equals(str)){
					strArr = str.split("[$]");
				}
				treedata = "[" + treedata.replaceAll("open:true,", "").replaceAll("\r\n", "\\\\r\\\\n") + "]";
				JSONArray jsonArray = JSONArray.fromObject(treedata);
				List<ZTreeBean> treeList = JSONArray.toList(jsonArray, ZTreeBean.class);
				
				int j = 0;
				String pids[] = new String[3];
				for(ZTreeBean zree : treeList){
					if(zree != null && !"0".equals(zree.getId()) && "0".equals(zree.getPid())){
						pids[j] = zree.getId();
						j ++;
					}
				}
				Long orderNo = 0L, idx1 = 0L, idx2 = 5L, idx3 = 10L;
				for(ZTreeBean zree : treeList){
					if(zree != null && !"0".equals(zree.getId())){
						//菜单信息
						MenuInfo mInfoVO = new MenuInfo();
						MenuInfo vo = menuInfoDAO.selectByMenuIdAndPubNo(zree.getId(), NameUtils.pubNo);
						if(vo != null){
							mInfoVO = vo;
						}
						
						if(strArr != null && strArr.length>0){
							for (int i = 0; i < strArr.length; i++) {
								if((zree.getId() != null && !"".equals(zree.getId())) 
										&& (strArr[i] != null && !"".equals(strArr[i]))
										&& "2".equals(zree.getType()) && zree.getId().equals(strArr[i])){
									mInfoVO.setVchar3(strArr[i+1].trim());
								}
								i++;
							}
						}
						//记录子菜单序号
						if(!"0".equals(zree.getPid())){
							String temp = zree.getPid();
							if(pids[0] !=null && temp.equals(pids[0])){
								idx1 ++;
								orderNo = idx1;
							}else if(pids[1] !=null && temp.equals(pids[1])){
								idx2 ++;
								orderNo = idx2;
							}else if(pids[2] !=null && temp.equals(pids[2])){
								idx3 ++;
								orderNo = idx3;
							}
							mInfoVO.setOrderNo(orderNo);
						}
						mInfoVO.setUrl(zree.getUrl());
						mInfoVO.setOpTime(new Date());
						mInfoVO.setWxpublicNo(NameUtils.pubNo);
						mInfoVO.setMenuName(zree.getName());
						mInfoVO.setMenuType(zree.getType());//1为文本、2为图文、3为链接
						mInfoVO.setParentMenuId(zree.getPid()!=null?Long.parseLong(zree.getPid()):null);
						mInfoVO.setVchar1(treedata.replaceAll("pid:0", "pid:0,open:true"));//zTreeNodes字串
						
						//图文修改成其他
						if(!"2".equals(zree.getType()) && mInfoVO.getVchar3() != null && !"".equals(mInfoVO.getVchar3())){
							mInfoVO.setVchar3("");
						}
						//链接修改成其他
						if(!"3".equals(zree.getType()) && mInfoVO.getUrl() != null && !"".equals(mInfoVO.getUrl())){
							mInfoVO.setUrl("");
						}
						
						if(vo != null){
							mInfoVO.setMenuId(vo.getMenuId());
							menuInfoDAO.update(mInfoVO);
						}else{
							mInfoVO.setMenuId(Integer.parseInt(zree.getId()));
							menuInfoDAO.insert(mInfoVO);
						}
						
						//菜单回复内容
						MenuKeySet keySetVO = new MenuKeySet();
						keySetVO.setCreateTime(new Date());
						keySetVO.setKeyWord(zree.getId());
						keySetVO.setReType(zree.getType());
						keySetVO.setMenuId(Long.parseLong(zree.getId()));//据此查询菜单点击事件回复内容
						keySetVO.setWxpublicNo(NameUtils.pubNo);
						if("1".equals(zree.getType())){
							keySetVO.setRefText(zree.getContent());
						}else if("2".equals(zree.getType()) && zree.getContent()!=null && !"".equals(zree.getContent())){
							//zree.getContent()为图文id
							keySetVO.setRefImageTextId(zree.getContent());//据此查询菜单点击跳转图文链接
						}
						menuKeySetDAO.insert(keySetVO);
					}
				}
			}
			return 1;
		} catch (Exception e) {
			return 0;
		}
	}

}
