package com.gxwebsoft.oa.controller;

import com.gxwebsoft.common.core.web.*;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.oa.entity.AppField;
import com.gxwebsoft.oa.param.AppFieldParam;
import com.gxwebsoft.oa.service.AppFieldService;
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-01-12 20:08:35
 */
@Api(tags = "应用参数管理")
@RestController
@RequestMapping("/api/oa/app-field")
public class AppFieldController extends BaseController {
    @Resource
    private AppFieldService appFieldService;

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

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

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

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

    @ApiOperation("修改应用参数")
    @PutMapping()
    public ApiResult<?> update(@RequestBody AppField appField) {
        if (appFieldService.updateById(appField)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

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

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

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

}
