package com.tsiwebserver.service.impl;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

import com.tsiwebserver.VO.*;
import com.tsiwebserver.security.TSIEncoder;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.tsiwebserver.mapper.SystemManagementMapper;
import com.tsiwebserver.service.SystemManagementService;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
@RequiredArgsConstructor
public class SystemManagementServiceImpl implements SystemManagementService {

	private final SystemManagementMapper systemManagementMapper;

	@Override
	public List<LinkVO> getSearchAllLink() {
		return this.systemManagementMapper.getSearchAllLink();
	}

	@Override
	public List<String> getSearchAllLinkId() {
		return this.systemManagementMapper.getSearchAllLinkId();
	}

	@Override
	public int insertLink(JSONArray array) {
		int affectedRow = 0;

		for(int i = 0; i < array.size(); ++i) {
			LinkVO vo = new LinkVO();
			JSONObject ob = (JSONObject)array.get(i);
			vo.setLinkId(Long.parseLong((String)ob.get("linkId")));
			vo.setNodeid(Long.parseLong((String)ob.get("nodeid")));
			vo.setDir(Long.parseLong((String)ob.get("dir")));
			vo.setRoadName((String)ob.get("roadName"));
			vo.setLinkInfo((String)ob.get("linkInfo"));
			affectedRow += this.systemManagementMapper.insertLink(vo);
		}

		return affectedRow;
	}

	@Override
	public int linkDelete(String nodeid) {
		int affectedRow = 0;
		String[] nodeidArr = nodeid.split(",");

		for(int i = 0; i < nodeidArr.length; ++i) {
			affectedRow += this.systemManagementMapper.linkDelete(nodeidArr[i]);
		}

		return affectedRow;
	}

	@Override
	public List<BaseVO> getAllBase() {
		return this.systemManagementMapper.getAllBase();
	}

	@Override
	public List<BaseVO> getSearchAllBase(String city, String gu, String intersection, String nodeid) {
		Map<String, Object> list = new HashMap();
		list.put("city", city.isEmpty() ? null : city);
		list.put("gu", gu.isEmpty() ? null : gu);
		list.put("intersection", intersection.isEmpty() ? null : intersection);
		list.put("nodeid", nodeid.isEmpty() ? null : nodeid);
		List<BaseVO> baseList =  this.systemManagementMapper.getSearchAllBase(list);
		//log.info("baseList : {}",baseList);
		if (baseList.size() > 0) {
			List<LinkVO> listList = this.systemManagementMapper.getSearchAllLink();
			if (listList.size() > 0) {
				Map<Long, List<LinkVO>> map = new HashMap<>();
				for (LinkVO vo : listList) {
					if (map.get(vo.getNodeid()) == null) {
						map.put(vo.getNodeid(), new ArrayList<LinkVO>());
					}
					map.get(vo.getNodeid()).add(vo);
				}
				for (BaseVO vo :  baseList) {
					if (map.get(vo.getNodeid()) == null) {
						continue;
					}
					vo.setLinkList(map.get(vo.getNodeid()));
				}
			}
		}
		return baseList;
	}

	@Override
	public List<String> getBaseCityName() {
		return this.systemManagementMapper.getBaseCityName();
	}

	@Override
	public List<String> getBaseGuName(String city) {
		return this.systemManagementMapper.getBaseGuName(city);
	}

	@Override
	public List<String> getBaseIntersection(String city, String gu) {
		Map<String, Object> list = new HashMap();
		list.put("city", city);
		list.put("gu", gu);
		return this.systemManagementMapper.getBaseIntersection(list);
	}

	@Override
	public List<String> getBaseNodeId() {
		return this.systemManagementMapper.getBaseNodeId();
	}

