<?php

namespace App\Controller;

use App\Amqp\Producer\AssignDoctorProducer;
use App\Constants\DoctorTitleCode;
use App\Constants\HttpEnumCode;
use App\Exception\BusinessException;
use App\Model\Hospital;
use App\Model\MessageIm;
use App\Model\OrderInquiry;
use App\Model\OrderInquiryCoupon;
use App\Model\OrderInquiryRefund;
use App\Model\OrderProduct;
use App\Model\OrderProductItem;
use App\Model\OrderProductRefund;
use App\Model\Product;
use App\Model\ProductPlatformAmount;
use App\Model\UserCoupon;
use App\Model\UserDoctor;
use App\Model\UserPatient;
use App\Services\BaseService;
use App\Services\ImService;
use App\Utils\Log;
use Extend\TencentIm\Account;
use Extend\TencentIm\Message;
use Extend\TencentIm\Profile;
use Extend\Wechat\WechatPay;
use Hyperf\Amqp\Producer;
use Hyperf\Amqp\Result;
use Hyperf\DbConnection\Db;
use Hyperf\HttpMessage\Stream\SwooleFileStream;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Hyperf\Redis\Redis;
use Hyperf\Utils\ApplicationContext;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;

class CallBackController extends AbstractController
{
    /**
     * 患者端问诊微信支付回调
     * @return ResponseInterface
     * @throws \Throwable
     */
    public function wxPayInquirySuccessCallBack(): ResponseInterface
    {
        try {
            // 处理支付结果事件
            $WechatPay = new WechatPay(1,1);
            $app = $WechatPay->createApp();
            $server = $app->getServer();

            $message = $server->getRequestMessage();
            if (empty($message)){
                return $this->response->withStatus(500)->withBody(new SwooleStream(strval(json_encode(['code' => 'ERROR', 'message' => "问诊微信支付回调数据为空"], JSON_UNESCAPED_UNICODE))));
            }

            // 验证推送消息签名
            $app->getValidator()->validate($app->getRequest());

            Log::getInstance()->info("问诊微信支付回调数据：" . json_encode($message->toArray(),JSON_UNESCAPED_UNICODE));

            if (empty($message['out_trade_no'])){
                Log::getInstance()->info("问诊微信支付回调数据处理失败，缺少外部订单号");
                return $server->serve();
            }

            // 查询订单
            $params = array();
            $params['inquiry_no'] = $message['out_trade_no'];
            $order_inquiry = OrderInquiry::getOne($params);
            if (empty($order_inquiry)){
                Log::getInstance()->info("问诊微信支付回调数据处理失败，无订单数据");
                return $server->serve();
            }

            // 验证订单状态
            if ($order_inquiry['inquiry_status'] != 1){
                // 问诊订单状态（1:待支付 2:待分配 3:待接诊 4:已接诊 5:已完成 6:已结束 7:已取消）
                Log::getInstance()->info("问诊微信支付回调数据处理失败，订单状态当前为" . $order_inquiry['inquiry_status']);
                return $server->serve();
            }

            // 支付状态无需验证，如第一次支付失败，会修改支付状态，再次支付时，会出现验证不通过的情况

            // 修改支付状态
            $data = array();
            if ($message['trade_state'] == "SUCCESS"){
                // 支付成功
                $data['inquiry_pay_status'] = 2;
                $data['pay_time'] = date('Y-m-d H:i:s',strtotime($message['success_time']));// 支付时间
                if ($order_inquiry['inquiry_type'] == 1 || $order_inquiry['inquiry_type'] == 3){
                    // 专家-公益
                    $data['inquiry_status'] = 3;// 3:待接诊
                }elseif ($order_inquiry['inquiry_type'] == 2 || $order_inquiry['inquiry_type'] == 4){
                    // 快速-购药
                    $data['inquiry_status'] = 2;// 2:待分配
                }
            }elseif($message['trade_state'] == "CLOSED"){
                // 已关闭
                $data['inquiry_pay_status'] = 6;
            }elseif($message['trade_state'] == "REVOKED"){
                // 已撤销（付款码支付）
                $data['inquiry_pay_status'] = 7;
            }elseif($message['trade_state'] == "USERPAYING"){
                // 用户支付中（付款码支付）
                $data['inquiry_pay_status'] = 3;
            }elseif($message['trade_state'] == "PAYERROR"){
                // 支付失败(其他原因，如银行返回失败)
                $data['inquiry_pay_status'] = 4;
            }

            $data['escrow_trade_no'] = $message['transaction_id'];
            $data['updated_at'] = date('Y-m-d H:i:s',time());

            $params = array();
            $params['order_inquiry_id'] = $order_inquiry['order_inquiry_id'];
            OrderInquiry::edit($params,$data);
        }catch (\Exception $e) {
            // 验证失败
            Log::getInstance()->error("问诊微信支付回调数据处理失败:" . $e->getMessage());
            return $this->wxPayErrorReturn($e->getMessage());
        }

        Log::getInstance()->info("问诊微信支付回调数据处理成功");
        Log::getInstance()->info("问诊微信支付回调数据处理成功，开始分配医生/发送问诊消息");

        try {
            if ($message['trade_state'] == "SUCCESS"){
                if ($order_inquiry['inquiry_type'] == 2 || $order_inquiry['inquiry_type'] == 4){
                    Log::getInstance()->info("加入分配医生队列");

                    // 快速-购药
                    // 加入分配医生队列
                    $data = array();
                    $data['order_inquiry_id'] = $order_inquiry['order_inquiry_id'];

                    $message = new AssignDoctorProducer($data);
                    $producer = ApplicationContext::getContainer()->get(Producer::class);
                    $result = $producer->produce($message);
                    if (!$result) {
                        Log::getInstance()->info("加入分配医生队列失败");
                        return $server->serve();
                    }

                    Log::getInstance()->info("加入分配医生队列成功");
                }elseif ($order_inquiry['inquiry_type'] == 1 || $order_inquiry['inquiry_type'] == 3){
                    // 专家-公益，发送im消息
                    Log::getInstance()->info("开始发送im消息");

                    $ImService = new ImService();

                    // 获取订单医生数据
                    $params = array();
                    $params['doctor_id'] = $order_inquiry['doctor_id'];
                    $user_doctor = UserDoctor::getOne($params);
                    if (empty($user_doctor)){
                        Log::getInstance()->info("医生数据错误");
                        return $server->serve();
                    }

                    // 自定义消息
                    $cloud_custom_data = array();
                    $cloud_custom_data['order_inquiry_id'] = $order_inquiry['order_inquiry_id'];
                    $cloud_custom_data['is_system'] = 1;
                    $cloud_custom_data['inquiry_type'] = $order_inquiry['inquiry_type'];
                    $cloud_custom_data['message_rounds'] = 0;
                    $cloud_custom_data['patient_family_data']['patient_name'] = $order_inquiry['patient_name'];
                    $cloud_custom_data['patient_family_data']['patient_sex'] = $order_inquiry['patient_sex'];
                    $cloud_custom_data['patient_family_data']['patient_age'] = $order_inquiry['patient_age'];

                    // 消息内容
                    $message_content_data = array();
                    $message_content_data['message_type'] = 1;
                    $message_content_data['title'] = "--等待医生接诊--";

                    if ($order_inquiry['inquiry_type'] == 1){
                        $message_content_data['desc'] = "温馨提示：当前服务为专家问诊，可以在24小时内和医生沟通20个回合。医生均为一线工作，还请耐心等待，医生接诊会第一时间短信通知您。";
                    }else{
                        $message_content_data['desc'] = "温馨提示：当前服务为公益问诊，可以在24小时内和医生沟通10个回合。医生均为一线工作，还请耐心等待，医生接诊会第一时间短信通知您。";
                    }

                    $message_content = [
                        'Data' => json_encode($message_content_data,JSON_UNESCAPED_UNICODE),
                    ];

                    $ImService->sendMessage($user_doctor['user_id'], $order_inquiry['user_id'], $message_content, "TIMCustomElem", $cloud_custom_data);



                    Log::getInstance()->info("发送im消息成功");
                }
            }
        }catch (\Exception $e) {
            // 验证失败
            Log::getInstance()->error("问诊微信支付回调数据处理成功，分配医生/发送问诊消息失败:" . $e->getMessage());
            return $server->serve();
        }

        Log::getInstance()->info("问诊微信支付回调处理成功");

        return $server->serve();
    }

