package com.gxwebsoft.oa.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.oa.service.OaAssetsCodeService;
import com.gxwebsoft.oa.entity.OaAssetsCode;
import com.gxwebsoft.oa.param.OaAssetsCodeParam;
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-10-18 18:27:01
 */
@Tag(name = "代码仓库管理")
@RestController
@RequestMapping("/api/oa/oa-assets-code")
public class OaAssetsCodeController extends BaseController {
    @Resource
    private OaAssetsCodeService oaAssetsCodeService;

    @PreAuthorize("hasAuthority('oa:oaAssetsCode:list')")
    @Operation(summary = "分页查询代码仓库")
    @GetMapping("/page")
    public ApiResult<PageResult<OaAssetsCode>> page(OaAssetsCodeParam param) {
        // 使用关联查询
        return success(oaAssetsCodeService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsCode:list')")
    @Operation(summary = "查询全部代码仓库")
    @GetMapping()
    public ApiResult<List<OaAssetsCode>> list(OaAssetsCodeParam param) {
        // 使用关联查询
        return success(oaAssetsCodeService.listRel(param));
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsCode:list')")
    @Operation(summary = "根据id查询代码仓库")
    @GetMapping("/{id}")
    public ApiResult<OaAssetsCode> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(oaAssetsCodeService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsCode:save')")
    @OperationLog
    @Operation(summary = "添加代码仓库")
    @PostMapping()
    public ApiResult<?> save(@RequestBody OaAssetsCode oaAssetsCode) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          oaAssetsCode.setUserId(loginUser.getUserId());
        }
        if (oaAssetsCodeService.save(oaAssetsCode)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsCode:update')")
    @Operation(summary = "修改代码仓库")
    @PutMapping()
    public ApiResult<?> update(@RequestBody OaAssetsCode oaAssetsCode) {
        if (oaAssetsCodeService.updateById(oaAssetsCode)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsCode:remove')")
    @Operation(summary = "删除代码仓库")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (oaAssetsCodeService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsCode:save')")
    @Operation(summary = "批量添加代码仓库")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<OaAssetsCode> list) {
        if (oaAssetsCodeService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsCode:update')")
    @Operation(summary = "批量修改代码仓库")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<OaAssetsCode> batchParam) {
        if (batchParam.update(oaAssetsCodeService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsCode:remove')")
    @Operation(summary = "批量删除代码仓库")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (oaAssetsCodeService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
