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.MallCouponUserBo;
import org.dromara.mall.coupon.domain.vo.MallCouponUserVo;
import org.dromara.mall.coupon.service.MallCouponUserService;
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/user")
public class AdminMallCouponUserController extends BaseController {

    private final MallCouponUserService mallCouponUserService;

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

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

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

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

}
