index.vue 14 KB

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