package com.jsowell.pile.service.impl;

import com.google.common.collect.Lists;
import com.jsowell.common.constant.CacheConstants;
import com.jsowell.common.constant.Constants;
import com.jsowell.common.core.redis.RedisCache;
import com.jsowell.common.util.DateUtils;
import com.jsowell.common.util.StringUtils;
import com.jsowell.pile.domain.AdapayMemberAccount;
import com.jsowell.pile.mapper.AdapayMemberAccountMapper;
import com.jsowell.pile.service.AdapayMemberAccountService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

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

    @Autowired
    private RedisCache redisCache;

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

    @Override
    public List<AdapayMemberAccount> selectAdapayMemberAccountList(String merchantId) {
        if (StringUtils.isBlank(merchantId)) {
            return Lists.newArrayList();
        }
        AdapayMemberAccount adapayMemberAccount = new AdapayMemberAccount();
        adapayMemberAccount.setMerchantId(merchantId);
        return selectAdapayMemberAccountList(adapayMemberAccount);
    }

    /**
     * 新增
     *
     * @param adapayMemberAccount
     * @return 结果
     */
    @Override
    public int insertAdapayMemberAccount(AdapayMemberAccount adapayMemberAccount) {
        adapayMemberAccount.setCreateTime(DateUtils.getNowDate());
        return adapayMemberAccountMapper.insertSelective(adapayMemberAccount);
    }

    /**
     * 修改
     *
     * @param adapayMemberAccount
     * @return 结果
     */
    @Override
    public int updateAdapayMemberAccount(AdapayMemberAccount adapayMemberAccount) {
        adapayMemberAccount.setUpdateTime(DateUtils.getNowDate());
        int i = adapayMemberAccountMapper.updateByPrimaryKeySelective(adapayMemberAccount);
        if (StringUtils.isNotBlank(adapayMemberAccount.getMerchantId())) {
            String redisKey = CacheConstants.ADAPAY_MEMBER_ACCOUNT + adapayMemberAccount.getMerchantId();
            redisCache.deleteObject(redisKey);
        }
        return i;
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteAdapayMemberAccountByIds(List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return 0;
        }
        return adapayMemberAccountMapper.deleteAdapayMemberAccountByIds(ids);
    }

    @Override
    public AdapayMemberAccount selectByMerchantId(String merchantId) {
        if (StringUtils.isBlank(merchantId)) {
            return null;
        }
        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_10m);
            }
        }
        return adapayMemberAccount;
    }


    @Override
    public AdapayMemberAccount selectByMemberId(String memberId) {
        return adapayMemberAccountMapper.selectByMemberId(memberId);
    }

    @Override
    public void updateAdapayMemberAccountByMemberId(AdapayMemberAccount adapayMemberAccount) {
        adapayMemberAccountMapper.updateAdapayMemberAccountByMemberId(adapayMemberAccount);
    }

    @Override
    public AdapayMemberAccount getDefault() {
        AdapayMemberAccount account = new AdapayMemberAccount();
        account.setAdapayMemberId(Constants.ZERO);
        account.setRemark(Constants.DEFAULT);
        return account;
    }

    /**
     * 根据运营商id逻辑删除审核未通过的汇付会员账户
     * @param merchantId 运营商id
     * @return
     */
    @Override
    public int deleteAuditFailed(String merchantId) {
        int result = 0;
        // 查询运营商所有的汇付会员账号配置信息
        List<AdapayMemberAccount> accountList = this.selectAdapayMemberAccountList(merchantId);
        if (CollectionUtils.isNotEmpty(accountList)) {
            List<String> ids = accountList.stream()
                    .filter(x -> StringUtils.equals(x.getStatus(), Constants.TWO))
                    .map(x -> x.getId() + "")
                    .collect(Collectors.toList());
            result = this.deleteAdapayMemberAccountByIds(ids);
            // 清缓存
            String redisKey = CacheConstants.ADAPAY_MEMBER_ACCOUNT + merchantId;
            redisCache.deleteObject(redisKey);
        }
        return result;
    }


    @Override
    public int insert(AdapayMemberAccount record) {
        return adapayMemberAccountMapper.insert(record);
    }

    @Override
    public int insertOrUpdate(AdapayMemberAccount record) {
        return adapayMemberAccountMapper.insertOrUpdate(record);
    }

    @Override
    public int insertOrUpdateSelective(AdapayMemberAccount record) {
        return adapayMemberAccountMapper.insertOrUpdateSelective(record);
    }

    @Override
    public int insertSelective(AdapayMemberAccount record) {
        return adapayMemberAccountMapper.insertSelective(record);
    }

    @Override
    public AdapayMemberAccount selectByPrimaryKey(Integer id) {
        return adapayMemberAccountMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(AdapayMemberAccount record) {
        return adapayMemberAccountMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(AdapayMemberAccount record) {
        return adapayMemberAccountMapper.updateByPrimaryKey(record);
    }

    @Override
    public int updateBatch(List<AdapayMemberAccount> list) {
        return adapayMemberAccountMapper.updateBatch(list);
    }

    @Override
    public int batchInsert(List<AdapayMemberAccount> list) {
        return adapayMemberAccountMapper.batchInsert(list);
    }
}

