package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.ZxFj;
import com.ruoyi.system.domain.notice.ZxNotice;
import com.ruoyi.system.domain.notice.ZxUserNotice;
import com.ruoyi.system.mapper.ZxFjMapper;
import com.ruoyi.system.service.IZxNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.mapper.ZxNoticeMapper;
import java.util.ArrayList;
import java.util.List;

import static com.ruoyi.common.constant.CommonConstants.THR;

/**
 * 政协通知公告Service业务层处理
 * 
 * @author boman
 * @date 2024-03-05
 */
@Service
public class ZxNoticeServiceImpl implements IZxNoticeService
{
    @Autowired
    private ZxNoticeMapper zxNoticeMapper;

    @Autowired
    private ZxFjMapper zxFjMapper;

    /**
     * 查询政协通知公告
     * 
     * @param zxNoticeId 政协通知公告主键
     * @return 政协通知公告
     */
    @Override
    public ZxNotice selectZxNoticeByZxNoticeId(Long zxNoticeId)
    {
        //记录是谁看的
        ZxUserNotice zxUserNotice = new ZxUserNotice();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        ZxNotice zxNotice = zxNoticeMapper.selectZxNoticeByZxNoticeId(zxNoticeId);
        zxUserNotice.setUserId(user.getUserId());
        zxUserNotice.setUserName(user.getUserName());
        zxUserNotice.setZxNoticeId(zxNoticeId);
        zxNoticeMapper.insterZxUserNotice(zxUserNotice);
        return zxNotice;
    }

    /**
     * 查询政协通知公告列表
     * 
     * @param zxNotice 政协通知公告
     * @return 政协通知公告
     */
    @Override
    public List<ZxNotice> selectZxNoticeList(ZxNotice zxNotice)
    {
        return zxNoticeMapper.selectZxNoticeList(zxNotice);
    }

    /**
     * 新增政协通知公告
     * 
     * @param zxNotice 政协通知公告
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertZxNotice(ZxNotice zxNotice)
    {
        zxNotice.setCreateTime(DateUtils.getNowDate());
        int rows = zxNoticeMapper.insertZxNotice(zxNotice);
        insertZxFj(zxNotice);
        insertZxUserNotice(zxNotice);
        return rows;
    }

    /**
     * 修改政协通知公告
     * 
     * @param zxNotice 政协通知公告
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateZxNotice(ZxNotice zxNotice)
    {
        zxNotice.setUpdateTime(DateUtils.getNowDate());
        zxNoticeMapper.deleteZxUserNoticeByZxNoticeId(zxNotice.getZxNoticeId());
        zxFjMapper.deleteZxFjBySourceId(zxNotice.getZxNoticeId());
        insertZxUserNotice(zxNotice);
        insertZxFj(zxNotice);
        return zxNoticeMapper.updateZxNotice(zxNotice);
    }

    /**
     * 批量删除政协通知公告
     * 
     * @param zxNoticeIds 需要删除的政协通知公告主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteZxNoticeByZxNoticeIds(Long[] zxNoticeIds)
    {
        zxNoticeMapper.deleteZxUserNoticeByZxNoticeIds(zxNoticeIds);
        return zxNoticeMapper.deleteZxNoticeByZxNoticeIds(zxNoticeIds);
    }

    /**
     * 删除政协通知公告信息
     * 
     * @param zxNoticeId 政协通知公告主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteZxNoticeByZxNoticeId(Long zxNoticeId)
    {
        zxNoticeMapper.deleteZxUserNoticeByZxNoticeId(zxNoticeId);
        return zxNoticeMapper.deleteZxNoticeByZxNoticeId(zxNoticeId);
    }

    /**
     * 新增用户与政协通知公告关联信息
     * 
     * @param zxNotice 政协通知公告对象
     */
    public void insertZxUserNotice(ZxNotice zxNotice)
    {
        List<ZxUserNotice> zxUserNoticeList = zxNotice.getZxUserNoticeList();
        Long zxNoticeId = zxNotice.getZxNoticeId();
        if (StringUtils.isNotNull(zxUserNoticeList))
        {
            List<ZxUserNotice> list = new ArrayList<ZxUserNotice>();
            for (ZxUserNotice zxUserNotice : zxUserNoticeList)
            {
                zxUserNotice.setZxNoticeId(zxNoticeId);
                list.add(zxUserNotice);
            }
            if (list.size() > 0)
            {
                zxNoticeMapper.batchZxUserNotice(list);
            }
        }
    }

    /**
     * 新增通知公告附件关联信息
     *
     * @param zxNotice 政协通知公告对象
     */
    public void insertZxFj(ZxNotice zxNotice)
    {
        List<ZxFj> zxFjListFj = zxNotice.getZxFjListFj();
        Long zxNoticeId = zxNotice.getZxNoticeId();
        if (StringUtils.isNotNull(zxFjListFj))
        {
            List<ZxFj> list = new ArrayList<ZxFj>();
            for (ZxFj zxFj : zxFjListFj)
            {
                zxFj.setType(THR);
                zxFj.setMainId(zxNoticeId);
                zxFj.setSourceId(zxNoticeId);
                list.add(zxFj);
            }
            if (list.size() > 0)
            {
                zxFjMapper.batchZxFj(list);
            }
        }
    }
}
