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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jeesharp.business.modules.setting.entity.AboutUs;
import com.jeesharp.business.modules.setting.mapper.AboutUsMapper;
import com.jeesharp.business.modules.setting.service.AboutUsService;
import com.jeesharp.business.modules.setting.vo.AboutUsQueryVO;
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 AboutUsServiceImpl extends CrudService<AboutUsMapper, AboutUs, AboutUsQueryVO> implements AboutUsService {

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

    @Override
    public AboutUs getByApp(String app) {
        if (app == null || app.trim().isEmpty()) {
            return null;
        }
        LambdaQueryWrapper<AboutUs> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AboutUs::getApp, app);
        wrapper.eq(AboutUs::getStatus, 1); // 只查询启用的
        wrapper.orderByDesc(AboutUs::getSortOrder);
        wrapper.last("LIMIT 1");
        return this.getOne(wrapper);
    }

    @Override
    @Transactional(readOnly = false)
    public void createAboutUs(AboutUs aboutUs) {
        if (aboutUs.getStatus() == null) {
            aboutUs.setStatus(DEFAULT_ENABLED_STATUS);
        }
        if (aboutUs.getSortOrder() == null) {
            aboutUs.setSortOrder(DEFAULT_SORT_ORDER);
        }
        save(aboutUs);
    }

    @Override
    @Transactional(readOnly = false)
    public void updateAboutUs(AboutUs aboutUs) {
        update(aboutUs);
    }

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