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.VersionService;
import com.gxwebsoft.common.system.entity.Version;
import com.gxwebsoft.common.system.param.VersionParam;
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-01-15 18:52:24
 */
@Tag(name = "系统版本")
@RestController
@RequestMapping("/api/system/version")
public class VersionController extends BaseController {
    @Resource
    private VersionService versionService;

    @PreAuthorize("hasAuthority('sys:version:list')")
    @OperationLog
    @Operation(summary = "分页查询版本更新")
    @GetMapping("/page")
    public ApiResult<PageResult<Version>> page(VersionParam param) {
        PageParam<Version, VersionParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(versionService.page(page, page.getWrapper()));
        // 使用关联查询
        //return success(versionService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('sys:version:list')")
    @OperationLog
    @Operation(summary = "查询全部版本更新")
    @GetMapping()
    public ApiResult<List<Version>> list(VersionParam param) {
        PageParam<Version, VersionParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(versionService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(versionService.listRel(param));
    }

    @PreAuthorize("hasAuthority('sys:version:list')")
    @OperationLog
    @Operation(summary = "根据id查询版本更新")
    @GetMapping("/{id}")
    public ApiResult<Version> get(@PathVariable("id") Integer id) {
        return success(versionService.getById(id));
        // 使用关联查询
        //return success(versionService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('sys:version:save')")
    @OperationLog
    @Operation(summary = "添加版本更新")
    @PostMapping()
    public ApiResult<?> save(@RequestBody Version version) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          version.setUserId(loginUser.getUserId());
        }
        if (versionService.save(version)) {
            return success("发布成功");
        }
        return fail("发布失败");
    }

    @PreAuthorize("hasAuthority('sys:version:save')")
    @OperationLog
    @Operation(summary = "修改版本更新")
    @PutMapping()
    public ApiResult<?> update(@RequestBody Version version) {
        if (versionService.updateById(version)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('sys:version:save')")
    @OperationLog
    @Operation(summary = "删除版本更新")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (versionService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('sys:version:save')")
    @OperationLog
    @Operation(summary = "批量添加版本更新")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<Version> list) {
        if (versionService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('sys:version:save')")
    @OperationLog
    @Operation(summary = "批量修改版本更新")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<Version> batchParam) {
        if (batchParam.update(versionService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('sys:version:save')")
    @OperationLog
    @Operation(summary = "批量删除版本更新")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (versionService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }
}
