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.OaEmailAccountVo;
import com.ruoyi.oa.domain.bo.OaEmailAccountBo;
import com.ruoyi.oa.service.IOaEmailAccountService;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.oa.domain.request.EmailSendRequest;

/**
 * 发件人邮箱账号管理
 *
 * @author Joshi
 * @date 2025-07-10
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/oa/emailAccount")
public class OaEmailAccountController extends BaseController {

    private final IOaEmailAccountService iOaEmailAccountService;

    /**
     * 查询发件人邮箱账号管理列表
     */
    @GetMapping("/list")
    public TableDataInfo<OaEmailAccountVo> list(OaEmailAccountBo bo, PageQuery pageQuery) {
        return iOaEmailAccountService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出发件人邮箱账号管理列表
     */
    @Log(title = "发件人邮箱账号管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OaEmailAccountBo bo, HttpServletResponse response) {
        List<OaEmailAccountVo> list = iOaEmailAccountService.queryList(bo);
        ExcelUtil.exportExcel(list, "发件人邮箱账号管理", OaEmailAccountVo.class, response);
    }

    /**
     * 获取发件人邮箱账号管理详细信息
     *
     * @param emailId 主键
     */
    @GetMapping("/{emailId}")
    public R<OaEmailAccountVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long emailId) {
        return R.ok(iOaEmailAccountService.queryById(emailId));
    }

    /**
     * 新增发件人邮箱账号管理
     */
    @Log(title = "发件人邮箱账号管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody OaEmailAccountBo bo) {
        return toAjax(iOaEmailAccountService.insertByBo(bo));
    }

    /**
     * 修改发件人邮箱账号管理
     */
    @Log(title = "发件人邮箱账号管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody OaEmailAccountBo bo) {
        return toAjax(iOaEmailAccountService.updateByBo(bo));
    }

    /**
     * 删除发件人邮箱账号管理
     *
     * @param emailIds 主键串
     */
    @Log(title = "发件人邮箱账号管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{emailIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] emailIds) {
        return toAjax(iOaEmailAccountService.deleteWithValidByIds(Arrays.asList(emailIds), true));
    }

    /**
     * 批量发送邮件（支持网易企业邮箱、飞书SMTP）
     */
    @PostMapping("/sendBatchEmail")
    public R<String> sendBatchEmail(@RequestBody EmailSendRequest request) {
        // 调用Service批量发送邮件
        String result = iOaEmailAccountService.sendBatchEmail(request);
        return R.ok(result);
    }
}
