package com.yihu.hos.system.service;

import com.yihu.hos.common.Services;
import com.yihu.hos.system.dao.DatasourceDao;
import com.yihu.hos.system.model.SystemDatasource;
import com.yihu.hos.web.framework.model.ActionResult;
import com.yihu.hos.web.framework.model.DataGridResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by hzp on 2016/1/11.
 */
@Service(Services.Datasource)
@Transactional
public class DatasourceManager {

    public static final String BEAN_ID = Services.Datasource;

    @Resource(name = DatasourceDao.BEAN_ID)
    private DatasourceDao datasourceDao;

    /**
     * 测试连接
     */
    private boolean testDB(String type, String config)
    {
        try {
            datasourceDao.testDatasource(type,config);
            return true;
        } catch (Exception e) {
            return false;
        }
    }


    /**
     * 根据ID获取数据源信息
     * @return
     */
   
    public SystemDatasource getDatasourcegById(String id) throws Exception
    {
        return datasourceDao.getEntity(SystemDatasource.class, id);
    }

    /**
     * 获取数据源列表
     * @param page
     * @param rows
     * @return
     * @throws Exception
     */
   
    public DataGridResult getDatasource(Map<String, Object> conditionMap, int page, int rows) throws Exception{
        return datasourceDao.getDatasource(conditionMap,page,rows);
    }

    /**
     * 根据组织ID获取据源列表
     */
   
    public DataGridResult getDatasourceByOrg(String orgId) throws Exception{
        return datasourceDao.getDatasourceByOrg(orgId);
    }

    /**
     * 测试数据源
     * @param dataSource
     * @param config
     * @return
     */
   
    public ActionResult testDatasource(String dataSource, String config) {
        try {
            boolean re = testDB(dataSource, config);

            if(re)
                return new ActionResult(true,"测试成功！");
            else
                return new ActionResult(false,"测试失败！");
        }
        catch (Exception e)
        {
            System.out.print(e.getMessage());
            return new ActionResult(false,"测试失败！");
        }
    }

    /**
     * 新增数据源
     * @param obj
     * @return
     */
   
    @Transactional
    public ActionResult addDatasource(SystemDatasource obj) {
        try{
            datasourceDao.saveEntity(obj);
            return new ActionResult(true,"新增成功！");
        }
        catch (Exception ex)
        {
            return new ActionResult(false,"新增失败！");
        }
    }

    /**
     * 修改数据源
     * @param obj
     * @return
     */
   
    @Transactional
    public ActionResult updateDatasource(SystemDatasource obj) {
        try{
            datasourceDao.updateEntity(obj);
            return new ActionResult(true,"修改成功！");
        }
        catch (Exception ex)
        {
            return new ActionResult(false,"修改失败！");
        }
    }

    /**
     * 删除数据源
     */
   
    @Transactional
    public ActionResult deleteDatasource(String id)
    {
        try{
            datasourceDao.deleteEntity(SystemDatasource.class,id);
            return new ActionResult(true,"删除成功！");
        }
        catch (Exception ex)
        {
            return new ActionResult(false,"删除失败！");
        }
    }

   
    public List<SystemDatasource> getDatasources(String orgId) {
        try {
            return datasourceDao.getDatasources(orgId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
