package com.aegis.cases.service.impl;

import com.aegis.common.utils.StringUtils;
import com.aegis.cases.domain.Case;
import com.aegis.cases.mapper.CaseMapper;
import com.aegis.cases.service.ICaseService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 案例 Service 实现
 */
@Service
public class CaseServiceImpl extends ServiceImpl<CaseMapper, Case> implements ICaseService {

    @Override
    public List<Case> listByCondition(String title, String eventType, String eventLevel, String tags, Boolean isTypical, Date beginTime, Date endTime) {
        LambdaQueryWrapper<Case> wrapper = new LambdaQueryWrapper<>();
        
        // 标题模糊搜索
        if (StringUtils.isNotBlank(title)) {
            wrapper.like(Case::getTitle, title);
        }
        
        // 事件类型筛选
        if (StringUtils.isNotBlank(eventType)) {
            wrapper.eq(Case::getEventType, eventType);
        }
        
        // 事件级别筛选
        if (StringUtils.isNotBlank(eventLevel)) {
            wrapper.eq(Case::getEventLevel, eventLevel);
        }
        
        // 标签筛选
        if (StringUtils.isNotBlank(tags)) {
            String[] tagArray = tags.split(",");
            for (String tag : tagArray) {
                if (StringUtils.isNotBlank(tag)) {
                    wrapper.like(Case::getTags, tag.trim());
                }
            }
        }
        
        // 典型案例筛选
        if (isTypical != null) {
            wrapper.eq(Case::getIsTypical, isTypical);
        }
        
        // 时间范围（按发生时间）
        if (beginTime != null) {
            wrapper.ge(Case::getOccurredTime, beginTime);
        }
        if (endTime != null) {
            wrapper.le(Case::getOccurredTime, endTime);
        }
        
        wrapper.eq(Case::getDelFlag, "0");
        wrapper.orderByDesc(Case::getOccurredTime);
        
        return this.list(wrapper);
    }

    @Override
    public boolean markTypical(Long id) {
        Case caseEntity = this.getById(id);
        if (caseEntity == null || "2".equals(caseEntity.getDelFlag())) {
            return false;
        }
        caseEntity.setIsTypical(true);
        caseEntity.setUpdateTime(new Date());
        return this.updateById(caseEntity);
    }

    @Override
    public boolean unmarkTypical(Long id) {
        Case caseEntity = this.getById(id);
        if (caseEntity == null || "2".equals(caseEntity.getDelFlag())) {
            return false;
        }
        caseEntity.setIsTypical(false);
        caseEntity.setUpdateTime(new Date());
        return this.updateById(caseEntity);
    }

    @Override
    public List<Case> listTypicalCases() {
        LambdaQueryWrapper<Case> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Case::getIsTypical, true);
        wrapper.eq(Case::getDelFlag, "0");
        wrapper.orderByDesc(Case::getOccurredTime);
        return this.list(wrapper);
    }

    @Override
    public void incrementViewCount(Long id) {
        Case caseEntity = this.getById(id);
        if (caseEntity != null && !"2".equals(caseEntity.getDelFlag())) {
            caseEntity.setViewCount((caseEntity.getViewCount() == null ? 0 : caseEntity.getViewCount()) + 1);
            caseEntity.setUpdateTime(new Date());
            this.updateById(caseEntity);
        }
    }
}

