Clerk.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace app\shop\controller\store;
  3. use app\shop\controller\Controller;
  4. use app\shop\model\store\Clerk as ClerkModel;
  5. use app\shop\model\store\Store as StoreModel;
  6. /**
  7. * 店员控制器
  8. */
  9. class Clerk extends Controller
  10. {
  11. /**
  12. * 店员列表
  13. */
  14. public function index($store_id = 0, $search = '')
  15. {
  16. // 店员列表
  17. $model = new ClerkModel;
  18. $list = $model->getList(-1, $store_id, $search, $this->postData());
  19. // 门店列表
  20. $store_list = (new StoreModel)->getList();
  21. return $this->renderSuccess('', compact('list', 'store_list'));
  22. }
  23. /**
  24. * 添加店员
  25. */
  26. public function add()
  27. {
  28. $model = new ClerkModel;
  29. //传过来的信息
  30. $data = $this->postData();
  31. $list = $model->getAll()->toArray();
  32. $list_user_id = array_column($list, 'user_id');
  33. if (in_array($data['user_id'], $list_user_id)) {
  34. return $this->renderError('', '该用户已经是店员,无需重复添加');
  35. }
  36. // 新增记录
  37. if ($model->add($data)) {
  38. return $this->renderSuccess('', '添加成功');
  39. }
  40. return $this->renderError('', $model->getError() ?: '添加失败');
  41. }
  42. public function detail($clerk_id)
  43. {
  44. $detail = ClerkModel::detail($clerk_id);
  45. // 门店列表
  46. $store_list = StoreModel::getAllList();
  47. return $this->renderSuccess('', compact('detail', 'store_list'));
  48. }
  49. /**
  50. * 编辑店员
  51. */
  52. public function edit($clerk_id)
  53. {
  54. if($this->request->isGet()){
  55. return $this->detail($clerk_id);
  56. }
  57. $model = ClerkModel::detail($clerk_id);
  58. //编辑店员的数据
  59. if ($model->edit($this->postData())) {
  60. return $this->renderSuccess('', '更新成功');
  61. }
  62. return $this->renderError('', $model->getError() ?: '更新失败');
  63. }
  64. /**
  65. * 删除店员
  66. */
  67. public function delete($clerk_id)
  68. {
  69. // 店员详情
  70. $model = ClerkModel::detail($clerk_id);
  71. if (!$model->setDelete()) {
  72. return $this->renderError('', '删除失败');
  73. }
  74. return $this->renderSuccess('', $model->getError() ?: '删除成功');
  75. }
  76. }