DeptPremisesController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace app\admin\controller\marketing;
  3. use app\admin\service\sys_manage\DeptService;
  4. use app\admin\validate\sys_manage\DeptValidate;
  5. use app\controller\Curd;
  6. use app\model\SysDept;
  7. use support\Db;
  8. use support\exception\BusinessException;
  9. use support\Request;
  10. use support\Response;
  11. class DeptPremisesController extends Curd
  12. {
  13. public function __construct()
  14. {
  15. $this->model = new SysDept();
  16. $this->validate = true;
  17. $this->validateClass = new DeptValidate();
  18. }
  19. /**
  20. * @Desc 营业场所列表
  21. * @Author Gorden
  22. * @Date 2024/3/7 13:17
  23. *
  24. * @param Request $request
  25. * @return Response
  26. * @throws BusinessException
  27. */
  28. public function select(Request $request): Response
  29. {
  30. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  31. $order = $request->get('order', 'desc');
  32. $field = $request->get('field','dept_addtimes');
  33. $where['dept_super_id'] = 0;
  34. $where['dept_category'] = '营业场所';
  35. $query = $this->doSelect($where, $field, $order);
  36. return $this->doFormat($query, $format, $limit);
  37. }
  38. /**
  39. * @Desc 添加营业场所
  40. * @Author Gorden
  41. * @Date 2024/3/7 11:48
  42. *
  43. * @param Request $request
  44. * @return Response
  45. */
  46. public function insert(Request $request): Response
  47. {
  48. if ($this->validate && !$this->validateClass->scene('add_premises')->check($request->post())) {
  49. return json_fail($this->validateClass->getError());
  50. }
  51. // 验证编号是否存在
  52. if (SysDept::checkExist('营业场所','dept_code',$request->post('dept_code'))){
  53. return json_fail('编号重复,请重新输入');
  54. }
  55. Db::beginTransaction();
  56. try {
  57. $data = $this->insertInput($request);
  58. $data['dept_super_id'] = 0;
  59. $deptId = $this->doInsert($data);
  60. if (!$deptId) {
  61. throw new BusinessException('创建营业场所失败');
  62. }
  63. // 更新部门path
  64. $path = '/0/' . $deptId . '/';
  65. if (!SysDept::where('dept_id', $deptId)->update(['dept_super_path' => $path])) {
  66. throw new BusinessException('创建营业场所失败');
  67. }
  68. Db::commit();
  69. } catch (BusinessException $customException) {
  70. Db::rollBack();
  71. return json_fail($customException->getMessage());
  72. } catch (\Exception $exception) {
  73. Db::rollBack();
  74. return json_fail('创建营业场所失败');
  75. }
  76. return json_success('success');
  77. }
  78. /**
  79. * @Desc 更新
  80. * @Author Gorden
  81. * @Date 2024/3/7 13:14
  82. *
  83. * @param Request $request
  84. * @return Response
  85. */
  86. public function update(Request $request): Response
  87. {
  88. if ($this->validate && !$this->validateClass->scene('update_premises')->check($request->post())) {
  89. return json_fail($this->validateClass->getError());
  90. }
  91. try {
  92. [$id, $data] = $this->updateInput($request);
  93. $this->doUpdate($id, $data);
  94. } catch (BusinessException $customException) {
  95. return json_fail($customException->getMessage());
  96. } catch (\Exception $e) {
  97. return json_fail('数据更新失败');
  98. }
  99. return json_success('success');
  100. }
  101. }