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

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liquidnet.common.cache.redis.util.RedisUtil;
import com.liquidnet.commons.lang.util.IDGenerator;
import com.liquidnet.commons.lang.util.JsonUtils;
import com.liquidnet.service.adam.constant.AdamRedisConst;
import com.liquidnet.service.adam.dto.AdamMemberPriceParam;
import com.liquidnet.service.adam.dto.vo.AdamMemberPriceVo;
import com.liquidnet.service.adam.entity.AdamMemberPrice;
import com.liquidnet.service.adam.mapper.AdamMemberPriceMapper;
import com.liquidnet.service.adam.service.admin.IAdamMemberPriceAdminService;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Slf4j
@Service
public class AdamMemberPriceAdminServiceImpl extends ServiceImpl<AdamMemberPriceMapper, AdamMemberPrice> implements IAdamMemberPriceAdminService {
    @Autowired
    AdamMemberPriceMapper adamMemberPriceMapper;
    @Autowired
    MongoTemplate mongoTemplate;
    @Autowired
    RedisUtil redisUtil;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void add(AdamMemberPrice info) {
        info.setMemberPriceId(IDGenerator.nextSnowId().toString());
        info.setCreatedAt(LocalDateTime.now());

        adamMemberPriceMapper.insert(info);
        mongoTemplate.insert(AdamMemberPriceVo.getNew().copy(info), AdamMemberPriceVo.class.getSimpleName());
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void edit(AdamMemberPriceParam param) {
        AdamMemberPrice updateInfo = new AdamMemberPrice();
        updateInfo.setState(param.getState());
        updateInfo.setName(param.getName());
        updateInfo.setPrice(param.getPrice());
        updateInfo.setPriceFixed(param.getPriceFixed());
        updateInfo.setDays(param.getDays());
        updateInfo.setDetail(param.getDetail());
        updateInfo.setUpdatedAt(LocalDateTime.now());

        String memberPriceId = param.getMemberPriceId();

        adamMemberPriceMapper.update(
                updateInfo, Wrappers.lambdaQuery(AdamMemberPrice.class).eq(AdamMemberPrice::getMemberPriceId, memberPriceId));
        mongoTemplate.getCollection(AdamMemberPriceVo.class.getSimpleName()).updateOne(
                Query.query(Criteria.where("memberPriceId").is(memberPriceId)).getQueryObject(),
                new Document("$set", Document.parse(JsonUtils.toJson(updateInfo)))
        );
        redisUtil.del(AdamRedisConst.INFO_MEMBER_PRICE.concat(memberPriceId));
    }
}
