package thyyxxk.webserver.service.mzpayqrcode; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import thyyxxk.webserver.config.exception.ExceptionEnum; import thyyxxk.webserver.constants.Capacity; import thyyxxk.webserver.constants.ExternalAddr; import thyyxxk.webserver.dao.his.mzpayqrcode.MzPayQrcodeDao; import thyyxxk.webserver.entity.HrgResponse; import thyyxxk.webserver.entity.ResultVo; import thyyxxk.webserver.entity.mzpayqrcode.PatInfo; import thyyxxk.webserver.entity.mzpayqrcode.RoughIndex; import thyyxxk.webserver.utils.*; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @author dj */ @Slf4j @Service public class MzPayQrcodeService { private final MzPayQrcodeDao dao; private static final int MIN_PATIENT_ID_LENGTH = 7; @Autowired public MzPayQrcodeService(MzPayQrcodeDao dao) { this.dao = dao; } private PatInfo getPatientInfo(String patientId) { String socialNo = dao.selectSocialNoByPatientId(patientId); JSONObject obj = new JSONObject(); obj.put("patIdType", 11); obj.put("patIdNo", socialNo); RestTemplate template = new RestTemplate(); return template.postForObject(ExternalAddr.QUERY_PATIENT_INFO, obj, PatInfo.class); } public ResultVo>> getRoughList(String patientId) { if (null == patientId || patientId.length() < MIN_PATIENT_ID_LENGTH) { return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "请输入正确的门诊ID号。"); } patientId = patientId.trim(); JSONObject obj = new JSONObject(); obj.put("patCardType", 21); obj.put("patCardNo", patientId); obj.put("hisOrdNum", ""); RestTemplate template = new RestTemplate(); HrgResponse hrgRes = template.postForObject(ExternalAddr.GET_MZ_CHARGE_DETAIL_FOR_UN_PAID, obj, HrgResponse.class); if (null == hrgRes || null == hrgRes.getResultCode()) { return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "网络服务错误!"); } if (hrgRes.getResultCode() != 0 ) { return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, hrgRes.getResultMessage()); } List> list = FilterUtil.cast(hrgRes.getData()); for (Map item : list) { item.put("fixedAmt", DecimalUtil.divide(String.valueOf(item.get("totalAmt")), "100")); } return ResultVoUtil.success(list); } @SuppressWarnings("unchecked") public ResultVo getDetailFees(RoughIndex param) { PatInfo patInfo = getPatientInfo(param.getPatientId()); if (null == patInfo || null == patInfo.getResultCode() || 0 != patInfo.getResultCode()) { return ResultVoUtil.fail(ExceptionEnum.INTERNAL_SERVER_ERROR, "无法获取到患者个人信息!"); } JSONObject pObj = new JSONObject(); pObj.put("patCardType", 21); pObj.put("patCardNo", param.getPatientId()); pObj.put("hisOrdNum", param.getHisOrdNum()); RestTemplate template = new RestTemplate(); HrgResponse hrgRes = template.postForObject(ExternalAddr.GET_CHARGE_DETAIL_BY_HIS_ORD_NUM, pObj, HrgResponse.class); if (null == hrgRes || null == hrgRes.getResultCode()) { return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "网络服务错误!"); } if (hrgRes.getResultCode() != 0 ) { return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, hrgRes.getResultMessage()); } List> list = FilterUtil.cast(hrgRes.getData()); JSONObject retObj = new JSONObject(); retObj.put("list", makeSimpleData(list)); RestTemplate restTemplate = new RestTemplate(); ResultVo qrResult = restTemplate.postForObject(ExternalAddr.CREATE_ORDER_FOR_MZ_GUIDE_BILL, param, ResultVo.class); if (null == qrResult || qrResult.getCode() != ExceptionEnum.SUCCESS.getCode()) { return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "请求微信支付二维码失败,请联系管理员。"); } retObj.put("wxQrcode", qrResult.getData()); return ResultVoUtil.success(retObj); } private JSONArray makeSimpleData(List> arr) { JSONArray array = new JSONArray(); Map map = new HashMap<>(Capacity.DEFAULT); for (Map item : arr) { final String itemType = "".equals(item.get("itemType")) ? " - " : item.get("itemType"); String execDeptName = "".equals(item.get("execDeptName")) ? " - " : item.get("execDeptName"); String execDeptAddress = "".equals(item.get("execDeptAddress")) ? " - " : item.get("execDeptAddress"); if ("静脉采血".equals(item.get("itemName"))) { execDeptName = "急诊抽血室"; execDeptAddress = "门诊大楼一楼"; } String key = itemType + "^" + execDeptName + "^" + execDeptAddress; if (!map.containsKey(key)) { map.put(key, String.valueOf(item.get("itemTotalFee"))); } else { String fee = DecimalUtil.add(String.valueOf(map.get(key)), String.valueOf(item.get("itemTotalFee"))); map.replace(key, fee); } } for (Map.Entry entry : map.entrySet()) { String[] keys = entry.getKey().split("\\^"); JSONObject obj = new JSONObject(); obj.put("itemType", keys[0]); obj.put("execDeptName", keys[1]); obj.put("execDeptAddress", keys[2]); obj.put("itemTotalFee", entry.getValue()); obj.put("fixedAmt", DecimalUtil.divide(entry.getValue(), "100")); array.add(obj); } return array; } }