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.MallPromotionBo;
import org.dromara.mall.promotion.domain.vo.MallPromotionVo;
import org.dromara.mall.promotion.service.MallPromotionService;
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/promotion")
public class AdminMallPromotionController extends BaseController {

    private final MallPromotionService mallPromotionService;

    /**
     * 查询营销活动列表
     */
    @Operation(summary = "查询营销活动列表", description = "分页查询营销活动列表")
    @SaCheckPermission("mall:promotion:list")
    @GetMapping("/list")
    public TableDataInfo<MallPromotionVo> list(@Parameter(description = "营销活动查询条件") MallPromotionBo bo,
                                               @Parameter(description = "分页参数") PageQuery pageQuery) {
        return mallPromotionService.queryPageList(bo, pageQuery);
    }

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

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

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

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

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

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

}
