package com.aegis.emergency.report.service.impl;

import com.aegis.common.utils.StringUtils;
import com.aegis.emergency.report.domain.ReportTemplate;
import com.aegis.emergency.report.mapper.ReportTemplateMapper;
import com.aegis.emergency.report.service.IReportTemplateService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * 上报模板 Service 实现
 * 
 * @author aegis
 */
@Service
public class ReportTemplateServiceImpl extends ServiceImpl<ReportTemplateMapper, ReportTemplate>
        implements IReportTemplateService {

    @Override
    public List<ReportTemplate> listTemplates(ReportTemplate condition) {
        LambdaQueryWrapper<ReportTemplate> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ReportTemplate::getDelFlag, "0");
        
        if (condition != null) {
            // 关键字搜索（名称或描述）
            if (StringUtils.isNotBlank(condition.getKeyword())) {
                wrapper.and(w -> w.like(ReportTemplate::getName, condition.getKeyword())
                    .or()
                    .like(ReportTemplate::getDescription, condition.getKeyword()));
            } else {
                wrapper.like(StringUtils.isNotBlank(condition.getName()), 
                    ReportTemplate::getName, condition.getName());
            }
        }
        
        wrapper.orderByDesc(ReportTemplate::getCreateTime);
        return this.list(wrapper);
    }

    /**
     * 重写 getById：自动过滤已删除的记录
     */
    @Override
    public ReportTemplate getById(Serializable id) {
        LambdaQueryWrapper<ReportTemplate> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ReportTemplate::getId, id)
            .eq(ReportTemplate::getDelFlag, "0");
        return this.getOne(wrapper);
    }

    /**
     * 重写 save：自动设置 delFlag 默认值
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(ReportTemplate entity) {
        if (entity == null) {
            return false;
        }
        if (StringUtils.isBlank(entity.getDelFlag())) {
            entity.setDelFlag("0");
        }
        return super.save(entity);
    }

    /**
     * 重写 updateById：添加业务校验
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateById(ReportTemplate entity) {
        if (entity == null || entity.getId() == null) {
            return false;
        }
        return super.updateById(entity);
    }

    /**
     * 重写 removeByIds：逻辑删除（设置 delFlag = "2"）
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @SuppressWarnings("unchecked")
    public boolean removeByIds(Collection<?> idList) {
        if (idList == null || idList.isEmpty()) {
            return false;
        }
        // 逻辑删除：批量更新 delFlag
        List<ReportTemplate> templates = super.listByIds((Collection<? extends Serializable>) idList);
        templates.forEach(t -> t.setDelFlag("2"));
        return this.updateBatchById(templates);
    }
}