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.ModulesService;
import com.gxwebsoft.common.system.entity.Modules;
import com.gxwebsoft.common.system.param.ModulesParam;
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 2023-10-18 15:53:43
 */
@Tag(name = "模块")
@RestController
@RequestMapping("/api/system/modules")
public class ModulesController extends BaseController {
    @Resource
    private ModulesService modulesService;

    @PreAuthorize("hasAuthority('sys:modules:list')")
    @OperationLog
    @Operation(summary = "分页查询模块管理")
    @GetMapping("/page")
    public ApiResult<PageResult<Modules>> page(ModulesParam param) {
        PageParam<Modules, ModulesParam> page = new PageParam<>(param);
//        page.setDefaultOrder("create_time desc");
        return success(modulesService.page(page, page.getWrapper()));
        // 使用关联查询
        //return success(modulesService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('sys:modules:list')")
    @OperationLog
    @Operation(summary = "查询全部模块管理")
    @GetMapping()
    public ApiResult<List<Modules>> list(ModulesParam param) {
        PageParam<Modules, ModulesParam> page = new PageParam<>(param);
//        page.setDefaultOrder("create_time desc");
        return success(modulesService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(modulesService.listRel(param));
    }

    @PreAuthorize("hasAuthority('sys:modules:list')")
    @OperationLog
    @Operation(summary = "根据id查询模块管理")
    @GetMapping("/{id}")
    public ApiResult<Modules> get(@PathVariable("id") Integer id) {
        return success(modulesService.getById(id));
        // 使用关联查询
        //return success(modulesService.getByIdRel(id));
    }

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

    @PreAuthorize("hasAuthority('sys:modules:update')")
    @OperationLog
    @Operation(summary = "修改模块管理")
    @PutMapping()
    public ApiResult<?> update(@RequestBody Modules modules) {
        if (modulesService.updateById(modules)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('sys:modules:remove')")
    @OperationLog
    @Operation(summary = "删除模块管理")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (modulesService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('sys:modules:save')")
    @OperationLog
    @Operation(summary = "批量添加模块管理")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<Modules> list) {
        if (modulesService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('sys:modules:update')")
    @OperationLog
    @Operation(summary = "批量修改模块管理")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<Modules> batchParam) {
        if (batchParam.update(modulesService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

}
