package com.liquidnet.client.admin.zhengzai.merchant.utils;

import com.liquidnet.common.cache.redis.util.RedisDataSourceUtil;
import com.liquidnet.service.merchant.constant.MerchantRedisConst;
import com.liquidnet.service.merchant.dto.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

@Slf4j
@Service
public class MerchantRedisUtil {
    @Autowired
    RedisDataSourceUtil redisDataSourceUtil;

    @Autowired
    MerchantMongoUtil mongoUtil;

    // 【场地】获取详情
    public MerchantFieldsVo getFieldsVoByFieldId(String fieldId) {
        if (null == fieldId || fieldId.isEmpty()) {
            return null;
        }
        String key = MerchantRedisConst.INFO_FIELD.concat(fieldId);
        long s = System.currentTimeMillis();
        MerchantFieldsVo vo = (MerchantFieldsVo) redisDataSourceUtil.getRedisKylinUtil().get(key);
        log.debug("#RDM耗时:{}ms", System.currentTimeMillis() - s);
        if (null == vo) {
            vo = mongoUtil.getFieldsVoByFieldId(fieldId);
            redisDataSourceUtil.getRedisKylinUtil().set(key, vo);
        }
        return vo;
    }

    // 【场地】更新详情
    public boolean setFieldsVoByFieldId(String fieldId, MerchantFieldsVo vo) {
        return redisDataSourceUtil.getRedisKylinUtil().set(MerchantRedisConst.INFO_FIELD.concat(fieldId), vo);
    }


    // 【场地申请】我的列表
    public List<MerchantFieldAppliesVo> getFieldAppliesVosByUid(String uid) {
        if (null == uid || uid.isEmpty()) {
            return null;
        }
        String key = MerchantRedisConst.INFO_FIELD_APPLIES.concat(uid);
        long s = System.currentTimeMillis();
        List<MerchantFieldAppliesVo> vos = (List<MerchantFieldAppliesVo>) redisDataSourceUtil.getRedisKylinUtil().get(key);
        log.debug("#RDM耗时:{}ms", System.currentTimeMillis() - s);
        if (null == vos) {
            vos = mongoUtil.getFieldAppliesVosByUid(uid);
            redisDataSourceUtil.getRedisKylinUtil().set(key, vos);
        }
        return vos;
    }

    // 【场地认领】更新我的列表
    public boolean setFieldAppliesVosByUid(String uid, List<MerchantFieldAppliesVo> vos) {
        return redisDataSourceUtil.getRedisKylinUtil().set(MerchantRedisConst.INFO_FIELD_APPLIES.concat(uid), vos);
    }

    // 【场地申请】新增
    public boolean addFieldAppliesVoByUid(String uid, List<MerchantFieldAppliesVo> vos, MerchantFieldAppliesVo vo) {
        if (null == vos) {
            vos = new ArrayList<>();
        }
        vos.add(vo);
        return redisDataSourceUtil.getRedisKylinUtil().set(MerchantRedisConst.INFO_FIELD_APPLIES.concat(uid), vos);
    }

    // 【场地申请】删除
    public boolean delFieldAppliesVoByUid(String uid, List<MerchantFieldAppliesVo> vos, MerchantFieldAppliesVo vo) {
        if (CollectionUtils.isEmpty(vos)) {
            return true;
        }
        vos.removeIf(r -> r.getFieldApplyId().equals(vo.getFieldApplyId()));
        return redisDataSourceUtil.getRedisKylinUtil().set(MerchantRedisConst.INFO_FIELD_APPLIES.concat(uid), vos);
    }

    // 【场地申请】更新
    public boolean setFieldAppliesVoByUid(String uid, List<MerchantFieldAppliesVo> vos, MerchantFieldAppliesVo vo) {
        if (CollectionUtils.isEmpty(vos)) {
            return false;
        }
        int idx = IntStream.range(0, vos.size())
                .filter(i -> vos.get(i).getFieldApplyId().equals(vo.getFieldApplyId()))
                .findFirst().orElse(-1);
        if (idx == -1) {
            return false;
        }
        vos.set(idx, vo);
        return redisDataSourceUtil.getRedisKylinUtil().set(MerchantRedisConst.INFO_FIELD_APPLIES.concat(uid), vos);
    }


    // 【场地默认验票员】列表
    public List<MerchantFieldCheckersVo> getFieldCheckersVosByFieldId(String fieldId) {
        String key = MerchantRedisConst.INFO_FIELD_CHECKERS.concat(fieldId);
        long s = System.currentTimeMillis();
        List<MerchantFieldCheckersVo> vos = (List<MerchantFieldCheckersVo>) redisDataSourceUtil.getRedisKylinUtil().get(key);
        log.debug("#RDM耗时:{}ms", System.currentTimeMillis() - s);
        return vos;
    }

