package com.jsowell.thirdparty.service.impl;

import com.jsowell.common.constant.CacheConstants;
import com.jsowell.common.core.redis.RedisCache;
import com.jsowell.common.exception.BusinessException;
import com.jsowell.common.util.DateUtils;
import com.jsowell.pile.mapper.ThirdPartyStationRelationMapper;
import com.jsowell.pile.vo.ThirdPartySecretInfoVO;
import com.jsowell.pile.vo.base.StationInfoVO;
import com.jsowell.thirdparty.domain.ThirdpartySecretInfo;
import com.jsowell.thirdparty.mapper.ThirdpartySecretInfoMapper;
import com.jsowell.thirdparty.service.ThirdpartySecretInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 对接三方平台配置Service业务层处理
 *
 * @author jsowell
 * @date 2024-04-18
 */
@Service
public class ThirdpartySecretInfoServiceImpl implements ThirdpartySecretInfoService {
    @Autowired
    private ThirdpartySecretInfoMapper thirdpartySecretInfoMapper;

    @Autowired
    private ThirdPartyStationRelationMapper thirdPartyStationRelationMapper;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询对接三方平台配置
     *
     * @param id 对接三方平台配置主键
     * @return 对接三方平台配置
     */
    @Override
    public ThirdpartySecretInfo selectThirdpartySecretInfoById(Long id) {
        return thirdpartySecretInfoMapper.selectThirdpartySecretInfoById(id);
    }

    /**
     * 查询对接三方平台配置列表
     *
     * @param thirdpartySecretInfo 对接三方平台配置
     * @return 对接三方平台配置
     */
    @Override
    public List<ThirdpartySecretInfo> selectThirdpartySecretInfoList(ThirdpartySecretInfo thirdpartySecretInfo) {
        return thirdpartySecretInfoMapper.selectThirdpartySecretInfoList(thirdpartySecretInfo);
    }

    /**
     * 新增对接三方平台配置
     *
     * @param thirdpartySecretInfo 对接三方平台配置
     * @return 结果
     */
    @Override
    public int insertThirdpartySecretInfo(ThirdpartySecretInfo thirdpartySecretInfo) {
        thirdpartySecretInfo.setCreateTime(DateUtils.getNowDate());
        return thirdpartySecretInfoMapper.insertThirdpartySecretInfo(thirdpartySecretInfo);
    }

    /**
     * 修改对接三方平台配置
     *
     * @param thirdpartySecretInfo 对接三方平台配置
     * @return 结果
     */
    @Override
    public int updateThirdpartySecretInfo(ThirdpartySecretInfo thirdpartySecretInfo) {
        thirdpartySecretInfo.setUpdateTime(DateUtils.getNowDate());
        return thirdpartySecretInfoMapper.updateThirdpartySecretInfo(thirdpartySecretInfo);
    }

    /**
     * 批量删除对接三方平台配置
     *
     * @param ids 需要删除的对接三方平台配置主键
     * @return 结果
     */
    @Override
    public int deleteThirdpartySecretInfoByIds(Long[] ids) {
        return thirdpartySecretInfoMapper.deleteThirdpartySecretInfoByIds(ids);
    }

    /**
     * 删除对接三方平台配置信息
     *
     * @param id 对接三方平台配置主键
     * @return 结果
     */
    @Override
    public int deleteThirdpartySecretInfoById(Long id) {
        return thirdpartySecretInfoMapper.deleteThirdpartySecretInfoById(id);
    }

    /**
     * 查询第三方平台密钥配置信息
     * @param theirOperatorId 第三方平台的组织结构代码
     * @return
     */
    @Override
    public ThirdPartySecretInfoVO queryByOperatorId(String theirOperatorId) {
        // 加缓存
        String redisKey = CacheConstants.THIRD_PARTY_SECRET_INFO_BY_ID + theirOperatorId;
        ThirdPartySecretInfoVO result = redisCache.getCacheObject(redisKey);
        if (Objects.isNull(result)) {
            result = thirdpartySecretInfoMapper.queryByOperatorId(theirOperatorId);
            if (Objects.nonNull(result)) {
                redisCache.setCacheObject(redisKey, result, CacheConstants.cache_expire_time_1h);
            }
        }
        return result;
    }

    @Override
    public ThirdPartySecretInfoVO queryByThirdPlatformType(String thirdPlatformType) {
        // 加缓存
        String redisKey = CacheConstants.THIRD_PARTY_SECRET_INFO_BY_TYPE + thirdPlatformType;
        ThirdPartySecretInfoVO result = redisCache.getCacheObject(redisKey);
        if (Objects.isNull(result)) {
            result = thirdpartySecretInfoMapper.queryByThirdPlatformType(thirdPlatformType);
            if (Objects.nonNull(result)) {
                redisCache.setCacheObject(redisKey, result, CacheConstants.cache_expire_time_1h);
            }
        }
        return result;
    }

    @Override
    public List<ThirdPartySecretInfoVO> queryStationToPlatformList(String stationId) {
        List<ThirdPartySecretInfoVO> list = thirdpartySecretInfoMapper.queryStationToPlatformList(stationId);
        return list;
    }

    @Override
    public List<StationInfoVO> selectStationList(String thirdPlatformType) {
        return thirdPartyStationRelationMapper.selectStationList(thirdPlatformType);
    }
}
