package org.xyzh.workcase.service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.xyzh.api.workcase.dto.TbWordCloudDTO;
import org.xyzh.api.workcase.service.WorkcaseChatService;
import org.xyzh.common.core.domain.ResultDomain;
import org.xyzh.common.core.page.PageDomain;
import org.xyzh.common.core.page.PageParam;
import org.xyzh.common.core.page.PageRequest;
import org.xyzh.common.utils.id.IdUtil;
import org.xyzh.workcase.mapper.TbWordCloudMapper;

@DubboService(version = "1.0.0",group = "workcase",timeout = 30000,retries = 0)
public class WorkcaseChatServiceImpl implements WorkcaseChatService{

    private static final Logger logger = LoggerFactory.getLogger(WorkcaseChatServiceImpl.class);

    @Autowired
    private TbWordCloudMapper wordCloudMapper;

    // =============================== 词云管理 ==========================

    @Override
    public ResultDomain<TbWordCloudDTO> addWordCloud(TbWordCloudDTO wordCloud) {
        logger.info("添加词云: word={}", wordCloud.getWord());
        
        if (wordCloud.getWordId() == null || wordCloud.getWordId().isEmpty()) {
            wordCloud.setWordId(IdUtil.generateUUID());
        }
        if (wordCloud.getStatDate() == null || wordCloud.getStatDate().isEmpty()) {
            wordCloud.setStatDate(LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE));
        }
        if (wordCloud.getFrequency() == null) {
            wordCloud.setFrequency("1");
        }
        
        TbWordCloudDTO queryFilter = new TbWordCloudDTO();
        queryFilter.setWord(wordCloud.getWord());
        queryFilter.setCategory(wordCloud.getCategory());
        queryFilter.setStatDate(wordCloud.getStatDate());
        TbWordCloudDTO existing = wordCloudMapper.selectWordCloudOne(queryFilter);
        
        if (existing != null) {
            wordCloudMapper.incrementFrequency(existing.getWordId(), Integer.parseInt(wordCloud.getFrequency()));
            TbWordCloudDTO updated = wordCloudMapper.selectWordCloudById(existing.getWordId());
            return ResultDomain.success("词频更新成功", updated);
        }
        
        int rows = wordCloudMapper.insertWordCloud(wordCloud);
        if (rows > 0) {
            return ResultDomain.success("添加成功", wordCloud);
        }
        return ResultDomain.failure("添加失败");
    }

    @Override
    public ResultDomain<TbWordCloudDTO> updateWordCloud(TbWordCloudDTO wordCloud) {
        logger.info("更新词云: wordId={}", wordCloud.getWordId());
        
        int rows = wordCloudMapper.updateWordCloud(wordCloud);
        if (rows > 0) {
            TbWordCloudDTO updated = wordCloudMapper.selectWordCloudById(wordCloud.getWordId());
            return ResultDomain.success("更新成功", updated);
        }
        return ResultDomain.failure("更新失败");
    }

    @Override
    public ResultDomain<TbWordCloudDTO> getWordCloudList(TbWordCloudDTO filter) {
        List<TbWordCloudDTO> list = wordCloudMapper.selectWordCloudList(filter);
        return ResultDomain.success("查询成功", list);
    }

    @Override
    public ResultDomain<TbWordCloudDTO> getWordCloudPage(PageRequest<TbWordCloudDTO> pageRequest) {
        TbWordCloudDTO filter = pageRequest.getFilter();
        PageParam pageParam = pageRequest.getPageParam();
        
        List<TbWordCloudDTO> list = wordCloudMapper.selectWordCloudPage(filter, pageParam);
        long total = wordCloudMapper.countWordClouds(filter);
        
        pageParam.setTotal((int) total);
        PageDomain<TbWordCloudDTO> pageDomain = new PageDomain<>(pageParam, list);
        return ResultDomain.success("查询成功", pageDomain);
    }

}
