<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 会员类型封装
 */
namespace app\allwin\widget;
use app\allwin\model\Bank;
use app\allwin\model\BankBill;
use app\allwin\model\FundBill;
use app\allwin\model\AllwinUser;
use app\allwin\model\Levels;
use app\common\model\SystemUser;

class Vip{
  
    /**
     * 开通会员,并注册为会员用户
     * 如果已经是会员,更换会员类型,以第一次开通邀请人的所属店铺为当前用户所属店铺
     * @return void
     */
    public static function addLevel(int $uid,int $invite_uid = 0,int $store_id = 0){
        //判断直推关系
        $level = Levels::where(['user_id' => $uid])->find();
        if (empty($level) && !empty($invite_uid)) {
            Levels::addLevel($uid,$invite_uid);  //增加要求邀请关系
        }
        $rel = AllwinUser::where(['uid' => $uid])->find();
        if (empty($rel)) {
            $data['uid']      = $uid;
            $data['store_id'] = $store_id;
            return AllwinUser::create($data);
        }
        return true;
    }

    /**
     * 已邀请码来判断邀请关系和开通,并注册为会员用户
     * @return void
     */
    public static function register(int $uid,$invite){
        $store_id = 0;
        $invite_uid = SystemUser::isInvite($invite);
        if($invite_uid){
            $vipuser = AllwinUser::where(['uid' => $invite_uid])->find();
            if($vipuser){
                $store_id = $vipuser['store_id'];
            }
        }
        //增加要求邀请关系
        return self::addLevel($uid,$invite_uid,$store_id);
    }
    
    /**
     * 收益计算器
     * $vip->store_workers      //直推（必须是会员、如果是老板或员工免开会员）
     * $vip->store              //给好店管理员（如推荐人不属于某个好店,则直接归运营公司）
     * $vip->company_workers    //公司员工或签约的社群主
     * $vip->company            //地方运营公司
     * $vip->stock              //采购基金
     * $vip->group              //给所属总代或总公司
     * @param array $order
     * @return void
     */
    public static function income(array $order){
        $miniapp_id = $order['member_miniapp_id'];
        //读取所属VIP
        $vip_card = model('VipCard')->where(['id' => $order['vipcard_id']])->find();
        if (empty($vip_card)) {
            return;
        }
        //如果订单金额和创客配置不一样禁止分配
        if ($order['amount'] != $vip_card->price) {
            return;
        }
        //判断直接推荐人UID
        $parent_uid = 0; 
        $levels = model('Levels')->where(['user_id' => $order['user_id'],'level' => 1])->field('parent_id')->find();
        if (isset($levels)) {
            $parent_vip = model('Vip')->where(['user_id' => $levels->parent_id,'is_lock' => 0,'state' => 1])->field('vipcard_id,user_id')->find();
            if (isset($parent_vip)) {
                if ($parent_vip->vipcard->is_up) {
                    if ($parent_vip->vipcard_id == $vip_card->id) {
                        $parent_uid = $levels->parent_id;
                    }
                }else{
                    $parent_uid = $levels->parent_id;
                }
            }
        }
        //判断用户是好店老板、好店员工还是社群主
        $allwinUser = model('AllwinUser')->where(['uid' => $order['user_id']])->field('store_id')->find();  //判断开通用户所属好店
        $store_id = empty($allwinUser) ? 0 : $allwinUser->store_id;
        if($store_id){
            //读取好店老板
            $store = model('Store')->where(['id' => $store_id])->field('manage_uid')->find();
            $store_uid = empty($store->manage_uid) ? 0 : $store->manage_uid;
            //判断好店是否是运营公司的员工
            $worker = model('Worker')->whereRaw("FIND_IN_SET({$store_id},store_ids)")->find();
            $worker_uid = empty($worker) ? 0 : $worker->uid;
        }else{
            $store_uid = model('AllwinUserAgent')->agentUid($order['user_id'],$miniapp_id);
            $worker_uid = 0;  //员工UID
        }
        //判断是否有社群主
        //如果分配金额小于待分配,将不分配
        if($parent_uid && $vip_card->store_workers > 0){
            Bank::dueMoney($miniapp_id,$parent_uid,$vip_card->store_workers);
            BankBill::add(['miniapp_id'=>$miniapp_id,'store_id' => 0,'money'=>$vip_card->store_workers,'uid'=>$parent_uid,'pay_uid' =>$order['user_id']],'推荐会员奖励');
        }
        //好店管理员
        if($store_uid && $vip_card->store > 0){
            Bank::dueMoney($miniapp_id,$store_uid,$vip_card->store);
            BankBill::add(['miniapp_id'=>$miniapp_id,'store_id' => 0,'money' => $vip_card->store,'uid'=>$store_uid,'pay_uid' =>$order['user_id']],'会员奖励');
            if($store_id){
                model('StoreStats')->vip($miniapp_id,$store_id,$vip_card->store);  //增加创客分润
            }
        }
        //员工奖励
        if($worker_uid && $vip_card->company_workers > 0){
            Bank::dueMoney($miniapp_id,$worker_uid,$vip_card->company_workers);
            BankBill::add(['miniapp_id'=>$miniapp_id,'store_id' => 0,'money' => $vip_card->company_workers,'uid'=> $worker_uid,'pay_uid' =>$order['user_id']],'员工会员奖励');
        }
        //购物基金
        if($vip_card->stock > 0){
            model('Fund')->recharge($miniapp_id,$vip_card->stock);
            FundBill::add(['miniapp_id'=>$miniapp_id,'types'=>0,'store_id'=>$order['store_id'],'money'=>$vip_card->stock,'order_no' => $order['order_no'],'uid' =>$order['user_id']]);
        }
        //采购公司利润
        if($vip_card->company > 0){
            model('Platform')->company($miniapp_id,$vip_card->company);
        }
        if($vip_card->group > 0){
            model('Platform')->platforms($miniapp_id,$vip_card->group);
        }
        return model('vip')->where(['id' => $order['id']])->update(['is_divided' => 1]);
    }
}