package cn.iocoder.yudao.module.basic.api.redis;

import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.basic.api.redis.dto.BusinessInfoDTO;
import cn.iocoder.yudao.module.basic.api.redis.dto.DealerInfoDTO;
import cn.iocoder.yudao.module.basic.api.redis.dto.GoodsInfoDTO;
import cn.iocoder.yudao.module.basic.api.vendorinfo.dto.VendorInfoRespDTO;
import cn.iocoder.yudao.module.basic.convert.businessinfo.BusinessInfoConvert;
import cn.iocoder.yudao.module.basic.convert.dealerinfo.DealerInfoConvert;
import cn.iocoder.yudao.module.basic.convert.goodsinfo.GoodsInfoConvert;
import cn.iocoder.yudao.module.basic.convert.vendorinfo.VendorInfoConvert;
import cn.iocoder.yudao.module.basic.dal.mysql.businessinfo.BusinessInfoMapper;
import cn.iocoder.yudao.module.basic.dal.mysql.dealerinfo.DealerInfoMapper;
import cn.iocoder.yudao.module.basic.dal.mysql.goodsinfo.GoodsInfoMapper;
import cn.iocoder.yudao.module.basic.dal.mysql.vendorinfo.VendorInfoMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static cn.iocoder.yudao.module.system.enums.ApiConstants.VERSION;

/**
 * @ClassName getBusinessInfoRedis
 * @Description TODO
 * @Author q5932
 * @Date 2023/4/27 14:12
 * @Version 1.0
 **/
@RestController // 提供 RESTful API 接口，给 Feign 调用
@DubboService(version = VERSION) // 提供 Dubbo RPC 接口，给 Dubbo Consumer 调用
public class RedisBasicInfoUtilImpl implements RedisBasicInfoUtilApi {
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private BusinessInfoMapper businessInfoMapper;
    @Resource
    private VendorInfoMapper vendorInfoMapper;
    @Resource
    private DealerInfoMapper dealerInfoMapper;
    @Resource
    private GoodsInfoMapper goodsInfoMapper;

