Files
amb_wechatapp/application/admin/controller/app/agent/Member.php
T

222 lines
6.0 KiB
PHP

<?php
namespace app\admin\controller\app\agent;
use app\common\controller\Backend;
use app\admin\model\app\agent\Relation as RelationModel;
use think\Db;
/**
* 分销成员
*
* @icon fa fa-circle-o
*/
class Member extends Backend
{
/**
* Member模型对象
* @var \app\admin\model\app\agent\Member
*/
protected $model = null;
public function _initialize()
{
parent::_initialize();
\app\common\model\fill\Handle::check('app_config','agent');
$this->model = new \app\admin\model\app\agent\Member;
}
/**
* 查看
*/
public function index()
{
//当前是否为关联查询
$this->relationSearch = true;
//设置过滤方法
$this->request->filter(['strip_tags', 'trim']);
//如果发送的来源是Selectpage,则转发到Selectpage
if ($this->request->request('keyField')) {
return $this->selectpage();
}
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$orderModel = new \app\admin\model\order\Order();
$parentWhere = [];
$relationusername = input('relationusername');
if($relationusername){
//获取符合规则的上级用户ID集合
$userIds = \app\admin\model\User::where([
'username'=>['like',"%{$relationusername}%"]
])->field("id")->select();
if($userIds){
foreach ($userIds as $id){
$ids[] = $id['id'];
}
$parentWhere['relation.parent_id'] = ['in',$ids];
}
}
$list = $this->model
->with(['user','relation'])
->where($where)
->where($parentWhere)
->order($sort, $order)
->paginate($limit);
$memberCommonModel = (new \app\common\model\app\agent\Member);
foreach ($list as &$row) {
if($row->relation){
$row->relation->parent = \app\admin\model\User::where([
'id'=>$row->relation->parent_id
])->find();
}
$row->getRelation('user')->visible(\app\admin\model\User::$listShowField);
$row->condition = $orderModel->getTransactionStatistic($row->user_id);
$row->statistics = $memberCommonModel->getMemberStatistics($row->user_id);
}
$result = array("total" => $list->total(), "rows" => $list->items());
$result['attr'] = [
'auditList'=>$this->model->getAuditList(),
'level'=>(new \app\common\model\app\agent\Level)->getLevelList()
];
return $this->success("获取成功",$result);
}
/**
* 获取分销员详情
* @return mixed
*/
public function getDetail(){
$id = $this->request->post("id");
$data = $this->model->with(['user','relation.parent'])->where([
'member.id'=>$id
])->find();
if(!$data){
return $this->error("获取信息失败,请刷新重试");
}
if($data['level'] == 0){
$data['level'] = intval($data['level']);
}
$memberCommonModel = (new \app\common\model\app\agent\Member);
$data['statistics'] = $memberCommonModel->getMemberStatistics($data['user_id']);
return $this->success("获取成功",$data);
}
/**
* 编辑
*
* @param $ids
* @return string
* @throws DbException
* @throws \think\Exception
*/
public function edit($ids = null)
{
$row = $this->model->get($ids);
if (!$row) {
$this->error(__('No Results were found'));
}
$adminIds = $this->getDataLimitAdminIds();
if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
$this->error(__('You have no permission'));
}
$params = $this->request->post('row/a');
if (empty($params)) {
$this->error(__('Parameter %s can not be empty', ''));
}
$params = $this->preExcludeFields($params);
$parent_id = 0;
if(isset($params['parent_id'])){
$parent_id = $params['parent_id'];
unset($params['parent_id']);
}
$result = false;
Db::startTrans();
try {
//是否采用模型验证
if ($this->modelValidate) {
$name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
$row->validateFailException()->validate($validate);
}
$result = $row->allowField(true)->save($params);
//修改上级用户ID
(new RelationModel)->setRalation($params['user_id'],$parent_id);
Db::commit();
} catch (ValidateException|PDOException|Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if (false === $result) {
$this->error(__('No rows were updated'));
}
$this->success("提交成功");
}
/**
* 分销员审核
* @return void
*/
public function audit(){
$ids = $this->request->post("ids/a");
$audit = $this->request->post("audit");
if(!is_array($ids)){
$ids = [$ids];
}
$count = 0;
foreach ($ids as $id){
$data = $this->model->where([
'id'=>$id
])->find();
if(!$data){
continue;
}
if($audit == 1){
//通过申请
$data->status = 1;
$data->audit = 1;
}else{
//驳回
$data->audit = 3;
}
$ret = $data->save();
if($ret){
$count++;
}
}
return $this->success("已变更{$count}条数据");
}
}