package com.ruoyi.hywz.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.hywz.entity.AdsMatHyxtTaskSendMsgD;
import com.ruoyi.hywz.entity.AdsMatHyxtTaskSendMsgLogD;
import com.ruoyi.hywz.service.IAdsMatHyxtIAppService;
import com.ruoyi.hywz.service.IAdsMatHyxtTaskSendMsgDService;
import com.ruoyi.hywz.service.IAdsMatHyxtTaskSendMsgLogDService;
import com.ruoyi.hywz.utils.IappMsgUtil;
import com.ruoyi.hywz.utils.IdGen;
import com.ruoyi.hywz.utils.MailUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.mail.MessagingException;
import java.io.IOException;
import java.util.Date;

/**
 * @author: jxh
 * @description:
 * @create: 2024/4/15
 */
@Slf4j
@Service
//@AllArgsConstructor
public class AdsMatHyxtIAppServiceImpl implements IAdsMatHyxtIAppService {

    @Value("${msg.url}")
    private String url;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private IAdsMatHyxtTaskSendMsgDService adsMatHyxtTaskSendMsgDService;

    @Autowired
    private IAdsMatHyxtTaskSendMsgLogDService adsMatHyxtTaskSendMsgLogDService;

    @Override
    public void sendApp(String applyId, String msg, String node, SysUser owner, boolean openTask, boolean addLog) {
        // 1.i国网提醒
//        IappMsgUtil.getInstance().sendApp(owner.getPhonenumber(), msg);
        this.requestApi(owner.getPhonenumber(), msg);

        // 2.开启定时提醒任务
        if (openTask) {
            adsMatHyxtTaskSendMsgDService.openTask(applyId, node, msg, owner);
        }

        // 3.保存本次发送记录
        if (addLog) {
            Date date = new Date();
            AdsMatHyxtTaskSendMsgLogD log = new AdsMatHyxtTaskSendMsgLogD();
            log.setId(IdGen.get32UUID());
            log.setApplyId(applyId);
            log.setNodeLabel(node);
            log.setMsgContent(msg);
            log.setOwnerUserId(owner.getUserId());
            log.setOwnerUser(owner.getUserName());
            log.setOwnerUserName(owner.getNickName());
            log.setOwnerUserPhone(owner.getPhonenumber());
            log.setCreateTime(date);
            log.setIsDeleted(0);
            adsMatHyxtTaskSendMsgLogDService.save(log);
        }

        // 发送邮件通知
//        this.sendMail(owner, msg);
    }

    @Override
    public void sendMail(SysUser owner, String msg) {
        String mailId = "wzbxtyy@hn.sgcc.com.cn";
        String mailpsw = "zouqian243@163.com";
        String subject = "【财务资金管控应用】您有一个待处理的任务，请及时处理！登录地址：http://25.212.177.102:19888";
        String targetId = owner.getUserName()+"@hn.sgcc.com.cn";
        try {
            MailUtil.getInstance().singleSendMail(mailId, mailpsw, subject, msg, targetId);
        } catch (MessagingException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public void sendApp(AdsMatHyxtTaskSendMsgD task, boolean addLog) {
        // i国网提醒
//        IappMsgUtil.getInstance().sendApp(task.getOwnerUserPhone(), task.getMsgContent());
        this.requestApi(task.getOwnerUserPhone(), task.getMsgContent());

        // 保存本次发送记录
        if (addLog) {
            Date date = new Date();
            AdsMatHyxtTaskSendMsgLogD log = new AdsMatHyxtTaskSendMsgLogD();
            BeanUtils.copyProperties(task, log);
            log.setId(IdGen.get32UUID());
            log.setCreateTime(date);
            log.setIsDeleted(0);
            adsMatHyxtTaskSendMsgLogDService.save(log);
        }
    }

    private void requestApi(String phone, String content) {
        try {
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);

            // 构造请求参数
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("phoneNum", phone);
            jsonObject.put("content", content);

            HttpEntity<String> request = new HttpEntity<>(jsonObject.toJSONString(), headers);
            restTemplate.postForObject(url, request, String.class);
        } catch (Exception e) {
            log.error("通知发送失败："+e.getMessage());
        }

    }
}
