package com.ruoyi.pay.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.pay.domain.vo.PaySubPlanVo;
import com.ruoyi.pay.domain.bo.PaySubPlanBo;
import com.ruoyi.pay.service.IPaySubPlanService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订阅套餐
 *
 * @author ruoyi
 * @date 2025-05-13
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/pay/subPlan")
public class PaySubPlanController extends BaseController {

    private final IPaySubPlanService iPaySubPlanService;

    /**
     * 查询订阅套餐列表
     */
    @SaCheckPermission("pay:subPlan:list")
    @GetMapping("/list")
    public TableDataInfo<PaySubPlanVo> list(PaySubPlanBo bo, PageQuery pageQuery) {
        return iPaySubPlanService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出订阅套餐列表
     */
    @SaCheckPermission("pay:subPlan:export")
    @Log(title = "订阅套餐", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PaySubPlanBo bo, HttpServletResponse response) {
        List<PaySubPlanVo> list = iPaySubPlanService.queryList(bo);
        ExcelUtil.exportExcel(list, "订阅套餐", PaySubPlanVo.class, response);
    }

    /**
     * 获取订阅套餐详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("pay:subPlan:query")
    @GetMapping("/{id}")
    public R<PaySubPlanVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iPaySubPlanService.queryById(id));
    }

    /**
     * 新增订阅套餐
     */
    @SaCheckPermission("pay:subPlan:add")
    @Log(title = "订阅套餐", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PaySubPlanBo bo) {
        return toAjax(iPaySubPlanService.insertByBo(bo));
    }

    /**
     * 修改订阅套餐
     */
    @SaCheckPermission("pay:subPlan:edit")
    @Log(title = "订阅套餐", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PaySubPlanBo bo) {
        return toAjax(iPaySubPlanService.updateByBo(bo));
    }

    /**
     * 删除订阅套餐
     *
     * @param ids 主键串
     */
    @SaCheckPermission("pay:subPlan:remove")
    @Log(title = "订阅套餐", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iPaySubPlanService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
