package com.gxwebsoft.oa.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.oa.service.OaAppFieldService;
import com.gxwebsoft.oa.entity.OaAppField;
import com.gxwebsoft.oa.param.OaAppFieldParam;
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-09-10 20:57:41
 */
@Tag(name = "应用参数管理")
@RestController
@RequestMapping("/api/oa/oa-app-field")
public class OaAppFieldController extends BaseController {
    @Resource
    private OaAppFieldService oaAppFieldService;

    @Operation(summary = "分页查询应用参数")
    @GetMapping("/page")
    public ApiResult<PageResult<OaAppField>> page(OaAppFieldParam param) {
        // 使用关联查询
        return success(oaAppFieldService.pageRel(param));
    }

    @Operation(summary = "查询全部应用参数")
    @GetMapping()
    public ApiResult<List<OaAppField>> list(OaAppFieldParam param) {
        PageParam<OaAppField, OaAppFieldParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(oaAppFieldService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(oaAppFieldService.listRel(param));
    }

    @PreAuthorize("hasAuthority('oa:oaAppField:list')")
    @OperationLog
    @Operation(summary = "根据id查询应用参数")
    @GetMapping("/{id}")
    public ApiResult<OaAppField> get(@PathVariable("id") Integer id) {
        return success(oaAppFieldService.getById(id));
        // 使用关联查询
        //return success(oaAppFieldService.getByIdRel(id));
    }

    @Operation(summary = "添加应用参数")
    @PostMapping()
    public ApiResult<?> save(@RequestBody OaAppField oaAppField) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          oaAppField.setUserId(loginUser.getUserId());
        }
        if (oaAppFieldService.save(oaAppField)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @Operation(summary = "修改应用参数")
    @PutMapping()
    public ApiResult<?> update(@RequestBody OaAppField oaAppField) {
        if (oaAppFieldService.updateById(oaAppField)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @Operation(summary = "删除应用参数")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (oaAppFieldService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @Operation(summary = "批量添加应用参数")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<OaAppField> list) {
        if (oaAppFieldService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @Operation(summary = "批量修改应用参数")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<OaAppField> batchParam) {
        if (batchParam.update(oaAppFieldService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @Operation(summary = "批量删除应用参数")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (oaAppFieldService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
