alias('agent')->field(['agent.*,user.user_name'])->where('is_recycle', '=', (int)$is_recycle) ->join('dlagent_user user', 'user.agent_id = agent.agent_id','left') ->where('agent.parent_id', '=', $agent_id) ->where('user.is_super', '=', 1) ->where('agent.is_delete', '=', 0) ->order(['create_time' => 'asc']) ->paginate($limit); $data->each(function($item,$key){ $item['lastlogin_time']=$this->getlastLogin($item['agent_id']); $item['client_num']=$this->getClinetTotal($item['agent_id']); }); return $data; } /** * 新增记录 */ public function add($data) { if ($data['password'] !== $data['password_confirm']) { $this->error = '确认密码不正确'; return false; } if (AgentUser::checkExist($data['user_name'])) { $this->error = '代理商用户名已存在'; return false; } $this->startTrans(); try { // 添加小程序记录 $data['parent_id']=self::$agent_id; $this->save($data); // 新增商家用户信息 $AgentUser = new AgentUser; if (!$AgentUser->dladd($this['agent_id'], $data)) { $this->error = $AgentUser>error; return false; } // // 新增应用diy配置 // (new PageModel)->insertDefault($this['agent_id']); // // 默认等级 // (new GradeModel)->insertDefault($this['agent_id']); $this->commit(); return true; } catch (\Exception $e) { $this->error = $e->getMessage(); $this->rollback(); return false; } } /** * 修改记录 */ public function edit($data) { $this->startTrans(); try { $save_data = [ 'agent_name' => $data['agent_name'], 'agent_phone' => $data['agent_phone'], 'email' => $data['email'], 'duetime'=>$data['duetime'] ]; $this->save($save_data); $user_data = [ 'user_name' => $data['user_name'] ]; if (!empty($data['password'])) { $user_data['password'] = salt_hash($data['password']); } $agent_user = (new AgentUser())->where('agent_id', '=', $this['agent_id'])->where('is_super', '=', 1)->find(); if($agent_user['user_name'] != $data['user_name']){ if (AgentUser::checkExist($data['user_name'])) { $this->error = '代理商用户名已存在'; return false; } } $agent_user->save($user_data); $this->commit(); return true; } catch (\Exception $e) { $this->error = $e->getMessage(); $this->rollback(); return false; } } /** * 移入移出回收站 */ public function recycle($is_recycle = true) { return $this->save(['is_recycle' => (int)$is_recycle]); } /** * 软删除 */ public function setDelete() { return $this->save(['is_delete' => 1]); } /** * 用户充值 */ public function recharge($storeUserName, $source, $data) { if ($source == 0) { return $this->rechargeToBalance($storeUserName, $data['balance']); } elseif ($source == 1) { return $this->rechargeToPoints($storeUserName, $data['points']); } return false; } /** * 用户充值:余额 */ private function rechargeToBalance($storeUserName, $data) { if (!isset($data['money']) || $data['money'] === '' || $data['money'] < 0) { $this->error = '请输入正确的金额'; return false; } // 判断充值方式,计算最终金额 $money = 0; if ($data['mode'] === 'inc') { $diffMoney = $this['balance'] + $data['money']; $money = $data['money']; } elseif ($data['mode'] === 'dec') { $diffMoney = $this['balance'] - $data['money'] <= 0 ? 0 : $this['balance'] - $data['money']; $money = -$data['money']; } else { $diffMoney = $data['money']; $money = $diffMoney - $this['balance']; } // 更新记录 $this->transaction(function () use ($storeUserName, $data, $diffMoney, $money) { // 更新账户余额 $this->where('agent_id', '=', $this['agent_id'])->update(['balance' => $diffMoney]); // 新增余额变动记录 BalanceLogModel::add(30, [ 'agent_id' => $this['agent_id'], 'money' => $money, 'remark' => $data['remark'], ], [$storeUserName]); }); return true; } // /** * 获取最后登录时间 */ public function getlastLogin($agent_id) { $model = new LoginLogModel; $model = $model->where('agent_id', '=', $agent_id)->order(['create_time' => 'desc'])->find(); return $model['create_time']; } //获取客户总数 public function getClinetTotal($agent_id){ $ClientModel = new ClientModel; $list = $ClientModel->where('agent_id','=',$agent_id)->where('is_delete', '=', 0)->select(); //dd($agent_id); return count($list); } //获取余额 public function getBalanceTotal($agent_id){ $balance = $this->where('agent_id','=',$agent_id)->where('is_delete', '=', 0)->find(); //dd($agent_id); return $balance['balance']; } //获取下级代理商总数 public function getAgentTotal($agent_id){ $list = $this->where('parent_id','=',$agent_id)->where('is_delete', '=', 0)->select(); return count($list); } //获取余额变动 public function getBalanceByDay($agent_id){ $list = []; $balancelog = new BalanceLogModel; $today = strtotime(date('Y-m-d')); $list['tday'] = $balancelog ->where('agent_id','=',$agent_id)->where('create_time','>=',$today) ->sum('money'); $list['ytd'] = $balancelog ->where('agent_id','=',$agent_id)->where('create_time','<',$today) ->where('create_time','>=',$today - 86400) ->sum('money'); $list['tday'] = number_format($list['tday']); $list['ytd'] = number_format($list['ytd']); return $list; } }