	@Override
	public int insertBase(JSONArray array) {
		int affectedRow = 0;

		for(int i = 0; i < array.size(); ++i) {
			BaseVO vo = new BaseVO();
			JSONObject ob = (JSONObject)array.get(i);
			vo.setTscNo(Long.parseLong((String)ob.get("tscNo")));
			vo.setNodeid(Long.parseLong((String)ob.get("nodeid")));
			vo.setName((String)ob.get("name"));
			vo.setLatitude(Double.parseDouble((String)ob.get("latitude")));
			vo.setLongitude(Double.parseDouble((String)ob.get("longitude")));
			vo.setAddr1((String)ob.get("addr1"));
			vo.setAddr2((String)ob.get("addr2"));
			vo.setAddr3((String)ob.get("addr3"));
			vo.setAddnode((String)ob.get("addnode"));
			vo.setIpaddr((String)ob.get("ipaddr"));
			vo.setUseyn((String)ob.get("useyn"));
			vo.setCvimyn((String)ob.get("cvimyn"));
			vo.setNodeyn((String)ob.get("nodeyn"));
			vo.setTestyn((String)ob.get("testyn"));
			vo.setNodetype(Integer.parseInt((String)ob.get("nodetype")));
			vo.setRegionId((String)ob.get("regionId"));
			vo.setTscCompany((String)ob.get("tscCompany"));
			vo.setCpuVer((String)ob.get("cpuVer"));
			vo.setModemCompany((String)ob.get("modemCompany"));
			vo.setTelecomCompany((String)ob.get("telecomCompany"));
			affectedRow += this.systemManagementMapper.insertBase(vo);
		}

		return affectedRow;
	}

	@Override
	public int baseUpdate(JSONObject ob) {
		List<BaseVO> list = this.systemManagementMapper.getAllBase();
		boolean result = false;
		int affectedRow = 0;
		BaseVO vo = new BaseVO();
		vo.setTscNo(Long.parseLong((String)ob.get("tscNo")));
		vo.setNodeid(Long.parseLong((String)ob.get("nodeid")));
		vo.setName((String)ob.get("name"));
		vo.setLatitude(Double.parseDouble((String)ob.get("latitude")));
		vo.setLongitude(Double.parseDouble((String)ob.get("longitude")));
		vo.setAddr1((String)ob.get("addr1"));
		vo.setAddr2((String)ob.get("addr2"));
		vo.setAddr3((String)ob.get("addr3"));
		vo.setAddnode((String)ob.get("addnode"));
		vo.setIpaddr((String)ob.get("ipaddr"));
		vo.setUseyn((String)ob.get("useyn"));
		vo.setCvimyn((String)ob.get("cvimyn"));
		vo.setNodeyn((String)ob.get("nodeyn"));
		vo.setTestyn((String)ob.get("testyn"));
		vo.setNodetype(Integer.parseInt((String)ob.get("nodetype")));
		vo.setRegionId((String)ob.get("regionId"));
		vo.setTscCompany((String)ob.get("tscCompany"));
		vo.setCpuVer((String)ob.get("cpuVer"));
		vo.setModemCompany((String)ob.get("modemCompany"));
		vo.setTelecomCompany((String)ob.get("telecomCompany"));

		for(int j = 0; j < list.size(); ++j) {
			if (Objects.equals(((BaseVO)list.get(j)).getNodeid(), vo.getNodeid())) {
				result = Objects.equals(list.get(j), vo);
			}
		}

		if (!result) {
			affectedRow += this.systemManagementMapper.baseUpdate(vo);
		}

		return affectedRow;
	}

