# ServiceRequestCenterNotifyConfigServiceImpl.java

    package cloud.wconnect.it.service.servicerequestcenter.impl;

    import cloud.wconnect.commonlib.cons.EmailConfigCons;
    import cloud.wconnect.commonlib.cons.LanguageEnum;
    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.TenantInternalService;
    import cloud.wconnect.commonlib.service.UserInternalService;
    import cloud.wconnect.commonlib.util.I18nMessageUtils;
    import cloud.wconnect.commonlib.util.LineMessageUtil;
    import cloud.wconnect.it.cons.servicerequestcenter.ServiceRequestCenterCons;
    import cloud.wconnect.it.cons.servicerequestcenter.ServiceRequestCenterPriorityEnum;
    import cloud.wconnect.it.cons.servicerequestcenter.ServiceRequestCenterStatusEnum;
    import cloud.wconnect.it.cons.servicerequestcenter.ServiceRequestCenterTypeEnum;
    import cloud.wconnect.it.entity.servicerequestcenter.ServiceRequestCenter;
    import cloud.wconnect.it.pojo.vo.servicerequestcenter.ServiceRequestCenterEmailVO;
    import cloud.wconnect.it.service.servicerequestcenter.IServiceRequestCenterNotifyConfigService;
    import cloud.wconnect.it.service.servicerequestcenter.IServiceRequestCenterPropertyService;
    import cloud.wconnect.it.service.servicerequestcenter.IServiceRequestCenterService;
    import com.alibaba.fastjson.JSONObject;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.google.common.collect.Lists;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Service;

    import javax.annotation.Nullable;
    import java.util.List;

    /**
     *
     * @author: yyt
     * 2025-12-17
     */
    @Service
    @DS("it_manage")
    public class ServiceRequestCenterNotifyConfigServiceImpl implements IServiceRequestCenterNotifyConfigService {

        @Autowired
        private IServiceRequestCenterService serviceRequestCenterService;
        @Autowired
        private IServiceRequestCenterPropertyService serviceRequestCenterPropertyService;
        @Autowired
        private TenantInternalService tenantInternalService;
        @Autowired
        private PropertyI18nRedisService propertyI18nRedisService;
        @Autowired
        private UserInternalService userInternalService;
        @Autowired
        private I18nMessageUtils i18nMessageUtils;
        @Autowired
        private LineMessageUtil lineMessageUtil;

        @Value("${here.link.service-request-center.list}")
        private String listPage;

        /**
         * 创建 line msg
         *
         * @return line msg
         * @author yyt
         * @date 2025/12/17
         */
        @Override
        public JSONObject generateLineMsg() {
            // 有待分配的 ticket
            String lineContent = "Ticket awaiting assignment";
            return lineMessageUtil.generateTextMsgByContent(lineContent);
        }

        /**
         * 封装公共邮件内容
         *
         * @param tenantId     租户 id
         * @param request      service request center
         * @param activeType   active type（对应页面不同的 tab）
         * @param notifyUserId 通知人
         * @return email content
         * @author yyt
         * @date 2025/12/17
         */
        @Override
        public ServiceRequestCenterEmailVO packageCommonEmailContent(int tenantId, ServiceRequestCenter request, int activeType, int notifyUserId) {
            PropertyDTO propertyDTO = propertyI18nRedisService.get(getPropertyIdForEmail(tenantId, request, notifyUserId));
            String language = LanguageEnum.ENGLISH.getLanguage();

            ServiceRequestCenterEmailVO emailVO = new ServiceRequestCenterEmailVO();
            emailVO.setPropertyName(getPropertyNameForEmail(tenantId, request));
            emailVO.setFunctionName(i18nMessageUtils.message(ServiceRequestCenterCons.SERVICE_REQUEST_CENTER, language));
            emailVO.setServiceNumber(request.getServiceNumber());
            emailVO.setTitle(request.getTitle());
            emailVO.setTypeStr(i18nMessageUtils.message(ServiceRequestCenterTypeEnum.getI18nCode(request.getType()), language));
            emailVO.setStatusStr(i18nMessageUtils.message(ServiceRequestCenterStatusEnum.getI18nCode(request.getStatus()), language));
            emailVO.setPriorityStr(i18nMessageUtils.message(ServiceRequestCenterPriorityEnum.getI18nCode(request.getPriority()), language));
            emailVO.setHereLink(getHereLink(propertyDTO, activeType, request.getServiceNumber()));
            return emailVO;
        }

        /**
         * 获取页面链接
         *
         * @param propertyDTO 资产
         * @param activeType  active type（对应页面不同的 tab）
         * @return 页面链接
         * @author yyt
         * @date 2025/12/23
         **/
        @Override
        public String getHereLink(PropertyDTO propertyDTO, Integer activeType) {
            return getHereLink(propertyDTO, activeType, null);
        }

        /**
         * 获取页面链接
         *
         * @param propertyDTO   资产
         * @param activeType    active type（对应页面不同的 tab）
         * @param serviceNumber service number
         * @return 页面链接
         * @author yyt
         * @date 2025/12/23
         **/
        @Override
        public String getHereLink(PropertyDTO propertyDTO, Integer activeType, @Nullable String serviceNumber) {
            String hereLink = listPage + "?property=" + propertyDTO.getAbbr() + "&tenant=" + propertyDTO.getTenantSymbol() + "&activeType=" + activeType;
            return serviceNumber == null ? hereLink : hereLink + "&myKeyWords=" + serviceNumber;
        }

        /**
         * 获取通知创建人
         *
         * @param request request
         * @return 创建人
         * @author yyt
         * @date 2025/12/26
         **/
        @Override
        public List<NotifyIdentityDTO> getNotifyCreator(ServiceRequestCenter request) {
            return Lists.newArrayList(new NotifyIdentityDTO()
                    .setTargetType(EmailConfigCons.TARGET_TYPE_TO)
                    .setIdentityId(null)
                    .setEmail(request.getContactEmail())
                    .setSource(EmailConfigCons.SOURCE_MANUAL));
        }

        /**
         * 获取资产 id（主要用于邮件通知中需要指定资产）
         *
         * @param tenantId     租户 id
         * @param request      request
         * @param notifyUserId 通知人
         * @return 资产 id
         * @author yyt
         * @date 2025/12/19
         **/
        @Override
        public Integer getPropertyIdForEmail(int tenantId, ServiceRequestCenter request, int notifyUserId) {
            // 财务类型，返回通知人资产中的任一资产
            if (request.getType() == ServiceRequestCenterTypeEnum.ACCOUNT.getCode()) {
                return userInternalService.getOne(tenantId, notifyUserId).getProperties().get(0).getId();
            } else {
                // 非财务类型，返回创建 request 时绑定的资产中的任一资产
                return serviceRequestCenterPropertyService.listPropertyIds(request.getId()).get(0);
            }
        }

        /**
         * 获取资产名称（主要用于邮件通知中需要指定资产）
         *
         * @param tenantId 租户 id
         * @param request  request
         * @return 资产名称
         * @author yyt
         * @date 2025/12/19
         **/
        @Override
        public String getPropertyNameForEmail(int tenantId, ServiceRequestCenter request) {
            // 财务类型，返回租户名称，因为不属于任一特定资产
            if (request.getType() == ServiceRequestCenterTypeEnum.ACCOUNT.getCode()) {
                return tenantInternalService.getTenantById(tenantId).getName();
            } else {
                // 非财务类型，返回创建 request 时绑定的资产中的所有资产名称
                List<PropertyDTO> propertyDTOS = propertyI18nRedisService.list(serviceRequestCenterPropertyService.listPropertyIds(request.getId()));
                return getPropertyNameForEmail(propertyDTOS);
            }
        }

        /**
         * 获取资产名称（主要用于邮件通知中需要指定资产）
         *
         * @param propertyDTOS request 绑定的资产
         * @return 资产名称
         * @author yyt
         * @date 2025/12/19
         **/
        @Override
        public String getPropertyNameForEmail(List<PropertyDTO> propertyDTOS) {
            // 非财务类型，返回创建 request 时绑定的资产中的所有资产名称
            if (propertyDTOS.size() == 1) {
                return propertyDTOS.get(0).getName();
            } else {
                String otherPropertyDesc = propertyDTOS.size() > 2 ? "other properties" : "other property";
                int propertyCount = propertyDTOS.size() - 1;
                return propertyDTOS.get(0).getName() + " and " + propertyCount + " " + otherPropertyDesc;
            }
        }

    }