/*
 * 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.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.yami.shop.bean.app.dto.UserCollectionDto;
import com.yami.shop.bean.model.UserCollection;
import com.yami.shop.dao.UserCollectionMapper;
import com.yami.shop.service.UserCollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 用户收藏表
 *
 * @author xwc
 * @date 2019-04-19 16:57:20
 */
@Service
public class UserCollectionServiceImpl extends ServiceImpl<UserCollectionMapper, UserCollection> implements UserCollectionService {

    @Autowired
    private UserCollectionMapper userCollectionMapper;

    @Override
    public IPage<UserCollectionDto> getUserCollectionDtoPageByUserId(Page page, String userId, String prodName, Integer prodType) {
        return userCollectionMapper.getUserCollectionDtoPageByUserId(page,userId,prodName,prodType);
    }

    @Override
    public void orderProdCollectionAll(List<String> prodIdList, String userId) {
        List<UserCollection> userCollectionListDB = userCollectionMapper.selectList(new LambdaQueryWrapper<UserCollection>()
                .eq(UserCollection::getUserId, userId)
                .in(UserCollection::getProdId, prodIdList));
        List<String> newProdCollectionList = Lists.newArrayList();
        for (UserCollection userCollection:userCollectionListDB){
            newProdCollectionList.add(userCollection.getProdId().toString());
        }
        prodIdList.removeAll(newProdCollectionList);

        List<UserCollection> userCollectionList = Lists.newArrayList();
        for (String prodId:prodIdList){
            UserCollection userCollection = new UserCollection();
            userCollection.setUserId(userId);
            userCollection.setCreateTime(new Date());
            userCollection.setProdId(Long.valueOf(prodId));
            userCollectionList.add(userCollection);
        }
        saveBatch(userCollectionList);
    }
}
