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); } } } }