package com.bocloud.ims.service.resource.database;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.driver.database.DatabaseDriver;
import com.bocloud.cmp.entity.enums.CacheMode;
import com.bocloud.cmp.model.database.DbInstanceDbModel;
import com.bocloud.cmp.model.database.DbInstanceModel;
import com.bocloud.cmp.model.database.DbInstanceUserModel;
import com.bocloud.common.encrypt.AESEncrptor;
import com.bocloud.common.model.*;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.GridHelper;
import com.bocloud.common.utils.MapTools;
import com.bocloud.ims.entity.enums.RdsInstanceStatus;
import com.bocloud.ims.entity.middleware.*;
import com.bocloud.ims.entity.resource.CloudVendor;
import com.bocloud.ims.interfaces.resource.database.RdsPublicCloudInstanceService;
import com.bocloud.ims.repository.database.*;
import com.bocloud.ims.repository.resource.CloudVendorRepository;
import com.bocloud.ims.service.utils.ResourceEventPublisher;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Company: 博云 www.bocloud.com
 * @ClassName: RdsServiceImpl
 * @Description:
 * @author: liuyuanyuan
 */
@Service("rdsPublicCloudInstanceService")
public class RdsPublicCloudInstanceServiceImpl implements RdsPublicCloudInstanceService {

    private static Logger logger = LoggerFactory.getLogger(RdsPublicCloudInstanceServiceImpl.class);
    @Autowired
    private DatabaseDriver databaseDriver;
    @Autowired
    private CloudVendorRepository cloudVendorRepository;
    @Autowired
    private RdsPublicCloudInstanceRepository rdsPublicCloudInstanceRepository;
    @Autowired
    private ThreadPoolTaskExecutor taskExecutor;
    @Autowired
    private ResourceEventPublisher resourceEventPublisher;
    @Autowired
    private RdsPublicCLoudFlavorRepository rdsPublicCLoudFlavorRepository;
    @Autowired
    private RdsPublicCloudInstanceParameterGroupRepository rdsPublicCloudInstanceParameterGroupRepository;
    @Autowired
    private RdsUserRepository rdsUserRepository;
    @Autowired
    private RdsDbRepository rdsDbRepository;

    @Override
    public BocloudResult list(Pager pager) {
        GridBean gridBean = null;
        try {
            int page = pager.getPage();
            int rows = pager.getRows();
            List<Param> params = pager.getParams();
            Map<String, String> sorter = pager.getSorter();
            boolean simple = pager.getSimple();

            if (null == params) {
                params = Lists.newArrayList();
            }
            if (null == sorter) {
                sorter = Maps.newHashMap();
            }
            sorter.put("gmtCreate", Common.ONE);
            int total = this.rdsPublicCloudInstanceRepository.count(params);
            if (simple) {
                List<SimpleBean> beans = this.rdsPublicCloudInstanceRepository.list(params, sorter);
                gridBean = new GridBean(1, 1, total, beans);
            } else {
                List<RdsPublicCloudInstance> partitions = this.rdsPublicCloudInstanceRepository.list(page, rows, params, sorter);
                gridBean = GridHelper.getBean(page, rows, total, partitions);
            }
            return new BocloudResult(true, gridBean, "查询成功");
        } catch (Exception e) {
            logger.error("Query dbInstance list fail:", e);
            return new BocloudResult(false, "查询失败");
        }
    }

    @Override
    public BocloudResult detail(Long id) {
        try {
            RdsPublicCloudInstance instance = rdsPublicCloudInstanceRepository.query(id);
//            DbInstanceModel rds = JSONObject.parseObject(JSONObject.parse(String.valueOf(instance)).toString(), DbInstanceModel.class);
//            Long vendorId = rds.getVendorId();
//            CloudVendor vendor = cloudVendorRepository.query(vendorId);
//            if (null == vendor) {
//                logger.info("cloud vendor is not exist!");
//                return new BocloudResult(false, "云平台不存在");
//            }
//            RdsPublicCloudInstance rdsPublicCloudInstance = rdsPublicCloudInstanceRepository.queryByInsId(rds.getInstanceId());
//            converter(rdsPublicCloudInstance);
            return new BocloudResult(true, instance, "查询成功");
        } catch (Exception e) {
            logger.error("Get error message：", e);
            return new BocloudResult(false, "查询失败");
        }
    }

