package ink.wgink.module.sms.service.email.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.exceptions.PropertiesException;
import ink.wgink.exceptions.SearchException;
import ink.wgink.module.sms.builder.email.EmailSendBuilder;
import ink.wgink.module.sms.dao.email.IEmailDao;
import ink.wgink.module.sms.pojo.dtos.email.EmailDTO;
import ink.wgink.module.sms.pojo.vos.email.EmailVO;
import ink.wgink.module.sms.service.email.IEmailService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.properties.sms.EmailProperties;
import ink.wgink.util.UUIDUtil;
import ink.wgink.util.map.HashMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import java.util.List;
import java.util.Map;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: EmailServiceImpl
 * @Description: 右键
 * @Author: wanggeng
 * @Date: 2021/5/1 5:26 下午
 * @Version: 1.0
 */
@Service
public class EmailServiceImpl extends DefaultBaseService implements IEmailService {

    @Autowired
    private EmailProperties emailProperties;
    @Autowired
    private IEmailDao emailDao;

    @Override
    public void save(EmailVO emailVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(emailVO);
        params.put("emailId", UUIDUtil.getUUID());
        setSaveInfoByUserId(params, "1");
        emailDao.save(params);
    }

    @Override
    public void remove(List<String> ids) {
        Map<String, Object> params = getHashMap(6);
        params.put("emailIds", ids);
        setUpdateInfo(params);
        emailDao.remove(params);
    }

    @Override
    public void updateSendingStatus(String emailId, int sendingStatus) {
        Map<String, Object> params = getHashMap(4);
        params.put("emailId", emailId);
        params.put("sendingStatus", sendingStatus);
        emailDao.updateSendingStatus(params);
    }

    @Override
    public EmailDTO get(Map<String, Object> params) {
        return emailDao.get(params);
    }

    @Override
    public EmailDTO get(String emailId) {
        Map<String, Object> params = getHashMap(2);
        params.put("emailId", emailId);
        return get(params);
    }

    @Override
    public List<EmailDTO> list(Map<String, Object> params) {
        return emailDao.list(params);
    }

    @Override
    public SuccessResultList<List<EmailDTO>> listPage(ListPage page) {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<EmailDTO> emailDTOs = list(page.getParams());
        PageInfo<EmailDTO> pageInfo = new PageInfo<>(emailDTOs);
        return new SuccessResultList<>(emailDTOs, pageInfo.getPageNum(), pageInfo.getTotal());
    }

    @Override
    public void send(EmailVO emailVO) {
        if (!emailProperties.getActive()) {
            throw new PropertiesException("邮件未激活");
        }
        try {
            EmailSendBuilder.getEmailSend(emailProperties, emailVO.getEmailReceiver()).html(emailVO.getEmailSubject(), emailVO.getEmailContent());
            emailVO.setSendingStatus(1);
        } catch (MessagingException e) {
            LOG.error(e.getMessage(), e);
            emailVO.setSendingStatus(0);
        } finally {
            save(emailVO);
        }
    }

    @Override
    public void reSend(String emailId) {
        if (!emailProperties.getActive()) {
            throw new PropertiesException("邮件未激活");
        }
        EmailDTO emailDTO = get(emailId);
        if (emailDTO == null) {
            throw new SearchException("邮件不存在");
        }
        int sendingStatus = 0;
        try {
            EmailSendBuilder.getEmailSend(emailProperties, emailDTO.getEmailReceiver()).html(emailDTO.getEmailSubject(), emailDTO.getEmailContent());
            sendingStatus = 1;
        } catch (MessagingException e) {
            LOG.error(e.getMessage(), e);
        } finally {
            updateSendingStatus(emailId, sendingStatus);
        }
    }
}
