package com.cf.imes.module.system.service.systemconfig;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.cf.imes.framework.common.enums.CommonStatusEnum;
import com.cf.imes.framework.common.exception.ServiceException;
import com.cf.imes.framework.organ.core.context.OrganContextHolder;
import com.cf.imes.module.system.controller.admin.systemconfig.vo.*;
import com.cf.imes.module.system.dal.dataobject.systemconfig.SystemConfigDO;
import com.cf.imes.module.system.dal.mysql.systemconfig.SystemConfigMapper;
import com.cf.imes.module.system.dal.redis.RedisKeyConstants;
import com.cf.imes.module.system.enums.config.SystemConfigTypeEnum;
import com.cf.imes.module.system.service.systemconfig.factory.SystemConfigServiceFactory;
import com.cf.imes.module.system.service.systemconfig.factory.service.AbstractSystemConfigServiceHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import jakarta.annotation.Resource;
import java.util.List;

import static com.cf.imes.module.system.enums.ErrorCodeConstants.THIS_CONFIG_DATA_ERROR;

/**
 * 系统配置接口实现类
 *
 * @author Gqr
 * @since 2024/11/7 16:27
 */
@Slf4j
@Service
public class SystemConfigServiceImpl implements SystemConfigService {

    @Resource
    private SystemConfigMapper systemConfigMapper;

    @Resource
    private SystemConfigServiceFactory configServiceFactory;

    @Override
    public List<SystemConfigDO> getSystemConfigList(ConfigPageReqVO reqVO) {
        // 根据配置类型选择对应的service
        AbstractSystemConfigServiceHandler configService = configServiceFactory.getConfigServiceHandler(reqVO.getType());
        return configService.selectList(reqVO);
    }

    @Override
    public SystemConfigDO getSystemConfig(Long id,Integer type) {

        AbstractSystemConfigServiceHandler configService = configServiceFactory.getConfigServiceHandler(type);

        return configService.getSystemConfig(id);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void modifySystemConfig(ConfigSaveReqVO reqVO) {
        // 根据配置类型选择对应的service
        AbstractSystemConfigServiceHandler configService = configServiceFactory.getConfigServiceHandler(reqVO.getType());

        configService.modifySystemConfig(reqVO);

    }

    @Override
    public void updateStatus(ConfigUpdateStatusReqVO reqVO) {
        // 根据配置类型选择对应的service
        AbstractSystemConfigServiceHandler configService = configServiceFactory.getConfigServiceHandler(reqVO.getType());

        configService.updateConfigStatus(reqVO);

    }


    @Override
    public void updateConfigSort(List<ConfigUpdateSortReqVO> reqVO) {

        if(reqVO.isEmpty()){
            return;
        }

        Integer type = reqVO.stream().map(ConfigUpdateSortReqVO::getType).findFirst().orElseThrow(() -> new ServiceException(THIS_CONFIG_DATA_ERROR));

        // 根据配置类型选择对应的service
        AbstractSystemConfigServiceHandler configService = configServiceFactory.getConfigServiceHandler(type);

        configService.updateConfigSort(reqVO);

    }


    @Override
    @Cacheable(cacheNames = RedisKeyConstants.SYSTEM_CONFIG, key = "'" + RedisKeyConstants.CUSTOM_PLATE_NO + "'", unless = "#result == null")
    public SystemConfigDO getOrgCustomPlateNoConfig() {
        return systemConfigMapper.selectOne(new LambdaUpdateWrapper<SystemConfigDO>()
                .eq(SystemConfigDO::getOrganId, OrganContextHolder.getOrganId())
                .eq(SystemConfigDO::getType, SystemConfigTypeEnum.CUSTOM_PLATE_NO.getType())
                .eq(SystemConfigDO::getStatus, CommonStatusEnum.ENABLE.getStatus()));
    }


    @Override
    public void deleteSystemConfig(ConfigDeleteReqVO reqVO) {
        // 根据配置类型选择对应的service
        AbstractSystemConfigServiceHandler configService = configServiceFactory.getConfigServiceHandler(reqVO.getType());
        configService.deleteConfig(reqVO);

    }

}
