<?php namespace Score\Lib;
use Dever;
class Log
{
    /**
     * 增加行为日志 Dever::load('score/log')->add(1, '订单', '下单', 100);
     *
     * @return mixed
     */
    public function log_commit(){}
    public function log($uid, $cate_name, $name, $num = 0)
    {
        $action = Dever::load('action', 'score')->get($cate_name, $name);
        if ($action && $action['id'] > 0) {
            $insert = array();
            $insert['uid'] = $uid;
            $insert['action_id'] = $action['id'];
            //$insert['ip'] = Dever::ip();
            $action_log_id = Dever::db('action_log', 'score')->insert($insert);

            if ($action_log_id) {
                $rule = Dever::db('rule', 'score')->select(array('action_id' => $action['id']));
                $name = $cate_name . $name;
                if ($rule) {
                    foreach ($rule as $k => $v) {
                        if ($num > 0) {
                            $v['num'] = $num;
                        }
                        $this->rule($uid, $v, $action_log_id, $name);
                    }
                } elseif ($num > 0) {
                    $info = Dever::db('info', 'score')->find(array('type' => 1));
                    $rule = array();
                    $rule['info_id'] = $info['id'];
                    $rule['action_id'] = $action['id'];
                    $rule['num'] = $num;
                    $this->rule($uid, $rule, $action_log_id, $name);
                }
            }
        }
    }

    private function rule($uid, $rule, $action_log_id, $name)
    {
        if (isset($rule['upper']) && $rule['upper'] == 2 && $rule['upper_limit'] >= 1) {
            $upper = $this->upper($uid, $rule);
            if (!$upper) {
                # 达到积分上限
                return;
            }
        }
        $log = $this->add($uid, $rule, $action_log_id, $name);

        if (!$log['id']) {
            return;
        }

        # 增加返利
        if (isset($rule['rebate']) && $rule['rebate'] > 1) {
            $this->rebate($uid, $rule, $log, $name);
        }
    }

    # 设置上限限制
    private function upper($uid, $rule)
    {
        # 有上限限制
        $limit = $rule['upper_limit'];
        # 获取用户最新一次积分变化日志

        $where = array();
        $where['uid'] = $uid;
        $where['info_id'] = $rule['info_id'];
        $where['action_id'] = $rule['action_id'];
        $user_log = Dever::db('user_log', 'score')->find($where);
        if ($user_log) {
            if ($rule['upper_type'] == 1) {
                # 按天
                $where['cdate'] = array('>=', strtotime(date('Y-m-d 00:00:00')));
            } elseif ($rule['upper_type'] == 2) {
                # 按小时
                $where['cdate'] = array('>=', time() - ($rule['upper_time'] * 3600));
            } elseif ($rule['upper_type'] == 3) {
                # 永久
            } else {
                return true;
            }

            $user_log_num = Dever::db($this->user_log)->count($where);
            if ($user_log_num >= $rule['upper_limit']) {
                return false;
            }
        }
        return true;
    }

    # 增加积分
    private function add($uid, $rule, $action_log_id, $name, $user_log_id = false)
    {
        $where = array();
        $where['uid'] = $uid;
        $where['info_id'] = $rule['info_id'];
        $user = Dever::db('user', 'score')->find($where);

        if (!$user) {
            $user = array();
            $user['id'] = Dever::db('user', 'score')->insert($where);
            $user['balance'] = 0;
            $user['udate'] = 0;
        }

        $update = array();

        $insert = $where;
        $insert['action_id'] = $rule['action_id'];
        $insert['action_log_id'] = $action_log_id;
        $insert['num'] = $rule['num'];
        if ($user_log_id) {
            $insert['user_log_id'] = $user_log_id;
            $insert['mode'] = 2;
        }

        if ($rule['num'] > 0) {
            $update['total'] = array('+', $rule['num']);
        } elseif ($rule['num'] < 0) {
            $update['used'] = array('+', $rule['num']);
        }
        $update['balance'] = array('+', $rule['num']);
        $insert['balance'] = $user['balance'] + $rule['num'];
        $insert['name'] = $name;
        $insert['id'] = Dever::db('user_log', 'score')->insert($insert);

        if ($insert['id']) {
            $state = Dever::db('user', 'score')->update(array('id' => $user['id'], 'udate' => $user['udate']), $update);
            if (!$state) {
                Dever::error('请求失败请重试');
            }
        }

        return $insert;
    }

    # 返利
    private function rebate($uid, $rule, $log, $name)
    {
        if (!strstr($rule['rebate_value'], ':')) {
            $rule['rebate_value'] = '1:' . $rule['rebate_value'];
        }
        $temp = explode(',', $rule['rebate_value']);
        $parent_uid = false;
        $rule['info_id'] = $rule['rebate_info_id'];
        $name = '用户[' . $uid . ']' . $name . '返利';
        foreach ($temp as $k => $v) {
            list($level, $value) = explode(':', $v);
            if ($rule['rebate'] == 2) {
                # 百分比
                $value = round($rule['num']*($value/100), 2);
            }
            $parent_uid = Dever::load('relation', 'invite')->getParent($uid, $level);
            if ($parent_uid && $parent_uid > 0) {
                $rule['num'] = $value;
                $this->add($parent_uid, $rule, $log['action_log_id'], $name, $log['id']);
            }
        }
    }
}