    @Override
    public BocloudResult createUser(Long id, RdsUser rdsUser, RequestContext context) {
        try {
            RdsPublicCloudInstance instance = rdsPublicCloudInstanceRepository.query(id);
            if (null == instance) {
                logger.info("db instance is not exist!");
                return new BocloudResult(false, "rds实例不存在");
            }
            CloudVendor cloudVendor = cloudVendorRepository.query(rdsUser.getVendorId());
            if (null == cloudVendor) {
                logger.info("cloud vendor is not exist!");
                return new BocloudResult(false, "云平台不存在");
            }
            rdsUser.setGmtCreate(new Date());
            rdsUser.setCreatorId(context.getTarget());
            rdsUser.setDeleted(false);
            rdsUser.setStatus(RdsInstanceStatus.Unavailable.name());
            rdsUserRepository.save(rdsUser);
            taskExecutor.execute(() -> {
                String operate = cloudVendor.getType().toLowerCase() + ".rds.user.create";
                String content = "rds实例用户创建失败";
                RdsUser user = null;
                DbInstanceUserModel userModel = new DbInstanceUserModel();
                userModel.setDBInstanceId(instance.getInstanceId());
                userModel.setAccountName(rdsUser.getUsername());
                userModel.setAccountPassword(new AESEncrptor().decrypt(rdsUser.getPassword(), null));
                userModel.setAccountDescription(rdsUser.getRemark());
                userModel.setAccountType(rdsUser.getPermission());
                BocloudResult result = databaseDriver.createAccount(cloudVendor.getUuid(), null, userModel);
                if (result.isSuccess()) {
                    try {
                        while (true) {
                            Thread.sleep(5000);
                            BocloudResult bocloudResult = databaseDriver.getAccount(cloudVendor.getUuid(), null, userModel.getDBInstanceId(), userModel.getAccountName());
                            DbInstanceUserModel model = (DbInstanceUserModel) bocloudResult.getData();
                            if (bocloudResult.isSuccess()) {
                                if (RdsInstanceStatus.Available.name().equals(model.getAccountStatus())) {
                                    content = "rds实例用户创建成功";
                                    user = new RdsUser();
                                    user.setId(rdsUser.getId());
                                    user.setUsername(model.getAccountName());
                                    user.setPermission(model.getAccountType());
                                    user.setStatus(model.getAccountStatus());
                                    user.setRemark(model.getAccountDescription());
                                    user.setVendorId(rdsUser.getVendorId());
                                    user.setRdsId(instance.getId());
                                    user.setPrivileges(model.getDatabasePrivileges().toJSONString());
                                    user.setCreatorId(context.getTarget());
                                    user.setGmtCreate(new Date());
                                    user.setDeleted(false);
                                    break;
                                }
                            } else {
                                logger.error(bocloudResult.getMessage());
                                break;
                            }
                        }
                    } catch (InterruptedException e) {
                        logger.error("create is error:", e);
                    }
                }
                if (null != user) {
                    rdsUserRepository.update(user);
                } else {
                    try {
                        rdsUserRepository.remove(rdsUser.getId(), context.getTarget());
                    } catch (Exception e) {
                        logger.error("rollback is error:", e);
                    }
                }
                resourceEventPublisher.send(new OperateResult(result.isSuccess(), content, operate,
                        MapTools.simpleMap(Common.VENDORID, cloudVendor.getId()), OperateResult.OperateCategory.ResourceEvent, context));
            });
            return new BocloudResult(true, "rds实例用户创建任务已下发");
        } catch (Exception e) {
            logger.error("create rds instance user is error：", e);
            return new BocloudResult(false, "rds实例用户创建失败");
        }
    }

