<%_ if (position === 'import') { _%>
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import <%= packageName %>.system.service.SmsSupplierService;
    <%_ if (ormTool === 'hibernate') { %>
import <%= packageName %>.system.domain.SmsSupplier;
import <%= packageName %>.system.repository.SmsSupplierRepository;
    <%_ } %>
<%_ } _%>
<%_ if (position === 'classExtend') { _%>
<%_ } _%>
<%_ if (position === 'classImplements') { _%>
<%_ } _%>
<%_ if (position === 'extendsImport') { _%>
import <%= packageName %>.system.repository.SmsSupplierRepository;
<%_ } _%>
<%_ if (position === 'methodInterface') { _%>
<%_ } _%>
<%_ if (position === 'method') { _%>

    public void importSmsTemplateFromSupplier(Long supplierId) {
        try {
            List<SmsTemplate> smsTemplates = smsSupplierService.querySmsTemplateList(supplierId);
            <%_ if (ormTool === 'mybatis') { _%>
            LambdaQueryWrapper<SmsTemplate> queryWrapper = new LambdaQueryWrapper<>();
            <%_ } else { _%>
            ExampleMatcher matcher = ExampleMatcher.matching().withIgnoreNullValues();
            <%_ } _%>

            smsTemplates.forEach(smsTemplate -> {
                <%_ if (ormTool === 'mybatis') { _%>
                queryWrapper.clear();
                queryWrapper.eq(SmsTemplate::getSupplierId, supplierId);
                queryWrapper.eq(SmsTemplate::getCode, smsTemplates.get(0).getCode());
                    <% if (javaVersion === '8') { _%>
                Optional<SmsTemplate> stOptional = Optional.ofNullable(smsTemplateRepository.selectOne(queryWrapper));
                    <%_ } else { _%>
                Optional.ofNullable(smsTemplateRepository.selectOne(queryWrapper)).ifPresentOrElse(
                    <%_ } _%>
                <%_ } else { _%>
                SmsTemplate querySmsTemplate = new SmsTemplate();
                querySmsTemplate.setSupplier(new SmsSupplier().id(supplierId));
                querySmsTemplate.setCode(smsTemplates.get(0).getCode());
                    <% if (javaVersion === '8') { _%>
                Optional<SmsTemplate> stOptional = smsTemplateRepository.findOne(Example.of(querySmsTemplate, matcher));
                    <%_ } else { _%>
                smsTemplateRepository.findOne(Example.of(querySmsTemplate, matcher)).ifPresentOrElse(
                    <%_ } _%>
                <%_ } _%>
                <%_ if (javaVersion === '8') { _%>
                if (stOptional.isPresent()) {
                    SmsTemplate existSmsTemplate = stOptional.get();
                    existSmsTemplate.setContent(smsTemplate.getContent());
                    existSmsTemplate.setEnabled(smsTemplate.getEnabled());
                    existSmsTemplate.setName(smsTemplate.getName());
                    existSmsTemplate.setRemark(smsTemplate.getRemark());
                    existSmsTemplate.setSendType(smsTemplate.getSendType());
                    existSmsTemplate.setTestJson(smsTemplate.getTestJson());
                    existSmsTemplate.setType(smsTemplate.getType());
                    <%_ if (ormTool === 'mybatis') { _%>
                    smsTemplateRepository.updateById(existSmsTemplate);
                    <%_ } else { _%>
                    smsTemplateRepository.save(existSmsTemplate);
                    <%_ } _%>
                } else {
                    <%_ if (ormTool === 'mybatis') { _%>
                    smsTemplate.setSupplierId(supplierId);
                    smsTemplateRepository.insert(smsTemplate);
                    <%_ } else { _%>
                    smsTemplate.setSupplier(smsSupplierRepository.getReferenceById(supplierId));
                    smsTemplateRepository.save(smsTemplate);
                    <%_ } _%>
                }
                <%_ } else { _%>
                    existSmsTemplate -> {
                        existSmsTemplate.setContent(smsTemplate.getContent());
                        existSmsTemplate.setEnabled(smsTemplate.getEnabled());
                        existSmsTemplate.setName(smsTemplate.getName());
                        existSmsTemplate.setRemark(smsTemplate.getRemark());
                        existSmsTemplate.setSendType(smsTemplate.getSendType());
                        existSmsTemplate.setTestJson(smsTemplate.getTestJson());
                        existSmsTemplate.setType(smsTemplate.getType());
                        <%_ if (ormTool === 'mybatis') { _%>
                        smsTemplateRepository.updateById(existSmsTemplate);
                        <%_ } else { _%>
                        smsTemplateRepository.save(existSmsTemplate);
                        <%_ } _%>
                    },
                    () -> {
                    <%_ if (ormTool === 'mybatis') { _%>
                        smsTemplate.setSupplierId(supplierId);
                        smsTemplateRepository.insert(smsTemplate);
                    <%_ } else { _%>
                        smsTemplate.setSupplier(smsSupplierRepository.getReferenceById(supplierId));
                        smsTemplateRepository.save(smsTemplate);
                    <%_ } _%>
                    });
                <%_ } _%>
            });
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
<%_ } _%>
