<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
              v-model="database"
              placeholder="请输入搜索系统名/模式名"
              clearable
              prefix-icon="Search"
              style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
              :data="databaseList"
              :props="{ label: 'name', children: 'children' }"
              :expand-on-click-node="false"
              :filter-node-method="filterNode"
              ref="databaseTreeRef"
              node-key="id"
              highlight-current
              default-expand-all
              @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" :inline="true" label-width="75px">
          <el-form-item label="对象名称:" prop="userName">
            <el-input
                v-model="queryParams.tabName"
                placeholder="请输入搜索表中文/英文名"
                clearable
                style="width: 240px"
                @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item label="列名称:">
            <el-input
                v-model="queryParams.colName"
                placeholder="请输入搜索字段中文/英文名"
                clearable
                style="width: 240px"
                @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item label="对象类型:">
            <el-select
                v-model="queryParams.tabType"
                placeholder="请输入数据类型"
                clearable
                style="width: 240px"
            >
              <el-option key="TABLE" value="TABLE" label="表"/>
              <el-option key="VIEW" value="VIEW" label="视图"/>
              <el-option key="PROCEDURE" value="PROCEDURE" label="存储过程"/>
              <el-option key="FUNCTION" value="FUNCTION" label="函数"/>
            </el-select>
          </el-form-item>
          <el-form-item label="标签名称:">
            <el-input
                v-model="queryParams.tagName"
                placeholder="请输入搜索标签名称"
                clearable
                style="width: 240px"
                @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item label="补录状态:">
            <el-input
                v-model="queryParams.recStat"
                placeholder="请输入搜索状态"
                clearable
                style="width: 240px"
                @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