    @Override
    public BocloudResult createDb(Long id, RdsDb rdsDb, RequestContext context) {
        try {
            RdsPublicCloudInstance instance = rdsPublicCloudInstanceRepository.query(id);
            if (null == instance) {
                logger.info("db instance is not exist!");
                return new BocloudResult(false, "rds实例不存在");
            }
            CloudVendor cloudVendor = cloudVendorRepository.query(rdsDb.getVendorId());
            if (null == cloudVendor) {
                logger.info("cloud vendor is not exist!");
                return new BocloudResult(false, "云平台不存在");
            }
            rdsDb.setGmtCreate(new Date());
            rdsDb.setCreatorId(context.getTarget());
            rdsDb.setDeleted(false);
            rdsDb.setStatus(RdsInstanceStatus.Unavailable.name());
            rdsDbRepository.save(rdsDb);
            taskExecutor.execute(() -> {
                String operate = cloudVendor.getType().toLowerCase() + ".rds.db.create";
                String content = "rds实例数据库创建失败";
                RdsDb db = null;
                DbInstanceDbModel dbModel = new DbInstanceDbModel();
                dbModel.setDBInstanceId(instance.getInstanceId());
                dbModel.setDBName(rdsDb.getName());
                dbModel.setCharacterSetName(rdsDb.getCharacterSet());
                dbModel.setDBDescription(rdsDb.getRemark());
                BocloudResult result = databaseDriver.createDatabase(cloudVendor.getUuid(), null, dbModel);
                if (result.isSuccess()) {
                    try {
                        while (true) {
                            Thread.sleep(5000);
                            BocloudResult bocloudResult = databaseDriver.getDb(cloudVendor.getUuid(), null, dbModel.getDBInstanceId(), dbModel.getDBName());
                            DbInstanceDbModel model = (DbInstanceDbModel) bocloudResult.getData();
                            if (bocloudResult.isSuccess()) {
                                if (RdsInstanceStatus.Available.name().equals(model.getDBStatus())) {
                                    content = "rds实例数据库创建成功";
                                    db = new RdsDb();
                                    db.setName(model.getDBName());
                                    db.setStatus(model.getDBStatus());
                                    db.setCharacterSet(model.getCharacterSetName());
                                    db.setRemark(model.getDBDescription());
                                    db.setEngine(model.getEngine());
                                    db.setAccounts(model.getAccounts().toJSONString());
                                    db.setCreatorId(context.getTarget());
                                    db.setGmtCreate(new Date());
                                    db.setVendorId(cloudVendor.getId());
                                    db.setDeleted(false);
                                    rdsDb.setRdsId(rdsDb.getId());
                                    break;
                                }
                            } else {
                                logger.error(bocloudResult.getMessage());
                                break;
                            }
                        }
                    } catch (InterruptedException e) {
                        logger.error("create is error:", e);
                    }
                }
                if (null != db) {
                    rdsDbRepository.update(db);
                } else {
                    try {
                        rdsDbRepository.remove(rdsDb.getId(), context.getTarget());
                    } catch (Exception e) {
                        logger.error("rollback is error:", e);
                    }
                }
                resourceEventPublisher.send(new OperateResult(result.isSuccess(), content, operate,
                        MapTools.simpleMap(Common.VENDORID, cloudVendor.getId()), OperateResult.OperateCategory.ResourceEvent, context));
            });
            return new BocloudResult(true, "rds实例数据库创建任务已下发");
        } catch (Exception e) {
            logger.error("create rds instance db is error：", e);
            return new BocloudResult(false, "rds实例数据库创建失败");
        }
    }

    @Override
    public BocloudResult getUsers(Pager pager) {
        GridBean gridBean = null;
        try {
            int page = pager.getPage();
            int rows = pager.getRows();
            List<Param> params = pager.getParams();
            Map<String, String> sorter = pager.getSorter();
            boolean simple = pager.getSimple();

            if (null == params) {
                params = Lists.newArrayList();
            }
            if (null == sorter) {
                sorter = Maps.newHashMap();
            }
            sorter.put("gmtCreate", Common.ONE);
            int total = this.rdsUserRepository.count(params);
            if (simple) {
                List<SimpleBean> beans = this.rdsUserRepository.list(params, sorter);
                gridBean = new GridBean(1, 1, total, beans);
            } else {
                List<RdsUser> partitions = this.rdsUserRepository.list(page, rows, params, sorter);
                gridBean = GridHelper.getBean(page, rows, total, partitions);
            }
            return new BocloudResult(true, gridBean, "查询成功");
        } catch (Exception e) {
            logger.error("Query dbInstance user list fail:", e);
            return new BocloudResult(false, "查询失败");
        }
    }

    @Override
    public BocloudResult getDbs(Pager pager) {
        GridBean gridBean = null;
        try {
            int page = pager.getPage();
            int rows = pager.getRows();
            List<Param> params = pager.getParams();
            Map<String, String> sorter = pager.getSorter();
            boolean simple = pager.getSimple();

            if (null == params) {
                params = Lists.newArrayList();
            }
            if (null == sorter) {
                sorter = Maps.newHashMap();
            }
            sorter.put("gmtCreate", Common.ONE);
            int total = this.rdsDbRepository.count(params);
            if (simple) {
                List<SimpleBean> beans = this.rdsDbRepository.list(params, sorter);
                gridBean = new GridBean(1, 1, total, beans);
            } else {
                List<RdsDb> partitions = this.rdsDbRepository.list(page, rows, params, sorter);
                gridBean = GridHelper.getBean(page, rows, total, partitions);
            }
            return new BocloudResult(true, gridBean, "查询成功");
        } catch (Exception e) {
            logger.error("Query dbInstance db list fail:", e);
            return new BocloudResult(false, "查询失败");
        }
    }

    @Override
    public BocloudResult deleteUser(Long id, Long userId, RequestContext context) {
        try {
            RdsPublicCloudInstance instance = rdsPublicCloudInstanceRepository.query(id);
            if (null == instance) {
                logger.info("db instance is not exist!");
                return new BocloudResult(false, "rds实例不存在");
            }
            CloudVendor cloudVendor = cloudVendorRepository.query(instance.getVendorId());
            if (null == cloudVendor) {
                logger.info("cloud vendor is not exist!");
                return new BocloudResult(false, "云平台不存在");
            }
            RdsUser user = rdsUserRepository.query(userId);
            if (null == user) {
                logger.info("db instance user is not exist!");
                return new BocloudResult(false, "rds实例用户不存在");
            }
            BocloudResult result = databaseDriver.deleteAccount(cloudVendor.getUuid(), null, instance.getInstanceId(), user.getUsername());
            if (result.isFailed()) {
                logger.error("delete rds instance user is error：", result.getMessage());
                return new BocloudResult(false, "删除失败");
            }
            rdsUserRepository.remove(userId, context.getTarget());
            return new BocloudResult(true, "删除成功");
        } catch (Exception e) {
            logger.error("delete rds instance user is error：", e);
            return new BocloudResult(false, "rds实例用户删除失败");
        }
    }

