package com.hotent.portal.persistence.manager.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hotent.base.manager.impl.BaseManagerImpl;
import com.hotent.base.model.CommonResult;
import com.hotent.base.util.BeanUtils;
import com.hotent.base.util.UniqueIdUtil;
import com.hotent.portal.model.PortalWeixinMaterial;
import com.hotent.portal.model.SysIndexColumn;
import com.hotent.portal.params.NewsItem;
import com.hotent.portal.persistence.dao.PortalWeixinMaterialDao;
import com.hotent.portal.persistence.manager.PortalWeixinMaterialManager;
import com.hotent.portal.persistence.manager.SysIndexColumnManager;
import com.hotent.portal.service.WeChatOfficialService;

import dm.jdbc.util.StringUtil;

/**
 * 微信永久素材列表 服务实现类
 *
 * @company 广州宏天软件股份有限公司
 * @author 超级管理员
 * @email zhangxw@jee-soft.cn
 * @since 2021-09-07
 */
@Service
public class PortalWeixinMaterialManagerImpl extends BaseManagerImpl<PortalWeixinMaterialDao, PortalWeixinMaterial> implements PortalWeixinMaterialManager {

	@Resource
	WeChatOfficialService weChatOfficialService;
	@Resource
	SysIndexColumnManager sysIndexColumnService;
	@Resource
	PortalWeixinMaterialDao portalWeixinMaterialDao;
	
	@Transactional
	@Override
	public CommonResult<String> pullWeixinMaterial() throws Exception {
		//获取微信公众号素材栏目
		try {
			QueryWrapper wrapper = new QueryWrapper();
			wrapper.isNotNull("APPID_");
			List<SysIndexColumn> columns = sysIndexColumnService.list(wrapper);
			if(BeanUtils.isNotEmpty(columns) && columns.size()>0) {
				Set<String> appIds = new HashSet<String>();
				for (SysIndexColumn column : columns) {
					if(StringUtil.isNotEmpty(column.getAppid()) && StringUtil.isNotEmpty(column.getSecret()) && !appIds.contains(column.getAppid())) {
						List<NewsItem> items = weChatOfficialService.getMaterial(column.getAppid(), column.getSecret(), "news", 0, 20);
						handleMaterial(column.getAppid(), items);
					}
				}
				
			}
		} catch (Exception e) {
			return new CommonResult<String>(false, "拉取失败："+e.getMessage());
		}
		return new CommonResult<String>(true, "拉取成功！");
	}
	
	/**
	 * 根据appId删除旧的图文数据，取最新的数据
	 * @param appId
	 * @param items
	 */
	@Transactional
	private void handleMaterial(String appId,List<NewsItem> items) {
		baseMapper.delByAppid(appId);
		if(BeanUtils.isNotEmpty(items)) {
			List<PortalWeixinMaterial> list = trans(appId, items);
			this.saveBatch(list);
		}
	}
	
	/**
	 * 实体转换
	 * @param appId
	 * @param items
	 * @return
	 */
	private List<PortalWeixinMaterial> trans(String appId,List<NewsItem> items){
		LocalDateTime pullTime = LocalDateTime.now();
		List<PortalWeixinMaterial> materials = new ArrayList<PortalWeixinMaterial>();
		for (NewsItem newsItem : items) {
			PortalWeixinMaterial material = new PortalWeixinMaterial();
			material.setId(UniqueIdUtil.getSuid());
			material.setAppid(appId);
			material.setAuthor(newsItem.getAuthor());
			material.setContent(newsItem.getContent());
			material.setTitle(newsItem.getTitle());
			material.setDigest(newsItem.getDigest());
			material.setContentSourceUrl(newsItem.getContent_source_url());
			material.setThumbMediaId(newsItem.getThumb_media_id());
			material.setThumbUrl(newsItem.getThumb_url());
			material.setShowCoverPic(newsItem.getShow_cover_pic());
			material.setUrl(newsItem.getUrl());
			material.setNeedOpenComment(newsItem.getNeed_open_comment());
			material.setOnlyFansCanComment(newsItem.getOnly_fans_can_comment());
			material.setCreateTime(newsItem.getCreateTime());
			material.setUpdateTime(newsItem.getUpdateTime());
			material.setPullTime(pullTime);
			materials.add(material);
		}
		return materials;
	}

}
