Service.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace app\admin\controller\shopro\goods;
  3. use app\admin\controller\shopro\Common;
  4. use think\Db;
  5. use app\admin\model\shopro\goods\Service as ServiceModel;
  6. /**
  7. * 服务保障
  8. */
  9. class Service extends Common
  10. {
  11. protected $noNeedRight = ['select'];
  12. public function _initialize()
  13. {
  14. parent::_initialize();
  15. $this->model = new ServiceModel;
  16. }
  17. /**
  18. * 服务保障列表
  19. *
  20. * @return \think\Response
  21. */
  22. public function index()
  23. {
  24. if (!$this->request->isAjax()) {
  25. return $this->view->fetch();
  26. }
  27. $services = $this->model->sheepFilter()->paginate(request()->param('list_rows', 10));
  28. $this->success('获取成功', null, $services);
  29. }
  30. /**
  31. * 添加服务保障
  32. *
  33. * @return \think\Response
  34. */
  35. public function add()
  36. {
  37. if (!$this->request->isAjax()) {
  38. return $this->view->fetch();
  39. }
  40. $params = $this->request->only([
  41. 'name', 'image', 'description'
  42. ]);
  43. $this->svalidate($params, ".add");
  44. Db::transaction(function () use ($params) {
  45. $this->model->save($params);
  46. });
  47. $this->success('保存成功');
  48. }
  49. /**
  50. * 服务保障详情
  51. *
  52. * @param $id
  53. * @return \think\Response
  54. */
  55. public function detail($id)
  56. {
  57. $service = $this->model->where('id', $id)->find();
  58. if (!$service) {
  59. $this->error(__('No Results were found'));
  60. }
  61. $this->success('获取成功', null, $service);
  62. }
  63. /**
  64. * 修改服务保障
  65. *
  66. * @return \think\Response
  67. */
  68. public function edit($id = null)
  69. {
  70. if (!$this->request->isAjax()) {
  71. return $this->view->fetch('add');
  72. }
  73. $params = $this->request->only([
  74. 'name', 'image', 'description'
  75. ]);
  76. $this->svalidate($params, ".edit");
  77. $id = explode(',', $id);
  78. $list = $this->model->whereIn('id', $id)->select();
  79. $result = Db::transaction(function () use ($list, $params) {
  80. $count = 0;
  81. foreach ($list as $item) {
  82. $params['id'] = $item->id;
  83. $count += $item->save($params);
  84. }
  85. return $count;
  86. });
  87. if ($result) {
  88. $this->success('更新成功', null, $result);
  89. } else {
  90. $this->error('更新失败,未改变任何记录');
  91. }
  92. }
  93. /**
  94. * 删除服务标签
  95. *
  96. * @param string $id 要删除的服务保障列表
  97. * @return void
  98. */
  99. public function delete($id)
  100. {
  101. if (empty($id)) {
  102. $this->error(__('Parameter %s can not be empty', 'id'));
  103. }
  104. $list = $this->model->where('id', 'in', $id)->select();
  105. Db::transaction(function () use ($list) {
  106. $count = 0;
  107. foreach ($list as $item) {
  108. $count += $item->delete();
  109. }
  110. return $count;
  111. });
  112. $this->success('删除成功');
  113. }
  114. /**
  115. * 获取所有服务列表
  116. *
  117. * @return \think\Response
  118. */
  119. public function select()
  120. {
  121. $services = $this->model->field('id, name')->select();
  122. $this->success('获取成功', null, $services);
  123. }
  124. }