index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. <script lang="ts" setup>
  2. import { ref, onMounted, reactive, watch, inject } from "vue";
  3. import { useRouter, useRoute } from 'vue-router'
  4. import { post, get } from "@/utils/request";
  5. import { GET_STRUCT_BY_TABLE_NAME, ADD_OR_UPDATE_REPORT_FORM } from "../api"
  6. import { generateSql, rangeGetSql, translatingArray } from "../function"
  7. import { ElMessage, ElMessageBox } from 'element-plus'
  8. import { VueDraggable } from 'vue-draggable-plus'
  9. import rangeFilter from "./rangeFilter.vue";
  10. import previewTable from "./previewTable.vue";
  11. import { confirmAction } from "@/utils/tools";
  12. interface collapseItemList {
  13. busObject: any,
  14. columnList: any[],
  15. }
  16. interface CollapseItem {
  17. data: {
  18. label: string;
  19. };
  20. list: collapseItemList;
  21. }
  22. const globalPopup = inject<GlobalPopup>('globalPopup')
  23. const router = useRouter()
  24. const route = useRoute()
  25. const reportName = ref('')
  26. const addEditData = JSON.parse(sessionStorage.getItem('reportJson') || '{}')
  27. const activeNames = ref([])
  28. const collapseList = ref<CollapseItem[]>([])
  29. // 拖拽表格参数
  30. const tableData = ref<any>([]);
  31. const tableColumns = ref<any>([{}, {}, {}]);
  32. // 全部Loading的Visable
  33. const allLoading = reactive({
  34. collapseListLoading: false,
  35. saveLoading: false
  36. })
  37. const allVisable = reactive({
  38. previewTableVisable: false
  39. })
  40. // ref
  41. const rangeFilterRef = ref<InstanceType<typeof rangeFilter> | null>(null);
  42. const previewTableRef = ref<InstanceType<typeof previewTable> | null>(null);
  43. // 保存预览的判断以及生成
  44. function preMethod(): any {
  45. const rangeFilterData = rangeFilterRef.value?.getRangeData()
  46. if(!tableData.value.length) {
  47. ElMessage.warning('请选择列')
  48. return
  49. }
  50. if(rangeFilterData.length) {
  51. for(let i in rangeFilterData) {
  52. if(!rangeFilterData[i].twoDisable && !rangeFilterData[i].filterValueTwo) {
  53. ElMessage.warning('请选择第二个条件')
  54. return
  55. }
  56. }
  57. }
  58. const tabeName = tableData.value.map((item: any) => item.tableName)
  59. const tableNames = addEditData.mindMapJSON.selectNodes[0].data.tblName
  60. if(!tabeName.includes(tableNames)) {
  61. ElMessage.warning('请选择主表数据')
  62. return
  63. }
  64. const list = rangeFilterData.filter((item: any) => item.filterValueThree)
  65. const SQL = generateSql(tableData.value, rangeFilterData, addEditData)
  66. const rangeSQL = rangeGetSql(rangeFilterData, addEditData)
  67. const finalSql = `${SQL} ${list.length ? rangeSQL : 'where 1=1'}`
  68. return {
  69. finalSql,
  70. formTransConditionJson: '',
  71. }
  72. }
  73. // 保存页面
  74. function saveReport() {
  75. const rangeFilterData = rangeFilterRef.value?.getRangeData()
  76. const { finalSql } = preMethod()
  77. const formFieldHead = tableData.value.map((item: any) => {
  78. return {
  79. tableName: item.tableName,
  80. columnName: item.columnName,
  81. columnVal: item.columnComment,
  82. tableAlias: item.seqPrefix
  83. }
  84. })
  85. // 需要转存的 JSON
  86. const formJson = {
  87. addEditData,
  88. tableData: tableData.value,
  89. rangeFilterData: rangeFilterData,
  90. }
  91. const { reportFormName, description, userAccessList = [], visibleRangeData = [], parentStoreId, privilege, id } = addEditData.addFormVal
  92. const formVal: any = {
  93. reportFormName,
  94. description,
  95. parentStoreId: parentStoreId ? parentStoreId : 0,
  96. privilege: privilege ? 1 : 2,
  97. executeSql: finalSql,
  98. userIds: visibleRangeData.filter((item: any) => item.isUser).map((item: any) => item.id).join(','),
  99. departmentIds: visibleRangeData.filter((item: any) => !item.isUser).map((item: any) => item.id).join(','),
  100. formJson: JSON.stringify(formJson),
  101. formFieldHead: JSON.stringify(formFieldHead),
  102. formTransConditionJson: translatingArray(rangeFilterData).length ? JSON.stringify(translatingArray(rangeFilterData)) : '',
  103. }
  104. if(id) {
  105. formVal.id = id
  106. }
  107. allLoading.saveLoading = true
  108. post(ADD_OR_UPDATE_REPORT_FORM, { ...formVal }).then(_res => {
  109. globalPopup?.showSuccess('保存成功')
  110. sessionStorage.removeItem('reportJson');
  111. sessionStorage.removeItem('editReportJson');
  112. router.go(-2)
  113. }).finally(() => {
  114. allLoading.saveLoading = false
  115. })
  116. }
  117. // 预览页面
  118. function preview() {
  119. const rangeFilterData = rangeFilterRef.value?.getRangeData()
  120. const { finalSql } = preMethod()
  121. allVisable.previewTableVisable = true
  122. setTimeout(() => {
  123. previewTableRef.value?.initializedData(finalSql, rangeFilterData, tableData.value)
  124. }, 100)
  125. }
  126. // 关闭页面
  127. function closePage() {
  128. ElMessageBox.confirm('要保存当前修改吗', '', {
  129. distinguishCancelAndClose: true,
  130. confirmButtonText: '确定',
  131. cancelButtonText: '取消',
  132. type: 'warning',
  133. }).then(() => {
  134. saveReport()
  135. }).catch((action) => {
  136. if(action === 'cancel') {
  137. sessionStorage.removeItem('reportJson');
  138. sessionStorage.removeItem('editReportJson');
  139. router.go(-2)
  140. }
  141. })
  142. }
  143. // 分组
  144. function grouping(index: number) {
  145. const val = tableData.value[index]?.singleColumnOperation?.grouping
  146. if(!tableData.value[index].singleColumnOperation) {
  147. tableData.value[index].singleColumnOperation = {}
  148. tableData.value[index].singleColumnOperation.grouping = val ? false : true
  149. } else {
  150. tableData.value[index].singleColumnOperation.grouping = val ? false : true
  151. }
  152. }
  153. // 统计
  154. function statistics(index: number) {
  155. const val = tableData.value[index]?.singleColumnOperation?.statistics
  156. if(!tableData.value[index].singleColumnOperation) {
  157. tableData.value[index].singleColumnOperation = {}
  158. tableData.value[index].singleColumnOperation.statistics = val ? false : true
  159. } else {
  160. tableData.value[index].singleColumnOperation.statistics = val ? false : true
  161. }
  162. }
  163. // 修改名称
  164. function changeName(index: number) {
  165. ElMessageBox.prompt('修改名称', '', {
  166. confirmButtonText: '确定',
  167. cancelButtonText: '取消',
  168. inputPattern: /^.+$/,
  169. inputErrorMessage: '请输入',
  170. }).then(({ value }) => {
  171. tableData.value[index].columnComment = value
  172. })
  173. }
  174. // 删除列
  175. function deleteColumn(index: number) {
  176. tableData.value.splice(index, 1);
  177. }
  178. // 添加列
  179. function addTable(row: any) {
  180. const { tableName = '', columnName = '', dictCode = '', columnComment = '' } = row.data
  181. const index = row.newIndex;
  182. console.log(tableData.value, '<====== tableData.value')
  183. if (tableData.value.filter((item: any) => `${item.tableName}.${item.columnName}.${item.dictCode ? item.dictCode : ''}` === `${tableName}.${columnName}.${dictCode}`).length >= 2) {
  184. ElMessage.warning('该列已存在')
  185. tableData.value.splice(index, 1);
  186. return
  187. }
  188. if(tableData.value.filter((item: any) => item.columnComment === columnComment).length >= 2) {
  189. ElMessage.warning(`【${columnComment}】列名称已存在、请先修改列名称`)
  190. tableData.value.splice(index, 1);
  191. return
  192. }
  193. }
  194. function listOnClone(clonedItem: any) {
  195. clonedItem.prop = `col_${Date.now()}`; // 赋予唯一 key
  196. }
  197. async function getCollapseListData() {
  198. const { mindMapJSON = {} } = addEditData
  199. const selectNodes = mindMapJSON?.selectNodes
  200. const alphabet = 'abcdefghijklmnopqrstuvwxyz';
  201. selectNodes.forEach((obj: any, index: number) => {
  202. obj.data.seqPrefix = alphabet[index % 26]; // 通过取余处理
  203. });
  204. allLoading.collapseListLoading = true
  205. for (let i in selectNodes) {
  206. console.log(selectNodes, '<===== selectNodes')
  207. const { dictCode = '', tblName = '' } = selectNodes[i].data
  208. const res = await post(GET_STRUCT_BY_TABLE_NAME, { tableName: tblName })
  209. res.data?.columnList.forEach((item: any) => {
  210. item.dictCode = dictCode
  211. });
  212. res.data.columnList.forEach((obj: any) => {
  213. obj.seqPrefix = selectNodes[i].data.seqPrefix
  214. });
  215. selectNodes[i].list = res.data
  216. }
  217. collapseList.value = selectNodes
  218. allLoading.collapseListLoading = false
  219. }
  220. function goBack() {
  221. router.go(-1)
  222. }
  223. function displayBackData() {
  224. const json = JSON.parse(sessionStorage.getItem('editReportJson') || '{}')
  225. const { rangeFilterData = [], tableData: tableDataList = [] } = json
  226. if(rangeFilterData.length) {
  227. setTimeout(() => {
  228. rangeFilterRef.value?.setRangeData(rangeFilterData, false)
  229. }, 300)
  230. }
  231. if(tableDataList.length) {
  232. setTimeout(() => {
  233. tableData.value = tableDataList
  234. }, 300)
  235. }
  236. }
  237. onMounted(() => {
  238. console.log(addEditData)
  239. const { reportFormName } = addEditData.addFormVal
  240. reportName.value = reportFormName
  241. getCollapseListData()
  242. displayBackData()
  243. setTimeout(() => {
  244. rangeFilterRef.value?.setIsItAnEditor()
  245. }, 100)
  246. })
  247. </script>
  248. <template>
  249. <div class="dragEdit flex flex-row h-full bg-white rounded-md w-full">
  250. <div class="w-[15%] h-full overflow-auto scroll-bar p-4" v-loading="allLoading.collapseListLoading">
  251. <el-collapse v-model="activeNames">
  252. <el-collapse-item :title="item.data.newLabel || item.data.label" :name="index" v-for="(item, index) in (collapseList as any)" :key="index">
  253. <VueDraggable v-model="item.list.columnList" :animation="150"
  254. :group="{ name: 'people', pull: 'clone', put: false }" :sort="false"
  255. class="flex flex-col gap-2 p-4 w-300px bg-gray-500/5 rounded" @clone="listOnClone">
  256. <div v-for="listItem in item.list.columnList" class="cursor-all-scroll px-3 py-2 bg-white">
  257. {{ listItem.columnComment }}
  258. </div>
  259. </VueDraggable>
  260. </el-collapse-item>
  261. </el-collapse>
  262. </div>
  263. <div class="w-[85%] h-full">
  264. <div class="h-full w-full flex flex-col p-3 box-border">
  265. <div class="dragEdit-header">
  266. <div class="flex justify-between">
  267. <div><el-button @click="goBack()">返回上一级</el-button></div>
  268. <div class="text-[20px] font-bold">{{ reportName }}</div>
  269. <div>
  270. <el-button @click="closePage()">关闭</el-button>
  271. <el-button @click="preview()">预览</el-button>
  272. <el-button type="primary" @click="saveReport()" :loading="allLoading.saveLoading">保存</el-button>
  273. </div>
  274. </div>
  275. <div class="range mt-3 mb-3">
  276. <div class="flex items-center pb-3 font-bold text-[16px]">
  277. 数据范围
  278. </div>
  279. <rangeFilter ref="rangeFilterRef" />
  280. </div>
  281. </div>
  282. <!-- 中间 -->
  283. <div class="flex justify-between items-center mb-3">
  284. <div class="flex items-center">
  285. <div class="font-bold text-[16px]">报表示例</div>
  286. </div>
  287. <div>点击顶部预览按钮可查看全部数据</div>
  288. </div>
  289. <!-- 表格 -->
  290. <div class="flex-1 relative border-2">
  291. <div class="table-container scroll-bar">
  292. <VueDraggable v-model="tableData" group="people" target=".sort-target" @add="addTable">
  293. <div class="absolute top-0 left-0 text-center text-[#999] w-full text-[20px] p-3" v-if="!tableData.length">
  294. <div class="border-2 border-dashed py-2">这里是表格头部区域,请将左侧字段拖入此区域生成报表</div>
  295. </div>
  296. <table class="table table-striped">
  297. <thead class="thead-dark">
  298. <tr class="sort-target">
  299. <th class="cursor-move" v-for="(header, headerIndex) in tableData" :key="header.value">
  300. <div class="w-full flex justify-between items-center">
  301. <div>{{ header.columnComment }}</div>
  302. <el-dropdown>
  303. <span class="el-dropdown-link">
  304. <el-icon color="#075985">
  305. <InfoFilled />
  306. </el-icon>
  307. </span>
  308. <template #dropdown>
  309. <el-dropdown-menu>
  310. <el-dropdown-item v-if="['varchar', 'text'].includes(header.dataType)">
  311. <el-text :underline="false" type="primary" @click="grouping(headerIndex)">
  312. {{ header?.singleColumnOperation?.grouping ? '取消分组' : '分组' }}
  313. </el-text>
  314. </el-dropdown-item>
  315. <el-dropdown-item v-if="['decimal'].includes(header.dataType)">
  316. <el-text :underline="false" type="primary" @click="statistics(headerIndex)">
  317. {{ header?.singleColumnOperation?.statistics ? '取消统计' : '统计' }}
  318. </el-text>
  319. </el-dropdown-item>
  320. <el-dropdown-item>
  321. <el-text :underline="false" type="primary" @click="changeName(headerIndex)">
  322. 修改名称
  323. </el-text>
  324. </el-dropdown-item>
  325. <el-dropdown-item>
  326. <el-text :underline="false" type="danger"
  327. @click="deleteColumn(headerIndex)">删除列</el-text>
  328. </el-dropdown-item>
  329. </el-dropdown-menu>
  330. </template>
  331. </el-dropdown>
  332. </div>
  333. </th>
  334. </tr>
  335. </thead>
  336. <tbody>
  337. <tr v-for="item in tableColumns" :key="item.name">
  338. <td v-for="header in tableData" :key="header.value">
  339. 示例数据
  340. </td>
  341. </tr>
  342. </tbody>
  343. </table>
  344. </VueDraggable>
  345. </div>
  346. </div>
  347. </div>
  348. </div>
  349. <!-- 全屏预览 -->
  350. <el-dialog v-model="allVisable.previewTableVisable" fullscreen top="40vh" width="70%" draggable>
  351. <previewTable ref="previewTableRef"></previewTable>
  352. </el-dialog>
  353. </div>
  354. </template>
  355. <style lang="scss" scoped>
  356. .dragEdit {
  357. .table {
  358. display: table;
  359. width: 100%;
  360. font-size: 15px;
  361. border-collapse: collapse;
  362. margin: 0 0;
  363. overflow-x: auto;
  364. }
  365. tr {
  366. background-color: #fff;
  367. border-top: 1px solid #e2e2e3;
  368. transition: background-color .5s;
  369. }
  370. th {
  371. text-align: left;
  372. font-size: 14px;
  373. font-weight: 600;
  374. color: rgba(60, 60, 67, .78);
  375. background-color: #f6f6f7;
  376. border: 1px solid #e2e2e3;
  377. padding: 8px 16px;
  378. min-width: 240px;
  379. }
  380. td {
  381. padding: 8px 16px;
  382. font-size: 14px;
  383. min-width: 200px;
  384. }
  385. .cursor-move {
  386. cursor: move;
  387. }
  388. .table-container {
  389. overflow-x: auto;
  390. white-space: nowrap;
  391. width: 100%;
  392. height: 100%;
  393. }
  394. }
  395. </style>