|  | @@ -66,9 +66,11 @@ import org.springframework.web.bind.annotation.*;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  import javax.servlet.http.HttpServletRequest;
 | 
	
		
			
				|  |  |  import java.math.BigDecimal;
 | 
	
		
			
				|  |  | +import java.math.BigInteger;
 | 
	
		
			
				|  |  |  import java.text.SimpleDateFormat;
 | 
	
		
			
				|  |  |  import java.util.*;
 | 
	
		
			
				|  |  |  import java.util.concurrent.TimeUnit;
 | 
	
		
			
				|  |  | +import java.util.stream.Collectors;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  @Api("商城接口")
 | 
	
	
		
			
				|  | @@ -355,64 +357,51 @@ public class StoreOrderScrmController extends AppBaseController {
 | 
	
		
			
				|  |  |      //@Synchronized
 | 
	
		
			
				|  |  |      public R pay(HttpServletRequest request, @Validated @RequestBody FsStoreOrderPayParam param)
 | 
	
		
			
				|  |  |      {
 | 
	
		
			
				|  |  | -        FsStoreOrderScrm order=orderService.selectFsStoreOrderById(param.getOrderId());
 | 
	
		
			
				|  |  | -        if(order==null){
 | 
	
		
			
				|  |  | +        FsStoreOrderScrm order = orderService.selectFsStoreOrderById(param.getOrderId());
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        String errorMsg = checkPrescriptionOrders(Collections.singletonList(order));
 | 
	
		
			
				|  |  | +        if (errorMsg != null) {
 | 
	
		
			
				|  |  | +            return R.error(errorMsg);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        if (order == null) {
 | 
	
		
			
				|  |  |              return R.error("订单不存在");
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | -        if(order.getStatus()!= OrderInfoEnum.STATUS_0.getValue()){
 | 
	
		
			
				|  |  | +        if (order.getStatus() != OrderInfoEnum.STATUS_0.getValue()) {
 | 
	
		
			
				|  |  |              return R.error("订单状态不正确");
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | -        String orderId=redisCache.getCacheObject("isPaying:"+order.getId());
 | 
	
		
			
				|  |  | -        if(StringUtils.isNotEmpty(orderId)&&orderId.equals(order.getId().toString())){
 | 
	
		
			
				|  |  | -            return R.error("正在支付中...");
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -        FsUserScrm user=userService.selectFsUserById(order.getUserId());
 | 
	
		
			
				|  |  | -        if(user!=null&& StringUtils.isNotEmpty(user.getMaOpenId())){
 | 
	
		
			
				|  |  | +        FsUserScrm user = userService.selectFsUserById(order.getUserId());
 | 
	
		
			
				|  |  | +        if (user != null && StringUtils.isNotEmpty(user.getMaOpenId())) {
 | 
	
		
			
				|  |  |              //已改价处理
 | 
	
		
			
				|  |  | -            if(order.getIsEditMoney()!=null&&order.getIsEditMoney()==1){
 | 
	
		
			
				|  |  | +            if (order.getIsEditMoney() != null && order.getIsEditMoney() == 1) {
 | 
	
		
			
				|  |  |                  //改过价不做处理
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -            }
 | 
	
		
			
				|  |  | -            else{
 | 
	
		
			
				|  |  | -                String config=configService.selectConfigByKey("his.store");
 | 
	
		
			
				|  |  | -                StoreConfig storeConfig= JSONUtil.toBean(config,StoreConfig.class);
 | 
	
		
			
				|  |  | -                if(param.getPayType().equals(1)){
 | 
	
		
			
				|  |  | +            } else {
 | 
	
		
			
				|  |  | +                String config = configService.selectConfigByKey("store.config");
 | 
	
		
			
				|  |  | +                StoreConfig storeConfig = JSONUtil.toBean(config, StoreConfig.class);
 | 
	
		
			
				|  |  | +                if (param.getPayType().equals(1)) {
 | 
	
		
			
				|  |  |                      order.setPayType("1");
 | 
	
		
			
				|  |  |                      order.setPayMoney(order.getPayPrice());
 | 
	
		
			
				|  |  |                      order.setPayDelivery(BigDecimal.ZERO);
 | 
	
		
			
				|  |  | -                }
 | 
	
		
			
				|  |  | -                else if(param.getPayType().equals(2)){
 | 
	
		
			
				|  |  | +                } else if (param.getPayType().equals(2)) {
 | 
	
		
			
				|  |  |                      order.setPayType("2");
 | 
	
		
			
				|  |  | -                    BigDecimal payMoney=order.getPayPrice().multiply(new BigDecimal(storeConfig.getPayRate())).divide(new BigDecimal(100));
 | 
	
		
			
				|  |  | -                    payMoney=new BigDecimal(payMoney.setScale(0, BigDecimal.ROUND_HALF_UP).longValue());
 | 
	
		
			
				|  |  | +                    BigDecimal payMoney = order.getPayPrice().multiply(new BigDecimal(storeConfig.getPayRate())).divide(new BigDecimal(100));
 | 
	
		
			
				|  |  | +                    payMoney = new BigDecimal(payMoney.setScale(0, BigDecimal.ROUND_HALF_UP).longValue());
 | 
	
		
			
				|  |  |                      order.setPayDelivery(order.getPayPrice().subtract(payMoney));
 | 
	
		
			
				|  |  |                      order.setPayMoney(payMoney);
 | 
	
		
			
				|  |  | -                }
 | 
	
		
			
				|  |  | -                else if(param.getPayType().equals(3)){
 | 
	
		
			
				|  |  | +                } else if (param.getPayType().equals(3)) {
 | 
	
		
			
				|  |  |                      //货到付款
 | 
	
		
			
				|  |  |                      order.setPayType("3");
 | 
	
		
			
				|  |  | -                    BigDecimal amount=redisCache.getCacheObject("orderAmount:"+order.getId());
 | 
	
		
			
				|  |  | -                    BigDecimal payMoney = BigDecimal.ZERO;
 | 
	
		
			
				|  |  | -                    if (amount != null){
 | 
	
		
			
				|  |  | -                        payMoney=amount;
 | 
	
		
			
				|  |  | -                    }
 | 
	
		
			
				|  |  | -                    order.setPayMoney(payMoney);
 | 
	
		
			
				|  |  | -                    order.setPayDelivery(order.getPayPrice().subtract(payMoney) );
 | 
	
		
			
				|  |  | -//                    order.setPayMoney(BigDecimal.ZERO);
 | 
	
		
			
				|  |  | +                    order.setPayDelivery(order.getPayPrice());
 | 
	
		
			
				|  |  | +                    order.setPayMoney(BigDecimal.ZERO);
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  |                  orderService.updateFsStoreOrder(order);
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |              String payCode = IdUtil.getSnowflake(0, 0).nextIdStr();
 | 
	
		
			
				|  |  | -//            order.setOrderCode(orderCode);
 | 
	
		
			
				|  |  | -//            if(order.getPayType().equals("1")||order.getPayType().equals("2")){
 | 
	
		
			
				|  |  | -            if((order.getPayType().equals("1")||order.getPayType().equals("2")||order.getPayType().equals("3")) && order.getPayMoney().compareTo(new BigDecimal(0))>0){
 | 
	
		
			
				|  |  | -                String json = configService.selectConfigByKey(STORE_PAY_CONF);
 | 
	
		
			
				|  |  | -                FsPayConfigScrm fsPayConfig = JSON.parseObject(json, FsPayConfigScrm.class);
 | 
	
		
			
				|  |  | -                FsStorePaymentScrm storePayment=new FsStorePaymentScrm();
 | 
	
		
			
				|  |  | +//          order.setOrderCode(orderCode);
 | 
	
		
			
				|  |  | +            if (order.getPayType().equals("1") || order.getPayType().equals("2")) {
 | 
	
		
			
				|  |  | +                FsStorePaymentScrm storePayment = new FsStorePaymentScrm();
 | 
	
		
			
				|  |  |                  storePayment.setCompanyId(order.getCompanyId());
 | 
	
		
			
				|  |  |                  storePayment.setCompanyUserId(order.getCompanyUserId());
 | 
	
		
			
				|  |  | -                storePayment.setPayMode(fsPayConfig.getType());
 | 
	
		
			
				|  |  |                  storePayment.setStatus(0);
 | 
	
		
			
				|  |  |                  storePayment.setPayCode(payCode);
 | 
	
		
			
				|  |  |                  storePayment.setPayMoney(order.getPayMoney());
 | 
	
	
		
			
				|  | @@ -425,72 +414,59 @@ public class StoreOrderScrmController extends AppBaseController {
 | 
	
		
			
				|  |  |                  storePayment.setBusinessOrderId(order.getId().toString());
 | 
	
		
			
				|  |  |                  storePayment.setOrderId(order.getId());
 | 
	
		
			
				|  |  |                  fsStorePaymentMapper.insertFsStorePayment(storePayment);
 | 
	
		
			
				|  |  | +                HuiFuCreateOrder o = new HuiFuCreateOrder();
 | 
	
		
			
				|  |  | +                o.setTradeType("T_MINIAPP");
 | 
	
		
			
				|  |  | +                o.setOpenid(user.getMaOpenId());
 | 
	
		
			
				|  |  | +                o.setReqSeqId("store-" + storePayment.getPayCode());
 | 
	
		
			
				|  |  | +                o.setTransAmt(storePayment.getPayMoney().toString());
 | 
	
		
			
				|  |  | +                o.setGoodsDesc("商城订单支付");
 | 
	
		
			
				|  |  | +                HuifuCreateOrderResult result = huiFuService.createOrder(o);
 | 
	
		
			
				|  |  | +                //创建订单
 | 
	
		
			
				|  |  | +                if (result.getBank_code() != null && result.getBank_code().equals("00000000")) {
 | 
	
		
			
				|  |  | +                    FsStorePaymentScrm mt = new FsStorePaymentScrm();
 | 
	
		
			
				|  |  | +                    mt.setPaymentId(storePayment.getPaymentId());
 | 
	
		
			
				|  |  | +                    mt.setTradeNo(result.getHf_seq_id());
 | 
	
		
			
				|  |  | +                    fsStorePaymentMapper.updateFsStorePayment(mt);
 | 
	
		
			
				|  |  | +//                    redisCache.setCacheObject("isPaying:" + order.getId(), order.getId().toString(), 1, TimeUnit.MINUTES);
 | 
	
		
			
				|  |  | +                    String mes = result.getPay_info();
 | 
	
		
			
				|  |  | +                    JSONObject payInfo = JSONUtil.parseObj(mes);
 | 
	
		
			
				|  |  | +                    FsHuiFuPayInfo fsHuiFuPayInfo = new FsHuiFuPayInfo();
 | 
	
		
			
				|  |  | +                    if (payInfo.get("timeStamp") != null) {
 | 
	
		
			
				|  |  | +                        fsHuiFuPayInfo.setTimeStamp(payInfo.get("timeStamp").toString());
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                    if (payInfo.get("package") != null) {
 | 
	
		
			
				|  |  | +                        fsHuiFuPayInfo.setPackageStr(payInfo.get("package").toString());
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                    if (payInfo.get("paySign") != null) {
 | 
	
		
			
				|  |  | +                        fsHuiFuPayInfo.setPaySign(payInfo.get("paySign").toString());
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -                if (fsPayConfig.getType().equals("hf")){
 | 
	
		
			
				|  |  | -                    HuiFuCreateOrder o = new HuiFuCreateOrder();
 | 
	
		
			
				|  |  | -                    o.setTradeType("T_MINIAPP");
 | 
	
		
			
				|  |  | -                    o.setOpenid(user.getMaOpenId());
 | 
	
		
			
				|  |  | -                    o.setReqSeqId("store-"+storePayment.getPayCode());
 | 
	
		
			
				|  |  | -                    o.setTransAmt(storePayment.getPayMoney().toString());
 | 
	
		
			
				|  |  | -                    o.setGoodsDesc("商城订单支付");
 | 
	
		
			
				|  |  | -                    if (param != null && StringUtils.isNotBlank(param.getAppId())) {
 | 
	
		
			
				|  |  | -                        o.setAppId(param.getAppId());
 | 
	
		
			
				|  |  |                      }
 | 
	
		
			
				|  |  | -                    HuifuCreateOrderResult result = huiFuService.createOrder(o);
 | 
	
		
			
				|  |  | -                    if(result.getResp_code()!=null&&(result.getResp_code().equals("00000000")||result.getResp_code().equals("00000100"))){
 | 
	
		
			
				|  |  | -                        FsStorePaymentScrm mt=new FsStorePaymentScrm();
 | 
	
		
			
				|  |  | -                        mt.setPaymentId(storePayment.getPaymentId());
 | 
	
		
			
				|  |  | -                        mt.setTradeNo(result.getHf_seq_id());
 | 
	
		
			
				|  |  | -                        fsStorePaymentMapper.updateFsStorePayment(mt);
 | 
	
		
			
				|  |  | -                        redisCache.setCacheObject("isPaying:"+order.getId(),order.getId().toString(),1, TimeUnit.MINUTES);
 | 
	
		
			
				|  |  | -                        Map<String, Object> resultMap = JSON.parseObject(result.getPay_info(), new TypeReference<Map<String, Object>>() {});
 | 
	
		
			
				|  |  | -                        String s = (String) resultMap.get("package");
 | 
	
		
			
				|  |  | -                        resultMap.put("packageValue",s);
 | 
	
		
			
				|  |  | -                        return R.ok().put("payType",param.getPayType()).put("result",resultMap);
 | 
	
		
			
				|  |  | +                    if (payInfo.get("appId") != null) {
 | 
	
		
			
				|  |  | +                        fsHuiFuPayInfo.setAppId(payInfo.get("appId").toString());
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |                      }
 | 
	
		
			
				|  |  | -                    else{
 | 
	
		
			
				|  |  | -                        return R.error(result.getResp_desc());
 | 
	
		
			
				|  |  | +                    if (payInfo.get("signType") != null) {
 | 
	
		
			
				|  |  | +                        fsHuiFuPayInfo.setSignType(payInfo.get("signType").toString());
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |                      }
 | 
	
		
			
				|  |  | -                }else  if (fsPayConfig.getType().equals("wx")){
 | 
	
		
			
				|  |  | -                    WxPayConfig payConfig = new WxPayConfig();
 | 
	
		
			
				|  |  | -                    payConfig.setAppId(fsPayConfig.getAppId());
 | 
	
		
			
				|  |  | -                    payConfig.setMchId(fsPayConfig.getWxMchId());
 | 
	
		
			
				|  |  | -                    payConfig.setMchKey(fsPayConfig.getWxMchKey());
 | 
	
		
			
				|  |  | -                    payConfig.setSubAppId(org.apache.commons.lang3.StringUtils.trimToNull(null));
 | 
	
		
			
				|  |  | -                    payConfig.setSubMchId(org.apache.commons.lang3.StringUtils.trimToNull(null));
 | 
	
		
			
				|  |  | -                    payConfig.setKeyPath(fsPayConfig.getKeyPath());
 | 
	
		
			
				|  |  | -                    payConfig.setNotifyUrl(fsPayConfig.getNotifyUrlScrm());
 | 
	
		
			
				|  |  | -                    wxPayService.setConfig(payConfig);
 | 
	
		
			
				|  |  | -                    WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
 | 
	
		
			
				|  |  | -                    orderRequest.setOpenid(user.getMaOpenId());//公众号支付提供用户openid
 | 
	
		
			
				|  |  | -                    orderRequest.setBody("商城订单支付");
 | 
	
		
			
				|  |  | -                    orderRequest.setOutTradeNo("store-" + storePayment.getPayCode());
 | 
	
		
			
				|  |  | -                    orderRequest.setTotalFee(WxPayUnifiedOrderRequest.yuanToFen(storePayment.getPayMoney().toString()));//测试
 | 
	
		
			
				|  |  | -                    //orderRequest.setTotalFee(WxPayUnifiedOrderRequest.yuanToFen(money));//测试
 | 
	
		
			
				|  |  | -                    orderRequest.setTradeType("JSAPI");
 | 
	
		
			
				|  |  | -                    orderRequest.setSpbillCreateIp(IpUtils.getIpAddr(ServletUtils.getRequest()));
 | 
	
		
			
				|  |  | -                    //调用统一下单接口,获取"预支付交易会话标识"
 | 
	
		
			
				|  |  | -                    try {
 | 
	
		
			
				|  |  | -                        WxPayMpOrderResult orderResult = wxPayService.createOrder(orderRequest);
 | 
	
		
			
				|  |  | -                        return R.ok().put("result", orderResult).put("type", "wx").put("isPay", 0).put("payType",param.getPayType());
 | 
	
		
			
				|  |  | -                    } catch (WxPayException e) {
 | 
	
		
			
				|  |  | -                        e.printStackTrace();
 | 
	
		
			
				|  |  | -                        throw new CustomException("支付失败" + e.getMessage());
 | 
	
		
			
				|  |  | +                    if (payInfo.get("nonceStr") != null) {
 | 
	
		
			
				|  |  | +                        fsHuiFuPayInfo.setNonceStr(payInfo.get("nonceStr").toString());
 | 
	
		
			
				|  |  |                      }
 | 
	
		
			
				|  |  | +                    return R.ok().put("payType", param.getPayType()).put("result", fsHuiFuPayInfo);
 | 
	
		
			
				|  |  | +                } else {
 | 
	
		
			
				|  |  | +                    return R.error(result.getResp_desc());
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  | -            }
 | 
	
		
			
				|  |  | -//            else if(order.getPayType().equals("3")){
 | 
	
		
			
				|  |  | -            else if(order.getPayType().equals("3") && order.getPayMoney().compareTo(new BigDecimal(0))<=0){
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            } else if (order.getPayType().equals("3")) {
 | 
	
		
			
				|  |  |                  //货到付款
 | 
	
		
			
				|  |  | -                orderService.payConfirm(2,order.getId(),null,null,null,null);
 | 
	
		
			
				|  |  | -                return R.ok().put("payType",param.getPayType());
 | 
	
		
			
				|  |  | +                orderService.payConfirm(2, order.getId(), null, null, null, null);
 | 
	
		
			
				|  |  | +                return R.ok().put("payType", param.getPayType());
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |              return R.error();
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -        else{
 | 
	
		
			
				|  |  | +        } else {
 | 
	
		
			
				|  |  |              return R.error("用户OPENID不存在");
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      @Login
 | 
	
	
		
			
				|  | @@ -507,6 +483,12 @@ public class StoreOrderScrmController extends AppBaseController {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          String payCode = IdUtil.getSnowflake(0, 0).nextIdStr();
 | 
	
		
			
				|  |  |          List<FsStoreOrderScrm> orders = orderService.getStoreOrderByCombinationId(param.getCombinationOrderId());
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        String errorMsg = checkPrescriptionOrders(orders);
 | 
	
		
			
				|  |  | +        if (errorMsg != null) {
 | 
	
		
			
				|  |  | +            return R.error(errorMsg);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |          //新增的支付详情id
 | 
	
		
			
				|  |  |          ArrayList<Long> paymentIds = new ArrayList<>();
 | 
	
		
			
				|  |  |          for (FsStoreOrderScrm order : orders) {
 | 
	
	
		
			
				|  | @@ -1465,4 +1447,30 @@ public class StoreOrderScrmController extends AppBaseController {
 | 
	
		
			
				|  |  |              return R.error(e.getMessage());
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 检查订单列表中是否存在含处方药但未开方的订单
 | 
	
		
			
				|  |  | +     * @param orders 待检查的订单列表
 | 
	
		
			
				|  |  | +     * @return 存在问题订单返回错误提示信息,否则返回null
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    public String checkPrescriptionOrders(List<FsStoreOrderScrm> orders) {
 | 
	
		
			
				|  |  | +        if (orders == null || orders.isEmpty()) {
 | 
	
		
			
				|  |  | +            return null;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        List<Long> orderIds = orders.stream()
 | 
	
		
			
				|  |  | +                .map(FsStoreOrderScrm::getId)
 | 
	
		
			
				|  |  | +                .collect(Collectors.toList());
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        Map<Long, Long> prescriptionCountMap = orderService.batchGetPrescriptionDrugCount(orderIds);
 | 
	
		
			
				|  |  | +        if(prescriptionCountMap != null){
 | 
	
		
			
				|  |  | +            for (FsStoreOrderScrm order : orders) {
 | 
	
		
			
				|  |  | +                System.out.println(prescriptionCountMap.containsKey(BigInteger.valueOf(order.getId())));
 | 
	
		
			
				|  |  | +                if (prescriptionCountMap.containsKey(BigInteger.valueOf(order.getId())) && (order.getIsPrescribe() == null || order.getIsPrescribe() < 1)) {
 | 
	
		
			
				|  |  | +                    return "处方订单未开具,无法支付!";
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return null;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  |  }
 |