<?php

namespace app\event\order;

use app\admin\service\order\OrderProcessService;
use app\model\OrderProcess;
use support\Log;
use Tinywan\Jwt\JwtToken;

class OrderProcessEvent
{
    public function orderComplete($params)
    {
        try {
            $userId = JwtToken::getCurrentId();
        } catch (\Exception $e) {
        }

        try {
            $data = [
                'process_key' => 'DONE',
                'process_name' => '订单完成',
                'user_id' => $userId ?? 'SYSTEM',
                'process_json' => $params
            ];
            if (!empty($params['join_order_member_id'])) {
                $data['member_id'] = $params['join_order_member_id'];
            }
            if (!empty($params['orderId'])) {
                $data['order_id'] = $params['orderId'];
            }
            if (!empty($params['submit_premises_id'])) {
                $data['dept_id'] = $params['submit_premises_id'];
            }
            OrderProcessService::insert($data);
        } catch (\Exception $e) {
            dump($e->getMessage());
            $params['msg'] = $e->getMessage();
            Log::error("WRITE_ORDER_PROCESS_ERROR", $params);
        }
    }

    public function orderWriteOff($params)
    {
        try {
            $userId = JwtToken::getCurrentId();
        } catch (\Exception $e) {
        }

        try {
            $data = [
                'process_key' => 'PAYOFF',
                'process_name' => '订单核销',
                'user_id' => $userId ?? '',
                'process_json' => $params['write_off_data']
            ];
            if (!empty($params['join_order_member_id'])) {
                $data['member_id'] = $params['join_order_member_id'];
            }
            if (!empty($params['order_id'])) {
                $data['order_id'] = $params['order_id'];
            }
            if (!empty($params['dept'])) {
                $data['dept_id'] = $params['dept']->dept_id;
            }
            OrderProcessService::insert($data);
        } catch (\Exception $e) {
            dump($e->getMessage());
            $params['msg'] = $e->getMessage();
            Log::error("WRITE_ORDER_PROCESS_ERROR", $params);
        }
    }
}