package sgc.portal.cw.cw0804.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sgc.portal.base.util.EasyHashMap;
import sgc.portal.base.util.SessionUtil;
import sgc.portal.base.websquare.GridRowStatus;
import sgc.portal.com.sym.user.model.UserDetails;
import sgc.portal.cw.cw0804.dto.CW08040001USearch;
import sgc.portal.cw.cw0804.model.CW08040001U;
import sgc.portal.cw.cw0804.persistence.CW08040001UMapper;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CW08040001UService {
    private final CW08040001UMapper mapper;

    public CW08040001UService(CW08040001UMapper mapper) {
        this.mapper = mapper;
    }
    
    public List<EasyHashMap<String, Object>> getRomInfo(CW08040001USearch search) {
        return mapper.getRomInfo(search);
    }

    public List<EasyHashMap<String, Object>> getListDay(CW08040001USearch search) {
        return mapper.getListDay(search);
    }
    
    public List<EasyHashMap<String, Object>> getListRom(CW08040001USearch search) {
        return mapper.getListRom(search);
    }
    
    public List<EasyHashMap<String, Object>> getListRem(CW08040001USearch search) {
        return setRemAuth(mapper.getListRem(search));
    }

    public EasyHashMap<String, Object> getViewRem(CW08040001USearch search) {
    	EasyHashMap<String, Object> view = mapper.getViewRem(search);
    	if (view != null) {
    		List<EasyHashMap<String, Object>> list = new ArrayList<EasyHashMap<String, Object>>();
    		list.add(view);
    		setRemAuth(list);
    	}
        return view;
    }

    private List<EasyHashMap<String, Object>> setRemAuth(List<EasyHashMap<String, Object>> list) {
    	UserDetails userDtail = SessionUtil.getUserDetails();
    	for(EasyHashMap<String, Object> item : list) {
    		String subscSabun = (String) item.get("subscSabun");
    		item.put("is_mine", subscSabun != null && !"".equals(subscSabun) && subscSabun.equals(userDtail.getEmpNo()) );
    	}
    	return list;
    }
    
    public List<EasyHashMap<String, Object>> getSelectRom(CW08040001USearch search) {
        return mapper.getSelectRom(search);
    }
    
    public List<EasyHashMap<String, Object>> getSelectLoc(CW08040001USearch search) {
        return mapper.getSelectLoc(search);
    }

    @Transactional
    public Map<String, String> save(List<CW08040001U> CW08040001U) {
    	
    	Map<String,String> result = new HashMap<>();
    	
        for (CW08040001U cW08040001U : CW08040001U) {
        	
            switch (cW08040001U.getRowStatus()) {
                case GridRowStatus.CREATED:
                	insert(cW08040001U);
                    break;
                case GridRowStatus.UPDATED:
                	update(cW08040001U);
                    break;
                case GridRowStatus.DELETED:
                    delete(cW08040001U);
                    break;
                    
                default:
                    break;
            }
        }
        return result;
    }

    private void insert(CW08040001U cW08040001U) {
        mapper.insert(cW08040001U);
    }

    private void update(CW08040001U cW08040001U) {
        mapper.update(cW08040001U);
    }

    private void delete(CW08040001U cW08040001U) {
        mapper.delete(cW08040001U);
    }
}
