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

import cn.com.ctop.common.module.entity.UserAllocation;
import cn.com.ctop.common.module.service.IProjectService;
import cn.com.ctop.common.module.service.ISysUserService;
import cn.com.ctop.common.module.service.IUserAllocationService;
import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouCampaignService;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouCreativeService;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouGroupService;
import cn.com.ctop.kuaishou.modules.report.entity.EtlKuaiShouNewly;
import cn.com.ctop.kuaishou.modules.report.mapper.EtlKuaiShouNewlyMapper;
import cn.com.ctop.kuaishou.modules.report.service.IEtlKuaiShouNewlyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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


/**
 * 快手 新上计划数据汇总
 *
 * @author jeecg-boot
 * @version V1.0
 * @date 2021-03-08
 */
@Service
public class EtlKuaiShouNewlyServiceImpl extends ServiceImpl<EtlKuaiShouNewlyMapper, EtlKuaiShouNewly> implements IEtlKuaiShouNewlyService {
    @Autowired
    private IUserAllocationService userAllocationService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IProjectService projectService;
    @Autowired
    private IKuaiShouCampaignService campaignService;
    @Autowired
    private IKuaiShouGroupService groupService;
    @Autowired
    private IKuaiShouCreativeService creativeService;
    @Autowired
    private EtlKuaiShouNewlyMapper kuaiShouNewlyMapper;

    private static List<String> typeList = new ArrayList();

    /**
     * 清洗上新数量
     */
    @Override
    public void cleanNewlyData(String statDate) {
        List<UserAllocation> userAllocations = userAllocationService.listByMediaId("2", 0);
        if (Check.isNull(userAllocations)) {
            return;
        }
        Map<Long, Long> projectIdMap = new HashMap<>();
        Map<String, String> userIdMap = new HashMap<>();
        for (UserAllocation allocation : userAllocations) {
            if (!Check.isNull(allocation.getProjectId())) {
                projectIdMap.put(allocation.getProjectId(), allocation.getProjectId());
            }
            if (!Check.isNull(allocation.getUserId())) {
                userIdMap.put(allocation.getUserId(), allocation.getUserId());
            }
        }
        Map<String, String> userMap = sysUserService.getUserMapByMap(userIdMap);
        Map<Long, String> projectMap = projectService.getProjectMap(projectIdMap);
        List<EtlKuaiShouNewly> addList = new ArrayList<>();
        if (Check.isNull(typeList)) {
            getTypeList();
        }
        for (int i = 0; i < typeList.size(); i++) {
            String newType = typeList.get(i);
            for (UserAllocation allocation : userAllocations) {
                EtlKuaiShouNewly addNewly = new EtlKuaiShouNewly();
                addNewly.setAccountId(allocation.getAccountId());
                addNewly.setOauthName(allocation.getAuthName());
                addNewly.setProjectId(allocation.getProjectId());
                if (!Check.isNullMap(projectMap)) {
                    addNewly.setProjectName(projectMap.get(allocation.getProjectId()));
                }
                addNewly.setUserId(allocation.getUserId());
                if (!Check.isNullMap(userMap)) {
                    addNewly.setUserName(userMap.get(allocation.getUserId()));
                }
                Integer count = 0;
                if (newType.equals("campaign")) {
                    count = campaignService.getNewDataByAccountIdAndDate(allocation.getAccountId(), statDate);
                } else if (newType.equals("unit")) {
                    count = groupService.getNewDataByAccountIdAndDate(allocation.getAccountId(), statDate);
                } else if (newType.equals("creative")) {
                    count = creativeService.getNewDataByAccountIdAndDate(allocation.getAccountId(), statDate);
                }
                addNewly.setNewCount(count);
                addNewly.setNewType(newType);
                addNewly.setStatDate(statDate);
                addList.add(addNewly);
            }
            if (!Check.isNull(addList)) {
                kuaiShouNewlyMapper.replaceBatch(addList);
                addList = new ArrayList<>();
            }
        }
    }

    private void getTypeList() {
        typeList.add("campaign");
        typeList.add("unit");
        typeList.add("creative");
    }

    @Override
    public Result<Object> getNewlyCounts(Long accountId) {
        List<Object> list = kuaiShouNewlyMapper.getNewlyCounts(accountId);
        if (Check.isNull(list)) {
            return Result.ok("未查询到数据");
        }
        return Result.ok(list);
    }

}
