package com.palmnest.service.notice.impl;

import com.palmnest.application.bean.Page;
import com.palmnest.dao.notice.WxtemplateMessageDao;
import com.palmnest.model.notice.WxtemplateMessageModel;
import com.palmnest.model.notice.WxtemplateMessageTypeModel;
import com.palmnest.service.impl.GenericManagerImpl;
import com.palmnest.service.notice.WxtemplateMessageManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("wxtemplateMessageManager")
public class WxtemplateMessageManagerImpl extends GenericManagerImpl<WxtemplateMessageModel, Integer> implements WxtemplateMessageManager {

	private WxtemplateMessageDao wxtemplateMessageDao;

	@Autowired
	public void setRoomInfoDao(WxtemplateMessageDao wxtemplateMessageDao) {
		this.wxtemplateMessageDao = wxtemplateMessageDao;
		this.dao = wxtemplateMessageDao;
	}

	@Override
	public Page getDataList(Map<String, Object> map, int pageNo, int pageSize)
			throws Exception {

		return wxtemplateMessageDao.getDataList(map, pageNo, pageSize);
	}

	@Override
	public Page getDataListWithWxPub(int pageNo, int pageSize)
			throws Exception {

		return wxtemplateMessageDao.getDataListWithWxPub(pageNo, pageSize);
	}


	@Override
	public Page getPublicList(String orgIds)
			throws Exception {

		return wxtemplateMessageDao.getPublicList(orgIds);
	}

	@Override
	public String saveOrUpdate(WxtemplateMessageModel wxtemplateMessageModel) {

		String isSuccess = "fail";
		if (wxtemplateMessageModel.getId() == null) {
			WxtemplateMessageModel model = wxtemplateMessageDao.save(wxtemplateMessageModel);
			if (model != null) {
				isSuccess = "success";
			}
		} else {
			try {
				wxtemplateMessageDao.updateObj(wxtemplateMessageModel);
				isSuccess = "success";
			} catch (Exception e) {

				isSuccess = "fail";
			}
		}
		return isSuccess;
	}

	@Override
	public Page getWxPublicno(int id) throws Exception {
		return wxtemplateMessageDao.getWxPublicno(id);
	}


	@Override
	public Page getAllTempByWxPub(int id, int pageNo, int pageSize) throws Exception {

		return wxtemplateMessageDao.getAllTempByWxPub(id, pageNo, pageSize);
	}

	@Override
	public ArrayList<WxtemplateMessageTypeModel> getWxtemplateMessageTypeModelList(int wxId) throws Exception {

		return wxtemplateMessageDao.getWxtemplateMessageTypeModelList(wxId);
	}


	@Override
	public Page getWxPublicByWxTableId(int id) throws Exception {

		return wxtemplateMessageDao.getWxPublicByWxTableId(id);
	}

	@Override
	public Page getWxTableByWxPublicId(int id) throws Exception {

		return wxtemplateMessageDao.getWxTableByWxPublicId(id);
	}


//	@Override
//	public WxtemplateMessageModel getWxtemplateMessageModelByWechat_template_id(int wechat_template_id) throws Exception {
//
//		return wxtemplateMessageDao.getWxtemplateMessageModelByWechat_template_id( wechat_template_id);
//	}
//	

	@Override
	public String sendTemplate(WxtemplateMessageModel wechatTemplateMsg) {

//		
//		WxtemplateMessageModel wechatTemplate = wechatTemplateMapper.selectByPrimaryKey(wechatTemplateMsg.getWechatTemplateId());
//		WechatAccount wechatAccount = wechatAccountMapper.selectByPrimaryKey(wechatTemplate.getWechatAccountId());
//		WxtemplateMessageModel tem=new TemplateMessage();  
//		tem.setTemplateId(wechatTemplate.getTemplateId());  
//		tem.setTopColor("#00DD00");  
//		tem.setUrl(wechatTemplateMsg.getUrl());  
//		String token;
//		boolean flag=false;  
//		String errorMessage = "";
//		try {
//			token = WeiXinHelper.getToken(wechatAccount.getAppId(), wechatAccount.getServerSecret());
//			if(StringUtils.isEmpty(token)){
//				errorMessage = "公众号配置错误或IP未授权";
//				return errorMessage;
//			}
//			List<String> users = WeiXinHelper.getUserList(token);
//			if(users.size() == 0){
//				errorMessage = "公众号关注列表为空，无法推送消息";
//				return errorMessage;
//			}
//			Map<String, Object> queryMap = new HashMap<String, Object>();
//			queryMap.put("wechatTemplateMsgId", wechatTemplateMsg.getId());
//			List<TemplateParam> paras = templateParamMapper.findAll(queryMap);
//			tem.setTemplateParamList(paras);
//			for(String openId : users){
//				tem.setOpenId(openId);
//				//发布消息（设置参数，组建json）
//				JSONObject jsonResult=HttpPostUtil.doHttpPostJson(WeiXinHelper.TEMPLATE_SEND_URL.replace("ACCESS_TOKEN", token),  tem.toJSON());  
//		        if(jsonResult!=null){  
//		            int errorCode=jsonResult.getInteger("errcode");  
//		            if(errorCode==0){  
//		                flag=true;  
//		            }else{  
//		            	errorMessage+=jsonResult.getString("errmsg");  
//		                System.out.println("模板消息发送失败:"+errorCode+","+errorMessage);  
//		                flag=false;  
//		            }  
//		        }  
//			}
//		} catch (Exception e) {
//			errorMessage = "模板消息发送失败";
//			e.printStackTrace();
//		}
//		//设置消息返回状态
//		if(StringUtils.isEmpty(errorMessage)){
//			wechatTemplateMsg.setStatus(Integer.parseInt("2"));
//			wechatTemplateMsgMapper.updateByPrimaryKeySelective(wechatTemplateMsg);
//			return "模板消息发送成功";
//		}
//		else{
//			wechatTemplateMsg.setStatus(Integer.parseInt("3"));
//			wechatTemplateMsgMapper.updateByPrimaryKeySelective(wechatTemplateMsg);
//			return errorMessage;
//		}
//	
		return null;
	}

	@Override
	public Integer getTotal(String orgIds) {
		return wxtemplateMessageDao.getTotal(orgIds);
	}

	@Override
	public List<Map<String, Object>> getUserlistByUserIds(String userIds) {
		return wxtemplateMessageDao.getUserlistByUserIds(userIds);
	}

}
