/*
*  Copyright 2019-2025 Tz
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.domain.BusUser;
import me.zhengjie.modules.system.domain.dto.BusTreeVo;
import me.zhengjie.modules.system.domain.dto.BusUserQueryCriteria;
import me.zhengjie.modules.system.service.BusUserDeviceService;
import me.zhengjie.modules.system.service.BusUserService;
import lombok.RequiredArgsConstructor;
import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;

/**
* @author tz
* @date 2025-05-19
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "系统：系统用户")
@RequestMapping("/api/busUser")
public class BusUserController {

    private final BusUserService busUserService;
    private final BusUserDeviceService busUserDeviceService;

    @Log("修改用户设备指令")
    @ApiOperation("修改用户设备指令")
    @PutMapping(value = "/updUserCommands")
    @PreAuthorize("@el.check('roles:edit')")
    public ResponseEntity<Object> updUserCommands(@RequestBody BusUser busUser) {
        busUserDeviceService.updUserCommands(busUser);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @ApiOperation("查询用户信息")
    @GetMapping(value = "/{id}")
    @PreAuthorize("@el.check('busUser:list')")
    public ResponseEntity<BusUser> findUserById(@PathVariable Long id) {
        BusUser busUser = busUserService.getById(id);
        List<BusTreeVo> userCommands = busUserService.getUserCommands(busUser.getId());
        busUser.setCommands(userCommands);
        return new ResponseEntity<>(busUser, HttpStatus.OK);
    }

    @GetMapping
    @ApiOperation("查询busUser")
    @PreAuthorize("@el.check('busUser:list')")
    public ResponseEntity<PageResult<BusUser>> queryBusUser(BusUserQueryCriteria criteria){
        Page<Object> page = new Page<>(criteria.getPage(), criteria.getSize());
        PageResult<BusUser> result = busUserService.queryAll(criteria, page);
        List<BusUser> content = result.getContent();
        for (BusUser busUser : content) {
            List<BusTreeVo> userCommands = busUserService.getUserCommands(busUser.getId());
            busUser.setCommands(userCommands);
        }
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    @PutMapping
    @Log("修改busUser")
    @ApiOperation("修改busUser")
    @PreAuthorize("@el.check('busUser:edit')")
    public ResponseEntity<Object> updateBusUser(@Validated @RequestBody BusUser resources){
        busUserService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

}