package com.ruoyi.project.plant.service.impl;

import java.util.List;

import com.ruoyi.project.plant.domain.TMtdItem;
import com.ruoyi.project.plant.mapper.TMtdItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.plant.mapper.TMtdDailyMapper;
import com.ruoyi.project.plant.domain.TMtdDaily;
import com.ruoyi.project.plant.service.ITMtdDailyService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 调度会议Service业务层处理
 *
 * @author ssy
 * @date 2023-11-14
 */
@Service
public class TMtdDailyServiceImpl implements ITMtdDailyService
{
    @Resource
    private TMtdDailyMapper tMtdDailyMapper;
    @Resource
    private TMtdItemMapper tMtdItemMapper;

    /**
     * 查询调度会议
     *
     * @param id 调度会议ID
     * @return 调度会议
     */
    @Override
    public TMtdDaily selectTMtdDailyById(Long id)
    {
        return tMtdDailyMapper.selectTMtdDailyById(id);
    }

    /**
     * 查询调度会议列表
     *
     * @param tMtdDaily 调度会议
     * @return 调度会议
     */
    @Override
    public List<TMtdDaily> selectTMtdDailyList(TMtdDaily tMtdDaily)
    {
        return tMtdDailyMapper.selectTMtdDailyList(tMtdDaily);
    }

    /**
     * 新增调度会议
     *
     * @param tMtdDaily 调度会议
     * @return 结果
     */
    @Override
    public int insertTMtdDaily(TMtdDaily tMtdDaily)
    {
        tMtdDailyMapper.insertTMtdDaily(tMtdDaily);
        insertItem(tMtdDaily);
        return 1;
    }

    /**
     * 修改调度会议
     *
     * @param tMtdDaily 调度会议
     * @return 结果
     */
    @Override
    @Transactional
    public int updateTMtdDaily(TMtdDaily tMtdDaily)
    {
        tMtdDailyMapper.updateTMtdDaily(tMtdDaily);
        tMtdItemMapper.deleteTMtdItemByDailyId(tMtdDaily.getId());
        insertItem(tMtdDaily);

        return tMtdDailyMapper.updateTMtdDaily(tMtdDaily);
    }

    private void insertItem(TMtdDaily tMtdDaily) {
        List<TMtdItem> items0 = tMtdDaily.getItems0();
        if (items0 != null) {

        }

        List<TMtdItem> items1 = tMtdDaily.getItems1();
        if (items1 != null) {
            for (TMtdItem t: items1
            ) {
                t.setItemType(1);
                t.setDailyId(tMtdDaily.getId());
                tMtdItemMapper.insertTMtdItem(t);
            }
        }

        List<TMtdItem> items2 = tMtdDaily.getItems2();
        if (items2 != null) {
            for (TMtdItem t: items2
            ) {
                t.setItemType(2);
                t.setDailyId(tMtdDaily.getId());
                tMtdItemMapper.insertTMtdItem(t);
            }
        }


        List<TMtdItem> items3 = tMtdDaily.getItems3();
        if (items3 != null) {
            for (TMtdItem t: items3
            ) {
                t.setItemType(3);
                t.setDailyId(tMtdDaily.getId());
                tMtdItemMapper.insertTMtdItem(t);
            }
        }

        List<TMtdItem> items4 = tMtdDaily.getItems4();
        if (items4 != null) {
            for (TMtdItem t: items4
            ) {
                t.setItemType(4);
                t.setDailyId(tMtdDaily.getId());
                tMtdItemMapper.insertTMtdItem(t);
            }
        }

        List<TMtdItem> items5 = tMtdDaily.getItems5();
        if (items5 != null) {
            for (TMtdItem t: items5
            ) {
                t.setItemType(5);
                t.setDailyId(tMtdDaily.getId());
                tMtdItemMapper.insertTMtdItem(t);
            }
        }

    }

    /**
     * 批量删除调度会议
     *
     * @param ids 需要删除的调度会议ID
     * @return 结果
     */
    @Override
    public int deleteTMtdDailyByIds(Long[] ids)
    {
        return tMtdDailyMapper.deleteTMtdDailyByIds(ids);
    }

    /**
     * 删除调度会议信息
     *
     * @param id 调度会议ID
     * @return 结果
     */
    @Override
    public int deleteTMtdDailyById(Long id)
    {
        return tMtdDailyMapper.deleteTMtdDailyById(id);
    }
}
