<?php

namespace app\business;

use Illuminate\Support\Arr;
use support\Db;
use support\Redis;

class UserIdentityBusiness
{
    /** 用户实名信息
     * @param array $param
     * @return void
     */
    static public function data(array $param)
    {
        $data = Db::table('wa_user_identity')->where(function ($query) use ($param) {
            if (Arr::get($param, 'uid')) {
                $query->where('uid', $param['uid']);
            }
        })->first();
        return $data;
    }

    /**绑定和修改实名信息
     * @param array $param
     * @return void
     */
    static public function UpData(array $param)
    {
        try {
            if (empty($param['user_data']['is_autonym'])) {
                /** 给上级增加 */
                Db::table('wa_users')->where('id', $param['user_data']['pid'])->increment('is_num', 1);
                /** 绑定 */
                Db::table('wa_user_identity')->insert([
                    'name'       => $param['name'],
                    'number'     => $param['number'],
                    'uid'        => $param['user_data']['id'],
                    'created_at' => date('Y-m-d H:i:s'),
                    'updated_at' => date('Y-m-d H:i:s'),
                ]);
                $system = Db::table('wa_system')->first();
                /** 实名赠送 */
                if (!empty($system) && !empty($system->autonym)) {
                    StreamBusiness::addStream($param['user_data']['id'], $system->autonym, streamType11, moldType4, moldTypefild4);
                }
                if (!empty($system) && !empty($system->register_award)) {
                    StreamBusiness::addStream($param['user_data']['id'], $system->register_award, streamType11, moldType5, moldTypefild5);
                }

                Db::table('wa_users')->where('id', $param['user_data']['pid'])->increment('raffle_num', 1);
                Db::table('wa_users')->where('id', $param['user_data']['pid'])->increment('accumulate_raffle_num', 1);

                Db::table('wa_users')->where('id', $param['user_data']['pid'])->increment('buy_num', 1);


                /** 邀请 */
                $user = Db::table('wa_users')->where('id', $param['user_data']['id'])->first();
                if (!empty($user->pid) && !empty($system->invitation_award)) {
                    StreamBusiness::addStream($user->pid, $system->invitation_award, streamType3, moldType5, moldTypefild5, $user->id);
                }

//                $num        = Db::table('wa_users')
//                    ->where('id', $param['user_data']['pid'])
//                    ->value('is_num');
//                $inviteData = Db::table('wa_invite')
//                    ->where('num', $num)->first();
//                if (!empty($inviteData)) {
//                    StreamBusiness::addStream($param['user_data']['pid'], $inviteData->money, streamType3, moldType3, moldTypefild3, $inviteData->id);
//                }
//                if (!empty($user->ppid) && !empty($system->invitation_award)) {
//                    StreamBusiness::addStream($user->ppid, bcmul($system->invitation_award, '0.1', 2), streamType3, moldType1, moldTypefild1,$user->id);
//                }
//                if (!empty($user->toppid) && !empty($system->invitation_award)) {
//                    StreamBusiness::addStream($user->toppid, bcmul(bcmul($system->invitation_award, '0.1', 2),'0.1',2), streamType3, moldType1, moldTypefild1, $user->id);
//                }

            } else {
                throw new \Exception('请联系客服进行修改!');
                $data = Db::table('wa_user_identity')->where('uid', $param['user_data']['id'])->first();

                /** 修改 */
                Db::table('wa_user_identity')->where('uid', $param['user_data']['id'])->update([
                    'name'       => $param['name'],
                    'number'     => $param['number'],
                    'oname'      => $data->name,
                    'onumber'    => $data->number,
                    'created_at' => date('Y-m-d H:i:s'),
                    'updated_at' => date('Y-m-d H:i:s'),
                ]);
                Db::table('wa_users')->where('id', $param['user_data']['id'])->update(['name' => $param['name']]);
            }
            Db::table('wa_users')->where('id', $param['user_data']['id'])->update(['is_autonym' => 1, 'name' => $param['name']]);
        } catch (\Throwable $exception) {
            throw new \Exception($exception->getMessage());
        }
        return true;
    }

//    /** 邀请赠送抽奖次数
//     * @return void
//     */
//    static public function invite($uid)
//    {
//        if($uid){
//            try {
//                $users=Db::table('wa_users')->where('id',$uid)->first();
//                $system=Db::table('wa_system')->first();
//                $raffle_num=bcsub(bcdiv(bcadd($users->is_num,1),$system->raffle),$users->total_raffle_num);
//                if($raffle_num){
//                    Db::table('wa_users')->where('id',$uid)->increment('raffle_num',$raffle_num);
//                    Db::table('wa_users')->where('id',$uid)->increment('total_raffle_num',$raffle_num);
//                }
//            }catch (\Throwable $exception){
//                throw new \Exception($exception->getMessage());
//            }
//        }
//
//    }
}