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.MallGroupBuyBo;
import org.dromara.mall.promotion.domain.vo.MallGroupBuyVo;
import org.dromara.mall.promotion.service.MallGroupBuyService;
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/groupbuy")
public class AdminMallGroupBuyController extends BaseController {

    private final MallGroupBuyService mallGroupBuyService;

    /**
     * 查询拼团活动列表
     */
    @Operation(summary = "查询拼团活动列表", description = "分页查询拼团活动列表")
    @SaCheckPermission("mall:groupbuy:list")
    @GetMapping("/list")
    public TableDataInfo<MallGroupBuyVo> list(@Parameter(description = "拼团活动查询条件") MallGroupBuyBo bo,
                                              @Parameter(description = "分页参数") PageQuery pageQuery) {
        return mallGroupBuyService.queryPageList(bo, pageQuery);
    }

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

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

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

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

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

    /**
     * 启用/禁用拼团活动
     */
    @Operation(summary = "启用/禁用拼团活动", description = "更改拼团活动状态")
    @SaCheckPermission("mall:groupbuy: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(mallGroupBuyService.changeStatus(id, status));
    }

}
