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

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.ruoyi.project.plant.domain.*;
import com.ruoyi.project.plant.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.plant.service.ITMtMeetingService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 装置会议Service业务层处理
 *
 * @author ruoyi
 * @date 2020-12-30
 */
@Service
public class TMtMeetingServiceImpl implements ITMtMeetingService
{
    @Resource
    private TMtMeetingMapper tMtMeetingMapper;
    @Resource
    private TMtActionlistMapper tMtActionlistMapper;
    @Resource
    private TMtKeymaintenanceMapper tMtKeymaintenanceMapper;
    @Resource
    private TMtEhsopenitemMapper tMtEhsopenitemMapper;
    @Resource
    private TMtMessageattentionMapper tMtMessageattentionMapper;
    @Resource
    private TMtInterlockMapper tMtInterlockMapper;
    @Resource
    private TMtRiskreminderMapper tMtRiskreminderMapper;
    @Resource
    private TMtOpextrackMapper tMtOpextrackMapper;
    @Resource
    private TMtPersonMapper tMtPersonMapper;
    /**
     * 查询装置会议
     *
     * @param id 装置会议ID
     * @return 装置会议
     */
    @Override
    public TMtMeeting selectTMtMeetingById(Long id)
    {
        return tMtMeetingMapper.selectTMtMeetingById(id);
    }

    @Override
    public TMtMeeting selectTMtMeetingLast(TMtMeeting tMtMeeting) {
        return  tMtMeetingMapper.selectTMtMeetingLast(tMtMeeting);
    }

    /**
     * 查询装置会议列表
     *
     * @param tMtMeeting 装置会议
     * @return 装置会议
     */
    @Override
    public List<TMtMeeting> selectTMtMeetingList(TMtMeeting tMtMeeting)
    {
        return tMtMeetingMapper.selectTMtMeetingList(tMtMeeting);
    }

