package sgc.portal.ei.ei0102.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import sgc.portal.base.util.EasyHashMap;
import sgc.portal.ei.ei0102.dto.EI01020001USearch;
import sgc.portal.ei.ei0102.model.EI01020001U;

import sgc.portal.ei.ei0102.service.EI01020001UService;

import lombok.extern.slf4j.Slf4j;


@Slf4j
@RestController
@RequestMapping("/ei/ei0102")
public class EI01020001UController {
    private final EI01020001UService service;

    public EI01020001UController(EI01020001UService service) {
        this.service = service;
    }
    
    /*
     * 조회 LIST
     */
    @PostMapping("/EI01020001U/list")
    public List<EasyHashMap<String, Object>> getList(@RequestBody EI01020001USearch search) {
        return service.getList(search);
    }
    
    @PostMapping("/EI01020001U/list2")
    public List<EasyHashMap<String, Object>> getList2(@RequestBody EI01020001USearch search) {
    	// EIS_MI_BACKLOG (경영정보 - Backlog) 데이터 유무 확인 
    	log.info("=== EIS_MI_BACKLOG (경영정보 - Backlog) 데이터 유무 확인 ===");
    	
    	int backlogCnt  = service.getBacklogCnt(search);
    	log.info("backlogCnt : " + backlogCnt);
    	
    	if(backlogCnt < 1) {
    		// 사업본부 구분코드 조회
    		log.info("=== 사업본부 구분코드 조회 ===");
    		List<EasyHashMap<String, Object>> list = service.getSpecCd(search);  // 사업본부 구분코드
    		
    		List<EI01020001U> eI01020001U = new ArrayList<>();
    		
    		for(int i = 0; i < list.size(); i++) {
    			EI01020001U ei = new EI01020001U();
    			ei.setCmpnCd(search.getCmpnCd());
    			ei.setPyear(search.getPyear());
    			ei.setPmonth(search.getPmonth());
    			ei.setCcgrpGbCd(list.get(i).get("specCd").toString());
    			ei.setBacklogFcst("0");
    			
    			eI01020001U.add(ei);
    		}
    		
    		service.update4(eI01020001U);
    	}
    	
        return service.getList2(search);
    }
    
    @PostMapping("/EI01020001U/list3")
    public List<EasyHashMap<String, Object>> getList3(@RequestBody EI01020001USearch search) {
        return service.getList3(search);
    }
    
    /*
     * SAVE LIST
     */
    @PostMapping("/EI01020001U/save1")
    public Map<String, String> save1(@RequestBody List<EI01020001U> eI01020001U) {
    	return service.update1(eI01020001U);
    }
    
    @PostMapping("/EI01020001U/save2")
    public Map<String, String> save2(@RequestBody List<EI01020001U> eI01020001U) {
    	return service.update2(eI01020001U);
    }
    
    @PostMapping("/EI01020001U/save3")
    public Map<String, String> save3(@RequestBody List<EI01020001U> eI01020001U) {
    	log.info("EI01020001UController - update3()");
    	
    	return service.update3(eI01020001U);
    }
       
}
