<?php

namespace App\Http\Controllers\API\Admin\Yewu;

use App\Http\Controllers\API\PEISApiController;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ReportController extends Controller
{

  //获取思信那边的报告信息，推送或者拉取
  public function ReportInfo()
  {
    $type = "la";  //la拉取 //tui 推送
    $hospital_id = request('hospital_id');
    $id_number = request('id_number');
    $reports_detail_list = [];
    $peis = new PEISApiController();
    if ($type == 'la') {
      $person = DB::table('web_user_person')->where(['id_number' => $id_number, 'is_del' => 0])->first();
      if (!$person) return \Yz::echoError1('体检人不存在');
      $data = [
        '电话号码' => "",
        '证件号码' => $id_number,
        '体检号' => ""
      ];

      $reports = $peis::Post('体检报告查询', $hospital_id, $data);
      if (count($reports['data']) > 0) {
        $reports_detail_list = $reports['data'];
      }
    }
    //获取到报告列表后
    foreach ($reports_detail_list as $key => $reports_detail) {
      $data = [
        '电话号码' => "",
        '证件号码' => "",
        '体检号' => $reports_detail['体检号']
      ];
      $res = $peis::Post('体检报告查询', $hospital_id, $data);
      $res = $res['data'][0];
      //根据体检号查询，获取id，万一不小心有多个相同的体检号 所以返回数组，正常就1个
      $report_l1_records_ids = DB::table('report_l1_records')->where(['体检号' => $res['体检号']])->pluck('id')->toArray();
      //查找对应的收费项目列表 获取id数组
      $report_l2_items_ids = DB::table('report_l2_items')->whereIn('reprort_l1_id', $report_l1_records_ids)->pluck('id')->toArray();
      //查找 基础项目列表 删除
      $report_l3_base_items_del = DB::table('report_l3_base_items')->whereIn('report_l2_id', $report_l2_items_ids)->delete();
      //删除report_l2_items表数据
      DB::table('report_l2_items')->whereIn('id', $report_l2_items_ids)->delete();
      //删除report_l1_records表数据
      DB::table('report_l1_records')->whereIn('id', $report_l1_records_ids)->delete();
      //删除完后，进行添加
      $r_data = [
        "体检类型" => isset($res['体检类型']) ? $res['体检类型'] : null,
        "查询密码" => isset($res['查询密码']) ? $res['查询密码'] : null,
        "审核时间" => isset($res['审核时间']) ? $res['审核时间'] : null,
        "总检医生" => isset($res['总检医生']) ? $res['总检医生'] : null,
        "总检结论" => isset($res['总检结论']) ? $res['总检结论'] : null,
        "总检建议" => isset($res['总检建议']) ? $res['总检建议'] : null,
        "病种列表" => isset($res['病种列表']) ? json_encode($res['病种列表'], JSON_UNESCAPED_UNICODE) : null,
        "报告文件列表" => isset($res['报告文件列表']) ? json_encode($res['报告文件列表'], JSON_UNESCAPED_UNICODE) : null,

        "卡号" => $res['卡号'],
        "证件类型" => $res['证件类型'],
        "证件号码" => $res['证件号码'],
        "地址" => $res['地址'],
        "电话号码" => $res['电话号码'],
        "婚姻状态" => $res['婚姻状态'],
        "单位名称" => $res['单位名称'],
        "批次Id" => $res['批次Id'],
        "批次名称" => $res['批次名称'],
        "部门名称" => $res['部门名称'],
        "分组名称" => $res['分组名称'],
        "体检号" => $res['体检号'],
        "姓名" => $res['姓名'],
        "性别" => $res['性别'],
        "出生日期" => $res['出生日期'],
        "登记时间" => $res['登记时间'],
        "检前签到时间" => $res['检前签到时间'],
        "体检状态" => $res['体检状态'],
        "报告日期" => $res['报告日期'],
        "套餐名称" => $res['套餐名称'],
        "团检" => $res['团检'],
        "支持下载报告文件" => $res['支持下载报告文件'],
      ];
      $l1_id = DB::table('report_l1_records')->insertGetId($r_data);
      if ($res['体检状态'] == '报告已出') {
        foreach ($res['收费项目列表'] as $key => $l2_item) {
          $r2_data = [
            "reprort_l1_id" => $l1_id,
            "科室类型" => $l2_item['科室类型'],
            "科室名称" => $l2_item['科室名称'],
            "科室顺序" => $l2_item['科室顺序'],
            "收费项目名称" => $l2_item['收费项目名称'],
            "收费项目顺序" => $l2_item['收费项目顺序'],
            "检查医生" => $l2_item['检查医生'],
            "审核医生" => $l2_item['审核医生'],
            "最后保存时间" => $l2_item['最后保存时间'],
          ];
          $l2_id = DB::table('report_l2_items')->insertGetId($r2_data);
          if ($l2_id) {
            $report_l3_base_items = [];
            foreach ($l2_item['基础项目列表'] as $key2 => $l3_item) {
              $r3_data = [
                "report_l2_id" => $l2_id,
                "基础项目名称" => $l3_item['基础项目名称'],
                "基础项目代码" => $l3_item['基础项目代码'],
                "基础项目顺序" => $l3_item['基础项目顺序'],
                "结果类型" => $l3_item['结果类型'],
                "结果值" => $l3_item['结果值'],
                "结果值单位" => $l3_item['结果值单位'],
                "结果值范围" => $l3_item['结果值范围'],
                "异常标识" => $l3_item['异常标识'],
                "所见" => $l3_item['所见'],
                "描述" => $l3_item['描述'],
                "结论" => $l3_item['结论'],
              ];
              $report_l3_base_items[] = $r3_data;
            }
            //批量插入l3
            DB::table('report_l3_base_items')->insert($report_l3_base_items);
          }
        }


      }
    }
    return \Yz::Return(true, "操作完成", []);
  }
}
