<?php

namespace App\Http\Controllers\API\H5;

use App\Http\Controllers\API\XCXApiController;
use App\Http\Controllers\Controller;
use App\Services\ConfigService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class UserController extends Controller
{
  // 获取配置更新时间
  public function info(Request $request)
  {
    $openid = $request->post('openid');
    if(!isset($openid)) return \Yz::echoError1("openid不能为空");
    $user=DB::table('web_users')->where(['openid'=>$openid])->first();
    $userid=false;
    if(!$user){
      $userid=DB::table('web_users')->insertGetId(['openid'=>$openid]);
    }else{
      $userid=$user->id;
    }
    if($userid) {
     if(self::UpdatePersonList($openid)){//调用更新就诊人方法
        //查询默认就诊人
       $person_list=DB::table('web_user_person')->where(['user_id'=>$userid,'is_del'=>0])->get();
       $default_person=DB::table('web_user_person')->where(['user_id'=>$userid,'is_default'=>1,'is_del'=>0])->first();
       $count=count($person_list);
       if($count>0 and !$default_person){
         DB::table('web_user_person')->where(['id'=>$person_list[0]->id])->update(['is_default'=>1]);
         $default_person=$person_list[0];
       }

       $info = [
         'name' => isset($default_person->name)? $default_person->name:null,
         'sex' => isset($default_person->sex)?$default_person->sex:null,
         'count' => $count,
         'openid' => $openid,
       ];
       return \Yz::Return(true, '获取成功', [
         'info' => $info
       ]);
     }
    }

  }

  //获取名下体检人列表
  public function GetPersonList()
  {
    $openid = request('openid');
    $user = DB::table('web_users')->where(['openid' => $openid, 'status' => 1, 'is_del' => 0])->first();
    if (!$user) return \Yz::echoError1('用户不存在');
    $persons = DB::table('web_user_person')->where(['user_id' => $user->id, 'is_del' => 0])->get();
    return \Yz::Return(true, "查询完成", ['list' => $persons]);
  }

  //切换默认就诊人
  public function SetDefaultPerson()
  {
    $openid = request('openid');
    $person_id = request('person_id');
    $user = DB::table('web_users')->where(['openid' => $openid, 'status' => 1, 'is_del' => 0])->first();
    if (!$user) return \Yz::echoError1('用户不存在');
    DB::table('web_user_person')->where(['user_id' => $user->id, 'is_del' => 0, 'is_default' => 1])->update([
      'is_default' => 2
    ]);
    $u = DB::table('web_user_person')->where(['id' => $person_id, 'user_id' => $user->id, 'is_del' => 0])->update([
      'is_default' => 1
    ]);
    if ($u) {
      return \Yz::Return(true, "操作完成", []);
    } else {
      return \Yz::echoError1('操作失败');
    }
  }

  //获取就诊人基本信息 积分、预存款等
  public function GetPersonInfo()
  {
    $openid = request('openid');
    $person_id = request('person_id');
    $user = DB::table('web_users')->where(['openid' => $openid, 'status' => 1, 'is_del' => 0])->first();
    if (!$user) return \Yz::echoError1('用户不存在');
    $person = DB::table('web_user_person')->where(['user_id' => $user->id, 'is_del' => 0, 'is_default' => 1])->first();
    if (!$person) return \Yz::echoError1('查询就就诊人信息出错');
    //调用his接口查询用户积分和预存款
    $integral = 90;
    $save_money = 150;
    $coupon_count = 2;//优惠券数量

    $person->integral = $integral;
    $person->save_money = $save_money;
    $person->coupon_count = $coupon_count;
    return \Yz::Return(true, "查询完成", ['person_info' => $person]);

  }

  //更新用户列表，调用远程小程序接口
  public function UpdatePersonList($openid)
  {
    $XCX=new XCXApiController();
   // $list=$XCX::Post('就诊人列表',['a'=>1]);
   // $openid = "234243";
    $ApiPersonList = [
      [
        'ghzid' => 'ghz11',
        'id_number'=>'24324324322',
        'name' => '测试1',
        'phone' => '11111111111',
        'sex' => '1',
        'birthday' => '1989-01-01',
        'patient_type' => '0',
        'marriage' => '1',
      ],
      [
        'ghzid' => 'ghz22',
        'id_number'=>'1132112432432',
        'name' => '测试2',
        'phone' => '222222222',
        'sex' => '1',
        'birthday' => '1990-01-01',
        'patient_type' => '1',
        'marriage' => '1',
      ]
    ];
    $user = DB::table('web_users')->where(['openid' => $openid, 'status' => 1, 'is_del' => 0])->first();
    if (!$user) return \Yz::echoError1('用户不存在');
    //库里存在的用户ghzid数组
    $db_person_ghzids = DB::table('web_user_person')->where(['user_id' => $user->id,'is_del'=>0])->pluck('ghzid')->toArray();
    //接口返回的用户ghzids数组
    $api_person_ghzids = [];
    foreach ($ApiPersonList as $apiperson) {
      $api_person_ghzids[] = $apiperson['ghzid'];
    }
    // 取交集
    $intersection = array_intersect($db_person_ghzids, $api_person_ghzids);
    // 仅存在于 数据库里 中的ghzid
    $onlyInDb = array_diff($db_person_ghzids, $api_person_ghzids);
    // 仅存在于 api接口 中的ghzid
    $onlyInApi = array_diff($api_person_ghzids, $db_person_ghzids);
    $success_count=0;
    foreach ($ApiPersonList as $apiperson) {
      $personInfo = [
        'ghzid' => $apiperson['ghzid'],
        'id_number' => $apiperson['id_number'],
        'name' => $apiperson['name'],
        'birthday' => $apiperson['birthday'],
        'sex' => $apiperson['sex'],
        'phone' => $apiperson['phone'],
        'married' => $apiperson['marriage'],
        'user_id' => $user->id,
        'patient_type' => $apiperson['patient_type'],
        'updated_at'=>date('Y-m-d H:i:s')
      ];
      if (in_array($apiperson['ghzid'], $intersection)) {//双方都有的，更新
        $u=DB::table('web_user_person')->where(['user_id' => $user->id, 'ghzid' => $apiperson['ghzid']])->update($personInfo);
        if($u) $success_count+=$u;
      }
      if (in_array($apiperson['ghzid'], $onlyInApi)) { //小程序新增的 添加
        $i=DB::table('web_user_person')->insert($personInfo);
        if($i) $success_count++;
      }
    }

    if (count($onlyInDb) > 0) { //小程序不存在的用户 ，数据进行删除
      $d=DB::table('web_user_person')->where(['user_id' => $user->id])->whereIn('ghzid',$onlyInDb)->update([
        'is_del' => 1
      ]);
      if($d) $success_count++;
    }
    //设置默认体检人
//    $default=DB::table('web_user_person')->where(['user_id' => $user->id,'is_del'=>0,'is_default'=>1])->get();
//    $p_list=DB::table('web_user_person')->where(['user_id' => $user->id,'is_del'=>0])->get();
//    if(count($p_list)>0 and  count($default)===0){
//      DB::table('web_user_person')->where(['id'=>$p_list[0]->id])->update(['is_default'=>1]);
//    }
    return true;


  }
}
