package com.gxwebsoft.shop.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.shop.service.ShopArticleService;
import com.gxwebsoft.shop.entity.ShopArticle;
import com.gxwebsoft.shop.param.ShopArticleParam;
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 com.gxwebsoft.common.system.entity.User;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 商品文章控制器
 *
 * @author 科技小王子
 * @since 2025-08-13 05:14:53
 */
@Tag(name = "商品文章管理")
@RestController
@RequestMapping("/api/shop/shop-article")
public class ShopArticleController extends BaseController {
    @Resource
    private ShopArticleService shopArticleService;

    @PreAuthorize("hasAuthority('shop:shopArticle:list')")
    @Operation(summary = "分页查询商品文章")
    @GetMapping("/page")
    public ApiResult<PageResult<ShopArticle>> page(ShopArticleParam param) {
        // 使用关联查询
        return success(shopArticleService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('shop:shopArticle:list')")
    @Operation(summary = "查询全部商品文章")
    @GetMapping()
    public ApiResult<List<ShopArticle>> list(ShopArticleParam param) {
        // 使用关联查询
        return success(shopArticleService.listRel(param));
    }

    @PreAuthorize("hasAuthority('shop:shopArticle:list')")
    @Operation(summary = "根据id查询商品文章")
    @GetMapping("/{id}")
    public ApiResult<ShopArticle> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(shopArticleService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('shop:shopArticle:save')")
    @OperationLog
    @Operation(summary = "添加商品文章")
    @PostMapping()
    public ApiResult<?> save(@RequestBody ShopArticle shopArticle) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
         shopArticle.setUserId(loginUser.getUserId());
        }
        if (shopArticleService.save(shopArticle)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('shop:shopArticle:update')")
    @OperationLog
    @Operation(summary = "修改商品文章")
    @PutMapping()
    public ApiResult<?> update(@RequestBody ShopArticle shopArticle) {
        if (shopArticleService.updateById(shopArticle)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('shop:shopArticle:remove')")
    @OperationLog
    @Operation(summary = "删除商品文章")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (shopArticleService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('shop:shopArticle:save')")
    @OperationLog
    @Operation(summary = "批量添加商品文章")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<ShopArticle> list) {
        if (shopArticleService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('shop:shopArticle:update')")
    @OperationLog
    @Operation(summary = "批量修改商品文章")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<ShopArticle> batchParam) {
        if (batchParam.update(shopArticleService, "article_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('shop:shopArticle:remove')")
    @OperationLog
    @Operation(summary = "批量删除商品文章")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (shopArticleService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
