package com.iamberry.wechat.service.qrcode;

import java.util.Date;

import com.iamberry.wechat.tools.WeixinUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iamberry.wechat.core.entity.member.Member;
import com.iamberry.wechat.core.entity.qrcode.QrcodeResult;
import com.iamberry.wechat.core.entity.qrcode.TemporaryQrcode;
import com.iamberry.wechat.core.entity.wx.QRCJson;
import com.iamberry.wechat.face.qrcode.TemporaryQrcodeService;
import com.iamberry.wechat.service.home.dao.HomeServiceDao;
import com.iamberry.wechat.service.mapper.TemporaryQrcodeMapper;
import com.iamberry.wechat.tools.DateTimeUtil;
import com.iamberry.wechat.tools.NameUtils;

/**
 * 临时二维码
 * @author 穆再兴
 * Create Date:2016年7月5日
 */
@Service
public class TemporaryQrcodeServiceImpl implements TemporaryQrcodeService {
	@Autowired
	private HomeServiceDao homeServiceDao;

	private WeixinUtil weixinUtil;
	@Autowired
	private TemporaryQrcodeMapper temporaryQrcodeMapper;
	
	/**
	 * 申请一个临时二维码
	 * @return
	 */
	public QrcodeResult applyTempQrcode(String openid){
		QrcodeResult qr = new QrcodeResult();
		if (openid == null || openid.isEmpty()) {
			qr.setSuccess(false);
			qr.setQroceUrl("openid 不能为空!");
			return qr;
		}
		// 获取openid对应的qrcodeURL
		Member member = homeServiceDao.selectMemberQrcodeUrlByOpenid(openid);
		if (!(member != null && member.getUserId() != null)) {
			qr.setSuccess(false);
			qr.setQroceUrl("此用户不存在!");
			return qr;
		}
		TemporaryQrcode tempQrcode = temporaryQrcodeMapper.getTempQrcodeByOpenId(openid);
		return refreshTempQrcode(openid,tempQrcode);
	}
	
	/**
	 * 根据用户 openid 查询临时二维码
	 * @param openid
	 * @return
	 */
	public TemporaryQrcode getTempQrcodeByOpenId(String openid){
		return temporaryQrcodeMapper.getTempQrcodeByOpenId(openid);
	}
	
	/**
	 * 根据用户 openid 查询临时二维码
	 * @param openid
	 * @return
	 */
	public TemporaryQrcode getTempQrcodeById(Integer id){
		return temporaryQrcodeMapper.getTempQrcodeById(id);
	}
	/**
	 * 添加一条 TemporaryQrcode 记录
	 * @param temporaryQrcode
	 * @return
	 */
	public Integer insertTempQrcode(TemporaryQrcode temporaryQrcode){
		return temporaryQrcodeMapper.insertTempQrcode(temporaryQrcode);
	}
	
	/**
	 * 根据用户 openid 修改二维码url
	 * @return
	 */
	public Integer updateTempQrcodeURLByOpenid(TemporaryQrcode temporaryQrcode){
		return temporaryQrcodeMapper.updateTempQrcodeURLByOpenid(temporaryQrcode);
	}
	
	/**
	 * 根据用户 openid 查询该用户的 微代理二维码
	 * @param openid
	 * @return
	 */
	public QrcodeResult getTempQrcodeUrlByOpenid(String openid){
		TemporaryQrcode tq = temporaryQrcodeMapper.getTempQrcodeByOpenId(openid);
		return refreshTempQrcode(openid, tq);
	}
	
	/**
	 * 刷新临时二维码 返回正确的二维码 url
	 * @param member
	 * @param tempQrcode
	 * @return
	 */
	private QrcodeResult refreshTempQrcode(String openid,TemporaryQrcode tempQrcode){
		QrcodeResult qr = new QrcodeResult();
		if(tempQrcode != null && tempQrcode.getQrId() != null){		//临时二维码已存在
			Date now = new Date(); 
			if(now.before(tempQrcode.getQrEndDate())){		//临时二维码未过期正常使用
				qr.setSuccess(true);
				qr.setQroceUrl(tempQrcode.getQrUrl());
				return qr;
			}else{		//临时二维码已过期过期 则重新生成
				qr = createWXqrcode(openid, tempQrcode);
			}
		}else{		//临时二维码不存在 则生成二维码表记录 并生成二维码
			TemporaryQrcode tempQr = new TemporaryQrcode();
			tempQr.setQrOpenid(openid);
			Date now = new Date();
			tempQr.setQrCreateDate(now);
			tempQr.setQrUpdateDate(now);
			int count = temporaryQrcodeMapper.insertTempQrcode(tempQr);
			if (count < 1 || tempQr.getQrId() == null) {
				qr.setSuccess(false);
				qr.setQroceUrl("服务器忙，请稍后...");
				return qr;
			}
			qr = createWXqrcode(openid, tempQr);
		}
		return qr;
	}
	/**
	 * 创建临时二维码 并更新用户表 二维码表
	 * @param member
	 * @param tempQrcode
	 * @return
	 */
	private QrcodeResult createWXqrcode(String openid,TemporaryQrcode tempQrcode){
		QrcodeResult qr = new QrcodeResult();
		//创建临时二维码 有效期30天
		String json = String.format(NameUtils.QR_SCENE, 2592000 ,tempQrcode.getQrId().toString());
		QRCJson qrc = weixinUtil.createQrcode(json);
		if (!(qrc == null || qrc.getUrl() == null || qrc.getUrl().isEmpty())) {
			//设置过期时间为 29天23小时55分钟
			tempQrcode.setQrEndDate(DateTimeUtil.addMinute(DateTimeUtil.addDay(29), 1435));
			tempQrcode.setQrUpdateDate(new Date());
			tempQrcode.setQrUrl(qrc.getUrl());
			tempQrcode.setQrValue(tempQrcode.getQrId().toString());
			temporaryQrcodeMapper.updateTempQrcodeURLByOpenid(tempQrcode);
			Member member = new Member();
			member.setUserOpenid(openid);
			member.setUserQrcode(qrc.getUrl());
			int count = homeServiceDao.updateMemberQrcodeByOpenid(member);
			if (count >= 1) {
				qr.setQroceUrl(qrc.getUrl());
				qr.setSuccess(true);
				return qr;
			}
		}
		return qr;
	}
}
