package com.fjhx.mail.controller.message;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.fjhx.mail.config.MailServiceConfig;
import com.fjhx.mail.entity.dto.GetMessagePageDto;
import com.fjhx.mail.entity.message.dto.DeleteBatchMailDto;
import com.fjhx.mail.entity.message.dto.DeleteMailDto;
import com.fjhx.mail.entity.message.dto.SendDto;
import com.fjhx.mail.entity.message.dto.SetSeenDto;
import com.fjhx.mail.service.message.InfoService;
import com.ruoyi.common.annotation.NonInterception;
import com.ruoyi.common.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/mailService")
public class MailController {

    @Autowired
    private InfoService infoService;

    /**
     * 获取用户邮箱列表
     */
    @NonInterception
    @GetMapping("/getUserEmailList")
    public Object getUserEmailList() {
        String get = HttpUtil.get(MailServiceConfig.urlPrefix + "getUserEmailList/" + SecurityUtils.getUserId());
        return JSON.parseObject(get);
    }

    /**
     * 获取用户邮箱列表
     */
    @NonInterception
    @PostMapping("/getMessagePage")
    public Object getMessagePage(@RequestBody JSONObject dtoJson) {
        dtoJson.remove("id");//解决前端传的字符串id
        GetMessagePageDto dto = BeanUtil.copyProperties(dtoJson, GetMessagePageDto.class);
        return infoService.getMessagePage(dto);

//        String post = HttpUtil.post(MailServiceConfig.urlPrefix + "getMessagePage", JSONObject.toJSONString(dto));
//        return JSON.parseObject(post);
    }

    /**
     * 获取用户邮箱明细
     */
    @NonInterception
    @PostMapping("/getMessageDetail")
    public Object getMessageDetail(@RequestBody JSONObject dto) {
        String post = HttpUtil.post(MailServiceConfig.urlPrefix + "getMessageDetail", dto.toJSONString());
        return JSON.parseObject(post);
    }

    /**
     * 发送邮件
     */
    @NonInterception
    @PostMapping("/sendMail")
    public Object sendMail(@RequestBody SendDto dto) {
        String result = infoService.sendMail(dto);
        return JSON.parseObject(result);
    }

    /**
     * 删除邮件
     */
    @PostMapping("/deleteMail")
    public void deleteMail(@Validated @RequestBody DeleteMailDto dto) {
        infoService.deleteMail(dto);
    }

    /**
     * 批量删除邮件
     */
    @PostMapping("/deleteBatchMail")
    public void deleteBatchMail(@Validated @RequestBody DeleteBatchMailDto dto) {
        infoService.deleteBatchMail(dto);
    }

    /**
     * 设置已读
     */
    @NonInterception
    @PostMapping("/setSeen")
    public JSONObject setSeen(@Validated @RequestBody SetSeenDto dto) {
        return infoService.setSeen(dto);
    }

    /**
     * 取消已读
     */
    @NonInterception
    @PostMapping("/cancelSeen")
    public JSONObject cancelSeen(@Validated @RequestBody SetSeenDto dto) {
        return infoService.cancelSeen(dto);
    }

}
