package com.ruoyi.hezhi.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.hezhi.domain.TbConferenceNotice;
import com.ruoyi.hezhi.domain.TbConferenceRegistration;
import com.ruoyi.hezhi.domain.dto.ConferenceRegistrationDTO;
import com.ruoyi.hezhi.mapper.TbConferenceRegistrationMapper;
import com.ruoyi.hezhi.service.ITbConferenceNoticeService;
import com.ruoyi.hezhi.service.ITbConferenceRegistrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 参会报名Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-28
 */
@Service
public class TbConferenceRegistrationServiceImpl implements ITbConferenceRegistrationService
{
    @Autowired
    private TbConferenceRegistrationMapper tbConferenceRegistrationMapper;
    @Resource
    private ITbConferenceNoticeService conferenceNoticeService;

    /**
     * 查询参会报名
     *
     * @param conferenceRegistrationId 参会报名主键
     * @return 参会报名
     */
    @Override
    public TbConferenceRegistration selectTbConferenceRegistrationByConferenceRegistrationId(Long conferenceRegistrationId)
    {
        return tbConferenceRegistrationMapper.selectTbConferenceRegistrationByConferenceRegistrationId(conferenceRegistrationId);
    }

    /**
     * 查询参会报名列表
     *
     * @param tbConferenceRegistration 参会报名
     * @return 参会报名
     */
    @Override
    public List<TbConferenceRegistration> selectTbConferenceRegistrationList(TbConferenceRegistration tbConferenceRegistration)
    {
        return tbConferenceRegistrationMapper.selectTbConferenceRegistrationList(tbConferenceRegistration);
    }

    /**
     * 新增参会报名
     *
     * @param tbConferenceRegistration 参会报名
     * @return 结果
     */
    @Override
    public int insertTbConferenceRegistration(TbConferenceRegistration tbConferenceRegistration)
    {
        tbConferenceRegistration.setCreateTime(DateUtils.getNowDate());
        return tbConferenceRegistrationMapper.insertTbConferenceRegistration(tbConferenceRegistration);
    }

    /**
     * 修改参会报名
     *
     * @param tbConferenceRegistration 参会报名
     * @return 结果
     */
    @Override
    public int updateTbConferenceRegistration(TbConferenceRegistration tbConferenceRegistration)
    {
        tbConferenceRegistration.setUpdateTime(DateUtils.getNowDate());
        return tbConferenceRegistrationMapper.updateTbConferenceRegistration(tbConferenceRegistration);
    }

    /**
     * 批量删除参会报名
     *
     * @param conferenceRegistrationIds 需要删除的参会报名主键
     * @return 结果
     */
    @Override
    public int deleteTbConferenceRegistrationByConferenceRegistrationIds(Long[] conferenceRegistrationIds)
    {
        return tbConferenceRegistrationMapper.deleteTbConferenceRegistrationByConferenceRegistrationIds(conferenceRegistrationIds);
    }

    /**
     * 删除参会报名信息
     *
     * @param conferenceRegistrationId 参会报名主键
     * @return 结果
     */
    @Override
    public int deleteTbConferenceRegistrationByConferenceRegistrationId(Long conferenceRegistrationId)
    {
        return tbConferenceRegistrationMapper.deleteTbConferenceRegistrationByConferenceRegistrationId(conferenceRegistrationId);
    }

    /**
     * 参会报名
     *
     * @param conferenceRegistrationDTO
     * @return ok
     */
    @Override
    public AjaxResult conferenceRegistration(ConferenceRegistrationDTO conferenceRegistrationDTO) {
        TbConferenceNotice conferenceNotice = conferenceNoticeService.selectTbConferenceNoticeByEnroll();
        if (conferenceNotice == null) {
            return AjaxResult.error("会议未启动报名");
        }
        TbConferenceRegistration userRegistration = tbConferenceRegistrationMapper.selectTbConferenceRegistrationBy(conferenceRegistrationDTO.getMemberUserId(), conferenceNotice.getConferenceNoticeId());
        if (userRegistration != null) {
            return AjaxResult.error("您已报名，请勿重复提交");
        }
        TbConferenceRegistration registration = new TbConferenceRegistration();
        registration.setMemberUserId(conferenceRegistrationDTO.getMemberUserId());
        registration.setConferenceNoticeId(conferenceRegistrationDTO.getConferenceNoticeId());
        registration.setName(conferenceRegistrationDTO.getName());
        registration.setPhone(conferenceRegistrationDTO.getPhone());
        registration.setDocumentType(conferenceRegistrationDTO.getDocumentType());
        registration.setDocumentNumber(conferenceRegistrationDTO.getDocumentNumber());
        registration.setEmail(conferenceRegistrationDTO.getEmail());
        registration.setCompanyName(conferenceRegistrationDTO.getCompanyName());
        registration.setCompanyType(conferenceRegistrationDTO.getCompanyType());
        registration.setPosition(conferenceRegistrationDTO.getPosition());
        registration.setPositionType(conferenceRegistrationDTO.getPositionType());
        registration.setProvinceId(conferenceRegistrationDTO.getProvinceId());
        registration.setCityId(conferenceRegistrationDTO.getCityId());
        registration.setAreaId(conferenceRegistrationDTO.getAreaId());
        registration.setIdPhoto(conferenceRegistrationDTO.getIdPhoto());
        registration.setBusiness(conferenceRegistrationDTO.getBusiness());
        registration.setObjective(conferenceRegistrationDTO.getObjective());
        registration.setIsParticipate(conferenceRegistrationDTO.getIsParticipate());
        registration.setIsParticipateNext(conferenceRegistrationDTO.getIsParticipateNext());

        registration.setCreateTime(DateUtils.getNowDate());
        tbConferenceRegistrationMapper.insertTbConferenceRegistration(registration);
        return AjaxResult.success();
    }

    /**
     * 参会报名详情
     *
     * @param params 参数
     * @return 参会报名详情
     */
    @Override
    public Map<String, Object> getConferenceRegistrationInfo(Map<String, Object> params) {
        HashMap<String, Object> map = new HashMap<>();
        int conferenceStatus = 0;// 默认无会议
        String demand = "";// 会议报名要求
        Long conferenceNoticeId = 0L;// 会议通知ID

        TbConferenceNotice conferenceNotice = conferenceNoticeService.selectTbConferenceNoticeByEnroll();
        if (conferenceNotice != null) {
            TbConferenceRegistration userRegistration = tbConferenceRegistrationMapper.selectTbConferenceRegistrationBy(Long.parseLong(params.get("memberUserId").toString()), conferenceNotice.getConferenceNoticeId());
            if (userRegistration == null) {
                // 可报名
                conferenceStatus = 1;
            } else {
                // 已报名
                conferenceStatus = 2;
            }

            demand = conferenceNotice.getDemand();
            conferenceNoticeId = conferenceNotice.getConferenceNoticeId();
        }
        map.put("conferenceStatus", conferenceStatus);
        map.put("demand", demand);
        map.put("conferenceNoticeId", conferenceNoticeId);
        return map;
    }
}
