package com.jasamedika.medifirst2000.controller;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jasamedika.medifirst2000.constants.Constants;
import com.jasamedika.medifirst2000.constants.MessageResource;
import com.jasamedika.medifirst2000.controller.base.IBaseRestController;
import com.jasamedika.medifirst2000.controller.base.LocaleController;
import com.jasamedika.medifirst2000.core.web.WebConstants;
import com.jasamedika.medifirst2000.dao.BentukProdukDao;
import com.jasamedika.medifirst2000.dao.MapGenerikToProdukDao;
import com.jasamedika.medifirst2000.dao.StrukPelayananDetailDao;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.ProdukService;
import com.jasamedika.medifirst2000.util.JsonUtil;
import com.jasamedika.medifirst2000.util.rest.RestUtil;
import com.jasamedika.medifirst2000.vo.DetailJenisProdukVO;
import com.jasamedika.medifirst2000.vo.JenisLimbahB3MasukVO;
import com.jasamedika.medifirst2000.vo.JenisProdukVO;
import com.jasamedika.medifirst2000.vo.KelompokProdukVO;
import com.jasamedika.medifirst2000.vo.ProdukDetailLaboratoriumHasilVO;
import com.jasamedika.medifirst2000.vo.ProdukVO;
import com.jasamedika.medifirst2000.vo.SatuanStandarVO;