    @Override
    public BocloudResult deleteDb(Long id, Long dbId, RequestContext context) {
        try {
            RdsPublicCloudInstance instance = rdsPublicCloudInstanceRepository.query(id);
            if (null == instance) {
                logger.info("db instance is not exist!");
                return new BocloudResult(false, "rds实例不存在");
            }
            CloudVendor cloudVendor = cloudVendorRepository.query(instance.getVendorId());
            if (null == cloudVendor) {
                logger.info("cloud vendor is not exist!");
                return new BocloudResult(false, "云平台不存在");
            }
            RdsDb db = rdsDbRepository.query(dbId);
            if (null == db) {
                logger.info("db instance db is not exist!");
                return new BocloudResult(false, "rds实例数据库不存在");
            }
            BocloudResult result = databaseDriver.deleteDatabase(cloudVendor.getUuid(), null, instance.getInstanceId(), db.getName());
            if (result.isFailed()) {
                logger.error("delete rds instance db is error：", result.getMessage());
                return new BocloudResult(false, "删除失败");
            }
            rdsDbRepository.remove(dbId, context.getTarget());
            return new BocloudResult(true, "删除成功");
        } catch (Exception e) {
            logger.error("delete rds instance db is error：", e);
            return new BocloudResult(false, "rds实例用户删除失败");
        }
    }

    @Override
    public BocloudResult resetAccountPassword(Long id, RdsUser rdsUser, RequestContext context) {
        try {
            RdsPublicCloudInstance instance = rdsPublicCloudInstanceRepository.query(id);
            if (null == instance) {
                logger.info("db instance is not exist!");
                return new BocloudResult(false, "rds实例不存在");
            }
            Long vendorId = instance.getVendorId();
            CloudVendor vendor = cloudVendorRepository.query(vendorId);
            if (null == vendor) {
                logger.info("cloud vendor is not exist!");
                return new BocloudResult(false, "云平台不存在");
            }
            RdsUser user = rdsUserRepository.query(rdsUser.getId());
            if (null == user) {
                logger.info("db instance user is not exist!");
                return new BocloudResult(false, "用户不存在");
            }
            BocloudResult result = databaseDriver.resetAccountPassword(vendor.getUuid(), null, instance.getInstanceId(),
                    rdsUser.getUsername(), rdsUser.getPassword());
            if (result.isFailed()) {
                logger.error("rds instance user reset password is error：", result.getMessage());
                return new BocloudResult(false, "密码重置失败");
            }
            user.setGmtModify(new Date());
            user.setMenderId(context.getTarget());
            rdsUserRepository.update(rdsUser);
            return new BocloudResult(true, "密码重置成功");
        } catch (Exception e) {
            logger.error("rds instance user reset password is error：", e);
            return new BocloudResult(false, "密码重置失败");
        }
    }

    @Override
    public BocloudResult reset(Long id, RdsUser rdsUser, RequestContext context) {
        CloudVendor vendor = null;
        RdsUser user = null;
        RdsPublicCloudInstance instance = null;
        try {
            instance = rdsPublicCloudInstanceRepository.query(id);
            if (null == instance) {
                logger.info("db instance is not exist!");
                return new BocloudResult(false, "rds实例不存在");
            }
            Long vendorId = instance.getVendorId();
            vendor = cloudVendorRepository.query(vendorId);
            if (null == vendor) {
                logger.info("cloud vendor is not exist!");
                return new BocloudResult(false, "云平台不存在");
            }
            user = rdsUserRepository.query(rdsUser.getId());
            if (null == user) {
                logger.info("db instance user is not exist!");
                return new BocloudResult(false, "用户不存在");
            }
            List<JSONObject> oldData = JSONArray.parseArray(user.getPrivileges(), JSONObject.class);
            Map<String, String> map = Maps.newHashMap();
            for (JSONObject oldDatum : oldData) {
                map.put(oldDatum.getString("name"), oldDatum.getString("accountPrivilege"));
            }
            List<JSONObject> newData = JSONArray.parseArray(rdsUser.getPrivileges(), JSONObject.class);
            for (JSONObject newDatum : newData) {
                if (map.containsKey(newDatum.getString("name"))) {
                    BocloudResult result = databaseDriver.grantAccountPrivilege(vendor.getUuid(), null,
                            instance.getInstanceId(), user.getUsername(), newDatum.getString("name"), newDatum.getString("accountPrivilege"));
                    if (result.isFailed()) {
                        logger.error("rds instance user grant password is error：", result.getMessage());
                        return new BocloudResult(false, "授权失败");
                    }
                    map.remove(newDatum.getString("name"));
                }
            }
            for (String name : map.keySet()) {
                BocloudResult result = databaseDriver.revokeAccountPrivilege(vendor.getUuid(), null, instance.getInstanceId(), user.getUsername(), name);
                if (result.isFailed()) {
                    logger.error("rds instance user grant password is error：", result.getMessage());
                    return new BocloudResult(false, "授权失败");
                }
            }
            return new BocloudResult(true, "授权成功");
        } catch (Exception e) {
            logger.error("rds instance user grant password is error：", e);
            return new BocloudResult(false, "授权失败");
        } finally {
            BocloudResult bocloudResult = databaseDriver.getAccount(vendor.getUuid(), null, instance.getInstanceId(), user.getName());
            DbInstanceUserModel model = (DbInstanceUserModel) bocloudResult.getData();
            user.setPrivileges(model.getDatabasePrivileges().toJSONString());
            user.setGmtModify(new Date());
            user.setMenderId(context.getTarget());
            rdsUserRepository.update(user);
        }
    }

