Shop.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace app\sys\controller;
  3. use app\model\CustomerVisitLog;
  4. use think\facade\View;
  5. use app\model\Shop as ShopModel;
  6. class Shop
  7. {
  8. /**
  9. * 列表页面
  10. */
  11. public function index()
  12. {
  13. if (!request()->isAjax()){
  14. return View::fetch();
  15. } else {
  16. $keyword = input('keyword', '', 'trim');
  17. if ($keyword){
  18. $where[] = ['name', 'like', '%' . $keyword . '%'];
  19. }
  20. $page = input('page', 1, 'intval');
  21. $limit = input('limit', 10, 'intval');
  22. $where[] = ['root_id', '=', request()->employee->root_id];
  23. $count = ShopModel::where($where)->count();
  24. $list = ShopModel::where($where)->page($page, $limit)->order('addtime desc')->select()->toArray();
  25. return json(['code'=> 0, 'data'=> $list, 'count'=> $count]);
  26. }
  27. }
  28. /**
  29. * 添加
  30. */
  31. public function add()
  32. {
  33. if (request()->isPost()) {
  34. $root_id = request()->employee->root_id;
  35. $param = request()->only(['name', 'address'=> '']);
  36. $param['root_id'] = $root_id;
  37. $find = ShopModel::where([['root_id', '=', $root_id], ['name', '=', $param['name']]])->findOrEmpty();
  38. if (!$find->isEmpty()){
  39. return json(['code'=> 1, 'msg'=> '店面已存在']);
  40. }
  41. $result = ShopModel::create($param);
  42. if ($result !== false) {
  43. return json(['code'=> 0, 'msg'=> '添加成功']);
  44. } else {
  45. return json(['code'=> 1, 'msg'=> '添加失败']);
  46. }
  47. } else {
  48. return View::fetch();
  49. }
  50. }
  51. /**
  52. * 修改
  53. */
  54. public function edit()
  55. {
  56. $id = input('id', '', 'intval');
  57. if (request()->isPost()){
  58. $info = ShopModel::find($id);
  59. if ($info['root_id'] !== request()->employee->root_id){
  60. return json(['code'=> 1, 'msg'=> '编辑失败']);
  61. }
  62. $name = input('name', '', 'trim');
  63. $root_id = request()->employee->root_id;
  64. $find = ShopModel::where([['root_id', '=', $root_id], ['name', '=', $name]])->findOrEmpty();
  65. if (!$find->isEmpty() && $find['id'] != $id){
  66. return json(['code'=> 1, 'msg'=> '店面已存在']);
  67. }
  68. $address = input('address', '', 'trim');
  69. $info->name = $name;
  70. $info->address = $address;
  71. $result = $info->save();
  72. if ($result !== false) {
  73. return json(['code'=> 0, 'msg'=> '操作成功']);
  74. } else {
  75. return json(['code'=> 1, 'msg'=> '操作失败']);
  76. }
  77. } else {
  78. $info = ShopModel::find($id);
  79. View::assign('data', $info);
  80. return View::fetch();
  81. }
  82. }
  83. /**
  84. * 删除
  85. */
  86. public function delete()
  87. {
  88. $id = input('id', '', 'intval');
  89. $info = ShopModel::find($id);
  90. if ($info['root_id'] !== request()->employee->root_id){
  91. return json(['code'=> 1, 'msg'=> '删除失败']);
  92. }
  93. $use = CustomerVisitLog::where('shop_id', '=', $id)->findOrEmpty();
  94. if (!$use->isEmpty()){
  95. return json(['code'=> 1, 'msg'=> '已有员工报备客户到店,无法删除']);
  96. }
  97. $result = $info->delete();
  98. if ($result !== false) {
  99. return json(['code'=> 0, 'msg'=> '操作成功']);
  100. } else {
  101. return json(['code'=> 1, 'msg'=> '操作失败']);
  102. }
  103. }
  104. }