package com.yida.data.school.news.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.notice.EduNoticeSystem;
import com.yida.data.common.core.entity.notice.EduNoticeSystemUser;
import com.yida.data.common.core.entity.system.SystemUser;
import com.yida.data.school.news.mapper.EduNoticeSystemMapper;
import com.yida.data.school.news.mapper.EduNoticeSystemUserMapper;
import com.yida.data.school.news.service.EduNoticeSystemUserService;
import com.yida.data.system.feign.RemoteUserService;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import lombok.RequiredArgsConstructor;

/**
 * 系统通知关联用户中间表 Service实现
 *
 * @author zhanghaijun
 * @date 2021-06-21 15:29:18
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class EduNoticeSystemUserServiceImpl extends ServiceImpl
        <EduNoticeSystemUserMapper, EduNoticeSystemUser> implements EduNoticeSystemUserService {

    private final EduNoticeSystemUserMapper eduNoticeSystemUserMapper;
    @Resource
    private EduNoticeSystemMapper eduNoticeSystemMapper;
    @Resource
    private RemoteUserService remoteUserService;

    @Override
    public EduNoticeSystemUser selectNotice(EduNoticeSystemUser eduNoticeSystemUser) {
        return null;
    }


    /**
     * 新增通知和用户关系，顺便发送 通知给对应用户
     *
     * @param eduNoticeSystemUser
     * @return
     */

    @Override
    @Transactional
    public Long insertNotice(EduNoticeSystemUser eduNoticeSystemUser) {
        eduNoticeSystemUserMapper.insert(eduNoticeSystemUser);
        EduNoticeSystem eduNoticeSystem = eduNoticeSystemMapper.selectById(eduNoticeSystemUser.getNoticeId());
        ResultBean<SystemUser> infoById = remoteUserService.getInfoById(eduNoticeSystemUser.getUserId());
        return eduNoticeSystemUser.getId();
    }

    /**
     * 修改为已阅读
     *
     * @param eduNoticeSystemUser
     */
    @Override
    public void updateNotice(EduNoticeSystemUser eduNoticeSystemUser) {
        QueryWrapper<EduNoticeSystemUser> queryWrapper = new QueryWrapper<EduNoticeSystemUser>(eduNoticeSystemUser);
        eduNoticeSystemUserMapper.update(eduNoticeSystemUser, queryWrapper);
    }

    /**
     * 删除对应关系
     *
     * @param eduNoticeSystemUser
     */
    @Override
    public void delNotice(EduNoticeSystemUser eduNoticeSystemUser) {
        QueryWrapper<EduNoticeSystemUser> queryWrapper = new QueryWrapper<EduNoticeSystemUser>(eduNoticeSystemUser);
        eduNoticeSystemUserMapper.delete(queryWrapper);
    }
}
