# EngineeringRequestUpdateOperation.java

    package cloud.wconnect.frontoffice.service.engineeringrequest.notify;

    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.commonlib.cons.BaseConstant;
    import cloud.wconnect.commonlib.cons.EmailConfigCons;
    import cloud.wconnect.commonlib.pojo.dto.NotifyDTO;
    import cloud.wconnect.commonlib.pojo.dto.NotifyIdentityDTO;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.redis.PropertyI18nRedisService;
    import cloud.wconnect.commonlib.service.NotifyInternalService;
    import cloud.wconnect.commonlib.util.EmailUtil;
    import cloud.wconnect.frontoffice.cons.FunctionPointEnum;
    import cloud.wconnect.frontoffice.cons.engineeringrequest.EngineeringRequestNotifyEnum;
    import cloud.wconnect.frontoffice.entity.engineeringrequest.EngineeringRequest;
    import cloud.wconnect.frontoffice.entity.engineeringrequest.EngineeringRequestNotification;
    import cloud.wconnect.frontoffice.entity.engineeringrequest.EngineeringRequestNotificationTarget;
    import cloud.wconnect.frontoffice.pojo.bo.engineeringrequest.NotifyBodyBO;
    import cloud.wconnect.frontoffice.pojo.vo.engineeringrequest.EngineeringRequestEmailVO;
    import cloud.wconnect.frontoffice.service.engineeringrequest.IEngineeringRequestNotificationService;
    import cloud.wconnect.frontoffice.service.engineeringrequest.IEngineeringRequestNotificationTargetService;
    import cloud.wconnect.frontoffice.service.engineeringrequest.IEngineeringRequestService;
    import com.alibaba.fastjson.JSONObject;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.google.common.collect.Lists;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.thymeleaf.TemplateEngine;
    import org.thymeleaf.context.Context;

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.Objects;

    /**
     * @author: yyt
     * 2024-11-18
     */
    @Service
    @DS("logbook")
    public class EngineeringRequestUpdateOperation implements EngineeringRequestNotifyService{

        @Autowired
        private IEngineeringRequestService engineeringRequestService;
        @Autowired
        private IEngineeringRequestNotificationService engineeringRequestNotificationService;
        @Autowired
        private IEngineeringRequestNotificationTargetService engineeringRequestNotificationTargetService;
        @Autowired
        private PropertyI18nRedisService propertyI18nRedisService;
        @Autowired
        private BeanMapper beanMapper;
        @Autowired
        private TemplateEngine templateEngine;
        @Autowired
        private NotifyInternalService notifyInternalService;

        @Override
        public void notify(NotifyBodyBO notifyBodyBO) {
            EngineeringRequest engineeringRequest = engineeringRequestService.getById(notifyBodyBO.getEngineeringRequestId());
            EngineeringRequestNotification notification = engineeringRequestNotificationService.getEngineeringRequestNotification(engineeringRequest.getPropertyId());
            if (Objects.isNull(notification)) {
                return;
            }
            if (!notification.getStatus()) return;

            // 查询邮件配置用户
            List<EngineeringRequestNotificationTarget> emailTargets = engineeringRequestNotificationTargetService.listByNotificationId(notification.getId());

            Integer propertyId = engineeringRequest.getPropertyId();

            NotifyDTO notifyDTO = new NotifyDTO();
            notifyDTO.setPropertyId(propertyId);
            notifyDTO.setFunctionPoint(FunctionPointEnum.ENGINEERING_REQUEST_OPERATION.getFunctionPoint());

            String defaultSubject = engineeringRequestNotificationService.getDefaultSubject(propertyId, engineeringRequest.getId(), EngineeringRequestNotifyEnum.OPERATION_UPDATED.getCode());
            notifyDTO.setSubject(StringUtils.isNotBlank(notification.getSubject()) ? notification.getSubject() : defaultSubject);

            // 通知人：填写 email、通知配置人
            List<NotifyIdentityDTO> notifyIdentityDTOs = new ArrayList<>(beanMapper.mapList(emailTargets, NotifyIdentityDTO.class));
            if (StringUtils.isNotEmpty(engineeringRequest.getCcemail())) {
                notifyIdentityDTOs.addAll(Arrays.stream(engineeringRequest.getCcemail().split(","))
                        .map(item -> new NotifyIdentityDTO().setTargetType(EmailConfigCons.TARGET_TYPE_TO)
                                .setSource(EmailConfigCons.SOURCE_MANUAL).setEmail(item.trim())
                                .setIdentityId(BaseConstant.ZERO)).toList());
            }
            notifyDTO.setIdentityList(notifyIdentityDTOs);

            JSONObject msg = engineeringRequestNotificationService.generateLineMsg(engineeringRequest, EngineeringRequestNotifyEnum.OPERATION_UPDATED.getCode());
            notifyDTO.setLineContent(Lists.newArrayList(msg));

            // 封装邮件内容
            EngineeringRequestEmailVO emailVO = engineeringRequestNotificationService.packageEmailContent(engineeringRequest);
            Context context = new Context();
            context.setVariable("emailVO", emailVO);

            PropertyDTO propertyDTO = propertyI18nRedisService.get(propertyId);
            String language = propertyDTO.getLanguage();
            String template = "engineeringrequest/engineering-request_";
            notifyDTO.setEmailContent(EmailUtil.getFullEmailContent(defaultSubject, templateEngine.process(template + language, context), language));

            notifyInternalService.sendNotify(notifyDTO);
        }

    }