package com.ssj.service.weixin.user.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ssj.bean.weixin.message.custom.Image;
import com.ssj.bean.weixin.message.custom.ImageCustom;
import com.ssj.bean.weixin.user.domain.QrCode;
import com.ssj.bean.weixin.user.domain.SubActivity;
import com.ssj.bean.weixin.user.domain.User;
import com.ssj.dao.weixin.user.dao.SubActivityDao;
import com.ssj.dao.weixin.user.dao.SubActivityQueryDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.framework.core.security.manager.TokenManager;
import com.ssj.framework.weixin.util.ImageUtil;
import com.ssj.framework.weixin.util.MUDload;
import com.ssj.framework.weixin.util.NewsUtil;
import com.ssj.framework.weixin.util.WeixinMedia;
import com.ssj.service.weixin.user.service.QrCodeService;
import com.ssj.service.weixin.user.service.SubActivityService;
import com.ssj.service.weixin.user.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.transaction.annotation.Transactional;

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


@Service(version="1.0")
@Transactional
public class SubActivityServiceImpl extends BaseServiceImpl<SubActivity, String> implements SubActivityService {
	private static Logger log = LoggerFactory.getLogger(SubActivityServiceImpl.class);
	
	@Autowired
	private SubActivityDao dao;
	
	@Autowired
	private UserService userService;

	@Autowired
	private QrCodeService qrCodeService;
	
	@Autowired
	private TokenManager tokenManager;

	@Autowired
	private SubActivityQueryDao queryDao;

	@Override
	public PagingAndSortingRepository<SubActivity, String> getDao() {
		return dao;
	}


	@Override
	public List<SubActivity> findSubActivityByRemarks(String remarks){
		return dao.findSubActivityByRemarks(remarks);
	}


	@Override
	public List<SubActivity> findSubActivityByPO(String remarks, String openId) {
		return dao.findSubActivityByPO(remarks, openId);
	}


	@Override
	public List<SubActivity> findSubActivityByOpenid(String openId) {
		return dao.findSubActivityByOpenid(openId);
	}

	@Override
	public SubActivity findByOpenidAndParentId(String openId, String objectId,int type) {
		return dao.findByOpenidAndParentId(openId,objectId,type);
	}

	@Override
	public int findCountByParentId(String objectId,int type) {
		return dao.findCountByParentId(objectId,type);
	}




	@Override
	public SubActivity generatePosterByTerminal(String openId,String parentId,String templateUrl) {
		log.error("进入生产海报---openid---"+openId);
		if(StringUtils.isEmpty(templateUrl)){
			templateUrl="/back_terminal.jpg";
		}
		SubActivity subActivity=null;
		User user=userService.findByAccount(openId);
		if(user!=null) {
			subActivity=dao.getSubActivityByOpenidAndType(user.getAccount(), 11);
			if(subActivity==null) {
				//生成海报
				QrCode	qrcode = new  QrCode();
				qrcode.setType("QR_STR_SCENE");//设置临时的二维码
				qrcode.setExpireSeconds("2592000");//时间最大为30天这里为30天
				qrcode.setObjectId(user.getAccount());
				qrcode.setSceneId("");
				qrcode.setParentId(parentId);
				QrCode qrCode=qrCodeService.createQrCode(qrcode);
				
				String image=ImageUtil.compositeTerminalImg(templateUrl, user.getPhoto(), qrCode.getQrcodeUrl());
				subActivity=new SubActivity();
				subActivity.setType(11);
				subActivity.setRemarks(image);
				subActivity.setCreateTime(new Date());
				subActivity.setOpenid(user.getAccount());
				subActivity.setQrCodeId(qrCode.getId());
				subActivity = this.save(subActivity);
			}
		}
		
		if(subActivity!=null && StringUtils.isNotEmpty(subActivity.getRemarks())) {
			try {
				String mediaId = tokenManager.getString("mask"+subActivity.getId());
				if(StringUtils.isEmpty(mediaId)){
					WeixinMedia weixinMedia = MUDload.uploadMedia(tokenManager.getSSJAccessToken(), "image", subActivity.getRemarks());
					mediaId = weixinMedia.getMediaId();
					tokenManager.setString("mask"+subActivity.getId(),mediaId,3*24*3600);
				}
				ImageCustom  imgCustom = new  ImageCustom();
				imgCustom.setMsgtype("image");
				imgCustom.setTouser(openId);
				Image image = new Image();
				image.setMedia_id(mediaId);
				imgCustom.setImage(image);
				NewsUtil.send(tokenManager.getSSJAccessToken(), imgCustom);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return subActivity;
	}

	@Override
	public Page<Map<String, Object>> findByTypePages(Map<String, Object> params, Pageable initPage) {
		return queryDao.findByTypePages(params,initPage);
	}

}