    @Override
    public BocloudResult resetAccount(Long id, RdsUser rdsUser, RequestContext context) {
        try {
            RdsPublicCloudInstance instance = rdsPublicCloudInstanceRepository.query(id);
            if (null == instance) {
                logger.info("db instance is not exist!");
                return new BocloudResult(false, "rds实例不存在");
            }
            Long vendorId = instance.getVendorId();
            CloudVendor vendor = cloudVendorRepository.query(vendorId);
            if (null == vendor) {
                logger.info("cloud vendor is not exist!");
                return new BocloudResult(false, "云平台不存在");
            }
            RdsUser user = rdsUserRepository.query(rdsUser.getId());
            if (null == user) {
                logger.info("db instance user is not exist!");
                return new BocloudResult(false, "用户不存在");
            }
            BocloudResult result = databaseDriver.resetAccount(vendor.getUuid(), null, instance.getInstanceId(),
                    rdsUser.getUsername(), rdsUser.getPassword());
            if (result.isFailed()) {
                logger.error("rds instance super user reset account is error：", result.getMessage());
                return new BocloudResult(false, "高级权限用户重置失败");
            }
            user.setGmtModify(new Date());
            user.setMenderId(context.getTarget());
            rdsUserRepository.update(rdsUser);
            return new BocloudResult(true, "高级权限用户重置成功");
        } catch (Exception e) {
            logger.error("rds instance super user reset account is error：", e);
            return new BocloudResult(false, "高级权限用户重置失败");
        }
    }

    @Override
    public BocloudResult restart(Long id, RequestContext context) {
        try {
            RdsPublicCloudInstance instance = rdsPublicCloudInstanceRepository.query(id);
            if (null == instance) {
                logger.info("db instance is not exist!");
                return new BocloudResult(false, "rds实例不存在");
            }
            Long vendorId = instance.getVendorId();
            CloudVendor vendor = cloudVendorRepository.query(vendorId);
            if (null == vendor) {
                logger.info("cloud vendor is not exist!");
                return new BocloudResult(false, "云平台不存在");
            }
            taskExecutor.execute(() -> {
                String operate = vendor.getType().toLowerCase() + ".rds.restart";
                String content = "rds实例重启失败";
                Boolean isSuccess = false;
                BocloudResult result = databaseDriver.restartDBInstance(vendor.getUuid(), null, instance.getInstanceId());
                if (result.isSuccess()) {
                    try {
                        while (true) {
                            Thread.sleep(5000);
                            BocloudResult bocloudResult = databaseDriver.detail(instance.getInstanceId(), vendor.getUuid(), null);
                            if (bocloudResult.isSuccess()) {
                                DbInstanceModel model = (DbInstanceModel) bocloudResult.getData();
                                if (RdsInstanceStatus.Running.name().equals(model.getStatus())) {
                                    isSuccess = true;
                                    content = "rds实例重启成功";
                                    instance.setStatus(RdsInstanceStatus.Running.name());
                                    instance.setGmtModify(new Date());
                                    instance.setMenderId(context.getTarget());
                                    break;
                                }
                            } else {
                                logger.error(bocloudResult.getMessage());
                                break;
                            }
                        }
                    } catch (InterruptedException e) {
                        logger.error("restart is error:", e);
                    }
                }
                if (!isSuccess) {
                    instance.setStatus(RdsInstanceStatus.RebootFailed.name());
                    instance.setGmtModify(new Date());
                    instance.setMenderId(context.getTarget());
                }
                rdsPublicCloudInstanceRepository.update(instance);
                resourceEventPublisher.send(new OperateResult(result.isSuccess(), content, operate,
                        MapTools.simpleMap(Common.VENDORID, vendor.getId()), OperateResult.OperateCategory.ResourceEvent, context));
            });
            instance.setStatus(RdsInstanceStatus.Rebooting.name());
            instance.setMenderId(context.getTarget());
            instance.setGmtModify(new Date());
            rdsPublicCloudInstanceRepository.update(instance);
            return new BocloudResult(true, "rds实例重启任务已下发");
        } catch (Exception e) {
            logger.error("restart is error：", e);
            return new BocloudResult(false, "rds实例重启失败");
        }
    }

