/*
 * 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.google.common.collect.Lists;
import com.yami.shop.bean.model.Product;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.util.Json;
import com.yami.shop.distribution.comment.dao.DistributionAuditingSetMapper;
import com.yami.shop.distribution.comment.model.DistributionAuditingSet;
import com.yami.shop.distribution.comment.service.DistributionAuditingSetService;
import com.yami.shop.distribution.comment.vo.DistributionAuditingConditionsVO;
import com.yami.shop.distribution.comment.vo.DistributionAuditingInfoVO;
import com.yami.shop.service.ProductService;
import lombok.AllArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author lgh on 2019/04/01.
 */
@Service
@AllArgsConstructor
public class DistributionAuditingSetServiceImpl extends ServiceImpl<DistributionAuditingSetMapper, DistributionAuditingSet> implements DistributionAuditingSetService {

    @Autowired
    private DistributionAuditingSetMapper distributionAuditingSetMapper;

    @Autowired
    private ProductService productService;


    @Override
    @Cacheable(cacheNames = "DistributionAuditingSet", key = "'0'")
    public DistributionAuditingSet getByShopId() {
        DistributionAuditingSet distributionAuditingSet = distributionAuditingSetMapper.selectOne(new LambdaQueryWrapper<DistributionAuditingSet>()
                .eq(DistributionAuditingSet::getShopId, Constant.PLATFORM_SHOP_ID)
        );
//        if (distributionAuditingSet == null) {
//            return createInitialDistributionAuditingSet(shopId);
//        }
        distributionAuditingSet.setAuditingInfo(Json.parseObject(distributionAuditingSet.getAuditingInfoJson(), DistributionAuditingInfoVO.class));
        distributionAuditingSet.setAuditingConditions(Json.parseObject(distributionAuditingSet.getAuditingConditionsJson(), DistributionAuditingConditionsVO.class));

        DistributionAuditingConditionsVO auditingConditions = distributionAuditingSet.getAuditingConditions();
        List<Long> prodIds = auditingConditions.getProdList();
        if (CollectionUtils.isNotEmpty(prodIds)) {
            List<Product> list = Lists.newArrayList();
            for (Long prodId : prodIds) {
                Product product = productService.getProductByProdId(prodId);
                if (product != null) {
                    list.add(productService.getProductByProdId(prodId));
                }
            }
            auditingConditions.setProductDtoList(list);
        }
        return distributionAuditingSet;
    }

//    @Override
//    public DistributionAuditingSet createInitialDistributionAuditingSet(Long shopId) {
//        DistributionAuditingSet distributionAuditingSet = new DistributionAuditingSet();
//        distributionAuditingSet.setShopId(shopId);
//        distributionAuditingSet.setAuditingConditions(new DistributionAuditingConditionsVO());
//        distributionAuditingSet.setAuditingInfo(new DistributionAuditingInfoVO());
//        distributionAuditingSet.setAutoCheck(0);
//        distributionAuditingSetMapper.insert(distributionAuditingSet);
//        return distributionAuditingSet;
//    }

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

    }
}
