package com.gxwebsoft.cms.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.cms.service.ComponentsService;
import com.gxwebsoft.cms.entity.Components;
import com.gxwebsoft.cms.param.ComponentsParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.system.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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:00:41
 */
@Api(tags = "组件管理")
@RestController
@RequestMapping("/api/cms/components")
public class ComponentsController extends BaseController {
    @Resource
    private ComponentsService componentsService;

    @ApiOperation("分页查询组件")
    @GetMapping("/page")
    public ApiResult<PageResult<Components>> page(ComponentsParam param) {
        // 使用关联查询
        return success(componentsService.pageRel(param));
    }

    @ApiOperation("查询全部组件")
    @GetMapping()
    public ApiResult<List<Components>> list(ComponentsParam param) {
        // 使用关联查询
        return success(componentsService.listRel(param));
    }

    @ApiOperation("根据id查询组件")
    @GetMapping("/{id}")
    public ApiResult<Components> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(componentsService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('cms:components:save')")
    @ApiOperation("添加组件")
    @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('cms:components:update')")
    @ApiOperation("修改组件")
    @PutMapping()
    public ApiResult<?> update(@RequestBody Components components) {
        if (componentsService.updateById(components)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

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

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

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

}
