package com.gxwebsoft.oa.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.oa.service.OaCompanyFieldService;
import com.gxwebsoft.oa.entity.OaCompanyField;
import com.gxwebsoft.oa.param.OaCompanyFieldParam;
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-20 12:33:12
 */
@Tag(name = "企业参数管理")
@RestController
@RequestMapping("/api/oa/oa-company-field")
public class OaCompanyFieldController extends BaseController {
    @Resource
    private OaCompanyFieldService oaCompanyFieldService;

    @Operation(summary = "分页查询企业参数")
    @GetMapping("/page")
    public ApiResult<PageResult<OaCompanyField>> page(OaCompanyFieldParam param) {
        // 使用关联查询
        return success(oaCompanyFieldService.pageRel(param));
    }

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

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

    @Operation(summary = "添加企业参数")
    @PostMapping()
    public ApiResult<?> save(@RequestBody OaCompanyField oaCompanyField) {
        if (oaCompanyFieldService.save(oaCompanyField)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @Operation(summary = "修改企业参数")
    @PutMapping()
    public ApiResult<?> update(@RequestBody OaCompanyField oaCompanyField) {
        if (oaCompanyFieldService.updateById(oaCompanyField)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

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

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

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

}
