package com.eactive.eai.rms.bat.loader.intf;

import com.eactive.eai.rms.bat.loader.intf.dao.InterfaceDeployDao;
import com.eactive.eai.rms.bat.loader.intf.deploy.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("BatInterfaceDeployService")
@Transactional
public class InterfaceDeployService {

    @Autowired @Qualifier("BatInterfaceDeployDao")
    private InterfaceDeployDao interfaceDeployDao;

    public InterfaceDeploy selectDeploy(String intfid) {

        final Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("intfid", intfid);

        InterfaceDeploy interfaceDeploy = interfaceDeployDao.selectDeploy(paramMap, InterfaceDeploy.class);

        List<DbStepDeploy> dbStepDeployList = interfaceDeployDao.selectDeployList(paramMap, DbStepDeploy.class);
        for (DbStepDeploy dbStepDeploy : dbStepDeployList) {
            paramMap.put("intfidseq", dbStepDeploy.getIntfidseq());
            dbStepDeploy.getLayoutDeployList().addAll(interfaceDeployDao.selectDeployList(paramMap, LayoutDeploy.class));
        }
        interfaceDeploy.getDbStepDeployList().addAll(dbStepDeployList);

        List<FileStepDeploy> fileStepDeployList = interfaceDeployDao.selectDeployList(paramMap, FileStepDeploy.class);
        for (FileStepDeploy fileStepDeploy : fileStepDeployList) {
            paramMap.put("intfidseq", fileStepDeploy.getIntfidseq());
            fileStepDeploy.getLayoutDeployList().addAll(interfaceDeployDao.selectDeployList(paramMap, LayoutDeploy.class));
        }
        interfaceDeploy.getFileStepDeployList().addAll(fileStepDeployList);

        List<IntfScheMappingDeploy> intfScheMappingDeployList = interfaceDeployDao.selectDeployList(paramMap, IntfScheMappingDeploy.class);
        for(IntfScheMappingDeploy intfScheMappingDeploy : intfScheMappingDeployList) {
            paramMap.put("bjobmsgscheid", intfScheMappingDeploy.getBjobmsgscheid());
            intfScheMappingDeploy.setScheduleDeploy(interfaceDeployDao.selectDeploy(paramMap, ScheduleDeploy.class));
        }
        interfaceDeploy.getIntfScheMappingDeployList().addAll(intfScheMappingDeployList);

        return interfaceDeploy;
    }

    public void saveDeploy(InterfaceDeploy interfaceDeploy) {

        final Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("intfid", interfaceDeploy.getIntfid());

        /** DELETE **/
        for (DbStepDeploy dbStepDeploy : interfaceDeploy.getDbStepDeployList()) {
            paramMap.put("intfidseq", dbStepDeploy.getIntfidseq());
            interfaceDeployDao.deleteAllDeploy(paramMap, LayoutDeploy.class);
        }
        for (FileStepDeploy fileStepDeploy : interfaceDeploy.getFileStepDeployList()) {
            paramMap.put("intfidseq", fileStepDeploy.getIntfidseq());
            interfaceDeployDao.deleteAllDeploy(paramMap, LayoutDeploy.class);
        }
        for (IntfScheMappingDeploy intfScheMappingDeploy : interfaceDeploy.getIntfScheMappingDeployList()) {
            paramMap.put("bjobmsgscheid", intfScheMappingDeploy.getBjobmsgscheid());
            interfaceDeployDao.deleteAllDeploy(paramMap, ScheduleDeploy.class);
        }
        interfaceDeployDao.deleteAllDeploy(paramMap, InterfaceDeploy.class);
        interfaceDeployDao.deleteAllDeploy(paramMap, DbStepDeploy.class);
        interfaceDeployDao.deleteAllDeploy(paramMap, FileStepDeploy.class);
        interfaceDeployDao.deleteAllDeploy(paramMap, IntfScheMappingDeploy.class);

        /** INSERT **/
        interfaceDeployDao.insertDeploy(interfaceDeploy);
        for (DbStepDeploy dbStepDeploy : interfaceDeploy.getDbStepDeployList()) {
            interfaceDeployDao.insertDeploy(dbStepDeploy);
            for (LayoutDeploy layoutDeploy : dbStepDeploy.getLayoutDeployList()) {
                interfaceDeployDao.insertDeploy(layoutDeploy);
            }
        }
        for (FileStepDeploy fileStepDeploy : interfaceDeploy.getFileStepDeployList()) {
            interfaceDeployDao.insertDeploy(fileStepDeploy);
            for (LayoutDeploy layoutDeploy : fileStepDeploy.getLayoutDeployList()) {
                interfaceDeployDao.insertDeploy(layoutDeploy);
            }
        }
        for (IntfScheMappingDeploy intfScheMappingDeploy : interfaceDeploy.getIntfScheMappingDeployList()) {
            interfaceDeployDao.insertDeploy(intfScheMappingDeploy);
            interfaceDeployDao.insertDeploy(intfScheMappingDeploy.getScheduleDeploy());
        }

    }

}