    /**
     * 微信问诊退款回调
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws \Throwable
     */
    public function wxPayInquiryRefundCallBack(): ResponseInterface
    {
        Db::beginTransaction();

        try {
            // 处理支付结果事件
            $WechatPay = new WechatPay(1,1);
            $app = $WechatPay->createApp();
            $server = $app->getServer();

            $message = $server->getRequestMessage();
            if (empty($message)){
                Db::rollBack();
                return $this->response->withStatus(500)->withBody(new SwooleStream(strval(json_encode(['code' => 'ERROR', 'message' => "回调数据为空"], JSON_UNESCAPED_UNICODE))));
            }

            // 验证推送消息签名
            $app->getValidator()->validate($app->getRequest());

            Log::getInstance()->info("微信退款回调数据：" . json_encode($message->toArray(),JSON_UNESCAPED_UNICODE));

            if (empty($message['out_trade_no'])){
                Log::getInstance()->info("微信退款回调数据错误");
                return $server->serve();
            }

            // 验证订单数据
            $params = array();
            $params['inquiry_no'] = $message['out_trade_no'];
            $order_inquiry = OrderInquiry::getOne($params);
            if (empty($order_inquiry)){
                Log::getInstance()->info("非法订单");
                return $server->serve();
            }

            // 验证订单状态
            if ($order_inquiry['inquiry_status'] == 1){
                // 问诊订单状态（1:待支付 2:待分配 3:待接诊 4:已接诊 5:已完成 6:已结束 7:已取消）
                Log::getInstance()->info("订单状态错误：当前为" . $order_inquiry['inquiry_status']);
                return $server->serve();
            }


            // 验证订单退款状态
            if ($order_inquiry['inquiry_refund_status'] == 3) {
                // 问诊订单退款状态（0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭）
                Log::getInstance()->info("订单退款状态错误：当前为" . $order_inquiry['inquiry_refund_status']);
                return $server->serve();
            }

            // 验证订单支付状态
            if (in_array($order_inquiry['inquiry_pay_status'],[1,3,4,5,6,7])) {
                // 支付状态（1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款）
                Log::getInstance()->error("队列执行失败原因：订单未支付");
                return $server->serve();
            }

            // 退款状态
            if ($message['refund_status'] == "SUCCESS"){
                // 退款成功
                $inquiry_refund_status = 3;
            }elseif ($message['refund_status'] == "CLOSED"){
                // 退款关闭
                $inquiry_refund_status = 5;
            }elseif ($message['refund_status'] == "ABNORMAL"){
                // 退款异常
                $inquiry_refund_status = 6;
            }

            if (empty($inquiry_refund_status)){
                // 错误，无退款状态
                Log::getInstance()->error("队列执行失败原因：订单未支付");
                return $this->wxPayErrorReturn("退款状态错误");
            }

            // 修改订单
            $data = array();
            $data['inquiry_refund_status'] = $inquiry_refund_status;

            $params = array();
            $params['order_inquiry_id'] = $order_inquiry['order_inquiry_id'];
            OrderInquiry::edit($params,$data);

            // 修改退款订单
            $data = array();
            $data['inquiry_refund_status'] = $inquiry_refund_status;
            $data['success_time'] = $message['success_time'];

            $params = array();
            $params['order_inquiry_id'] = $order_inquiry['order_inquiry_id'];
            OrderInquiryRefund::edit($params,$data);

            Db::commit();
        } catch (\Exception $e) {
            // 验证失败
            Db::rollBack();
            Log::getInstance()->error("微信支付回调数据验证失败:" . $e->getMessage());
            return $this->wxPayErrorReturn($e->getMessage());
        }

        Log::getInstance()->info("微信退款回调处理成功");

        // 发送短信消息

        return $server->serve();
    }

