index.vue 15 KB

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