package com.palmnest.service.estateserve.airconditioner.impl;

import cn.superdesk.libs.common.json.JsonUtils;

import com.palmnest.application.bean.Page;
import com.palmnest.application.core.util.PropertiesKit;
import com.palmnest.dao.airconditioner.ConditionerAddTimeDao;
import com.palmnest.dao.wechat.ReplayContentDao;
import com.palmnest.model.airconditioner.AirConditionerAddTime;
import com.palmnest.model.estateserve.WxUser;
import com.palmnest.model.release.GoodsRelease;
import com.palmnest.service.estateserve.ScWxuserManager;
import com.palmnest.service.estateserve.airconditioner.ConditionerAddTimeManager;
import com.palmnest.service.impl.GenericManagerImpl;
import com.palmnest.service.wechat.WeChatApiManager;

import lombok.extern.slf4j.Slf4j;
import net.sf.json.util.JSONUtils;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Slf4j
@Service("ConditionerAddTimeManager")
public class ConditionerAddTimeManagerImpl extends GenericManagerImpl<AirConditionerAddTime, Long> implements ConditionerAddTimeManager {


	public static String host                  = PropertiesKit.readPropertiesValue("hostUrl", "wechat.properties");
	//审批结果
	public static String auditResultTempleteId = PropertiesKit.readPropertiesValue("auditResultTempleteId", "wechat.properties");
	public static String wxAirApplyDetailUrl   = PropertiesKit.readPropertiesValue("wxAirApplyDetailUrl", "wechat.properties");


	private ConditionerAddTimeDao conditionerAddTimeDao;

	@Autowired
	private ReplayContentDao replayContentDao;
	@Autowired
	private ScWxuserManager  scWxuserManager;
	@Autowired
	private WeChatApiManager weChatApiManager;


	@Autowired
	public void setConditionerAddTimeDao(ConditionerAddTimeDao conditionerAddTimeDao) {
		this.conditionerAddTimeDao = conditionerAddTimeDao;
		this.dao = conditionerAddTimeDao;
	}

	@Override
	public Page findPageBy(Map<String, Object> params, int pageSize, int pageNo) {

		return conditionerAddTimeDao.findPageBy(params, pageSize, pageNo);
	}

	@Override
	public Page reportFindPageBy(Map<String, Object> params, int pageSize,
								 int pageNo) {
		return conditionerAddTimeDao.reportFindPageBy(params, pageSize, pageNo);
	}

	@Override
	public List<Map<String, Object>> reportFind(Map<String, Object> params) {

		return conditionerAddTimeDao.reportFind(params);
	}

	@Override
	public Page reportDetailFindPageBy(Map<String, Object> params,
									   int pageSize, int pageNo) {
		return conditionerAddTimeDao.reportDetailFindPageBy(params, pageSize, pageNo);
	}

	@Override
	public void sendAgreeWechat(AirConditionerAddTime bill, String remark) {

		log.info("ConditionerAddTimeManagerImpl sendAgreeWechat======");
		/*List<WxtemplateMessageTypeModel> templateList=replayContentDao.getWxTemplateByTempId(0, auditResultTempleteId);
		if(!CollectionUtils.isEmpty(templateList)){
			String templateId=(String) templateList.get(0).getTemplateid();
			Map<String,String> paramMap=new HashMap<String,String>();
			//boolean bool=scWxuserManager.exists(Integer.valueOf(String.valueOf(bill.getCreatorBillId())));
			WxUser wxUser=scWxuserManager.getScWxuserByOpenId(bill.getCreatorBillId());
			if(null!=wxUser){
				//WxUser wxUser=scWxuserManager.get(Integer.valueOf(String.valueOf(bill.getCreatorBillId())));
				String detailUrl=host+wxAirApplyDetailUrl.replaceAll("@id", bill.getId().toString());
				paramMap.put("developId", wxUser.getDevelopId());
				paramMap.put("openId", wxUser.getOpenId());
				paramMap.put("templateId", templateId);
				paramMap.put("url", detailUrl);
				paramMap.put("title","您好！您已通过贵司审核！");
				paramMap.put("type", "空调加时");//审核类型
				paramMap.put("result", "审核通过");//审核结果
				paramMap.put("suggestion", bill.getRoomNames()+":空调加时申请审核通过");//审核原因
				paramMap.put("remark", "感谢您的配合，祝您生活愉快！");
				log.info("ConditionerAddTimeManagerImpl sendAgreeWechat paramMap======"+paramMap.toString());
				String str=weChatApiManager.sendTemplateMsg02(paramMap);
				log.info("ConditionerAddTimeManagerImpl sendAgreeWechat str======"+str);
			}
		}*/
		//有可能不是openid
		if (StringUtils.isNotEmpty(bill.getCreatorBillId())) {
			WxUser wxUser = scWxuserManager.getScWxuserByOpenId(bill.getCreatorBillId());
			if (wxUser != null) {
				//String detailUrl = host + wxAirApplyDetailUrl.replaceAll("@id", bill.getId().toString());
				
				
				String detailUrl = wxAirApplyDetailUrl.replaceAll("@id", bill.getId().toString());
				detailUrl = detailUrl.replaceAll("@roleId", bill.getRoleId()==null?"":bill.getRoleId().toString() );
				detailUrl = detailUrl.replaceAll("@orgId", bill.getOrganizationId().toString());
				detailUrl = detailUrl.replaceAll("@virId", bill.getVirtualId().toString());
				detailUrl = detailUrl.replaceAll("@openId", wxUser.getOpenId().toString());
				detailUrl = detailUrl + "&isAuth=false";
				
				
				String content   = "你好，你的空调加时申请已经审核通过";
				content += "<a href=\"" + detailUrl + "\">查看详情>></a>";
				weChatApiManager.sendServiceWithOpenId(wxUser.getDevelopId(), wxUser.getId(), content);
			}
		}


	}

