TaxiServiceCategory.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace app\admin\controller\store;
  3. use app\common\controller\AdminController;
  4. use app\http\IResponse;
  5. use think\App;
  6. use think\exception\PDOException;
  7. class TaxiServiceCategory extends AdminController
  8. {
  9. protected $model;
  10. public function __construct(App $app = null)
  11. {
  12. parent::__construct($app);
  13. $this->model = new \app\common\model\TaxiServiceCategory();
  14. }
  15. /**
  16. * 列表
  17. *
  18. * @author 许祖兴 < zuxing.xu@lettered.cn>
  19. * @date 2020/3/16 14:39
  20. *
  21. * @return \think\response\Json
  22. * @throws \think\exception\DbException
  23. */
  24. public function index()
  25. {
  26. $where = [];
  27. $list = $this->model->where($where)->paginate(input('limit'),false);
  28. return IResponse::paginate($list);
  29. }
  30. /**
  31. * @desc desc
  32. * @throws PDOException
  33. * @author weichuanbao<654745815@qq.com>
  34. * @date 2021/12/3 0003
  35. */
  36. public function create()
  37. {
  38. $params = input();
  39. $result = false;
  40. $this->model->startTrans();
  41. try {
  42. $result = $this->model->save($params);
  43. $this->model->commit();
  44. }
  45. catch(PDOException $e) {
  46. $this->model->rollback();
  47. }
  48. if ($result) {
  49. IResponse::success([],'操作成功');
  50. }
  51. IResponse::failure('操作失败');
  52. }
  53. /**
  54. * @desc desc
  55. * @param $id
  56. * @throws PDOException
  57. * @author weichuanbao<654745815@qq.com>
  58. * @date 2021/12/3 0003
  59. */
  60. public function update($id)
  61. {
  62. $params = input();
  63. $row = $this->model::get($id);
  64. if (!$row) {
  65. IResponse::failure('该记录不存在');
  66. }
  67. $result = false;
  68. $this->model->startTrans();
  69. try {
  70. $result = $row->allowField(true)->save($params);
  71. $this->model->commit();
  72. }
  73. catch(PDOException $e) {
  74. $this->model->rollback();
  75. }
  76. if ($result !== false) {
  77. IResponse::success([],'操作成功');
  78. }
  79. IResponse::failure('操作失败');
  80. }
  81. /**
  82. * @desc desc
  83. * @param $id
  84. * @throws PDOException
  85. * @author weichuanbao<654745815@qq.com>
  86. * @date 2021/12/3 0003
  87. */
  88. public function delete($id)
  89. {
  90. $row = $this->model::get($id);
  91. if (!$row) {
  92. IResponse::failure('该记录不存在');
  93. }
  94. $result = false;
  95. $this->model->startTrans();
  96. try {
  97. $result = $row->delete();
  98. $this->model->commit();
  99. }
  100. catch(PDOException $e) {
  101. $this->model->rollback();
  102. }
  103. if ($result) {
  104. IResponse::success([],'操作成功');
  105. }
  106. IResponse::failure('操作失败');
  107. }
  108. /**
  109. * @desc desc
  110. * @param $id
  111. * @throws PDOException
  112. * @author weichuanbao<654745815@qq.com>
  113. * @date 2021/12/3 0003
  114. */
  115. public function change()
  116. {
  117. $params = input();
  118. $list = $this->model->whereIn('id', $params['id'])->select();
  119. if (!$list) {
  120. IResponse::failure('记录不存在');
  121. }
  122. $result = 0;
  123. $this->model->startTrans();
  124. try {
  125. foreach ($list as $item) {
  126. $item->status = $params['status'];
  127. $res = $item->save();
  128. if ($res) {
  129. $result++;
  130. }
  131. }
  132. $this->model->commit();
  133. }
  134. catch(PDOException $e) {
  135. $this->model->rollback();
  136. }
  137. if ($result) {
  138. IResponse::success([],'操作成功');
  139. }
  140. IResponse::failure('操作失败');
  141. }
  142. }