package com.liquidnet.service.adam.service.impl.admin;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liquidnet.common.cache.redis.util.RedisUtil;
import com.liquidnet.commons.lang.util.DateUtil;
import com.liquidnet.service.adam.constant.AdamRedisConst;
import com.liquidnet.service.adam.dto.AdamMemberCodeParam;
import com.liquidnet.service.adam.dto.vo.AdamMemberCodeVo;
import com.liquidnet.service.adam.entity.AdamMemberCode;
import com.liquidnet.service.adam.mapper.AdamMemberCodeMapper;
import com.liquidnet.service.adam.service.admin.IAdamMemberCodeAdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.convert.MongoConverter;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class AdamMemberCodeAdminServiceImpl extends ServiceImpl<AdamMemberCodeMapper, AdamMemberCode> implements IAdamMemberCodeAdminService {

    @Autowired
    AdamMemberCodeMapper adamMemberCodeMapper;
    @Autowired
    MongoTemplate mongoTemplate;
    @Autowired
    MongoConverter mongoConverter;
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    RedisUtil redisUtil;

    @Override
    public void createGiftCode(AdamMemberCodeParam param) {
        // 需要主动生成会员编号
        AdamMemberCode adamMemberCode = (new AdamMemberCodeVo()).setCodeParam(param, 0);
        adamMemberCodeMapper.insert(adamMemberCode);

        // mongo
        AdamMemberCodeVo vo = new AdamMemberCodeVo();
        BeanUtils.copyProperties(adamMemberCode, vo);
        vo.setCreatedAt(DateUtil.format(adamMemberCode.getCreatedAt(), DateUtil.Formatter.yyyyMMddHHmmss));
        vo.setUpdatedAt(DateUtil.format(adamMemberCode.getUpdatedAt(), DateUtil.Formatter.yyyyMMddHHmmss));
        mongoTemplate.insert(vo, AdamMemberCodeVo.class.getSimpleName());
    }
}