    @Override
    public BocloudResult create(RdsPublicCloudInstance instance, RequestContext context) {
        try {
            Long vendorId = instance.getVendorId();
            CloudVendor cloudVendor = cloudVendorRepository.query(vendorId);
            if (null == cloudVendor) {
                return new BocloudResult(false, "云平台不存在");
            }
            String flavorId = instance.getFlavorId();
            RdsPublicCloudFlavor flavor = rdsPublicCLoudFlavorRepository.query(Long.valueOf(flavorId));
            String paramGroupId = instance.getParamGroupId();
            RdsPublicCloudInstanceParameterGroup parameterGroup = rdsPublicCloudInstanceParameterGroupRepository.query(Long.valueOf(paramGroupId));
            instance.setStatus(RdsInstanceStatus.Creating.name());
            instance.setCreatorId(context.getTarget());
            instance.setGmtCreate(new Date());
            instance.setDeleted(false);
            rdsPublicCloudInstanceRepository.save(instance);
            taskExecutor.execute(() -> {
                String operate = cloudVendor.getType().toLowerCase() + ".rds.create";
                String content = "rds实例创建失败";
                RdsPublicCloudInstance publicCloudInstance = null;
                BocloudResult result = databaseDriver.create(cloudVendor.getUuid(), null, convertor(instance, flavor.getFlavorUuid(), parameterGroup.getParameterGroupId()));
                if (result.isSuccess()) {
                    try {
                        DbInstanceModel instanceModel = (DbInstanceModel) result.getData();
                        while (true) {
                            Thread.sleep(30000);
                            BocloudResult bocloudResult = databaseDriver.detail(instanceModel.getInstanceId(), cloudVendor.getUuid(), null);
                            DbInstanceModel model = (DbInstanceModel) bocloudResult.getData();
                            if (bocloudResult.isSuccess()) {
                                if (RdsInstanceStatus.Running.name().equals(model.getStatus())) {
                                    content = "rds实例创建成功";
                                    publicCloudInstance = convertor(model, context.getTarget(), cloudVendor);
                                    break;
                                }
                            } else {
                                logger.error(bocloudResult.getMessage());
                                break;
                            }
                        }
                    } catch (InterruptedException e) {
                        logger.error("create is error:", e);
                    }
                }
                if (null != publicCloudInstance) {
                    publicCloudInstance.setId(instance.getId());
                    rdsPublicCloudInstanceRepository.update(publicCloudInstance);
                }
                resourceEventPublisher.send(new OperateResult(result.isSuccess(), content, operate,
                        MapTools.simpleMap(Common.VENDORID, cloudVendor.getId()), OperateResult.OperateCategory.ResourceEvent, context));
            });
            return new BocloudResult(true, "rds实例创建任务已下发");
        } catch (Exception e) {
            logger.error("create rds instance is error：", e);
            return new BocloudResult(false, "rds实例创建失败");
        }
    }

    private RdsPublicCloudInstance convertor(DbInstanceModel model, Long userId, CloudVendor vendor) {
        RdsPublicCloudInstance rds = new RdsPublicCloudInstance();
        BeanUtils.copyProperties(model, rds);
        rds.setInstanceId(model.getInstanceId());
        rds.setRegion(model.getRegionId());
        rds.setAvailZone(model.getZoneId());
        rds.setDataStoreType(model.getEngine());
        rds.setDataStoreVersion(model.getVersion());
        rds.setName(model.getName());
        rds.setVswitchId(model.getVswitchId());
        rds.setVpcCloudInstanceId(model.getVpcCloudInstanceId());
        rds.setVolSize(model.getDbInstanceStorage());
        rds.setType(model.getInstanceType());    //主备
        rds.setCategory(model.getCategory());
        rds.setCreatorId(userId);
        rds.setGmtCreate(new Date());
        rds.setVendorId(vendor.getId());
        rds.setVendorType(vendor.getType());
        rds.setFlavorId(model.getInstanceClass());
        rds.setVolSize(model.getDisk());
        //M转换GB
        if (null != model.getMemory()) {
            rds.setMemory(model.getMemory().longValue() / 1024);
        }
        //内网连接地址
        rds.setDnsName(model.getConnectionString());
        rds.setDbPort(model.getPort());
        rds.setVpcId(model.getVpcId());
        rds.setInstanceNetworkType(model.getNetworkType());
        rds.setCategory(model.getCategory());
        rds.setDbInstanceNetType(model.getNetType());
        rds.setMaxIops(model.getMaxIOPS());
        rds.setVpcCloudInstanceId(model.getVpcCloudInstanceId());
        rds.setPeriod(model.getPeriod());
        rds.setUsedTime(model.getUsedTime());
        rds.setResourceId(model.getResourceId());
        rds.setAutoRenew(model.getAutoRenew());
        rds.setParamGroupId(model.getDBParamGroupId());
        rds.setTimeZone(model.getDBTimeZone());
        rds.setIgnoreCase(model.getDBIsIgnoreCase());
        rds.setDeleted(false);
        return rds;
    }

