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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jeesharp.business.modules.setting.entity.HelpCategory;
import com.jeesharp.business.modules.setting.mapper.HelpCategoryMapper;
import com.jeesharp.business.modules.setting.service.HelpCategoryService;
import com.jeesharp.business.modules.setting.vo.HelpCategoryQueryVO;
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 HelpCategoryServiceImpl extends CrudService<HelpCategoryMapper, HelpCategory, HelpCategoryQueryVO> implements HelpCategoryService {

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

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

    @Override
    @Transactional(readOnly = false)
    public void createCategory(HelpCategory category) {
        if (category.getStatus() == null) {
            category.setStatus(DEFAULT_ENABLED_STATUS);
        }
        if (category.getSortOrder() == null) {
            category.setSortOrder(DEFAULT_SORT_ORDER);
        }
        save(category);
    }

    @Override
    @Transactional(readOnly = false)
    public void updateCategory(HelpCategory category) {
        update(category);
    }

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