package com.gxwebsoft.oa.controller;

import com.gxwebsoft.common.core.web.*;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.oa.entity.ProductTabs;
import com.gxwebsoft.oa.param.ProductTabsParam;
import com.gxwebsoft.oa.service.ProductTabsService;
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 2023-12-14 15:55:52
 */
@Api(tags = "产品标签记录表管理")
@RestController
@RequestMapping("/api/oa/product-tabs")
public class ProductTabsController extends BaseController {
    @Resource
    private ProductTabsService productTabsService;

    @ApiOperation("分页查询产品标签记录表")
    @GetMapping("/page")
    public ApiResult<PageResult<ProductTabs>> page(ProductTabsParam param) {
        // 使用关联查询
        return success(productTabsService.pageRel(param));
    }

    @ApiOperation("查询全部产品标签记录表")
    @GetMapping()
    public ApiResult<List<ProductTabs>> list(ProductTabsParam param) {
        // 使用关联查询
        return success(productTabsService.listRel(param));
    }

    @ApiOperation("根据id查询产品标签记录表")
    @GetMapping("/{id}")
    public ApiResult<ProductTabs> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(productTabsService.getByIdRel(id));
    }

    @ApiOperation("添加产品标签记录表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody ProductTabs productTabs) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          productTabs.setUserId(loginUser.getUserId());
        }
        if (productTabsService.save(productTabs)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("修改产品标签记录表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody ProductTabs productTabs) {
        if (productTabsService.updateById(productTabs)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("删除产品标签记录表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (productTabsService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @ApiOperation("批量添加产品标签记录表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<ProductTabs> list) {
        if (productTabsService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("批量修改产品标签记录表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<ProductTabs> batchParam) {
        if (batchParam.update(productTabsService, "tab_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("批量删除产品标签记录表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (productTabsService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
