//package com.eactive.eai.rms.onl.manage.serviceinfo.osidinsti;
//
//import java.util.List;
//
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Qualifier;
//import org.springframework.stereotype.Service;
//
//import com.eactive.eai.rms.common.base.BaseService;
//
//@Service("osidInstiService")
//public class OsidInstiService extends BaseService {
//
//    @Autowired
//    @Qualifier("osidInstiDao")
//    private OsidInstiDao osidInstiDaoBean;
//
//    public List<OsidInstiVo> findList(OsidInstiVo vo) throws Exception {
//    	vo.setTotalCount(osidInstiDaoBean.count(vo));
//        return osidInstiDaoBean.findList(vo);
//    }
//
//	public List<OsidInstiSubVo> findListSub(String adptrBzwkGroupName, String osidInstiNo) {
//		return osidInstiDaoBean.findListSub(adptrBzwkGroupName, osidInstiNo);
//	}
//	
//    public int insertOsidInsti(OsidInstiVo vo) throws Exception {
//    	return osidInstiDaoBean.insertOsidInsti(vo);
//    }
//
//    public int insertOsidInstiSub(OsidInstiSubVo vo) throws Exception {
//    	return osidInstiDaoBean.insertOsidInstiSub(vo);
//    }
//
//    public int updateOsidInstiSub(OsidInstiSubVo vo) throws Exception {
//    	return osidInstiDaoBean.updateOsidInstiSub(vo);
//    }
//
//    public int deleteOsidInstiSub(OsidInstiSubVo vo) throws Exception {
//    	return osidInstiDaoBean.deleteOsidInstiSub(vo);
//    }
//
//    public int updateOsidInsti(OsidInstiVo vo) throws Exception {
//    	return osidInstiDaoBean.updateOsidInsti(vo);
//    }
//
//    public int deleteOsidInsti(OsidInstiVo vo) throws Exception {
//    	return osidInstiDaoBean.deleteOsidInsti(vo);
//    }
//}

package com.eactive.eai.rms.onl.manage.serviceinfo.osidinsti;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eactive.eai.data.entity.onl.adapter.Adapter;
import com.eactive.eai.data.entity.onl.adapter.AdapterGroup;
import com.eactive.eai.data.entity.onl.adapter.AdapterProp;
import com.eactive.eai.data.entity.onl.adapter.AdapterPropGroup;
import com.eactive.eai.data.entity.onl.unifbwk.UnifBwkTp;
import com.eactive.eai.rms.common.base.BaseService;
import com.eactive.eai.rms.data.entity.onl.adapter.AdapterService;
import com.eactive.eai.rms.data.entity.onl.adapter.OsidinstiSearch;
import com.eactive.eai.rms.data.entity.onl.unifbwk.UnifBwkTpService;



@Service
@Transactional
public class OsidInstiManService extends BaseService {
    
    private AdapterService adapterService;
    
    private UnifBwkTpService unifBwkTpService;

    @Autowired
    public OsidInstiManService(AdapterService adapterService, UnifBwkTpService unifBwkTpService) {
    	this.adapterService = adapterService;
    	this.unifBwkTpService = unifBwkTpService;
    }
    
    public Page<OsidinstiUI> selectList(Pageable pageable, OsidinstiSearch osidinstiSearch) {
    	Page<Adapter> adapters = adapterService.selectList(pageable, osidinstiSearch);
		return adapters.map(this::convertToOsidInstiUI);
	}

	private OsidinstiUI convertToOsidInstiUI(Adapter adapter) {
		AdapterGroup adapterGroup = adapter.getAdapterGroup();
		AdapterPropGroup adapterPropGroup = adapter.getAdapterPropGroup();
		
		OsidinstiUI ui = new OsidinstiUI();
		ui.setAdptrBzwkGroupName(adapterGroup.getAdptrbzwkgroupname());
		ui.setOsidInstiNo(adapterGroup.getOsidinstino());
		ui.setInstiName(adapterGroup.getAdptrbzwkgroupdesc());
		ui.setEaiBzwkDstcd(adapterGroup.getEaibzwkdstcd());
		ui.setPrptyGroupName(adapterPropGroup.getPrptygroupname());
		
		unifBwkTpService.findById(adapterGroup.getEaibzwkdstcd())
							   .ifPresent(unifBwkTp -> ui.setEaiBzwkHanglName(unifBwkTp.getBzwkdsticname()));
		
		setIpAndPort(ui, adapterPropGroup.getAdapterProps());
		return ui;
	}
    