    /**
     * 患者端药品微信支付回调
     * @return ResponseInterface
     * @throws \Throwable
     */
    public function wxPayProductSuccessCallBack(): ResponseInterface
    {
        try {
            // 处理支付结果事件
            $WechatPay = new WechatPay(1,2);
            $app = $WechatPay->createApp();
            $server = $app->getServer();

            $message = $server->getRequestMessage();

            if (empty($message)){
                return $this->response->withStatus(500)->withBody(new SwooleStream(strval(json_encode(['code' => 'ERROR', 'message' => "药品微信支付回调数据为空"], JSON_UNESCAPED_UNICODE))));
            }

            // 验证推送消息签名
            $app->getValidator()->validate($app->getRequest());

            Log::getInstance()->info("药品微信支付回调数据：" . json_encode($message->toArray(),JSON_UNESCAPED_UNICODE));

            if (empty($message['out_trade_no'])){
                Log::getInstance()->info("药品微信支付回调数据处理失败，缺少外部订单号");
                return $server->serve();
            }



        }catch (\Exception $e) {
            // 验证失败
            Log::getInstance()->error("药品微信支付回调数据处理失败:" . $e->getMessage());
            return $this->wxPayErrorReturn($e->getMessage());
        }

        Log::getInstance()->info("药品微信支付回调数据验证成功，执行数据库操作");

        Db::beginTransaction();

        try {
            // 查询药品订单
            $params = array();
            $params['order_product_no'] = $message['out_trade_no'];
            $order_product = OrderProduct::getOne($params);
            if (empty($order_product)){
                Db::rollBack();
                Log::getInstance()->info("药品微信支付回调数据处理失败，无订单数据");
                return $server->serve();
            }

            // 验证订单状态
            if ($order_product['order_product_status'] != 1){
                Db::rollBack();
                // 订单状态（1:待支付 2:待发货 3:已发货 4:已签收 5:已取消）
                Log::getInstance()->info("药品微信支付回调数据处理失败，订单状态当前为" . $order_product['order_product_status']);
                return $server->serve();
            }

            // 支付状态无需验证，如第一次支付失败，会修改支付状态，再次支付时，会出现验证不通过的情况

            // 修改支付状态
            $data = array();
            if ($message['trade_state'] == "SUCCESS"){
                // 支付成功
                $data['order_product_status'] = 2;
                $data['pay_status'] = 2;// 支付状态（1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款）

            }elseif($message['trade_state'] == "CLOSED"){
                // 已关闭
                $data['pay_status'] = 6;
            }elseif($message['trade_state'] == "REVOKED"){
                // 已撤销（付款码支付）
                $data['pay_status'] = 7;
            }elseif($message['trade_state'] == "USERPAYING"){
                // 用户支付中（付款码支付）
                $data['pay_status'] = 3;
            }elseif($message['trade_state'] == "PAYERROR"){
                // 支付失败(其他原因，如银行返回失败)
                $data['pay_status'] = 4;
            }

            $data['escrow_trade_no'] = $message['transaction_id'];
            $data['updated_at'] = date('Y-m-d H:i:s',time());

            $params = array();
            $params['order_product_id'] = $order_product['order_product_id'];
            OrderProduct::edit($params,$data);

            // 获取订单商品订单列表
            $params = array();
            $params['order_product_id'] = $order_product['order_product_id'];
            $order_product_item = OrderProductItem::getList($params);
            if (empty($order_product_item)){
                Db::rollBack();
                Log::getInstance()->info("药品微信支付回调数据处理失败，未查询到对应订单商品订单列表");
                return $server->serve();
            }

            Db::commit();
        } catch (\Exception $e) {
            Db::rollBack();
            Log::getInstance()->error("药品微信支付回调数据处理失败:" . $e->getMessage());
            return $this->wxPayErrorReturn($e->getMessage());
        }

        Log::getInstance()->info("药品微信支付回调数据验证成功");

        return $server->serve();
    }

