package com.backendsys.modules.cms.siteinfo.service.impl;

import cn.hutool.core.util.StrUtil;
import com.backendsys.modules.cms.siteinfo.dao.SiteInfoDao;
import com.backendsys.modules.cms.siteinfo.entity.SiteInfo;
import com.backendsys.modules.cms.siteinfo.service.SiteInfoService;
import com.backendsys.modules.common.config.security.utils.HttpRequestUtil;
import com.backendsys.modules.common.utils.CookieUtil;
import com.backendsys.utils.LanguageUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class SiteInfoServiceImpl implements SiteInfoService {

    @Value("${DEFAULT_LANGUAGE}")
    private String DEFAULT_LANGUAGE;

    @Autowired
    private LanguageUtil languageUtil;

    @Autowired
    private HttpRequestUtil httpRequestUtil;

    @Autowired
    private SiteInfoDao siteInfoDao;

    /**
     * 获取站点信息
     */
    @Override
    public SiteInfo selectSiteInfo() {
        LambdaQueryWrapper<SiteInfo> wrapper = new LambdaQueryWrapper();
        wrapper.eq(SiteInfo::getLanguage, languageUtil.getLang());
        return siteInfoDao.selectOne(wrapper);
    }

    /**
     * 编辑站点信息
     */
    @Override
    public Map<String, Object> updateSiteInfo(SiteInfo siteInfo) {

        HttpServletRequest request = httpRequestUtil.getRequest();
        String lang = request.getHeader("lang");
        if (StrUtil.isEmpty(lang)) lang = DEFAULT_LANGUAGE;

        LambdaQueryWrapper<SiteInfo> wrapper = new LambdaQueryWrapper();
        wrapper.eq(SiteInfo::getLanguage, lang);
        siteInfoDao.update(siteInfo, wrapper);
        return Map.of("id", siteInfo.getId());
    }

}