	@Override
	public int baseMultiUpdate(JSONArray array) {
		List<BaseVO> list = this.systemManagementMapper.getAllBase();
		int affectedRow = 0;

		for(int i = 0; i < array.size(); ++i) {
			boolean result = false;
			BaseVO vo = new BaseVO();
			JSONObject ob = (JSONObject)array.get(i);
			vo.setTscNo(Long.parseLong((String)ob.get("tscNo")));
			vo.setNodeid(Long.parseLong((String)ob.get("nodeid")));
			vo.setName((String)ob.get("name"));
			vo.setLatitude(Double.parseDouble((String)ob.get("latitude")));
			vo.setLongitude(Double.parseDouble((String)ob.get("longitude")));
			vo.setAddr1((String)ob.get("addr1"));
			vo.setAddr2((String)ob.get("addr2"));
			vo.setAddr3((String)ob.get("addr3"));
			vo.setAddnode((String)ob.get("addnode"));
			vo.setIpaddr((String)ob.get("ipaddr"));
			vo.setUseyn((String)ob.get("useyn"));
			vo.setCvimyn((String)ob.get("cvimyn"));
			vo.setNodeyn((String)ob.get("nodeyn"));
			vo.setTestyn((String)ob.get("testyn"));
			vo.setNodetype(Integer.parseInt((String)ob.get("nodetype")));
			vo.setRegionId((String)ob.get("regionId"));
			vo.setTscCompany((String)ob.get("tscCompany"));
			vo.setCpuVer((String)ob.get("cpuVer"));
			vo.setModemCompany((String)ob.get("modemCompany"));
			vo.setTelecomCompany((String)ob.get("telecomCompany"));

			for(int j = 0; j < list.size(); ++j) {
				if (Objects.equals(((BaseVO)list.get(j)).getNodeid(), vo.getNodeid())) {
					result = Objects.equals(list.get(j), vo);
				}
			}

			if (!result) {
				affectedRow += this.systemManagementMapper.baseUpdate(vo);
			}
		}

		return affectedRow;
	}

	@Override
	public int baseDelete(String nodeid) {
		int affectedRow = 0;
		String[] nodeidArr = nodeid.split(",");

		for(int i = 0; i < nodeidArr.length; ++i) {
			affectedRow += this.systemManagementMapper.baseDelete(nodeidArr[i]);
		}

		return affectedRow;
	}

	@Override
	public List<String> getCityName() {
		return systemManagementMapper.getCityName();
	}

	@Override
	public List<String> getGuName(String city) {
		return systemManagementMapper.getGuName(city);
	}

	@Override
	public List<String> getIntersection(String city, String gu) {
		Map<String, Object> list = new HashMap<String, Object>();
		
		list.put("city", city);
		list.put("gu", gu);
		
		if (gu.equals("(비고)")) {
			return systemManagementMapper.getNullIntersection(list);
		}

		return systemManagementMapper.getIntersection(list);
	}

	@Override
	public List<NodeVO> getSearchAll() {
		return systemManagementMapper.getSearchAllNode();
	}

	@Override
	public List<NodeVO> getSearchWithCity(String city) {
		return systemManagementMapper.getSearchWithCity(city);
	}

	@Override
	public List<NodeVO> getSearchWithCityAndGu(String city, String gu) {
		Map<String, Object> list = new HashMap<String, Object>();
	
		if (gu.equals("(비고)"))
			return systemManagementMapper.getSearchWithCityAndNull(city);
		
		list.put("city", city);
		list.put("gu", gu);

		return systemManagementMapper.getSearchWithCityAndGu(list);
	}

	@Override
	public List<NodeVO> getSearchWithCityAndGuAndIntersection(String city, String gu, String intersection) {
		Map<String, Object> list = new HashMap<String, Object>();
		list.put("city", city);
		list.put("gu", gu);
		list.put("intersection", intersection);
		if (gu.equals("(비고)"))
			return systemManagementMapper.getSearchWithCityAndIntersection(list);
		return systemManagementMapper.getSearchWithCityAndGuAndIntersection(list);
	}

	@Override
	public int insertNode(JSONArray array) {
		int affectedRow = 0;
		for (int i = 0; i < array.size(); i++) {
			NodeVO vo = new NodeVO();
			JSONObject ob = (JSONObject) array.get(i);
			vo.setNodeid(Long.parseLong((String)ob.get("nodeid")));
			vo.setName((String) ob.get("name"));
			vo.setLatitude(Double.parseDouble((String) ob.get("latitude")));
			vo.setLongitude(Double.parseDouble((String) ob.get("longitude")));
			vo.setAddr1((String) ob.get("addr1"));
			vo.setAddr2((String) ob.get("addr2"));
			vo.setAddr3((String) ob.get("addr3"));
			vo.setAddnode((String) ob.get("addnode"));
			vo.setIpaddr((String) ob.get("ipaddr"));
			vo.setUseyn((String) ob.get("useyn"));
			vo.setCvimyn((String) ob.get("cvimyn"));
			vo.setNodeyn((String) ob.get("nodeyn"));
			vo.setTestyn((String) ob.get("testyn"));
			vo.setNodetype(Integer.parseInt((String) ob.get("nodetype")));
			vo.setRegionId((String) ob.get("regionId"));
			affectedRow += systemManagementMapper.insertNode(vo);
		}
		return affectedRow;
	}

