/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.distribution.comment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.util.Json;
import com.yami.shop.distribution.comment.dao.DistributionBindSetMapper;
import com.yami.shop.distribution.comment.model.DistributionBindSet;
import com.yami.shop.distribution.comment.service.DistributionBindSetService;
import com.yami.shop.distribution.comment.vo.DistributionBindSceneVO;
import com.yami.shop.distribution.comment.vo.DistributionBindSetRobVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 *
 * @author lgh on 2019/04/01.
 */
@Service
public class DistributionBindSetServiceImpl extends ServiceImpl<DistributionBindSetMapper, DistributionBindSet> implements DistributionBindSetService {

    @Autowired
    private DistributionBindSetMapper distributionBindSetMapper;

    @Override
    @Cacheable(cacheNames = "distribution_bind_set", key = "'0'")
    public DistributionBindSet getByShopId() {
        DistributionBindSet distributionBindSet=distributionBindSetMapper.selectOne(new LambdaQueryWrapper<DistributionBindSet>()
            .eq(DistributionBindSet::getShopId, Constant.PLATFORM_SHOP_ID)
        );
//        if(distributionBindSet == null){
//            return createInitialDistributionBindSet(shopId);
//        }
        distributionBindSet.setRob(Json.parseObject(distributionBindSet.getRobJson(), DistributionBindSetRobVO.class));
        distributionBindSet.setBindScene(Json.parseObject(distributionBindSet.getBindSceneJson(), DistributionBindSceneVO.class));
        return distributionBindSet;
    }

//    @Override
//    public DistributionBindSet createInitialDistributionBindSet(Long shopId) {
//        DistributionBindSet distributionBindSet=new DistributionBindSet();
//        distributionBindSet.setShopId(shopId);
//        distributionBindSet.setAttribution(0);
//        distributionBindSet.setRob(new DistributionBindSetRobVO());
//        distributionBindSet.setRobJson(Json.toJsonString(distributionBindSet.getRob()));
//        distributionBindSet.setProtectDay(0);
//        distributionBindSet.setBindScene(new DistributionBindSceneVO());
//        distributionBindSet.setBindSceneJson(Json.toJsonString(distributionBindSet.getBindScene()));
//        distributionBindSetMapper.insert(distributionBindSet);
//        return distributionBindSet;
//    }

    @Override
    @CacheEvict(cacheNames = "distribution_bind_set", key = "'0'")
    public void removeCacheByShopId(Long shopId) {

    }
}
