<?php

namespace Score\Lib;

use Dever;

class Core
{
	/**
	 * 增加日志 Dever::load('score/lib/core')->log($uid, $key, $name);
	 *
	 * @return mixed
	 */
	public function log($uid, $action_key, $action_name, $callback = '', $score = false, $num = false)
	{
		$action = Dever::db('score/action')->one(array('key' => $action_key));

		if (!$action) {
			$action = array();
			$action['id'] = Dever::db('score/action')->insert(array('key' => $action_key, 'name' => $action_name));
		}

		if (isset($action['id']) && $action['id'] > 0) {
			$insert = array();
			$insert['uid'] = $uid;
			$insert['action_id'] = $action['id'];
			$insert['callback'] = $callback;
			if ($score > 0) {
				$insert['mscore'] = $score;
			}
			if ($num > 0) {
				$insert['num'] = $num;
			}
			$insert['cron_type'] = 1;
			$log_id = Dever::db('score/action_log')->insert($insert);

			//Dever::daemon('lib/core.oper?log_id='.$log_id, 'score');
			//Dever::load('score/lib/core.oper?log_id='.$log_id);
		}
	}

	public function oper_api()
	{

	}

	/**
	 * 根据用户行为，增加积分
	 *
	 * @return mixed
	 */
	public function oper()
	{
		$log_id = Dever::input('log_id');

		$log = Dever::db('score/action_log')->one($log_id);

		$this->operAction($log);

		return $log;
	}

	private function rule($log, $info)
	{
		# 手动增加积分
		if (isset($log['mscore']) && $log['mscore'] > 0) {
			$info['num'] = $log['mscore'];
		}
		# 积分乘以倍数
		if (isset($log['num']) && $log['num'] > 0) {
			$info['num'] = $info['num'] * $log['num'];
		}
		if ($info['num'] == 0) {
			Dever::db('score/action_log')->update(array('where_id' => $log['id'], 'cron_type' => 3));
			return;
		}

		$uid = $log['uid'];

		$user = Dever::db('score/user')->one(array('uid' => $uid, 'config_id' => $info['config_id']));

		if (!$user) {
			$user = array();
			$user['id'] = Dever::db('score/user')->insert(array('uid' => $uid, 'config_id' => $info['config_id'], 'score' => 0, 'no_score' => 0));
			$user['score'] = 0;
		}

		$num = $info['num'];

		if ($info['upper'] == 2 && $info['upper_limit'] >= 1) {
			$upper = $this->upper($uid, $info, $log);
			if (!$upper) {
				Dever::db('score/action_log')->update(array('where_id' => $log['id'], 'score_type' => 3, 'cron_type' => 3, 'score' => '0'));
				return;
			}
		}
		Dever::db('score/action_log')->update(array('where_id' => $log['id'], 'score_type' => 2, 'cron_type' => 3, 'score' => $num));

		$update = array();
		$update['where_id'] = $user['id'];
		$update['score'] = $user['score'] + $num;

		$insert['uid'] = $uid;
		$insert['config_id'] = $info['config_id'];
		$insert['action_log_id'] = $log['id'];
		$insert['action_id'] = $info['action_id'];
		$insert['status'] = 1;
		$insert['num'] = $num;
		$insert['total'] = $update['score'];
		$insert['cdate'] = time();
		$user_log_id = Dever::db('score/user_log')->insert($insert);

		Dever::db('score/user')->update($update);

		# 提升等级
		$this->setLevel($uid);

		if ($log['callback']) {
			if (strstr($log['callback'], '?')) {
				Dever::load($log['callback'] . '&user_log_id=' . $user_log_id);
			} else {
				Dever::load($log['callback'], $user_log_id);
			}
		}
	}

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

		$time = time();

		$where = array();
		$where['config_id'] = $info['config_id'];
		$where['action_id'] = $info['action_id'];
		$where['uid'] = $uid;
		$where['status'] = 1;
		$user_log = Dever::db('score/user_log')->getNew($where);
		if ($user_log) {
			if ($info['upper_type'] == 1) {
				# 按天
				$where['start'] = Dever::maketime(date('Y-m-d 00:00:00', $time));
				//$where['end'] = $end;
			} elseif ($info['upper_type'] == 2) {
				# 按小时
				$where['start'] = $time - ($info['upper_time'] * 3600);
				//$where['end'] = $end;
			} elseif ($info['upper_type'] == 3) {
				# 永久
			} else {
				return true;
			}

			$user_log_num = Dever::db('score/user_log')->getNewTotal($where);
			if ($user_log_num >= $info['upper_limit']) {
				return false;
			}
		}

		return true;
	}

	private function operAction($log)
	{
		if ($log && $log['cron_type'] == 1) {
			Dever::db('score/action_log')->update(array('where_id' => $log['id'], 'cron_type' => 2));
			$where = array();
			$where['state'] = 1;
			$where['id'] = $log['action_id'];
			$action = Dever::db('score/action')->one($where);

			if ($action) {
				$rule = Dever::db('score/rule')->state(array('action_id' => $action['id']));

				if ($rule) {
					foreach ($rule as $k => $v) {
						$this->rule($log, $v);
					}
				}
			}
		}
	}

    /**
	 * 根据积分日志和规则，增加积分，这是一个定时任务，可以设置每天凌晨启动
	 *
	 * @return mixed
	 */
	public function cron()
	{
		$where['cron_type'] = 1;
		$log = Dever::db('score/action_log')->all($where);
		if ($log) {
			foreach ($log as $k => $v) {
				$this->operAction($v);
			}
		}
	}

	/**
	 * 根据积分算等级
	 *
	 * @return mixed
	 */
	public function setLevel($uid)
	{
		# 获取当前用户的等级
		$where['uid'] = $uid;
		$user_level = Dever::db('score/user_level')->one($where);
		if ($user_level && $user_level['type'] == 2) {
			return;
		} elseif ($user_level && $user_level['type'] == 1) {
			$config = Dever::db('score/level')->one($user_level['level_id']);
			$level = $config['level'] + 1;
		} else {
			$level = 1;
		}

		$config = Dever::db('score/level')->one(array('level' => $level, 'state' => 1));

		if ($config) {
			# 是否能升级
			$yes = false;
			$rule = Dever::array_decode($config['score']);
			if ($rule) {
				$user_score = Dever::db('score/user')->getScoreByConfig($where);
				if ($user_score) {
					foreach ($rule as $k => $v) {
						$score = $v['config'];
						$num = $v['num'];
						if (isset($user_score[$score]) && $user_score[$score]['score'] && $user_score[$score]['score'] >= $num) {
							$yes = $config['id'];
						} else {
							$yes = false;
						}
					}
				}
			}

			if ($yes) {
				# 可以升级
				if ($user_level) {
					Dever::db('score/user_level')->update(array('where_id' => $user_level['id'], 'level_id' => $yes));
				} else {
					Dever::db('score/user_level')->insert(array('uid' => $uid,'level_id' => $yes, 'type' => 1));
				}
			}
		}
	}
}