/**
 * Project Name : 전사 법무/계약관리시스템 개선
 * File Name    : LegacyGSEMSJob.java
 * Description  :
 * Author       : 강미선(miseon0.kang)
 * Date         : 2023.01.13
 * Copyright 	: 2023 by SAMSUNG. All rights reserved.
 */
package com.samsung.embo.cronJob.control.service.impl;

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



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


import com.samsung.core.annotation.BatchJob;
import com.samsung.embo.api.service.GhrpService;
import com.samsung.embo.common.dept.dao.DeptDao;
import com.samsung.embo.common.user.dao.UserCfDao;
import com.samsung.embo.cronJob.control.service.GHRPJobService;

import lombok.extern.log4j.Log4j2;

@Service
@Log4j2
public class GHRPJobServiceImpl  implements GHRPJobService {

	@Autowired
	private DeptDao deptDao;

	@Autowired
	private UserCfDao userCfDao;

	@Autowired
	private GhrpService ghrpService;

	@Override
	@BatchJob("com.samsung.embo.cronJob.control.service.impl.recvDeptInfo")
	public  void recvDeptInfo() throws Exception{
		log.info("=============== SMDM 부서정보 IF 진입 =============== :");
		System.out.println("=============== SMDM 부서정보 IF 진입 =============== :");

		log.info("=============== Start . 부서정보 Receive  ===============");
		System.out.println("=============== Start . 부서정보 Receive  ===============");

		try {
			// Rest 수신 받는 부분 추가
			ArrayList<Map<String, Object>> result = ghrpService.ghrpGvXstgOrgclmPasgvCdv();
			
			log.info("부서정보 Counts : [{}]", result.size());
			System.out.println("부서정보 Counts : [" + result.size() + "]");

			if(result.size() > 0) {
				deptDao.deleteTnInfHrDept();

				deptDao.insertTnInfHrDept(result);//20240514 GHRP → SMDM 변경

				deptDao.deleteComDept();
				int iValue = deptDao.insertComDept();

				log.info("부서정보 Bulk Insert Count [{}]", iValue);
				System.out.println("부서정보 Bulk Insert Count ["+iValue+"]");

				//삼성전자_기타 추가등록
				deptDao.insertEtcDept();
			}
		} catch (Exception e) {
			log.error(e.getMessage());
			System.out.println(e.getMessage());
		}

		log.info("=============== End  . 부서정보 Receive  ===============");
		System.out.println("=============== End  . 부서정보 Receive  ===============");
	}

	@Override
	@BatchJob("com.samsung.embo.cronJob.control.service.impl.recvUserInfo")
	public  void recvUserInfo() throws Exception{
		log.info("=============== SMDM 임직원정보 IF 진입 =============== :");

		log.info("=============== SMDM . 임직원정보 Receive  ===============");

		
		try {
			// Rest 수신 받는 부분 추가
			ArrayList<Map<String, Object>> result = ghrpService.ghrpGvXstgEmpclmPasgvCdv();
			
			log.info("임직원정보 Counts : [{}]", result.size());
			if(result.size() > 0) {

				userCfDao.deleteTnInfHrEmpMas();
				int iValue = userCfDao.insertTnInfHrEmpMas(result);//이것만 수정
				log.info("임직원정보 Bulk Insert Count [{}]", iValue);

			
				log.info("임직원 정보 테이블 Merge");
				//임직원 insert
				userCfDao.insertTnCfUser();
				

				log.info("사용자 기본 Role Insert");
				userCfDao.insertTnCfUserRole();

				// P1SEC-6900 [3선]인사에서 받아오는 부서정보 관련 데이터 수정
				log.info("사용자 부서명 update");
				userCfDao.updateTnCfUserDeptName();
				
				//20240731 퇴직자들 update 로직 추가
				//userCfDao.updateTnCfUserExpireUser();
				
			}

		} catch (Exception e) {
			log.error(e.getMessage());
		}

		log.info("=============== End  . 임직원정보 Receive  ===============");
	}

}