package com.gxwebsoft.oa.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.oa.service.OaCompanyService;
import com.gxwebsoft.oa.entity.OaCompany;
import com.gxwebsoft.oa.param.OaCompanyParam;
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")
public class OaCompanyController extends BaseController {
    @Resource
    private OaCompanyService oaCompanyService;

    @Operation(summary = "分页查询企业信息")
    @GetMapping("/page")
    public ApiResult<PageResult<OaCompany>> page(OaCompanyParam param) {
        // 使用关联查询
        return success(oaCompanyService.pageRel(param));
    }

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

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

    @Operation(summary = "添加企业信息")
    @PostMapping()
    public ApiResult<?> save(@RequestBody OaCompany oaCompany) {
        if (oaCompanyService.save(oaCompany)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @Operation(summary = "修改企业信息")
    @PutMapping()
    public ApiResult<?> update(@RequestBody OaCompany oaCompany) {
        if (oaCompanyService.updateById(oaCompany)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

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

    @Operation(summary = "批量修改企业信息")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<OaCompany> batchParam) {
        if (batchParam.update(oaCompanyService, "company_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

}
