package com.hx.phip.service.impl; import com.alibaba.fastjson.JSON; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.hx.mybatisTool.SqlSentence; import com.hx.phiappt.model.Appointment; import com.hx.phip.common.HisUrl; import com.hx.phip.config.SystemConfig; import com.hx.phiappt.dao.mapper.AppointmentMapper; import com.hx.phiappt.dao.mapper.SystemParameterMapper; import com.hx.phip.dao.mapper.syn.SyncAppointmentRecordMapper; import com.hx.phip.model.SyncAppointmentRecord; import com.hx.phip.service.AppointmentLogService; import com.hx.phip.uti.ApiUtil; import com.hx.resultTool.ResponseCode; import com.hx.resultTool.Result; import com.hx.util.StringUtils; import com.hz.util.http.HttpHzUtil; import com.hz.util.http.dto.HttpHzResponse; import net.sf.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @Author */ @Transactional @Service public class AppointmentLogServiceImpl implements AppointmentLogService { private static Logger logger = LoggerFactory.getLogger(AppointmentLogServiceImpl.class); @Resource private SyncAppointmentRecordMapper syncAppointmentRecordMapper ; @Resource private AppointmentMapper appointmentMapper; @Resource private SystemParameterMapper systemParameterMapper; @Resource private SystemConfig systemConfig; /** * 获取HIS token * * @return */ public String getToken(){ String at = ApiUtil.getOpenApiAccessToken(systemParameterMapper, systemConfig.getApiAppId(), systemConfig.getApiCode(), systemConfig.getApiSecret(), systemConfig.getApiUrl()); return at; } @Override public Result appointmentListLog(JSONObject jsonObject) { Integer pageNum= jsonObject.optInt("pageNum",0); Integer pageSize=jsonObject.optInt("pageSize",0); //分页插件 PageHelper.startPage(pageNum==0?1:pageNum, pageSize==0?10:pageSize); SqlSentence sqlSentence = new SqlSentence(); Map map=new HashMap<>(); sqlSentence.setM(map); StringBuffer sql = new StringBuffer(); sql.append("SELECT * FROM sync_appointment WHERE isDel=0 "); if(!StringUtils.isEmpty(jsonObject.optString("startTime",null))){ sql.append(" and createTime >= #{m.startTime} "); map.put("startTime", jsonObject.optString("startTime")); } if(!StringUtils.isEmpty(jsonObject.optString("endTime",null))){ sql.append(" and createTime <= #{m.endTime} "); map.put("endTime", jsonObject.optString("endTime")); } if(!StringUtils.isEmpty(jsonObject.optString("type",null))){ sql.append(" and type = #{m.type} "); map.put("type", jsonObject.optString("type")); } sql.append(" ORDER BY editTime desc"); sqlSentence.setSqlSentence(sql.toString()); List list = syncAppointmentRecordMapper.selectList(sqlSentence); PageInfo pageInfo = new PageInfo<>(list); Map data = new HashMap<>(); data.put("list", pageInfo.getList()); data.put("total", pageInfo.getTotal()); data.put("pages", pageInfo.getPages()); data.put("pageNum", pageInfo.getPageNum()); data.put("pageSize", pageInfo.getPageSize()); data.put("isLastPage", pageInfo.isIsLastPage()); return Result.success(data); } @Override public Result resynchronizeOrder(JSONObject jsonObject) { String id=jsonObject.optString("id"); if(StringUtils.isEmpty(id)) return Result.failure(ResponseCode.ERROR_PARAMS_VALIDATOR,"请选择需要重新同步的预约订单id"); SqlSentence sqlSentence = new SqlSentence(); Map map=new HashMap<>(); sqlSentence.setM(map); sqlSentence.setSqlSentence("SELECT * FROM sync_appointment WHERE isDel=0 and id = #{m.id}"); map.put("id", id); SyncAppointmentRecord appointmentRecord=syncAppointmentRecordMapper.selectOne(sqlSentence); if(appointmentRecord==null) return Result.failure(ResponseCode.ERROR_PARAMS_VALIDATOR,"找不到需要重新同步的预约订单"); if(appointmentRecord.getStatus()!=2 || appointmentRecord.getFailNumber()>=2 ) return Result.failure(ResponseCode.ERROR_PARAMS_VALIDATOR,"当前预约订单不支持重发"); Appointment appointment = appointmentMapper.selectOneByKey(appointmentRecord.getForeignKeyId()); if(appointment==null) return Result.failure(ResponseCode.ERROR_PARAMS_VALIDATOR,"当前数据找不到对应的预约订单"); String at = getToken(); //传参处理 Map header = new HashMap<>(); header.put("x-access-token",at); logger.info("主动同步HIS预约订单:"+appointmentRecord.getRequestParameters()); HttpHzResponse httpHzResponse = HttpHzUtil.HttpURLUtilJson(systemConfig.getApiUrl()+ HisUrl.WORKFLOW_APPOINTMENT,appointmentRecord.getRequestParameters(),null,header,"POST",null); logger.info("主动同步HIS预约订单返回:"+ JSON.toJSONString(httpHzResponse)); appointmentRecord.setReturnParameters(JSON.toJSONString(httpHzResponse)); appointmentRecord.setEditTime(new Date()); Result result=null; if("201".equals(httpHzResponse.getCode())){ JSONObject resultJosn=JSONObject.fromObject(httpHzResponse.getData()); if(resultJosn.containsKey("id")){ appointmentRecord.setSuccessNumber(1); appointmentRecord.setStatus(1); result=Result.success("请求成功"); appointment.setApiId(resultJosn.optString("id")); appointmentMapper.updateAll(appointment); }else { appointmentRecord.setFailNumber(appointmentRecord.getFailNumber()+1); appointmentRecord.setStatus(2); result= Result.success("预约订单重新发送失败"); } }else { appointmentRecord.setFailNumber(appointmentRecord.getFailNumber()+1); appointmentRecord.setStatus(2); result= Result.success("预约订单重新发送失败"); } syncAppointmentRecordMapper.updateAll(appointmentRecord); return result; } @Override public Result manualsyncOrder(JSONObject jsonObject) { String id=jsonObject.optString("id"); String hisId=jsonObject.optString("hisId"); if(StringUtils.isEmpty(id)) return Result.failure(ResponseCode.ERROR_PARAMS_VALIDATOR,"请选择需要手动同步的预约订单id"); if(StringUtils.isEmpty(hisId)) return Result.failure(ResponseCode.ERROR_PARAMS_VALIDATOR,"HIS预约订单id不能为空"); SqlSentence sqlSentence = new SqlSentence(); Map map=new HashMap<>(); sqlSentence.setM(map); sqlSentence.setSqlSentence("SELECT * FROM sync_appointment WHERE isDel=0 and id = #{m.id}"); map.put("id", id); SyncAppointmentRecord appointmentRecord=syncAppointmentRecordMapper.selectOne(sqlSentence); if(appointmentRecord==null) return Result.failure(ResponseCode.ERROR_PARAMS_VALIDATOR,"找不到需要重新同步的预约订单"); if(appointmentRecord.getStatus()!=2) return Result.failure(ResponseCode.ERROR_PARAMS_VALIDATOR,"当前预约订单不支持手动"); Appointment appointment = appointmentMapper.selectOneByKey(appointmentRecord.getForeignKeyId()); if(appointment==null) return Result.failure(ResponseCode.ERROR_PARAMS_VALIDATOR,"当前数据找不到对应的预约订单"); appointmentRecord.setReturnParameters(hisId); appointmentRecord.setEditTime(new Date()); appointmentRecord.setSuccessNumber(1); appointmentRecord.setStatus(1); syncAppointmentRecordMapper.updateAll(appointmentRecord); appointment.setApiId(hisId); appointmentMapper.updateAll(appointment); return Result.success("手动同步成功"); } }