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.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 com.liquidnet.service.adam.util.MemberUtil;
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;

@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 String createGiftCode(AdamMemberCodeParam param) {
        // 需要主动生成会员编号
        AdamMemberCode adamMemberCode = new AdamMemberCode();
        adamMemberCode.setType(param.getType());
        adamMemberCode.setCode(MemberUtil.freeCode());
        adamMemberCode.setMemberId(param.getMemberId());
        adamMemberCode.setMemberPriceId(param.getMemberPriceId());
        adamMemberCode.setState(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());

        return adamMemberCode.getCode();
    }
}
