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.MemberReadHistory;
import com.buy507.mall.portal.repository.MemberReadHistoryRepository;
import com.buy507.mall.portal.service.MemberReadHistoryService;
import com.buy507.mall.portal.service.UmsMemberService;

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

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

/**
 * 会员浏览记录管理Service实现类
 */
@Service
public class MemberReadHistoryServiceImpl implements MemberReadHistoryService {
    @Autowired
    private MemberReadHistoryRepository memberReadHistoryRepository;
    
    @Autowired
    private UmsMemberService memberService;
    
    @Autowired
    private PmsProductMapper pmsProductMapper;
    
//    @Autowired
//    private MemberReadHistoryRepository memberReadHistory;
    
    @Override
    public int create(MemberReadHistory memberReadHistory) {
    	UmsMember m = memberService.getCurrentMember();//获取用户信息
//    	MemberReadHistory mbReadHistory=memberReadHistory.
        //获取商品信息
        PmsProduct pp  =  pmsProductMapper.getProductInfoById(memberReadHistory.getProductId());
        memberReadHistory.setId(System.currentTimeMillis()+"");
        memberReadHistory.setCreateTime(new Date());
        memberReadHistory.setMemberIcon(m.getIcon());
        memberReadHistory.setMemberId(m.getId());
        memberReadHistory.setMemberNickname(m.getNickname());
        memberReadHistory.setProductName(pp.getName());
        memberReadHistory.setProductPic(pp.getPic());
        memberReadHistory.setProductPrice(pp.getPrice()+"");
        memberReadHistoryRepository.save(memberReadHistory);
        return 1;
    }

    @Override
    public int delete(List<String> ids) {
        List<MemberReadHistory> deleteList = new ArrayList<>();
        for(String id:ids){
            MemberReadHistory memberReadHistory = new MemberReadHistory();
            memberReadHistory.setId(id);
            deleteList.add(memberReadHistory);
        }
        memberReadHistoryRepository.deleteAll(deleteList);
        return ids.size();
    }

    @Override
    public List<MemberReadHistory> list(Long memberId) {
    	UmsMember m = memberService.getCurrentMember();//获取用户信息
        return memberReadHistoryRepository.findByMemberIdOrderByCreateTimeDesc(m.getId());
    }
}
