package com.ruoyi.branch.service.impl;

import java.util.List;

import com.ruoyi.branch.domain.TBranchActivityBudget;
import com.ruoyi.branch.mapper.TBranchActivityBudgetMapper;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.branch.mapper.TBranchActivityNoticeMapper;
import com.ruoyi.branch.domain.TBranchActivityNotice;
import com.ruoyi.branch.service.ITBranchActivityNoticeService;

/**
 * 支部活动预告Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-07-20
 */
@Service
public class TBranchActivityNoticeServiceImpl implements ITBranchActivityNoticeService 
{
    @Autowired
    private TBranchActivityNoticeMapper tBranchActivityNoticeMapper;

    @Autowired
    private TBranchActivityBudgetMapper tBranchActivityBudgetMapper;

    /**
     * 查询支部活动预告
     * 
     * @param noticeId 支部活动预告主键
     * @return 支部活动预告
     */
    @Override
    public TBranchActivityNotice selectTBranchActivityNoticeByNoticeId(Long noticeId)
    {
        TBranchActivityNotice tBranchActivityNotice = tBranchActivityNoticeMapper.selectTBranchActivityNoticeByNoticeId(noticeId);
        // 根据noticeId查询budgetList
        TBranchActivityBudget budget = new TBranchActivityBudget();
        budget.setNoticeId(noticeId);
        tBranchActivityNotice.setBudgetList(tBranchActivityBudgetMapper.selectTBranchActivityBudgetList(budget));
        return tBranchActivityNotice;
    }

    /**
     * 查询支部活动预告列表
     * 
     * @param tBranchActivityNotice 支部活动预告
     * @return 支部活动预告
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<TBranchActivityNotice> selectTBranchActivityNoticeList(TBranchActivityNotice tBranchActivityNotice)
    {
        return tBranchActivityNoticeMapper.selectTBranchActivityNoticeList(tBranchActivityNotice);
    }

    /**
     * 新增支部活动预告
     * 
     * @param tBranchActivityNotice 支部活动预告
     * @return 结果
     */
    @Override
    public int insertTBranchActivityNotice(TBranchActivityNotice tBranchActivityNotice)
    {
        tBranchActivityNotice.setCreateTime(DateUtils.getNowDate());
        int result = tBranchActivityNoticeMapper.insertTBranchActivityNotice(tBranchActivityNotice);
        // 添加相关活动预算
        for (TBranchActivityBudget tBranchActivityBudget : tBranchActivityNotice.getBudgetList()) {
            tBranchActivityBudget.setNoticeId(tBranchActivityNotice.getNoticeId());
            tBranchActivityBudget.setDeptId(tBranchActivityNotice.getDeptId());
            tBranchActivityBudgetMapper.insertTBranchActivityBudget(tBranchActivityBudget);
        }
        return result;
    }

    /**
     * 修改支部活动预告
     * 
     * @param tBranchActivityNotice 支部活动预告
     * @return 结果
     */
    @Override
    public int updateTBranchActivityNotice(TBranchActivityNotice tBranchActivityNotice)
    {
        tBranchActivityNotice.setUpdateTime(DateUtils.getNowDate());
        // 删除相关活动相关预算
        tBranchActivityBudgetMapper.deleteTBranchActivityBudgetByNoticeId(tBranchActivityNotice.getNoticeId());
        // 重新添加活动预算
        for (TBranchActivityBudget tBranchActivityBudget : tBranchActivityNotice.getBudgetList()) {
            tBranchActivityBudget.setNoticeId(tBranchActivityNotice.getNoticeId());
            tBranchActivityBudget.setDeptId(tBranchActivityNotice.getDeptId());
            tBranchActivityBudgetMapper.insertTBranchActivityBudget(tBranchActivityBudget);
        }
        return tBranchActivityNoticeMapper.updateTBranchActivityNotice(tBranchActivityNotice);
    }

    /**
     * 批量删除支部活动预告
     * 
     * @param noticeIds 需要删除的支部活动预告主键
     * @return 结果
     */
    @Override
    public int deleteTBranchActivityNoticeByNoticeIds(Long[] noticeIds)
    {
        for (Long noticeId : noticeIds) {
            // 删除相关活动预算
            tBranchActivityBudgetMapper.deleteTBranchActivityBudgetByNoticeId(noticeId);
        }
        return tBranchActivityNoticeMapper.deleteTBranchActivityNoticeByNoticeIds(noticeIds);
    }

    /**
     * 删除支部活动预告信息
     * 
     * @param noticeId 支部活动预告主键
     * @return 结果
     */
    @Override
    public int deleteTBranchActivityNoticeByNoticeId(Long noticeId)
    {
        // 删除相关活动预算
        tBranchActivityBudgetMapper.deleteTBranchActivityBudgetByNoticeId(noticeId);
        return tBranchActivityNoticeMapper.deleteTBranchActivityNoticeByNoticeId(noticeId);
    }
}
