/*
 * Copyright 2015-2016 http://hsweb.me
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.hsweb.web.service.impl.datasource;

import org.hsweb.ezorm.executor.AbstractJdbcSqlExecutor;
import org.hsweb.ezorm.render.support.simple.SimpleSQL;
import org.hsweb.web.bean.common.QueryParam;
import org.hsweb.web.bean.common.UpdateMapParam;
import org.hsweb.web.bean.common.UpdateParam;
import org.hsweb.web.bean.po.datasource.DataSource;
import org.hsweb.web.core.exception.BusinessException;
import org.hsweb.web.dao.datasource.DataSourceMapper;
import org.hsweb.web.service.config.ConfigService;
import org.hsweb.web.service.datasource.DataSourceService;
import org.hsweb.web.service.impl.AbstractServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * 数据源服务类
 * Created by generator
 */
@Service("dataSourceService")
public class DataSourceServiceImpl extends AbstractServiceImpl<DataSource, String> implements DataSourceService {

    private static final String CACHE_NAME = "datasource";

    @Autowired
    protected DataSourceProperties properties;

    @Resource
    protected DataSourceMapper dataSourceMapper;

    @Resource
    protected ConfigService configService;

    @Override
    protected DataSourceMapper getMapper() {
        return this.dataSourceMapper;
    }

    @Override
    @Cacheable(value = CACHE_NAME, key = "'id:'+#id")
    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    public DataSource selectByPk(String id) {
        return super.selectByPk(id);
    }

    @Override
    public String insert(DataSource data) {
        data.setCreateDate(new Date());
        data.setEnabled(1);
        tryValidPo(data);
        DataSource old = selectSingle(QueryParam.build().where("name", data.getName()));
        if (old != null) throw new BusinessException("名称" + data.getName() + "已存在");
        return super.insert(data);
    }

    @Override
    @CacheEvict(allEntries = true)
    public int update(List<DataSource> data) {
        return super.update(data);
    }

    @Override
    @CacheEvict(value = CACHE_NAME, key = "'id:'+#data.id")
    public int update(DataSource data) {
        DataSource old = selectSingle(QueryParam.build()
                .where("name", data.getName())
                .and("id$not", data.getId()));
        if (old != null) throw new BusinessException("名称" + data.getName() + "已存在");
        return getMapper().update(UpdateParam.build(data).excludes("createDate", "enabled"));
    }

    @Override
    @CacheEvict(value = CACHE_NAME, key = "'id:'+#id")
    public void enable(String id) {
        getMapper().update((UpdateParam) UpdateMapParam.build().set("enabled", 1).where("id", id));
    }

    @Override
    @CacheEvict(value = CACHE_NAME, key = "'id:'+#id")
    public void disable(String id) {
        getMapper().update((UpdateParam) UpdateMapParam.build().set("enabled", 0).where("id", id));
    }


    @Override
    @CacheEvict(value = CACHE_NAME, key = "'id:'+#id")
    public int delete(String id) {
        return super.delete(id);
    }

}

@@ -650,121 +650,8 @@
 e;%0A%0A
-import org.hsweb.ezorm.executor.AbstractJdbcSqlExecutor;%0Aimport org.hsweb.ezorm.render.support.simple.SimpleSQL;%0A
 impo
@@ -687,24 +687,24 @@
 QueryParam;%0A
+
 import org.h
@@ -1179,78 +1179,8 @@
 ed;%0A
-import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;%0A
 impo
@@ -1363,68 +1363,8 @@
 le;%0A
-import org.springframework.jdbc.datasource.DataSourceUtils;%0A
 impo
@@ -1573,66 +1573,8 @@
 ce;%0A
-import java.sql.Connection;%0Aimport java.sql.SQLException;%0A
 impo
@@ -2625,16 +2625,39 @@
 tName())
+.or(%22id%22, data.getId())
 );%0A     
@@ -2699,32 +2699,33 @@
 essException(%22%E5%90%8D%E7%A7%B0
+:
 %22 + data.getName
@@ -2720,32 +2720,56 @@
 data.getName() +
+ %22%E6%88%96id:%22 + data.getId() +
  %22%E5%B7%B2%E5%AD%98%E5%9C%A8%22);%0A       
@@ -3284,32 +3284,32 @@
 ame() + %22%E5%B7%B2%E5%AD%98%E5%9C%A8%22);%0A
-
         return g
@@ -3383,16 +3383,41 @@
 nabled%22)
+.where(%22id%22,data.getId())
 );%0A    %7D

insert 时判断id是否已存在