<?php
|
|
namespace app\admin\model;
|
|
use app\admin\model\page\Page as PageModel;
|
use app\common\model\shop\Agent as AgentModel;
|
use app\common\model\agent\User as AgentUser;
|
use app\admin\model\user\Grade as GradeModel;
|
|
use app\common\model\agent\LoginLog as LoginLogModel;
|
use app\common\model\agent\Client as ClientModel;
|
|
class Agent extends AgentModel
|
{
|
/**
|
* 获取小程序列表
|
*/
|
public function getList($limit, $is_recycle = false)
|
{
|
|
$data=$this->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', '=', 0)
|
->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 {
|
// 添加小程序记录
|
$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'],
|
'level'=>$data['level']
|
];
|
$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 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);
|
}
|
}
|