<?php
namespace crmeb\services;

use app\admin\model\system\SystemLevelEquity;
use app\admin\model\user\User;
use app\admin\model\user\UserBill;
use app\models\store\StoreOrder;
use MongoDB\Driver\Exception\Exception;

class MycService
{
    /**
     * 公司分红
     * @param $data
     */
    public static function all_stock_right_bonus($data)
    {
        if(intval($data['all_stock_right'])==0) return true;
        $pre_day = strtotime(date('Y-m-d', strtotime('-1 day')));//昨日
        $before_day = strtotime(date('Y-m-d', strtotime('-2 day')));//前天
        $rs = SystemLevelEquity::where('equity_type', 'all_stock_right_bonus')->where('is_show', 1)->select()->toArray();
        $all_stock_equity_list = [];
        foreach ($rs as $v) {
            $all_stock_equity_list[$v['levelid']] = $v;
        }
        try {
            $list = UserBill::where('type', 'all_stock_right')->where('category', 'right')->where('add_time', '<', $pre_day)
                ->group('uid')->field("uid,sum(number) as all_stock_right")->select()->toArray();
            foreach ($list as $v) {
                $level = User::where('uid', $v['uid'])->value('level');
                $profit = bcmul($data['all_profit'], $all_stock_equity_list[$level]['proportion'] / 100, 2);
                $profit_thigh = bcdiv($profit, $data['all_stock_right'], 2);
                $member = bcmul($profit_thigh, $v['all_stock_right'], 2);
                User::where('uid', $v['uid'])->inc('brokerage_price', $member)->inc('all_stock_right_bonus', $member)->update();
                $mark = "公司利润".$data['all_profit'].",总计分红：".$profit.",共".$data['all_stock_right']."股,每股分润".$profit_thigh.",".$all_stock_equity_list[$level]['title'] . ",获得分红:" . $member;
                UserBill::income($all_stock_equity_list[$level]['real_title'], $v['uid'], 'bonus', 'all_stock_right_bonus', $member, 0, $member, $mark);
            }
        }catch (Exception $e)
        {
            return false;
        }
        return true;

    }

    /**
     * 高级合伙人分红
     * @param $data
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function senior_stock_right_bonus($data)
    {
        if(intval($data['senior_stock_right'])==0) return true;
        $pre_day = strtotime(date('Y-m-d', strtotime('-1 day')));//昨日
        $before_day = strtotime(date('Y-m-d', strtotime('-2 day')));//前天
        $rs = SystemLevelEquity::where('equity_type', 'senior_stock_right_bonus')->where('is_show', 1)->select()->toArray();
        $all_stock_equity_list = [];
        foreach ($rs as $v) {
            $all_stock_equity_list[$v['levelid']] = $v;
        }
        try {
            $list = UserBill::where('type', 'senior_stock_right')->where('category', 'right')->where('add_time', '<', $pre_day)
                ->group('uid')->field("uid,sum(number) as senior_stock_right")->select()->toArray();
            foreach ($list as $v) {
                $level = User::where('uid', $v['uid'])->value('level');
                if(isset($all_stock_equity_list[$level]['proportion'])) {
                    $profit = bcmul($data['all_profit'], $all_stock_equity_list[$level]['proportion'] / 100, 2);
                    $profit_thigh = bcdiv($profit, $data['senior_stock_right'], 2);
                    $member = bcmul($profit_thigh, $v['senior_stock_right'], 2);
                    User::where('uid', $v['uid'])->inc('brokerage_price', $member)->inc('all_stock_right_bonus', $member)->update();
                    $mark = "公司利润" . $data['all_profit'] . ",总计分红：" . $profit . ",共" . $data['senior_stock_right'] . "股,每股分润" . $profit_thigh . "," . $all_stock_equity_list[$level]['title'] . ",获得分红:" . $member;
                    UserBill::income($all_stock_equity_list[$level]['real_title'], $v['uid'], 'bonus', 'senior_stock_right_bonus', $member, 0, $member, $mark);
                }
            }
        }catch (Exception $e)
        {
            return false;
        }
        return true;
    }

    /**
     * 全民分红
     * @param $data
     */
    public static function code_stock_right_bonus($data)
    {
        $rs = SystemLevelEquity::where('equity_type', 'code_stock_right_bonus')->where('is_show', 1)->select()->toArray();
        $code_stock_equity_list = [];
        foreach ($rs as $v) {
            $code_stock_equity_list[$v['levelid']] = $v;
        }
        try {
            if(isset($data['code'])) {
                foreach ($data['code'] as $key => $v) {
                    $code_stock_right_sum = User::where('code', $key == 'system' ? '' : $key)->whereExp('code_stock_right_bonus', '<monetary')->sum('code_stock_right');
                    if ($code_stock_right_sum == 0) continue;
                    $userlist = User::where('code', $key == 'system' ? '' : $key)->where('code_stock_right_bonus', 'exp', "<monetary")->field('uid,level,code_stock_right,monetary,code_stock_right_bonus')->select()->toarray();
                    foreach ($userlist as $v1) {
                        $level = $v1['level'];
                        $profit = bcmul($v['order_profit_sum'], bcdiv($code_stock_equity_list[$level]['proportion'], 100, 2));
                        $profit_thigh = bcdiv($profit, $code_stock_right_sum, 2);
                        $member = bcmul($profit_thigh, $v1['code_stock_right'], 2);
                        if (bcsub($v1['monetary'], $v1['code_stock_right_bonus'], 2) < $member) {
                            if (bcsub($v1['monetary'], $v1['code_stock_right_bonus'], 2) > 0) {
                                $member = bcsub($v1['monetary'], $v1['code_stock_right_bonus'], 2);
                            } else {
                                continue;
                            }
                        }
                        User::where('uid', $v1['uid'])->inc('brokerage_price', $member)->inc('code_stock_right_bonus', $member)->update();
                        $mark = User::where('code', $key)->where('code_top', 1)->value('nickname') . "总代利润" . $v['order_profit_sum'] . ",总计分红：" . $profit . ",共" . $code_stock_right_sum . "股,每股分润" . $profit_thigh . "," . $code_stock_equity_list[$level]['title'] . ",获得分红:" . $member;
                        UserBill::income($code_stock_equity_list[$level]['real_title'], $v1['uid'], 'bonus', 'code_stock_right_bonus', $member, 0, $member, $mark);
                    }
                }
            }
        }catch (Exception $e)
        {
            return false;
        }
        return true;
    }