    // 【场地默认验票员】新增
    public boolean addFieldCheckersVoByFieldId(String fieldId, List<MerchantFieldCheckersVo> vos, MerchantFieldCheckersVo vo) {
        if (null == vos) {
            vos = new ArrayList<>();
        }
        vos.add(vo);
        return redisDataSourceUtil.getRedisKylinUtil().set(MerchantRedisConst.INFO_FIELD_CHECKERS.concat(fieldId), vos);
    }

    // 【场地默认验票员】删除
    public boolean delFieldCheckersVoByFieldId(String fieldId, List<MerchantFieldCheckersVo> vos,  MerchantFieldCheckersVo vo) {
        if (CollectionUtils.isEmpty(vos)) {
            return true;
        }
        vos.removeIf(r -> r.getFieldCheckerId().equals(vo.getFieldCheckerId()));
        return redisDataSourceUtil.getRedisKylinUtil().set(MerchantRedisConst.INFO_FIELD_CHECKERS.concat(fieldId), vos);
    }

    // 【主办】详情
    public MerchantSponsorsVo getSponsorsVoBySponsorId(String sponsorId) {
        if (null == sponsorId || sponsorId.isEmpty()) {
            return null;
        }
        String key = MerchantRedisConst.INFO_SPONSOR.concat(sponsorId);
        long s = System.currentTimeMillis();
        MerchantSponsorsVo vo = (MerchantSponsorsVo) redisDataSourceUtil.getRedisKylinUtil().get(key);
        log.debug("#RDM耗时:{}ms", System.currentTimeMillis() - s);
        if (null == vo) {
            vo = mongoUtil.getSponsorsVoBySponsorId(sponsorId);
            redisDataSourceUtil.getRedisKylinUtil().set(key, vo);
        }
        return vo;
    }

    // 【主办】更新详情
    public boolean setSponsorsVoBySponsorId(String sponsorId, MerchantSponsorsVo vo) {
        return redisDataSourceUtil.getRedisKylinUtil().set(MerchantRedisConst.INFO_SPONSOR.concat(sponsorId), vo);
    }

    // 【主办申请】我的列表
    public List<MerchantSponsorAppliesVo> getSponsorAppliesVosByUid(String uid) {
        if (null == uid || uid.isEmpty()) {
            return null;
        }
        String key = MerchantRedisConst.INFO_SPONSOR_APPLIES.concat(uid);
        long s = System.currentTimeMillis();
        List<MerchantSponsorAppliesVo> vos = (List<MerchantSponsorAppliesVo>) redisDataSourceUtil.getRedisKylinUtil().get(key);
        log.debug("#RDM耗时:{}ms", System.currentTimeMillis() - s);
        if (null == vos) {
            vos = mongoUtil.getSponsorAppliesVosByUid(uid);
            redisDataSourceUtil.getRedisKylinUtil().set(key, vos);
        }
        return vos;
    }

    // 【主办申请】更新我的列表
    public boolean setSponsorAppliesVosByUid(String uid, List<MerchantSponsorAppliesVo> vos) {
        return redisDataSourceUtil.getRedisKylinUtil().set(MerchantRedisConst.INFO_SPONSOR_APPLIES.concat(uid), vos);
    }

    // 【主办申请】新增
    public boolean addSponsorAppliesVoByUid(String uid, List<MerchantSponsorAppliesVo> vos, MerchantSponsorAppliesVo vo) {
        if (null == vos) {
            vos = new ArrayList<>();
        }
        vos.add(vo);
        return redisDataSourceUtil.getRedisKylinUtil().set(MerchantRedisConst.INFO_SPONSOR_APPLIES.concat(uid), vos);
    }

    // 【主办申请】删除
    public boolean delSponsorAppliesVoByUid(String uid, List<MerchantSponsorAppliesVo> vos, MerchantSponsorAppliesVo vo) {
        if (CollectionUtils.isEmpty(vos)) {
            return true;
        }
        vos.removeIf(r -> r.getSponsorApplyId().equals(vo.getSponsorApplyId()));
        return redisDataSourceUtil.getRedisKylinUtil().set(MerchantRedisConst.INFO_SPONSOR_APPLIES.concat(uid), vos);
    }

    // 【主办申请】更新
    public boolean setSponsorAppliesVoByUid(String uid, List<MerchantSponsorAppliesVo> vos, MerchantSponsorAppliesVo vo) {
        if (CollectionUtils.isEmpty(vos)) {
            return false;
        }
        int idx = IntStream.range(0, vos.size())
                .filter(i -> vos.get(i).getSponsorApplyId().equals(vo.getSponsorApplyId()))
                .findFirst().orElse(-1);
        if (idx == -1) {
            return false;
        }
        vos.set(idx, vo);
        return redisDataSourceUtil.getRedisKylinUtil().set(MerchantRedisConst.INFO_SPONSOR_APPLIES.concat(uid), vos);
    }

}
