package org.jeecg.modules.stm.bcs.handle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.stm.bcs.handle.entity.*;
import org.jeecg.modules.stm.bcs.handle.mapper.BcsRiskEventMapper;
import org.jeecg.modules.stm.bcs.handle.service.*;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

/**
 * @Description: 应急处置管理
 * @Author: jeecg-boot
 * @Date:   2025-09-25
 * @Version: V1.0
 */
@Service
public class BcsRiskEventServiceImpl extends ServiceImpl<BcsRiskEventMapper, BcsRiskEvent> implements IBcsRiskEventService {
    @Autowired
    private IBcsEventOrgService bcsEventOrgService;
    @Autowired
    private IBcsEventStepService bcsEventStepService;

    @Autowired
    private IBcsHandleOrgService bcsHandleOrgService;
    @Autowired
    private IBcsHandleMethodService bcsHandleMethodService;

    public void saveAndOrgAndStep(BcsRiskEvent bcsRiskEvent){
        this.save(bcsRiskEvent);
        //System.out.println(bcsRiskEvent.getId());
        QueryWrapper<BcsEventOrg> bcsEventOrgQueryWrapper = new QueryWrapper<>();
        bcsEventOrgQueryWrapper.eq("event_tp",bcsRiskEvent.getTp());
        bcsEventOrgQueryWrapper.eq("level",bcsRiskEvent.getLevel());
        List<BcsEventOrg> orglist = bcsEventOrgService.list(bcsEventOrgQueryWrapper);
        for(int i=0;i<orglist.size();i++){
            BcsEventOrg orgb=orglist.get(i);
            //System.out.println("-------------"+orgb.getDepart()+"   "+orgb.getDuty());
            BcsHandleOrg horg=new BcsHandleOrg();
            horg.setEventId(bcsRiskEvent.getId());
            horg.setUname(orgb.getUname());
            horg.setOrg(orgb.getOrg());
            horg.setDepart(orgb.getDepart());
            horg.setPosition(orgb.getPosition());
            horg.setPerLevel(orgb.getPerLevel());
            horg.setTel(orgb.getTel());
            horg.setDuty(orgb.getDuty());

            horg.setCreateBy(bcsRiskEvent.getCreateBy());
            horg.setCreateTime(bcsRiskEvent.getCreateTime());
            horg.setSysOrgCode(bcsRiskEvent.getSysOrgCode());
            bcsHandleOrgService.save(horg);

        }

        QueryWrapper<BcsEventStep> bcsEventStepQueryWrapper = new QueryWrapper<>();
        bcsEventStepQueryWrapper.eq("event_tp",bcsRiskEvent.getTp());
        bcsEventStepQueryWrapper.eq("level",bcsRiskEvent.getLevel());
        List<BcsEventStep> steplist = bcsEventStepService.list(bcsEventStepQueryWrapper);
        for(int j=0;j<steplist.size();j++){
            BcsEventStep stb=steplist.get(j);
            BcsHandleMethod st=new BcsHandleMethod();
            st.setEventId(bcsRiskEvent.getId());
            st.setHandleDes(stb.getHandleDes());
            st.setStep(stb.getStep());
            st.setExeOrg(stb.getExeOrg());
            st.setExeDepart(stb.getExeDepart());
            st.setExeRole(stb.getExeRole());
            st.setTel(stb.getTel());
            st.setDes(stb.getDes());
            st.setCreateBy(bcsRiskEvent.getCreateBy());
            st.setCreateTime(bcsRiskEvent.getCreateTime());
            st.setSysOrgCode(bcsRiskEvent.getSysOrgCode());
            bcsHandleMethodService.save(st);


        }


    };
}