    /**
     * 赠送分红
     * @param $sum
     */
    public static  function give_stock_right_bonus($data)
    {
        $rs = SystemLevelEquity::where('equity_type', 'give_stock_right_bonus')->where('is_show', 1)->select()->toArray();
        $code_stock_equity_list = [];
        foreach ($rs as $v) {
            $code_stock_equity_list[$v['levelid']] = $v;
        }
        try {
            if(isset($data['code'])) {
                foreach ($data['code'] as $key => $v) {
                    $userlist = User::alias('a')->join("system_user_level b", "a.level=b.id", "left")->where('a.code', $key == 'system' ? '' : $key)->where('a.give_stock_right', '>', 0)->where('a.give_stock_right_bonus', 'exp', "<if(a.monetary>b.up_amount,a.monetary,b.up_amount)")->field('a.uid,a.level,a.give_stock_right,b.up_amount,a.monetary,if(a.monetary>b.up_amount,a.monetary,b.up_amount) as monetary_ok,a.give_stock_right_bonus')->select()->toarray();
                    $give_stock_right_sum = User::alias('a')->join("system_user_level b", "a.level=b.id", "left")->where('a.code', $key == 'system' ? '' : $key)->where('a.give_stock_right', '>', 0)->where('a.give_stock_right_bonus', 'exp', "<if(a.monetary>b.up_amount,a.monetary,b.up_amount)")->sum('give_stock_right');
                    if ($give_stock_right_sum == 0) continue;
                    foreach ($userlist as $v1) {
                        $level = $v1['level'];

                        if ($give_stock_right_sum > 0) {
                            if (isset($code_stock_equity_list[$level]['proportion'])) {
                                $profit = bcmul($v['order_profit_sum'], $code_stock_equity_list[$level]['proportion'] / 100, 2);
                                $profit_thigh = bcdiv($profit, $give_stock_right_sum, 2);
                                $member = bcmul($profit_thigh, $v1['give_stock_right'], 2);
                                if (bcsub($v1['monetary_ok'], $v1['give_stock_right_bonus'], 2) < $member) {
                                    if (bcsub($v1['monetary'], $v1['give_stock_right_bonus'], 2) > 0) {
                                        $member = bcsub($v1['monetary'], $v1['give_stock_right_bonus'], 2);
                                    } else {
                                        continue;
                                    }
                                }
                                User::where('uid', $v1['uid'])->inc('brokerage_price', $member)->inc('give_stock_right_bonus', $member)->update();
                                $mark = User::where('code', $key)->where('code_top', 1)->value('nickname') . "总代利润" . $v['order_profit_sum'] . ",总计分红：" . $profit . ",共" . $give_stock_right_sum . "股,每股分润" . $profit_thigh . "," . $code_stock_equity_list[$level]['title'] . ",获得分红:" . $member;
                                UserBill::income($code_stock_equity_list[$level]['real_title'], $v1['uid'], 'bonus', 'give_stock_right_bonus', $member, 0, $member, $mark);
                            }
                        }
                    }
                }
            }
        }catch (Exception $e)
        {
            return false;
        }
        return true;
    }

