<?php

namespace App\Http\Controllers\API\Third;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Log;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Services\Xml\ShenQingDanService;

class EntrustController extends Controller
{
    //创建医嘱记录进入 s_list表
    public function CreateRecord()
    {
        $Info = request('info');
        $params = [
            'list_status' => 0,
            'reg_num' => $Info['reg_num'] ?? null,
            'user_name' => $Info['user_name'] ?? null,
            'user_sex' => $Info['user_sex'] ?? null,
            'entrust' => $Info['entrust'] ?? null,
            'is_pay' => $Info['is_pay'] ?? null,
            'reservation_department' => $Info['reservation_department'] ?? null,
            'entrust_date' => $Info['entrust_date'] ?? null,
            'entrust_time' => $Info['entrust_time'] ?? null,
            'user_brithday' => $Info['user_brithday'] ?? null,
            'docotr' => $Info['docotr'] ?? null,
            'patient_type' => $Info['patient_type'] ?? null,
            'user_phone' => $Info['user_phone'] ?? null,
            'implement_department' => $Info['implement_department'] ?? null,
            'entrust_id' => $Info['entrust_id'] ?? null,//his传过来的本地医嘱id
            'episodeid' => $Info['episodeid'] ?? null,  //就诊号
            'RISRExamID' => $Info['RISRExamID'] ?? null,  //检查号
            'RISRAcceptDeptCode' => $Info['RISRAcceptDeptCode'] ?? null,  //接收科室代码
        ];
        $requiredFields = [
            'reg_num', 'user_name', 'user_sex', 'entrust', 'is_pay',
            'reservation_department', 'entrust_date', 'entrust_time',
            'user_brithday', 'docotr', 'patient_type', 'user_phone',
            'implement_department', 'entrust_id', 'episodeid',
            'RISRExamID', 'RISRAcceptDeptCode'
        ];

        // 判断是否为空
        foreach ($requiredFields as $field) {
            if (!isset($params[$field]) || $params[$field] === null) {
                return \Yz::echoError1('参数' . $field . '不能为空');
            }
        }
        DB::beginTransaction();
        $insertListId = DB::table('s_list')->insertGetId($params);
        if ($insertListId) {
            $data = [
                'list_id' => $insertListId,
                'reg_num' => $Info['reg_num'],
                'new_status' => 0,
                'create_user' => '接口',
                'note' => '创建记录',
                'data' => json_encode($Info, JSON_UNESCAPED_UNICODE),//原始数据
            ];
            $insertLog = DB::table('s_list_log')->insert($data);
            if ($insertLog) {
                DB::commit();
                return \Yz::Return(true, '保存成功', []);
            } else {
                DB::rollBack();
                return \Yz::echoError1('保存失败');
            }
        } else {
            DB::rollBack();
            return \Yz::echoError1('保存失败');
        }

    }

    public function CreateRecordXml(Request $request)
    {
        $xmlString = $request->getContent(); // 获取请求的原始内容（XML）
        //   try {
        $res = explode('<soap:Body>', $xmlString)[1];
        $res = explode('</soap:Body>', $res)[0];
        $xmlData = simplexml_load_string($res);
        $jsonData = json_encode($xmlData, JSON_UNESCAPED_UNICODE);
        $result = json_decode($jsonData, true);
        //如果是推送的检查申请单
        if ($result['action'] == 'T0002') {
            $service = new ShenQingDanService();
            return $service->Save($result, $jsonData);
        }
        //如果是推送的检查申请单状态变更
        if ($result['action'] == 'T0003') {
            $service = new ShenQingDanService();
            return $service->UpdateStatus($result, $jsonData);
        }
        //如果是 推送检查完成
        if ($result['action'] == 'MES0006') {
            $service = new ShenQingDanService();
            return $service->CheckFinish($result, $jsonData);
        }
        //   }catch (\Exception $e){
        //       return \Yz::XMLReturn('SYS001', '269624', -1, '处理失败，接收数据不符合预期结构 '.$e->getMessage());
        //   }


    }


}
