/**
 * 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.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.RdAccountDailyStatDao;
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.dto.RdAccountDailyStatDTO;
import io.renren.modules.sys.entity.AdvertOauthAccountEntity;
import io.renren.modules.sys.entity.RdAccountDailyStatEntity;
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.util.Date;
import java.util.List;

/**
 * 刷新账户日流水
 *
 */
@Component("refreshAdvertiserFundDailyStatTask")
public class RefreshAdvertiserFundDailyStatTask implements ITask{
	private Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private RdAdvertiserDao rdAdvertiserDao;
	@Autowired
	private AdvertOauthAccountDao advertOauthAccountDao;
	@Autowired
	private RdAccountDailyStatDao rdAccountDailyStatDao;
	@Autowired
	private AdvertPlatformOauthContext advertPlatformOauthContext;

	@Override
	public void run(String params) {
		logger.debug("RefreshAdvertiserFundDailyStatTask定时任务正在执行，参数为：{}", params);

		//查询所有广告主账号
		List<RdAdvertiserEntity> advertiserEntityList = rdAdvertiserDao.selectList(new QueryWrapper<RdAdvertiserEntity>().eq("status", 1));
		if (advertiserEntityList.isEmpty()) {
			return;
		}

		String date = DateUtils.format(DateUtils.addDateDays(new Date(), -1));
		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());
					RdAccountDailyStatDTO rdAccountDailyStatDTO = remoteAdvertPort.getAdvertiserFundDailyStat(advertiserEntity.getAdvertiserId(), account.getAccessToken(), date);
					if (rdAccountDailyStatDTO != null) {
						//判断该广告主账户日流水信息是否已经存在
						RdAccountDailyStatEntity rdAccountDailyStatEntity = rdAccountDailyStatDao.selectOne(new QueryWrapper<RdAccountDailyStatEntity>()
								.eq("status", 1).eq("advertiser_id", advertiserEntity.getAdvertiserId()).eq("date", date));
						if (rdAccountDailyStatEntity == null) {
							rdAccountDailyStatEntity = new RdAccountDailyStatEntity();
							BeanUtils.copyProperties(rdAccountDailyStatDTO, rdAccountDailyStatEntity);
							rdAccountDailyStatEntity.setCreateTime(new Date());
							rdAccountDailyStatEntity.setUpdateTime(new Date());
							rdAccountDailyStatDao.insert(rdAccountDailyStatEntity);
						} else {
							Long id = rdAccountDailyStatEntity.getId();
							BeanUtils.copyProperties(rdAccountDailyStatDTO, rdAccountDailyStatEntity);
							rdAccountDailyStatEntity.setId(id);
							rdAccountDailyStatEntity.setUpdateTime(new Date());
							rdAccountDailyStatDao.updateById(rdAccountDailyStatEntity);
						}
					}
				}
				Thread.sleep(200);
			} catch (Exception e) {
				logger.error("refreshAdvertiserFundDailyStatTask advertiserId={},error={}", advertiserEntity.getAdvertiserId(), e);
			}
		}
	}
}