    /**
     * 微信药品退款回调
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws \Throwable
     */
    public function wxPayProductRefundCallBack(): ResponseInterface
    {
        Db::beginTransaction();

        try {
            // 处理支付结果事件
            $WechatPay = new WechatPay(1,1);
            $app = $WechatPay->createApp();
            $server = $app->getServer();

            $message = $server->getRequestMessage();
            if (empty($message)){
                Db::rollBack();
                return $this->response->withStatus(500)->withBody(new SwooleStream(strval(json_encode(['code' => 'ERROR', 'message' => "回调数据为空"], JSON_UNESCAPED_UNICODE))));
            }

            // 验证推送消息签名
            $app->getValidator()->validate($app->getRequest());

            Log::getInstance()->info("微信退款回调数据：" . json_encode($message->toArray(),JSON_UNESCAPED_UNICODE));

            if (empty($message['out_trade_no'])){
                Log::getInstance()->info("药品微信退款回调数据处理失败，缺少外部订单号");
                return $server->serve();
            }

            // 查询药品订单
            $params = array();
            $params['order_product_no'] = $message['out_trade_no'];
            $order_product = OrderProduct::getOne($params);
            if (empty($order_product)){
                Db::rollBack();
                Log::getInstance()->info("药品微信退款回调数据处理失败，无订单数据");
                return $server->serve();
            }

            // 验证订单状态
            if ($order_product['order_product_status'] == 1){
                // 订单状态（1:待支付 2:待发货 3:已发货 4:已签收 5:已取消）
                Log::getInstance()->info("药品微信退款回调数据处理失败,订单状态错误：当前为" . $order_product['order_product_status']);
                return $server->serve();
            }

            // 验证订单退款状态
            if ($order_product['refund_status'] == 3) {
                // 商品订单退款状态（0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭）
                Log::getInstance()->info("药品微信退款回调数据处理失败，订单退款状态为" . $order_product['refund_status']);
                return $server->serve();
            }

            // 验证订单支付状态
            if (in_array($order_product['pay_status'],[1,3,4,5,6,7])) {
                // 支付状态（1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款）
                Log::getInstance()->error("药品微信退款回调数据处理失败：订单未支付");
                return $server->serve();
            }

            // 退款状态
            if ($message['refund_status'] == "SUCCESS"){
                // 退款成功
                $refund_status = 3;
            }elseif ($message['refund_status'] == "CLOSED"){
                // 退款关闭
                $refund_status = 5;
            }elseif ($message['refund_status'] == "ABNORMAL"){
                // 退款异常
                $refund_status = 6;
            }

            if (empty($refund_status)){
                // 错误，无退款状态
                Log::getInstance()->error("药品微信退款回调数据处理失败：订单未支付");
                return $this->wxPayErrorReturn("退款状态错误");
            }

            // 修改订单
            $data = array();
            $data['refund_status'] = $refund_status;

            $params = array();
            $params['order_product_id'] = $order_product['order_product_id'];
            OrderProduct::edit($params,$data);

            // 修改退款订单
            $data = array();
            $data['product_refund_status'] = $refund_status;
            $data['success_time'] = $message['success_time'];

            $params = array();
            $params['order_product_id'] = $order_product['order_product_id'];
            OrderProductRefund::edit($params,$data);

            Db::commit();
        } catch (\Exception $e) {
            // 验证失败
            Db::rollBack();
            Log::getInstance()->error("药品微信退款回调数据处理失败:" . $e->getMessage());
            return $this->wxPayErrorReturn($e->getMessage());
        }

        Log::getInstance()->info("药品微信退款回调数据处理成功");

        // 发送短信消息

        // 告知处方平台
        return $server->serve();
    }