    /**
     * 新增装置会议
     *
     * @param tMtMeeting 装置会议
     * @return 结果
     */
    @Override
    @Transactional
    public int insertTMtMeeting(TMtMeeting tMtMeeting)
    {
        tMtMeetingMapper.insertTMtMeeting(tMtMeeting);

        TMtActionlist t = new TMtActionlist();
        t.setIsHis(0l);
        List<TMtActionlist> actionlists =  tMtActionlistMapper.selectTMtActionlistList(t);
        for (TMtActionlist a : actionlists
             ) {
            if ("3".equals(a.getStatus())  ) { //已经完成的项目删除
                tMtActionlistMapper.deleteTMtActionlistById(a.getId());
            }

            a.setMeetingId(tMtMeeting.getId());
            a.setIsHis(1l);
            if (areDatesInSameWeek(a.getUpdatedate(),tMtMeeting.getMeetingDate()) == true) {
                a.setIsNew(1l);
            }
            tMtActionlistMapper.insertTMtActionlist(a);

        }

        TMtEhsopenitem tMtEhsopenitem = new TMtEhsopenitem();
        tMtEhsopenitem.setIsHis(0l);
        List<TMtEhsopenitem> ehsopenitemList = tMtEhsopenitemMapper.selectTMtEhsopenitemList(tMtEhsopenitem);
        for (TMtEhsopenitem m : ehsopenitemList
        ) {
            m.setMeetingId(tMtMeeting.getId());
            m.setIsHis(1l);
            if (areDatesInSameWeek(m.getUpdatedate(),tMtMeeting.getMeetingDate()) == true) {
                m.setIsNew(1l);
            }
            tMtEhsopenitemMapper.insertTMtEhsopenitem(m);
        }

        TMtKeymaintenance tk = new TMtKeymaintenance();
        tk.setIsHis(0l);
        List<TMtKeymaintenance> tMtKeymaintenanceList = tMtKeymaintenanceMapper.selectTMtKeymaintenanceList(tk);
        for (TMtKeymaintenance m : tMtKeymaintenanceList
             ) {
            //已经完成的项目删除
            if ("3".equals(m.getStatus())  ) { //已经完成的项目删除
                tMtKeymaintenanceMapper.deleteTMtKeymaintenanceById(m.getId());
            }
            m.setMeetingId(tMtMeeting.getId());
            m.setIsHis(1l);
            if (areDatesInSameWeek(m.getUpdatedate(),tMtMeeting.getMeetingDate()) == true) {
                m.setIsNew(1l);
            }
            tMtKeymaintenanceMapper.insertTMtKeymaintenance(m);

        }

        TMtMessageattention tMtMessageattention = new TMtMessageattention();
        tk.setIsHis(0l);
        List<TMtMessageattention> tMtMessageattentionList = tMtMessageattentionMapper.selectTMtMessageattentionList(tMtMessageattention);
        for (TMtMessageattention m : tMtMessageattentionList
        ) {
            //已经完成的项目删除
            if ("3".equals(m.getStatus())  ) { //已经完成的项目删除
                tMtMessageattentionMapper.deleteTMtMessageattentionById(m.getId());
            }
            m.setMeetingId(tMtMeeting.getId());
            m.setIsHis(1l);
            if (areDatesInSameWeek(m.getUpdatedate(),tMtMeeting.getMeetingDate()) == true) {
                m.setIsNew(1l);
            }
            tMtMessageattentionMapper.insertTMtMessageattention(m);

        }

        TMtInterlock tMtInterlock = new TMtInterlock();
        tk.setIsHis(0l);
        List<TMtInterlock> tMtInterlockList = tMtInterlockMapper.selectTMtInterlockList(tMtInterlock);
        for (TMtInterlock m : tMtInterlockList
        ) {
            m.setMeetingId(tMtMeeting.getId());
            m.setIsHis(1l);
            if (areDatesInSameWeek(m.getUpdatedate(),tMtMeeting.getMeetingDate()) == true) {
                m.setIsNew(1l);
            }
            tMtInterlockMapper.insertTMtInterlock(m);
        }

        TMtRiskreminder tMtRiskreminder = new TMtRiskreminder();
        tk.setIsHis(0l);
        List<TMtRiskreminder> tMtRiskreminderList = tMtRiskreminderMapper.selectTMtRiskreminderList(tMtRiskreminder);
        for (TMtRiskreminder m : tMtRiskreminderList
        ) {
            m.setMeetingId(tMtMeeting.getId());
            m.setIsHis(1l);
            if (areDatesInSameWeek(m.getUpdatedate(),tMtMeeting.getMeetingDate()) == true) {
                m.setIsNew(1l);
            }
            tMtRiskreminderMapper.insertTMtRiskreminder(m);
        }

        TMtOpextrack tMtOpextrack = new TMtOpextrack();
        tk.setIsHis(0l);
        List<TMtOpextrack> tMtOpextrackList = tMtOpextrackMapper.selectTMtOpextrackList(tMtOpextrack);
        for (TMtOpextrack m : tMtOpextrackList
        ) {
            m.setMeetingId(tMtMeeting.getId());
            m.setIsHis(1l);
            if (areDatesInSameWeek(m.getUpdatedate(),tMtMeeting.getMeetingDate()) == true) {
                m.setIsNew(1l);
            }
            tMtOpextrackMapper.insertTMtOpextrack(m);
        }

        TMtPerson tMtPerson = new TMtPerson();
        tk.setIsHis(0l);
        List<TMtPerson> tMtPersonList = tMtPersonMapper.selectTMtPersonList(tMtPerson);
        for (TMtPerson m : tMtPersonList
        ) {
            m.setMeetingId(tMtMeeting.getId());
            m.setIsHis(1l);
            tMtPersonMapper.insertTMtPerson(m);
        }
        return 1;
    }

    public static boolean areDatesInSameWeek(Date date1, Date date2) {
        if (date1 == null || date2 == null) {
            return false; // 如果日期为空，返回false
        }

        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();

        cal1.setTime(date1);
        cal2.setTime(date2);

        int week1 = cal1.get(Calendar.WEEK_OF_YEAR);
        int week2 = cal2.get(Calendar.WEEK_OF_YEAR);
        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);

        return (year1 == year2) && (week1 == week2);
    }

    /**
     * 修改装置会议
     *
     * @param tMtMeeting 装置会议
     * @return 结果
     */
    @Override
    public int updateTMtMeeting(TMtMeeting tMtMeeting)
    {
        return tMtMeetingMapper.updateTMtMeeting(tMtMeeting);
    }

    /**
     * 批量删除装置会议
     *
     * @param ids 需要删除的装置会议ID
     * @return 结果
     */
    @Override
    public int deleteTMtMeetingByIds(Long[] ids)
    {
        return tMtMeetingMapper.deleteTMtMeetingByIds(ids);
    }

    /**
     * 删除装置会议信息
     *
     * @param id 装置会议ID
     * @return 结果
     */
    @Override
    public int deleteTMtMeetingById(Long id)
    {
        return tMtMeetingMapper.deleteTMtMeetingById(id);
    }
}
