package com.yunzhupaas.base.model.dblink;

import io.swagger.v3.oas.annotations.media.Schema;
import com.yunzhupaas.database.model.entity.DbLinkEntity;
import com.yunzhupaas.database.source.impl.DbOracle;
import com.yunzhupaas.exception.DataException;
import com.yunzhupaas.util.*;
import lombok.Data;

import java.util.Map;

/**
 * 页面显示对象
 *
 * @author 云筑产品开发平台组
 * @version V3.1.0
 * @copyright 深圳市乐程软件有限公司
 * @date 2024/3/12 15:31
 */
@Data
public class DbLinkInfoVO extends DbLinkBaseForm {

    /**
     * 获取连接页面显示对象
     * @param entity 连接实体对象
     * @return 返回显示对象
     * @throws DataException ignore
     */
    public DbLinkInfoVO getDbLinkInfoVO(DbLinkEntity entity) throws DataException {
        DbLinkInfoVO vo = JsonUtilEx.getJsonToBeanEx(entity, DbLinkInfoVO.class);
        vo.setPassword(DesUtil.aesOrDecode(vo.getPassword(), true, true));
        vo.setServiceName(XSSEscape.escape(entity.getDbName()));
        vo.setTableSpace(XSSEscape.escape(entity.getDbTableSpace()));
        vo.setOracleExtend(entity.getOracleExtend() != null && entity.getOracleExtend() == 1);
        if(StringUtil.isNotEmpty(entity.getOracleParam())){
            Map<String, Object> oracleParam = JsonUtil.stringToMap(entity.getOracleParam());
            if(oracleParam.size() > 0){
                vo.setOracleLinkType(oracleParam.get(DbOracle.ORACLE_LINK_TYPE) != null ? oracleParam.get(DbOracle.ORACLE_LINK_TYPE).toString() : null);
                vo.setOracleRole(oracleParam.get(DbOracle.ORACLE_ROLE) != null ? oracleParam.get(DbOracle.ORACLE_ROLE).toString() : null);
                vo.setOracleService(oracleParam.get(DbOracle.ORACLE_SERVICE) != null ? oracleParam.get(DbOracle.ORACLE_SERVICE).toString() : null);
            }
        }
        return vo;
    }

    @Schema(description = "主键")
    private String id;

}

