package com.gxwebsoft.oa.controller;

import com.gxwebsoft.common.core.web.*;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.oa.entity.CompanyField;
import com.gxwebsoft.oa.param.CompanyFieldParam;
import com.gxwebsoft.oa.service.CompanyFieldService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 企业参数控制器
 *
 * @author 科技小王子
 * @since 2024-02-04 14:44:42
 */
@Api(tags = "企业参数管理")
@RestController
@RequestMapping("/api/oa/company-field")
public class CompanyFieldController extends BaseController {
    @Resource
    private CompanyFieldService companyFieldService;

    @ApiOperation("分页查询企业参数")
    @GetMapping("/page")
    public ApiResult<PageResult<CompanyField>> page(CompanyFieldParam param) {
        PageParam<CompanyField, CompanyFieldParam> page = new PageParam<>(param);
        page.setDefaultOrder("sort_number desc");
        page.setDefaultOrder("create_time asc");
        return success(companyFieldService.page(page, page.getWrapper()));
        // 使用关联查询
        //return success(companyFieldService.pageRel(param));
    }

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

    @ApiOperation("根据id查询企业参数")
    @GetMapping("/{id}")
    public ApiResult<CompanyField> get(@PathVariable("id") Integer id) {
        return success(companyFieldService.getById(id));
        // 使用关联查询
        //return success(companyFieldService.getByIdRel(id));
    }

    @ApiOperation("添加企业参数")
    @PostMapping()
    public ApiResult<?> save(@RequestBody CompanyField companyField) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          companyField.setUserId(loginUser.getUserId());
        }
        if (companyFieldService.save(companyField)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("修改企业参数")
    @PutMapping()
    public ApiResult<?> update(@RequestBody CompanyField companyField) {
        if (companyFieldService.updateById(companyField)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

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

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

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

}
