package com.fjhx.mail.service.enterprise.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.mail.config.MailServiceConfig;
import com.fjhx.mail.entity.enterprise.dto.EnterpriseMailboxDto;
import com.fjhx.mail.entity.enterprise.dto.EnterpriseMailboxSelectDto;
import com.fjhx.mail.entity.enterprise.po.EnterpriseDomain;
import com.fjhx.mail.entity.enterprise.po.EnterpriseMailbox;
import com.fjhx.mail.entity.enterprise.vo.EnterpriseMailboxVo;
import com.fjhx.mail.mapper.enterprise.EnterpriseMailboxMapper;
import com.fjhx.mail.service.enterprise.EnterpriseDomainService;
import com.fjhx.mail.service.enterprise.EnterpriseMailboxService;
import com.obs.services.internal.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.wrapper.IWrapper;
import com.ruoyi.common.utils.wrapper.SqlField;
import com.ruoyi.system.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;


/**
 * <p>
 * 企业邮箱 服务实现类
 * </p>
 *
 * @author
 * @since 2023-04-06
 */
@Service
public class EnterpriseMailboxServiceImpl extends ServiceImpl<EnterpriseMailboxMapper, EnterpriseMailbox> implements EnterpriseMailboxService {

    @Autowired
    private EnterpriseDomainService enterpriseDomainService;

    @Override
    public Page<EnterpriseMailboxVo> getPage(EnterpriseMailboxSelectDto dto) {
        IWrapper<EnterpriseMailbox> wrapper = getWrapper();
        wrapper.keyword(dto, new SqlField("em.mail_user_prefix + '@' + ed.domain_name"));
        wrapper.orderByDesc("em", EnterpriseMailbox::getId);
        Page<EnterpriseMailboxVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        List<EnterpriseMailboxVo> records = page.getRecords();

        if (records.size() == 0) {
            return page;
        }

        // 赋值用户名称
        UserUtil.assignmentNickName(records, EnterpriseMailbox::getUserId, EnterpriseMailboxVo::setUserName);

        return page;
    }

    @Override
    public EnterpriseMailboxVo detail(Long id) {
        EnterpriseMailbox EnterpriseMailbox = this.getById(id);
        EnterpriseMailboxVo result = BeanUtil.toBean(EnterpriseMailbox, EnterpriseMailboxVo.class);
        return result;
    }

    @Transactional
    @Override
    public JSONObject add(EnterpriseMailboxDto enterpriseMailboxDto) {
        enterpriseMailboxDto.setCreateUser(SecurityUtils.getUserId());
        enterpriseMailboxDto.setUpdateUser(SecurityUtils.getUserId());
        enterpriseMailboxDto.setTenantId(SecurityUtils.getTenantId());
        Long domainId = enterpriseMailboxDto.getDomainId();
        EnterpriseDomain domain = enterpriseDomainService.getById(domainId);
        if (domain == null) {
            throw new ServiceException("找不到企业邮箱域名");
        }
        String urlPrefix;
        if (Objects.equals(domain.getType(), 2)) {
            urlPrefix = MailServiceConfig.abroadUrlPrefix;
        } else {
            urlPrefix = MailServiceConfig.urlPrefix;
        }

        String post = HttpUtil.post(urlPrefix + "addEnterpriseMailbox", JSON.toJSONString(enterpriseMailboxDto));
        return JSON.parseObject(post);
    }

    @Override
    public void edit(EnterpriseMailboxDto enterpriseMailboxDto) {
        this.updateById(enterpriseMailboxDto);
    }

    @Override
    public void delete(Long id) {
        this.removeById(id);
    }

}
