package com.eactive.eai.rms.common.board;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.eactive.eai.common.util.DatetimeUtil;
import com.eactive.eai.rms.common.base.BaseService;

@Service("boardService")
public class BoardService extends BaseService {
	
	@Autowired
	@Qualifier("boardDao")
	private BoardDao dao;
	
	public HashMap selectList(int startNum,int endNum, HashMap<String, Object> paramMap,String table) throws Exception {
		// 전체 목록수 얻기
		paramMap.put("table", table);
		int totalCount = dao.selectListCount(paramMap);

		paramMap.put("startNum" , startNum);// 시작 record
		paramMap.put("endNum"   , endNum);// 끝 record

		List list = dao.selectList(paramMap);
		
		HashMap map = new HashMap();
		map.put("totalCount", totalCount);
		map.put("list", list);

		return map;
	}	
	
	public Map selectDetail(int seqNo, String userId,String table) throws Exception {
		
		HashMap paramMap = new HashMap();

		paramMap.put("table", table);
		paramMap.put("seqNo", seqNo);
		paramMap.put("table", table);
		HashMap detailMap = dao.selectDetail( paramMap);
		
		if(!detailMap.get("AUTHOR").equals(userId)){
			paramMap.put("rDate", DatetimeUtil.getCurrentDateTime());
			dao.updateReadCount( paramMap);			
		}
		
		return detailMap;
	}
	
	public void insert(HashMap<String, Object> paramMap,String table) throws Exception {
		paramMap.put("table", table);
		String nextLvlNo = "001";
		int sortSeq = 0;
		int lvl = 1;
		
		String lvlNo = "001";
		
		if(paramMap.get("lvlNo") != null) {
			lvlNo = (String)paramMap.get("lvlNo"); // 001
		}
		
		try {
			sortSeq = Integer.parseInt((String)paramMap.get("sortSeq"));
		}
		catch(Exception e) {
			sortSeq = 0;
		}
		
		String seqNoStr  = dao.selectSeqNo(paramMap);
		int seqNo = 0;
		try {
			seqNo = Integer.parseInt(seqNoStr) + 1;
		}
		catch(Exception e) {
			seqNo = 1;
		}
		
		if(sortSeq == 0) {
			nextLvlNo = "001";
			sortSeq = seqNo;
			lvl = 1;
		}
		else {
			HashMap levelMap  = dao.selectNextLevelNo(paramMap);
			if(levelMap == null) {
				nextLvlNo = lvlNo + "-001";  
			}
			else {
				nextLvlNo = (String)levelMap.get("LVLNO");
				if(nextLvlNo == null) {
					nextLvlNo = lvlNo + "-001";
				}
			}
			
			try {
				lvl = Integer.parseInt((String)paramMap.get("lvl"));
			}
			catch(Exception e) {
				lvl = 1;
			}
			
			lvl = lvl + 1;
		}
		
		paramMap.put("seqNo", seqNo);
		
		paramMap.put("regDate", DatetimeUtil.getCurrentDate());
		paramMap.put("readCnt", 0);
		paramMap.put("lvlNo", nextLvlNo);
		paramMap.put("sortSeq", sortSeq);
		paramMap.put("lvl", lvl);
		paramMap.put("answer", "0");
		dao.insert(paramMap);
		
		// reply Set
		if(lvl > 1) {
			HashMap map = new HashMap();
			map.put("seqNo", sortSeq);
			map.put("answer", "1");
			map.put("table", table);
			dao.updateAnswer(map);
		}
	}
	
	public void update(HashMap<String, Object> paramMap,String table) throws Exception {
		paramMap.put("table", table);
		dao.update(paramMap);
	}

	public void delete(HashMap<String, Object> paramMap,String table) throws Exception {
		paramMap.put("table", table);
		
		List<Map<String, Object>> list = dao.selectFIleList(paramMap);
		logger.warn("Delete File : list size ="+ list.size());
		for(Map<String, Object> row: list) {
			try {
				int seqNo = (int)row.get("SEQNO");
				String location = (String)row.get("LOCATION");
				String filename = (String)row.get("FILENAME");
				
				logger.warn("Delete File Check : orgNo="+seqNo+", filename="+ filename +", location="+location);
				
				// delete file
				if(location != null && location.trim().length() > 0) {
					File f = new File(location);
					if(f.exists() && f.isFile()) {
						f.delete();
						logger.warn("Delete File : orgNo="+seqNo+", filename="+ filename +", location="+location);
					}
				}
			}
			catch(Exception ex) {
				logger.warn("file delete fail", ex);
			}
		}
		
		dao.delete(paramMap);
		
		int lvl = 0;
		try {
			lvl = Integer.parseInt((String)paramMap.get("lvl"));
		}
		catch(Exception e) {
			lvl = 1;
		}
		
		String sortSeq = (String)paramMap.get("sortSeq");
		if(lvl > 1) {
			HashMap map = new HashMap();
			map.put("seqNo", sortSeq);
			map.put("answer", "0");
			map.put("table", table);
			dao.updateAnswer(map);
		}
	}
}