	private void setIpAndPort(OsidinstiUI ui, List<AdapterProp> adapterProps) {
		String ip = getPropertyValue(adapterProps, "host.name");
		String port = getPropertyValue(adapterProps, "port.number");
		String url = getPropertyValue(adapterProps, "URL");
		
		ui.setIpNum(StringUtils.isBlank(ip) ? url : ip);
		ui.setPortNum(StringUtils.isBlank(port) ? url : port);
	}

	private String getPropertyValue(List<AdapterProp> adapterProps, String propertyName) {
			return adapterProps.stream()
										.filter(adapterProp -> propertyName.equals(adapterProp.getId().getPrptyname()))
										.findFirst()
										.map(AdapterProp::getPrpty2val)
										.orElse(null);
	}

	public OsidinstiUI selectDetail(String adptrBzwkGroupName) {
		AdapterGroup adapterGroup = adapterService.findAdapter(adptrBzwkGroupName).getAdapterGroup();
		
		OsidinstiUI osidinstiUI = new OsidinstiUI();
		osidinstiUI.setAdptrBzwkGroupName(adapterGroup.getAdptrbzwkgroupname());
		osidinstiUI.setInstiName(adapterGroup.getAdptrbzwkgroupdesc());
		osidinstiUI.setEaiBzwkDstcd(adapterGroup.getEaibzwkdstcd());
		
		return osidinstiUI;
	}
		
    public List<OsidinstiRsEmpUI> selectDetailRsempInfo(String adptrBzwkGroupName) {
    	List<OsidinstiRsEmpUI> resultList = new ArrayList<>();
    	
    	AdapterGroup adapterGroup = adapterService.findAdapter(adptrBzwkGroupName).getAdapterGroup();
    	Optional<UnifBwkTp> unifBwkTpOptional = unifBwkTpService.findById(adapterGroup.getEaibzwkdstcd());
    	
		OsidinstiRsEmpUI ui = new OsidinstiRsEmpUI();
		ui.setKbBzwkRsempName(adapterGroup.getOsidinstibzwkrsempname());
		ui.setOsidInstiTelno(adapterGroup.getOsidinstitelno());
		
		unifBwkTpOptional.ifPresent(unifBwkTp -> {
			ui.setKbBzwkDvsnName(unifBwkTp.getTrackasiskey2name());
			ui.setKbBzwkRsempName(unifBwkTp.getRsempname());
		});
		resultList.add(ui);
		
		return resultList;
	}
    
    public List<Map<String, Object>> selectListToExcel(OsidinstiSearch osidinstiSearch) {
    	List<Adapter> adapters = adapterService.findAllAdapter(osidinstiSearch);
    	
    	return adapters.stream()
			.map(this::convertToOsidInstiUI)
			.map(ui ->  {
				Map<String, Object> map = new HashMap<>();
				map.put("ADPTRBZWKGROUPNAME",ui.getAdptrBzwkGroupName());
				map.put("PRPTYGROUPNAME", ui.getPrptyGroupName());
				map.put("OSIDINSTINO",ui.getOsidInstiNo());
				map.put("INSTINAME",ui.getInstiName());
				map.put("IPNUM", ui.getIpNum());
				map.put("PORTNUM",ui.getPortNum());
				map.put("EAIBZWKDSTCD",ui.getEaiBzwkDstcd());
				map.put("EAIBZWKHANGLNAME",ui.getEaiBzwkHanglName());
				return map;
			}).collect(Collectors.toList());
    	}
}