	@Override
	public void sendRefuseWechat(AirConditionerAddTime bill, String remark) {

		log.info("ConditionerAddTimeManagerImpl sendRefuseWechat======");
		/*List<WxtemplateMessageTypeModel> templateList=replayContentDao.getWxTemplateByTempId(0, auditResultTempleteId);
		if(!CollectionUtils.isEmpty(templateList)){
			String templateId=(String) templateList.get(0).getTemplateid();
			Map<String,String> paramMap=new HashMap<String,String>();
			WxUser wxUser=scWxuserManager.getScWxuserByOpenId(bill.getCreatorBillId());
			if(null!=wxUser){
				//WxUser wxUser=scWxuserManager.get(Integer.valueOf(String.valueOf(bill.getCreatorBillId())));
				String detailUrl=host+wxAirApplyDetailUrl.replaceAll("@id", bill.getId().toString());
				paramMap.put("developId", wxUser.getDevelopId());
				paramMap.put("openId", wxUser.getOpenId());
				paramMap.put("templateId", templateId);
				paramMap.put("url", detailUrl);
				paramMap.put("title","您好！您未通过贵司审核！");
				paramMap.put("type", "空调加时");//审核类型
				paramMap.put("result", "审核不通过");//审核结果
				paramMap.put("suggestion",bill.getRoomNames()+"不通过原因为:"+remark);//审核原因
				paramMap.put("remark", "感谢您的配合，祝您生活愉快！");
				log.info("ConditionerAddTimeManagerImpl sendRefuseWechat paramMap======"+paramMap.toString());
				String str=weChatApiManager.sendTemplateMsg02(paramMap);
				log.info("ConditionerAddTimeManagerImpl sendRefuseWechat str======"+str);
			}
		}*/

		//有可能不是openid
		if (StringUtils.isNotEmpty(bill.getCreatorBillId())) {
			WxUser wxUser = scWxuserManager.getScWxuserByOpenId(bill.getCreatorBillId());
			if (wxUser != null) {
				//String detailUrl = host + wxAirApplyDetailUrl.replaceAll("@id", bill.getId().toString());
				
				
				String detailUrl = wxAirApplyDetailUrl.replaceAll("@id", bill.getId().toString());
				detailUrl = detailUrl.replaceAll("@roleId", bill.getRoleId()==null?"":bill.getRoleId().toString() );
				detailUrl = detailUrl.replaceAll("@orgId", bill.getOrganizationId().toString());
				detailUrl = detailUrl.replaceAll("@virId", bill.getVirtualId().toString());
				detailUrl = detailUrl.replaceAll("@openId", wxUser.getOpenId().toString());
				detailUrl = detailUrl + "&isAuth=false";
				
				
				String content   = "你好，你的空调加时申请审核不通过,原因为:" + remark;
				content += "<a href=\"" + detailUrl + "\">查看详情>></a>";
				weChatApiManager.sendServiceWithOpenId(wxUser.getDevelopId(), wxUser.getId(), content);
			}
		}

	}

