Complainlist.php 6.6 KB
<?php

namespace app\admin\controller\agent;

use app\common\controller\Backend;
use think\Db;

/**
 * 用户投诉记录
 *
 * @icon fa fa-circle-o
 */
class Complainlist extends Backend
{

    /**
     * Complainlist模型对象
     * @var \app\admin\model\agent\Complainlist
     */
    protected $model = null;
    protected $dataLimit = 'auth';
    protected $dataLimitField = 'admin_id';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\agent\Complainlist;
        $this->view->assign("typeList", $this->model->getTypeList());
        $this->view->assign("statusList", $this->model->getStatusList());
        //判断是否是超级管理员
        if (in_array($this->auth->id, [1, 2])) {
            $isSup = 1;
        } else {
            $isSup = 0;
        }
        $this->view->assign("isSup", $isSup);
    }

    public function import()
    {
        parent::import();
    }


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage,则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->with(['agentlist', 'agentshop', 'agentusers', 'complaincat', 'user'])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);

            foreach ($list as $row) {
                $row->visible(['id', 'user_id', 'agent_list_id', 'agent_shop_id', 'agent_user_id', 'type', 'askcat', 'asktit', 'createtime', 'status']);
                $row->visible(['agentlist']);
                $row->getRelation('agentlist')->visible(['agent_name']);
                $row->visible(['agentshop']);
                $row->getRelation('agentshop')->visible(['shop_name']);
                $row->visible(['agentusers']);
                $row->getRelation('agentusers')->visible(['jjr_name']);
                $row->visible(['complaincat']);
                $row->getRelation('complaincat')->visible(['catname']);
                $row->visible(['user']);
                $row->getRelation('user')->visible(['nickname']);
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    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)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $params = $this->preExcludeFields($params);
                $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(true)->validate($validate);
                    }

                    $result = $row->allowField(true)->save($params);
                    Db::commit();
                } catch (ValidateException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (PDOException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    //审核通过,生成统计表
                    if ($params['status'] == '1') {
                        //判断分类
                        if ($params['type'] == '1') {
                            $wh['agent_list_id'] = ['=', $params['agent_list_id']];
                            $ctall['agent_list_id'] = $params['agent_list_id'];
                        }
                        if ($params['type'] == '2') {
                            $wh['agent_shop_id'] = ['=', $params['agent_shop_id']];
                            $ctall['agent_shop_id'] = $params['agent_shop_id'];
                        }
                        if ($params['type'] == '3') {
                            $wh['agent_user_id'] = ['=', $params['agent_user_id']];
                            $ctall['agent_user_id'] = $params['agent_user_id'];
                        }
                        $cks = Db::name('agent_user_complain_ctall')
                            ->where($wh)
                            ->find();
                        $cmpAll = Db::name('agent_user_complain_list')
                            ->where($wh)
                            ->where(['status' => '1'])
                            ->count();
                        if (empty($cks)) {
                            //生成统计信息
                            $ctall['type'] = $params['type'];
                            $ctall['compall'] = $cmpAll ? $cmpAll : 1;
                            $ctall['createtime'] = time();
                            $ctall['admin_id'] = $this->auth->id;
                            Db::name('agent_user_complain_ctall')->insertGetId($ctall);
                        } else {
                            //更新统计信息
                            $upall['compall'] = $cmpAll;
                            $upall['updatetime'] = time();
                            Db::name('agent_user_complain_ctall')
                                ->where($wh)
                                ->update($upall);
                        }
                    }
                    $this->success();
                } else {
                    $this->error(__('No rows were updated'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

}