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.PayProductsVo;
import com.ruoyi.pay.domain.bo.PayProductsBo;
import com.ruoyi.pay.service.IPayProductsService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 产品
 *
 * @author ruoyi
 * @date 2025-05-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/pay/products")
public class PayProductsController extends BaseController {

    private final IPayProductsService iPayProductsService;

    /**
     * 查询产品列表
     */
    @SaCheckPermission("pay:products:list")
    @GetMapping("/list")
    public TableDataInfo<PayProductsVo> list(PayProductsBo bo, PageQuery pageQuery) {
        return iPayProductsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出产品列表
     */
    @SaCheckPermission("pay:products:export")
    @Log(title = "产品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PayProductsBo bo, HttpServletResponse response) {
        List<PayProductsVo> list = iPayProductsService.queryList(bo);
        ExcelUtil.exportExcel(list, "产品", PayProductsVo.class, response);
    }

    /**
     * 获取产品详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("pay:products:query")
    @GetMapping("/{id}")
    public R<PayProductsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iPayProductsService.queryById(id));
    }

    /**
     * 新增产品
     */
    @SaCheckPermission("pay:products:add")
    @Log(title = "产品", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PayProductsBo bo) {
        return toAjax(iPayProductsService.insertByBo(bo));
    }

    /**
     * 修改产品
     */
    @SaCheckPermission("pay:products:edit")
    @Log(title = "产品", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PayProductsBo bo) {
        return toAjax(iPayProductsService.updateByBo(bo));
    }

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