package com.rongwei.bscommon.sys.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rongwei.bscommon.sys.dao.EquMaintenanceReportDao;
import com.rongwei.bscommon.sys.service.EquMaintenanceReportService;
import com.rongwei.bsentity.domain.EquMaintenanceReportDo;
import com.rongwei.rwadmincommon.system.domain.SysUserDo;
import com.rongwei.rwcommon.base.R;
import com.rongwei.rwcommon.vo.MailDo;
import com.rongwei.safecommon.fegin.CXCommonFeginClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@Slf4j
public class EquMaintenanceReportServiceImpl extends ServiceImpl<EquMaintenanceReportDao, EquMaintenanceReportDo> implements EquMaintenanceReportService {

    @Autowired
    private CXCommonFeginClient cxCommonFeginClient;
    @Autowired
    private EquMaintenanceReportDao equMaintenanceReportDao;

    @Override
    public R sendEmail(Map<String, Object> myObject) {
        String reportueser = (String) myObject.get("REPORTUESER");//上报人
        String incidenttime = (String) myObject.get("INCIDENTTIME");//申报时间
        String workshop = (String) myObject.get("WORKSHOP");//使用车间名称
        String workshopid = (String) myObject.get("WORKSHOPID");//使用车间ID
        String equipmentname = (String) myObject.get("EQUIPMENTNAME");//设备名称
        String equipmentnumber = (String) myObject.get("EQUIPMENTNUMBER");//设备编号


        List<SysUserDo> emais = equMaintenanceReportDao.selectEmailList(workshopid);
        StringBuilder sb = new StringBuilder();
        for (SysUserDo user : emais) {
            // 从 SysUserDo 对象中获取邮件地址，并添加到字符串构建器中
            String emailAddress = user.getEmail();
            sb.append(emailAddress).append(",");
        }
        // 移除最后一个逗号
        if (sb.length() > 0) {
            sb.deleteCharAt(sb.length() - 1);
        }
        String[] filteredEmailArray = Arrays.stream(sb.toString().split(","))
                .map(String::trim) // 去除每个邮件地址的前后空白
                .filter(email -> !email.isEmpty()) // 过滤掉空字符串
                .toArray(String[]::new); // 收集为字符串数组

        if (filteredEmailArray.length > 0) {
            MailDo mailDo = new MailDo();
            mailDo.setReceiveEmail(filteredEmailArray);
            mailDo.setNeedTransReceive(false);
            mailDo.setCcEmail(new String[]{});
            mailDo.setSubject("维修上报设备故障流程审批任务提醒");
            String content = "尊敬的各位领导，" +
                    "<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" +
                    reportueser + "于 " + incidenttime +" 对"+workshop+"的"+equipmentname+ "（" + equipmentnumber + "） 的设备进行了设备故障上报登记，请各位领导知悉，请车间主管人员及时确认故障是否真实存在，同时请计划人员到设备管理平台子系统中的“维修上报设备故障”模块中对上报的设备故障进行安排时间维修，及时对设备故障进行解决修复，避免设备在生产时出现停机异常。" +
                    "<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" +
                    "系统登录地址： http://192.168.100.77/#/passport/login";
            mailDo.setContent(content);
            // 发送邮件
            R r = cxCommonFeginClient.sendHtmlMail(mailDo);
        } else {
            return R.error("汇报对象邮箱不能为空");
        }
        return R.ok();
    }


}
