| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 | <?phpnamespace 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']);        if ($source) {            $info['username'] = $source[$project['source_name']];        }        $info['config_name'] = $config['name'];        $info['status_name'] = Dever::status($status, $info['status']);        $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, $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, $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, $uid, $cash, $profit_id, $project_source, $status, $source, $source_id, $name, $desc);            }        }        return $cash;    }    private function rule($rule, $child, $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($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($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 ($cash > 0 && $log['cash'] != $cash) {                $status = 3;                if ($cash > $log['cash']) {                    $cash = $log['cash'];                }            }            echo $status;die;            if ($status == 2) {                $data['ycash'] = $cash;            } elseif ($status == 3) {                $data['ycash'] = $log['ycash'] + $cash;            } elseif ($status == 4) {                $data['ycash'] = 0;            }            $data['where_id'] = $log['id'];            Dever::db('account/profit_log')->update($data);            $update['where_id'] = $profit_id;            $update['set_ycash'] = $data['ycash'];            Dever::db('account/profit')->incYcash($update);        } else {            # 入账            $data['cash'] = $cash;            if ($status == 2) {                $data['ycash'] = $cash;            }            $data['order_num'] = $this->createOrderNum();            $id = Dever::db('account/profit_log')->insert($data);            $update['where_id'] = $profit_id;            $update['set_cash'] = $data['cash'];            Dever::db('account/profit')->incCash($update);        }    }}
 |