package com.jasamedika.medifirst2000.controller;

import com.jasamedika.medifirst2000.constants.MessageResource;
import com.jasamedika.medifirst2000.controller.base.LocaleController;
import com.jasamedika.medifirst2000.dao.MapJabatanToUraianTugasDao;
import com.jasamedika.medifirst2000.dao.MapUraianTugasToRincianKegiatanDao;
import com.jasamedika.medifirst2000.dto.EditPegawaiDTO;
import com.jasamedika.medifirst2000.entities.LoginUser;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.security.model.AppPermission;
import com.jasamedika.medifirst2000.security.service.TokenAuthenticationService;
import com.jasamedika.medifirst2000.service.*;
import com.jasamedika.medifirst2000.util.JsonUtil;
import com.jasamedika.medifirst2000.util.PasswordUtil;
import com.jasamedika.medifirst2000.vo.*;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.jasamedika.medifirst2000.constants.Constants.MessageInfo.ERROR_MESSAGE;
import static com.jasamedika.medifirst2000.core.web.WebConstants.HttpHeaderInfo.LABEL_ERROR;
import static com.jasamedika.medifirst2000.core.web.WebConstants.HttpHeaderInfo.LABEL_SUCCESS;
import static com.jasamedika.medifirst2000.util.CommonUtil.isNotNullOrEmpty;
import static com.jasamedika.medifirst2000.util.rest.RestUtil.getJsonHttpStatus;
import static com.jasamedika.medifirst2000.util.rest.RestUtil.getJsonResponse;
import static org.slf4j.LoggerFactory.getLogger;
import static org.springframework.http.HttpStatus.*;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * Controller class for Pasien Business
 * 
 * @author Roberto
 */
@RestController
@RequestMapping("/pegawai")
public class PegawaiController extends LocaleController<PegawaiVO> {

	private static final Logger LOGGER = getLogger(PegawaiController.class);

	@Autowired
	private LoginUserService loginUserService;

	@Autowired
	private TokenAuthenticationService tokenAuthenticationService;

	@Autowired
	private MapJabatanToUraianTugasDao mapUraianToJabatanDao;

	@Autowired
	private MapUraianTugasToRincianKegiatanDao mapUraianTugasToRincianKegiatanDao;

	@Autowired
	private PegawaiService pegawaiService;

	@Autowired
	private KeluargaPegawaiService keluargaPegawaiService;

	@Autowired
	private RiwayatPendidikanService riwayatPendidikanService;

	@Autowired
	private RiwayatJabatanService riwayatJabatanService;

	@Autowired
	private PegawaiJadwalKerjaService pegawaiJadwalKerjaServices;

