package com.jsowell.pile.service.impl;

import com.jsowell.common.constant.CacheConstants;
import com.jsowell.common.core.redis.RedisCache;
import com.jsowell.common.util.DateUtils;
import com.jsowell.pile.domain.AdapayMemberAccount;
import com.jsowell.pile.domain.PileStationInfo;
import com.jsowell.pile.mapper.AdapayMemberAccountMapper;
import com.jsowell.pile.service.IAdapayMemberAccountService;
import com.jsowell.pile.service.IPileStationInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author jsowell
 * @date 2023-06-15
 */
@Service
public class AdapayMemberAccountServiceImpl implements IAdapayMemberAccountService {
    @Autowired
    private AdapayMemberAccountMapper adapayMemberAccountMapper;

    @Autowired
    private IPileStationInfoService pileStationInfoService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public AdapayMemberAccount selectAdapayMemberAccountById(Long id) {
        return adapayMemberAccountMapper.selectAdapayMemberAccountById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param adapayMemberAccount 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<AdapayMemberAccount> selectAdapayMemberAccountList(AdapayMemberAccount adapayMemberAccount) {
        return adapayMemberAccountMapper.selectAdapayMemberAccountList(adapayMemberAccount);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param adapayMemberAccount 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertAdapayMemberAccount(AdapayMemberAccount adapayMemberAccount) {
        adapayMemberAccount.setCreateTime(DateUtils.getNowDate());
        return adapayMemberAccountMapper.insertAdapayMemberAccount(adapayMemberAccount);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param adapayMemberAccount 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateAdapayMemberAccount(AdapayMemberAccount adapayMemberAccount) {
        adapayMemberAccount.setUpdateTime(DateUtils.getNowDate());
        return adapayMemberAccountMapper.updateAdapayMemberAccount(adapayMemberAccount);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteAdapayMemberAccountByIds(Long[] ids) {
        return adapayMemberAccountMapper.deleteAdapayMemberAccountByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteAdapayMemberAccountById(Long id) {
        return adapayMemberAccountMapper.deleteAdapayMemberAccountById(id);
    }

    @Override
    public AdapayMemberAccount selectByMerchantId(String merchantId) {
        String redisKey = CacheConstants.ADAPAY_MEMBER_ACCOUNT + merchantId;
        AdapayMemberAccount adapayMemberAccount = redisCache.getCacheObject(redisKey);
        if (adapayMemberAccount == null) {
            adapayMemberAccount = adapayMemberAccountMapper.selectByMerchantId(merchantId);
            if (adapayMemberAccount != null) {
                redisCache.setCacheObject(redisKey, adapayMemberAccount, CacheConstants.cache_expire_time_1h);
            }
        }
        return adapayMemberAccount;
    }

    @Override
    public String selectAdapayMemberIdByStationId(String stationId) {
        PileStationInfo pileStationInfo = pileStationInfoService.selectPileStationInfoById(Long.parseLong(stationId));
        if (pileStationInfo == null) {
            return null;
        }
        AdapayMemberAccount adapayMemberAccount = selectByMerchantId(pileStationInfo.getMerchantId() + "");
        if (adapayMemberAccount == null) {
            return null;
        }
        return adapayMemberAccount.getAdapayMemberId();
    }
}
