package com.gxwebsoft.oa.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.oa.service.OaProductTabsService;
import com.gxwebsoft.oa.entity.OaProductTabs;
import com.gxwebsoft.oa.param.OaProductTabsParam;
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-tabs")
public class OaProductTabsController extends BaseController {
    @Resource
    private OaProductTabsService oaProductTabsService;

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

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

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

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

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

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

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

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

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

}