	@Override
	public List<String> getSearchAllNodeId() {
		return systemManagementMapper.getSearchAllNodeId();
	}

	@Override
	public int nodeUpdate(JSONObject ob) {
		List<NodeVO> list = systemManagementMapper.getAllNode();
		boolean result = false;
		int affectedRow = 0;
		
		NodeVO vo = new NodeVO();
		vo.setNodeid(Long.parseLong((String) ob.get("nodeid")));
		vo.setName((String) ob.get("name"));
		vo.setLatitude(Double.parseDouble((String) ob.get("latitude")));
		vo.setLongitude(Double.parseDouble((String) ob.get("longitude")));
		vo.setAddr1((String) ob.get("addr1"));
		vo.setAddr2((String) ob.get("addr2"));
		vo.setAddr3((String) ob.get("addr3"));
		vo.setAddnode((String) ob.get("addnode"));
		vo.setIpaddr((String) ob.get("ipaddr"));
		vo.setUseyn((String) ob.get("useyn"));
		vo.setCvimyn((String) ob.get("cvimyn"));
		vo.setNodeyn((String) ob.get("nodeyn"));
		vo.setTestyn((String) ob.get("testyn"));
		vo.setNodetype(Integer.parseInt((String) ob.get("nodetype")));
		vo.setRegionId((String) ob.get("regionId"));

		for (int j = 0; j < list.size(); j++) {
			if (Objects.equals(list.get(j).getNodeid(), vo.getNodeid())) {
				result = Objects.equals(list.get(j), vo);
			}
		}
		if (!result) {
			affectedRow += systemManagementMapper.nodeUpdate(vo);
		}
		return affectedRow;
	}

	@Override
	public int nodeMultiUpdate(JSONArray array) {
		List<NodeVO> list = this.systemManagementMapper.getAllNode();
		int affectedRow = 0;

		for(int i = 0; i < array.size(); ++i) {
			boolean result = false;
			NodeVO vo = new NodeVO();
			JSONObject ob = (JSONObject)array.get(i);
			vo.setNodeid(Long.parseLong((String)ob.get("nodeid")));
			vo.setName((String)ob.get("name"));
			vo.setLatitude(Double.parseDouble((String)ob.get("latitude")));
			vo.setLongitude(Double.parseDouble((String)ob.get("longitude")));
			vo.setAddr1((String)ob.get("addr1"));
			vo.setAddr2((String)ob.get("addr2"));
			vo.setAddr3((String)ob.get("addr3"));
			vo.setAddnode((String)ob.get("addnode"));
			vo.setIpaddr((String)ob.get("ipaddr"));
			vo.setUseyn((String)ob.get("useyn"));
			vo.setCvimyn((String)ob.get("cvimyn"));
			vo.setNodeyn((String)ob.get("nodeyn"));
			vo.setTestyn((String)ob.get("testyn"));
			vo.setNodetype(Integer.parseInt((String)ob.get("nodetype")));
			vo.setRegionId((String)ob.get("regionId"));

			for(int j = 0; j < list.size(); ++j) {
				if (Objects.equals(((NodeVO)list.get(j)).getNodeid(), vo.getNodeid())) {
					result = Objects.equals(list.get(j), vo);
				}
			}

			if (!result) {
				affectedRow += this.systemManagementMapper.nodeUpdate(vo);
			}
		}

		return affectedRow;
	}

