package com.cyksj.service.corp.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyksj.common.exception.BusinessRuntimeException;
import com.cyksj.mapper.channel.ChannelPopularizeMapper;
import com.cyksj.mapper.corp.CorpCustomerAcquisitionLinkMapper;
import com.cyksj.mapper.corp.CorpCustomerAcquisitionLinkPopularizeMapper;
import com.cyksj.model.entity.CorpCustomerAcquisitionLink;
import com.cyksj.model.entity.CorpCustomerAcquisitionLinkPopularize;
import com.cyksj.service.corp.CorpCustomerAcquisitionLinkPopularizeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author chan
 * @date 2023/11/10 11:15
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class CorpCustomerAcquisitionLinkPopularizeServiceImpl extends ServiceImpl<CorpCustomerAcquisitionLinkPopularizeMapper, CorpCustomerAcquisitionLinkPopularize> implements CorpCustomerAcquisitionLinkPopularizeService {

    private final CorpCustomerAcquisitionLinkMapper corpCustomerAcquisitionLinkMapper;


    @Override
    public CorpCustomerAcquisitionLinkPopularize add(CorpCustomerAcquisitionLinkPopularize corpCustomerAcquisitionLinkPopularize) {
        int count = count(Wrappers.lambdaQuery(CorpCustomerAcquisitionLinkPopularize.class).eq(CorpCustomerAcquisitionLinkPopularize::getPopularizeId, corpCustomerAcquisitionLinkPopularize.getPopularizeId()));
        if (count > 0) {
            throw BusinessRuntimeException.getInstance("暂只支持创建一条获客链接.");
        }
        CorpCustomerAcquisitionLink link = corpCustomerAcquisitionLinkMapper.selectById(corpCustomerAcquisitionLinkPopularize.getCustomerAcquistionLinkId());
        if (link == null) {
            throw BusinessRuntimeException.getInstance("获客助手链接不存在.");
        }

        if (corpCustomerAcquisitionLinkPopularize.getTime()  == null || corpCustomerAcquisitionLinkPopularize.getTime() > 72) {
            corpCustomerAcquisitionLinkPopularize.setTime(24L);
        }

        save(corpCustomerAcquisitionLinkPopularize);

        return corpCustomerAcquisitionLinkPopularize;
    }
}