    /**
     * 推荐分红
     * @param $data
     */
    public static function recommend_stock_right_bonus($data)
    {
        if(intval($data['recommend_stock_right'])==0) return true;
        $rs = SystemLevelEquity::where('equity_type', 'recommend_bonus')->where('is_show', 1)->select()->toArray();
        $all_stock_equity_list = [];
        foreach ($rs as $v) {
            $all_stock_equity_list[$v['levelid']] = $v;
        }

        try {
            $list = UserBill::where('type', 'recommend_stock_right')->where('category', 'right')->where('add_time', '<',  strtotime(date('Y-m-d')))
                ->group('uid')->field("uid,sum(number) as all_stock_right")->select()->toArray();


            foreach ($list as $v) {
                $level = User::where('uid', $v['uid'])->value('level');
                if(isset($all_stock_equity_list[$level]['proportion'])) {
                    $profit_thigh = bcdiv(bcmul($data['all_profit'], $all_stock_equity_list[$level]['proportion'] / 100, 2), $data['recommend_stock_right'], 2);
                    $member = bcmul($profit_thigh, $v['all_stock_right'], 2);
                    User::where('uid', $v['uid'])->inc('brokerage_price', $member)->inc('recommend_stock_right_bonus', $member)->update();
                    $mark = "公司利润" . $data['all_profit'] . ",总计分红：" . bcmul($data['all_profit'], $all_stock_equity_list[$level]['proportion'] / 100, 2) . ",共" . $data['recommend_stock_right'] . "股,每股分润".$all_stock_equity_list[$level]['title'] . ",获得分红:" . $member;
                    UserBill::income($all_stock_equity_list[$level]['real_title'], $v['uid'], 'bonus', 'recommend_stock_right_bonus', $member, 0, $member, $mark);

                }
            }
        }catch (Exception $e)
        {
            return false;
        }
        return true;
    }
    /**
     * 总代推荐分红
     * @param $data
     */
    public static function code_recommend_stock_right_bonus($data)
    {
        if(intval($data['code_recommend_stock_right'])==0) return true;
        $rs = SystemLevelEquity::where('equity_type', 'code_recommend_bonus')->where('is_show', 1)->select()->toArray();
        $all_stock_equity_list = [];
        foreach ($rs as $v) {
            $all_stock_equity_list[$v['levelid']] = $v;
        }
        try {

            $list = UserBill::where('type', 'code_recommend_stock_right')->where('category', 'right')->where('add_time', '<',  strtotime(date('Y-m-d')))
                ->group('uid')->field("uid,sum(number) as code_recommend_stock_right")->select()->toArray();
            foreach ($list as $v) {
                $level = User::where('uid', $v['uid'])->value('level');
                if(isset($all_stock_equity_list[$level]['proportion'])) {
                    $profit_thigh = bcdiv(bcmul($data['all_profit'], $all_stock_equity_list[$level]['proportion'] / 100, 2), $data['code_recommend_stock_right'], 2);
                    $member = bcmul($profit_thigh, $v['code_recommend_stock_right'], 2);
                    User::where('uid', $v['uid'])->inc('brokerage_price', $member)->inc('recommend_stock_right_bonus', $member)->update();
                    $mark = "公司利润" . $data['all_profit'] . ",总计分红：" . bcmul($data['all_profit'], $all_stock_equity_list[$level]['proportion'] / 100, 2) . ",共" . $data['code_recommend_stock_right'] . "股,每股分润". $all_stock_equity_list[$level]['title'] . ",获得分红:" . $member;
                    UserBill::income($all_stock_equity_list[$level]['real_title'], $v['uid'], 'bonus', 'code_recommend_stock_right_bonus', $member, 0, $member, $mark);

                }
            }
        }catch (Exception $e)
        {
            return false;
        }
        return true;
    }
}
