package com.gxwebsoft.common.system.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.common.system.service.ComponentsService;
import com.gxwebsoft.common.system.entity.Components;
import com.gxwebsoft.common.system.param.ComponentsParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.annotation.OperationLog;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 组件控制器
 *
 * @author 科技小王子
 * @since 2024-08-25 19:08:51
 */
@Tag(name = "组件管理")
@RestController
@RequestMapping("/api/system/components")
public class ComponentsController extends BaseController {
    @Resource
    private ComponentsService componentsService;

    @PreAuthorize("hasAuthority('sys:components:list')")
    @OperationLog
    @Operation(summary = "分页查询组件")
    @GetMapping("/page")
    public ApiResult<PageResult<Components>> page(ComponentsParam param) {
        PageParam<Components, ComponentsParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(componentsService.page(page, page.getWrapper()));
        // 使用关联查询
        //return success(componentsService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('sys:components:list')")
    @OperationLog
    @Operation(summary = "查询全部组件")
    @GetMapping()
    public ApiResult<List<Components>> list(ComponentsParam param) {
        PageParam<Components, ComponentsParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(componentsService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(componentsService.listRel(param));
    }

    @PreAuthorize("hasAuthority('sys:components:list')")
    @OperationLog
    @Operation(summary = "根据id查询组件")
    @GetMapping("/{id}")
    public ApiResult<Components> get(@PathVariable("id") Integer id) {
        return success(componentsService.getById(id));
        // 使用关联查询
        //return success(componentsService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('sys:components:save')")
    @OperationLog
    @Operation(summary = "添加组件")
    @PostMapping()
    public ApiResult<?> save(@RequestBody Components components) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          components.setUserId(loginUser.getUserId());
        }
        if (componentsService.save(components)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('sys:components:update')")
    @OperationLog
    @Operation(summary = "修改组件")
    @PutMapping()
    public ApiResult<?> update(@RequestBody Components components) {
        if (componentsService.updateById(components)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('sys:components:remove')")
    @OperationLog
    @Operation(summary = "删除组件")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (componentsService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('sys:components:save')")
    @OperationLog
    @Operation(summary = "批量添加组件")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<Components> list) {
        if (componentsService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('sys:components:update')")
    @OperationLog
    @Operation(summary = "批量修改组件")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<Components> batchParam) {
        if (batchParam.update(componentsService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('sys:components:remove')")
    @OperationLog
    @Operation(summary = "批量删除组件")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (componentsService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
