package com.gxwebsoft.oa.controller;

import com.gxwebsoft.common.core.web.*;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.oa.entity.Product;
import com.gxwebsoft.oa.param.ProductParam;
import com.gxwebsoft.oa.service.ProductService;
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 14:18:01
 */
@Api(tags = "产品记录表管理")
@RestController
@RequestMapping("/api/oa/product")
public class ProductController extends BaseController {
    @Resource
    private ProductService productService;

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

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

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

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

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

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

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

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

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

}
