package com.backendsys.modules.system.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.backendsys.exception.CustException;
import com.backendsys.modules.system.dao.SysCommonDao;
import com.backendsys.modules.system.entity.SysCommon;
import com.backendsys.modules.system.service.SysCommonService;
import com.backendsys.utils.response.PageEntity;
import com.backendsys.utils.response.PageInfoResult;
import com.backendsys.utils.v2.PageUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class SysCommonServiceImpl implements SysCommonService {

    @Autowired
    private SysCommonDao sysCommonDao;

    /**
     * 获得系统配置列表
     */
    @Override
    public List<SysCommon> selectCommonList(String category) {
        LambdaQueryWrapper<SysCommon> wrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotEmpty(category)) wrapper.eq(SysCommon::getCategory, category);
        wrapper.orderByDesc(SysCommon::getSort);
        return sysCommonDao.selectList(wrapper);
    }

    /**
     * 获取系统配置详情
     */
    @Override
    public SysCommon selectCommonDetail(SysCommon sysCommon) {
        LambdaQueryWrapper<SysCommon> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysCommon::getId, sysCommon.getId());
        return sysCommonDao.selectOne(wrapper);
    }

    /**
     * 编辑系统配置
     */
    @Override
    public Map<String, Object> updateCommon(SysCommon sysCommon) {
        SysCommon detail = sysCommonDao.selectById(sysCommon.getId());
        if (detail == null) throw new CustException("系统配置不存在");
        sysCommonDao.updateCommon(sysCommon);
        return Map.of("id", sysCommon.getId());
    }


    // 获取系统配置值
    @Override
    @Cacheable(value = "catch::common", key = "'tag::' + #tag", unless = "#result == null")
    public Object getCommonByTag(String tag) {
        LambdaQueryWrapper<SysCommon> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysCommon::getTag, tag);
        SysCommon sysCommon = sysCommonDao.selectOne(wrapper);
        return (sysCommon != null) ? sysCommon.getValue() : null;
    }
    // 获取系统配置值
    @Override
    @Cacheable(value = "catch::common-option", key = "'tag::' + #tag", unless = "#result == null")
    public JSONArray getCommonOptionByTag(String tag) {
        LambdaQueryWrapper<SysCommon> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysCommon::getTag, tag);
        SysCommon sysCommon = sysCommonDao.selectOne(wrapper);
        return (sysCommon != null) ? JSONUtil.parseArray(sysCommon.getValue_option()) : null;
    }

    // 通过可选值组，获取对应的文本翻译
    @Override
    public Object getLabelByValue(JSONArray common_options, Object value) {
        Object label ="";
        for (Object option : common_options) {
            JSONObject option_obj = JSONUtil.parseObj(option);
            Object opt_obj_value = option_obj.get("value");
            if (opt_obj_value == value) {
                label = option_obj.get("label");
            }
        }
        return label;
    }

    // 获取系统配置值 (按分类)
    @Override
    @Cacheable(value = "catch::common", key = "'category::' + #category", unless = "#result == null")
    public List<SysCommon> getCommonByCategory(String category) {

        LambdaQueryWrapper<SysCommon> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysCommon::getCategory, category);
        List<SysCommon> sysCommon = sysCommonDao.selectList(wrapper);


        return sysCommon;
    }

}