    private DbInstanceModel convertor(RdsPublicCloudInstance instance, String flavorUuid, String paramGroup) {
        DbInstanceModel model = new DbInstanceModel();
        BeanUtils.copyProperties(instance, model);
        model.setEngine(instance.getType());
        model.setVersion(instance.getDataStoreVersion());
        model.setInstanceClass(flavorUuid);
        model.setDbInstanceStorage(instance.getVolSize());
        model.setDBInstanceNetType(null == instance.getVpcId() ? "Internet" : "Intranet");
        model.setName(instance.getName());
        model.setPayType(instance.getPayType());
        model.setZoneId(instance.getAvailZone());
        model.setInstanceNetworkType(instance.getInstanceNetworkType());
        model.setVpcId(instance.getVpcId());
        model.setVswitchId(instance.getVswitchId());
        model.setPeriod(instance.getPeriod());
        model.setUsedTime(instance.getUsedTime());
        model.setResourceId(instance.getResourceId());
        model.setDbInstanceStorageType(instance.getVolType());
        model.setAutoRenew(instance.getAutoRenew());
        model.setDBParamGroupId(paramGroup);
        model.setDBTimeZone(instance.getTimeZone());
        model.setDBIsIgnoreCase(instance.getIgnoreCase());
        return model;
    }

    @Override
    public BocloudResult delete(Long id, RequestContext context) {
        try {
            RdsPublicCloudInstance instance = rdsPublicCloudInstanceRepository.query(id);
            if (null == instance) {
                logger.info("db instance is not exist!");
                return new BocloudResult(false, "rds实例不存在");
            }
            Long vendorId = instance.getVendorId();
            CloudVendor vendor = cloudVendorRepository.query(vendorId);
            if (null == vendor) {
                logger.info("cloud vendor is not exist!");
                return new BocloudResult(false, "云平台不存在");
            }
            BocloudResult result = databaseDriver.remove(instance.getInstanceId(), vendor.getUuid(), null);
            if (result.isFailed()) {
                logger.error(result.getMessage());
                return new BocloudResult(false, "释放RDS实例失败");
            }
            rdsPublicCloudInstanceRepository.delete(instance.getId(), context.getTarget());
            return new BocloudResult(true, "释放RDS实例成功");
        } catch (Exception e) {
            logger.error("delete db instance is error：", e);
            return new BocloudResult(false, "释放RDS实例失败");
        }
    }

    private void converter(RdsPublicCloudInstance rdsPublicCloud) {
        if (StringUtils.isNotBlank(rdsPublicCloud.getDBInstanceClassType())) {
            rdsPublicCloud.setDBInstanceClassType(CacheMode.byValue(rdsPublicCloud.getDBInstanceClassType()));
        }
        if (StringUtils.isNotBlank(rdsPublicCloud.getPayType())) {
            rdsPublicCloud.setPayType(CacheMode.byValue(rdsPublicCloud.getPayType()));
        }
        if (StringUtils.isNotBlank(rdsPublicCloud.getAutoUpgradeMinorVersion())) {
            rdsPublicCloud.setAutoUpgradeMinorVersion(CacheMode.byValue(rdsPublicCloud.getAutoUpgradeMinorVersion()));
        }
        if (StringUtils.isNotBlank(rdsPublicCloud.getVolType())) {
            rdsPublicCloud.setVolType(CacheMode.byValue(rdsPublicCloud.getVolType()));
        }
    }


