package com.ruoyi.cai.controller.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.cai.domain.SmsVerify;
import com.ruoyi.cai.service.SmsVerifyService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
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.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;

/**
 * 短信验证码
 *
 * @author 77
 * @date 2024-01-01
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/cai/smsVerify")
public class SmsVerifyController extends BaseController {

    private final SmsVerifyService smsVerifyService;

    /**
     * 查询短信验证码列表
     */
    @SaCheckPermission("cai:smsVerify:list")
    @GetMapping("/list")
    public TableDataInfo<SmsVerify> list(SmsVerify bo, PageQuery pageQuery) {
        Page<SmsVerify> page = smsVerifyService.page(pageQuery.build(), Wrappers.lambdaQuery(bo));
        return TableDataInfo.build(page);
    }

    /**
     * 获取短信验证码详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("cai:smsVerify:query")
    @GetMapping("/{id}")
    public R<SmsVerify> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(smsVerifyService.getById(id));
    }

    /**
     * 新增短信验证码
     */
    @SaCheckPermission("cai:smsVerify:add")
    @Log(title = "短信验证码", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SmsVerify bo) {
        return toAjax(smsVerifyService.save(bo));
    }

    /**
     * 修改短信验证码
     */
    @SaCheckPermission("cai:smsVerify:edit")
    @Log(title = "短信验证码", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SmsVerify bo) {
        return toAjax(smsVerifyService.updateById(bo));
    }

    /**
     * 删除短信验证码
     *
     * @param ids 主键串
     */
    @SaCheckPermission("cai:smsVerify:remove")
    @Log(title = "短信验证码", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(smsVerifyService.removeBatchByIds(Arrays.asList(ids)));
    }
}
