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.PaySubInfoVo;
import com.ruoyi.pay.domain.bo.PaySubInfoBo;
import com.ruoyi.pay.service.IPaySubInfoService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 租户订阅信息
 *
 * @author ruoyi
 * @date 2025-05-13
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/pay/subInfo")
public class PaySubInfoController extends BaseController {

    private final IPaySubInfoService iPaySubInfoService;

    /**
     * 查询租户订阅信息列表
     */
    @SaCheckPermission("pay:subInfo:list")
    @GetMapping("/list")
    public TableDataInfo<PaySubInfoVo> list(PaySubInfoBo bo, PageQuery pageQuery) {
        return iPaySubInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出租户订阅信息列表
     */
    @SaCheckPermission("pay:subInfo:export")
    @Log(title = "租户订阅信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PaySubInfoBo bo, HttpServletResponse response) {
        List<PaySubInfoVo> list = iPaySubInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "租户订阅信息", PaySubInfoVo.class, response);
    }

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

    /**
     * 新增租户订阅信息
     */
    @SaCheckPermission("pay:subInfo:add")
    @Log(title = "租户订阅信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PaySubInfoBo bo) {
        //判断该公司是否已经有订阅信息
        PaySubInfoBo queryBo = new PaySubInfoBo();
        queryBo.setTenantId(bo.getTenantId());
        List<PaySubInfoVo> infoVos = iPaySubInfoService.queryList(queryBo);
        if(infoVos!=null&&infoVos.size()>0){
            return R.fail("公司已有订阅信息，不能新增");
        }
        return toAjax(iPaySubInfoService.insertByBo(bo));
    }

    /**
     * 修改租户订阅信息
     */
    @SaCheckPermission("pay:subInfo:edit")
    @Log(title = "租户订阅信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PaySubInfoBo bo) {
        return toAjax(iPaySubInfoService.updateByBo(bo));
    }

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