package top.imwork.window.silos.controller.upms;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;
import top.imwork.commons.core.base.BaseController;
import top.imwork.commons.core.pojo.BaseTreeNode;
import top.imwork.commons.core.pojo.DeleteParams;
import top.imwork.commons.core.pojo.ResponseMsg;
import top.imwork.window.silos.convert.upms.PermissionConvert;
import top.imwork.window.silos.pojo.upms.bo.PermissionBO;
import top.imwork.window.silos.pojo.upms.bo.PermissionResultBO;
import top.imwork.window.silos.pojo.upms.po.PermissionListParamVO;
import top.imwork.window.silos.pojo.upms.po.PermissionParamVO;
import top.imwork.window.silos.service.upms.IPermissionService;

import java.util.List;


/**
 * 权限信息
 *
 * @author jiangxiaowei
 * email: e-jiangxiaowei@outlook.com
 * date: 2025-12-11 12:21:32
 */
@RestController
@RequestMapping("/upms/permission")
public class PermissionServiceController extends BaseController {

    @Resource
    public IPermissionService permissionService;

    /**
     * 根据id查询数据
     * @param id long类型的id
     * @return 返回查询结果
     */
    @GetMapping("/info/{id}")
    public ResponseMsg info(@PathVariable("id") Long id){
        return ResponseMsg.buildResponse(permissionService.info(id));
    }

    /**
     * 新增/修改info
     * @param permissionParamVO 实体信息
     * @return responseMsg 操作结果
     */
    @PostMapping("/save")
    public ResponseMsg save(@RequestBody @Valid PermissionParamVO permissionParamVO){
        PermissionBO permissionBO = permissionService.save(PermissionConvert.permissionParamVoToDto(permissionParamVO));
        return ResponseMsg.buildResponse(PermissionConvert.permissionBoToVo(permissionBO));
    }

    /**
     * 根据id删除权限信息
     * @param id id
     * @return true/false 成功/失败
     */
    @DeleteMapping("/delete/{id}")
    public ResponseMsg delete(@PathVariable("id") Long id){
        return permissionService.delete(id)? ResponseMsg.ok() : ResponseMsg.fail();
    }

    /**
     * 批量逻辑删除信息
     *
     * @param dto 删除条件
     * @return true/false 是否删除成功
     */
    @DeleteMapping("/deleteByIds")
    public ResponseMsg deleteByIds(@RequestBody DeleteParams dto){
        String[] idArr = dto.getIds().split(",");
        return permissionService.deleteByIds(idArr)? ResponseMsg.ok() : ResponseMsg.fail();
    }

    @PostMapping("/queryPage")
    public ResponseMsg queryPage(@RequestBody @Valid PermissionListParamVO permissionListParamVO) {
        PermissionResultBO permissionResultBO = permissionService.queryPage(PermissionConvert.permissionListParamVoToDTO(permissionListParamVO));
        return ResponseMsg.buildResponse(PermissionConvert.permissionResultBoToVo(permissionResultBO));
    }

    @PostMapping("/queryTreeList")
    public ResponseMsg queryTreeList() {
        List<BaseTreeNode> list = permissionService.queryTreeList();
        return ResponseMsg.buildResponse(list);
    }
}