    /**
     * 微信支付返回错误响应
     * @param string $message
     * @return ResponseInterface
     */
    protected function wxPayErrorReturn(string $message): ResponseInterface
    {
        return $this->response
            ->withStatus(500)
            ->withBody(
                new SwooleStream(
                    strval(
                        json_encode(['code' => 'ERROR', 'message' => $message], JSON_UNESCAPED_UNICODE)
                    )
                )
            );
    }

    /**
     * im回调
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function imCallBack(): ResponseInterface
    {
        $request_params = $this->request->all();
        try {
            Log::getInstance()->info("Im回调数据：" . json_encode($request_params,JSON_UNESCAPED_UNICODE));

            if (empty($request_params['RequestTime']) || empty($request_params['Sign'])){
                Log::getInstance()->error("Im回调数据处理失败:缺少时间时间戳/签名字段");
                return $this->ImErrorReturn("缺少时间时间戳/签名字段");
            }

            // 鉴定回调签名
            $imService = new ImService();
            $result = $imService->validateSign($request_params['RequestTime'],$request_params['Sign']);
            if (!$result){
                Log::getInstance()->error("Im回调数据处理失败:回调签名不匹配");
                return $this->ImErrorReturn("回调签名不匹配");
            }

            // 验证消息内容
            if (empty($request_params['MsgBody'])){
                Log::getInstance()->error("Im回调数据处理失败:消息内容错误");
                return $this->ImErrorReturn("消息内容错误，缺少MsgBody");
            }

            // 验证消息内容类型
            if (empty($request_params['MsgBody'][0]['MsgType'])){
                Log::getInstance()->error("Im回调数据处理失败:缺少MsgType");
                return $this->ImErrorReturn("消息内容错误，缺少MsgType");
            }

            // 验证消息内容详情
            if (empty($request_params['MsgBody'][0]['MsgContent'])){
                Log::getInstance()->error("Im回调数据处理失败:缺少MsgContent");
                return $this->ImErrorReturn("消息内容错误，缺少MsgContent");
            }

            // 验证接收方user_id
            if (empty($request_params['To_Account'])){
                Log::getInstance()->error("Im回调数据处理失败:接收用户错误");
                return $this->ImErrorReturn("消息内容错误，接收用户错误");
            }

            // 验证消息唯一id
            if (empty($request_params['MsgKey'])){
                Log::getInstance()->error("Im回调数据处理失败:消息唯一标识错误");
                return $this->ImErrorReturn("消息内容错误，消息唯一标识错误");
            }

            // 验证消息重复性
            $params = array();
            $params['message_key'] = $request_params['MsgKey'];
            $message = MessageIm::getExists($params);
            if ($message){
                // 消息重复
                Log::getInstance()->info("Im回调数据处理失败:消息重复");
                return $this->ImSuccessReturn();
            }

            // 处理发送结果
            if ($request_params['SendMsgResult'] == 0){
                // im中0表示成功
                $message_send_result = 1;
            }

            // 验证自定义消息内容
            $is_system = 0;// 是否系统操作发送（0:否 1:是）
            if (!empty($request_params['CloudCustomData'])){
                $cloud_custom_data = json_decode($request_params['CloudCustomData'],true);

                if (!empty($cloud_custom_data['order_inquiry_id'])){
                    // 获取订单数据
                    $params = array();
                    $params['order_inquiry_id'] = $cloud_custom_data['order_inquiry_id'];
                    $order_inquiry = OrderInquiry::getOne($params);
                    if (empty($order_inquiry)){
                        Log::getInstance()->error("Im回调数据处理失败:非法订单");
                        return $this->ImErrorReturn("消息内容错误，非法订单");
                    }

                    $order_inquiry_id = $cloud_custom_data['order_inquiry_id'];
                }

                if (!empty($cloud_custom_data['is_system'])){
                    if ($cloud_custom_data['is_system'] == 1){
                        // 系统发送
                        $is_system = 1;
                    }
                }
            }

            // 入库
            $data = array();
            if (!empty($request_params['From_Account'])){
                // 系统发送时不带参数
                $data['from_user_id'] = $request_params['From_Account'];
            }
            $data['to_user_id'] = $request_params['To_Account'];
            $data['message_key'] = $request_params['MsgKey'];
            $data['message_send_time'] = $request_params['RequestTime'];
            $data['message_seq'] = $request_params['MsgSeq'];
            $data['message_send_result'] = $message_send_result ?? 0;
            $data['send_error_info'] = $request_params['ErrorInfo'];
            $data['message_type'] = $request_params['MsgBody'][0]['MsgType'];

            $data['is_system'] = $is_system;
            if (!empty($order_inquiry_id)){
                $data['order_inquiry_id'] = $order_inquiry_id;
            }

            $message_content = $request_params['MsgBody'][0]['MsgContent'];
            $data['message_content'] = json_encode($message_content,JSON_UNESCAPED_UNICODE);
            $data['message_custom_content'] = $request_params['CloudCustomData'] ?? "";
            $message = MessageIm::addMessage($data);
            if (empty($message)){
                Log::getInstance()->error("Im回调数据处理失败:存储数据库失败");
                return $this->wxPayErrorReturn("存储数据库失败");
            }
        } catch (\Exception $e) {
            // 验证失败
            Log::getInstance()->error("Im回调数据处理失败:" . $e->getMessage());
            return $this->wxPayErrorReturn($e->getMessage());
        }

        Log::getInstance()->info("Im回调数据处理成功");
        return $this->ImSuccessReturn();
    }

    /**
     * im返回错误响应
     * @param string $message
     * @return ResponseInterface
     */
    protected function ImErrorReturn(string $message): ResponseInterface
    {
        return $this->response
            ->withStatus(200)
            ->withBody(
                new SwooleStream(
                    strval(
                        json_encode([
                            'ActionStatus' => 'FAIL',
                            'ErrorCode' => 1,
                            'ErrorInfo' => $message,
                        ], JSON_UNESCAPED_UNICODE)
                    )
                )
            );
    }