<!--        <el-row :gutter="10" class="mb8">-->
<!--          <el-col :span="1.5">-->
<!--            <el-button-->
<!--                type="primary"-->
<!--                plain-->
<!--                icon="Download"-->
<!--                @click="exportData"-->
<!--            >导出</el-button>-->
<!--          </el-col>-->
<!--        </el-row>-->
        <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="系统英文名" width="100" align="center" prop="ssysCd"></el-table-column>
          <el-table-column label="模式名称" align="center" prop="mdlName"></el-table-column>
          <el-table-column label="对象英文名" width="170" align="center" prop="tabEngName" >
            <template #default="scope">
              <el-link type="primary" @click="showColumnDialog(scope.row)" :underline="false">{{ scope.row.tabEngName }}</el-link>
            </template>
          </el-table-column>
          <el-table-column label="对象中文名" width="100" align="center" prop="tabCnName"></el-table-column>
          <el-table-column label="对象标签" width="200" align="center" prop="batchTabClas">
            <template #default="scope">
              <el-popover v-for="item in scope.row.tempTabClas" placement="right" width="auto" trigger="hover">
                <template #reference>
                  <el-tag v-if="item.clasEffFlag && item.clasEffFlag === '1'">{{item.clasName}}</el-tag>
                </template>
                <div>
                  <div style="margin: 5px" v-for="item in scope.row.showTabClas"><el-tag v-if="item.clasEffFlag && item.clasEffFlag === '1'">{{item.clasName + ":" + item.clasValue}}</el-tag></div>
                </div>
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column label="记录数" width="80" align="center" prop="tabRecNum"></el-table-column>
          <el-table-column label="补录对象名称" width="150" align="center" prop="tabCrrctName"></el-table-column>
          <el-table-column label="补录对象描述" width="150" align="center" prop="tabDesc"></el-table-column>
          <el-table-column label="对象类型" align="center" prop="tabType"></el-table-column>
          <el-table-column label="对象治理标志" width="120" align="center" prop="govFlag"></el-table-column>
          <el-table-column label="补录审批状态" width="120" align="center" prop="recStat"></el-table-column>
          <el-table-column label="负责人" align="center" prop="pic"></el-table-column>
          <el-table-column label="采集时间" width="200" align="center" prop="extractUpdTime">
            <template #default="scope">
              <span>{{scope.row.extractUpdTime.replace("T"," ")}}</span>
            </template>
          </el-table-column>
          <el-table-column label="补录时间" align="center" prop="suppUpdTime"></el-table-column>
        </el-table>
        <pagination
            v-show="total > 0"
            :total="total"
            v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize"
            @pagination="getList"
        />
      </el-col>
    </el-row>
    <el-drawer v-model="drawer" size="80%" :show-close="false">
      <template #header>
        <h3>元数据信息</h3>
        <div style="flex: none">
          <el-button @click="cancel">取消</el-button>
          <el-button type="primary" @click="submit">提交</el-button>
        </div>
      </template>
      <template #default>
        <el-row :gutter="20">
            <el-col :span="6">
              <el-form :model="currentMetaData" :inline="true" label-width="120px">
                <el-form-item label="对象英文名">
                  <el-input
                      v-model="currentMetaData.tabEngName"
                      disabled
                  />
                </el-form-item>
                <el-form-item label="对象中文名">
                  <el-input
                      v-model="currentMetaData.tabCnName"
                      disabled
                  />
                </el-form-item>
                <el-form-item label="所属系统">
                  <el-input
                      v-model="currentMetaData.ssysCd"
                      disabled
                  />
                </el-form-item>
                <el-form-item label="所属SCHEMA">
                  <el-input
                      v-model="currentMetaData.mdlName"
                      disabled
                  />
                </el-form-item>
              </el-form>
            </el-col>
            <el-col :span="6">
              <el-form :model="currentMetaData"  label-width="120px">
                <el-form-item label="补录对象名称">
                  <el-input
                      v-model="currentMetaData.tabCrrctName"
                      placeholder="补录对象名称"
                      clearable
                  />
                </el-form-item>
                <el-form-item label="补录对象描述">
                  <el-input
                      v-model="currentMetaData.tabDesc"
                      placeholder="请输入描述内容"
                      clearable
                  />
                </el-form-item>
                <el-form-item label="对象治理标志">
                  <el-select
                      v-model="currentMetaData.govFlag"
                      placeholder="请输入数据类型"
                      clearable
                      style="width: 100%"
                  >
                    <el-option key="0" :value="'0'" label="是"/>
                    <el-option key="1" :value="'1'" label="否"/>
                  </el-select>
                </el-form-item>
                <el-form-item label="对象负责人">
                  <el-input
                      v-model="currentMetaData.pic"
                      placeholder="请输入搜索状态"
                      clearable
                  />
                </el-form-item>
              </el-form>
            </el-col>
            <el-col :span="12" :xs="24">
              <el-divider content-position="left">对象标签集合
                <el-button icon="Plus" type="primary" text @click="openTableTagsDialog">新增</el-button>
              </el-divider>
              <template  v-for="(tag,index) in currentMetaData.tags">
                <el-tag style="margin-left: 10px;margin-top: 10px" :key="tag.clasName +'-'+tag.clasValue"
                        v-if="tag.clasEffFlag && tag.clasEffFlag === '1'"
                        size="large" closable type="primary" @close="handleTableTagClose(index)">
                  {{ tag.clasName}}
                </el-tag>
              </template>
            </el-col>
          </el-row>
        <el-row :gutter="20">
            <el-col :span="24">
              <el-tabs v-model="activeColumnTab" type="border-card" class="full-height-tabs" @tab-change="changeColumnTab">
                <el-tab-pane label="字段信息" name="column">
                  <div class="table-container">
                    <el-table :data="columnList" height="calc(100vh - 395px)">
                      <el-table-column label="字段英文名" width="100" align="center" prop="fldEngName">
                        <template #default="scope">
                          <el-link type="primary" @click="openEditColumnDialog(scope.row,scope.$index)" :underline="false">{{ scope.row.fldEngName }}</el-link>
                        </template>
                      </el-table-column>
                      <el-table-column label="字段中文名" align="center" prop="fldCnName"></el-table-column>
                      <el-table-column label="字段类型" width="170" align="center" prop="fldType" ></el-table-column>
                      <el-table-column label="主键标志" width="100" align="center" prop="pkFlag">
                        <template #default="scope">
                          <span v-if="scope.row.pkFlag === '1' || scope.row.pkFlag === 'Y' || scope.row.pkFlag === true">是</span>
                          <span v-else>否</span>
                        </template>
                      </el-table-column>
    <!--                  <el-table-column label="字段描述" width="250" align="center" prop="fldDesc"></el-table-column>-->
                      <el-table-column label="是否必填" width="70" align="center" prop="requireFlag">
                        <template #default="scope">
                          <span v-if="scope.row.requireFlag === '1' || scope.row.requireFlag === 'Y' || scope.row.requireFlag === true">是</span>
                          <span v-else>否</span>
                        </template>
                      </el-table-column>
                      <el-table-column label="是否索引" width="150" align="center" prop="idxFlag">
                        <template #default="scope">
                          <span v-if="scope.row.idxFlag === '1' || scope.row.idxFlag === 'Y' || scope.row.idxFlag === true">是</span>
                          <span v-else>否</span>
                        </template>
                      </el-table-column>
                      <el-table-column label="字段补录名" width="150" align="center" prop="fldCrrctName">
                        <template #default="scope">
                          <span>{{scope.row.fldCrrctName}}</span>
                        </template>
                      </el-table-column>
                      <el-table-column label="补录主键" align="center" prop="crrctPkFlag">
                        <template #default="scope">
                          <span>{{scope.row.crrctPkFlag}}</span>
                        </template>
                      </el-table-column>
                      <el-table-column label="补录字段描述" align="center" prop="fldDesc">
                        <template #default="scope">
                          <span>{{scope.row.fldDesc}}</span>
                        </template>
                      </el-table-column>