    /**
     * 初始化
     */
//    @PostConstruct
//    public void init() {
//        redisUtilImpl = this;
//        redisUtilImpl.redisTemplate = this.redisTemplate;
//    }
    /*----------------------------------------商户信息Redis缓存方法----------------------------------------*/
    @Override
    public boolean validateBusinessInfoRedisExists(Long id) {
        String tenantId = String.valueOf(TenantContextHolder.getRequiredTenantId());

        if (!redisTemplate.hasKey("BusinessInfo:" + tenantId)) {
            HashMap<String, BusinessInfoDTO> map = new HashMap<>();
            List<BusinessInfoDTO> businessInfoDOList = BusinessInfoConvert.INSTANCE.convertToDTOList(businessInfoMapper.selectList());
            businessInfoDOList.forEach(item -> map.put(String.valueOf(item.getId()), item));
            System.out.println("插入Redis," + "BusinessInfo:" + tenantId);
            redisTemplate.opsForHash().putAll("BusinessInfo:" + tenantId, map);
            redisTemplate.expire("BusinessInfo:" + tenantId, 7, TimeUnit.DAYS);
        }

        if (redisTemplate.opsForHash().hasKey("BusinessInfo:" + tenantId, String.valueOf(id))) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public BusinessInfoDTO getBusinessInfoRedis(Long id) {
        String tenantId = String.valueOf(TenantContextHolder.getRequiredTenantId());

        if (!redisTemplate.hasKey("BusinessInfo:" + tenantId)) {
            HashMap<String, String> map = new HashMap<>();
            List<BusinessInfoDTO> businessInfoDOList = BusinessInfoConvert.INSTANCE.convertToDTOList(businessInfoMapper.selectList());
            businessInfoDOList.forEach(item -> map.put(String.valueOf(item.getId()), JsonUtils.toJsonString(item)));
            System.out.println("插入Redis," + "BusinessInfo:" + tenantId);
            redisTemplate.opsForHash().putAll("BusinessInfo:" + tenantId, map);
            redisTemplate.expire("BusinessInfo:" + tenantId, 7, TimeUnit.DAYS);
        }

        String result = (String) redisTemplate.opsForHash().get("BusinessInfo:" + tenantId, String.valueOf(id));
        return JsonUtils.parseObject(result, BusinessInfoDTO.class);
    }

    @Override
    public void updateBusinessInfoRedis(Long id, BusinessInfoDTO businessInfoDO) {
        String tenantId = String.valueOf(TenantContextHolder.getRequiredTenantId());

        if (!redisTemplate.hasKey("BusinessInfo:" + tenantId)) {
            System.out.println("未插入Redis," + "BusinessInfo:" + tenantId);
        } else {
            System.out.println("更新Redis," + "BusinessInfo:" + tenantId);
            redisTemplate.opsForHash().put("BusinessInfo:" + tenantId, String.valueOf(id), businessInfoDO);
            redisTemplate.expire("BusinessInfo:" + tenantId, 7, TimeUnit.DAYS);
        }
    }

    @Override
    public void delBusinessInfoRedis(Long id) {
        String tenantId = String.valueOf(TenantContextHolder.getRequiredTenantId());

        if (!redisTemplate.hasKey("BusinessInfo:" + tenantId)) {
            System.out.println("未插入Redis," + "BusinessInfo:" + tenantId);
        } else {
            System.out.println("删除Redis," + "BusinessInfo:" + tenantId);
            redisTemplate.opsForHash().delete("BusinessInfo:" + tenantId, String.valueOf(id));
        }
    }

    /*----------------------------------------供应商信息Redis缓存方法----------------------------------------*/
    @Override
    public boolean validateVendorInfoRedisExists(Long id) {
        String tenantId = String.valueOf(TenantContextHolder.getRequiredTenantId());

        if (!redisTemplate.hasKey("VendorInfo:" + tenantId)) {
            HashMap<String, VendorInfoRespDTO> map = new HashMap<>();
            List<VendorInfoRespDTO> vendorInfoRespDTOList = VendorInfoConvert.INSTANCE.convertToDTOList(vendorInfoMapper.selectList());
            vendorInfoRespDTOList.forEach(item -> map.put(String.valueOf(item.getId()), item));
            System.out.println("插入Redis," + "VendorInfo:" + tenantId);
            redisTemplate.opsForHash().putAll("VendorInfo:" + tenantId, map);
            redisTemplate.expire("VendorInfo:" + tenantId, 7, TimeUnit.DAYS);
        }

        if (redisTemplate.opsForHash().hasKey("VendorInfo:" + tenantId, String.valueOf(id))) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public VendorInfoRespDTO getVendorInfoRedis(Long id) {
        String tenantId = String.valueOf(TenantContextHolder.getRequiredTenantId());

        if (!redisTemplate.hasKey("VendorInfo:" + tenantId)) {
            HashMap<String, VendorInfoRespDTO> map = new HashMap<>();
            List<VendorInfoRespDTO> vendorInfoRespDTOList = VendorInfoConvert.INSTANCE.convertToDTOList(vendorInfoMapper.selectList());
            vendorInfoRespDTOList.forEach(item -> map.put(String.valueOf(item.getId()), item));
            System.out.println("插入Redis," + "VendorInfo:" + tenantId);
            redisTemplate.opsForHash().putAll("VendorInfo:" + tenantId, map);
            redisTemplate.expire("VendorInfo:" + tenantId, 7, TimeUnit.DAYS);
        }

        VendorInfoRespDTO o = (VendorInfoRespDTO) redisTemplate.opsForHash().get("VendorInfo:" + tenantId, String.valueOf(id));
        return o;
    }

    @Override
    public void updateVendorInfoRedis(Long id, VendorInfoRespDTO vendorInfoRespDTO) {
        String tenantId = String.valueOf(TenantContextHolder.getRequiredTenantId());

        if (!redisTemplate.hasKey("VendorInfo:" + tenantId)) {
            System.out.println("未插入Redis," + "VendorInfo:" + tenantId);
        } else {
            System.out.println("更新Redis," + "VendorInfo:" + tenantId);
            redisTemplate.opsForHash().put("VendorInfo:" + tenantId, String.valueOf(id), vendorInfoRespDTO);
            redisTemplate.expire("VendorInfo:" + tenantId, 7, TimeUnit.DAYS);
        }
    }

    @Override
    public void delVendorInfoRedis(Long id) {
        String tenantId = String.valueOf(TenantContextHolder.getRequiredTenantId());

        if (!redisTemplate.hasKey("VendorInfo:" + tenantId)) {
            System.out.println("未插入Redis," + "VendorInfo:" + tenantId);
        } else {
            System.out.println("删除Redis," + "VendorInfo:" + tenantId);
            redisTemplate.opsForHash().delete("VendorInfo:" + tenantId, String.valueOf(id));
        }
    }

    /*----------------------------------------经销商信息Redis缓存方法----------------------------------------*/
    @Override
    public boolean validateDealerInfoRedisExists(Long id) {
        String tenantId = String.valueOf(TenantContextHolder.getRequiredTenantId());

        if (!redisTemplate.hasKey("DealerInfo:" + tenantId)) {
            HashMap<String, DealerInfoDTO> map = new HashMap<>();
            List<DealerInfoDTO> dealerInfoDTOList = DealerInfoConvert.INSTANCE.convertToDTOList(dealerInfoMapper.selectList());
            dealerInfoDTOList.forEach(item -> map.put(String.valueOf(item.getId()), item));
            System.out.println("插入Redis," + "DealerInfo:" + tenantId);
            redisTemplate.opsForHash().putAll("DealerInfo:" + tenantId, map);
            redisTemplate.expire("DealerInfo:" + tenantId, 7, TimeUnit.DAYS);
        }

        if (redisTemplate.opsForHash().hasKey("DealerInfo:" + tenantId, String.valueOf(id))) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public DealerInfoDTO getDealerInfoRedis(Long id) {
        String tenantId = String.valueOf(TenantContextHolder.getRequiredTenantId());

        if (!redisTemplate.hasKey("DealerInfo:" + tenantId)) {
            HashMap<String, DealerInfoDTO> map = new HashMap<>();
            List<DealerInfoDTO> dealerInfoDTOList = DealerInfoConvert.INSTANCE.convertToDTOList(dealerInfoMapper.selectList());
            dealerInfoDTOList.forEach(item -> map.put(String.valueOf(item.getId()), item));
            System.out.println("插入Redis," + "DealerInfo:" + tenantId);
            redisTemplate.opsForHash().putAll("DealerInfo:" + tenantId, map);
            redisTemplate.expire("DealerInfo:" + tenantId, 7, TimeUnit.DAYS);
        }

        DealerInfoDTO o = (DealerInfoDTO) redisTemplate.opsForHash().get("VendorInfo:" + tenantId, String.valueOf(id));
        return o;
    }

    @Override
    public void updateDealerInfoRedis(Long id, DealerInfoDTO dealerInfoDTO) {
        String tenantId = String.valueOf(TenantContextHolder.getRequiredTenantId());

        if (!redisTemplate.hasKey("DealerInfo:" + tenantId)) {
            System.out.println("未插入Redis," + "DealerInfo:" + tenantId);
        } else {
            System.out.println("更新Redis," + "DealerInfo:" + tenantId);
            redisTemplate.opsForHash().put("DealerInfo:" + tenantId, String.valueOf(id), dealerInfoDTO);
            redisTemplate.expire("DealerInfo:" + tenantId, 7, TimeUnit.DAYS);
        }
    }

    @Override
    public void delDealerInfoRedis(Long id) {
        String tenantId = String.valueOf(TenantContextHolder.getRequiredTenantId());

        if (!redisTemplate.hasKey("DealerInfo:" + tenantId)) {
            System.out.println("未插入Redis," + "DealerInfo:" + tenantId);
        } else {
            System.out.println("删除Redis," + "DealerInfo:" + tenantId);
            redisTemplate.opsForHash().delete("DealerInfo:" + tenantId, String.valueOf(id));
        }
    }

    /*----------------------------------------商品信息Redis缓存方法----------------------------------------*/
    @Override
    public boolean validateGoodsInfoRedisExists(Long id) {
        String tenantId = String.valueOf(TenantContextHolder.getRequiredTenantId());

        if (!redisTemplate.hasKey("GoodsInfo:" + tenantId)) {
            HashMap<String, GoodsInfoDTO> map = new HashMap<>();
            List<GoodsInfoDTO> goodsInfoDTOList = GoodsInfoConvert.INSTANCE.convertToGoodsInfoDTOList(goodsInfoMapper.selectList());
            goodsInfoDTOList.forEach(item -> map.put(String.valueOf(item.getId()), item));
            System.out.println("插入Redis," + "GoodsInfo:" + tenantId);
            redisTemplate.opsForHash().putAll("GoodsInfo:" + tenantId, map);
            redisTemplate.expire("GoodsInfo:" + tenantId, 7, TimeUnit.DAYS);
        }

        if (redisTemplate.opsForHash().hasKey("GoodsInfo:" + tenantId, String.valueOf(id))) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public GoodsInfoDTO getGoodsInfoRedis(Long id) {
        String tenantId = String.valueOf(TenantContextHolder.getRequiredTenantId());

        if (!redisTemplate.hasKey("GoodsInfo:" + tenantId)) {
            HashMap<String, GoodsInfoDTO> map = new HashMap<>();
            List<GoodsInfoDTO> goodsInfoDTOList = GoodsInfoConvert.INSTANCE.convertToGoodsInfoDTOList(goodsInfoMapper.selectList());
            goodsInfoDTOList.forEach(item -> map.put(String.valueOf(item.getId()), item));
            System.out.println("插入Redis," + "GoodsInfo:" + tenantId);
            redisTemplate.opsForHash().putAll("GoodsInfo:" + tenantId, map);
            redisTemplate.expire("GoodsInfo:" + tenantId, 7, TimeUnit.DAYS);
        }

        GoodsInfoDTO o = (GoodsInfoDTO) redisTemplate.opsForHash().get("GoodsInfo:" + tenantId, String.valueOf(id));
        return o;
    }

    @Override
    public void updateGoodsInfoRedis(Long id, GoodsInfoDTO goodsInfoDTO) {
        String tenantId = String.valueOf(TenantContextHolder.getRequiredTenantId());

        if (!redisTemplate.hasKey("GoodsInfo:" + tenantId)) {
            System.out.println("未插入Redis," + "GoodsInfo:" + tenantId);
        } else {
            System.out.println("更新Redis," + "GoodsInfo:" + tenantId);
            redisTemplate.opsForHash().put("GoodsInfo:" + tenantId, String.valueOf(id), goodsInfoDTO);
            redisTemplate.expire("GoodsInfo:" + tenantId, 7, TimeUnit.DAYS);
        }
    }

    @Override
    public void delGoodsInfoRedis(Long id) {
        String tenantId = String.valueOf(TenantContextHolder.getRequiredTenantId());

        if (!redisTemplate.hasKey("GoodsInfo:" + tenantId)) {
            System.out.println("未插入Redis," + "GoodsInfo:" + tenantId);
        } else {
            System.out.println("删除Redis," + "GoodsInfo:" + tenantId);
            redisTemplate.opsForHash().delete("GoodsInfo:" + tenantId, String.valueOf(id));
        }
    }

}
