package com.gxwebsoft.oa.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.oa.service.OaProductService;
import com.gxwebsoft.oa.entity.OaProduct;
import com.gxwebsoft.oa.param.OaProductParam;
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-10 20:57:42
 */
@Tag(name = "产品记录表管理")
@RestController
@RequestMapping("/api/oa/oa-product")
public class OaProductController extends BaseController {
    @Resource
    private OaProductService oaProductService;

    @Operation(summary = "分页查询产品记录表")
    @GetMapping("/page")
    public ApiResult<PageResult<OaProduct>> page(OaProductParam param) {
        // 使用关联查询
        return success(oaProductService.pageRel(param));
    }

    @Operation(summary = "查询全部产品记录表")
    @GetMapping()
    public ApiResult<List<OaProduct>> list(OaProductParam param) {
        PageParam<OaProduct, OaProductParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(oaProductService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(oaProductService.listRel(param));
    }

    @PreAuthorize("hasAuthority('oa:oaProduct:list')")
    @OperationLog
    @Operation(summary = "根据id查询产品记录表")
    @GetMapping("/{id}")
    public ApiResult<OaProduct> get(@PathVariable("id") Integer id) {
        return success(oaProductService.getById(id));
        // 使用关联查询
        //return success(oaProductService.getByIdRel(id));
    }

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

    @Operation(summary = "修改产品记录表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody OaProduct oaProduct) {
        if (oaProductService.updateById(oaProduct)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

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

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

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

}
