package com.gxwebsoft.oa.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.oa.service.OaAssetsTrademarkService;
import com.gxwebsoft.oa.entity.OaAssetsTrademark;
import com.gxwebsoft.oa.param.OaAssetsTrademarkParam;
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-10-18 19:46:21
 */
@Tag(name = "商标注册管理")
@RestController
@RequestMapping("/api/oa/oa-assets-trademark")
public class OaAssetsTrademarkController extends BaseController {
    @Resource
    private OaAssetsTrademarkService oaAssetsTrademarkService;

    @PreAuthorize("hasAuthority('oa:oaAssetsTrademark:list')")
    @Operation(summary = "分页查询商标注册")
    @GetMapping("/page")
    public ApiResult<PageResult<OaAssetsTrademark>> page(OaAssetsTrademarkParam param) {
        // 使用关联查询
        return success(oaAssetsTrademarkService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsTrademark:list')")
    @Operation(summary = "查询全部商标注册")
    @GetMapping()
    public ApiResult<List<OaAssetsTrademark>> list(OaAssetsTrademarkParam param) {
        // 使用关联查询
        return success(oaAssetsTrademarkService.listRel(param));
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsTrademark:list')")
    @Operation(summary = "根据id查询商标注册")
    @GetMapping("/{id}")
    public ApiResult<OaAssetsTrademark> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(oaAssetsTrademarkService.getByIdRel(id));
    }
    @PreAuthorize("hasAuthority('oa:oaAssetsTrademark:save')")
    @Operation(summary = "添加商标注册")
    @OperationLog
    @PostMapping()
    public ApiResult<?> save(@RequestBody OaAssetsTrademark oaAssetsTrademark) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          oaAssetsTrademark.setUserId(loginUser.getUserId());
        }
        if (oaAssetsTrademarkService.save(oaAssetsTrademark)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }
    @PreAuthorize("hasAuthority('oa:oaAssetsTrademark:update')")
    @Operation(summary = "修改商标注册")
    @PutMapping()
    public ApiResult<?> update(@RequestBody OaAssetsTrademark oaAssetsTrademark) {
        if (oaAssetsTrademarkService.updateById(oaAssetsTrademark)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }
    @PreAuthorize("hasAuthority('oa:oaAssetsTrademark:remove')")
    @Operation(summary = "删除商标注册")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (oaAssetsTrademarkService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsTrademark:save')")
    @Operation(summary = "批量添加商标注册")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<OaAssetsTrademark> list) {
        if (oaAssetsTrademarkService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsTrademark:update')")
    @Operation(summary = "批量修改商标注册")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<OaAssetsTrademark> batchParam) {
        if (batchParam.update(oaAssetsTrademarkService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsTrademark:remove')")
    @Operation(summary = "批量删除商标注册")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (oaAssetsTrademarkService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
