2021-01-07 11:34:56 +07:00

165 lines
5.2 KiB
Java

package com.jasamedika.medifirst2000.controller;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
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.service.ModelService;
import com.jasamedika.medifirst2000.vo.custom.BridgeBUKBorVO;
import com.jasamedika.medifirst2000.vo.custom.BridgeKunjunganPasienVO;
import com.jasamedika.medifirst2000.vo.custom.BridgeSiranapKamarVO;
import scala.actors.threadpool.Arrays;
import com.jasamedika.medifirst2000.dao.AntrianPasienDiPeriksaDao;
import com.jasamedika.medifirst2000.dao.BridgingDao;
@RestController
@RequestMapping("/bridging")
public class BridgingController {
@Autowired
BridgingDao myDao;
//@Value("${reportDirectory}")
//String reportDirectory;
@RequestMapping(value = "/testbor", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
public BridgeBUKBorVO test(@RequestParam(value = "bulan", required = true) String bulanTahun) throws ParseException {
//String ret = "";
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
List<Object[]> bed = myDao.getQtyBed();
Integer totalBed =0;
ArrayList<Integer> ruanganId= new ArrayList<Integer>();
for(int i = 0; i<bed.size();i++){
totalBed = totalBed + ((Byte)bed.get(i)[1]).intValue();
if(ruanganId.size()==0){
ruanganId.add((Integer)bed.get(i)[0]);
}else{
boolean found=false;
for(int j = 0;j<ruanganId.size();j++){
if(ruanganId.get(j)==(Integer)bed.get(i)[0]){
found = true;
break;
}
}
if(!found){
ruanganId.add((Integer)bed.get(i)[0]);
}
}
}
System.out.println("totalBed"+ String.valueOf(totalBed));
String bb[]= bulanTahun.split("-");
String bulan = bb[0].trim();
String tahun = bb[1].trim();
int month = Integer.parseInt(bulan);
int year = Integer.parseInt(tahun);
GregorianCalendar mycal = new GregorianCalendar(year,month, 1);
int daysInMonth= mycal.getActualMaximum(Calendar.DAY_OF_MONTH);
Date d1 = (Date) sdf.parse("01"+"/"+bulan+"/"+tahun);
Date d2 = (Date) sdf.parse(String.valueOf(daysInMonth)+"/"+bulan+"/"+tahun);
List<Object[]> x = myDao.getListPasienRanap(d1,d2);
System.out.println( "Jumlah Pasien total" + String.valueOf(x.size()) );
int cnt[] = new int[daysInMonth];
for(int i = 0;i<x.size();i++){
for(int j = 0;j<daysInMonth;j++){
Date tgl1 = (Date)x.get(i)[0];
Date tgl2 = (Date)x.get(i)[1];
String tgl1s[],tgl2s[];
try{
tgl1s= sdf.format(tgl1).split("/");
String tmp1[] =tgl1s;
if(Integer.valueOf(tmp1[0])==j){
}
}catch(Exception ex){
tgl1s= new String[0];
}
try{
tgl2s= sdf.format(tgl2).split("/");
}catch(Exception ex){
tgl2s= (String.valueOf(daysInMonth)+"/"+bulan+"/"+tahun).split("/");
}
if((j>=Integer.valueOf(tgl1s[0])+1)&(j<=Integer.valueOf(tgl2s[0])+1)){
cnt[j]++;
}
}
}
int totalCnt = 0;
for(int i=0;i<cnt.length;i++){
totalCnt=totalCnt+cnt[i];
}
BridgeBUKBorVO returnvalue = new BridgeBUKBorVO();
NumberFormat formatter = new DecimalFormat("#0.00");
returnvalue.setBOR(Double.valueOf(formatter.format((double)(totalCnt*100)/(double)(totalBed*daysInMonth))));
return returnvalue ;
}
// download:
@RequestMapping(value = "/test", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
public Map<String, Object> testFile(@RequestParam(value = "param", required = false) String param1,HttpServletResponse response) throws IOException {
String isiFile="halo";
//String fileName=reportDirectory+ UUID.randomUUID().toString().replaceAll("-", "")+".txt";
//String fileName="d:/"+ UUID.randomUUID().toString().replaceAll("-", "")+".txt";
String fileName="d:/testfile.txt";
FileWriter fw = new FileWriter(fileName);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(isiFile);
bw.flush();
bw.close();
//FileOutputStream out = new FileOutputStream(fileName);
String fileType = "application/text";
response.setContentType(fileType);
// Make sure to show the download dialog
response.setHeader("Content-disposition", "attachment; filename="+fileName);
File my_file = new File(fileName);
// This should send the file to browser
OutputStream out = response.getOutputStream();
FileInputStream in = new FileInputStream(my_file);
byte[] buffer = new byte[4096];
int length;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
in.close();
out.flush();
return null;
}
}