package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.DateUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TbNoticeMapper;
import com.ruoyi.system.domain.TbNotice;
import com.ruoyi.system.service.ITbNoticeService;
import com.ruoyi.common.core.text.Convert;

/**
 * 通知公告Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-10-18
 */
@Service
public class TbNoticeServiceImpl implements ITbNoticeService 
{
    @Autowired
    private TbNoticeMapper tbNoticeMapper;

    @Autowired
    private CommonServiceImpl commonService;

    /**
     * 查询通知公告
     * 
     * @param id 通知公告主键
     * @return 通知公告
     */
    @Override
    public TbNotice selectTbNoticeById(Long id)
    {
        TbNotice tn=tbNoticeMapper.selectTbNoticeById(id);
        tn.setCreatorDesc(commonService.getUserName(tn.getCreator()));
        if(ObjectUtils.isNotEmpty(tn.getModifiedBy())){
            tn.setModifiedByDesc(commonService.getUserName(tn.getModifiedBy()));
        }
        tn.setNoticeTypeDesc(commonService.convertDictToString(String.valueOf(tn.getNoticeType()), Constants.BUSINESS_NOTICE_TYPE));
        tn.setStatusDesc(commonService.convertDictToString(String.valueOf(tn.getStatus()),Constants.BUSINESS_NOTICE_STATUS));
        return tn;
    }

    /**
     * 查询通知公告列表
     * 
     * @param tbNotice 通知公告
     * @return 通知公告
     */
    @Override
    public List<TbNotice> selectTbNoticeList(TbNotice tbNotice)
    {
        List<TbNotice> list=tbNoticeMapper.selectTbNoticeList(tbNotice);
        list.stream().filter(s -> {
            s.setCreatorDesc(commonService.getUserName(s.getCreator()));
            if(ObjectUtils.isNotEmpty(s.getModifiedBy())){
                s.setModifiedByDesc(commonService.getUserName(s.getModifiedBy()));
            }
            s.setNoticeTypeDesc(commonService.convertDictToString(String.valueOf(s.getNoticeType()), Constants.BUSINESS_NOTICE_TYPE));
            s.setStatusDesc(commonService.convertDictToString(String.valueOf(s.getStatus()),Constants.BUSINESS_NOTICE_STATUS));
            return true;
        }).collect(Collectors.toList());
        return list;
    }

    /**
     * 新增通知公告
     * 
     * @param tbNotice 通知公告
     * @return 结果
     */
    @Override
    public int insertTbNotice(TbNotice tbNotice)
    {
        tbNotice.setCreateTime(DateUtils.getNowDate());
        return tbNoticeMapper.insertTbNotice(tbNotice);
    }

    /**
     * 修改通知公告
     * 
     * @param tbNotice 通知公告
     * @return 结果
     */
    @Override
    public int updateTbNotice(TbNotice tbNotice)
    {
        tbNotice.setUpdateTime(DateUtils.getNowDate());
        return tbNoticeMapper.updateTbNotice(tbNotice);
    }

    /**
     * 批量删除通知公告
     * 
     * @param ids 需要删除的通知公告主键
     * @return 结果
     */
    @Override
    public int deleteTbNoticeByIds(String ids)
    {
        return tbNoticeMapper.deleteTbNoticeByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除通知公告信息
     * 
     * @param id 通知公告主键
     * @return 结果
     */
    @Override
    public int deleteTbNoticeById(Long id)
    {
        return tbNoticeMapper.deleteTbNoticeById(id);
    }
}
