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.AdPlanCreative;
import com.moka.gdtauto.mapper.AdPlanCreativeMapper;
import com.moka.gdtauto.service.AdPlanCreativeService;
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 AdPlanCreativeServiceImpl extends ServiceImpl<AdPlanCreativeMapper, AdPlanCreative> implements AdPlanCreativeService {

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchCreateAdPlanCreatives(List<AdPlanCreative> adPlanCreatives) {
        if (adPlanCreatives == null || adPlanCreatives.isEmpty()) {
            log.warn("批量创建广告计划创意失败，创意列表为空");
            return false;
        }
        
        LocalDateTime now = LocalDateTime.now();
        for (AdPlanCreative creative : adPlanCreatives) {
            creative.setCreateTime(now);
            creative.setUpdateTime(now);
            creative.setDeleted(0);
            if (creative.getStatus() == null) {
                creative.setStatus(1); // 默认待执行
            }
        }
        
        boolean result = this.saveBatch(adPlanCreatives);
        log.info("批量创建广告计划创意成功，数量: {}", adPlanCreatives.size());
        
        return result;
    }

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

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

    @Override
    public List<AdPlanCreative> getCreativesByPlanId(Long planId) {
        return this.list(new LambdaQueryWrapper<AdPlanCreative>()
                .eq(AdPlanCreative::getAdPlanId, planId)
                .eq(AdPlanCreative::getDeleted, 0)
                .orderByDesc(AdPlanCreative::getCreateTime));
    }

    @Override
    public Page<AdPlanCreative> getAdPlanCreatives(Long planId, Integer status, Integer pageNum, Integer pageSize) {
        Page<AdPlanCreative> page = new Page<>(pageNum, pageSize);
        
        LambdaQueryWrapper<AdPlanCreative> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AdPlanCreative::getDeleted, 0);
        
        if (planId != null) {
            queryWrapper.eq(AdPlanCreative::getAdPlanId, planId);
        }
        if (status != null) {
            queryWrapper.eq(AdPlanCreative::getStatus, status);
        }
        
        queryWrapper.orderByDesc(AdPlanCreative::getCreateTime);
        
        return this.page(page, queryWrapper);
    }

    @Override
    public AdPlanCreative getAdPlanCreativeById(Long creativeId) {
        return this.getOne(new LambdaQueryWrapper<AdPlanCreative>()
                .eq(AdPlanCreative::getId, creativeId)
                .eq(AdPlanCreative::getDeleted, 0));
    }

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