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

package com.yami.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.yami.shop.bean.app.dto.ProductDto;
import com.yami.shop.bean.app.dto.SkuDto;
import com.yami.shop.bean.dto.AreaDto;
import com.yami.shop.bean.dto.SearchProdDto;
import com.yami.shop.bean.model.Area;
import com.yami.shop.bean.model.ChannelProd;
import com.yami.shop.bean.model.UserShopBehavior;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.dao.*;
import com.yami.shop.service.AreaService;
import com.yami.shop.service.UserShopBehaviorService;
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.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lgh on 2018/10/26.
 */
@Service
public class UserShopBehaviorServiceImpl extends ServiceImpl<UserShopBehaviorMapper, UserShopBehavior> implements UserShopBehaviorService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private SkuMapper skuMapper;

    @Autowired
    private BasketMapper basketMapper;

    @Override
    public int findByUserIdCount(String userId) {

        return  baseMapper.findByUserIdCount(userId);
    }

    @Override
    public List<UserShopBehavior> findByUserId(String userId,Integer num, Long shopId, Long channelId) {
        return baseMapper.findByUserId(userId,num,shopId,channelId);
    }

    @Override
    public UserShopBehavior findByUserIdAndProd(String userId,Long shopId, Long prodId,Long channelId) {
        return baseMapper.findByUserIdAndProd(userId,shopId,prodId,channelId);
    }

    @Override
    public ChannelProd findByProdId(Long prodId, Long shopId,Long channelId) {
        return productMapper.findByProdIdAndShopId(prodId,shopId,channelId);
    }

    @Override
    public List<ProductDto> findByProdIdAndShopIdAndPrice(Long prodId, Long shopId,Long channelId, BigDecimal increased, BigDecimal decreased) {
        return productMapper.findByProdIdAndShopIdAndPrice(prodId,shopId,channelId,increased,decreased);
    }

    @Override
    public IPage<SearchProdDto> findByProdAndShop(PageParam<SearchProdDto> page, List<ProductDto> productDtos,String userId) {
        IPage<SearchProdDto> byProdAndShop = productMapper.findByProdAndShop(page,productDtos);
        if (byProdAndShop!=null&&byProdAndShop.getRecords()!=null&&!byProdAndShop.getRecords().isEmpty()){
            for (SearchProdDto searchProdDto : byProdAndShop.getRecords()) {
                List<SkuDto> skuList = skuMapper.listByProdId(searchProdDto.getProdId()).stream().map((sku) -> SkuDto.builder()
                        .skuName(sku.getSkuName()).pic(sku.getPic()).skuScore(sku.getSkuScore()).price(sku.getPrice())
                        .stocks(sku.getStocks()).properties(sku.getProperties()).skuId(sku.getSkuId())
                        .build()).collect(Collectors.toList());
                searchProdDto.setSkuList(skuList);
                searchProdDto.setBasketTotal(basketMapper.findByCount(searchProdDto.getProdId(),searchProdDto.getShopId(),userId));
            }
        }


        return byProdAndShop;
    }
}
