/**
 * Copyright (c) 2022 雷掣 All rights reserved.
 *
 * https://www.lc_crm.com
 *
 * 版权所有，侵权必究！
 */

package io.renren.modules.job.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.advert.AdvertPlatformAuthStatusEnum;
import io.renren.common.advert.AdvertPlatformOauthContext;
import io.renren.common.advert.AdvertService;
import io.renren.common.utils.DateUtils;
import io.renren.modules.demo.service.RemoteAdvertPort;
import io.renren.modules.sys.dao.AdvertOauthAccountDao;
import io.renren.modules.sys.dao.AdvertOauthDao;
import io.renren.modules.sys.dao.RdAccountDao;
import io.renren.modules.sys.dao.RdAdvertiserDao;
import io.renren.modules.sys.dto.RdAccountDTO;
import io.renren.modules.sys.entity.AdvertOauthAccountEntity;
import io.renren.modules.sys.entity.AdvertOauthEntity;
import io.renren.modules.sys.entity.RdAccountEntity;
import io.renren.modules.sys.entity.RdAdvertiserEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.Clock;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 刷新用户资金账户
 *
 */
@Component("refreshAdvertiserFundTask")
public class RefreshAdvertiserFundTask implements ITask{
	private Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private RdAdvertiserDao rdAdvertiserDao;
	@Autowired
	private AdvertOauthAccountDao advertOauthAccountDao;
	@Autowired
	private RdAccountDao rdAccountDao;
	@Autowired
	private AdvertPlatformOauthContext advertPlatformOauthContext;

	@Override
	public void run(String params){
		logger.debug("RefreshAdvertiserFundTask定时任务正在执行，参数为：{}", params);
		//查询所有广告主账号
		List<RdAdvertiserEntity> advertiserEntityList = rdAdvertiserDao.selectList(new QueryWrapper<RdAdvertiserEntity>().eq("status", 1));
		if (advertiserEntityList.isEmpty()) {
			return;
		}

		for (RdAdvertiserEntity advertiserEntity:advertiserEntityList){
			try {
				//查询授权账户表获取token
				AdvertOauthAccountEntity account = advertOauthAccountDao.selectOne(new QueryWrapper<AdvertOauthAccountEntity>().eq("status",1)
					.eq("account_id",advertiserEntity.getAccountId()).orderByDesc("update_time").last("limit 1"));
				if (account!=null){
					//获取资金账户信息
					RemoteAdvertPort remoteAdvertPort = advertPlatformOauthContext.getAdvertOauthPortByAnnotation(AdvertService.SERVICE_OAUTH + advertiserEntity.getAdvertPlatform());
					RdAccountDTO rdAccountDTO = remoteAdvertPort.getAdvertiserFund(advertiserEntity.getAdvertiserId(),account.getAccessToken());
					if (rdAccountDTO!=null){
						//判断该广告主资金账户信息是否已经存在
						RdAccountEntity rdAccountEntity = rdAccountDao.selectOne(new QueryWrapper<RdAccountEntity>()
							.eq("status",1).eq("advertiser_id",advertiserEntity.getAdvertiserId()));
						if (rdAccountEntity==null){
							rdAccountEntity = new RdAccountEntity();
							BeanUtils.copyProperties(rdAccountDTO,rdAccountEntity);
							rdAccountEntity.setCreateTime(new Date());
							rdAccountEntity.setUpdateTime(new Date());
							rdAccountDao.insert(rdAccountEntity);
						}else{
							Long id = rdAccountEntity.getId();
							BeanUtils.copyProperties(rdAccountDTO,rdAccountEntity);
							rdAccountEntity.setId(id);
							rdAccountEntity.setUpdateTime(new Date());
							rdAccountDao.updateById(rdAccountEntity);
						}
					}
				}
				Thread.sleep(200);
			}catch (Exception e){
				logger.error("refreshAdvertiserFundTask advertiserId={},error={}",advertiserEntity.getAdvertiserId(),e);
			}
		}
	}
}