package com.moka.gdtauto.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moka.gdtauto.entity.AdPlan;
import com.moka.gdtauto.mapper.AdPlanMapper;
import com.moka.gdtauto.service.AdPlanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 广告计划服务实现类
 * 
 * @author moka
 * @since 2026-02-24
 */
@Slf4j
@Service
public class AdPlanServiceImpl extends ServiceImpl<AdPlanMapper, AdPlan> implements AdPlanService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createAdPlan(AdPlan adPlan) {
        adPlan.setCreateTime(LocalDateTime.now());
        adPlan.setUpdateTime(LocalDateTime.now());
        adPlan.setDeleted(0);
        if (adPlan.getStatus() == null) {
            adPlan.setStatus(1); // 默认待执行
        }
        
        this.save(adPlan);
        log.info("创建广告计划成功，计划ID: {}, 广告组名称: {}", adPlan.getId(), adPlan.getAdgroupName());
        
        return adPlan.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateAdPlan(AdPlan adPlan) {
        AdPlan existingPlan = this.getById(adPlan.getId());
        
        if (existingPlan == null || existingPlan.getDeleted() == 1) {
            log.warn("广告计划不存在，计划ID: {}", adPlan.getId());
            return false;
        }
        
        adPlan.setUpdateTime(LocalDateTime.now());
        boolean result = this.updateById(adPlan);
        log.info("更新广告计划成功，计划ID: {}", adPlan.getId());
        
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteAdPlan(Long planId) {
        AdPlan existingPlan = this.getById(planId);
        
        if (existingPlan == null || existingPlan.getDeleted() == 1) {
            log.warn("广告计划不存在，计划ID: {}", planId);
            return false;
        }
        
        existingPlan.setDeleted(1);
        existingPlan.setUpdateTime(LocalDateTime.now());
        boolean result = this.updateById(existingPlan);
        
        log.info("删除广告计划成功，计划ID: {}", planId);
        return result;
    }

    @Override
    public Page<AdPlan> getAdPlans(Long confId, Long accountId, Integer status, Integer pageNum, Integer pageSize) {
        Page<AdPlan> page = new Page<>(pageNum, pageSize);
        
        LambdaQueryWrapper<AdPlan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AdPlan::getDeleted, 0);
        if (confId != null) {
            queryWrapper.eq(AdPlan::getAdPlanConfId, confId);
        }
        
        if (accountId != null) {
            queryWrapper.eq(AdPlan::getAccountId, accountId);
        }
        if (status != null) {
            queryWrapper.eq(AdPlan::getStatus, status);
        }
        
        queryWrapper.orderByDesc(AdPlan::getCreateTime);
        
        return this.page(page, queryWrapper);
    }

    @Override
    public AdPlan getAdPlanById(Long planId) {
        return this.getOne(new LambdaQueryWrapper<AdPlan>()
                .eq(AdPlan::getId, planId)
                .eq(AdPlan::getDeleted, 0));
    }

    @Override
    public List<AdPlan> getPlansByConfId(Long confId) {
        return this.list(new LambdaQueryWrapper<AdPlan>()
                .eq(AdPlan::getAdPlanConfId, confId)
                .eq(AdPlan::getDeleted, 0).orderByAsc(AdPlan::getId));
    }
}
