# EngineeringRequestNotificationController.java

    package cloud.wconnect.frontoffice.controller.engineeringrequest;

    import cloud.wconnect.commonlib.annotation.ApiId;
    import cloud.wconnect.commonlib.component.SessionContext;
    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.cons.CommonErrorI18nConstant;
    import cloud.wconnect.commonlib.cons.EmailConfigCons;
    import cloud.wconnect.commonlib.exception.BadRequestException;
    import cloud.wconnect.commonlib.pojo.dto.UserDTO;
    import cloud.wconnect.commonlib.pojo.dto.UsergroupDTO;
    import cloud.wconnect.commonlib.service.UserInternalService;
    import cloud.wconnect.commonlib.service.UsergroupInternalService;
    import cloud.wconnect.frontoffice.pojo.dto.engineeringrequest.EngineeringRequestNotificationDTO;
    import cloud.wconnect.frontoffice.pojo.dto.engineeringrequest.EngineeringRequestNotificationTargetDTO;
    import cloud.wconnect.frontoffice.service.engineeringrequest.IEngineeringRequestNotificationService;
    import com.google.common.collect.ImmutableMap;
    import com.google.common.collect.Lists;
    import jakarta.validation.Valid;
    import org.apache.commons.collections.CollectionUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;

    import java.util.Collections;
    import java.util.List;
    import java.util.Map;
    import java.util.stream.Collectors;

    /**
     * <p>
     * 控制层：Engineering Request Notification
     * </p>
     *
     * @author yyt
     * @since 2023-03-29
     */
    @RestController
    @RequestMapping("/engineering-request/notification")
    public class EngineeringRequestNotificationController {

        @Autowired
        private IEngineeringRequestNotificationService engineeringRequestNotificationService;
        @Autowired
        private SessionContext sessionContext;
        @Autowired
        private UsergroupInternalService usergroupInternalService;
        @Autowired
        private UserInternalService userInternalService;

        /**
         * 获取通知列表
         *
         * @param property 资产标识
         * @return 通知列表
         * @author yyt
         * @date 2023/3/30
         */
        @ApiId(222)
        @GetMapping
        public EngineeringRequestNotificationDTO getNotification(@RequestParam String property) {
            return engineeringRequestNotificationService.getNotificationDTO(sessionContext.getPropertyId(property));
        }

        /**
         * 新增、修改通知
         *
         * @param property        资产标识
         * @param notificationDTO 配置内容
         * @return 通知 id
         * @author yyt
         * @date 2023/3/30
         */
        @ApiId(223)
        @PostMapping
        public Map saveNotification(@RequestParam String property, @Valid @RequestBody EngineeringRequestNotificationDTO notificationDTO) {
            int propertyId = sessionContext.getPropertyId(property);
            // 检查目标（用户/用户组）是否存在
            checkTargetExist(propertyId, notificationDTO.getToList(), notificationDTO.getCcList());

            return ImmutableMap.of("id", engineeringRequestNotificationService.saveNotification(propertyId, notificationDTO, sessionContext.getLoginUserId()));
        }

        /**
         * 检查目标（用户/用户组）是否存在
         *
         * @param propertyId 资产 id
         * @param toList     目标
         * @param ccList     目标
         * @author yyt
         * @date 2024/3/12
         **/
        private void checkTargetExist(Integer propertyId, List<EngineeringRequestNotificationTargetDTO> toList, List<EngineeringRequestNotificationTargetDTO> ccList) {
            List<Integer> userIds = Lists.newArrayList();
            List<Integer> userGroupIds = Lists.newArrayList();
            if (CollectionUtils.isNotEmpty(toList)) {
                Map<Integer, List<Integer>> targetIdByUserSource = toList.stream().collect(Collectors.groupingBy(EngineeringRequestNotificationTargetDTO::getSource,
                        Collectors.collectingAndThen(Collectors.toList(), item -> item.stream().map(EngineeringRequestNotificationTargetDTO::getIdentityId).toList())));
                userIds.addAll(targetIdByUserSource.getOrDefault(EmailConfigCons.SOURCE_USER, Collections.emptyList()));
                userGroupIds.addAll(targetIdByUserSource.getOrDefault(EmailConfigCons.SOURCE_USERGROUP, Collections.emptyList()));
            }
            if (CollectionUtils.isNotEmpty(ccList)) {
                Map<Integer, List<Integer>> targetIdByUserSource = ccList.stream().collect(Collectors.groupingBy(EngineeringRequestNotificationTargetDTO::getSource,
                        Collectors.collectingAndThen(Collectors.toList(), item -> item.stream().map(EngineeringRequestNotificationTargetDTO::getIdentityId).toList())));
                userIds.addAll(targetIdByUserSource.getOrDefault(EmailConfigCons.SOURCE_USER, Collections.emptyList()));
                userGroupIds.addAll(targetIdByUserSource.getOrDefault(EmailConfigCons.SOURCE_USERGROUP, Collections.emptyList()));
            }

            if (CollectionUtils.isNotEmpty(userIds)) {
                List<UserDTO> userDTOs = userInternalService.getUsers(sessionContext.getTenantSymbolBO().getId(), userIds);
                if (userDTOs.size() != userIds.size()) {
                    throw new BadRequestException(CommonErrorI18nConstant.DATA_NOT_FOUND,
                            BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(), "the user is not found");
                }
            }
            if (CollectionUtils.isNotEmpty(userGroupIds)) {
                List<UsergroupDTO> usergroups = usergroupInternalService.getUsergroups(sessionContext.getTenantSymbolBO().getId(), propertyId, userGroupIds);
                if (usergroups.size() != userGroupIds.size()) {
                    throw new BadRequestException(CommonErrorI18nConstant.DATA_NOT_FOUND,
                            BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(), "the userGroup is not found");
                }
            }
        }

    }