package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.PostCollections;
import com.ruoyi.system.domain.vo.PostCollectionsVo;
import com.ruoyi.system.mapper.PostCollectionsMapper;
import com.ruoyi.system.mapper.PostCollectionsSystemMapper;
import com.ruoyi.system.service.IIssuerService;
import com.ruoyi.system.service.IPostCollectionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

import static com.ruoyi.common.utils.SecurityUtils.getUsername;

/**
 * 藏品Service业务层处理
 *
 * @author ruoyi
 * @date 2023-02-15
 */
@Transactional//涉及两张表要加事务
@Service
public class PostCollectionsServiceImpl implements IPostCollectionsService
{
    @Autowired
    private PostCollectionsMapper postCollectionsMapper;
    @Autowired
    private IIssuerService iIssuerService;
    @Autowired
    private PostCollectionsSystemMapper postCollectionsSystemMapper;
    /**
     * 查询藏品详细信息
     *
     * @param id 藏品主键
     * @return 藏品
     */
    @Override
    public PostCollectionsVo selectPostCollectionsById(Long id)
    {
        return postCollectionsMapper.selectPostCollectionsById(id);
    }

    /**
     * 查询藏品列表
     *
     * @param systemId 藏品
     * @return 藏品
     */
    @Override
    public List<PostCollections> selectPostCollectionsList(Long systemId)
    {
        PostCollections postCollections = new PostCollections();
        //systemId是套系里的主键id
        postCollections.setSystemId(systemId);
        return postCollectionsMapper.selectPostCollectionsList(postCollections.getSystemId());
    }

    /**
     * 新增藏品
     *
     * @param postCollectionsVo 藏品
     * @return 结果
     */
    @Override
    public AjaxResult insertPostCollections(PostCollectionsVo postCollectionsVo)
    {
        //获取此藏品套系status
        Long systemId = postCollectionsVo.getSystemId();
        String postCollectionsSystemStatus = postCollectionsSystemMapper.selectPostCollectionsSystemByStatus(systemId);
        //判断藏品套系是否上架，如果没上架，令藏品也未上架
        if (Objects.equals(postCollectionsSystemStatus, "0")){
            postCollectionsVo.setStatus("4");//未上架
            postCollectionsVo.setCreateBy(getUsername());
            postCollectionsVo.setUpdateBy(getUsername());
            postCollectionsVo.setUpdateTime(DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS,DateUtils.getTime()));
            postCollectionsVo.setCreateTime(DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS,DateUtils.getTime()));
            //通过发行方的名字查询id存进数据库
            postCollectionsVo.setSystemId(postCollectionsVo.getSystemId());
            postCollectionsVo.setIssuerId(iIssuerService.selectIssuerByIdForIssuerName(postCollectionsVo.getIssuerName()));

            return AjaxResult.success(postCollectionsMapper.insertPostCollections(postCollectionsVo));
        }

        else {
            //上架藏品不能新增
            return AjaxResult.error("此套系已上架，不能新增藏品");
        }

    }

    /**
     * 修改藏品
     *
     * @param postCollections 藏品
     * @return 结果
     */
    @Override
    public int updatePostCollections(PostCollections postCollections)
    {
        postCollections.setUpdateBy(getUsername());
        postCollections.setUpdateTime(DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS,DateUtils.getTime()));
        return postCollectionsMapper.updatePostCollections(postCollections);
    }

    /**
     * 批量删除藏品
     *
     * @param ids 需要删除的藏品主键
     * @return 结果
     */
    @Override
    public int deletePostCollectionsByIds(Long[] ids)
    {
        return postCollectionsMapper.deletePostCollectionsByIds(ids);
    }

    /**
     * 删除藏品信息
     *
     * @param id 藏品主键
     * @return 结果
     */
    @Override
    public int deletePostCollectionsById(Long id)
    {
        return postCollectionsMapper.deletePostCollectionsById(id);
    }
    /**
     * 根据藏品名称查询藏品数量
     *
     * @param postCollectionsVo 藏品
     * @return 结果
     */
    @Override
    public int selectPostCollectionsByName(PostCollectionsVo postCollectionsVo) {
        return postCollectionsMapper.selectPostCollectionsByName(postCollectionsVo);
    }
}
