# ItticketDistributionOperation.java

    package cloud.wconnect.it.service.itrequest.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.it.cons.FunctionPointEnum;
    import cloud.wconnect.it.cons.itrequest.ItRequestNotifyEnum;
    import cloud.wconnect.it.cons.itrequest.ItRequestNotifyTypeEnum;
    import cloud.wconnect.it.entity.itrequest.Itticket;
    import cloud.wconnect.it.entity.itrequest.ItticketNotification;
    import cloud.wconnect.it.entity.itrequest.ItticketNotificationTarget;
    import cloud.wconnect.it.pojo.bo.itrequest.NotifyBodyBO;
    import cloud.wconnect.it.pojo.vo.itrequest.ItticketEmailVO;
    import cloud.wconnect.it.service.itrequest.IItticketNotificationService;
    import cloud.wconnect.it.service.itrequest.IItticketNotificationTargetService;
    import cloud.wconnect.it.service.itrequest.IItticketService;
    import com.alibaba.fastjson.JSONObject;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import com.google.common.collect.Lists;
    import org.apache.commons.collections.CollectionUtils;
    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.Collections;
    import java.util.List;
    import java.util.Objects;

    /**
     * @author: yyt
     * 2024-04-13
     */
    @Service
    @DS("it_manage")
    public class ItticketDistributionOperation implements ItticketNotifyService {

        @Autowired
        private IItticketService itticketService;
        @Autowired
        private IItticketNotificationService itticketNotificationService;
        @Autowired
        private IItticketNotificationTargetService itticketNotificationTargetService;
        @Autowired
        private NotifyInternalService notifyInternalService;
        @Autowired
        private PropertyI18nRedisService propertyI18nRedisService;
        @Autowired
        private BeanMapper beanMapper;
        @Autowired
        private TemplateEngine templateEngine;

        /**
         * 通知
         *
         * @param notifyBodyBO notifyBodyBO
         * @author yyt
         * @date 2024/4/13
         **/
        @Override
        public void notify(NotifyBodyBO notifyBodyBO) {
            Itticket it = itticketService.getById(notifyBodyBO.getItId());

            // 通知用户 和 负责人 和 通知配置中第2分类下的人
            Integer propertyId = it.getPropertyId();
            ItticketNotification notification = itticketNotificationService.getItticketNotification(propertyId,
                    ItRequestNotifyTypeEnum.CHANGE_IT_REQUEST.getCode());
            if (Objects.nonNull(notification)) {
                if (!notification.getStatus()) {
                    // 状态是关闭时，通知配置无效
                    return;
                }
            }

            // 查询邮件配置用户，发送邮件
            List<ItticketNotificationTarget> emailTargets = Collections.emptyList();
            if(Objects.nonNull(notification)){
                emailTargets = itticketNotificationTargetService.getBaseMapper()
                        .selectList(new QueryWrapper<ItticketNotificationTarget>().lambda()
                                .eq(ItticketNotificationTarget::getNotificationId, notification.getId()));
            }

            PropertyDTO propertyDTO = propertyI18nRedisService.get(propertyId);

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

            String defaultSubject = itticketNotificationService.getDefaultSubject(propertyId, ItRequestNotifyEnum.OPERATION_STATUS_DISTRIBUTION.getCode());

            String subject = defaultSubject;
            if (Objects.nonNull(notification) && StringUtils.isNotBlank(notification.getSubject())) {
                subject = notification.getSubject();
            }
            notifyDTO.setSubject(subject);

            // 发送邮件目标用户：用户，责任人，邮件通知配置用户
            ArrayList<NotifyIdentityDTO> notifyIdentityDTOs = Lists.newArrayList(new NotifyIdentityDTO()
                    .setTargetType(EmailConfigCons.TARGET_TYPE_TO).setIdentityId(it.getUserId())
                    .setEmail(StringUtils.EMPTY).setSource(EmailConfigCons.SOURCE_USER));
            Integer dutyUser = it.getDutyUser();
            if (dutyUser > BaseConstant.ZERO) {
                notifyIdentityDTOs.add(new NotifyIdentityDTO()
                        .setTargetType(EmailConfigCons.TARGET_TYPE_TO).setIdentityId(dutyUser)
                        .setEmail(StringUtils.EMPTY).setSource(EmailConfigCons.SOURCE_USER));
            }
            if (CollectionUtils.isNotEmpty(emailTargets)) {
                notifyIdentityDTOs.addAll(beanMapper.mapList(emailTargets, NotifyIdentityDTO.class));
            }
            notifyDTO.setIdentityList(notifyIdentityDTOs);

            JSONObject msg = itticketNotificationService.generateLineMsg(it, ItRequestNotifyEnum.OPERATION_STATUS_DISTRIBUTION.getCode());
            notifyDTO.setLineContent(Lists.newArrayList(msg));

            // 封装邮件内容
            ItticketEmailVO itEmailVO = itticketNotificationService.packageItDetailEmailContent(it);
            itEmailVO.setOperation(ItRequestNotifyEnum.OPERATION_STATUS_DISTRIBUTION.getCode());

            String language = propertyDTO.getLanguage();

            Context context = new Context();
            context.setVariable("emailVO", itEmailVO);
            context.setVariable("logVO", itticketNotificationService.getLastOperationLogVO(propertyDTO, it.getId(), ItRequestNotifyEnum.OPERATION_STATUS_DISTRIBUTION.getCode()));
            String emailContent = templateEngine.process("itrequest/operate_" + language, context);
            notifyDTO.setEmailContent(EmailUtil.getFullEmailContent(defaultSubject, emailContent, language));

            notifyInternalService.sendNotify(notifyDTO);
        }

    }