<!--                      <el-table-column label="状态" width="120" align="center" prop="suppRecStat">-->
<!--                        <template #default="scope">-->
<!--                          <span>{{scope.row.suppRecStat}}</span>-->
<!--                        </template>-->
<!--                      </el-table-column>-->
                      <el-table-column label="负责人" align="center" prop="pic">
                        <template #default="scope">
                          <span>{{scope.row.pic}}</span>
                        </template>
                      </el-table-column>
                      <el-table-column label="字段标签" width="150" align="center" prop="fldClas">
                        <template #default="scope">
                          <el-popover v-for="item in scope.row.tempFldClas"  placement="right" width="auto" trigger="hover">
                            <template #reference>
                              <el-tag v-if="item.clasEffFlag === '1'">{{item.clasName}}</el-tag>
                            </template>
                            <div>
                              <div style="margin: 5px" v-for="item in scope.row.showFldClas"><el-tag v-if="item.clasEffFlag === '1'">{{item.clasName + ":" + item.clasValue}}</el-tag></div>
                            </div>
                          </el-popover>
                        </template>
                      </el-table-column>
                      <el-table-column label="字段空值率" align="center" prop="fldNullRate">
                        <template #default="scope">
                          <span>{{scope.row.fldNullRate}}</span>
                        </template>
                      </el-table-column>
                      <el-table-column label="引用字典/标准" align="center"></el-table-column>
                      <el-table-column label="更新时间" align="center" prop="suppUpdTime" width="180">
                        <template #default="scope">
                          <template v-if="scope.row.suppUpdTime && scope.row.suppUpdTime !== ''">
                            <span>{{scope.row.suppUpdTime.replace("T"," ")}}</span>
                          </template>
                        </template>
                      </el-table-column>
                    </el-table>
                  </div>
                </el-tab-pane>
                <el-tab-pane label="业务关系" name="businessRelation">业务关系</el-tab-pane>
                <el-tab-pane label="血缘关系" name="bloodRelation">血缘关系</el-tab-pane>
                <el-tab-pane label="存储过程" name="proc">
                  <SQLCodeMirror :data="procStr"></SQLCodeMirror>
                </el-tab-pane>
              </el-tabs>
            </el-col>
          </el-row>
      </template>
    </el-drawer>
    <el-dialog
        v-model="tableTagDialog"
        title="配置标签分类"
        width="1100"
    >
      <el-form :model="currentTableTag" :inline="true" label-width="75px">
        <el-form-item label="一级分类:" prop="userName">
          <el-input
              v-model="currentTableTag.clasPriClas"
              disabled
              style="width: 240px"
          />
        </el-form-item>
        <el-form-item label="二级分类:">
          <el-input
              v-model="currentTableTag.clasScdClas"
              disabled
              style="width: 240px"
          />
        </el-form-item>
        <el-form-item label="三级分类:">
          <el-input
              v-model="currentTableTag.clasThreClas"
              disabled
              style="width: 240px"
          ></el-input>
        </el-form-item>
        <el-form-item label="选择标签:">
          <el-select
              v-model="currentTableTag.clasOnum"
              filterable
              style="width: 240px"
              @change="changeTableTag"
          >
            <el-option v-for="item in metaClasList" :key="item.clasOnum" :value="item.clasOnum" :label="item.clasOnum+'  -  '+item.clasName"/>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Plus" @click="addTableTag">新增</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" :data="tableTags">
        <el-table-column label="标签编号" width="80" align="center" prop="onum"></el-table-column>
        <el-table-column label="标签名称" width="120" align="center" prop="clasName"></el-table-column>
        <el-table-column label="标签值" width="170" align="center" prop="clasValue" >
          <template #default="scope">
            <el-input @input="changeTagValue(scope.row)" v-model="scope.row.clasValue" :disabled="scope.row.clasEffFlag !== '1'"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="标签值说明" width="170" align="center" prop="tagRemark">
          <template #default="scope">
            <el-input @input="changeTagValue(scope.row)" v-model="scope.row.tagRemark" :disabled="scope.row.clasEffFlag !== '1'"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="标签状态"  align="center" prop="clasEffFlag">
          <template #default="scope">
            <span v-if="scope.row.clasEffFlag === '1'" style="color:#67C23A;">{{'有效'}}</span>
            <span v-else style="color: #F56C6C">{{'已删除'}}</span>
          </template>
        </el-table-column>
        <el-table-column label="设置方式" width="80" align="center" prop="tagSource">
          <template #default="scope">
            <span>{{scope.row.tagSource ? scope.row.tagSource:'自动'}}</span>
          </template>
        </el-table-column>
        <el-table-column label="设置对象" width="80" align="center" prop="setUser"></el-table-column>
        <el-table-column label="设置时间" width="150" align="center" prop="setTime"></el-table-column>
        <el-table-column label="操作" align="center" width="80">
          <template #default="scope">
            <el-button v-if="scope.row.clasEffFlag === '1'" link type="danger" icon="Delete" @click.prevent="deleteTag(scope.row)">删除</el-button>
            <el-button v-if="scope.row.clasEffFlag === '0'" link type="success" icon="RefreshLeft" @click.prevent="revertTag(scope.row)">恢复</el-button>
          </template>
        </el-table-column>
      </el-table>

      <template #footer>
        <div class="dialog-footer">
          <el-button @click="tableTagDialog = false">取消</el-button>
          <el-button type="primary" @click="confirmTableTags">
            确定
          </el-button>
        </div>
      </template>
    </el-dialog>
    <el-dialog
        v-model="columnInfoDialog"
        title="字段信息"
        width="1100"
    >
      <el-form :model="currentColumnData" :inline="true" label-width="100px">
        <el-form-item label="字段英文名">
          <el-input disabled v-model="currentColumnData.fldEngName"></el-input>
        </el-form-item>
        <el-form-item label="字段中文名">
          <el-input disabled v-model="currentColumnData.fldCnName"></el-input>
        </el-form-item>
        <el-form-item label="字段类型">
          <el-input disabled v-model="currentColumnData.fldType"></el-input>
        </el-form-item>
        <el-form-item label="主键标志">
          <template v-if="currentColumnData.pkFlag === '1' || currentColumnData.pkFlag === 'Y' || currentColumnData.pkFlag === true">
            <el-input disabled :model-value="'是'"></el-input>
          </template>
          <template v-else>
            <el-input disabled :model-value="'否'"></el-input>
          </template>
        </el-form-item>
        <el-form-item label="是否必填">
          <template v-if="currentColumnData.requireFlag === '1' || currentColumnData.requireFlag === 'Y' || currentColumnData.requireFlag === true">
            <el-input disabled :model-value="'是'"></el-input>
          </template>
          <template v-else>
            <el-input disabled :model-value="'否'"></el-input>
          </template>
        </el-form-item>
        <el-form-item label="是否索引">
          <template v-if="currentColumnData.idxFlag === '1' || currentColumnData.idxFlag === 'Y' || currentColumnData.idxFlag === true">
            <el-input disabled :model-value="'是'"></el-input>
          </template>
          <template v-else>
            <el-input disabled :model-value="'否'"></el-input>
          </template>
        </el-form-item>
        <el-form-item label="字段补录名">
          <el-input  v-model="currentColumnData.fldCrrctName"></el-input>
        </el-form-item>
        <el-form-item label="补录字段描述">
          <el-input v-model="currentColumnData.fldDesc"/>
        </el-form-item>
        <el-form-item label="补录主键">
          <div style="width: 192px">
            <el-switch v-model="currentColumnData.crrctPkFlag"/>
          </div>
        </el-form-item>
