Agentcredit.php 10.9 KB
<?php

namespace app\admin\controller\agent;

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

/**
 * 用户信用管理
 *
 * @icon fa fa-circle-o
 */
class Agentcredit extends Backend
{

    /**
     * Agentcredit模型对象
     * @var \app\admin\model\agent\Agentcredit
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\agent\Agentcredit;
        $this->view->assign("acTypeList", $this->model->getAcTypeList());
        $this->view->assign("acStuList", $this->model->getAcStuList());
        $actype = $this->request->param('actype', 0);//分类
        //选择的企业id
        $agentListIds = $this->request->param('ids', 0);
        if ($actype == 0) {
            $ginfo = Db::name('agent_list')
                ->where(['id' => $agentListIds])
                ->find();
        } else {
            $ginfo = [];
        }
        //门店信息
        if ($actype == 1) {
            $sinfo = Db::name('agent_shop')
                ->where(['id' => $agentListIds])
                ->find();
            //门店所属企业信息
            $ginfo = Db::name('agent_list')
                ->where(['id' => $sinfo['agent_list_id']])
                ->find();
        } else {
            $sinfo = [];
        }
        //经纪人信息
        if ($actype == 2) {
            $jinfo = Db::name('agent_users')
                ->where(['id' => $agentListIds])
                ->find();
            //所属企业信息
            $ginfo = Db::name('agent_list')
                ->where(['id' => $jinfo['agent_list_id']])
                ->find();
            //所属门店
            $sinfo = Db::name('agent_shop')
                ->where(['id' => $jinfo['agent_shop_id']])
                ->find();
        } else {
            $jinfo = [];
        }
        $this->view->assign("ginfo", $ginfo);
        $this->view->assign("sinfo", $sinfo);
        $this->view->assign("jinfo", $jinfo);
        $this->view->assign('actype', $actype);
    }


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

            $list = $this->model
                ->where($where)
                ->where($wh)
                ->order($sort, $order)
                ->paginate($limit);

            $rows = $list->items();
            foreach ($rows as $kk => $vv) {
                //关联企业、门店、经纪人信息
                $ginfo = Db::name('agent_list')
                    ->where(['id' => $vv['agent_list_id']])
                    ->field('id,agent_name')
                    ->find();
                $rows[$kk]['agent_name'] = $ginfo['agent_name'] ? $ginfo['agent_name'] : '-';

                $sinfo = Db::name('agent_shop')
                    ->where(['id' => $vv['agent_shop_id']])
                    ->field('id,shop_name')
                    ->find();
                $rows[$kk]['shop_name'] = $sinfo['shop_name'] ? $sinfo['shop_name'] : '-';
                $jinfo = Db::name('agent_users')
                    ->where(['id' => $vv['agent_user_id']])
                    ->field('id,jjr_name')
                    ->find();
                $rows[$kk]['jjr_name'] = $jinfo['jjr_name'] ? $jinfo['jjr_name'] : '-';
            }

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

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

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $params = $this->preExcludeFields($params);
                if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
                    $params[$this->dataLimitField] = $this->auth->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 . '.add' : $name) : $this->modelValidate;
                        $this->model->validateFailException(true)->validate($validate);
                    }
                    $params['admin'] = $this->auth->id;
                    //计算之前和之后的信用分数
                    if ($params['ac_type'] == 0) {
                        if (empty($params['agent_list_id'])) {
                            $this->error('请选择企业信息');
                        }
                        //企业原信用分
                        $ginfo = Db::name('agent_list')
                            ->where(['id' => $params['agent_list_id']])
                            ->find();
                        $params['change_before'] = $ginfo['credit'];
                        $params['change_after'] = $ginfo['credit'] + $params['change_num'];
                    }
                    //门店的
                    if ($params['ac_type'] == 1) {
                        if (empty($params['agent_list_id'])) {
                            $this->error('请选择企业信息');
                        }
                        if (empty($params['agent_shop_id'])) {
                            $this->error('请选择门店信息');
                        }
                        //原信用分
                        $sinfo = Db::name('agent_shop')
                            ->where(['id' => $params['agent_shop_id']])
                            ->find();
                        $params['change_before'] = $sinfo['credit'];
                        $params['change_after'] = $sinfo['credit'] + $params['change_num'];
                    }
                    //经纪人的
                    if ($params['ac_type'] == 2) {
                        if (empty($params['agent_user_id'])) {
                            $this->error('请选择经纪人信息');
                        }
                        //原信用分
                        $jinfo = Db::name('agent_users')
                            ->where(['id' => $params['agent_user_id']])
                            ->find();
                        $params['change_before'] = $jinfo['credit'];
                        $params['change_after'] = $jinfo['credit'] + $params['change_num'];
                    }
                    $result = $this->model->allowField(true)->save($params);
                    //更新企业、门店、经纪人表的信用分数
                    $agentnum['credit'] = $params['change_after'];
                    $agentnum['updatetime'] = time();
                    if ($params['ac_type'] == 0) {
                        Db::name('agent_list')
                            ->where(['id' => $params['agent_list_id']])
                            ->update($agentnum);
                    }
                    if ($params['ac_type'] == 1) {
                        Db::name('agent_shop')
                            ->where(['id' => $params['agent_shop_id']])
                            ->update($agentnum);
                    }
                    if ($params['ac_type'] == 2) {
                        Db::name('agent_users')
                            ->where(['id' => $params['agent_user_id']])
                            ->update($agentnum);
                    }
                    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) {
                    $this->success();
                } else {
                    $this->error(__('No rows were inserted'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        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) {
                    $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();
    }

}