<%_ 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());
                Optional.ofNullable(smsTemplateRepository.selectOne(queryWrapper)).ifPresentOrElse(
                <%_ } else { _%>
                SmsTemplate querySmsTemplate = new SmsTemplate();
                querySmsTemplate.setSupplier(new SmsSupplier().id(supplierId));
                querySmsTemplate.setCode(smsTemplates.get(0).getCode());
                smsTemplateRepository.findOne(Example.of(querySmsTemplate, matcher)).ifPresentOrElse(
                <%_ } _%>
                    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);
        }
    }
<%_ } _%>