<!--        <el-form-item label="状态">-->
<!--          <el-input v-model="currentColumnData.suppRecStat"/>-->
<!--        </el-form-item>-->
        <el-form-item label="负责人">
          <el-input v-model="currentColumnData.pic"/>
        </el-form-item>
        <el-form-item label="字段空值率">
          <el-input v-model="currentColumnData.fldNullRate"/>
        </el-form-item>
      </el-form>
      <el-form :model="currentColumnTag" :inline="true" label-width="100px">
        <el-form-item label="一级分类:" prop="userName">
          <el-input
              v-model="currentColumnTag.clasPriClas"
              disabled
          />
        </el-form-item>
        <el-form-item label="二级分类:">
          <el-input
              v-model="currentColumnTag.clasScdClas"
              disabled
          />
        </el-form-item>
        <el-form-item label="三级分类:">
          <el-input
              v-model="currentColumnTag.clasThreClas"
              disabled
          ></el-input>
        </el-form-item>
        <el-form-item label="选择标签:">
          <el-select
              v-model="currentColumnTag.clasOnum"
              filterable
              style="width: 192px"
              @change="changeColumnTag"
          >
            <el-option v-for="item in metaClasList" :key="item.clasOnum" :value="item.clasOnum" :label="item.clasOnum+'  -  '+item.clasName"/>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Plus" @click="addColumnTag">新增</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="currentColumnData.showFldClas">
        <el-table-column label="标签编号" width="80" align="center" prop="onum"></el-table-column>
        <el-table-column label="标签名称" width="120" align="center" prop="clasName"></el-table-column>
        <el-table-column label="标签值" width="170" align="center" prop="clasValue" >
          <template #default="scope">
            <el-input :disabled="scope.row.clasEffFlag === '0'" @input="changeTagValue(scope.row)" v-model="scope.row.clasValue"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="标签值说明" width="170" align="center" prop="tagRemark">
          <template #default="scope">
            <el-input :disabled="scope.row.clasEffFlag === '0'" @input="changeTagValue(scope.row)" v-model="scope.row.tagRemark"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="标签状态"  align="center" prop="clasEffFlag">
          <template #default="scope">
            <span>{{ (scope.row.clasEffFlag === '1')?'有效':'无效' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="设置方式" width="80" align="center" prop="tagSource">
          <template #default="scope">
            <span>{{scope.row.tagSource?scope.row.tagSource:'自动'}}</span>
          </template>
        </el-table-column>
        <el-table-column label="设置对象" width="80" align="center" prop="setUser"></el-table-column>
        <el-table-column label="设置时间" width="150" align="center" prop="setTime"></el-table-column>
        <el-table-column label="操作" align="center" width="80">
          <template #default="scope">
            <el-button v-if="scope.row.clasEffFlag === '1'"  link type="danger" icon="Delete" @click.prevent="deleteTag(scope.row)">删除</el-button>
            <el-button v-if="scope.row.clasEffFlag === '0'" link type="success" icon="RefreshLeft" @click.prevent="revertTag(scope.row)">恢复</el-button>
          </template>
        </el-table-column>
      </el-table>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="columnInfoDialog = false">取消</el-button>
          <el-button type="primary" @click="confirmColumn">
            确定
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Meta">
  import {getDataSourceList, getMetaDataList, getColumnList, getMetaClasList, postMetaSupp} from "@/api/meta/metaInfo"
  import { ref, nextTick, computed, watch, reactive, onMounted } from 'vue'
  import SQLCodeMirror from "@/components/codemirror/SQLCodeMirror.vue";
  import cache from "@/plugins/cache";


  const data = reactive({
    queryParams:{
      ssysCd:'',
      mdlName:'',
      tabName:'',
      colName:'',
      tabType:'',
      tagName:'',
      recStat:'',
      pageNum:1,
      pageSize:10
    },
    currentMetaData: {
      tabEngName:'',
      tabCnName:'',
      ssysCd:'',
      mdlName:'',
      tabCrrctName:'',
      tabDesc:'',
      govFlag:null,
      pic:'',
      tags:[]
    }
  })
  const { queryParams, currentMetaData } = toRefs(data);
  const loading = ref(true);
  const drawer = ref(false);
  const dataList = ref([]);
  const columnList = ref([]);
  const tableTagDialog = ref(false);
  const metaClasList = ref([]);
  const columnInfoDialog = ref(false);
  const currentColumnData = ref({});
  const currentTableTag = ref({
    "clasThreClas": "",
    "clasOnum": null,
    "clasName": "",
    "recSubmPrsn": "",
    "clasPriClas": "",
    "clasScdClas": ""
  });
  const currentColumnTag = ref({
    "clasThreClas": "",
    "clasOnum": null,
    "clasName": "",
    "recSubmPrsn": "",
    "clasPriClas": "",
    "clasScdClas": ""
  });
  const columnParam = ref({
    total:0,
    pageNum:1,
    pageSize:10
  });
  const total = ref(0);
  const databaseList = ref([]);
  const tableTags = ref([]);
  const database = ref("");
  const procStr = ref("");
  const activeColumnTab = ref("column");
  const { proxy } = getCurrentInstance();
  const changedColumns = ref([])
  function changeColumnTab(){
    if (activeColumnTab.value === 'proc'){
      procStr.value = "--基金量化产品监管报送存储过程：ADS.SP_ADS_SAC_QNTPRD_ALL\n" +
          "CREATE OR REPLACE PROCEDURE ADS.SP_ADS_SAC_QNTPRD_ALL(I_BUSI_DATE VARCHAR2, O_RET_CODE OUT NUMBER, O_RET_MSG OUT VARCHAR2) AS V_STATUS NUMBER DEFAULT 0; --状态,0:成功，-1失败 \n V_ETL_NAME VARCHAR2(50) DEFAULT 'ADS.SP_ADS_SAC_QNTPRD_ALL'; V_ETL_NAME_CN VARCHAR2(100) DEFAULT '量化产品监管报送报表总调度'; ----ETL中文名称 V_ETL_TYPE NUMBER DEFAULT 1; --ETL类型，0:采集，1:转换 V_START_TIME DATE DEFAULT SYSDATE; --开始时间 V_MSG VARCHAR2(1000) DEFAULT '成功'; ----ETL信息 V_SOURCE_TABLE VARCHAR2(200) DEFAULT 'ADS.SP_ADS_SAC_QNTPRD_DTL,ADS.SP_ADS_SAC_QNTPRD_AGGR'; V_DEST_TABLE VARCHAR2(200) DEFAULT 'ADS.T_ADS_SAC_QNTPRD_DTL_M_M_OC,ADS.T_ADS_SAC_QNTPRD_AGGR_M_M_OC'; V_SRC_CNT NUMBER DEFAULT 0; --源数据量 V_DEST_CNT NUMBER DEFAULT 0; --目标新增数据量 V_CNT NUMBER; V_SOURCE_CODE VARCHAR2(4) ; V_DATE DATE DEFAULT TO_DATE(I_BUSI_DATE,'YYYY-MM-DD'); --每月第一个交易日 V_RET_CODE NUMBER; V_RET_MSG VARCHAR2(4000); BEGIN O_RET_CODE := 0; O_RET_MSG := 'SUCCESS'; --每个月第一个交易日更新数据 SELECT T.TM_SH_FST_TRDY_FLAG INTO V_CNT FROM DWS.T_DWS_DIM_DATE_OC T WHERE T.D_DATE= V_DATE; IF V_CNT = 0 THEN RETURN; END IF; --加载明细表数据 ADS.SP_ADS_SAC_QNTPRD_DTL (I_BUSI_DATE => I_BUSI_DATE, --每月第一个交易日 O_RET_CODE => V_RET_CODE, O_RET_MSG => V_RET_MSG ) ; IF V_RET_CODE <> 0 THEN O_RET_CODE := V_RET_CODE; O_RET_MSG := V_RET_MSG; RETURN; END IF; --加载汇总表数据 ADS.SP_ADS_SAC_QNTPRD_AGGR(I_BUSI_DATE => I_BUSI_DATE, --每月第一个交易日 O_RET_CODE => V_RET_CODE, O_RET_MSG => V_RET_MSG ) ; IF V_RET_CODE <> 0 THEN O_RET_CODE := V_RET_CODE; O_RET_MSG := V_RET_MSG; RETURN; END IF; --日志：判断是否源表目标表的数量是否相同,如不相同则修改日志校验MSG --日志：写日志 DWS.SP_DWT_LOG(I_ETL_NAME => V_ETL_NAME, I_ETL_NAME_CN => V_ETL_NAME_CN, I_ETL_TYPE => V_ETL_TYPE, I_START_TIME => V_START_TIME, I_END_TIME => SYSDATE, I_STATUS => V_STATUS, I_MSG => V_MSG, I_SOURCE_TABLES => V_SOURCE_TABLE, I_DEST_TABLES => V_DEST_TABLE, I_SRC_CNT => V_SRC_CNT, I_DEST_CNT => V_DEST_CNT, I_VERI_SRC_CNT => NULL, I_VERI_DEST_CNT => NULL, I_VERI_MSG => NULL, I_VERI_STATUS => NULL, I_BUSI_DATE => I_BUSI_DATE); EXCEPTION WHEN OTHERS THEN O_RET_CODE := -1; O_RET_MSG := SQLCODE || ',' || SQLERRM; ROLLBACK; V_STATUS := -1; V_MSG := O_RET_MSG; DWS.SP_DWT_LOG(I_ETL_NAME => V_ETL_NAME, I_ETL_NAME_CN => V_ETL_NAME_CN, I_ETL_TYPE => V_ETL_TYPE, I_START_TIME => V_START_TIME, I_END_TIME => SYSDATE, I_STATUS => V_STATUS, I_MSG => V_MSG, I_SOURCE_TABLES => V_SOURCE_TABLE, I_DEST_TABLES => V_DEST_TABLE, I_SRC_CNT => V_SRC_CNT, I_DEST_CNT => V_DEST_CNT, I_VERI_SRC_CNT => NULL, I_VERI_DEST_CNT => NULL, I_VERI_MSG => NULL, I_VERI_STATUS => NULL, I_BUSI_DATE => I_BUSI_DATE); END;"
    }
  }
  function confirmTableTags(){
    currentMetaData.value.tags = JSON.parse(JSON.stringify(tableTags.value))
    tableTagDialog.value = false
  }
  function confirmColumn(){
    let changed = false
    for (let i = 0; i < columnList.value.length; i++) {
      if (columnList.value[i].fldEngName === currentColumnData.value.fldEngName){
        if (columnList.value[i].fldCrrctName !== currentColumnData.value.fldCrrctName){
          columnList.value[i].fldCrrctName = currentColumnData.value.fldCrrctName
          changed = true
        }
        if (columnList.value[i].fldDesc !== currentColumnData.value.fldDesc){
          columnList.value[i].fldDesc = currentColumnData.value.fldDesc
          changed = true
        }
        if (columnList.value[i].crrctPkFlag !== currentColumnData.value.crrctPkFlag){
          columnList.value[i].crrctPkFlag = currentColumnData.value.crrctPkFlag
          changed = true
        }
        // if (columnList.value[i].suppRecStat !== currentColumnData.value.suppRecStat){
        //   columnList.value[i].suppRecStat = currentColumnData.value.suppRecStat
        // }
        if (columnList.value[i].pic !== currentColumnData.value.pic){
          columnList.value[i].pic = currentColumnData.value.pic
          changed = true
        }
        if (columnList.value[i].fldNullRate !== currentColumnData.value.fldNullRate){
          columnList.value[i].fldNullRate = currentColumnData.value.fldNullRate
          changed = true
        }
        if (currentColumnData.value.showFldClas && currentColumnData.value.showFldClas.length > 0){
          let showFldClas = currentColumnData.value.showFldClas
          let fldClas = []
          for (let j = 0; j < showFldClas.length; j++) {
            let col = showFldClas[j]
            if (col.clasEffFlag === '0'){ // 无效
              fldClas.push(col)
            }else{  // 有效
              if (col.tagSource === '手动'){
                fldClas.push(col)
              }
            }
          }
          if (columnList.value[i].fldClas && columnList.value[i].fldClas !== '[]'){
            if (JSON.stringify(fldClas) !== columnList.value[i].fldClas){
              columnList.value[i].fldClas = JSON.stringify(fldClas)
              changed = true
            }
          }else {
            if (fldClas.length !== 0){
              columnList.value[i].fldClas = JSON.stringify(fldClas)
              changed = true
            }
          }
          let batchFldClas = columnList.value[i].batchFldClas
          if (fldClas.length > 0){
            for (let j = 0; j < fldClas.length; j++) {
              for (let k = 0; k < batchFldClas.length; k++) {
                if (batchFldClas[k].clasPriClas === fldClas[j].clasPriClas
                    && batchFldClas[k].clasScdClas === fldClas[j].clasScdClas
                    && batchFldClas[k].clasThreClas === fldClas[j].clasThreClas
                    && batchFldClas[k].clasName === fldClas[j].clasName
                ){
                  batchFldClas[k] = fldClas[j]
                }
              }
            }
          }
          let tempFldClas = []
          if (batchFldClas && batchFldClas.length>0){
            for (let j = 0; j < batchFldClas.length; j++) {
              if (tempFldClas.length > 0){
                let hasClas = false
                for (let k = 0; k < tempFldClas.length; k++) {
                  if (tempFldClas[k].clasPriClas === batchFldClas[j].clasPriClas
                      && tempFldClas[k].clasScdClas === batchFldClas[j].clasScdClas
                      && tempFldClas[k].clasThreClas === batchFldClas[j].clasThreClas
                  ){
                    hasClas = true
                  }
                }
                if (hasClas){
                  break
                }else {
                  if (batchFldClas[j].clasEffFlag === '1'){
                    tempFldClas.push(batchFldClas[j])
                  }
                }
              }else {
                if (batchFldClas[j].clasEffFlag === '1'){
                  tempFldClas.push(batchFldClas[j])
                }
              }
            }
          }
          columnList.value[i].showFldClas = batchFldClas
          columnList.value[i].tempFldClas = tempFldClas
        }
      }
    }
    columnInfoDialog.value = false
    if (changed){
      changedColumns.value.push(currentColumnData.value)
    }
  }
  function addTableTag(){
    let json = JSON.parse(JSON.stringify(currentTableTag.value))
    if (json.clasOnum !== null){
      tableTags.value.push({
        onum: json.clasOnum,
        clasName: json.clasName,
        clasValue: '',
        tagRemark:'',
        clasEffFlag:'1',
        tagSource:'手工',
        setUser: cache.local.get("username"),
        setTime: getNowDateTime()
      })
    } else {
      proxy.$modal.msgWarning("请选择一个标签");
    }

  }
  function addColumnTag(){
    let json = JSON.parse(JSON.stringify(currentColumnTag.value))
    if (json.clasOnum !== null){
      currentColumnData.value.showFldClas.push({
        onum: json.clasOnum,
        clasName: json.clasName,
        clasValue: '',
        tagRemark:'',
        clasEffFlag:'1',
        tagSource:'手工',
        setUser: cache.local.get("username"),
        setTime: getNowDateTime()
      })
    } else {
      proxy.$modal.msgWarning("请选择一个标签");
    }

  }
  function getNowDateTime(){
      const now = new Date();
      const year = now.getFullYear();
      const month = String(now.getMonth() + 1).padStart(2, '0');
      const day = String(now.getDate()).padStart(2, '0');
      const hours = String(now.getHours()).padStart(2, '0');
      const minutes = String(now.getMinutes()).padStart(2, '0');
      const seconds = String(now.getSeconds()).padStart(2, '0');
      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;

  }
  function deleteTag(row){
    row.clasEffFlag = '0'
    row.tagSource = '手动'
  }
  function revertTag(row){
    row.clasEffFlag = '1'
    row.tagSource = '手动'
  }

  function changeTagValue(row){
    row.tagSource = '手动'
  }
  function changeTableTag(){
    if (typeof currentTableTag.value.clasOnum !== null){
      for (let i = 0; i <metaClasList.value.length; i++) {
        let item = metaClasList.value[i]
        if (item.clasOnum === currentTableTag.value.clasOnum){
          currentTableTag.value = item
        }
      }
    }
  }
  function changeColumnTag(){
    if (typeof currentColumnTag.value.clasOnum !== null){
      for (let i = 0; i <metaClasList.value.length; i++) {
        let item = metaClasList.value[i]
        if (item.clasOnum === currentColumnTag.value.clasOnum){
          currentColumnTag.value = item
        }
      }
    }
  }
  function handleTableTagClose(index){
    currentMetaData.value.tags[index].clasEffFlag = '0'
    // for (let i = 0; i < currentMetaData.value.tags.length; i++) {
    //   if (tag.tagName === currentMetaData.value.tags[i].tagName && tag.tagValue === currentMetaData.value.tags[i].tagValue){
    //     currentMetaData.value.tags.splice(i, 1);
    //   }
    // }
  }
  function openTableTagsDialog(){
    tableTagDialog.value = true
    currentTableTag.value = {
      "clasThreClas": "",
      "clasOnum": null,
      "clasName": "",
      "recSubmPrsn": "",
      "clasPriClas": "",
      "clasScdClas": ""
    }
    tableTags.value = JSON.parse(JSON.stringify(currentMetaData.value.tags))
    getMetaClasList().then(res=>{
      metaClasList.value = res.data
    })
  }
  function openEditColumnDialog(row,index){
    columnInfoDialog.value = true
    currentColumnData.value = JSON.parse(JSON.stringify(row))
    currentColumnTag.value = {
      "clasThreClas": "",
      "clasOnum": null,
      "clasName": "",
      "recSubmPrsn": "",
      "clasPriClas": "",
      "clasScdClas": ""
    }
    currentColumnData.value.showFldClas = JSON.parse(JSON.stringify(row.showFldClas))
    getMetaClasList().then(res=>{
      metaClasList.value = res.data
    })
  }
  function showColumnDialog(row) {
    let tableData = JSON.parse(JSON.stringify(row))
    currentMetaData.value = {
      tabEngName: tableData.tabEngName,
      tabCnName: tableData.tabCnName,
      ssysCd: tableData.ssysCd,
      mdlName:tableData.mdlName,
      tabCrrctName:tableData.tabCrrctName,
      tabDesc:tableData.tabDesc,
      govFlag:tableData.govFlag,
      pic:tableData.pic,
      tags: tableData.showTabClas
    }
    let columnData = {
      tabName: row.tabEngName,
      ssysCd: row.ssysCd,
      mdlName: row.mdlName
    }
    getColumnList(columnData).then(res=>{
      columnList.value = res.data
      if (columnList.value.length>0){
        for (let i = 0; i < columnList.value.length; i++) {
          let batchFldClas = columnList.value[i].batchFldClas
          let fldClas =[]
          if (columnList.value[i].fldClas && columnList.value[i].fldClas !== '[]'){
            fldClas = JSON.parse(columnList.value[i].fldClas)
          }
          if (fldClas.length > 0){
            for (let j = 0; j < fldClas.length; j++) {
              for (let k = 0; k < batchFldClas.length; k++) {
                if (batchFldClas[k].clasPriClas === fldClas[j].clasPriClas
                    && batchFldClas[k].clasScdClas === fldClas[j].clasScdClas
                    && batchFldClas[k].clasThreClas === fldClas[j].clasThreClas
                    && batchFldClas[k].clasName === fldClas[j].clasName
                ){
                  batchFldClas[k] = fldClas[j]
                }
              }
            }
          }
          let tempFldClas = []
          if (batchFldClas && batchFldClas.length>0){
            for (let j = 0; j < batchFldClas.length; j++) {
              if (tempFldClas.length > 0){
                let hasClas = false
                for (let k = 0; k < tempFldClas.length; k++) {
                  if (tempFldClas[k].clasPriClas === batchFldClas[j].clasPriClas
                      && tempFldClas[k].clasScdClas === batchFldClas[j].clasScdClas
                      && tempFldClas[k].clasThreClas === batchFldClas[j].clasThreClas){
                    hasClas = true
                  }
                }
                if (hasClas){
                  break
                }else {
                  if (batchFldClas[j].clasEffFlag === '1'){
                    tempFldClas.push(batchFldClas[j])
                  }
                }
              }else {
                if (batchFldClas[j].clasEffFlag === '1') {
                  tempFldClas.push(batchFldClas[j])
                }
              }
            }
          }
          columnList.value[i].showFldClas = batchFldClas
          columnList.value[i].tempFldClas = tempFldClas
        }
      }
    })
    drawer.value = true
    changedColumns.value = []
  }

  function getDatabaseList() {
    let query = {
      userName:cache.local.get("username"),
      password:cache.local.get("password")
    }
    getDataSourceList(query).then(res=>{
      databaseList.value = [{ id: 100, name: "数据源", parentId: 0, children: res.data.totalList.map(element => ({ ...element, parentId: "100" })) }]
    })
  }
  function handleQuery(){
    queryParams.value.pageNum = 1;
    getList()
  }
  function resetQuery(){
    queryParams.value ={
          ssysCd:'',
          mdlName:'',
          tabName:'',
          colName:'',
          tabType:'',
          tagName:'',
          recStat:'',
          pageNum:1,
          pageSize:10
    }
    proxy.$refs.databaseTreeRef.setCurrentKey(null);
    handleQuery()
  }

  function handleSelectionChange(){

  }

  function getList(){
    getMetaDataList(queryParams.value).then(res=>{
      dataList.value = res.data.rows
      if (dataList.value.length>0){
        for (let i = 0; i < dataList.value.length; i++) {
          let batchTabClas = dataList.value[i].batchTabClas
          let tabClas =[]
          if (dataList.value[i].tabClas && dataList.value[i].tabClas !== '[]'){
            tabClas = JSON.parse(dataList.value[i].tabClas)
          }
          if (tabClas.length > 0){
            for (let j = 0; j < tabClas.length; j++) {
              for (let k = 0; k < batchTabClas.length; k++) {
                if (batchTabClas[k].clasPriClas === tabClas[j].clasPriClas
                    && batchTabClas[k].clasScdClas === tabClas[j].clasScdClas
                    && batchTabClas[k].clasThreClas === tabClas[j].clasThreClas
                    && batchTabClas[k].clasName === tabClas[j].clasName
                ){
                  batchTabClas[k] = tabClas[j]
                }
              }
            }
          }
          let tempTabClas = []
          if (batchTabClas && batchTabClas.length>0){
            for (let j = 0; j < batchTabClas.length; j++) {
              if (tempTabClas.length > 0){
                let hasClas = false
                for (let k = 0; k < tempTabClas.length; k++) {
                  if (tempTabClas[k].clasPriClas === batchTabClas[j].clasPriClas
                      && tempTabClas[k].clasScdClas === batchTabClas[j].clasScdClas
                      && tempTabClas[k].clasThreClas === batchTabClas[j].clasThreClas){
                    hasClas = true
                  }
                }
                if (hasClas){
                  break
                }else {
                  if (batchTabClas[j].clasEffFlag === '1'){
                    tempTabClas.push(batchTabClas[j])
                  }
                }
              }else {
                if (batchTabClas[j].clasEffFlag === '1') {
                  tempTabClas.push(batchTabClas[j])
                }
              }
            }
          }
          dataList.value[i].showTabClas = batchTabClas
          dataList.value[i].tempTabClas = tempTabClas
        }
      }
      loading.value = false
      total.value = res.data.total
    })
  }

  function exportData(){

  }
  const filterNode = (value, data) => {
    if (!value) return true;
    return data.name.indexOf(value) !== -1;
  };
  function handleNodeClick(data) {
    queryParams.value.ssysCd = data.name;
    handleQuery();
  }
  watch(database, val => {
    proxy.$refs["databaseTreeRef"].filter(val);
  });
  function submit(){
    let data = {
      ssysCd: currentMetaData.value.ssysCd,
      mdlName: currentMetaData.value.mdlName,
      tabEngName: currentMetaData.value.tabEngName,
      tabCrrctName: currentMetaData.value.tabCrrctName,
      tabDesc: currentMetaData.value.tabDesc,
      pic: currentMetaData.value.pic,
      govFlag: currentMetaData.value.govFlag,
      tabClas: JSON.stringify(currentMetaData.value.tags),
      columnInfo: []
    }
    if (changedColumns.value.length > 0){
      for (let i = 0; i < changedColumns.value.length; i++) {
        let column = changedColumns.value[i]
        data.columnInfo.push({
          fldEngName: column.fldEngName,
          fldCrrctName: column.fldCrrctName,
          crrctPkFlag: column.crrctPkFlag,
          fldDesc: column.fldDesc,
          pic: column.pic,
          fldClas: JSON.stringify(column.showFldClas),
          fldNullRate: column.fldNullRate,
          recStat: column.recStat
        })
      }
    }
    postMetaSupp(data).then(res=>{
      proxy.$modal.msgSuccess("操作成功");
      drawer.value = false
      getList()
    })
  }
  function cancel(){
    drawer.value = false
  }
  onMounted(()=>{
    getDatabaseList()
    handleQuery()
  })
</script>

<style scoped lang="scss">
:deep(.el-drawer__header) {
  margin-bottom: 0 !important;
}
.full-height-tabs {
  height: calc(100vh - 320px);
}
:deep(.el-tabs__content)  {
  height: 100%;
  overflow: auto;
}
.table-container {
  height: 100%;
  overflow: auto;
}
</style>