<?php

namespace App\Http\Controllers\API\H5;

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');
    $info = [
      'name' => '演示用户',
      'sex' => '2',
      'count' => 1,
      '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]);

  }
}