	@Override
	public int nodeDelete(String nodeid) {
		int affectedRow = 0;
		String nodeidArr[] = nodeid.split(",");
		for (int i = 0; i < nodeidArr.length; i++) {
			affectedRow += systemManagementMapper.nodeDelete(nodeidArr[i]);
		}
		return affectedRow;
	}

	public List<TscApiClientVO> getSearchConsumerAllData() {
		return systemManagementMapper.getSearchConsumerAllData();
	}

	@Override
	public List<TscApiClientVO> getSearchCompanyData(String company) {
		return systemManagementMapper.getSearchCompanyData(company);
	}

	@Override
	public List<String> getAllConsumerToken() {
		return systemManagementMapper.getAllConsumerToken();
	}

	@SuppressWarnings("unchecked")
	@Transactional(rollbackFor=Exception.class)
	public int updateConsumer(JSONObject ob) throws Exception, ParseException {
		String tokenCrtTp = "";
		
		int affectedRow = 0;
		
		TscApiClientVO vo = new TscApiClientVO();
		
		JSONArray apitoken=(JSONArray)ob.get("apitoken");
		
		JSONArray acl = (JSONArray)ob.get("acl");
		
		Map<String, Object> map=(HashMap<String, Object>)apitoken.get(0);
		
		String apiToken=(String)map.get("apiToken");
		
		vo.setCompany((String) ob.get("company"));
		vo.setManager((String) ob.get("manager"));
		vo.setPhone((String) ob.get("phone"));
		vo.setEmail((String) ob.get("email"));
		vo.setExpiredt(
				LocalDateTime.parse((String) ob.get("expiredt"), DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss")));
		vo.setEnroller((String) ob.get("enroller"));
		vo.setIpaddr((String) ob.get("ipaddr"));
		vo.setClientType((String) ob.get("clientType"));
		vo.setBrokerId((String) ob.get("brokerId"));
		vo.setBrokerPwd((String) ob.get("brokerPwd"));
		vo.setState((String) ob.get("state"));
		vo.setUseyn((String) ob.get("useyn"));
		
		//원래 데이터 불러오기
		TscApiClientVO origin=systemManagementMapper.getOneConsumer(apiToken);
		
		List<Map<String,Object>> aclMap=(List<Map<String, Object>>)acl;
		//acl 기존꺼 삭제
		
		affectedRow=systemManagementMapper.deleteAcl(origin.getApitoken());	
		
		//api Token 설정 변경이 있을 경우
		if(map.size()>1) {
			
			//토큰 정책 설정 사항 체크
			tokenCrtTp
		   		= map.get("company").equals("") ? tokenCrtTp : tokenCrtTp + "C,"; 
			tokenCrtTp
		  		= map.get("manager").equals("") ? tokenCrtTp : tokenCrtTp + "M,"; 
			tokenCrtTp
		  		= map.get("phone").equals("") ? tokenCrtTp : tokenCrtTp + "P,"; 
			tokenCrtTp 
		  		= map.get("email").equals("") ? tokenCrtTp : tokenCrtTp + "E,";
			
			//토큰 정책 설정이 있는 경우
			if(!tokenCrtTp.equals("")) {
				Map<String,Object> list = new HashMap<String, Object>();
				
				//map에 apiToken 지우기
				map.remove("apiToken");
				
				//map의 내용으로 sha256으로 트큰 재설정
				vo.setApitoken(systemManagementMapper.getSha256(map));
				
				//기존 토큰
				list.put("apitoken", origin.getApitoken());
				
				//새로운 토큰
				list.put("newApitoken", vo.getApitoken());
				
//				log.info("apiToken{}============{}",list);
				//토큰 수정
				
				systemManagementMapper.apitokenUpdate(list);
				
				//토큰 정책 기준
				tokenCrtTp = tokenCrtTp.substring(0, tokenCrtTp.lastIndexOf(","));
				
				//vo에 세팅
				vo.setTokenCrtTp(tokenCrtTp);
				
				affectedRow=systemManagementMapper.consumerUpdate(vo);
				
			}
			
			//토큰 정책 설정이 없는 경우
			if(tokenCrtTp.isEmpty()) {
				
				vo.setApitoken(apiToken);
				
				vo.setTokenCrtTp("");
				
				affectedRow=systemManagementMapper.consumerUpdate(vo);
			
			}
			
		}
		
		//api Token 설정 변경이 없는 경우
		if(map.size() == 1) {
			//원래 토큰 정보로 수정할 정보 세팅 
			vo.setTokenCrtTp(origin.getTokenCrtTp());
			vo.setApitoken(apiToken);
			vo.setEventdt(origin.getEventdt());
			affectedRow=systemManagementMapper.consumerUpdate(vo);
		}
		
		
		for (Map<String, Object> acls : aclMap) {
			Map<String, Object> list1 = new HashMap<String, Object>();
			list1.put("apiid", acls.get("apiid"));
			list1.put("apitoken", vo.getApitoken());
			list1.put("acl", acls.get("acl"));
			list1.put("enroller", vo.getEnroller());
			systemManagementMapper.aclInsert(list1);
		}
		
		return affectedRow;
	}
	
	@SuppressWarnings("unchecked")
	public int addConsumer(JSONArray array) throws NoSuchAlgorithmException, UnsupportedEncodingException {
		int affectedRow = 0;

		for (int ii = 0; ii < array.size(); ii++) {
			TscApiClientVO vo = new TscApiClientVO();
			JSONObject ob = (JSONObject) array.get(ii);
			Map<String, Object> list = (HashMap<String, Object>) ob.get("apitoken");

			vo.setApitoken(systemManagementMapper.getSha256(list));
			vo.setCompany((String) ob.get("company"));
			vo.setManager((String) ob.get("manager"));
			vo.setEmail((String) ob.get("email"));
			vo.setPhone((String) ob.get("phone"));
			vo.setExpiredt(LocalDateTime.parse((String) ob.get("expiredt"),
					DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm")));
			vo.setEventdt(LocalDateTime.parse((String) ob.get("eventdt"),
					DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss")));
			vo.setEnroller((String) ob.get("enroller"));
			vo.setIpaddr((String) ob.get("ipaddr"));
			vo.setClientType((String) ob.get("clientType"));
			vo.setBrokerId((String) ob.get("brokerId"));
			vo.setBrokerPwd((String) ob.get("brokerPwd"));
			vo.setState((String) ob.get("state"));
			vo.setUseyn((String) ob.get("useyn"));
			
			String tokenCrtTp = "";
			tokenCrtTp = list.get("company").equals("") ? tokenCrtTp : tokenCrtTp + "C,";
			tokenCrtTp = list.get("manager").equals("") ? tokenCrtTp : tokenCrtTp + "M,";
			tokenCrtTp = list.get("phone").equals("") ? tokenCrtTp : tokenCrtTp + "P,";
			tokenCrtTp = list.get("email").equals("") ? tokenCrtTp : tokenCrtTp + "E,";
			tokenCrtTp = tokenCrtTp.substring(0, tokenCrtTp.lastIndexOf(","));
			vo.setTokenCrtTp(tokenCrtTp);
			affectedRow += systemManagementMapper.addConsumer(vo);
		}

		return affectedRow;
	}
	
	@Transactional(rollbackFor=Exception.class)
	public int consumerDelete(String apitokens) {
		int affectedRow = 0;
		String apitokenArr[] = apitokens.split(",");
		for (int ii = 0; ii < apitokenArr.length; ii++) {
			systemManagementMapper.deleteAcl(apitokenArr[ii]);
			affectedRow += systemManagementMapper.consumerDelete(apitokenArr[ii]);
		}
		return affectedRow;
	}

	@Override
	public List<Map<String, Object>> getAllRegion() {
		return systemManagementMapper.getAllRegion();
	}

	@Override
	public List<AlarmConfigVO> getSearchAlarm() {
		return systemManagementMapper.getSearchAlarm();
	}

	@Override
	public int updateAlarm(JSONArray array) {
		int affectedRow = 0;
		
		for(int i = 0; i< array.size(); i++) {
			JSONObject ob=(JSONObject)array.get(i);
			AlarmConfigVO vo= new AlarmConfigVO(); 
			vo.setAlarmCode((String)ob.get("alarmCode"));
			vo.setValue(Integer.parseInt((String)ob.get("value")));
			vo.setUseyn((String)ob.get("useyn"));
			log.info(vo.toString());
			affectedRow+=systemManagementMapper.updateAlarm(vo);
		}
		return affectedRow;
	}

	@Override
	public List<Map<String, Object>> getApiAcl(String apiToken) {
		return systemManagementMapper.getApiAcl(apiToken);
	}

	@Override
	public List<UserVO> getSearchUser() {
		return this.systemManagementMapper.getSearchUser();
	}

	@Override
	public int addUser(JSONArray array) {
		int affectedRow = 0;

		for(int i = 0; i < array.size(); ++i) {
			UserVO vo = new UserVO();
			JSONObject ob = (JSONObject)array.get(i);
			vo.setUserId((String)ob.get("userId"));
			vo.setUserName((String)ob.get("userName"));
			vo.setUserPswd(TSIEncoder.encode((String)ob.get("userPswd")));
			vo.setUserGrade((String)ob.get("userGrade"));
			vo.setUseyn((String)ob.get("useryn"));
			affectedRow += this.systemManagementMapper.addUser(vo);
		}

		return affectedRow;
	}

	@Override
	public int updateUser(JSONObject ob) {
		List<UserVO> list = this.systemManagementMapper.getSearchUser();
		boolean result = false;
		int affectedRow = 0;
		UserVO vo = new UserVO();
		vo.setUserId((String)ob.get("userId"));
		vo.setUserName((String)ob.get("userName"));
		vo.setUserPswd(TSIEncoder.encode((String)ob.get("userPswd")));
		vo.setUserGrade((String)ob.get("userGrade"));
		vo.setUseyn((String)ob.get("useryn"));

		for(int j = 0; j < list.size(); ++j) {
			if (Objects.equals(((UserVO)list.get(j)).getUserId(), vo.getUserId())) {
				result = Objects.equals(list.get(j), vo);
			}
		}

		if (!result) {
			affectedRow += this.systemManagementMapper.updateUser(vo);
		}

		return affectedRow;
	}

	@Override
	public int deleteUser(String userIds) {
		int affectedRow = 0;
		String[] userIdArr = userIds.split(",");

		for(int i = 0; i < userIdArr.length; ++i) {
			affectedRow += this.systemManagementMapper.deleteUser(userIdArr[i]);
		}

		return affectedRow;
	}

	@Override
	public List<String> getAllUserId() {
		List<UserVO> list =  this.systemManagementMapper.getAllUserId();
		List<String> userIds = new ArrayList<>();
		if (list.size() > 0) {
			for (UserVO vo : list) {
				userIds.add(vo.getUserId());
			}
		}
		return userIds;
	}

	@Override
	public List<NodeVO> getNodeData(String city, String gu, String intersection) {
//		try {
//			if (page == null) page = "1";
			if (city.isEmpty()) city = null;
			if (gu.isEmpty()) gu = null;
			if (intersection.isEmpty()) intersection = null;
			Map<String, Object> paramMap = new HashMap<>();
//			PageClass pageClass = new PageClass(Integer.parseInt(page), 16, this.systemManagementMapper.getNodeCount());
//			paramMap.put("page", pageClass.getFirstRow());
//			paramMap.put("pagePerCount", pageClass.getPagePerCount());
			paramMap.put("city", city);
			paramMap.put("gu", gu);
			paramMap.put("intersection", intersection);
//			pageClass.setTablelist(this.systemManagementMapper.getNodeList(paramMap));
//			return pageClass;
			return this.systemManagementMapper.getNodeList(paramMap);
//		}
//		catch (NumberFormatException e) {
//			log.error("Page Is Not Integer - page : {}", page);
//			return null;
//		}
	}
}
