<?php

namespace App\Http\Controllers\API\H5;

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

class PersonController extends Controller
{
    public function Save(Request $request)
    {
        $info = request('info');
        $openid = $request->get('userid');//中间件产生的参数
        if(!isset($info['id'])) return \Yz::echoError1("id不能为空");
        if(!isset($info['name'])) return \Yz::echoError1("姓名不能为空");
        if(!isset($info['tel'])) return \Yz::echoError1("电话不能为空");
        if(!isset($info['sex'])) return \Yz::echoError1("性别不能为空");
        if(!isset($info['id_number'])) return \Yz::echoError1("证件号不能为空");
        $u=false;
        $person_id=0;
        $data=[
            'openid'=>$openid,
            'name'=>$info['name'],
            'tel'=>$info['tel'],
            'sex'=>$info['sex'],
            'id_number'=>$info['id_number'],
            'updated_at'=>date('Y-m-d H:i:s')
        ];
        if($info['id']==0){
            $u= DB::table('person')->insertGetId($data);
            $person_id=$u;

        }else{
            $u= DB::table('person')->where('id',$info['id'])->update($data);
            $person_id=$info['id'];
        }
        if(!!$u){
            return \Yz::Return(true,"保存成功",['id'=>$person_id]);
        }else{
            return \Yz::echoError1("保存失败");
        }
    }
    public function GetList(Request $request)
    {
        $userid = $request->get('userid');//中间件产生的参数
        $search = request('search');
        $list=DB::table('person')->where(['openid'=>$userid,'is_del'=>0])->get();
        return \Yz::Return(true,"查询成功",['list'=>$list]);
    }
    public function GetDetail(Request $request)
    {
        $userid = $request->get('userid');//中间件产生的参数
        $id = request('id');
        $info=DB::table('person')->where(['id'=> $id,'is_del'=>0,'openid'=>$userid])->first();
        if(!$info) return \Yz::echoError1("查询会员信息失败");

        return \Yz::Return(true,"查询成功",['info'=>$info]);
    }
    public function Del(Request $request)
    {
        $userid = $request->get('userid');//中间件产生的参数
        $id = request('id');
        $d=DB::table('person')->where(['id'=> $id,'openid'=>$userid])->update(['is_del'=>1]);
        if(!$d) return \Yz::echoError1("删除失败");

        return \Yz::Return(true,"删除成功",['id'=>$id]);
    }
}
