# EngineeringRequestNotificationTargetServiceImpl.java

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

    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.frontoffice.cons.EmailConfigCons;
    import cloud.wconnect.frontoffice.entity.engineeringrequest.EngineeringRequestNotificationTarget;
    import cloud.wconnect.frontoffice.mapper.engineeringrequest.EngineeringRequestNotificationTargetMapper;
    import cloud.wconnect.frontoffice.pojo.dto.engineeringrequest.EngineeringRequestNotificationDTO;
    import cloud.wconnect.frontoffice.service.engineeringrequest.IEngineeringRequestNotificationTargetService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import org.apache.commons.collections.CollectionUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    import java.util.ArrayList;
    import java.util.List;

    /**
     * <p>
     *  服务实现类
     * </p>
     *
     * @author yyt
     * @since 2023-05-10
     */
    @Service
    @DS("logbook")
    public class EngineeringRequestNotificationTargetServiceImpl extends ServiceImpl<EngineeringRequestNotificationTargetMapper, EngineeringRequestNotificationTarget> implements IEngineeringRequestNotificationTargetService {

        @Autowired
        private BeanMapper beanMapper;

        /**
         * 获取通知配置用户
         *
         * @param notificationId 通知 id
         * @return 配置用户
         * @author yyt
         * @date 2023/3/30
         */
        @Override
        public List<EngineeringRequestNotificationTarget> listByNotificationId(Integer notificationId) {
            return list(new QueryWrapper<EngineeringRequestNotificationTarget>().lambda()
                    .eq(EngineeringRequestNotificationTarget::getNotificationId, notificationId));
        }

        /**
         * 批量添加
         *
         * @param id              notification id
         * @param propertyId      资产 id
         * @param notificationDTO 通知用户
         * @author yyt
         * @date 2023/3/30
         */
        @Override
        @Transactional(rollbackFor = Exception.class)
        public void saveBatch(Integer id, int propertyId, EngineeringRequestNotificationDTO notificationDTO) {
            List<EngineeringRequestNotificationTarget> targets = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(notificationDTO.getToList())) {
                targets.addAll(notificationDTO.getToList().stream().map(item -> beanMapper.map(item, EngineeringRequestNotificationTarget.class,
                        target -> {
                            target.setTargetType(EmailConfigCons.TARGET_TYPE_TO);
                            target.setPropertyId(propertyId);
                            target.setNotificationId(id);
                        })).toList());
            }
            if (CollectionUtils.isNotEmpty(notificationDTO.getCcList())) {
                targets.addAll(notificationDTO.getCcList().stream().map(item -> beanMapper.map(item, EngineeringRequestNotificationTarget.class,
                        target -> {
                            target.setTargetType(EmailConfigCons.TARGET_TYPE_CC);
                            target.setPropertyId(propertyId);
                            target.setNotificationId(id);
                        })).toList());
            }
            if (CollectionUtils.isNotEmpty(targets)) {
                saveBatch(targets);
            }
        }

        /**
         * 清除通知下配置的用户
         *
         * @param id notification id
         * @author yyt
         * @date 2023/3/30
         */
        @Override
        public void clearNotificationTarget(Integer id) {
            remove(new QueryWrapper<EngineeringRequestNotificationTarget>().lambda()
                    .eq(EngineeringRequestNotificationTarget::getNotificationId, id));
        }

        /**
         * 查询通知下配置的用户
         *
         * @param id notification id
         * @return 用户
         * @author yyt
         * @date 2024/11/18
         **/
        @Override
        public List<EngineeringRequestNotificationTarget> list(Integer id) {
            return list(new QueryWrapper<EngineeringRequestNotificationTarget>()
                    .lambda().eq(EngineeringRequestNotificationTarget::getNotificationId, id));
        }

    }