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

import cn.com.ctop.kuaishou.modules.report.entity.KuaishouPlanCreate;
import cn.com.ctop.kuaishou.modules.report.mapper.KuaishouPlanCreateMapper;
import cn.com.ctop.kuaishou.modules.report.service.IKuaishouPlanCreateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 快手-计划创建记录
 *
 * @author jeecg-boot
 * 2021-11-18
 * @version V1.0
 */
@Service
@Slf4j
public class KuaishouPlanCreateServiceImpl extends ServiceImpl<KuaishouPlanCreateMapper, KuaishouPlanCreate> implements IKuaishouPlanCreateService {
    @Autowired
    private KuaishouPlanCreateMapper planCreateMapper;

    @Override
    public void cleanCreateDetail(Long projectId, String userId, String userName, Long accountId, String statDate, Date startDate, Date endDate) {
        Map<String, Object> deleteMap = new HashMap<>();
        deleteMap.put("account_id", accountId);
        deleteMap.put("stat_date", statDate);
        planCreateMapper.deleteByMap(deleteMap);
        KuaishouPlanCreate create = new KuaishouPlanCreate();
        create.setAccountId(accountId);
        create.setUserId(userId);
        create.setUserName(userName);
        create.setProjectId(projectId);
        create.setStatDate(statDate);
        String companyId = planCreateMapper.getCompanyId(userId);
        create.setCompanyId(companyId);
        String leaderName = planCreateMapper.getLeaderNameByUserId(userId);
        create.setLeaderName(leaderName);
        Integer planCount = planCreateMapper.getNewPlan(accountId, startDate, endDate);
        Integer newUnitCount = planCreateMapper.getNewUnit(accountId, startDate, endDate);
        if (planCount == 0 && newUnitCount == 0) {
            return;
        }
        create.setNewPlan(planCount);
        create.setNewUnit(newUnitCount);
        Integer autoUnitCount = planCreateMapper.getAutoUnit(accountId, startDate, endDate);
        create.setAutoUnit(autoUnitCount);

        // 智能托管新建计划数（助手）
        Integer hostPlan = planCreateMapper.getHostPlan(accountId, startDate, endDate);
        create.setHostingPlans(hostPlan);

        // 批量工具创建组数量
        Integer batch = planCreateMapper.getBatchUnit(accountId, startDate, endDate);
        if (batch != 0) {
            Integer batchCount = batch - autoUnitCount;
            if (batchCount >= 0) {
                create.setBatchUnit(batchCount);
            } else {
                create.setBatchUnit(0);
            }
        } else {
            create.setBatchUnit(0);
        }
        // 媒体后台创建组数量 （不包含助手智能托管组）
        Integer mediaCount = planCreateMapper.getMediaCount(accountId, startDate, endDate);
        create.setMediaUnit(mediaCount);

        Integer effectiveUnitCount = planCreateMapper.getEffectiveUnit(accountId, startDate, endDate);
        create.setEffectiveUnit(effectiveUnitCount);
        int insert = planCreateMapper.insert(create);
        if (insert > 0) {
            log.info("账户创建汇总清洗完成，accountId：{}，stat_date：{}", accountId, statDate);
        }
    }
}
