package com.ruoyi.oa.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 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.oa.domain.vo.OaEmailTemplateVo;
import com.ruoyi.oa.domain.bo.OaEmailTemplateBo;
import com.ruoyi.oa.service.IOaEmailTemplateService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 邮件模板
 *
 * @author ruoyi
 * @date 2025-07-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/oa/emailTemplate")
public class OaEmailTemplateController extends BaseController {

    private final IOaEmailTemplateService iOaEmailTemplateService;

    /**
     * 查询邮件模板列表
     */
    @GetMapping("/list")
    public TableDataInfo<OaEmailTemplateVo> list(OaEmailTemplateBo bo, PageQuery pageQuery) {
        return iOaEmailTemplateService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出邮件模板列表
     */
    @Log(title = "邮件模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OaEmailTemplateBo bo, HttpServletResponse response) {
        List<OaEmailTemplateVo> list = iOaEmailTemplateService.queryList(bo);
        ExcelUtil.exportExcel(list, "邮件模板", OaEmailTemplateVo.class, response);
    }

    /**
     * 获取邮件模板详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<OaEmailTemplateVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iOaEmailTemplateService.queryById(id));
    }

    /**
     * 新增邮件模板
     */
    @Log(title = "邮件模板", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody OaEmailTemplateBo bo) {
        return toAjax(iOaEmailTemplateService.insertByBo(bo));
    }

    /**
     * 修改邮件模板
     */
    @Log(title = "邮件模板", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody OaEmailTemplateBo bo) {
        return toAjax(iOaEmailTemplateService.updateByBo(bo));
    }

    /**
     * 删除邮件模板
     *
     * @param ids 主键串
     */
    @Log(title = "邮件模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iOaEmailTemplateService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
