package com.gxwebsoft.shop.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.shop.service.ShopActiveImageService;
import com.gxwebsoft.shop.entity.ShopActiveImage;
import com.gxwebsoft.shop.param.ShopActiveImageParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.annotation.OperationLog;
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.time.LocalDateTime;
import java.util.List;

/**
 * 推广码底图控制器
 *
 * @author xm
 * @since 2026-04-27 18:02:18
 */
@Tag(name = "推广码底图管理")
@RestController
@RequestMapping("/api/shop/shop-active-image")
public class ShopActiveImageController extends BaseController {
    @Resource
    private ShopActiveImageService shopActiveImageService;

//    @PreAuthorize("hasAuthority('shop:shopActiveImage:list')")
    @Operation(summary = "分页查询推广码底图")
    @GetMapping("/page")
    public ApiResult<PageResult<ShopActiveImage>> page(ShopActiveImageParam param) {
        // 使用关联查询
        return success(shopActiveImageService.pageRel(param));
    }

//    @PreAuthorize("hasAuthority('shop:shopActiveImage:list')")
    @Operation(summary = "查询全部推广码底图")
    @GetMapping()
    public ApiResult<List<ShopActiveImage>> list(ShopActiveImageParam param) {
        // 使用关联查询
        return success(shopActiveImageService.listRel(param));
    }

//    @PreAuthorize("hasAuthority('shop:shopActiveImage:list')")
    @Operation(summary = "根据id查询推广码底图")
    @GetMapping("/{id}")
    public ApiResult<ShopActiveImage> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(shopActiveImageService.getByIdRel(id));
    }

//    @PreAuthorize("hasAuthority('shop:shopActiveImage:save')")
    @OperationLog
    @Operation(summary = "添加推广码底图")
    @PostMapping()
    public ApiResult<?> save(@RequestBody ShopActiveImage shopActiveImage) {
        shopActiveImage.setCreator(String.valueOf(getLoginUserId()));
        shopActiveImage.setCreateTime(LocalDateTime.now());
        if (shopActiveImageService.save(shopActiveImage)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

//    @PreAuthorize("hasAuthority('shop:shopActiveImage:update')")
    @OperationLog
    @Operation(summary = "修改推广码底图")
    @PutMapping()
    public ApiResult<?> update(@RequestBody ShopActiveImage shopActiveImage) {
        shopActiveImage.setUpdater(String.valueOf(getLoginUserId()));
        shopActiveImage.setUpdateTime(LocalDateTime.now());
        if (shopActiveImageService.updateById(shopActiveImage)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

//    @PreAuthorize("hasAuthority('shop:shopActiveImage:remove')")
    @OperationLog
    @Operation(summary = "删除推广码底图")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (shopActiveImageService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('shop:shopActiveImage:save')")
    @OperationLog
    @Operation(summary = "批量添加推广码底图")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<ShopActiveImage> list) {
        if (shopActiveImageService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('shop:shopActiveImage:update')")
    @OperationLog
    @Operation(summary = "批量修改推广码底图")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<ShopActiveImage> batchParam) {
        if (batchParam.update(shopActiveImageService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

//    @PreAuthorize("hasAuthority('shop:shopActiveImage:remove')")
    @OperationLog
    @Operation(summary = "批量删除推广码底图")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (shopActiveImageService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
