package org.dromara.mall.promotion.controller.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.mall.promotion.domain.bo.MallSeckillBo;
import org.dromara.mall.promotion.domain.vo.MallSeckillVo;
import org.dromara.mall.promotion.service.MallSeckillService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 秒杀活动管理
 *
 * @author pengles
 */
@Tag(name = "管理端-秒杀活动管理", description = "秒杀活动管理接口")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/mall/seckill")
public class AdminMallSeckillController extends BaseController {

    private final MallSeckillService mallSeckillService;

    /**
     * 查询秒杀活动列表
     */
    @Operation(summary = "查询秒杀活动列表", description = "分页查询秒杀活动列表")
    @SaCheckPermission("mall:seckill:list")
    @GetMapping("/list")
    public TableDataInfo<MallSeckillVo> list(@Parameter(description = "秒杀活动查询条件") MallSeckillBo bo,
                                             @Parameter(description = "分页参数") PageQuery pageQuery) {
        return mallSeckillService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出秒杀活动列表
     */
    @Operation(summary = "导出秒杀活动列表", description = "导出秒杀活动到Excel")
    @SaCheckPermission("mall:seckill:export")
    @Log(title = "秒杀活动", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Parameter(description = "秒杀活动查询条件") MallSeckillBo bo,
                       @Parameter(description = "响应对象") HttpServletResponse response) {
        List<MallSeckillVo> list = mallSeckillService.queryList(bo);
        ExcelUtil.exportExcel(list, "秒杀活动", MallSeckillVo.class, response);
    }

    /**
     * 获取秒杀活动详细信息
     */
    @Operation(summary = "获取秒杀活动详情", description = "根据秒杀活动ID获取详细信息")
    @SaCheckPermission("mall:seckill:query")
    @GetMapping("/{id}")
    public R<MallSeckillVo> getInfo(@Parameter(description = "秒杀活动ID", required = true)
                                    @NotNull(message = "主键不能为空")
                                    @PathVariable Long id) {
        return R.ok(mallSeckillService.queryById(id));
    }

    /**
     * 新增秒杀活动
     */
    @Operation(summary = "新增秒杀活动", description = "创建新的秒杀活动")
    @SaCheckPermission("mall:seckill:add")
    @Log(title = "秒杀活动", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Parameter(description = "秒杀活动信息") @Validated @RequestBody MallSeckillBo bo) {
        return toAjax(mallSeckillService.insertByBo(bo));
    }

    /**
     * 修改秒杀活动
     */
    @Operation(summary = "修改秒杀活动", description = "更新秒杀活动信息")
    @SaCheckPermission("mall:seckill:edit")
    @Log(title = "秒杀活动", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Parameter(description = "秒杀活动信息") @Validated @RequestBody MallSeckillBo bo) {
        return toAjax(mallSeckillService.updateByBo(bo));
    }

    /**
     * 删除秒杀活动
     */
    @Operation(summary = "删除秒杀活动", description = "删除秒杀活动")
    @SaCheckPermission("mall:seckill:remove")
    @Log(title = "秒杀活动", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@Parameter(description = "秒杀活动ID数组", required = true)
                          @NotNull(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(mallSeckillService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 启用/禁用秒杀活动
     */
    @Operation(summary = "启用/禁用秒杀活动", description = "更改秒杀活动状态")
    @SaCheckPermission("mall:seckill:edit")
    @Log(title = "秒杀活动", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus/{id}")
    public R<Void> changeStatus(@Parameter(description = "秒杀活动ID", required = true)
                                @NotNull(message = "主键不能为空")
                                @PathVariable Long id,
                                @Parameter(description = "状态(0启用 1禁用)", required = true)
                                @RequestParam @NotNull(message = "状态不能为空") Integer status) {
        return toAjax(mallSeckillService.changeStatus(id, status));
    }

}
