<?php

namespace app\event;

use app\model\ClientMessage;
use app\model\Order;
use app\model\OrderReturn;
use app\model\OrderSheet;
use support\Log;

class ClientMessageEvent
{
    /**
     * @Desc 发货通知
     * @Author Gorden
     * @Date 2024/7/16 10:28
     *
     * @param $params
     * @return void
     */
    public function delivery($params)
    {
        Log::info("发送消息", $params);
        try {
            $order = Order::where('order_id', $params['order_id'])
                ->select('join_order_member_id')
                ->first();
            $clientMessage = new ClientMessage();
            $clientMessage->client_message_classify = 'ORDER';
            $clientMessage->join_client_message_send = 'SYSTEM';
            $clientMessage->join_client_message_recv_member_id = $order->join_order_member_id;
            $clientMessage->client_message_sendtime = date('Y-m-d H:i:s');
            $clientMessage->client_message_status = 'PENDING';
            $clientMessage->client_message_category = 'NORMAL';
            $clientMessage->client_message_header_json = json_encode(['title' => '订单消息']);
            $clientMessage->client_message_addtimes = time();

            $sheet = OrderSheet::with([
                'goods' => function ($query) {
                    $query->select('goods_id', 'goods_name');
                }
            ])->where('join_sheet_order_id', $params['order_id'])
                ->select('order_sheet_id', 'join_sheet_goods_id', 'join_sheet_order_id', 'order_sheet_num')
                ->first();


            if ($params['express_type'] == '自提') {
                $clientMessage->client_message_body_json = json_encode([
                    'content' => '提货通知：您于' . date('Y年m月d日') . '在' . $params['order_express_premises'] . '提取了您购买的商品【' . $sheet->goods->goods_name . '】,数量：【' . intval($sheet->order_sheet_num) . '】。'
                ]);
            } else {
                $clientMessage->client_message_body_json = json_encode([
                    'content' => '发货通知：您购买的商品【' . $sheet->goods->goods_name . '】,数量：【' . intval($sheet->order_sheet_num) . '】已发货，物流公司：' . $params['order_express_company'] . '，物流单号：' . $params['order_express_code'] . '，请注意查收。'
                ]);
            }

            $clientMessage->save();
        } catch (\Exception $e) {
            Log::error("消息发送失败", ['msg' => $e->getMessage()]);
        }
    }

    /**
     * @Desc 退货/退款状态变更站内信
     * @Author Gorden
     * @Date 2024/7/16 14:19
     *
     * @param $params
     * @return void
     */
    public function returnStatus($params)
    {
        try {
            $orderReturn = OrderReturn::where('orders_return_id', $params['orders_return_id'])->first();
            if (!$orderReturn) {
                return;
            }

            if ($params['order_return_status'] == 'DOING') {
                $content = "商家已同意您的退货/退款申请。";
            } else {
                $content = "商家已拒绝您的退货/退款申请。";
            }

            $clientMessage = new ClientMessage();
            $clientMessage->client_message_classify = 'ORDER';
            $clientMessage->join_client_message_send = 'SYSTEM';
            $clientMessage->join_client_message_recv_member_id = $orderReturn->join_return_member_id;
            $clientMessage->client_message_sendtime = date('Y-m-d H:i:s');
            $clientMessage->client_message_status = 'PENDING';
            $clientMessage->client_message_category = 'NORMAL';
            $clientMessage->client_message_header_json = json_encode(['title' => '订单消息']);
            $clientMessage->client_message_body_json = json_encode(['content' => '退货/退款通知：' . $content]);
            $clientMessage->client_message_addtimes = time();
            $clientMessage->save();

        } catch (\Exception $e) {
        }
    }


    /**
     * @Desc 退款成功通知
     * @Author Gorden
     * @Date 2024/7/16 14:09
     *
     * @param $memberId
     * @return void
     */
    public function refund($memberId)
    {
        try {
            $clientMessage = new ClientMessage();
            $clientMessage->client_message_classify = 'ORDER';
            $clientMessage->join_client_message_send = 'SYSTEM';
            $clientMessage->join_client_message_recv_member_id = $memberId;
            $clientMessage->client_message_sendtime = date('Y-m-d H:i:s');
            $clientMessage->client_message_status = 'PENDING';
            $clientMessage->client_message_category = 'NORMAL';
            $clientMessage->client_message_header_json = json_encode(['title' => '订单消息']);
            $clientMessage->client_message_body_json = json_encode(['content' => '退款通知：您申请的退款已原路返回，请注意查收。']);
            $clientMessage->client_message_addtimes = time();
            $clientMessage->save();

        } catch (\Exception $e) {
        }
    }
}