123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- <?php
- namespace Account\Lib;
- use Dever;
- class Profit
- {
- # 获取详情
- public function get($uid, $profit_id, $config_id, $project_id)
- {
- $data['uid'] = $uid;
- $data['config_id'] = $config_id;
- $data['project_id'] = $project_id;
- $data['config_profit_id'] = $profit_id;
- $data['clear'] = true;
- $info = Dever::db('account/profit')->find($data);
- if (!$info) {
- $info['id'] = Dever::db('account/profit')->insert($data);
- if (!$info['id']) {
- return Dever::alert('账户信息不存在');
- }
- $info['cash'] = '0.00';
- $info['ycash'] = '0.00';
- }
- return $info;
- }
- # 获取日志列表
- public function getList($uid, $profit_id)
- {
- $where['profit_id'] = $profit_id;
- $where['uid'] = $uid;
- $data = Dever::db('account/profit_log')->getData($where, function(&$info) {
- return $this->info($info);
- });
- return $data;
- }
- # 获取日志详情
- public function getInfo($id)
- {
- $data = Dever::db('account/profit_log')->find($id);
- if (!$data) {
- return false;
- }
- $data = $this->info($data);
- return $data;
- }
- # 获取详情
- private function info($info)
- {
- $info['username'] = '无';
- $profit = Dever::db('account/profit')->find($info['profit_id']);
- $status = Dever::db('account/profit_log')->config['status'];
- $config = Dever::db('account/config')->find($profit['config_id']);
- $project = Dever::db('account/config_project')->find($config['project_id']);
- //$source = Dever::db($project['source'])->find($info['uid']);
- $source = Dever::db($info['source'])->find($info['source_id']);
- if ($source) {
- $user = Dever::db($project['source'])->find($source['uid']);
- $info['username'] = $user['name'];
- $info['avatar'] = $user['avatar'];
- }
- $info['config_name'] = $config['name'];
- $info['status_name'] = Dever::status($status, $info['status']);
- $info['status_desc'] = '';
- if ($info['status'] == 1) {
- $info['status_desc'] = '未到账原因:需要等到购物订单确认收货后,收益即可到账';
- } elseif ($info['status'] == 4) {
- $info['status_desc'] = '失败原因:该订单商品已全部完成退款';
- } elseif ($info['status'] == 3) {
- $info['status_desc'] = '部分到账原因:有部分商品已退款,所以仅发放未退款的那部分收益';
- }
- $info['cdate_string'] = date('Y-m-d H:i', $info['cdate']);
- if ($info['fdate']) {
- $info['fdate_string'] = date('Y-m-d H:i', $info['fdate']);
- }
-
- return $info;
- }
- # 入账
- public function up_commit($uid, $cash, $profit, $status, $source, $source_id, $name = '', $desc = '')
- {
- if ($cash == 0) {
- return Dever::alert('金额不能为空0');
- }
- if (!is_array($profit)) {
- $profit = Dever::db('account/config_profit')->find(array('key' => $profit));
- }
-
- if (!$profit) {
- return Dever::alert('收益类型不存在');
- }
- $config = Dever::db('account/config')->find($profit['config_id']);
- if (!$config) {
- return Dever::alert('账户信息不存在');
- }
- $project = Dever::db('account/config_project')->find($config['project_id']);
- $user = Dever::db($project['source'])->find($uid);
- if (!$user) {
- return Dever::alert('账户信息不存在');
- }
- $info = $this->get($uid, $profit['id'], $config['id'], $config['project_id']);
- # 开始处理入账
- $this->handle($profit, $config, $uid, $cash, $info['id'], $project['source'], $status, $source, $source_id, $name, $desc);
-
- return true;
- }
- # 生成订单号
- private function createOrderNum()
- {
- $where['order_num'] = Dever::order('S');
- $state = Dever::db('account/profit_log')->one($where);
- if (!$state) {
- return $where['order_num'];
- } else {
- return $this->createOrderNum();
- }
- }
- # 计算金额
- private function handle($profit, $config, $uid, $cash, $profit_id, $project_source, $status, $source, $source_id, $name, $desc)
- {
- $rule = Dever::db('account/config_profit_rule')->select(array('profit_id' => $profit['id']));
- if ($rule) {
- $child = array();
- foreach ($rule as $k => $v) {
- $child[$v['level']] = $this->rule($v, $child, $config, $uid, $cash, $profit_id, $project_source, $status, $source, $source_id, $name, $desc);
- }
- }
- return $cash;
- }
- private function rule($rule, $child, $config, $uid, $cash, $profit_id, $project_source, $status, $source, $source_id, $name, $desc)
- {
- if ($rule['level'] > 0) {
- $parent = Dever::load('invite/api')->getParent($uid, $rule['level']);
- if ($parent) {
- $uid = $parent;
- } else {
- return false;
- }
- }
- $replace = array();
- $replace['{a}'] = $cash;
- $replace['{b}'] = $uid;
- $user = Dever::db($project_source)->find($uid);
- if (isset($user['role']) && $user['role']) {
- $temp = explode(',', $user['role']);
- $replace['{c}'] = $temp[0];
- if (isset($temp[1]) && $temp[1]) {
- $level = $temp[1];
- $level = Dever::db('user/role_level')->find($level);
- if ($level) {
- $replace['{d}'] = $level['level'];
- }
- }
- }
- # 验证条件
- $condition = $rule['condition'];
- if ($condition) {
- $check = $this->run($condition, $replace);
- if (!$check) {
- return false;
- }
- }
- $level = $rule['level'] - 1;
- if ($child && isset($child[$level]) && $child[$level]) {
- $replace['{e}'] = $child[$level];
- }
- # 计算公式
- $exp = $rule['exp'];
- if ($exp) {
- $cash = $this->run($exp, $replace);
- }
- if ($cash > 0) {
- $this->add($config, $uid, $cash, $profit_id, $status, $source, $source_id, $name, $desc);
- return $cash;
- }
- return false;
- }
- private function run($string, $replace)
- {
- $keys = array_keys($replace);
- $values = array_values($replace);
- $string = str_replace($keys, $values, $string);
- $eval = '$value = ' . $string . ';';
- eval($eval);
- return $value;
- }
- # 入账
- private function add($config, $uid, $cash, $profit_id, $status, $source, $source_id, $name = '', $desc = '')
- {
- $data['clear'] = true;
- $data['profit_id'] = $profit_id;
- $data['source'] = $source;
- $data['source_id'] = $source_id;
-
- $log = Dever::db('account/profit_log')->one($data);
- $data['uid'] = $uid;
- if ($name) {
- $data['name'] = $name;
- }
- if ($desc) {
- $data['desc'] = $desc;
- }
- $data['status'] = $status;
- if ($log) {
- # 结算
- if ($status < 5 && $cash > 0 && $log['cash'] != $cash) {
- $status = 3;
- if ($cash > $log['cash']) {
- $cash = $log['cash'];
- }
- }
- if ($status == 1) {
- $data['cash'] = $cash;
- } elseif ($status == 2) {
- $data['ycash'] = $cash;
- } elseif ($status == 3) {
- $data['ycash'] = $log['ycash'] + $cash;
- } elseif ($status == 4) {
- $data['ycash'] = 0;
- } elseif ($status == 5) {
- if ($log['status'] == 4) {
- return;
- }
- $data['status'] = 3;
- if ($cash == $log['cash']) {
- $data['status'] = 4;
- }
- $data['tcash'] = $cash;
- }
- $id = $data['where_id'] = $log['id'];
- Dever::db('account/profit_log')->update($data);
- } else {
- if ($status >= 4) {
- return;
- }
- # 入账
- $data['cash'] = $cash;
- if ($status == 2) {
- $data['ycash'] = $cash;
- }
- $data['order_num'] = $this->createOrderNum();
- $id = Dever::db('account/profit_log')->insert($data);
- }
- if ($id && $id > 0) {
- if (isset($data['cash']) && $data['cash']) {
- $update['where_id'] = $profit_id;
- $update['set_cash'] = $data['cash'];
- Dever::db('account/profit')->incCash($update);
- }
- if (isset($data['ycash']) && $data['ycash']) {
- $update['where_id'] = $profit_id;
- $update['set_ycash'] = $data['ycash'];
- Dever::db('account/profit')->incYcash($update);
- # 同步钱包
- Dever::load('account/lib/info.up_commit', $uid, $data['ycash'], 'shouyi', $config, $name, $source, $source_id, 1);
- }
- if (isset($data['tcash']) && $data['tcash']) {
- $update['where_id'] = $profit_id;
- $update['set_tcash'] = $data['tcash'];
- Dever::db('account/profit')->incTcash($update);
- # 同步钱包
- Dever::load('account/lib/info.up_commit', $uid, $data['tcash'], 'shouyi_tuikuan', $config, $name, $source, $source_id, 1);
- }
- }
- }
- }
|