package com.yihu.hos.system.controller;

import com.yihu.hos.common.Services;
import com.yihu.hos.system.model.SystemDatasource;
import com.yihu.hos.system.model.SystemOrganization;
import com.yihu.hos.system.service.DatasourceManager;
import com.yihu.hos.system.service.OrganizationManager;
import com.yihu.hos.web.framework.model.DataGridResult;
import com.yihu.hos.web.framework.model.Result;
import com.yihu.hos.web.framework.util.controller.BaseController;
import net.sf.json.JSONObject;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 数据源管理
 */
@RequestMapping("/datasource")
@Controller
public class DatasourceController extends BaseController {

    @Resource(name = Services.Datasource)
    DatasourceManager datasource;
    @Resource(name = Services.Organization)
    OrganizationManager organization;

    /*
        数据源配置
         */
    @RequestMapping("configSources")
    public String configSources(Model model) {
        model.addAttribute("contentPage", "system/datasource/configSources");
        return "partView";
    }

    /*
    数据源新增/编辑
     */
    @RequestMapping("editorSource")
    public String editorSource(Model model, String sourceId) {
        try {
            if (sourceId != null) {
                SystemDatasource obj = datasource.getDatasourcegById(sourceId);
                model.addAttribute("model", obj);
            }
            model.addAttribute("contentPage", "system/datasource/editorSource");
            return "pageView";
        } catch (Exception ex) {
            model.addAttribute("contentPage", "system/datasource/editorSource");
            return "pageView";
        }
    }

    /*******************************
     * 数据源配置
     ******************************************************/
    /*
       获取数据源列表
    */
    @RequestMapping("getDatasource")
    @ResponseBody
    public Result getDatasource(String orgId, String name, String type, int page, int rows) {
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("orgId", orgId);
            map.put("name", name);
            map.put("type", type);
            DataGridResult dataGridResult = datasource.getDatasource(map, page, rows);
            if (CollectionUtils.isNotEmpty(dataGridResult.getDetailModelList())) {
                for (Object obj : dataGridResult.getDetailModelList()) {
                    SystemDatasource datasource = (SystemDatasource) obj;
                    SystemOrganization _tempOrg = organization.getOrgById(datasource.getOrgId());
                    if (null != _tempOrg) {
                        datasource.setOrgName(_tempOrg.getFullName());
                    }
                }
            }
            return dataGridResult;
        } catch (Exception ex) {
            return Result.error(ex.getMessage());
        }
    }

    /*
      根据组织获取数据源列表
   */
    @RequestMapping("getDatasourceByOrg")
    @ResponseBody
    public Result getDatasourceByOrg(String orgId) {
        try {
            return datasource.getDatasourceByOrg(orgId);
        } catch (Exception ex) {
            return Result.error(ex.getMessage());
        }
    }

    /*
        新增数据源
     */
    @RequestMapping("addDatasource")
    @ResponseBody
    public Result addDatasource(HttpServletRequest request) {
        try {
            SystemDatasource obj = new SystemDatasource();
            BeanUtils.populate(obj, request.getParameterMap());

            return datasource.addDatasource(obj);
        } catch (Exception ex) {
            return Result.error(ex.getMessage());
        }
    }

    /*
        修改数据源
     */
    @RequestMapping("updateDatasource")
    @ResponseBody
    public Result updateDatasource(HttpServletRequest request) {
        try {
            SystemDatasource obj = new SystemDatasource();
            BeanUtils.populate(obj, request.getParameterMap());

            return datasource.updateDatasource(obj);
        } catch (Exception ex) {
            return Result.error(ex.getMessage());
        }
    }

    /*
        测试数据源连接
     */
    @RequestMapping("testDatasource")
    @ResponseBody
    public Result testDatasource(String dataSource, String config) {
        try {
            return datasource.testDatasource(dataSource, config);
        } catch (Exception ex) {
            return Result.error(ex.getMessage());
        }
    }

    /*
        删除数据源
     */
    @RequestMapping("deleteDatasource")
    @ResponseBody
    public Result DeleteDatasource(String id) {
        try {
            return datasource.deleteDatasource(id);
        } catch (Exception ex) {
            return Result.error(ex.getMessage());
        }
    }
}
