package cn.com.ctop.kuaishou.modules.report.service.impl;

import cn.com.ctop.common.module.db.dynamic.DataSourceCachePool;
import cn.com.ctop.common.module.entity.CtopOauthToken;
import cn.com.ctop.common.module.service.ICtopOauthTokenService;
import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaishouInterfaceService;
import cn.com.ctop.kuaishou.modules.report.entity.KuaishouCostGroup;
import cn.com.ctop.kuaishou.modules.report.mapper.KuaishouCostGroupMapper;
import cn.com.ctop.kuaishou.modules.report.mapper.KuaishouReportHourlyGroupMapper;
import cn.com.ctop.kuaishou.modules.report.service.IKuaishouCostGroupService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 快手组级时报统计
 *
 * @author jeecg-boot
 * @version V1.0
 * @date 2020-10-30
 */
@Slf4j
@Service
public class KuaishouCostGroupServiceImpl extends ServiceImpl<KuaishouCostGroupMapper, KuaishouCostGroup> implements IKuaishouCostGroupService {

    @Autowired
    private KuaishouCostGroupMapper kuaishouCostGroupMapper;

    @Autowired
    private KuaishouReportHourlyGroupMapper kuaishouReportHourlyGroupMapper;

    @Autowired
    private ICtopOauthTokenService oauthTokenService;
    @Autowired
    private IKuaishouInterfaceService iKuaishouInterfaceService;

    @Override
    public void saveInfo() {
        log.info("-----更新数据-start--------");
        long startTime = System.currentTimeMillis();
        String date = DateUtils.getNowDate(DateUtils.WEB_FORMAT);
        Map<String, Object> deleteMap = new HashMap<>();
        deleteMap.put("stat_date", date);
        kuaishouCostGroupMapper.deleteByMap(deleteMap);
        //获取账户级数据
        List<JSONObject> accountList = kuaishouCostGroupMapper.queryAccountCostInfo(date);
        if (!Check.isNull(accountList)) {
            //获取账户余额
            getTotalBalance(accountList);
            kuaishouCostGroupMapper.bulkInsert(accountList);
        }
        //获取计划级数据
        List<JSONObject> campaignList = kuaishouCostGroupMapper.queryCampaignCostInfo(date);
        if (!Check.isNull(campaignList)) {
            kuaishouCostGroupMapper.bulkInsert(campaignList);
        }
        //获取组级数据
        List<JSONObject> grouplist = kuaishouCostGroupMapper.queryGroupCostInfo(date);
        if (!Check.isNull(grouplist)) {
            kuaishouCostGroupMapper.bulkInsert(grouplist);
        }
        Map<String, List<JSONObject>> map = new HashMap<>();
        map.put("ACCOUNT", accountList);
        map.put("CAMPAIGN", campaignList);
        map.put("UNIT", grouplist);

        HashOperations<String, Object, Object> hash = DataSourceCachePool.getRedisTemplate().opsForHash();
        hash.putAll("KS_COST", map);
        long endTime = System.currentTimeMillis();
        log.info("---end---更新数据用时：{} ms", endTime - startTime);
    }

    /**
     * 查询余额
     *
     * @param
     * @return void
     * @throws
     * @author ZHAOXA
     */
    private void getTotalBalance(List<JSONObject> accountList) {
        for (JSONObject obj : accountList) {
            CtopOauthToken oauthToken = oauthTokenService.getTokenByAccountId(obj.getLong("accountId"));
            if (Check.isNull(oauthToken)) {
                log.warn("未获取到账户信息");
                continue;
            }
            JSONObject fundJson = iKuaishouInterfaceService.fundGet(oauthToken);
            if (!Check.isNull(fundJson)) {
                obj.put("totalBalance", fundJson.getBigDecimal("balance")); // 余额
            }
        }
    }
}
