package com.ruoyi.post.service.impl;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.post.domain.PostBulletin;
import com.ruoyi.post.domain.PostNews;
import com.ruoyi.post.domain.PostUser;
import com.ruoyi.post.mapper.PostBulletinMapper;
import com.ruoyi.post.mapper.PostNewsMapper;
import com.ruoyi.post.mapper.PostUserMapper;
import com.ruoyi.post.service.IPostBulletinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.validation.Validator;
import java.util.List;


/**
 * @Author HuiANan
 * @Date 2023/1/13 17:19
 * @Exegesis:
 */
@Service
public class PostBulletinServiceImpl implements IPostBulletinService {
    @Resource
    private PostBulletinMapper postBulletinMapper;

    /**
     * 查询通知公告管理;
     *
     * @param noticeId 通知公告管理;
     * @return 通知公告管理;
     */
    @Override
    public PostBulletin selectPostBulletinByNoticeId(Integer noticeId) {
        return postBulletinMapper.selectPostBulletinByNoticeId(noticeId);
    }

    /**
     * 查询通知公告管理;
     *
     * @param postBulletin 通知公告管理;
     * @return 通知公告管理;
     */
    @Override
    public List<PostBulletin> selectPostBulletinList(PostBulletin postBulletin) {
        return postBulletinMapper.selectPostBulletinList(postBulletin);
    }

    /**
     * 新增通知公告管理;
     *
     * @param postBulletin 通知公告管理;
     * @return 结果
     */
    @Override
    public int insertPostBulletin(PostBulletin postBulletin) {
        postBulletin.setCreateTime(DateUtils.getNowDate());
        return postBulletinMapper.insertPostBulletin(postBulletin);
    }

    /**
     * 修改通知公告管理;
     *
     * @param postBulletin 通知公告管理;
     * @return 结果
     */
    @Override
    public int updatePostBulletin(PostBulletin postBulletin) {
        postBulletin.setUpdateTime(DateUtils.getNowDate());
        return postBulletinMapper.updatePostBulletin(postBulletin);
    }

    /**
     * 批量删除通知公告管理;
     *
     * @param noticeIds 需要删除的通知公告管理;
     * @return 结果
     */
    @Override
    public int deletePostBulletinByNoticeIds(Integer[] noticeIds) {
        return postBulletinMapper.deletePostBulletinByNoticeIds(noticeIds);
    }

    /**
     * 删除通知公告管理;
     *
     * @param noticeId 通知公告管理;
     * @return 结果
     */
    @Override
    public int deletePostBulletinByNoticeId(Integer noticeId) {
        return postBulletinMapper.deletePostBulletinByNoticeId(noticeId);
    }


    @Override
    public PostBulletin selectViewDetail(Integer noticeId) {
        return postBulletinMapper.selectViewDetails(noticeId);
    }

    @Override
    public String checkTitle(PostBulletin postBulletin) {
        Long noticeId = StringUtils.isNull(postBulletin.getNoticeId()) ? -1l : postBulletin.getNoticeId();
        PostBulletin info = postBulletinMapper.checkTitleUnique(postBulletin.getNoticeTitle());
        if (StringUtils.isNotNull(info) && info.getNoticeId().longValue() != noticeId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public String checkContent(PostBulletin postBulletin) {
        Long noticeId = StringUtils.isNull(postBulletin.getNoticeId()) ? -1l : postBulletin.getNoticeId();
        PostBulletin info = postBulletinMapper.checkContentUnique(postBulletin.getNoticeContent());
        if (StringUtils.isNotNull(info) && info.getNoticeId().longValue() != noticeId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}
