# FdProcedureCheckSendNotifyServiceImpl.java

    package cloud.wconnect.workperformance.service.fdprocedurecheck.impl;

    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.commonlib.cons.CommonFieldI18nConstant;
    import cloud.wconnect.commonlib.cons.FunctionCons;
    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.pojo.dto.line.PublicFlexMsgParm;
    import cloud.wconnect.commonlib.util.EmailUtil;
    import cloud.wconnect.commonlib.util.I18nMessageUtils;
    import cloud.wconnect.commonlib.util.InternationalizationUtils;
    import cloud.wconnect.commonlib.util.LineMessageUtil;
    import cloud.wconnect.workperformance.cons.FunctionPointEnum;
    import cloud.wconnect.workperformance.cons.fdprocedurecheck.FdProcedureCheckHandledResultEnum;
    import cloud.wconnect.workperformance.cons.fdprocedurecheck.FdProcedureCheckNotifyTypeEnum;
    import cloud.wconnect.workperformance.entity.fdprocedurecheck.FdProcedureCheck;
    import cloud.wconnect.workperformance.entity.fdprocedurecheck.FdProcedureCheckEmailConfig;
    import cloud.wconnect.workperformance.pojo.vo.fdprocedurecheck.FdProcedureCheckVO;
    import cloud.wconnect.workperformance.service.fdprocedurecheck.IFdProcedureCheckEmailConfigService;
    import cloud.wconnect.workperformance.service.fdprocedurecheck.IFdProcedureCheckEmailTargetService;
    import cloud.wconnect.workperformance.service.fdprocedurecheck.IFdProcedureCheckSendNotifyService;
    import cloud.wconnect.workperformance.service.fdprocedurecheck.IFdProcedureCheckService;
    import com.alibaba.fastjson.JSONObject;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.google.common.collect.Lists;
    import jakarta.annotation.Resource;
    import org.apache.commons.collections.CollectionUtils;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Service;
    import org.thymeleaf.TemplateEngine;
    import org.thymeleaf.context.Context;

    import java.util.LinkedHashMap;
    import java.util.List;
    import java.util.Locale;
    import java.util.Objects;

    /**
     * Service: 发送通知
     *
     * @author zc
     * @date 2023/12/02
     */
    @Service
    @DS("work_performance")
    public class FdProcedureCheckSendNotifyServiceImpl implements IFdProcedureCheckSendNotifyService {
        @Resource
        private IFdProcedureCheckEmailConfigService emailConfigService;
        @Resource
        private IFdProcedureCheckEmailTargetService emailTargetService;
        @Resource
        private IFdProcedureCheckService fdProcedureCheckService;
        @Resource
        private LineMessageUtil lineMessageUtil;
        @Resource
        private I18nMessageUtils i18nMessageUtils;
        @Resource
        private TemplateEngine templateEngine;
        @Resource
        private BeanMapper beanMapper;

        @Value("${here.link.fd-procedure-check}")
        private String link;

        /**
         * 发送通知
         *
         * @param propertyDTO        资产
         * @param notifyType         通知类型
         * @param procedureCheckList 数据
         * @param total              总数量
         * @author zc
         * @date 2024/10/30
         */
        @Override
        public NotifyDTO getNotifyDTO(PropertyDTO propertyDTO, int notifyType, List<FdProcedureCheck> procedureCheckList, long total) {
            // 查询邮件配置
            FdProcedureCheckEmailConfig emailConfig = emailConfigService.getEmailConfig(propertyDTO.getId(), notifyType);
            if (Objects.isNull(emailConfig)) {
                return null;
            }
            if (!emailConfig.getStatus()) {
                return null;
            }
            List<NotifyIdentityDTO> identityDTOList = emailTargetService.listByConfigId(emailConfig.getId()).stream()
                    .map(item -> beanMapper.map(item, NotifyIdentityDTO.class)).toList();
            if (CollectionUtils.isEmpty(identityDTOList)) {
                return null;
            }

            String hereLink = link + "?tenant=" + propertyDTO.getTenantSymbol() + "&property=" + propertyDTO.getAbbr()
                    + "&handledResult=" + FdProcedureCheckHandledResultEnum.PENDING.getStatus();
            String functionName = i18nMessageUtils.message(FunctionCons.FD_PROCEDURE_CHECK, propertyDTO.getLanguage());
            String defaultSubject = i18nMessageUtils.message(FdProcedureCheckNotifyTypeEnum.getDefaultSubjectI18nCode(notifyType),
                    propertyDTO.getLanguage(), functionName, propertyDTO.getName());
            String subject = StringUtils.isNotBlank(emailConfig.getSubject()) ? emailConfig.getSubject() : defaultSubject;

            // 截取前 10 条数据，转换为 VO
            Locale locale = InternationalizationUtils.getLocale(propertyDTO.getLanguage());
            List<FdProcedureCheckVO> dataList = fdProcedureCheckService.convertToVo(propertyDTO, locale, procedureCheckList);

            // 邮件通知内容
            Context context = new Context();
            context.setVariable("propertyName", propertyDTO.getName());
            context.setVariable("functionName", functionName);
            context.setVariable("hereLink", hereLink);
            context.setVariable("dataCount", total);
            context.setVariable("dataList", dataList);
            String processContent = templateEngine.process("fd/fd-data-handle_" + propertyDTO.getLanguage(), context);
            String emailContent = EmailUtil.getFullEmailContent(defaultSubject, processContent, propertyDTO.getLanguage());

            // LINE 通知内容
            LinkedHashMap<String, String> businessMap = new LinkedHashMap<>();
            businessMap.put(i18nMessageUtils.message(CommonFieldI18nConstant.TOTAL, propertyDTO.getLanguage()), String.valueOf(total));
            JSONObject lineMsg = lineMessageUtil.generatePublicFlexMsg(PublicFlexMsgParm.builder()
                    .propertyId(propertyDTO.getId())
                    .propertyName(propertyDTO.getName())
                    .functionName(functionName)
                    .title("Pending data")
                    .link(link)
                    .businessMap(businessMap)
                    .build());

            return NotifyDTO.builder()
                    .propertyId(propertyDTO.getId())
                    .subject(subject)
                    .emailContent(emailContent)
                    .lineContent(Lists.newArrayList(lineMsg))
                    .identityList(identityDTOList)
                    .functionPoint(FunctionPointEnum.FD_PROCEDURE_CHECK_PENDING_DATA.getFunctionPoint())
                    .build();
        }
    }