package com.czg.service.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.czg.service.system.mapper.SysParamsMapper;
import com.czg.system.dto.SysParamsDTO;
import com.czg.system.entity.SysParams;
import com.czg.system.service.SysParamsService;
import com.czg.utils.AssertUtil;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;

import java.util.ArrayList;
import java.util.List;

/**
 * 服务层实现。
 *
 * @author mac
 * @since 2025-02-07
 */
@DubboService
@CacheConfig(cacheNames = "params")
public class SysParamsServiceImpl extends ServiceImpl<SysParamsMapper, SysParams> implements SysParamsService {

    @Override
    public CzgResult<String> insertParams(SysParamsDTO paramsDTO) {
        // 查询 paramCode 是否存在
        SysParams sysParams = getOne(new QueryWrapper().eq(SysParams::getParamCode, paramsDTO.getParamCode()));
        if (sysParams != null) {
            return CzgResult.failure("参数编码已存在");
        }

        Long userId = StpKit.USER.getLoginId(1L);

        // 新增参数
        sysParams = new SysParams();
        sysParams.setParamCode(paramsDTO.getParamCode())
                .setParamValue(paramsDTO.getParamValue())
                .setParamType(paramsDTO.getParamType())
                .setRemark(paramsDTO.getRemark())
                .setCreateUserId(userId);

        save(sysParams);

        return CzgResult.success(sysParams.getParamCode());
    }

    @Override
    @CacheEvict(key = "#paramsDTO.paramCode")
    public CzgResult<String> updateParams(SysParamsDTO paramsDTO) {
        // 查询 paramCode 是否存在
        SysParams sysParams = getOne(new QueryWrapper().eq(SysParams::getParamCode, paramsDTO.getParamCode())
                .ne(SysParams::getParamCode, paramsDTO.getParamCode()));
        if (sysParams != null) {
            return CzgResult.failure("参数编码已存在");
        }

        Long userId = StpKit.USER.getLoginId(1L);

        // 修改参数
        sysParams = getById(paramsDTO.getParamCode());

        if (!sysParams.getParamCode().equals(paramsDTO.getParamCode())) {
            return CzgResult.failure("参数编码不能修改");
        }
        sysParams.setParamValue(paramsDTO.getParamValue())
                .setParamType(paramsDTO.getParamType())
                .setRemark(paramsDTO.getRemark())
                .setUpdateUserId(userId);

        updateById(sysParams);

        return CzgResult.success(sysParams.getParamCode());
    }

    @Override
    @CacheEvict(key = "#code")
    public CzgResult<Boolean> deleteParams(String code) {
        SysParams sysParams = getById(code);
        if (sysParams == null) {
            return CzgResult.failure("参数不存在");
        }

        removeById(code);
        return CzgResult.success(true);
    }

    @Override
    public CzgResult<SysParamsDTO> getParamsByCode(String code) {
        SysParams sysParams = getMapper().getSysParam(code);
        if (sysParams == null) {
            return CzgResult.failure("参数不存在");
        }

        SysParamsDTO sysParamsDTO = BeanUtil.toBean(sysParams, SysParamsDTO.class);

        return CzgResult.success(sysParamsDTO);
    }

    @Override
    public SysParams getSysParam(String code) {
        return getMapper().getSysParam(code);
    }

    @Override
    public String getSysParamValue(String code) {
        SysParams sysParam = getSysParam(code);
        AssertUtil.isNull(sysParam, "参数不存在");
        AssertUtil.isBlank(sysParam.getParamValue(), "参数值为空");
        return sysParam.getParamValue();
    }

    @Override
    public CzgResult<List<SysParamsDTO>> getParamsByType(Integer type) {
        List<SysParams> sysParamsList = list(new QueryWrapper().eq(SysParams::getParamType, type));
        List<SysParamsDTO> sysParamsDTOList = new ArrayList<>();
        for (SysParams sysParams : sysParamsList) {
            sysParamsDTOList.add(BeanUtil.toBean(sysParams, SysParamsDTO.class));
        }
        return CzgResult.success(sysParamsDTOList);
    }
}
