package com.amms.service.impl;

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

import com.amms.domain.ItemCollection;
import com.amms.mapper.ItemCollectionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.amms.mapper.RelicMapper;
import com.amms.domain.Relic;
import com.amms.service.IRelicService;
import com.amms.security.SecurityUtils;

/**
 * 藏品Service业务层处理
 */
@Service
public class RelicServiceImpl implements IRelicService {

    @Autowired
    private RelicMapper relicMapper;
    @Autowired
    private ItemCollectionMapper itemCollectionMapper;

    /**
     * 查询藏品列表
     *
     * @param relic 藏品
     * @return 藏品
     */
    @Override
    public List<Relic> selectRelicList(Relic relic) {
        return relicMapper.selectRelicList(relic);
    }

    /**
     * 查询藏品
     *
     * @param id 藏品主键
     * @return 藏品
     */
    @Override
    public Relic selectRelicById(Long id) {
        Relic relic = relicMapper.selectRelicById(id);
        // 如果登录用户信息不为空，查询收藏数据
        if (SecurityUtils.getCurrentUser() != null && SecurityUtils.getCurrentUser().getId() != null) {
            ItemCollection itemCollection = new ItemCollection();
            itemCollection.setItemId(id);
            itemCollection.setUserId(SecurityUtils.getCurrentUser().getId());
            relic.setItemCollections(itemCollectionMapper.selectItemCollectionList(itemCollection));
        }
        return relic;
    }

    /**
     * 新增藏品
     *
     * @param relic 藏品
     * @return 结果
     */
    @Override
    public int insertRelic(Relic relic) {
        relic.setCreateTime(new Date());
        relic.setCreator(SecurityUtils.getCurrentUser().getId());
        return relicMapper.insertRelic(relic);
    }

    /**
     * 修改藏品
     *
     * @param relic 藏品
     * @return 结果
     */
    @Override
    public int updateRelic(Relic relic) {
        relic.setUpdateTime(new Date());
        relic.setUpdater(SecurityUtils.getCurrentUser().getId());
        return relicMapper.updateRelic(relic);
    }

    /**
     * 删除藏品信息
     *
     * @param id 藏品主键
     * @return 结果
     */
    @Override
    public int deleteRelicById(Long id) {
        return relicMapper.deleteRelicById(id);
    }

}
