package sgc.portal.cw.cw0804.service;

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

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import sgc.portal.base.util.EasyHashMap;
import sgc.portal.cw.cw0804.dto.CW08040007PSearch;
import sgc.portal.cw.cw0804.model.CW08040007P;
import sgc.portal.cw.cw0804.persistence.CW08040007PMapper;

@Service
public class CW08040007PService {
    private final CW08040007PMapper mapper;

    public CW08040007PService(CW08040007PMapper mapper) {
        this.mapper = mapper;
    }
    
    public EasyHashMap<String, Object> getSelectRemInfo(CW08040007PSearch search) {
        return mapper.getSelectRemInfo(search);
    }

	@Transactional
    public Map<String, String> save(CW08040007P cW08040007P) {
    	
    	Map<String,String> result = new HashMap<>();
    	
    	updateRes(cW08040007P);	//예약테이블 종료 업데이트
		insertHis(cW08040007P);	//히스토리테이블 
		
		//일예약 시간변경이 있는경우 
		if(!cW08040007P.getGubun().equals("30")) {
			cW08040007P.setCheck(disColCount(cW08040007P));	//디스플레이 컬럼수
			updateDis1(cW08040007P);						//디스플레이테이블
		}
		
		//기간 예약  날짜변경이 있는경우
		if(cW08040007P.getGubun().equals("30")) {
			updateDis2(cW08040007P);		//디스플레이테이블
		}
    	
        return result;
    }
    
    private void updateRes(CW08040007P cW08040007P) {
        mapper.updateRes(cW08040007P);
    }
    
    private void insertHis(CW08040007P cW08040007P) {
        mapper.insertHis(cW08040007P);
    }
    
    private void updateDis1(CW08040007P cW08040007P) {
    	
    	int cnt			= cW08040007P.getCheck();
    	String col 		= "";
    	String colname 	= "";
    	
    	colname = "T" + cW08040007P.getStartHhmm().substring(0, 3);
    	col 	= colname + " = 'N'";
    	
    	for( int i = 1; i < cnt; i++ ){
        	//컬럼명 변경
        	if(colname.length()<5){
          		colname = "," + colname;
          	}
          	if(colname.substring(4).equals("0")){
          		colname = colname.substring(0, 4) + "3";
          	}else{
          		colname = String.valueOf(Integer.parseInt(colname.substring(2, 4))+1)+"0";
          		if( colname.length() < 3 ){
          			colname = ",T0" + colname;
          		}else{
          			colname = ",T" + colname;
          		}
          	}
        	col = col + colname + " = 'N'";
    	}
    	cW08040007P.setColname(col);
    	
        mapper.updateDis1(cW08040007P);
    }
    
    private void updateDis2(CW08040007P cW08040007P) {
        mapper.updateDis2(cW08040007P);
    }
    
    private int disColCount(CW08040007P cW08040007P) {
    	
    	int 	cnt		= 0;
    	String 	start_t = "";
        String 	end_t	= "";
        String 	cnt_t	= "";
        
        int i_start_t	= 0;
        int i_end_t		= 0;
        int h			= 0;
        int m			= 0;
        
        start_t = cW08040007P.getStartHhmm().replace(":", "");
    	end_t 	= cW08040007P.getEndHhmm().replace(":", "");
    	
    	i_start_t	= Integer.parseInt(start_t);
    	i_end_t		= Integer.parseInt(end_t);
    	
    	cnt		= i_end_t - i_start_t;
    	cnt_t	= String.valueOf(cnt);
    	
    	if(cnt_t.length()>3){
    		h = Integer.parseInt(cnt_t.substring(0, 2));
    		m = Integer.parseInt(cnt_t.substring(2));
    	}else if(cnt_t.length() > 2){
    		h = Integer.parseInt(cnt_t.substring(0, 1));
    		m = Integer.parseInt(cnt_t.substring(1));
    	}else if(cnt_t.length() > 1){
    		m = Integer.parseInt(cnt_t);
    	}
    	
    	if(m == 0){
    		cnt = h * 2;
    	}else{
    		cnt = (h * 2) + 1;
    	}
    	
    	return cnt;
    }
    
    @Transactional
    public Map<String, String> cancel(List<CW08040007P> CW08040007P) {
    	
    	Map<String,String> result = new HashMap<>();
    	
        for (CW08040007P cW08040007P : CW08040007P) {
        	updateRes(cW08040007P);	//예약테이블 종료 업데이트
    		insertHis(cW08040007P);	//히스토리테이블 
    		
    		//일예약 시간변경이 있는경우 
    		if(!cW08040007P.getGubun().equals("30")) {
    			cW08040007P.setCheck(disColCount(cW08040007P));	//디스플레이 컬럼수
    			updateDis1(cW08040007P);						//디스플레이테이블
    		}
    		
    		//기간 예약  날짜변경이 있는경우
    		if(cW08040007P.getGubun().equals("30")) {
    			updateDis2(cW08040007P);		//디스플레이테이블
    		}
        }
        return result;
    }
}