@RestController
@RequestMapping("/product")
public class ProductController extends LocaleController<ProdukVO> implements
IBaseRestController<ProdukVO>  {
	
	@Autowired
	private ProdukService produkService;
	
	@Autowired
	private BentukProdukDao bentukProdukDao;
	@Autowired
	private StrukPelayananDetailDao strukPelayananDetailDao;
	private static final Logger LOGGER = LoggerFactory
			.getLogger(ProductController.class);

	@SuppressWarnings("deprecation")
	@RequestMapping(value = "/find-tindakan-by-ruangan", method = RequestMethod.GET)
	public ResponseEntity<List<ProdukVO>> findTindakanProduByRuangank(
			@RequestParam(value = "idRuangan", required = false) Integer idRuangan,HttpServletRequest request){
		String data =request.getQueryString();
		data = URLDecoder.decode(data);
		String criteria="";
		String values="";
		String[] arrQueries = data.split("&");
		for(String query : arrQueries)
		{
			if(query.indexOf("filter[filters][0][filter][field]")>=0)
			{
				if(criteria==null)
					criteria=query.replace("filter[filters][0][filter][field]=", "");
				else
					criteria+=query.replace("filter[filters][0][filter][field]=", "");
				
				
			}
			else if(query.indexOf("filter[filters][0][value]")>=0)
			{
				if(values==null)
					values=query.replace("filter[filters][0][value]=", "");
				else
					values+=query.replace("filter[filters][0][value]=", "");
			}
		}
		List<ProdukVO> resultPageMap = produkService.findTindakanByName(values,idRuangan);
		return RestUtil.getJsonResponse(resultPageMap, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/find-same-generic", method = RequestMethod.GET)	
	public ResponseEntity<List<Map<String,Object>>> findSameGeneric(
			@RequestParam(value = "idGeneric", required = false) Integer idGeneric){
		List<Map<String, Object>> resultPageMap = new ArrayList<>();
		try {
			resultPageMap = JsonUtil.ToMaps( produkService.findByGenericId(idGeneric));
		} catch (IllegalArgumentException | IllegalAccessException e) {
			LOGGER.debug(e.getMessage());
		}

		return RestUtil.getJsonResponse(resultPageMap, HttpStatus.OK);
	}
	
	@Autowired
	private MapGenerikToProdukDao mapGenerikToProdukDao;
	@RequestMapping(value = "/find-kekuatan", method = RequestMethod.GET)	
	public ResponseEntity<List<Map<String,Object>>> findKekuatan(
			@RequestParam(value = "idProduk", required = false) Integer idProduk,
			@RequestParam(value = "idGenerik", required = false) Integer idGenerik){
		List<Map<String, Object>> resultPageMap = new ArrayList<>();
		try {		
			resultPageMap = JsonUtil.ToMaps( mapGenerikToProdukDao.findByGenerik(idGenerik));
		} catch (IllegalArgumentException | IllegalAccessException e) {
			LOGGER.debug(e.getMessage());
		}

		return RestUtil.getJsonResponse(resultPageMap, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/find-stok-produk-darah", method = RequestMethod.GET)	
	public ResponseEntity<List<Map<String,Object>>> finStokProdukDarah(
			@RequestParam(value = "idProduk", required = false) Integer idProduk){
		List<Map<String, Object>> resultPageMap = new ArrayList<>();
		try {
		
			resultPageMap = JsonUtil.ToMaps( strukPelayananDetailDao.findByProdukId(idProduk));
		} catch (IllegalArgumentException | IllegalAccessException e) {
			LOGGER.debug(e.getMessage());
		}

		return RestUtil.getJsonResponse(resultPageMap, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/find-bentuk-darah", method = RequestMethod.GET)
	public ResponseEntity<List<Map<String,Object>>> findBentukProdukDarah(
			@RequestParam(value = "namaBentuk", required = false) String namaBentuk){
		List<Map<String, Object>> resultPageMap = new ArrayList<>();
		try {
			if(namaBentuk==null)
				namaBentuk="";
			resultPageMap = JsonUtil.ToMaps( bentukProdukDao.findByDepartemenIdAndNamaBentukProdukContains(Integer.parseInt(GetSettingDataFixed("KdDepartemenBankDarah")),namaBentuk));
		} catch (IllegalArgumentException | IllegalAccessException e) {
			LOGGER.debug(e.getMessage());
		}

		return RestUtil.getJsonResponse(resultPageMap, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/find-detail-jenis-produk", method = RequestMethod.GET)
	
	public ResponseEntity<List<DetailJenisProdukVO>> findDetailJenisProdukByJenisProduk(
			@RequestParam(value = "idJenisProduk", required = false) Integer idJenisProduk){
		List<DetailJenisProdukVO> resultPageMap = produkService.findDetailJenisProdukByJenisProduk(idJenisProduk);
		return RestUtil.getJsonResponse(resultPageMap, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/kelompok-produk-have-stok", method = RequestMethod.GET)
	public ResponseEntity<List<KelompokProdukVO>> kelompokProdukHaveStok(){
		List<KelompokProdukVO> resultPageMap = produkService.findKelompokProdukHaveStok();
		return RestUtil.getJsonResponse(resultPageMap, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/get-hirarki-satuan-produk", method = RequestMethod.GET)
	public ResponseEntity<List<SatuanStandarVO>> getHirarkiSatuanProduk(@RequestParam(value = "id", required = false) String id){
		List<SatuanStandarVO> resultPageMap = produkService.findAllSatuanStandardProduk(id);
		return RestUtil.getJsonResponse(resultPageMap, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/get-satuan-produk", method = RequestMethod.GET)
	public Map<String,Object> getSatuanProduk(@RequestParam(value = "id", required = false) Integer id){
		return produkService.getSatuanProduk(id);
	}
	
	@RequestMapping(value = "/save-deposit", method = RequestMethod.GET)
	public ResponseEntity<Map<String,Object>> saveDeposit(@RequestParam(value = "noRec", required = false) String noRec,
			@RequestParam(value = "deposite", required = false) Double deposite,
			@RequestParam(value = "tanggal", required = false) String tanggal){
		Map<String,Object> resultPageMap = produkService.saveDeposite(noRec,deposite,tanggal);
		return RestUtil.getJsonResponse(resultPageMap, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/update-total-biaya", method = RequestMethod.GET)
	public ResponseEntity<Map<String,Object>> updateTotalBiaya(@RequestParam(value = "noRec", required = false) String noRec, 
			@RequestParam(value = "total", required = false) Double total){
		Map<String,Object> resultPageMap = produkService.saveTotalBiaya(noRec,total);
		return RestUtil.getJsonResponse(resultPageMap, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/find-detail-produk", method = RequestMethod.GET)
	public ResponseEntity<List<ProdukVO>> findDetailProdukByJenisProduk(
			@RequestParam(value = "idJenisProduk", required = false) Integer idJenisProduk){
		List<ProdukVO> resultPageMap = produkService.findDetailProdukByDetailJenisProduk(idJenisProduk);
		return RestUtil.getJsonResponse(resultPageMap, HttpStatus.OK);
	}
	
	@SuppressWarnings("deprecation")
	@RequestMapping(value = "/find-menu-diet", method = RequestMethod.GET)
	public ResponseEntity<List<ProdukVO>> findMenuDiet(HttpServletRequest request){
		String data =request.getQueryString();
		data = URLDecoder.decode(data);
		String[] arrQueries = data.split("&");
		String criteria="";
		String values="";
		for(String query : arrQueries)
		{
			if(query.indexOf("filter[filters][0][field]")>=0)
			{
				if(criteria==null)
					criteria=query.replace("filter[filters][0][field]=", "");
				else
					criteria+=","+query.replace("filter[filters][0][field]=", "");
			}
			else if(query.indexOf("filter[filters][0][value]")>=0)
			{
				if(values==null)
					values=query.replace("filter[filters][0][value]=", "");
				else
					values+=query.replace("filter[filters][0][value]=", "");
			}
		}
		
		//edit by askur 01122016
		//List<ProdukVO> resultPageMap = produkService.findProdukByKelompokProduk(Integer.parseInt(GetSettingDataFixed("KdKelompokProdukGizi")),values);
		//
		//add by askur 01122016
		List<ProdukVO> resultPageMap2 = produkService.findProdukByDetailJenisProduk(Integer.valueOf(GetSettingDataFixed("IdDetailJenisProduk")));
		//
		return RestUtil.getJsonResponse(resultPageMap2, HttpStatus.OK);
	}
	
	@SuppressWarnings("deprecation")
	@RequestMapping(value = "/find-barang-by-kelompok-produk", method = RequestMethod.GET)
	public ResponseEntity<List<ProdukVO>> findBarangByKelompokProduk(@RequestParam(value = "id", required = false) String id,HttpServletRequest request){
		String data =request.getQueryString();
		data = URLDecoder.decode(data);
		String[] arrQueries = data.split("&");
		String criteria="";
		String values="";
		for(String query : arrQueries)
		{
			if(query.indexOf("filter[filters][0][field]")>=0)
			{
				if(criteria==null)
					criteria=query.replace("filter[filters][0][field]=", "");
				else
					criteria+=","+query.replace("filter[filters][0][field]=", "");
			}
			else if(query.indexOf("filter[filters][0][value]")>=0)
			{
				if(values==null)
					values=query.replace("filter[filters][0][value]=", "");
				else
					values+=query.replace("filter[filters][0][value]=", "");
			}
		}
		
		List<ProdukVO> resultPageMap = produkService.findProdukByKelompokProduk(id,values);
		return RestUtil.getJsonResponse(resultPageMap, HttpStatus.OK);
	}
	
	@SuppressWarnings("deprecation")
	@RequestMapping(value = "/find-barang-by-kelompok", method = RequestMethod.GET)
	public Map<String,Object> findProdukByKelompokProduk(@RequestParam(value = "id", required = false) Integer id, HttpServletRequest request) {
		String data = request.getQueryString();
		data = URLDecoder.decode(data);
		String[] arrQueries = data.split("&");
		String criteria = "";
		String values = "";
		for (String query : arrQueries) {
			if (query.indexOf("filter[filters][0][field]") >= 0) {
				if (criteria == null)
					criteria = query.replace("filter[filters][0][field]=", "");
				else
					criteria += "," + query.replace("filter[filters][0][field]=", "");

			} else if (query.indexOf("filter[filters][0][value]") >= 0) {
				if (values == null)
					values = query.replace("filter[filters][0][value]=", "");
				else
					values += query.replace("filter[filters][0][value]=", "");
			}
		}

		Map<String,Object>resultPageMap = produkService.findProdukByKelompok(id, values);
		return resultPageMap;
	}

	@SuppressWarnings("deprecation")
	@RequestMapping(value = "/find-obat", method = RequestMethod.GET)
	public ResponseEntity<List<ProdukVO>> findObatProduk(
			@RequestParam(value = "namaObat", required = false) String namaObat,HttpServletRequest request){
		String data =request.getQueryString();
		data = URLDecoder.decode(data);
		String criteria="";
		String values="";
		String[] arrQueries = data.split("&");
		for(String query : arrQueries)
		{
			if(query.indexOf("filter[filters][0][filter][field]")>=0)
			{
				if(criteria==null)
					criteria=query.replace("filter[filters][0][filter][field]=", "");
				else
					criteria+=query.replace("filter[filters][0][filter][field]=", "");
			}
			else if(query.indexOf("filter[filters][0][value]")>=0)
			{
				if(values==null)
					values=query.replace("filter[filters][0][value]=", "");
				else
					values+=query.replace("filter[filters][0][value]=", "");
			}
		}
		List<ProdukVO> resultPageMap = produkService.findObatByName(values);
		return RestUtil.getJsonResponse(resultPageMap, HttpStatus.OK);
	}

	@RequestMapping(value = "/find-produk", method = RequestMethod.GET)
	public ResponseEntity<List<ProdukVO>> findProdukByJenisProduk(
			@RequestParam(value = "idDetailJenisProduk", required = false) Integer idDetailJenisProduk){
		List<ProdukVO> resultPageMap = produkService.findDetailProdukByDetailJenisProduk(idDetailJenisProduk);

		return RestUtil.getJsonResponse(resultPageMap, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/find-stok-produk", method = RequestMethod.GET)
	public ResponseEntity<Integer> findStokProduk(@RequestParam(value = "idProduk", required = false) Integer idProduk,
			@RequestParam(value = "idRuangan", required = false) Integer idRuangan){
		Integer resultPageMap = produkService.findStokProdukService(idProduk,idRuangan);
		return RestUtil.getJsonResponse(resultPageMap, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/find-stok-produk-konversi", method = RequestMethod.GET)
	public Map<String,Object> findStokProdukKonversi(
			@RequestParam(value = "idProduk", required = false) Integer idProduk,
			@RequestParam(value = "idRuangan", required = false) Integer idRuangan,
			@RequestParam(value = "satuanStandarId", required = false) Integer satuanStandarId){
		Double data=produkService.findStokProdukServiceKonversi(idProduk, idRuangan, satuanStandarId);
		Map<String,Object> result=new HashMap<String,Object>();
		result.put("data", data);
		return result;
	}
	
	@RequestMapping(value = "/find-jenis-produk", method = RequestMethod.GET)
	public ResponseEntity<List<JenisProdukVO>> findJenisProdukByJenisProduk(
			@RequestParam(value = "idKelompokProduk", required = false) Integer idKelompokProduk){
		List<JenisProdukVO> resultPageMap = produkService.findJenisProdukByKelompokProduk(idKelompokProduk);
		return RestUtil.getJsonResponse(resultPageMap, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/save-barangmedis/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> addVOBaramgMedis(@Valid @RequestBody ProdukVO vo, HttpServletRequest request) {
		try {
			Map<String, Object> result = produkService.addBarangMedis(vo);

			if (null != result)
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
						getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getJsonResponse(result, HttpStatus.CREATED, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when add BarangMedis", e.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, e.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got exception {} when add BarangMedis", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}
	}
	
	@RequestMapping(value = "/update-barangmedis/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> updateVOBarangMedis(@Valid @RequestBody ProdukVO vo, HttpServletRequest request) {
		try {
			Map<String, Object> result = produkService.updateBarangMedis(vo);

			if (null != result)
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
						getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getJsonResponse(result, HttpStatus.CREATED, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when add BarangMedis", e.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, e.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got exception {} when add BarangMedis", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}

	}
	
	@RequestMapping(value = "/save-barang-non-medis/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String,Object>> addVOBarangNonMedis(@Valid @RequestBody ProdukVO vo,HttpServletRequest request) {
		
		try{
			Map<String, Object> result = produkService.addBarangNonMedis(vo);
			if (null != result)
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS,request));
				return RestUtil.getJsonResponse(result, HttpStatus.CREATED, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when add BarangNonMedis", e.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE,
					e.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR,
					mapHeaderMessage);
			
		} catch (JpaSystemException jse) {
			LOGGER.error("Got exception {} when add BarangNonMedis", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE,
					jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT,
					mapHeaderMessage);
		}
	}
	
	@RequestMapping(value = "/update-barang-non-medis/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String,Object>> updateVOBarangNonMedis(@Valid @RequestBody ProdukVO vo,HttpServletRequest request) {
		
		try{
			Map<String, Object> result = produkService.updateBarangNonMedis(vo);
			if (null != result)
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS,request));
				return RestUtil.getJsonResponse(result, HttpStatus.CREATED, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when update BarangNonMedis", e.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE,
					e.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR,
					mapHeaderMessage);
			
		} catch (JpaSystemException jse) {
			LOGGER.error("Got exception {} when update BarangNonMedis", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE,
					jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT,
					mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/find-produk-by-nama", method = RequestMethod.GET)
	public ResponseEntity<List<ProdukVO>> findProdukByNamaProduk(
			@RequestParam(value = "idDetailJenisProduk", required = false) Integer idDetailJenisProduk,
			@RequestParam(value = "namaProduk", required = false) String namaProduk){
		List<ProdukVO> resultPageMap = produkService.findDetailProdukByDetailJenisProdukAndNamaProduk(idDetailJenisProduk,namaProduk);

		return RestUtil.getJsonResponse(resultPageMap, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/save-jenis-limbah-b3-masuk/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String,Object>> addVO(@Valid @RequestBody JenisLimbahB3MasukVO vo,HttpServletRequest request) {
		
		try{
			Map<String, Object> result = produkService.addJenisLimbahB3(vo);
			if (null != result)
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS,request));
				return RestUtil.getJsonResponse(result, HttpStatus.CREATED, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when add jenisLimbahB3Masuk", e.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE,
					e.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR,
					mapHeaderMessage);
			
		} catch (JpaSystemException jse) {
			LOGGER.error("Got exception {} when add jenisLimbahB3Masuk", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE,
					jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT,
					mapHeaderMessage);
		}
	}
	
	@RequestMapping(value = "/find-all-jenis-limbah-b3-masuk/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	 public ResponseEntity<Map<String,Object>> getAllVO(HttpServletRequest request) {
	  
		  try {
			   Map<String,Object> result=produkService.findAllJenisLimbah();
			   if (null != result)
			   mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,getMessage(MessageResource.LABEL_SUCCESS,request ));
			   return RestUtil.getJsonResponse(result, HttpStatus.CREATED,mapHeaderMessage);
		  } catch (ServiceVOException e) {
			   LOGGER.error("Got exception {} when get all jenisLimbahB3Masuk", e.getMessage());
			   addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE,
			     e.getMessage());
			   return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR,
			     mapHeaderMessage);
		  } catch (JpaSystemException jse) {
			   LOGGER.error("Got exception {} when get all jenisLimbahB3Masuk", jse.getMessage());
			   addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE,
			     jse.getMessage());
			   return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT,
			     mapHeaderMessage);
		  }
	 }
	
	@RequestMapping(value = "/get-produk-hasil-by-id/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String,Object>> getProdukDetailHasilById(@RequestParam(value = "id") Integer id,HttpServletRequest request) {
		Map<String,Object> result = produkService.findByProdukId(id);
		
		return RestUtil.getJsonResponse(result, HttpStatus.OK,mapHeaderMessage);
	}
	
	@Override
	public ResponseEntity<Collection<ProdukVO>> getAllVOWithQueryString(HttpServletRequest request, Integer page,
			Integer limit, String sort, String dir) {
		return null;
	}

	@RequestMapping(value = "/save-detail-hasil/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String,Object>> saveDetailHasil(@Valid @RequestBody List<ProdukDetailLaboratoriumHasilVO> vos,HttpServletRequest request) {
		try{
			Map<String, Object> result = produkService.addDetailHasil(vos);
			if (null != result)
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS,request));
				return RestUtil.getJsonResponse(result, HttpStatus.CREATED, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when add detailHasil", e.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE,
					e.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR,
					mapHeaderMessage);
			
		} catch (JpaSystemException jse) {
			LOGGER.error("Got exception {} when add detailHasil", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE,
					jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT,
					mapHeaderMessage);
		}
	}

	@Override
	public ResponseEntity<ProdukVO> getVO(Integer id) {
		return null;
	}

	@Override
	public ResponseEntity<String> addVO(ProdukVO vo) {
		return null;
	}

	@Override
	public ResponseEntity<String> editVO(ProdukVO vo) {
		return null;
	}

	@Override
	public ResponseEntity<String> deleteVO(Integer id) {
		return null;
	}

	@Override
	public ResponseEntity<List<ProdukVO>> getAllVO() {
		return null;
	}
	
	@RequestMapping(value = "/find-produk-by-detail-jenis-produk-and-nama-produk", method = RequestMethod.GET)
	public ResponseEntity<Map<String,Object>> findProdukByDetailJenisProdukAndNamaProduk(HttpServletRequest request,@RequestParam(value = "idDetailJenisProduk", required = false) Integer idDetailJenisProduk) {
		try {
			Map<String,Object> result = produkService.findProdukByDetailJenisProdukAndNamaProduk(idDetailJenisProduk);
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getJsonResponse(result, HttpStatus.OK, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when get all produk", e.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, e.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got exception {} when get all produk", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}

	}
	
	@SuppressWarnings("deprecation")
	@RequestMapping(value = "/find-produk-by-jenis-produk-and-nama-produk", method = RequestMethod.GET)
	public Map<String,Object>  findProdukByJenisProdukAndNamaProduk(HttpServletRequest request,@RequestParam(value = "idDetailJenisProduk", required = false) Integer idDetailJenisProduk) {
		String data = request.getQueryString();
		data = URLDecoder.decode(data);
		String[] arrQueries = data.split("&");
		String criteria = "";
		String values = "";
		for (String query : arrQueries) {
			if (query.indexOf("filter[filters][0][field]") >= 0) {
				if (criteria == null)
					criteria = query.replace("filter[filters][0][field]=", "");
				else
					criteria += "," + query.replace("filter[filters][0][field]=", "");

			} else if (query.indexOf("filter[filters][0][value]") >= 0) {
				if (values == null)
					values = query.replace("filter[filters][0][value]=", "");
				else
					values += query.replace("filter[filters][0][value]=", "");
			}
		}

		Map<String,Object>resultPageMap = produkService.findProdukByJenisProdukAndNamaProduk(idDetailJenisProduk, values);

		return resultPageMap;

	}

	//dipake di pengadaan
	@RequestMapping(value = "/detail-jenis-produk", method = RequestMethod.GET)
	public ResponseEntity<Map<String,Object>> findDetailJenisProduk(HttpServletRequest request) {
		try {
			Map<String,Object> result = produkService.detailJenisProduk();
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getJsonResponse(result, HttpStatus.OK, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when get all Detail Jenis", e.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, e.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got exception {} when get all Detail Jenis", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}

	}
	
	@SuppressWarnings("deprecation")
	@RequestMapping(value = "/find-produk-single", method = RequestMethod.GET)
	public Map<String,Object> findProduk(HttpServletRequest request) {
		String data = request.getQueryString();
		data = URLDecoder.decode(data);
		String[] arrQueries = data.split("&");
		String criteria = "";
		String values = "";
		for (String query : arrQueries) {
			if (query.indexOf("filter[filters][0][field]") >= 0) {
				if (criteria == null)
					criteria = query.replace("filter[filters][0][field]=", "");
				else
					criteria += "," + query.replace("filter[filters][0][field]=", "");

			} else if (query.indexOf("filter[filters][0][value]") >= 0) {
				if (values == null)
					values = query.replace("filter[filters][0][value]=", "");
				else
					values += query.replace("filter[filters][0][value]=", "");
			}
		}

		Map<String,Object>resultPageMap = produkService.findProduk(values);

		return resultPageMap;
	}
	
	@SuppressWarnings("deprecation")
	@RequestMapping(value = "/find-supplier", method = RequestMethod.GET)
	public Map<String,Object> findSupplier(HttpServletRequest request) {

		String data = request.getQueryString();
		data = URLDecoder.decode(data);
		String[] arrQueries = data.split("&");
		String criteria = "";
		String values = "";
		for (String query : arrQueries) {
			if (query.indexOf("filter[filters][0][field]") >= 0) {
				if (criteria == null)
					criteria = query.replace("filter[filters][0][field]=", "");
				else
					criteria += "," + query.replace("filter[filters][0][field]=", "");

			} else if (query.indexOf("filter[filters][0][value]") >= 0) {
				if (values == null)
					values = query.replace("filter[filters][0][value]=", "");
				else
					values += query.replace("filter[filters][0][value]=", "");
			}
		}

		Map<String,Object>resultPageMap = produkService.findSupplier(values);

		return resultPageMap;
	}
	
	@RequestMapping(value = "/find-jenis-produk-laboratorium", method = RequestMethod.GET) 
	public ResponseEntity<List<JenisProdukVO>> getJenisProdukByJenisProdukLaboratorium(){
		List<JenisProdukVO> resultPageMap = produkService.findJenisProdukLaboratorium();
		return RestUtil.getJsonResponse(resultPageMap, HttpStatus.OK);
	}
	
}
