package com.ruoyi.system.service.impl;


import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.ZxFj;
import com.ruoyi.system.domain.activity.ZxActivity;
import com.ruoyi.system.domain.speak.ZxSpeak;
import com.ruoyi.system.mapper.ZxFjMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ZxSpeakMapper;
import com.ruoyi.system.service.IZxSpeakService;

import java.util.ArrayList;
import java.util.List;

import static com.ruoyi.common.constant.CommonConstants.FIV;
import static com.ruoyi.common.constant.CommonConstants.SIX;

/**
 * 政协发言Service业务层处理
 * 
 * @author boman
 * @date 2024-03-07
 */
@Service
public class ZxSpeakServiceImpl implements IZxSpeakService 
{
    @Autowired
    private ZxSpeakMapper zxSpeakMapper;
    @Autowired
    private ZxFjMapper zxFjMapper;

    /**
     * 查询政协发言
     * 
     * @param speakId 政协发言主键
     * @return 政协发言
     */
    @Override
    public ZxSpeak selectZxSpeakBySpeakId(Long speakId)
    {
        ZxSpeak zxSpeak = zxSpeakMapper.selectZxSpeakBySpeakId(speakId);
        ZxFj zxFj = new ZxFj();
        zxFj.setSourceId(zxSpeak.getSpeakId());
        zxFj.setType(SIX);
        List<ZxFj> zxFjs = zxFjMapper.selectZxFjList(zxFj);
        if (zxFjs !=  null){
            zxSpeak.setZxFjList(zxFjs);
        }
        return zxSpeak;
    }

    /**
     * 查询政协发言列表
     * 
     * @param zxSpeak 政协发言
     * @return 政协发言
     */
    @Override
    public List<ZxSpeak> selectZxSpeakList(ZxSpeak zxSpeak)
    {
        return zxSpeakMapper.selectZxSpeakList(zxSpeak);
    }

    /**
     * 新增政协发言
     * 
     * @param zxSpeak 政协发言
     * @return 结果
     */
    @Override
    public int insertZxSpeak(ZxSpeak zxSpeak)
    {
        zxSpeak.setCreateTime(DateUtils.getNowDate());
        zxSpeak.setPublishTime(DateUtils.getNowDate());
        int i = zxSpeakMapper.insertZxSpeak(zxSpeak);
        insertZxFj(zxSpeak);
        return i;
    }

    /**
     * 修改政协发言
     * 
     * @param zxSpeak 政协发言
     * @return 结果
     */
    @Override
    public int updateZxSpeak(ZxSpeak zxSpeak)
    {
        zxSpeak.setUpdateTime(DateUtils.getNowDate());
        zxFjMapper.deleteZxFjBySourceId(zxSpeak.getSpeakId());
        insertZxFj(zxSpeak);
        return zxSpeakMapper.updateZxSpeak(zxSpeak);
    }

    /**
     * 批量删除政协发言
     * 
     * @param speakIds 需要删除的政协发言主键
     * @return 结果
     */
    @Override
    public int deleteZxSpeakBySpeakIds(Long[] speakIds)
    {
        return zxSpeakMapper.deleteZxSpeakBySpeakIds(speakIds);
    }

    /**
     * 删除政协发言信息
     * 
     * @param speakId 政协发言主键
     * @return 结果
     */
    @Override
    public int deleteZxSpeakBySpeakId(Long speakId)
    {
        return zxSpeakMapper.deleteZxSpeakBySpeakId(speakId);
    }

    /**
     * 新增发言附件关联信息
     */
    public void insertZxFj(ZxSpeak zxSpeak)
    {
        List<ZxFj> zxFjListFj = zxSpeak.getZxFjList();
        Long speakId = zxSpeak.getSpeakId();
        if (StringUtils.isNotNull(zxFjListFj))
        {
            List<ZxFj> list = new ArrayList<ZxFj>();
            for (ZxFj zxFj : zxFjListFj)
            {
                zxFj.setType(SIX);
                zxFj.setMainId(speakId);
                zxFj.setSourceId(speakId);
                list.add(zxFj);
            }
            if (list.size() > 0)
            {
                zxFjMapper.batchZxFj(list);
            }
        }
    }
}
