package org.dromara.mall.coupon.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.coupon.domain.bo.MallCouponBo;
import org.dromara.mall.coupon.domain.vo.CouponStatisticsVo;
import org.dromara.mall.coupon.domain.vo.MallCouponVo;
import org.dromara.mall.coupon.service.MallCouponService;
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/coupon")
public class AdminMallCouponController extends BaseController {

    private final MallCouponService mallCouponService;

    /**
     * 查询优惠券列表
     */
    @Operation(summary = "查询优惠券列表", description = "分页查询优惠券列表")
    @SaCheckPermission("mall:coupon:list")
    @GetMapping("/list")
    public TableDataInfo<MallCouponVo> list(@Parameter(description = "优惠券查询条件") MallCouponBo bo,
                                            @Parameter(description = "分页参数") PageQuery pageQuery) {
        return mallCouponService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出售后申请列表
     */
    @Operation(summary = "导出优惠券列表", description = "导出优惠券到Excel")
    @SaCheckPermission("mall:coupon:export")
    @Log(title = "优惠券", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Parameter(description = "优惠券查询条件") MallCouponBo bo,
                       @Parameter(description = "响应对象") HttpServletResponse response) {
        List<MallCouponVo> list = mallCouponService.queryList(bo);
        ExcelUtil.exportExcel(list, "优惠券", MallCouponVo.class, response);
    }

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

    /**
     * 新增优惠券
     */
    @Operation(summary = "新增优惠券", description = "创建新的优惠券")
    @SaCheckPermission("mall:coupon:add")
    @Log(title = "优惠券", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Parameter(description = "优惠券信息") @Validated @RequestBody MallCouponBo bo) {
        return toAjax(mallCouponService.insertByBo(bo));
    }

    /**
     * 修改优惠券
     */
    @Operation(summary = "修改优惠券", description = "更新优惠券信息")
    @SaCheckPermission("mall:coupon:edit")
    @Log(title = "优惠券", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Parameter(description = "优惠券信息") @Validated @RequestBody MallCouponBo bo) {
        return toAjax(mallCouponService.updateByBo(bo));
    }

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

    /**
     * 启用/禁用优惠券
     */
    @Operation(summary = "启用/禁用优惠券", description = "更改优惠券状态")
    @SaCheckPermission("mall:coupon: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 = "状态", required = true)
                                @NotNull(message = "状态不能为空")
                                @RequestParam Integer status) {
        return toAjax(mallCouponService.changeStatus(id, status));
    }

    /**
     * 获取优惠券统计信息
     */
    @Operation(summary = "获取优惠券统计信息", description = "获取优惠券的发放、使用、核销等统计数据")
    @SaCheckPermission("mall:coupon:query")
    @GetMapping("/statistics")
    public R<CouponStatisticsVo> getStatistics(
            @Parameter(description = "优惠券ID，为空则统计所有优惠券")
            @RequestParam(required = false) Long couponId) {
        return R.ok(mallCouponService.getStatistics(couponId));
    }

}