    @Override
    public BocloudResult getErrLog(String params, RequestContext user) {
//        JSONObject object = JSONTools.isJSONObj(params);
//        if (object == null) {
//            return new BocloudResult(false, "参数格式不正确");
//        }
//        DbInstanceModel rds = JSONObject.parseObject(params, DbInstanceModel.class);
//        /*try {
//            SimpleDateFormat normalSdf = new SimpleDateFormat(CmpCommon.TIME_PATTERN);
//            SimpleDateFormat aliSdf = new SimpleDateFormat(CmpCommon.TIME_PATTERN_Z);
//
//            Date startRawDate = normalSdf.parse(rds.getStart_date());
//            Date endRawDate = normalSdf.parse(rds.getEnd_date());
//
//            rds.setStart_date(aliSdf.format(startRawDate).toString());
//            rds.setEnd_date(aliSdf.format(endRawDate).toString());
//
//        } catch (ParseException e1) {
//            logger.error("Query errorLog By RdsModel(" + rds.toString() + ")  message：", e1);
//            return new BocloudResult(false, "请求日期格式解析异常。");
//        }*/
//        try {
//
//            RdsPublicCloudInstance rds1 = rdsPublicCloudInstanceRepository.queryByInsId(rds.getInstanceId());
//            if (null == rds1) {
//                logger.info("db instance is not exist!");
//                return new BocloudResult(false, "实例不存在");
//            }
//            Long vendorId = rds1.getVendorId();
//            CloudVendor vendor = cloudVendorRepository.query(vendorId);
//            if (null == vendor) {
//                logger.info("cloud vendor is not exist!");
//                return new BocloudResult(false, "云平台不存在");
//            }
//            return databaseDriver.getErrLog(rds, vendor.getUuid(), "");
//        } catch (Exception e) {
//            logger.error("Query errorLog By RdsModel(" + rds.toString() + ")  message：", e);
//            return new BocloudResult(false, "请求查询错误日志失败.");
//        }
        return null;
    }

    @Override
    public BocloudResult getSlowLog(String params, RequestContext user) {
//        JSONObject object = JSONTools.isJSONObj(params);
//        if (object == null) {
//            return new BocloudResult(false, "参数格式不正确");
//        }
//        DbInstanceModel rds = JSONObject.parseObject(params, DbInstanceModel.class);
//        /*try {
//            SimpleDateFormat normalSdf = new SimpleDateFormat(CmpCommon.TIME_PATTERN);
//            SimpleDateFormat aliSdf = new SimpleDateFormat(CmpCommon.TIME_PATTERN_Z);
//
//            Date startRawDate = normalSdf.parse(rds.getStart_date());
//            Date endRawDate = normalSdf.parse(rds.getEnd_date());
//
//            rds.setStart_date(aliSdf.format(startRawDate).toString());
//            rds.setEnd_date(aliSdf.format(endRawDate).toString());
//
//        } catch (ParseException e1) {
//            logger.error("Query errorLog By RdsModel(" + rds.toString() + ")  message：", e1);
//            return new BocloudResult(false, "请求日期格式解析异常。");
//        }*/
//        try {
//            RdsPublicCloudInstance rdsPublicCloudInstance = rdsPublicCloudInstanceRepository.queryByInsId(rds.getInstanceId());
//            if (null == rdsPublicCloudInstance) {
//                logger.info("db instance is not exist!");
//                return new BocloudResult(false, "实例不存在");
//            }
//            Long vendorId = rdsPublicCloudInstance.getVendorId();
//            CloudVendor vendor = cloudVendorRepository.query(vendorId);
//            if (null == vendor) {
//                logger.info("cloud vendor is not exist!");
//                return new BocloudResult(false, "云平台不存在");
//            }
//            return databaseDriver.getSlowLog(rds, vendor.getUuid(), "");
//        } catch (Exception e) {
//            logger.error("Query slowLog By RdsModel(" + rds.toString() + ")  message：", e);
//            return new BocloudResult(false, "请求查询慢日志失败.");
//        }
        return null;
    }

    @Override
    public BocloudResult getRegions(Long vendorId, String type, String volType) {
        CloudVendor vendor = cloudVendorRepository.query(vendorId);
        if (null == vendor) {
            logger.info("cloud vendor is not exist!");
            return new BocloudResult(false, "云平台不存在");
        }
        BocloudResult bocloudResult = databaseDriver.getRegions(vendor.getUuid(), "", type, volType);
        if (bocloudResult.isFailed()) {
            logger.error(bocloudResult.getMessage());
            return new BocloudResult(false, "查询失败");
        }
        return new BocloudResult(true, bocloudResult.getData(), "查询成功");
    }

    @Override
    public BocloudResult flavors(String category, Long vendorId, String regionId, String zone, String engine, String version, String volType, String payType) {
        try {
            List<RdsPublicCloudFlavor> flavors = rdsPublicCLoudFlavorRepository.list(category, vendorId, regionId, zone, engine, version, volType, payType);
            return new BocloudResult(true, flavors, "查询成功");
        } catch (Exception e) {
            logger.error("query flavors is error：", e);
            return new BocloudResult(false, "查询失败");
        }
    }

    @Override
    public BocloudResult paramGroups(Long vendorId, String regionId, String engine, String version) {
        try {
            List<RdsPublicCloudInstanceParameterGroup> groups = rdsPublicCloudInstanceParameterGroupRepository.list(vendorId, regionId, engine, version);
            return new BocloudResult(true, groups, "查询成功");
        } catch (Exception e) {
            logger.error("query param groups is error：", e);
            return new BocloudResult(false, "查询失败");
        }
    }

}