package com.yihu.hos.tenant.service;

import com.yihu.hos.core.datatype.StringUtil;
import com.yihu.hos.tenant.dao.DBInfoDao;
import com.yihu.hos.tenant.model.DBInfoModel;
import com.yihu.hos.web.framework.model.DetailModelResult;
import com.yihu.hos.web.framework.model.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author HZY
 * @vsrsion 1.0
 * Created at 2016/12/2.
 */
@Service("dBInfoService")
public class DBInfoService {

    public static final String BEAN_ID = "dBInfoService";

    @Autowired
    private DBInfoDao dbInfoDao;
    

    @Value("${spring.data.mongodb.gridFsDatabase}")
    private String dbName;


    public List<DBInfoModel> findDBInfoByName(String name) throws Exception {
        if (name!=null && !StringUtil.isEmpty(name)){
            return dbInfoDao.getDBInfoList(name);
        }else {
            return dbInfoDao.getAllList();
        }
    }

    public Result getDBInfoList(Map<String, Object> params) throws Exception {
        return dbInfoDao.getDBInfoList(params);
    }

    public DBInfoModel getDBInfoById(Long id) throws Exception {
        return dbInfoDao.getEntity(DBInfoModel.class,id);
    }

    public Result addDBInfo(DBInfoModel obj) throws Exception {
        obj.setCreated(new Date());
        obj.setUpdated(new Date());
        dbInfoDao.saveEntity(obj);
        return Result.success("保存成功");
    }

    @Transactional
    public Result updateDBInfo(DBInfoModel obj) throws Exception {
        DBInfoModel DBInfo = dbInfoDao.getEntity(DBInfoModel.class, obj.getId());
        DBInfo.setName(obj.getName());
        DBInfo.setUserName(obj.getUserName());
        DBInfo.setPassword(obj.getPassword());
        DBInfo.setHost(obj.getHost());
        DBInfo.setPort(obj.getPort());
        DBInfo.setValid(obj.getValid());
        DBInfo.setUpdated(new Date());
        dbInfoDao.updateEntity(DBInfo);
        return Result.success("更新成功");
    }

    @Transactional
    public Result deleteDBInfo(Long id) throws Exception {
        DBInfoModel systemApp = dbInfoDao.getEntity(DBInfoModel.class, id);
        dbInfoDao.deleteEntity(systemApp);
        return Result.success("删除成功");
    }


    public DetailModelResult getDataBaseSelectList( String name) {
        try {
            List<DBInfoModel> dbList = findDBInfoByName(name);
            List<Map<String,Object>> detailModelList = new ArrayList<>();
            for (DBInfoModel dbInfoModel : dbList) {
                Map select2 = new HashMap<>();
                select2.put("code" ,dbInfoModel.getId().toString());
                select2.put("value",dbInfoModel.getName());
                detailModelList.add(select2);
            }
            DetailModelResult detailModelResult = DetailModelResult.success("获取数据库实例下拉列表成功");
            detailModelResult.setDetailModelList(detailModelList);
            detailModelResult.setTotalCount(detailModelList.size());
            return detailModelResult;
        } catch (Exception e) {
            return DetailModelResult.error("获取数据库实例下拉列表失败");
        }
    }

}