	@Override
	public void sendCancelWechat(AirConditionerAddTime bill, String remark) {

		log.info("ConditionerAddTimeManagerImpl sendCancelWechat======");

		/*List<WxtemplateMessageTypeModel> templateList=replayContentDao.getWxTemplateByTempId(0, auditResultTempleteId);
		if(!CollectionUtils.isEmpty(templateList)){
			String templateId=(String) templateList.get(0).getTemplateid();
			Map<String,String> paramMap=new HashMap<String,String>();
			WxUser wxUser=scWxuserManager.getScWxuserByOpenId(bill.getCreatorBillId());
			if(null!=wxUser){
				//WxUser wxUser=scWxuserManager.get(Integer.valueOf(String.valueOf(bill.getCreatorBillId())));
				String detailUrl=host+wxAirApplyDetailUrl.replaceAll("@id", bill.getId().toString());
				paramMap.put("developId", wxUser.getDevelopId());
				paramMap.put("openId", wxUser.getOpenId());
				paramMap.put("templateId", templateId);
				paramMap.put("url", detailUrl);
				paramMap.put("title","您好！您未通过贵司审核！");
				paramMap.put("type", "空调加时");//审核类型
				paramMap.put("result", "审核不通过");//审核结果
				paramMap.put("suggestion",bill.getRoomNames()+"取消原因为:"+remark);//审核原因
				paramMap.put("remark", "感谢您的配合，祝您生活愉快！");

				log.info("ConditionerAddTimeManagerImpl sendCancelWechat paramMap======"+paramMap.toString());
				String str=weChatApiManager.sendTemplateMsg02(paramMap);
				log.info("ConditionerAddTimeManagerImpl sendCancelWechat str======"+str);
			}
		}*/

		//有可能不是openid
		if (StringUtils.isNotEmpty(bill.getCreatorBillId())) {
			WxUser wxUser = scWxuserManager.getScWxuserByOpenId(bill.getCreatorBillId());
			if (wxUser != null) {
				//String detailUrl = wxAirApplyDetailUrl.replaceAll("@id", bill.getId().toString())+"&isAuth=false";				
				
				
				String detailUrl = wxAirApplyDetailUrl.replaceAll("@id", bill.getId().toString());
				detailUrl = detailUrl.replaceAll("@roleId", bill.getRoleId()==null?"":bill.getRoleId().toString() );
				detailUrl = detailUrl.replaceAll("@orgId", bill.getOrganizationId().toString());
				detailUrl = detailUrl.replaceAll("@virId", bill.getVirtualId().toString());
				detailUrl = detailUrl.replaceAll("@openId", wxUser.getOpenId().toString());
				detailUrl = detailUrl + "&isAuth=false";			
				
				
				String content   = "你好，你的空调加时申请已取消,取消原因为:" + remark;
				content += "<a href=\"" + detailUrl + "\">查看详情>></a>";
				weChatApiManager.sendServiceWithOpenId(wxUser.getDevelopId(), wxUser.getId(), content);
			}
		}

	}


	public void sendCancelWechat(String id,String CreatorBillId, String content,AirConditionerAddTime bill) {
		//有可能不是openid
		log.info("ConditionerAddTimeServiceImpl id======" + id);
		log.info("ConditionerAddTimeServiceImpl CreatorBillId======" + CreatorBillId);
		log.info("ConditionerAddTimeServiceImpl bill======" + JsonUtils.toJson(bill));
		if (StringUtils.isNotEmpty(CreatorBillId)) {


			WxUser wxUser = scWxuserManager.getScWxuserByOpenId(CreatorBillId);
			log.info("ConditionerAddTimeServiceImpl wxUser======" + JsonUtils.toJson(wxUser));
			if (wxUser != null) {
				String detailUrl = wxAirApplyDetailUrl.replaceAll("@id", bill.getId().toString());
				detailUrl = detailUrl.replaceAll("@roleId", bill.getRoleId()==null?"":bill.getRoleId().toString() );
				detailUrl = detailUrl.replaceAll("@orgId", bill.getOrganizationId().toString());
				detailUrl = detailUrl.replaceAll("@virId", bill.getVirtualId().toString());
				detailUrl = detailUrl.replaceAll("@openId", wxUser.getOpenId().toString());
				detailUrl = detailUrl + "&isAuth=false";

				content += "<a href=\"" + detailUrl + "\">查看详情>></a>";
				weChatApiManager.sendServiceWithOpenId(wxUser.getDevelopId(), wxUser.getId(), content);
			}
		}

	}
	
