package com.ruoyi.hezhi.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.hezhi.domain.TbMemberJoin;
import com.ruoyi.hezhi.domain.dto.MemberJoinDTO;
import com.ruoyi.hezhi.mapper.TbMemberJoinMapper;
import com.ruoyi.hezhi.service.ITbMemberJoinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户参与Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-15
 */
@Service
public class TbMemberJoinServiceImpl implements ITbMemberJoinService
{
    @Autowired
    private TbMemberJoinMapper tbMemberJoinMapper;

    /**
     * 查询用户参与
     *
     * @param memberJoinId 用户参与主键
     * @return 用户参与
     */
    @Override
    public TbMemberJoin selectTbMemberJoinByMemberJoinId(Long memberJoinId)
    {
        return tbMemberJoinMapper.selectTbMemberJoinByMemberJoinId(memberJoinId);
    }

    /**
     * 查询用户参与列表
     *
     * @param tbMemberJoin 用户参与
     * @return 用户参与
     */
    @Override
    public List<TbMemberJoin> selectTbMemberJoinList(TbMemberJoin tbMemberJoin)
    {
        return tbMemberJoinMapper.selectTbMemberJoinList(tbMemberJoin);
    }

    /**
     * 新增用户参与
     *
     * @param tbMemberJoin 用户参与
     * @return 结果
     */
    @Override
    public int insertTbMemberJoin(TbMemberJoin tbMemberJoin)
    {
        tbMemberJoin.setCreateTime(DateUtils.getNowDate());
        return tbMemberJoinMapper.insertTbMemberJoin(tbMemberJoin);
    }

    /**
     * 修改用户参与
     *
     * @param tbMemberJoin 用户参与
     * @return 结果
     */
    @Override
    public int updateTbMemberJoin(TbMemberJoin tbMemberJoin)
    {
        tbMemberJoin.setUpdateTime(DateUtils.getNowDate());
        return tbMemberJoinMapper.updateTbMemberJoin(tbMemberJoin);
    }

    /**
     * 批量删除用户参与
     *
     * @param memberJoinIds 需要删除的用户参与主键
     * @return 结果
     */
    @Override
    public int deleteTbMemberJoinByMemberJoinIds(Long[] memberJoinIds)
    {
        return tbMemberJoinMapper.deleteTbMemberJoinByMemberJoinIds(memberJoinIds);
    }

    /**
     * 删除用户参与信息
     *
     * @param memberJoinId 用户参与主键
     * @return 结果
     */
    @Override
    public int deleteTbMemberJoinByMemberJoinId(Long memberJoinId)
    {
        return tbMemberJoinMapper.deleteTbMemberJoinByMemberJoinId(memberJoinId);
    }

    /**
     * 立即参加
     *
     * @param memberJoinDTO
     * @return ok
     */
    @Override
    public AjaxResult joinNow(MemberJoinDTO memberJoinDTO) {
        TbMemberJoin tbMemberJoin = new TbMemberJoin();
        tbMemberJoin.setMemberUserId(memberJoinDTO.getMemberUserId());
        tbMemberJoin.setResId(memberJoinDTO.getResId());
        tbMemberJoin.setType(memberJoinDTO.getType());

        List<TbMemberJoin> memberJoinList = tbMemberJoinMapper.selectTbMemberJoinList(tbMemberJoin);
        if (memberJoinList.size() > 0) {
            return AjaxResult.error("您已参加");
        }

        tbMemberJoin.setCreateTime(DateUtils.getNowDate());
        tbMemberJoinMapper.insertTbMemberJoin(tbMemberJoin);
        return AjaxResult.success();
    }
}
