package com.jeesharp.business.modules.setting.application;

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.setting.convert.AboutUsConvert;
import com.jeesharp.business.modules.setting.dto.AboutUsSaveDTO;
import com.jeesharp.business.modules.setting.entity.AboutUs;
import com.jeesharp.business.modules.setting.vo.AboutUsListVO;
import com.jeesharp.business.modules.setting.service.AboutUsService;
import com.jeesharp.business.modules.setting.vo.AboutUsQueryVO;
import com.jeesharp.business.modules.setting.vo.AboutUsInfoVO;
import com.jeesharp.framework.pojo.Page;
import com.jeesharp.framework.pojo.query.PageRespVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 关于我们应用服务
 *
 * @author jeesharp
 * @version 2026-02-05
 */
@Service
@Transactional
@AllArgsConstructor
public class AboutUsApplicationService {

    private final AboutUsService aboutUsService;

    /**
     * 获取关于我们详情
     *
     * @param id ID
     * @return 关于我们VO
     */
    @Transactional(readOnly = true)
    public AboutUsInfoVO getAboutUs(String id) {
        AboutUs aboutUs = aboutUsService.get(id);
        return AboutUsConvert.INSTANCE.toInfoVO(aboutUs);
    }

    /**
     * 根据应用标识获取关于我们信息
     *
     * @param app 应用标识
     * @return 关于我们VO
     */
    @Transactional(readOnly = true)
    public AboutUsInfoVO getByApp(String app) {
        AboutUs aboutUs = aboutUsService.getByApp(app);
        if (aboutUs == null) {
            return null;
        }
        return AboutUsConvert.INSTANCE.toInfoVO(aboutUs);
    }

    /**
     * 分页查询关于我们列表
     *
     * @param page  分页对象
     * @param query 查询条件
     * @return 分页结果
     */
    @Transactional(readOnly = true)
    public PageRespVO<AboutUsListVO> findPage(Page<AboutUs> page, AboutUsQueryVO query) {
        Page<AboutUs> result = aboutUsService.findPage(page, query);
        return AboutUsConvert.INSTANCE.toPageRespVO(result);
    }

    /**
     * 保存关于我们
     *
     * @param dto 保存请求
     */
    @Transactional(readOnly = false)
    public void save(AboutUsSaveDTO dto) {
        AboutUs aboutUs = AboutUsConvert.INSTANCE.toEntity(dto);
        if (StrUtil.isBlank(dto.getId())) {
            aboutUsService.createAboutUs(aboutUs);
            return;
        }
        aboutUsService.updateAboutUs(aboutUs);
    }

    /**
     * 删除关于我们
     *
     * @param ids ID，多个用逗号分隔
     */
    @Transactional(readOnly = false)
    public void delete(String ids) {
        aboutUsService.batchDelete(StrUtil.splitTrim(ids, ','));
    }

    /**
     * 更新状态
     *
     * @param id     ID
     * @param status 状态
     */
    @Transactional(readOnly = false)
    public void updateStatus(String id, Integer status) {
        aboutUsService.updateStatus(id, status);
    }

    /**
     * 禁用
     *
     * @param id ID
     */
    @Transactional(readOnly = false)
    public void disable(String id) {
        updateStatus(id, 0);
    }

    /**
     * 启用
     *
     * @param id ID
     */
    @Transactional(readOnly = false)
    public void enable(String id) {
        updateStatus(id, 1);
    }
}
