package com.gxwebsoft.shop.controller;

import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.shop.entity.ShopFlashSaleActivity;
import com.gxwebsoft.shop.param.ShopFlashSaleActivityParam;
import com.gxwebsoft.shop.service.ShopFlashSaleActivityService;
import com.gxwebsoft.shop.vo.ShopFlashSaleActivityVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
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 xm
 * @since 2026-04-22 17:18:18
 */
@Tag(name = "秒杀活动管理")
@RestController
@RequestMapping("/api/shop/shop-flash-sale-activity")
public class ShopFlashSaleActivityController extends BaseController {
    @Resource
    private ShopFlashSaleActivityService shopFlashSaleActivityService;

//    @PreAuthorize("hasAuthority('shop:shopFlashSaleActivity:list')")
    @Operation(summary = "后台分页查询秒杀活动")
    @GetMapping("/page")
    public ApiResult<PageResult<ShopFlashSaleActivityVO>> page(ShopFlashSaleActivityParam param) {
        // 使用关联查询
        return success(shopFlashSaleActivityService.pageRel(param));
    }

    @Operation(summary = "个人获取秒杀活动数据")
    @GetMapping("/getMyActive")
    public ApiResult<List<ShopFlashSaleActivityVO>> getMyActive(@RequestParam("tenantId") Integer tenantId) {
        // 使用关联查询
        return success(shopFlashSaleActivityService.getMyActive(tenantId));
    }

//    @PreAuthorize("hasAuthority('shop:shopFlashSaleActivity:list')")
    @Operation(summary = "查询全部秒杀活动")
    @GetMapping()
    public ApiResult<List<ShopFlashSaleActivity>> list(ShopFlashSaleActivityParam param) {
        // 使用关联查询
        return success(shopFlashSaleActivityService.listRel(param));
    }

//    @PreAuthorize("hasAuthority('shop:shopFlashSaleActivity:list')")
    @Operation(summary = "根据id查询秒杀活动")
    @GetMapping("/{id}")
    public ApiResult<ShopFlashSaleActivity> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(shopFlashSaleActivityService.getByIdRel(id));
    }

//    @PreAuthorize("hasAuthority('shop:shopFlashSaleActivity:save')")
    @OperationLog
    @Operation(summary = "添加秒杀活动")
    @PostMapping()
    public ApiResult<?> save(@RequestBody ShopFlashSaleActivity shopFlashSaleActivity) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
            shopFlashSaleActivity.setCreator(loginUser.getUserId().toString());
        }
        if (shopFlashSaleActivityService.save(shopFlashSaleActivity)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

//    @PreAuthorize("hasAuthority('shop:shopFlashSaleActivity:update')")
    @OperationLog
    @Operation(summary = "修改秒杀活动")
    @PutMapping()
    public ApiResult<?> update(@RequestBody ShopFlashSaleActivity shopFlashSaleActivity) {
        shopFlashSaleActivity.setUpdater(String.valueOf(getLoginUserId()));
        if (shopFlashSaleActivityService.updateById(shopFlashSaleActivity)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @OperationLog
    @Operation(summary = "开启/关闭秒杀活动状态")
    @PutMapping("/updateStatus")
    public ApiResult<?> updateStatus(@RequestParam("id") Integer id) {
        return success(shopFlashSaleActivityService.updateStatus(id));
    }

    @Operation(summary = "修改秒杀活动排序")
    @PutMapping("/updateSortNumber")
    @Parameters({
            @Parameter(name = "id", description = "活动ID", required = true, example = "1"),
            @Parameter(name = "sortNumber", description = "排序", required = true, example = "2")
    })
    public ApiResult<?> updateSortNumber(@RequestParam("id") Integer id, @RequestParam("sortNumber") Integer sortNumber) {
        return success(shopFlashSaleActivityService.updateSortNumber(id, sortNumber));
    }

//    @PreAuthorize("hasAuthority('shop:shopFlashSaleActivity:remove')")
    @OperationLog
    @Operation(summary = "删除秒杀活动")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (shopFlashSaleActivityService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('shop:shopFlashSaleActivity:save')")
    @OperationLog
    @Operation(summary = "批量添加秒杀活动")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<ShopFlashSaleActivity> list) {
        if (shopFlashSaleActivityService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('shop:shopFlashSaleActivity:update')")
    @OperationLog
    @Operation(summary = "批量修改秒杀活动")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<ShopFlashSaleActivity> batchParam) {
        if (batchParam.update(shopFlashSaleActivityService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('shop:shopFlashSaleActivity:remove')")
    @OperationLog
    @Operation(summary = "批量删除秒杀活动")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (shopFlashSaleActivityService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
