Csrkpi.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace app\admin\controller\kefu;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\Exception;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. /**
  9. * 客服代表(csr)
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class Csrkpi extends Backend
  14. {
  15. /**
  16. * KeFuCsrKpi模型对象
  17. * @var \app\admin\model\KeFuCsrKpi
  18. */
  19. protected $model = null;
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = new \app\admin\model\KeFuCsrKpi;
  24. $this->view->assign("statusList", $this->model->getStatusList());
  25. }
  26. /**
  27. * 添加
  28. */
  29. public function add()
  30. {
  31. if ($this->request->isPost()) {
  32. $params = $this->request->post("row/a");
  33. if ($params) {
  34. $params = method_exists($this, 'preExcludeFields') ? $this->preExcludeFields($params) : $params;
  35. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  36. $params[$this->dataLimitField] = $this->auth->id;
  37. }
  38. if (Db::name('kefu_csr_config')->where('admin_id', $params['admin_id'])->value('id')) {
  39. $this->error('客服代表已经存在了~');
  40. }
  41. if ($params['keep_alive']) {
  42. $params['status'] = 3;
  43. }
  44. $result = false;
  45. Db::startTrans();
  46. try {
  47. //是否采用模型验证
  48. if ($this->modelValidate) {
  49. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  50. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  51. $this->model->validateFailException(true)->validate($validate);
  52. }
  53. $result = $this->model->allowField(true)->save($params);
  54. Db::commit();
  55. } catch (ValidateException $e) {
  56. Db::rollback();
  57. $this->error($e->getMessage());
  58. } catch (PDOException $e) {
  59. Db::rollback();
  60. $this->error($e->getMessage());
  61. } catch (Exception $e) {
  62. Db::rollback();
  63. $this->error($e->getMessage());
  64. }
  65. if ($result !== false) {
  66. $this->success();
  67. } else {
  68. $this->error(__('No rows were inserted'));
  69. }
  70. }
  71. $this->error(__('Parameter %s can not be empty', ''));
  72. }
  73. return $this->view->fetch();
  74. }
  75. /**
  76. * 编辑
  77. */
  78. public function edit($ids = null)
  79. {
  80. $row = $this->model->get($ids);
  81. if (!$row) {
  82. $this->error(__('No Results were found'));
  83. }
  84. $adminIds = $this->getDataLimitAdminIds();
  85. if (is_array($adminIds)) {
  86. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  87. $this->error(__('You have no permission'));
  88. }
  89. }
  90. if ($this->request->isPost()) {
  91. $params = $this->request->post("row/a");
  92. if ($params) {
  93. $params = method_exists($this, 'preExcludeFields') ? $this->preExcludeFields($params) : $params;
  94. if ($params['admin_id'] != $row['admin_id'] && Db::name('kefu_csr_config')
  95. ->where('admin_id', $params['admin_id'])
  96. ->value('id')) {
  97. $this->error('客服代表已经存在了~');
  98. }
  99. if ($params['keep_alive']) {
  100. $params['status'] = 3;
  101. }
  102. $result = false;
  103. Db::startTrans();
  104. try {
  105. //是否采用模型验证
  106. if ($this->modelValidate) {
  107. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  108. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  109. $row->validateFailException(true)->validate($validate);
  110. }
  111. $result = $row->allowField(true)->save($params);
  112. Db::commit();
  113. } catch (ValidateException $e) {
  114. Db::rollback();
  115. $this->error($e->getMessage());
  116. } catch (PDOException $e) {
  117. Db::rollback();
  118. $this->error($e->getMessage());
  119. } catch (Exception $e) {
  120. Db::rollback();
  121. $this->error($e->getMessage());
  122. }
  123. if ($result !== false) {
  124. $this->success();
  125. } else {
  126. $this->error(__('No rows were updated'));
  127. }
  128. }
  129. $this->error(__('Parameter %s can not be empty', ''));
  130. }
  131. $this->view->assign("row", $row);
  132. return $this->view->fetch();
  133. }
  134. /**
  135. * 查看
  136. */
  137. public function index()
  138. {
  139. //当前是否为关联查询
  140. $this->relationSearch = true;
  141. //设置过滤方法
  142. $this->request->filter(['strip_tags']);
  143. if ($this->request->isAjax()) {
  144. //如果发送的来源是Selectpage,则转发到Selectpage
  145. if ($this->request->request('keyField')) {
  146. return $this->selectpage();
  147. }
  148. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  149. $total = $this->model->with(['admin'])->where($where)->order($sort, $order)->count();
  150. $list = $this->model->with(['admin'])
  151. ->where($where)
  152. ->order($sort, $order)
  153. ->limit($offset, $limit)
  154. ->select();
  155. foreach ($list as $row) {
  156. $row->getRelation('admin')->visible(['nickname']);
  157. }
  158. $list = collection($list)->toArray();
  159. $result = ["total" => $total, "rows" => $list];
  160. return json($result);
  161. }
  162. return $this->view->fetch();
  163. }
  164. }