index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. <template>
  2. <el-card class="codecount-container">
  3. <div slot="header" class="header">
  4. <span><!-- 代码运算 -->{{$t('EtaBasePage.algorithm_btn')}}</span>
  5. <div>
  6. <el-button type="primary" @click="runCodeHandle"><!-- 运行 -->{{$t('CodeCountPage.run_btn')}}</el-button>
  7. <el-button type="primary" plain @click="saveHandle" v-if="!isView"><!-- 保存 -->{{$t('Dialog.confirm_save_btn')}}</el-button>
  8. </div>
  9. </div>
  10. <div class="bottom">
  11. <el-col :span="15" class="bottom-wrapper left-wrapper">
  12. <div class="code-editor">
  13. <code-mirror ref="codeRef" :code="runCode" @initResult="initResult"/>
  14. </div>
  15. <div class="code-result">
  16. <span><!-- 结果展示 -->{{$t('CodeCountPage.res_show')}}</span>
  17. <div class="code-result-wrapper" v-if="isShowResult">
  18. <data-table
  19. :data="runResultData"
  20. />
  21. </div>
  22. </div>
  23. </el-col>
  24. <el-col :span="8" class="bottom-wrapper">
  25. <el-tabs>
  26. <el-tab-pane :label="$t('CodeCountPage.tab_info')">
  27. <el-form
  28. ref="diaForm"
  29. label-position="left"
  30. inline
  31. label-width="110px"
  32. :model="formData"
  33. :disabled="isView"
  34. >
  35. <el-form-item :label="$t('Edb.Detail.e_name')" prop="edb_name">
  36. <el-input
  37. v-model="formData.edb_name"
  38. :placeholder="$t('Edb.InputHolderAll.input_name')"
  39. style="width:220px"
  40. />
  41. </el-form-item>
  42. <el-form-item :label="$t('Edb.Detail.e_unit')" prop="unit">
  43. <selectUnit
  44. v-model="formData.unit"
  45. style="width:220px"
  46. />
  47. </el-form-item>
  48. <el-form-item :label="$t('Edb.Detail.e_fre')" prop="frequency">
  49. <el-select
  50. v-model="formData.frequency"
  51. :placeholder="$t('Edb.InputHolderAll.input_fre')"
  52. clearable
  53. style="width:220px">
  54. <el-option
  55. v-for="item in frequencyArr"
  56. :key="item.label"
  57. :label="item.label"
  58. :value="item.value">
  59. </el-option>
  60. </el-select>
  61. </el-form-item>
  62. <el-form-item :label="$t('Edb.Detail.e_menu')" prop="menu">
  63. <el-cascader
  64. v-model="formData.menu"
  65. :options="menuOptions"
  66. :props="{
  67. label: 'ClassifyName',
  68. value: 'ClassifyId',
  69. children: 'Children',
  70. checkStrictly: true,
  71. emitPath: false,
  72. }"
  73. @change="menuChange"
  74. clearable
  75. style="width:220px"
  76. :placeholder="$t('Edb.InputHolderAll.input_menu')"/>
  77. </el-form-item>
  78. </el-form>
  79. </el-tab-pane>
  80. <el-tab-pane :label="$t('CodeCountPage.tab_edb')" v-if="!isView">
  81. <el-form
  82. label-position="left"
  83. inline
  84. label-width="120px"
  85. >
  86. <el-form-item :label="$t('Edb.Detail.source')">
  87. <el-cascader
  88. v-model="fromType"
  89. :options="allFromArr"
  90. :props="{
  91. label: 'SourceName',
  92. value: 'EdbSourceId',
  93. children: 'Child',
  94. emitPath: false
  95. }"
  96. clearable
  97. style="width:220px"
  98. :placeholder="$t('Edb.Detail.source')"/>
  99. <!-- <el-select
  100. v-model="fromType"
  101. placeholder="请选择来源"
  102. style="width:220px">
  103. <el-option
  104. v-for="item in allFromArr"
  105. :key="item.key"
  106. :label="item.name"
  107. :value="item.key">
  108. </el-option>
  109. </el-select> -->
  110. </el-form-item>
  111. <el-form-item :label="$t('Edb.InputHolderAll.input_name_orid')">
  112. <el-select
  113. v-model="search_txt"
  114. v-loadMore="searchLoad"
  115. ref="searchRef"
  116. :filterable="!search_txt"
  117. remote
  118. clearable
  119. :placeholder="$t('Edb.InputHolderAll.input_name_orid')"
  120. :remote-method="searchHandle"
  121. @click.native="inputFocusHandle"
  122. style="width:220px"
  123. >
  124. <i slot="prefix" class="el-input__icon el-icon-search"></i>
  125. <el-option
  126. v-for="item in searchOptions"
  127. :key="item.EdbInfoId"
  128. :label="currentLang==='en'?(item.EdbNameEn||item.EdbName):item.EdbName"
  129. :value="item.EdbInfoId"
  130. :disabled="!item.HaveOperaAuth"
  131. >
  132. <div>
  133. <img
  134. :src="$icons.lock_ico2"
  135. width="18"
  136. height="18"
  137. style="vertical-align:middle"
  138. v-if="!item.HaveOperaAuth"
  139. />
  140. {{currentLang==='en'?(item.EdbNameEn||item.EdbName):item.EdbName}}
  141. </div>
  142. </el-option>
  143. </el-select>
  144. </el-form-item>
  145. </el-form>
  146. <div class="search-result">
  147. <p>{{$t('CodeCountPage.res_search')}} <span style="color: #999;">(<!-- 展示指标所在表结构及调取指标代码 -->{{$t('CodeCountPage.res_search_tip')}})</span></p>
  148. <template v-if="resultInfo.sheet_name">
  149. <table border>
  150. <thead>
  151. <tr>
  152. <td v-for="item in resultInfo.headers" :key="item">{{item}}</td>
  153. </tr>
  154. </thead>
  155. <tbody>
  156. <tr v-for="(item,index) in resultInfo.sheetArr" :key="item.key">
  157. <td :rowspan="resultInfo.sheetArr.length" v-if="index === 0">{{resultInfo.sheet_name}}</td>
  158. <td>{{item.key}}</td>
  159. <td>{{item.label}}</td>
  160. </tr>
  161. </tbody>
  162. </table>
  163. <div class="sql-code" v-html="resultInfo.sql_code"></div>
  164. <el-button type="primary" class="copy-btn" @click="copyCode" :data-clipboard-text="resultInfo.sql_code"><!-- 复制代码 -->{{$t('CodeCountPage.copy_code')}}</el-button>
  165. </template>
  166. </div>
  167. </el-tab-pane>
  168. </el-tabs>
  169. </el-col>
  170. </div>
  171. </el-card>
  172. </template>
  173. <script>
  174. import { dataBaseInterface } from '@/api/api.js';
  175. import { unitArr,frequencySelectList } from '@/utils/defaultOptions';
  176. import storage from '@/utils/storage.js';
  177. import codeMirror from './compoments/codeMirror';
  178. import dataTable from './compoments/dataTable';
  179. import { mapState } from 'vuex';
  180. export default {
  181. name:'',
  182. components: { codeMirror,dataTable },
  183. computed: {
  184. ...mapState({
  185. currentLang: state => state.lang,
  186. }),
  187. frequencyArr(){
  188. return frequencySelectList(['半年度'])
  189. },
  190. },
  191. data () {
  192. return {
  193. isView: this.$route.query.isView ? true : false,
  194. formData: {},
  195. fromType:"",
  196. unitArr,
  197. allFromArr:[],//所有指标来源
  198. /* frequencyArr, */
  199. menuOptions:[],//目录数组
  200. search_txt: '',
  201. search_have_more: false,
  202. searchOptions:[],
  203. search_page: 1,
  204. current_search: '',
  205. isShowResult: false,
  206. runCode: '',
  207. runResultData:[],//运行结果
  208. resultInfo: {
  209. // headers: ['表名','字段名','*字段说明'],
  210. headers: [this.$t('CodeCountPage.table_name'),this.$t('CodeCountPage.field_name'),this.$t('CodeCountPage.field_instru')],
  211. sheetArr:[],
  212. sheet_name: '',
  213. sql_code: ''
  214. }
  215. };
  216. },
  217. watch: {
  218. //每搜索一次记录一次
  219. search_txt(newval) {
  220. if(!newval) return;
  221. let historySearch = storage.get('pycode_seachlist') || [];
  222. let obj = {
  223. EdbInfoId: newval,
  224. EdbName: this.searchOptions.find(_ => _.EdbInfoId===newval).EdbName
  225. }
  226. //有搜索记录删除上条
  227. let index = historySearch.findIndex(_ => _.EdbInfoId === newval)
  228. if(index !== -1) historySearch.splice(index, 1);
  229. historySearch.unshift(obj)
  230. storage.set('pycode_seachlist',historySearch,1)
  231. this.getEdbSheeetInfo(newval);
  232. }
  233. },
  234. methods: {
  235. /* 运行代码 */
  236. runCodeHandle: _.debounce(function() {
  237. if(!this.$refs.codeRef.codeContent) return this.$message.warning(this.$t('CodeCountPage.no_code_msg'));
  238. this.isShowResult = false;
  239. const loading = this.$loading({
  240. lock: true,
  241. text: `${this.$t('CodeCountPage.run_ing')}...`,
  242. target:'.left-wrapper',
  243. spinner: 'el-icon-loading'
  244. });
  245. dataBaseInterface.runCode({
  246. PythonCode: encodeURIComponent(this.$refs.codeRef.codeContent)
  247. }).then(res => {
  248. loading.close();
  249. if(res.Ret !== 200) return
  250. // this.$message.success('运行成功')
  251. this.$message.success(this.$t('CodeCountPage.run_success'))
  252. const { date,value } = res.Data;
  253. const data_arr = []
  254. for(let i in date) {
  255. data_arr.push([date[i],value[i]])
  256. }
  257. this.runResultData = data_arr;
  258. this.isShowResult = true;
  259. })
  260. },200),
  261. /* 获取指标详情 */
  262. getEdbInfo() {
  263. if(!this.$route.query.edbid) return;
  264. dataBaseInterface.countCodeDetail({
  265. EdbInfoId: Number(this.$route.query.edbid)
  266. }).then(res => {
  267. if(res.Ret !== 200) return
  268. const { EdbInfoDetail,PythonCode } = res.Data;
  269. const { EdbName,EdbNameEn,ClassifyId,Frequency,Unit,UnitEn } = EdbInfoDetail;
  270. this.formData = {
  271. edb_name: this.currentLang==='en'?EdbNameEn:EdbName,
  272. menu: ClassifyId,
  273. frequency: Frequency,
  274. unit: this.currentLang==='en'?UnitEn:Unit
  275. }
  276. this.runCode = PythonCode
  277. })
  278. },
  279. /* 获取表结构 */
  280. getEdbSheeetInfo(EdbInfoId) {
  281. dataBaseInterface.edbSheetDetail({
  282. EdbInfoId
  283. }).then(res => {
  284. if(res.Ret !== 200) return
  285. const { ColumnList,TableName,TemplateStr } = res.Data;
  286. this.resultInfo.sheet_name = TableName;
  287. this.resultInfo.sql_code = decodeURIComponent(TemplateStr);
  288. this.resultInfo.sheetArr = ColumnList.map(_ => {
  289. let key = Object.keys(_)[0];
  290. return {
  291. key,
  292. label: _[key]
  293. }
  294. })
  295. })
  296. },
  297. /* code变化时重置结果展示 */
  298. initResult() {
  299. this.isShowResult = false
  300. },
  301. /* 复制代码 */
  302. copyCode() {
  303. var clipboard = new this.Clipboard('.copy-btn')
  304. clipboard.on('success', e => {
  305. // this.$message.success('复制成功')
  306. this.$message.success(this.$t('MsgPrompt.copy_success_msg'))
  307. e.clearSelection() // 释放内存
  308. clipboard.destroy()
  309. })
  310. // // 浏览器不支持
  311. clipboard.on('error', e => {
  312. // this.$message.warning('浏览器暂不支持')
  313. this.$message.warning(this.$t('MsgPrompt.browser_not_support'))
  314. // 释放内存
  315. clipboard.destroy()
  316. })
  317. },
  318. /* 保存代码 */
  319. async saveHandle() {
  320. if(!this.formData.edb_name || !this.formData.menu || !this.formData.frequency || !this.formData.unit) return this.$message.warning(this.$t('CodeCountPage.noenough_info_msg'))
  321. if(!this.isShowResult) return this.$message.warning(this.$t('CodeCountPage.run_msg'))
  322. const loading = this.$loading({
  323. lock: true,
  324. text: `${this.$t('MsgPrompt.saveing_msg')}...`,
  325. target:'.codecount-container',
  326. spinner: 'el-icon-loading'
  327. });
  328. const { edb_name,menu,frequency,unit } = this.formData
  329. let params = {
  330. PythonCode: encodeURIComponent(this.$refs.codeRef.codeContent),
  331. EdbName: edb_name,
  332. Frequency: frequency,
  333. Unit: unit,
  334. ClassifyId: menu
  335. }
  336. const { Ret,Data } = this.$route.query.edbid ? await dataBaseInterface.editCountCode({ ...params,EdbInfoId: Number(this.$route.query.edbid) }) : await dataBaseInterface.addCountCode(params);
  337. loading.close();
  338. if( Ret !== 200 ) return
  339. // this.$message.success('保存成功')
  340. this.$message.success(this.$t('MsgPrompt.saved_msg'))
  341. const { UniqueCode,EdbInfoId } = Data;
  342. this.$router.replace({path:'/database', query: {
  343. code: UniqueCode,
  344. id: EdbInfoId
  345. }});
  346. },
  347. /* 获取目录结构 */
  348. getMenu() {
  349. dataBaseInterface.menuListV3().then((res) => {
  350. if (res.Ret !== 200) return
  351. this.filterNodes(res.Data.AllNodes||[]);
  352. this.menuOptions = res.Data.AllNodes || [];
  353. });
  354. },
  355. // 递归改变第三级目录结构
  356. filterNodes(arr) {
  357. arr.length &&
  358. arr.forEach((item) => {
  359. item.Children.length && this.filterNodes(item.Children);
  360. if (!item.Children.length) {
  361. delete item.Children;
  362. }
  363. });
  364. },
  365. /* 选择目录 */
  366. menuChange(val) {
  367. // this.formData.menu = val.length ? val[val.length - 1] : '';
  368. },
  369. /* 搜索 */
  370. searchHandle(query) {
  371. this.search_page = 1;
  372. this.current_search = query;
  373. this.searchApi(this.current_search)
  374. },
  375. searchApi(query,page=1) {
  376. if(!query){ this.searchOptions = []; return }
  377. dataBaseInterface.targetSearchByPage({
  378. KeyWord:query,
  379. CurrentIndex: page,
  380. Source: this.fromType || 0,
  381. }).then(res => {
  382. if(res.Ret !== 200) return
  383. const { List,Paging } = res.Data;
  384. this.search_have_more = page < Paging.Pages;
  385. this.searchOptions = page === 1 ? List : this.searchOptions.concat(List);
  386. })
  387. },
  388. /* 聚焦获取当前检索 */
  389. inputFocusHandle(e) {
  390. this.search_page = 1;
  391. this.current_search = e.target.value;
  392. this.searchOptions = this.fromType ? [] : storage.get('pycode_seachlist');
  393. // this.searchApi(this.current_search);
  394. },
  395. searchLoad() {
  396. if(!this.search_have_more) return;
  397. this.searchApi(this.current_search,++this.search_page);
  398. },
  399. getTargetSource(){
  400. dataBaseInterface.getCodecountEdbSources().then(res=>{
  401. if(res.Ret!==200) return
  402. if(res.Data){
  403. this.allFromArr = res.Data.map(_ => ({
  404. ..._,
  405. Child: _.Child.map(_item => ({
  406. ..._item,
  407. Child:null
  408. }))
  409. }))
  410. }
  411. })
  412. }
  413. },
  414. mounted() {
  415. this.getEdbInfo();
  416. this.getMenu();
  417. this.getTargetSource()
  418. },
  419. }
  420. </script>
  421. <style lang='scss' scoped>
  422. *{ box-sizing: border-box;}
  423. .codecount-container {
  424. .header {
  425. display: flex;
  426. justify-content: space-between;
  427. align-items: center;
  428. font-size: 16px;
  429. }
  430. .bottom {
  431. height: calc(100vh - 240px);
  432. .bottom-wrapper {
  433. padding: 20px;
  434. height: 100%;
  435. border: 1px solid #ECECEC;
  436. border-radius: 4px;
  437. &:first-child {
  438. margin-right: 20px;
  439. padding: 20px 0;
  440. }
  441. .code-editor {
  442. padding: 0 20px;
  443. }
  444. .code-result {
  445. border-top: 1px solid #ECECEC;
  446. margin-top: 20px;
  447. padding: 20px;
  448. }
  449. .search-result {
  450. border-top: 1px solid #ECECEC;
  451. padding: 20px 0;
  452. table {
  453. width: 100%;
  454. margin-top: 22px;
  455. td{ border-color: #DCDFE6; width: 33%; height: 36px;text-align: center;}
  456. }
  457. .sql-code {
  458. min-height: 100px;
  459. border: 1px solid #DCDFE6;
  460. border-top: none;
  461. }
  462. .copy-btn {
  463. display: block;
  464. margin: 20px auto;
  465. }
  466. }
  467. }
  468. }
  469. }
  470. </style>
  471. <style lang="scss">
  472. .codecount-container {
  473. .el-tabs__nav-wrap::after {
  474. background-color: #fff;
  475. }
  476. }
  477. </style>