|
@ -1,9 +1,18 @@
|
|
|
package com.yihu.wlyy.service.app.prescription;
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
|
import com.yihu.wlyy.entity.patient.prescription.PrescriptionDispensaryCode;
|
|
|
import com.yihu.wlyy.entity.patient.prescription.PrescriptionInfo;
|
|
|
import com.yihu.wlyy.repository.prescription.PrescriptionDispensaryCodeDao;
|
|
|
import com.yihu.wlyy.repository.prescription.PrescriptionInfoDao;
|
|
|
import com.yihu.wlyy.service.BaseService;
|
|
|
import com.yihu.wlyy.util.HttpUtil;
|
|
|
import com.yihu.wlyy.util.SystemConf;
|
|
|
import com.yihu.wlyy.util.fastdfs.FastDFSUtil;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.json.JSONArray;
|
|
|
import org.json.JSONObject;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.domain.Page;
|
|
|
import org.springframework.data.domain.PageRequest;
|
|
@ -12,9 +21,13 @@ import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.io.IOException;
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
import java.net.HttpURLConnection;
|
|
|
import java.net.MalformedURLException;
|
|
|
import java.net.URL;
|
|
|
import java.net.URLEncoder;
|
|
|
import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* Created by Administrator on 2017/7/21.
|
|
@ -25,14 +38,18 @@ import java.util.Map;
|
|
|
//@Transactional(rollbackFor = Exception.class)
|
|
|
public class PrescriptionDispensaryCodeService extends BaseService {
|
|
|
|
|
|
// @Autowired
|
|
|
// private PrescriptionDispensaryCodeDao prescriptionDispensaryCodeDao;
|
|
|
@Autowired
|
|
|
private PrescriptionDispensaryCodeDao prescriptionDispensaryCodeDao;
|
|
|
@Autowired
|
|
|
private JdbcTemplate jdbcTemplate;
|
|
|
@Autowired
|
|
|
private PrescriptionInfoDao prescriptionInfoDao;
|
|
|
private static Object obj = new Object();
|
|
|
|
|
|
String fastUrl = SystemConf.getInstance().getSystemProperties().getProperty("fastdfs_file_url");
|
|
|
|
|
|
//获取居民端取药码列表
|
|
|
public JSONArray findByStatusAndTime(Integer timeType, String patientCode, Integer status, Integer page, Integer pagesize) {
|
|
|
// 排序
|
|
|
//Sort sort = new Sort(Sort.Direction.DESC, "status","createTime");
|
|
|
// 分页信息
|
|
|
long timeLimit = 0;
|
|
|
if(timeType!=null){
|
|
@ -43,30 +60,139 @@ public class PrescriptionDispensaryCodeService extends BaseService {
|
|
|
case 4:timeLimit = 7*24*60*60*31*12;break;//近一年
|
|
|
}
|
|
|
}
|
|
|
//PageRequest pageRequest = new PageRequest(page, pagesize, sort);
|
|
|
List<Object> params = new ArrayList<>();
|
|
|
String sql = "select * from wlyy_prescription_dispensary_code a LEFT JOIN wlyy_prescription b on a.prescription_Code=b.code where 1=1 " ;
|
|
|
String precriptionSql = "select * from wlyy_prescription p1 LEFT JOIN wlyy_prescription_expressage p3 on p1.code=p3.prescription_code where 1=1 ";
|
|
|
|
|
|
if(status!=null){
|
|
|
sql += " and b.status = ? ";
|
|
|
precriptionSql += " and b.status = ? ";
|
|
|
params.add(status);
|
|
|
}else {
|
|
|
sql += " and b.status in (20,21,100)" ;
|
|
|
precriptionSql += " and b.status in (20,21,100)" ;
|
|
|
}
|
|
|
if(timeType!=null){
|
|
|
sql += "and TIMESTAMPDIFF(SECOND, a.create_Time, CURRENT_TIME) > ? ORDER BY b.status desc ,a.create_Time desc ";
|
|
|
precriptionSql += "and TIMESTAMPDIFF(SECOND, a.create_Time, CURRENT_TIME) > ? ORDER BY b.status desc ,a.create_Time desc ";
|
|
|
params.add(timeLimit);
|
|
|
}
|
|
|
sql += " limit "+(page-1)*pagesize+","+pagesize;
|
|
|
List<Map<String, Object>> result = jdbcTemplate.queryForList(sql,params.toArray());
|
|
|
JSONArray array = new JSONArray(result);
|
|
|
precriptionSql += " limit "+(page-1)*pagesize+","+pagesize;
|
|
|
List<Map<String, Object>> result = jdbcTemplate.queryForList(precriptionSql,params.toArray());
|
|
|
List<Map<String, Object>> result2 = new ArrayList<>();
|
|
|
for (Map<String, Object> m:result) {
|
|
|
List<PrescriptionInfo> list = prescriptionInfoDao.findByPrescriptionCode(m.get("code").toString());
|
|
|
m.put("data",list);
|
|
|
result2.add(m);
|
|
|
}
|
|
|
JSONArray array = new JSONArray(result2);
|
|
|
return array;
|
|
|
//return prescriptionDispensaryCodeDao.findByStatusAndTime(timeLimit,patientCode,status,pageRequest);
|
|
|
}
|
|
|
|
|
|
//判断居民是否有(21等待领药、20配送中、100已完成)取药码
|
|
|
public int dispensaryCodeCount(String patientCode){
|
|
|
String sql = "select count(1) from wlyy_prescription_dispensary_code p1 \n" +
|
|
|
" LEFT JOIN wlyy_prescription p2 on p1.prescription_Code = p2.code where p2.status in(21,20,100) and p2.patient =?";// where p2.status in(21,20,100) and p2.patient =?1
|
|
|
String sql = "select count(1) from wlyy_prescription where status in(21,20,100) and patient =?";// where p2.status in(21,20,100) and p2.patient =?1
|
|
|
int count = jdbcTemplate.queryForObject(sql,Integer.class,patientCode);
|
|
|
return count;
|
|
|
}
|
|
|
|
|
|
//生成取药码时,判断是否已经有重复的取药码
|
|
|
public boolean getResidentQRCodeCount(String code){
|
|
|
int count = prescriptionDispensaryCodeDao.getResidentQRCodeCount(code);
|
|
|
if(count>0){
|
|
|
return true;
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
//生成居民端取药码
|
|
|
public PrescriptionDispensaryCode savePatientQRCode(String token,String prescriptionCode){
|
|
|
//获取年月日8位数
|
|
|
Calendar cal = Calendar.getInstance();
|
|
|
String year = String.valueOf(cal.get(Calendar.YEAR));
|
|
|
int temp = cal.get(Calendar.MONTH)+1;
|
|
|
String month = cal.get(Calendar.MONTH)+1<10?"0"+temp:""+temp;
|
|
|
String day = ""+cal.get(Calendar.DAY_OF_MONTH);
|
|
|
String ymd = year+month+day;
|
|
|
// 二维码内容
|
|
|
String code = "";
|
|
|
synchronized (obj){
|
|
|
boolean bl = true;
|
|
|
while (bl){
|
|
|
String randomNum = randomString("0123456789",6);
|
|
|
code = ymd +randomNum;
|
|
|
bl = getResidentQRCodeCount(code);
|
|
|
}
|
|
|
}
|
|
|
// 生成二维码
|
|
|
ObjectNode imgNode = makeQrcodeFromWeiXin(token,code);
|
|
|
//Prescription类 取药类型:1 自取 2快递配送 3健管师配送
|
|
|
String fileUrl = fastUrl + imgNode.get("groupName").toString().replaceAll("\"","")
|
|
|
+ "/" + imgNode.get("remoteFileName").toString().replaceAll("\"","");
|
|
|
|
|
|
//新增居民处方取码记录
|
|
|
PrescriptionDispensaryCode prescriptionDispensaryCode = new PrescriptionDispensaryCode();
|
|
|
prescriptionDispensaryCode.setCode(code);
|
|
|
prescriptionDispensaryCode.setCreateTime(new Date());
|
|
|
prescriptionDispensaryCode.setImgUrl(fileUrl);
|
|
|
prescriptionDispensaryCode.setPrescriptionCode(prescriptionCode);
|
|
|
prescriptionDispensaryCode.setType(1);
|
|
|
return prescriptionDispensaryCodeDao.save(prescriptionDispensaryCode);
|
|
|
|
|
|
}
|
|
|
|
|
|
//生成配送员(健管师)取药码
|
|
|
public PrescriptionDispensaryCode saveDoctorQRCode(String code,String token,String prescriptionCode){
|
|
|
|
|
|
String jgsID= "";//配送员用户ID后4位
|
|
|
String jgsCode = code+jgsID.substring(jgsID.length()-4);
|
|
|
ObjectNode imgNode2 = makeQrcodeFromWeiXin(token,jgsCode);
|
|
|
String fileUrl = fastUrl + imgNode2.get("groupName").toString().replaceAll("\"","")
|
|
|
+ "/" + imgNode2.get("remoteFileName").toString().replaceAll("\"","");
|
|
|
//新增配送员处方取码记录
|
|
|
PrescriptionDispensaryCode prescriptionDispensaryCode = new PrescriptionDispensaryCode();
|
|
|
prescriptionDispensaryCode.setCode(jgsCode);
|
|
|
prescriptionDispensaryCode.setCreateTime(new Date());
|
|
|
prescriptionDispensaryCode.setImgUrl(fileUrl);
|
|
|
prescriptionDispensaryCode.setPrescriptionCode(prescriptionCode);
|
|
|
prescriptionDispensaryCode.setType(2);
|
|
|
return prescriptionDispensaryCodeDao.save(prescriptionDispensaryCode);
|
|
|
|
|
|
}
|
|
|
|
|
|
//在微信端生成二维码,并上传FastFDS
|
|
|
public ObjectNode makeQrcodeFromWeiXin(String token,String content){
|
|
|
String token_url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" + token;
|
|
|
String params = "{\"action_name\": \"QR_LIMIT_STR_SCENE\", \"action_info\": {\"scene\": {\"scene_str\": \"" + content + "\"}}}";
|
|
|
String result = HttpUtil.sendPost(token_url, params);
|
|
|
if (!StringUtils.isEmpty(result)) {
|
|
|
JSONObject json = new JSONObject(result);
|
|
|
|
|
|
// 下载二维码图片
|
|
|
URL urlGet = null;
|
|
|
try {
|
|
|
urlGet = new URL("https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket="
|
|
|
+ URLEncoder.encode(json.get("ticket").toString(), "UTF-8"));
|
|
|
HttpURLConnection connection = (HttpURLConnection) urlGet.openConnection();
|
|
|
connection.connect();
|
|
|
ObjectNode imgNode = FastDFSUtil.upload(connection.getInputStream(),"png","QRCode_"+System.currentTimeMillis());
|
|
|
return imgNode;
|
|
|
} catch (MalformedURLException e) {
|
|
|
e.printStackTrace();
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
e.printStackTrace();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
public String randomString(String base, int length) {
|
|
|
Random random = new Random();
|
|
|
StringBuffer sb = new StringBuffer();
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
int number = random.nextInt(base.length());
|
|
|
sb.append(base.charAt(number));
|
|
|
}
|
|
|
return sb.toString();
|
|
|
}
|
|
|
}
|