package com.ywt.outpatient.rpc.provider;

import com.ywt.gapi.ResultCode;
import com.ywt.gapi.checkup_booking.*;
import com.ywt.outpatient.core.BaseResponse;
import com.ywt.outpatient.service.rpc.CheckupBookingService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class CheckupBookingProvider extends DubboCheckupBookingRpcServiceTriple.CheckupBookingRpcServiceImplBase {
    private static Logger logger = LoggerFactory.getLogger(CheckupBookingProvider.class);


    @Autowired
    private CheckupBookingService checkupBookingService;

    @DubboReference
    private DubboCheckupBookingServiceTriple.CheckupBookingServiceStub bookingServiceBlockingStub;


    @Override
    public GetAnnouncementResponse getAnnouncement(GetAnnouncementRequest request) {
        try {
            return checkupBookingService.showCheckupAnnouncement(request.getHospitalId());
        } catch (Exception e) {
            logger.error("CheckupBookingServiceImpl#getAnnouncement(): {}", e.getMessage(), e);
            return GetAnnouncementResponse.newBuilder().setCode(ResultCode.APP_ERROR_VALUE).setInfo(e.getMessage()).build();
        }
    }

    // 创建订单
    @Override
    public CreateOrderResponse createOrder(CreateOrderRequest request) {
        CreateOrderResponse.Builder builder = CreateOrderResponse.newBuilder();
        try {
            return checkupBookingService.createOrder(request.getRegRecord(), request.getUserId(), request.getHospitalId(),
                    request.getSubHospitalId(), request.getMedCardNo());
        } catch (Exception e) {
            logger.error("CheckupBookingServiceImpl#createOrder(request={} ):\n {}", request, e.getMessage(), e);
            builder.setCode(ResultCode.APP_ERROR_VALUE).setInfo(e.getMessage()).build();
        }
        return builder.build();
    }

    //取消订单
    @Override
    public CancelOrderResponse cancelOrder(CancelRegRequest request) {
        CancelOrderResponse.Builder builder = CancelOrderResponse.newBuilder();
        try {
            CancelRegResponse rep = bookingServiceBlockingStub.cancelReg(CancelRegRequest.newBuilder()
                    .setRegId(request.getRegId())
                    .setIdCard(request.getIdCard())
                    .setHospitalId(request.getHospitalId())
                    .build());
            if (rep.getCode() == BaseResponse.SUCCEED) {
                BaseResponse resp = checkupBookingService.cancelOrder(request.getRegId(), request.getHospitalId());
                builder.setCode(resp.getCode())
                        .setInfo(resp.getInfo());
            } else {
                logger.error("CheckupBookingServiceImpl#cancelOrder(): {}", rep.getInfo());
                builder.setCode(ResultCode.APP_ERROR_VALUE).setInfo(rep.getInfo());
            }
        } catch (Exception e) {
            logger.error("cancelOrder(): {}", e.getMessage(), e);
            builder.setCode(ResultCode.APP_EXCEPTION_VALUE).setInfo(e.getMessage());
        }
        return builder.build();
    }

    //获取订单列表
    @Override
    public GetOrderListResponse getOrderList(GetOrderListRequest request) {

        try {
            return checkupBookingService.gerOrderList(request.getIdCard(), request.getHospitalId());
        } catch (Exception e) {
            logger.error("CheckupBookingServiceImpl#getOrderList(): {}", e.getMessage(), e);
            return GetOrderListResponse.newBuilder().setCode(ResultCode.APP_ERROR_VALUE).setInfo(e.getMessage()).build();
        }
    }


    @Override
    public GetNoticeResponse getPackageNotice(GetNoticeRequest request) {
        try {
            return checkupBookingService.getNotice(request.getOrdSetsId(), request.getHospitalId());
        } catch (Exception e) {
            logger.error("CheckupBookingServiceImpl#getPackageNotice(): {}", e.getMessage(), e);
            return GetNoticeResponse.newBuilder().setCode(ResultCode.APP_ERROR_VALUE).setInfo(e.getMessage()).build();
        }
    }
}