	@RequestMapping(value = "/get-all-dokter2", method = GET)
	public ResponseEntity<List<PegawaiVO>> getAllDokter2(HttpServletRequest request) {
		try {
			List<PegawaiVO> listPegawaiVo = pegawaiService.getAllDokter();
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(listPegawaiVo, OK, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when pegawaiService.getAllDokter", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when pegawaiService.getAllDokter", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/get-all-pegawai", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Collection<PegawaiVO>> getAllPegawai(
			@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "take", required = false, defaultValue = "20") Integer take,
			@RequestParam(value = "sort", required = false, defaultValue = "id") String sort,
			@RequestParam(value = "dir", required = false, defaultValue = "asc") String dir,
			@RequestParam(value = "id", required = false) Integer id,
			@RequestParam(value = "namaLengkap", required = false) String namaLengkap) {
		Map<String, Object> result = pegawaiService.getAllPegawai(page, take, sort, dir, id, namaLengkap);
		return constructListPageResult(result);
	}

	@RequestMapping(value = "/get-all-pegawai", method = GET)
	public ResponseEntity<List<Map<String, Object>>> getAllPegawai(
			@RequestParam(value = "key", required = false) String key,
			@RequestParam(value = "tanggal", required = false) String tanggal, HttpServletRequest request) {
		try {
			List<Map<String, Object>> listPegawaiVo = pegawaiService.getAll();
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(listPegawaiVo, OK, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when pegawaiService.getAll", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when pegawaiService.getAll", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/get-all-perawat", method = GET)
	public ResponseEntity<List<Map<String, Object>>> getAllPerawat(
			@RequestParam(value = "key", required = false) String key,
			@RequestParam(value = "tanggal", required = false) String tanggal, HttpServletRequest request) {
		try {
			List<Map<String, Object>> listPegawaiVo = pegawaiService
					.getAllByJenisPegawai(GetSettingDataFixed("KdJenisPegawaiParamedis"), key);
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(listPegawaiVo, OK, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when pegawaiService.getAllByJenisPegawai", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when pegawaiService.getAllByJenisPegawai", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/save-riwayat-pendidikan", method = POST, produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE)
	public ResponseEntity<String> saveRiwayatPendidikan(@Valid @RequestBody RiwayatPendidikanVO vo,
			HttpServletRequest request) {
		try {
			RiwayatPendidikanVO result = riwayatPendidikanService.add(vo);
			if (null != result)
				mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			getJsonHttpStatus(CREATED);
			return getJsonResponse("Status Sukses", CREATED, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when riwayatPendidikanService.add", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when riwayatPendidikanService.add", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/save-riwayat-jabatan", method = POST, produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE)
	public ResponseEntity<String> saveRiwayatJabatan(@Valid @RequestBody RiwayatJabatanVO vo,
			HttpServletRequest request) {
		try {
			RiwayatJabatanVO result = riwayatJabatanService.add(vo);
			if (null != result)
				mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			getJsonHttpStatus(CREATED);
			return getJsonResponse("Status Sukses", CREATED, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when riwayatJabatanService.add", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when riwayatJabatanService.add", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/save-pegawai", method = POST, produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE)
	public ResponseEntity<String> addVO(@Valid @RequestBody PegawaiVO vo) {
		try {
			PegawaiVO result = pegawaiService.add(vo);
			if (null != result)
				return getJsonResponse("", CREATED);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when pegawaiService.add", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when pegawaiService.add", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
		return getJsonHttpStatus(NOT_ACCEPTABLE);
	}

	@RequestMapping(value = "/update-pegawai", method = POST, produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE)
	@AppPermission(AppPermission.SPECIALS)
	public ResponseEntity<String> editVOWithPassword(@Valid @RequestBody EditPegawaiDTO vo,
			HttpServletRequest request) {
		try {
			Authentication authentication = tokenAuthenticationService.getAuthentication(request);
			String namaUser = authentication.getName();
			List<LoginUser> loginUser = loginUserService.findByNamaUser(namaUser);
			if (!loginUser.isEmpty()) {
				LoginUser user = loginUser.get(0);
				PasswordUtil passwordUtil = new PasswordUtil();
				boolean isValidPassword = passwordUtil.isPasswordEqual(vo.getPassword(), user.getKataSandi());
				if (isValidPassword) {
					PegawaiVO result = vo.getPegawaiVO();
					return editVO(result);
				} else {
					LOGGER.error("Got exception when isValidPassword is false");
					addHeaderMessage(ERROR_MESSAGE, "Invalid Password");
					return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
				}
			} else {
				LOGGER.error("Got exception when loginUser.isEmpty");
				addHeaderMessage(ERROR_MESSAGE, "User is unauthorized");
				return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
			}
		} catch (Exception e) {
			LOGGER.error("Got Exception {} when getAuthentication", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		}
	}

	public ResponseEntity<String> editVO(@Valid @RequestBody PegawaiVO vo) {
		try {
			PegawaiVO result = pegawaiService.update(vo);
			if (null != result) {
				mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS));
				return getJsonResponse("", OK, mapHeaderMessage);
			}
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when pegawaiService.update", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when pegawaiService.update", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
		return getJsonHttpStatus(NOT_ACCEPTABLE);
	}

	@RequestMapping(value = "/get-pegawai-by-nik/{nik}", method = GET)
	public ResponseEntity<List<PegawaiVO>> getAllVOWithQueryString(@PathVariable("nik") String nik,
			HttpServletRequest request) {
		try {
			List<PegawaiVO> listPegawaiVO = pegawaiService.findByNik(nik);
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(listPegawaiVO, OK, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when findByNik", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when findByNik", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/get-pegawai-by-id/{id}/", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getKelasByRuangan(@PathVariable("id") Integer id,
			HttpServletRequest request) {
		try {
			Map<String, Object> result = pegawaiService.findPegawaiById(id);
			boolean dataFound = (boolean) result.get("dataFound");
			if (dataFound) {
				mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			} else {
				mapHeaderMessage.put(LABEL_ERROR, getMessage(MessageResource.LABEL_ERROR, request));
			}
			return getJsonResponse(result, OK);
		} catch (Exception e) {
			throw new ServiceVOException(e.getMessage());
		}
	}

	@RequestMapping(value = "/get-pegawai-by-customs/{id}", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<RekamDataPegawaiVO> getPegawaiById(@PathVariable("id") Integer id) {
		RekamDataPegawaiVO rekamDataPegawaiVO = pegawaiService.findRekamDataPegawaiById(id);
		return getJsonResponse(rekamDataPegawaiVO, OK);
	}

	@RequestMapping(value = "/get-pegawai-by-ruangan/{unitKerja}/{tahun}/{bulan}", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getPegawaiByRuangan(@PathVariable("unitKerja") Integer unitKerja,
			@PathVariable("tahun") Integer tahun, @PathVariable("bulan") Integer bulan, HttpServletRequest request) {
		try {
			Map<String, Object> result = pegawaiService.findPegawaiByRuangan(unitKerja, tahun, bulan);
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(result, OK);
		} catch (Exception e) {
			throw new ServiceVOException(e.getMessage());
		}
	}

	@RequestMapping(value = "/get-pegawai-by-jadwal/{tahun}/{bulan}/{pegawai}", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getPegawaiByJadwal(@PathVariable("tahun") Integer tahun,
			@PathVariable("bulan") Integer bulan, @PathVariable("pegawai") Integer pegawai,
			HttpServletRequest request) {
		try {
			Map<String, Object> result = pegawaiService.findPegawaiByRuangan(0, tahun, bulan, pegawai);
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(result, OK);
		} catch (Exception e) {
			throw new ServiceVOException(e.getMessage());
		}
	}

	@RequestMapping(value = "/get-uraian/{jabatan}/{pegawai}", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getUraianPegawai(@PathVariable("jabatan") Integer jabatan,
			@PathVariable("pegawai") Integer pegawai, HttpServletRequest request) {
		try {
			Map<String, Object> result = new HashMap<>();
			result.put("items", JsonUtil.ToMaps(mapUraianTugasToRincianKegiatanDao
					.findByMapJabatanToUraianTugasId(mapUraianToJabatanDao.findByPegawaiId(pegawai).get(0).getId())));
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(result, OK);
		} catch (Exception e) {
			throw new ServiceVOException(e.getMessage());
		}
	}

	@RequestMapping(value = "/get-pegawai/", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getDataPegawai(HttpServletRequest request) {
		try {
			Map<String, Object> result = pegawaiService.dataAllPegawai();
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(result, OK);
		} catch (Exception e) {
			throw new ServiceVOException(e.getMessage());
		}
	}

	@RequestMapping(value = "/get-pegawai-by-pelaksana", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getPegawaiByPelaksana(@RequestParam(value = "id") Integer id,
			HttpServletRequest request) {
		try {
			Map<String, Object> result = pegawaiService.getPegawaiByPelaksana(id);
			if (null != result) {
				mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
				return getJsonResponse(result, OK, mapHeaderMessage);
			} else {
				return getJsonResponse(null, NOT_FOUND, mapHeaderMessage);
			}
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when getPegawaiByPelaksana", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when getPegawaiByPelaksana", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/get-all-pegawai-no-paging/", method = GET)
	public ResponseEntity<Map<String, Object>> findAllPegwawaiNoPaging(
			@RequestParam(value = "key", required = false) String key, HttpServletRequest request) {
		try {
			Map<String, Object> listPegawaiVo = pegawaiService.findAllPegawaiNoPaging();
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(listPegawaiVo, OK, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when findAllPegawaiNoPaging", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when findAllPegawaiNoPaging", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/get-all-pegawai-no-paging-search", method = GET)
	public ResponseEntity<Map<String, Object>> findAllPegawaiNoPagingSearch(
			@RequestParam(value = "namaPegawai") String namaPegawai,
			@RequestParam(value = "idJabatan") String idJabatan,
			@RequestParam(value = "idSubUnitKerja") Integer idSubUnitKerja, HttpServletRequest request) {
		try {
			Map<String, Object> listPegawaiVo = pegawaiService.findAllPegawaiNoPagingSearch(namaPegawai, idJabatan,
					idSubUnitKerja);
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(listPegawaiVo, OK, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when findAllPegawaiNoPagingSearch", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when findAllPegawaiNoPagingSearch", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/get-keluarga-pegawai", method = GET)
	public ResponseEntity<Map<String, Object>> getDataKeluargaPegawai(@RequestParam(value = "id") Integer id,
			HttpServletRequest request) {
		try {
			Map<String, Object> listPegawaiVo = keluargaPegawaiService.findAllDataKeluargaPegawai(id);
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(listPegawaiVo, OK, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when findAllDataKeluargaPegawai", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when findAllDataKeluargaPegawai", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/find-pegawai-by-id-custom/{idPegawai}", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> findpegawaiByIdCustom(@PathVariable("idPegawai") Integer idPegawai,
			HttpServletRequest request) {
		try {
			Map<String, Object> result = pegawaiService.findPegawaiByIdCustom(idPegawai);
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(result, OK);
		} catch (Exception e) {
			throw new ServiceVOException(e.getMessage());
		}
	}

	@RequestMapping(value = "/get-pegawai-by-ruangan/{ruangan}", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getPegawaiByRuangan(@PathVariable("ruangan") Integer ruangan,
			HttpServletRequest request) {
		try {
			Map<String, Object> result = pegawaiService.findPegawaiByRuangan(ruangan);
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(result, OK);
		} catch (Exception e) {
			throw new ServiceVOException(e.getMessage());
		}
	}

	@RequestMapping(value = "/save-all-jadwal-pegawai-rev2/", method = POST)
	public ResponseEntity<Map<String, Object>> addAllVOPegawai(@RequestBody List<PegawaiJadwalKerjaVO> vos,
			HttpServletRequest request) {
		try {
			Map<String, Object> result = pegawaiJadwalKerjaServices.saveListJadwalPegawai(vos, false, false);
			if (null != result)
				mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(result, CREATED, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when saveListJadwalPegawai", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when saveListJadwalPegawai", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/get-pegawai-by-ruangan-rev2/{subUnitKerja}/{tahun}/{bulan}", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getPegawaiByRuanganrev2(
			@PathVariable("subUnitKerja") Integer subUnitKerja, @PathVariable("tahun") Integer tahun,
			@PathVariable("bulan") Integer bulan, HttpServletRequest request) {
		try {
			Map<String, Object> result = pegawaiService.findPegawaiByRuanganRev2(subUnitKerja, tahun, bulan);
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(result, OK);
		} catch (Exception e) {
			throw new ServiceVOException(e.getMessage());
		}
	}

	@RequestMapping(value = "/get-all-pegawai-aktif-by-ruangan/", method = GET)
	public ResponseEntity<List<PegawaiVO>> getAllPegawaiAktifByRuangan(
			@RequestParam(value = "idRuangan", required = false) Integer idRuangan, HttpServletRequest request) {
		try {
			List<PegawaiVO> listPegawaiVo = pegawaiService.findAllPegawaiAktifByRuangan(idRuangan);
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(listPegawaiVo, OK, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when findAllPegawaiAktifByRuangan", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when findAllPegawaiAktifByRuangan", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/get-tgl-pensiun/{tglLahir}/{idjabatan}", method = GET)
	public ResponseEntity<Map<String, Object>> getPegawaiByIdForMobile(@PathVariable("idjabatan") Integer idjabatan,
			@PathVariable("tglLahir") String tglLahir, HttpServletRequest request) {
		try {
			Map<String, Object> tglpensiun = pegawaiService.getTglpensiun(tglLahir, idjabatan);
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(tglpensiun, OK, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when getTglpensiun", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when getTglpensiun", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/non-aktif-pegawai-by-id/{id}/", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> deletePegawai(@PathVariable("id") Integer id,
			HttpServletRequest request) {
		try {
			Map<String, Object> result = pegawaiService.nonAktifPegawai(id);
			if (isNotNullOrEmpty(result)) {
				mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			} else {
				mapHeaderMessage.put(LABEL_ERROR, getMessage(MessageResource.LABEL_ERROR, request));
			}
			return getJsonResponse(result, OK);
		} catch (Exception e) {
			throw new ServiceVOException(e.getMessage());
		}
	}

	@RequestMapping(value = "/get-pegawai-pensiun/{startPeriode}/{endPeriode}", method = GET)
	public ResponseEntity<List<Map<String, Object>>> getPegawaiPensiun(
			@PathVariable("startPeriode") String startPeriode, @PathVariable("endPeriode") String endPeriode,
			HttpServletRequest request) {
		try {
			List<Map<String, Object>> tglpensiun = pegawaiService.getPegawaiYgAkanPensiun(startPeriode, endPeriode);
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(tglpensiun, OK, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when getPegawaiYgAkanPensiun", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when getPegawaiYgAkanPensiun", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/get-pegawai-str-expired", method = GET)
	public ResponseEntity<List<Map<String, Object>>> getPegawaiStrExpired(HttpServletRequest request) {
		try {
			List<Map<String, Object>> pegawais = pegawaiService.getpegawaiStrExpired();
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(pegawais, OK, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when getpegawaiStrExpired", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when getpegawaiStrExpired", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/get-pegawai-sip-expired", method = GET)
	public ResponseEntity<List<Map<String, Object>>> getPegawaiSipExpired(HttpServletRequest request) {
		try {
			List<Map<String, Object>> pegawais = pegawaiService.getpegawaiSipExpired();
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(pegawais, OK, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when getpegawaiSipExpired", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when getpegawaiSipExpired", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/get-count-pegawai-by-unit-kerja", method = GET)
	public ResponseEntity<List<Map<String, Object>>> getCountPegawaiByUnitKerja(HttpServletRequest request) {
		try {
			List<Map<String, Object>> pegawais = pegawaiService.getpegawaiByUnitKerja();
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(pegawais, OK, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when getpegawaiByUnitKerja", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when getpegawaiByUnitKerja", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/get-count-pegawai-by-kelompok-jabatan", method = GET)
	public ResponseEntity<List<Map<String, Object>>> getCountPegawaiByKelompokJabatan(HttpServletRequest request) {
		try {
			List<Map<String, Object>> pegawais = pegawaiService.getpegawaiByKelompokJabatan();
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(pegawais, OK, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when getpegawaiByKelompokJabatan", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when getpegawaiByKelompokJabatan", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/get-count-pegawai-by-status-pegawai", method = GET)
	public ResponseEntity<List<Map<String, Object>>> getCountPegawaiByStatusPegawai(HttpServletRequest request) {
		try {
			List<Map<String, Object>> pegawais = pegawaiService.getpegawaiByStatusPegawai();
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(pegawais, OK, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when getpegawaiByStatusPegawai", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when getpegawaiByStatusPegawai", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/get-count-pegawai-by-jk", method = GET)
	public ResponseEntity<List<Map<String, Object>>> getCountPegawaiByJK(HttpServletRequest request) {
		try {
			List<Map<String, Object>> pegawais = pegawaiService.getpegawaiByJenisKelamin();
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(pegawais, OK, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when getpegawaiByJenisKelamin", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when getpegawaiByJenisKelamin", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/get-pegawai-meninggal-pindah-pensiun-no-paging/", method = GET)
	public ResponseEntity<Map<String, Object>> findPegawaiMeninggalPindahPensiunNoPaging(
			@RequestParam(value = "key", required = false) String key, HttpServletRequest request) {
		try {
			Map<String, Object> listPegawaiVo = pegawaiService.findPegawaiMeninggalPindahPensiunNoPaging();
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(listPegawaiVo, OK, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when findPegawaiMeninggalPindahPensiunNoPaging", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when findPegawaiMeninggalPindahPensiunNoPaging", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/get-pegawai-outsourcing/", method = GET)
	public ResponseEntity<Map<String, Object>> findPegawaiOutSourcingNoPaging(
			@RequestParam(value = "key", required = false) String key, HttpServletRequest request) {
		try {
			Map<String, Object> listPegawaiVo = pegawaiService.findPegawaiOutSourcingNoPaging();
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(listPegawaiVo, OK, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when findPegawaiOutSourcingNoPaging", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when findPegawaiOutSourcingNoPaging", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/get-all-pegawai-custom/", method = GET)
	public ResponseEntity<List<Map<String, Object>>> findAllPegawaiCustom(
			@RequestParam(value = "key", required = false) String key, HttpServletRequest request) {
		try {
			List<Map<String, Object>> listPegawaiVo = pegawaiService.getAllPegawaiCustom();
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(listPegawaiVo, OK, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when getAllPegawaiCustom", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when getAllPegawaiCustom", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/get-all-pegawai-kepala-ruangan", method = GET)
	public ResponseEntity<List<Map<String, Object>>> findAllPegawaiKepalaRuangan(HttpServletRequest request) {
		try {
			List<Map<String, Object>> listPegawai = pegawaiService.getAllKepalaRuangan();
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(listPegawai, OK, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when getAllKepalaRuangan", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when getAllKepalaRuangan", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

}
