Group.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * Author: luzheng.liu
  4. * Time: 2020/12/16 23:06
  5. */
  6. namespace app\api\controller;
  7. use app\api\BaseController;
  8. use app\api\exception\ApiException;
  9. use app\api\model\GroupModel;
  10. use app\api\model\StoreModel;
  11. use app\common\until\Until;
  12. class Group extends BaseController {
  13. /**
  14. * @OA\Get(path="/api/Group/index",
  15. * tags={"集团管理"},
  16. * summary="集团列表",
  17. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  18. * @OA\Parameter(name="page", in="query", description="页码", @OA\Schema(type="ineger",default="1")),
  19. * @OA\Parameter(name="pageSize", in="query", description="页尺寸", @OA\Schema(type="integer",default="10")),
  20. * @OA\RequestBody(
  21. * ),
  22. * @OA\Response(response="200", description="请求成功")
  23. * )
  24. */
  25. public function index() {
  26. $input = Until::getInput();
  27. $model = new GroupModel();
  28. $model->setPage($input['page'] ?? 1);
  29. $model->setPageSize($input['pageSize'] ?? 10);
  30. $where = [];
  31. $where[] = ['gr.admin_id','=',$this->adminId];
  32. $model->setWhere($where);
  33. $data = $model->getGroupList();
  34. $ids = array_column($data['list'], 'id');
  35. $storeList = (new StoreModel())::where([['group_id', 'in', $ids]])->select();
  36. $storeList = Until::modelToArray($storeList);
  37. $groupStore = [];
  38. foreach ($storeList as $info) {
  39. $groupStore[$info['group_id']][] = $info;
  40. }
  41. foreach ($data['list'] as &$info) {
  42. $info['storeList'] = $groupStore[$info['id']];
  43. }
  44. Until::output($data);
  45. }
  46. /**
  47. * @OA\Post(path="/api/Group/save",
  48. * tags={"集团管理"},
  49. * summary="保存集团信息",
  50. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  51. * @OA\RequestBody(
  52. * @OA\MediaType(
  53. * mediaType="multipart/form-data",
  54. * @OA\Schema(
  55. * @OA\Property(description="集团名称", property="name", type="string", default="四海集团"),
  56. * required={"name"})
  57. * )
  58. * ),
  59. * @OA\Response(response="200", description="请求成功")
  60. * )
  61. */
  62. public function save() {
  63. $input = Until::getInput();
  64. $rule = [
  65. 'name|集团名称' => 'require',
  66. ];
  67. Until::check($rule, $input);
  68. $model = new GroupModel();
  69. $info = $model::where(['group_name' => $input['name']])->find();
  70. if ($info !== null) {
  71. throw new ApiException('集团名称不许重复');
  72. }
  73. if (!empty($input['id']) ) {
  74. $id = (int)$input['id'];
  75. $model::where(['id' => $id])->update([
  76. 'group_name' => $input['name']
  77. ]);
  78. } else {
  79. $id = $model->insertGetId([
  80. 'group_name' => $input['name']
  81. ]);
  82. }
  83. $info = $model::get($id);
  84. Until::output(['info' => Until::modelToArray($info)]);
  85. }
  86. public function delete($id,$status) {
  87. $model = new GroupModel();
  88. $where[] = ['id', '=', (int)$id];
  89. $data = ['status' => (int)$status];
  90. $isSuccess = $model::where($where)->update($data);
  91. Until::output(['isSuccess' => $isSuccess]);
  92. }
  93. }