package org.dromara.business.controller;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
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.system.domain.bo.SysSmsConfigBo;
import org.dromara.system.domain.vo.SysSmsConfigVo;
import org.dromara.system.service.ISysSmsConfigService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 病例短信配置
 *
 * @author wuzhigang
 * @date 2023-12-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/smsConfig")
public class CaseSmsConfigController extends BaseController {

    private final ISysSmsConfigService caseSmsConfigService;


    /**
     * 初始化病例短信配置
     */
    @GetMapping("/init")
    public R<Void> initSmsConfig() {
        caseSmsConfigService.initSmsConfig();
        return R.ok();
    }

    /**
     * 查询病例短信配置列表
     */
    @GetMapping("/list")
    public TableDataInfo<SysSmsConfigVo> list(SysSmsConfigBo bo, PageQuery pageQuery) {
        return caseSmsConfigService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出病例短信配置列表
     */
    @Log(title = "病例短信配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysSmsConfigBo bo, HttpServletResponse response) {
        List<SysSmsConfigVo> list = caseSmsConfigService.queryList(bo);
        ExcelUtil.exportExcel(list, "病例短信配置", SysSmsConfigVo.class, response);
    }

    /**
     * 获取病例短信配置详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<SysSmsConfigVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(caseSmsConfigService.queryById(id));
    }

    /**
     * 新增病例短信配置
     */
    @Log(title = "病例短信配置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysSmsConfigBo bo) {
        return toAjax(caseSmsConfigService.insertByBo(bo));
    }

    /**
     * 修改病例短信配置
     */
    @Log(title = "病例短信配置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysSmsConfigBo bo) {
        return toAjax(caseSmsConfigService.updateByBo(bo));
    }

    /**
     * 删除病例短信配置
     *
     * @param ids 主键串
     */
    @Log(title = "病例短信配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(caseSmsConfigService.deleteWithValidByIds(List.of(ids), true));
    }
}
