package com.jeesharp.business.modules.setting.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.jeesharp.business.modules.setting.entity.HelpArticle;
import com.jeesharp.business.modules.setting.mapper.HelpArticleMapper;
import com.jeesharp.business.modules.setting.service.HelpArticleService;
import com.jeesharp.business.modules.setting.vo.HelpArticleQueryVO;
import com.jeesharp.framework.mybatis.core.service.CrudService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 帮助文章服务实现类
 *
 * @author jeesharp
 * @version 2026-02-05
 */
@Service
@Transactional(readOnly = true)
public class HelpArticleServiceImpl extends CrudService<HelpArticleMapper, HelpArticle, HelpArticleQueryVO> implements HelpArticleService {

    private static final int DEFAULT_ENABLED_STATUS = 1;
    private static final int DEFAULT_SORT_ORDER = 0;
    private static final int DEFAULT_FAQ = 0;

    @Override
    public List<HelpArticle> listByApp(String app) {
        if (app == null || app.trim().isEmpty()) {
            return List.of();
        }
        LambdaQueryWrapper<HelpArticle> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(HelpArticle::getApp, app);
        wrapper.eq(HelpArticle::getStatus, 1); // 只查询启用的
        wrapper.orderByAsc(HelpArticle::getSortOrder);
        wrapper.orderByDesc(HelpArticle::getCreateDate);
        return this.list(wrapper);
    }

    @Override
    @Transactional(readOnly = false)
    public void createArticle(HelpArticle article) {
        if (article.getStatus() == null) {
            article.setStatus(DEFAULT_ENABLED_STATUS);
        }
        if (article.getSortOrder() == null) {
            article.setSortOrder(DEFAULT_SORT_ORDER);
        }
        if (article.getIsFaq() == null) {
            article.setIsFaq(DEFAULT_FAQ);
        }
        save(article);
    }

    @Override
    @Transactional(readOnly = false)
    public void updateArticle(HelpArticle article) {
        update(article);
    }

    @Override
    @Transactional(readOnly = false)
    public void updateStatus(String id, Integer status) {
        HelpArticle article = get(id);
        if (article != null) {
            article.setStatus(status);
            update(article);
        }
    }

    @Override
    public List<HelpArticle> listByCategoryId(String categoryId) {
        if (categoryId == null || categoryId.trim().isEmpty()) {
            return List.of();
        }
        LambdaQueryWrapper<HelpArticle> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(HelpArticle::getCategoryId, categoryId);
        wrapper.eq(HelpArticle::getStatus, 1); // 只查询启用的
        wrapper.orderByAsc(HelpArticle::getSortOrder);
        wrapper.orderByDesc(HelpArticle::getCreateDate);
        return this.list(wrapper);
    }

    @Override
    public List<HelpArticle> listFaqByApp(String app) {
        if (app == null || app.trim().isEmpty()) {
            return List.of();
        }
        LambdaQueryWrapper<HelpArticle> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(HelpArticle::getApp, app);
        wrapper.eq(HelpArticle::getIsFaq, 1); // 常见问题
        wrapper.eq(HelpArticle::getStatus, 1); // 只查询启用的
        wrapper.orderByAsc(HelpArticle::getSortOrder);
        wrapper.orderByDesc(HelpArticle::getCreateDate);
        return this.list(wrapper);
    }

    @Override
    @Transactional(readOnly = false)
    public void incrementViewCount(String id) {
        LambdaUpdateWrapper<HelpArticle> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(HelpArticle::getId, id);
        wrapper.setSql("view_count = view_count + 1");
        this.update(wrapper);
    }

    @Override
    @Transactional(readOnly = false)
    public void incrementLikeCount(String id) {
        LambdaUpdateWrapper<HelpArticle> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(HelpArticle::getId, id);
        wrapper.setSql("like_count = like_count + 1");
        this.update(wrapper);
    }
}
