package com.simuwang.manage.api.email;

import com.simuwang.base.common.support.MybatisPage;
import com.simuwang.base.pojo.dto.query.EmailPageQuery;
import com.simuwang.base.pojo.vo.*;
import com.simuwang.manage.service.EmailConfigService;
import com.smppw.common.pojo.ResultVo;
import com.smppw.common.pojo.enums.status.ResultCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 接收邮箱配置管理
 * Author:   chenjianhua
 * Date:     2024/9/9 11:40
 * Description: ${DESCRIPTION}
 */
@RestController
@RequestMapping("/v1/email")
public class EmailConfigController{

    @Autowired
    private EmailConfigService emailConfigService;
    private static final Logger logger = LoggerFactory.getLogger(EmailConfigController.class);

    /**
     * 页面展示查询
     * @param emailPageQuery 邮箱
     * @return
     */
    @RequestMapping("search-email-list")
    public MybatisPage<MailboxInfoTableVO> searchEmailConfigList(EmailPageQuery emailPageQuery){
        MybatisPage<MailboxInfoTableVO> result = emailConfigService.searchEmailConfigList(emailPageQuery);
        return result;
    }
    /**
     * 保存邮箱配置
     * @param mailboxInfoVO
     * @return
     */
    @PostMapping("save-email-config")
    public ResultVo saveEmailConfig(@RequestBody MailboxInfoVO mailboxInfoVO){
        ResultVo vo = new ResultVo(ResultCode.SAVE_SUCCESS.getCode());
        try{
            if(mailboxInfoVO.getId() == null && !emailConfigService.checkEmailUnique(mailboxInfoVO.getEmail())){
                vo.setCode(ResultCode.SAVE_FAILED.getCode());
                vo.setMsg("邮箱地址已存在，无需添加");
                return vo;
            }
            emailConfigService.saveEmailConfig(mailboxInfoVO);
            vo.setData(true);
        }catch (Exception e){
            vo.setCode(ResultCode.SAVE_FAILED.getCode());
            logger.error(e.getMessage(),e);
        }
        return vo;
    }

    /**
     * 开启关闭邮箱启动状态
     * @param openStatusVO
     * @return
     */
    @PostMapping("control-email-status")
    public ResultVo controlEmailConfig(@RequestBody OpenStatusVO openStatusVO){
        ResultVo vo = new ResultVo(ResultCode.SAVE_SUCCESS.getCode());
        try{
            emailConfigService.controlEmailConfig(openStatusVO);
            vo.setData(true);
        }catch (Exception e){
            vo.setCode(ResultCode.SAVE_FAILED.getCode());
            logger.error(e.getMessage(),e);
        }
        return vo;
    }

    /**
     * 根据ID查询邮箱配置
     * @param idVO
     * @return
     */
    @GetMapping("search-email-config")
    public ResultVo searchEmailConfig(IdVO idVO){
        ResultVo vo = new ResultVo(ResultCode.SAVE_SUCCESS.getCode());
        try{
            MailboxInfoVO mailboxInfoVO = emailConfigService.searchEmailConfigById(idVO.getId());
            vo.setData(mailboxInfoVO);
        }catch (Exception e){
            vo.setCode(ResultCode.SAVE_FAILED.getCode());
            logger.error(e.getMessage(),e);
        }
        return vo;
    }

    /**
     * 批量删除邮箱配置
     * @param idListVO
     * @return
     */
    @RequestMapping("delete-email-config")
    public boolean deleteEmailConfig(@RequestBody IdListVO idListVO){
        emailConfigService.deleteEmailConfig(idListVO.getIdList());
        return true;
    }

    /**
     * 手动触发指定时间区间的邮件解析
     * @param parseParamVO
     * @return
     */
    @RequestMapping("parse-email")
    public boolean parseEmail(@RequestBody ParseParamVO parseParamVO){
        emailConfigService.parseEmail(parseParamVO);
        return true;
    }

    /**
     * 立即执行一次
     * @param idVO
     * @return
     */
    @RequestMapping("run-once")
    public boolean runOnce(@RequestBody IdVO idVO){
        emailConfigService.runOnce(idVO.getId());
        return true;
    }

    /**
     * 邮箱连接测试
     * @param mailboxInfoVO
     * @return
     */
    @RequestMapping("connect-test")
    public ResultVo connectTest(@RequestBody MailboxInfoVO mailboxInfoVO){
        ResultVo vo = new ResultVo(ResultCode.SUCCESS.getCode());
        try{
            vo = emailConfigService.connectTest(mailboxInfoVO);
        }catch (Exception e){
            vo.setMsg(e.getMessage());
        }
        return vo;
    }
}