    /**
     * im返回正确响应
     * @return ResponseInterface
     */
    protected function ImSuccessReturn(): ResponseInterface
    {
        return $this->response
            ->withStatus(200)
            ->withBody(
                new SwooleStream(
                    strval(
                        json_encode([
                            'ActionStatus' => 'OK',
                            'ErrorCode' => 0,
                            'ErrorInfo' => "",
                        ], JSON_UNESCAPED_UNICODE)
                    )
                )
            );
    }

    // 处方平台物流回调
    public function platformLogisticsCallBack(): ResponseInterface
    {
        $request_params = $this->request->all();
        try {
            Log::getInstance()->info("处方平台物流回调数据：" . json_encode($request_params,JSON_UNESCAPED_UNICODE));

            if (!isset($request_params['sign']) ){
                Log::getInstance()->error("处方平台物流回调数据处理失败:缺少签名结果");
                return $this->platformLogisticsErrorReturn("缺少签名结果");
            }

            if (!isset($request_params['nonce']) ){
                Log::getInstance()->error("处方平台物流回调数据处理失败:缺少随机数");
                return $this->platformLogisticsErrorReturn("缺少随机数");
            }

            if (!isset($request_params['timestamp']) ){
                Log::getInstance()->error("处方平台物流回调数据处理失败:缺少签名时间戳");
                return $this->platformLogisticsErrorReturn("缺少签名时间戳");
            }

            if (!isset($request_params['paramJsonStr']) ){
                Log::getInstance()->error("处方平台物流回调数据处理失败:缺少数据体");
                return $this->platformLogisticsErrorReturn("缺少数据体");
            }

            // 验证签名
            $sign_params = array();
            $sign_params['clientId'] = config('prescription_platform.client_id');
            $sign_params['paramJsonStr'] = $request_params['paramJsonStr'];
            $sign_params['timestamp'] = $request_params['timestamp'];
            $sign_params['nonce'] = $request_params['nonce'];
            $sign_params['clientSecret'] = config('prescription_platform.client_secret');
            $sign = md5(http_build_query($sign_params, '', '&'));
            if ($sign != $sign_params['sign']){
                Log::getInstance()->error("处方平台物流回调数据处理失败:签名错误");
                Log::getInstance()->error("系统签名:" . $sign);
                Log::getInstance()->error("处方平台签名:" . $sign);
                Log::getInstance()->error("处方平台签名:" . $sign_params['sign']);
                return $this->platformLogisticsErrorReturn("签名错误");
            }

            $param_json_str = json_decode($request_params['paramJsonStr'],true);
            if (empty($param_json_str)){
                // 数据体为空，直接返回成功，不进行验证签名
                Log::getInstance()->info("处方平台物流回调数据处理成功:数据体解析为空");
                return $this->platformLogisticsSuccessReturn();
            }


        } catch (\Exception $e) {
            // 验证失败
            Log::getInstance()->error("Im回调数据处理失败:" . $e->getMessage());
            return $this->platformLogisticsErrorReturn($e->getMessage());
        }

        return $this->platformLogisticsSuccessReturn();
    }

    /**
     * 处方平台物流返回错误响应
     * @param string $message
     * @return ResponseInterface
     */
    protected function platformLogisticsErrorReturn(string $message): ResponseInterface
    {
        return $this->response
            ->withStatus(200)
            ->withBody(
                new SwooleStream(
                    strval(
                        json_encode([
                            'resultCode' => '0',
                            'resultDesc' => $message,
                        ], JSON_UNESCAPED_UNICODE)
                    )
                )
            );
    }

    /**
     * 处方平台物流返回正确响应
     * @return ResponseInterface
     */
    protected function platformLogisticsSuccessReturn(): ResponseInterface
    {
        return $this->response
            ->withStatus(200)
            ->withBody(
                new SwooleStream(
                    strval(
                        json_encode([
                            'resultCode' => "1000",
                            'resultDesc' => "成功",
                        ], JSON_UNESCAPED_UNICODE)
                    )
                )
            );
    }

}