	@Override
	public void sendNextAgreeWechat(AirConditionerAddTime bill, WxUser wxUser) {

		if (wxUser != null) {

			//String new_id    = encodeNewId(bill.getId().toString());
			//String new_id    = bill.getId().toString();
			/*String detailUrl = host + wxReleaseRecordDetailUrl.replaceAll("@id", new_id).replaceAll("@orgId", bill.getOrgId().toString());
//			String content = "你好，你有新的电子放行单待审核！";
//			content += "<a href=\""+detailUrl+"\">点击审核>></a>";

			String content = "你的企业有人发起了电子放行条申请，请前往服务之窗电子放行条放行审核模块审核相应的放行申请单！";
			if (bill.getApplyType() == 1) {
				content = "你的企业有人发起了电子通行证申请，请前往服务之窗电子通行证审核模块审核相应的电子通行证单！";
			}*/
			
			String detailUrl = wxAirApplyDetailUrl.replaceAll("@id", bill.getId().toString());
			detailUrl = detailUrl.replaceAll("@roleId", bill.getRoleId()==null?"":bill.getRoleId().toString() );
			detailUrl = detailUrl.replaceAll("@orgId", bill.getOrganizationId().toString());
			detailUrl = detailUrl.replaceAll("@virId", bill.getVirtualId().toString());
			detailUrl = detailUrl.replaceAll("@openId", wxUser.getOpenId().toString());
			detailUrl = detailUrl + "&isAuth=true";
			String content   = "本企业有新的空调加时申请订单："+bill.getRoomNames()+"室申请于"+bill.getUseDisplayTime()+"加开空调，请及时审核！";
			content += "<a href=\"" + detailUrl + "\">去审批>></a>";
			
			
			weChatApiManager.sendServiceWithOpenId(wxUser.getDevelopId(), wxUser.getId(), content);
		}
	}

	/**
	 * 查找空 加时单 申请时间大于当前时间
	 * 条件:待使用状态
	 */
	@Override
	public List<Map<String, Object>> findMeeting(){
		return conditionerAddTimeDao.findMeeting();
	}

	/**
	 * 加时单 申请时间大于当前时间  把状态改为已完成
	 */
	@Override
	public void airAddTimeDoTimerTask(String id) {
		conditionerAddTimeDao.airAddTimeDoTimerTask(id);
	}

	@Override
	public List<Map<String, Object>> airConditioningOrder(Map<String, Object> params) {
		return conditionerAddTimeDao.airConditioningOrder(params);
	}

	@Override
	public void updateAirConditioningOrder(Map<String, Object> params) {
		conditionerAddTimeDao.updateAirConditioningOrder(params);
	}


	/**
	 * #加时审批过时间
	 * #当前时间>空调申请开始时间
	 * #且状态为21 22 23状态
	 * #将状态 改为:35(无效
	 * #且
	 * #cancelReason='管理员审批超时'
	 * #cancelUser='系统自动'
	 * #cancelTime=NOW()
	 */
	@Override
	public void airAddTimeAuditOverTimeTask() {
		conditionerAddTimeDao.airAddTimeAuditOverTimeTask();
	}

	@Override
	public List<Map<String, Object>> getAirAddTimeBusinessData(
			Map<String, Object> params) {
		return conditionerAddTimeDao.getAirAddTimeBusinessData(params);
	}

	
	@Override
	public List<Map<String, Object>> getGoodsReleaseBusinessData(
			Map<String, Object> params) {
		return conditionerAddTimeDao.getGoodsReleaseBusinessData(params);
	}

	@Override
	public List<Map<String, Object>> getNoticeBusinessData(Map<String, Object> params) {
		return conditionerAddTimeDao.getNoticeBusinessData(params);
	}

	@Override
	public List<Map<String, Object>> getRepairBusinessData(Map<String, Object> params) {
		return conditionerAddTimeDao.getRepairBusinessData(params);
	}

	@Override
	public List<Map<String, Object>> getVisitorBusinessData(Map<String, Object> params) {
		return conditionerAddTimeDao.getVisitorBusinessData(params);
	}
}
