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.MallBargainBo;
import org.dromara.mall.promotion.domain.vo.MallBargainVo;
import org.dromara.mall.promotion.service.MallBargainService;
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/bargain")
public class AdminMallBargainController extends BaseController {

    private final MallBargainService mallBargainService;

    /**
     * 查询砍价活动列表
     */
    @Operation(summary = "查询砍价活动列表", description = "分页查询砍价活动列表")
    @SaCheckPermission("mall:bargain:list")
    @GetMapping("/list")
    public TableDataInfo<MallBargainVo> list(@Parameter(description = "砍价活动查询条件") MallBargainBo bo,
                                             @Parameter(description = "分页参数") PageQuery pageQuery) {
        return mallBargainService.queryPageList(bo, pageQuery);
    }

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

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

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

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

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

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

}
