254 lines
9.4 KiB
PHP
254 lines
9.4 KiB
PHP
<?php
|
||
|
||
namespace app\admin\controller\user4s;
|
||
|
||
use app\common\controller\Backend;
|
||
use Exception;
|
||
use think\Db;
|
||
use think\exception\PDOException;
|
||
use think\exception\ValidateException;
|
||
|
||
/**
|
||
* 回访记录
|
||
*
|
||
* @icon fa fa-circle-o
|
||
*/
|
||
class Visitlog extends Backend
|
||
{
|
||
|
||
/**
|
||
* Visitlog模型对象
|
||
* @var \app\common\model\user4s\Visitlog
|
||
*/
|
||
protected $model = null;
|
||
protected $searchFields = 'id,description';
|
||
|
||
public function _initialize()
|
||
{
|
||
parent::_initialize();
|
||
$this->model = new \app\common\model\user4s\Visitlog;
|
||
$this->visit_model = new \app\common\model\user4s\Visit;
|
||
$this->type_model = new \app\common\model\user4s\Visittype;
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
|
||
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
|
||
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
|
||
*/
|
||
|
||
|
||
/**
|
||
* 查看
|
||
*/
|
||
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(['user','visit','visittype'])
|
||
->where($where)
|
||
->order($sort, $order)
|
||
->paginate($limit);
|
||
|
||
foreach ($list as $row) {
|
||
|
||
|
||
}
|
||
|
||
$result = array("total" => $list->total(), "rows" => $list->items());
|
||
|
||
return json($result);
|
||
}
|
||
return $this->view->fetch();
|
||
}
|
||
|
||
/**
|
||
* 添加
|
||
*/
|
||
public function add()
|
||
{
|
||
if ($this->request->isPost()) {
|
||
$this->token();
|
||
}
|
||
$all_type = $this->type_model->where('deletetime', null)->column('id,name');
|
||
$this->view->assign("all_type", $all_type);
|
||
$all_star = array(
|
||
1=>'★',
|
||
2=>'★★',
|
||
3=>'★★★',
|
||
4=>'★★★★',
|
||
5=>'★★★★★',
|
||
);
|
||
$this->view->assign("all_star", $all_star);
|
||
if (!$this->request->isPost()) {
|
||
$visit_id = $this->request->get("vid/d");
|
||
if ($visit_id > 0) {
|
||
$visit=$this->visit_model->get($visit_id,['user','visittype']);
|
||
if(isset($visit->id)){
|
||
$this->view->assign("row", $visit);
|
||
}else{
|
||
$this->error("回访单不存在!");
|
||
}
|
||
}else{
|
||
$this->error("请选择要记录的回访单");
|
||
}
|
||
}
|
||
if ($this->request->isPost()) {
|
||
$params = $this->request->post("row/a");
|
||
if ($params) {
|
||
$visi_time = strtotime($params['visit_time'].' 00:00:00');
|
||
$params['visit_time'] = $visi_time;
|
||
//处理结单等信息
|
||
if($params['visit_id'] > 0){
|
||
$visit_data = array(
|
||
'id'=>$params['visit_id'],
|
||
//'star'=>$params['star'],
|
||
//'visittime'=>strtotime($params['visit_time']),
|
||
'admin_id'=>$this->auth->id,
|
||
'status'=>$params['is_end']==1?1:0,
|
||
'endtime'=>$params['is_end']==1?time():null,
|
||
);
|
||
//var_dump($visit_data);exit();
|
||
$this->visit_model->where('id', $params['visit_id'])->update($visit_data);
|
||
// if($visit_id == false){
|
||
// $this->error("更新回访单失败!");
|
||
// }
|
||
}
|
||
$params = $this->preExcludeFields($params);
|
||
$params['admin_id'] = $this->auth->id;
|
||
// if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
|
||
// $params[$this->dataLimitField] = $this->auth->id;
|
||
// }
|
||
|
||
$result = false;
|
||
$this->model->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);
|
||
}
|
||
$result = $this->model->allowField(true)->save($params);
|
||
$this->model->commit();
|
||
} catch (ValidateException $e) {
|
||
$this->model->rollback();
|
||
$this->error($e->getMessage());
|
||
} catch (PDOException $e) {
|
||
$this->model->rollback();
|
||
$this->error($e->getMessage());
|
||
} catch (Exception $e) {
|
||
$this->model->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 parent::add();
|
||
}
|
||
/**
|
||
* 编辑
|
||
*/
|
||
public function edit($ids = null)
|
||
{
|
||
$all_type = $this->type_model->where('deletetime', null)->column('id,name');
|
||
$this->view->assign("all_type", $all_type);
|
||
$all_star = array(
|
||
1=>'★',
|
||
2=>'★★',
|
||
3=>'★★★',
|
||
4=>'★★★★',
|
||
5=>'★★★★★',
|
||
);
|
||
$this->view->assign("all_star", $all_star);
|
||
$row = $this->model->get($ids,['visittype', 'user', 'visit','admin']);
|
||
if (!$row) {
|
||
$this->error(__('No Results were found'));
|
||
}
|
||
if (empty($row->visit->id)) {
|
||
$this->error(__('没找到对应的回访单!'));
|
||
}
|
||
if (empty($row->user->id)) {
|
||
$this->error(__('没找到对应的客户!'));
|
||
}
|
||
$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) {
|
||
$visi_time = strtotime($params['visit_time'].' 00:00:00');
|
||
$params['visit_time'] = $visi_time;
|
||
//处理结单等信息
|
||
if($params['visit_id'] > 0){
|
||
$visit_data = array(
|
||
'id'=>$params['visit_id'],
|
||
//'star'=>$params['star'],
|
||
//'visittime'=>strtotime($params['visit_time']),
|
||
'admin_id'=>$this->auth->id,
|
||
'status'=>$params['is_end']==1?1:0,
|
||
'endtime'=>$params['is_end']==1?time():null,
|
||
);
|
||
//var_dump($visit_data);exit();
|
||
$this->visit_model->where('id', $params['visit_id'])->update($visit_data);
|
||
//$result = $row->allowField(true)->save($params);
|
||
}
|
||
unset($params['is_end']);
|
||
//var_dump($params);exit();
|
||
$params = $this->preExcludeFields($params);
|
||
$result = false;
|
||
$this->model->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);
|
||
}
|
||
//var_dump($params);exit();
|
||
$result = $row->allowField(true)->save($params);
|
||
$this->model->commit();
|
||
} catch (ValidateException $e) {
|
||
$this->model->rollback();
|
||
$this->error($e->getMessage());
|
||
} catch (PDOException $e) {
|
||
$this->model->rollback();
|
||
$this->error($e->getMessage());
|
||
} catch (Exception $e) {
|
||
$this->model->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();
|
||
}
|
||
|
||
}
|