package com.buy507.mall.portal.service.impl;

import com.buy507.mall.mapper.PmsProductMapper;
import com.buy507.mall.model.PmsProduct;
import com.buy507.mall.model.UmsMember;
import com.buy507.mall.portal.domain.MemberProductCollection;
import com.buy507.mall.portal.repository.MemberProductCollectionRepository;
import com.buy507.mall.portal.service.MemberCollectionService;
import com.buy507.mall.portal.service.UmsMemberService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 会员收藏Service实现类
 */
@Service
public class MemberCollectionServiceImpl implements MemberCollectionService {
    @Autowired
    private MemberProductCollectionRepository productCollectionRepository;
    
    @Autowired
    private UmsMemberService memberService;
    
    @Autowired
    private PmsProductMapper pmsProductMapper;
    
    @SuppressWarnings("null")
	@Override
    public int addProduct(MemberProductCollection productCollection) {
        int count = 0;
        UmsMember currentMember = memberService.getCurrentMember();//获取用户信息
        MemberProductCollection findCollection = productCollectionRepository.findByMemberIdAndProductId(productCollection.getMemberId(), productCollection.getProductId());
        //获取商品信息
        PmsProduct pp  =  pmsProductMapper.getProductInfoById(productCollection.getProductId());
        if (findCollection == null) {
        	productCollection.setMemberIcon(currentMember.getIcon());
        	productCollection.setMemberNickname(currentMember.getUsername());
        	productCollection.setCreateTime(new Date());
        	productCollection.setMemberId(currentMember.getId());
        	productCollection.setMemberNickname(currentMember.getNickname());
        	productCollection.setId(System.currentTimeMillis()+"");
        	productCollection.setProductName(pp.getName());
        	productCollection.setProductPic(pp.getPic());
        	productCollection.setProductPrice(pp.getPrice()+"");
        	productCollection.setProductSubTitle(pp.getBrandName());
            productCollectionRepository.save(productCollection);
            count = 1;
        }
        return count;
    }

    @Override
    public int deleteProduct(Long memberId, Long productId) {
        return productCollectionRepository.deleteByMemberIdAndProductId(memberId, productId);
    }

    @Override
    public List<MemberProductCollection> listProduct(Long memberId) {
        return productCollectionRepository.findByMemberId(memberId);
    }
}
