RelateTableCard.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. <template>
  2. <el-card class="box-card">
  3. <div slot="header" class="clearfix">
  4. <el-tag class="tag_NO">{{ dtName }}</el-tag>
  5. <!-- <span class="title">{{ cardData.title }}</span> -->
  6. <el-button icon="el-icon-plus" type="primary" @click="handleAdd"
  7. >新增数据</el-button
  8. >
  9. </div>
  10. <!-- body -->
  11. <el-table
  12. ref="singleTable"
  13. :data="tableList"
  14. height="300"
  15. width="100%"
  16. highlight-current-row
  17. @row-click="
  18. (row, e, column) => handleCurrentChange(row, e, column, cardData)
  19. "
  20. style="width: 100%"
  21. >
  22. <el-table-column
  23. label="操作"
  24. align="center"
  25. class-name="small-padding fixed-width"
  26. >
  27. <template slot-scope="scope">
  28. <el-dropdown>
  29. <el-button type="warning" plain size="small">
  30. 处理<i class="el-icon-arrow-down el-icon--right"></i>
  31. </el-button>
  32. <el-dropdown-menu slot="dropdown">
  33. <el-dropdown-item>
  34. <el-button
  35. size="mini"
  36. type="text"
  37. icon="el-icon-edit"
  38. @click="handleUpdate(scope.row)"
  39. >修改
  40. </el-button>
  41. </el-dropdown-item>
  42. <el-dropdown-item>
  43. <el-button
  44. size="mini"
  45. type="text"
  46. icon="el-icon-delete"
  47. @click="handleDelete(scope.row)"
  48. >删除
  49. </el-button>
  50. </el-dropdown-item>
  51. </el-dropdown-menu>
  52. </el-dropdown>
  53. </template>
  54. </el-table-column>
  55. <el-table-column
  56. v-for="item in columns"
  57. :prop="item.key"
  58. :key="item.key"
  59. :label="item.value"
  60. >
  61. </el-table-column>
  62. </el-table>
  63. <pagination
  64. v-show="total > 0"
  65. :total="total"
  66. :page.sync="queryParams.pageNum"
  67. :limit.sync="queryParams.pageSize"
  68. @pagination="gettableListHandler(tableKeyWatcher)"
  69. />
  70. <el-dialog :title="title" :visible.sync="open" append-to-body>
  71. <k-form-build
  72. class="formBuild"
  73. ref="addFromRef"
  74. :dynamicData="dynamicData"
  75. :defaultValue="defaultValue"
  76. @submit="tempSubBtn"
  77. @change="formChangeHandler"
  78. :value="jsonData"
  79. />
  80. <div slot="footer" class="dialog-footer">
  81. <el-button type="primary" @click="editConfirmHandler">确 定</el-button>
  82. <el-button @click="cancel">取 消</el-button>
  83. </div>
  84. </el-dialog>
  85. </el-card>
  86. </template>
  87. <script>
  88. import {
  89. delTableData,
  90. dragTableInfo,
  91. listTable,
  92. unionListTableData,
  93. getInfoBySqlKey,
  94. addTableData,
  95. batchEdit,
  96. } from "@/api/tablelist/commonTable";
  97. import { inputDisableComplete } from "@/utils/other";
  98. import { camelCase } from "@/utils";
  99. import { CloudFog } from "lucide-vue";
  100. import { bottom } from "@antv/x6/lib/registry/port-layout/line";
  101. export default {
  102. name: "RelateTableCard",
  103. props: ["cardData", "index", "tableKey", "sort"],
  104. components: {},
  105. data() {
  106. return {
  107. // 总条数
  108. total: 0,
  109. tableList: [],
  110. newsort: "",
  111. queryParams: {
  112. pageNum: 1, // 第几页
  113. pageSize: 10, // 每页大小
  114. orderByColumn: "", // 根据某列排序
  115. isAsc: "", // desc(降序)或 asc(升序)
  116. // 基本查询参数
  117. basicMap: {
  118. tableName: "drag_form",
  119. },
  120. // 当前表字段筛选参数
  121. queryMap: {
  122. // 当前查询基本参数
  123. // ... key : value 当前页面的筛选条件
  124. // 超级查询的唯一值
  125. queryCriteriaValue: "",
  126. // tableCondition: ''
  127. },
  128. },
  129. columns: [],
  130. // 回显表格数据,
  131. defaultValue: {},
  132. currentValue: {},
  133. //存放表单渲染数据
  134. jsonData: {},
  135. // 弹出层标题
  136. title: "",
  137. // cru 弹窗
  138. open: false,
  139. // 回显表格数据,
  140. currentRow: null,
  141. // 用户表格数据
  142. tableList: [],
  143. // 排序方式 默认降序
  144. sortOrder: true,
  145. // 当前模版信息
  146. templateInfo: {},
  147. tableName: "",
  148. queryFromWhere: "",
  149. condition: [],
  150. dtName: "",
  151. // 下拉框动态数据
  152. dynamicData: {},
  153. row: null,
  154. myContion: null,
  155. preRow: {},
  156. };
  157. },
  158. computed: {
  159. // currentValues
  160. cardDataWatcher: function () {
  161. return this.cardData;
  162. },
  163. tableKeyWatcher: function () {
  164. return this.tableKey;
  165. },
  166. sortWatcher: function () {
  167. return this.sort;
  168. },
  169. },
  170. watch: {
  171. cardDataWatcher: {
  172. handler(newval, oldval) {
  173. // debugger;
  174. this.currentValue = newval;
  175. this.$refs.singleTable?.doLayout();
  176. },
  177. deep: true,
  178. immediate: true,
  179. },
  180. tableKeyWatcher: {
  181. handler(newval) {
  182. // console.log(newval)
  183. this.gettableListHandler(newval);
  184. },
  185. deep: true,
  186. immediate: true,
  187. },
  188. sortWatcher: {
  189. handler(newval) {
  190. this.newsort = newval;
  191. },
  192. deep: true,
  193. immediate: true,
  194. },
  195. },
  196. mounted() {},
  197. methods: {
  198. // 根据tablekey请求数据表头数据
  199. async gettableListHandler(tableKey, paramsContion = [], defaultVal = {}) {
  200. const res = await dragTableInfo({ queryMap: { tableKey: tableKey } });
  201. this.getTableHandle(res, (paramsContion = []), (defaultVal = {}));
  202. },
  203. // 处理表格表头
  204. async getTableHandle(res) {
  205. this.columns = [];
  206. // 得到当前模版信息 --- sql columns queryWhere
  207. this.templateInfo = res.data.resultMap;
  208. console.log(1111, this.templateInfo.template);
  209. this.queryParams.orderByColumn =
  210. res.data.resultMap.querySql.orderByColumn;
  211. this.sortOrder = JSON.parse(res.data.resultMap.querySql.sortOrder);
  212. // 根据拖拽时设置当前列表排列顺序
  213. this.queryParams.isAsc = this.sortOrder ? "DESC" : "ASC";
  214. this.tableName = this.templateInfo.template.dtTableName;
  215. this.dtName = this.templateInfo.template.dtName;
  216. // 得到查询条件
  217. this.queryFromWhere = res.data.resultMap.where;
  218. // 得到当前列表表头信息
  219. this.columns = this.columnsHandler(
  220. JSON.parse(this.templateInfo.template.dtColumnName)
  221. );
  222. if (this.templateInfo) {
  223. this.tableCondition = this.templateInfo.querySql.tableCondition;
  224. this.tableCondition = this.tableCondition.split("AND");
  225. this.condition = [];
  226. // 调用查询需要携带当前table的唯一标识
  227. this.queryParams.queryMap.sqlkey = this.templateInfo.template.sqlKey;
  228. this.queryParams.orderByColumn = camelCase(
  229. this.queryParams.orderByColumn || ""
  230. );
  231. // 根据sql语句查询当前表数据
  232. this.contion =
  233. this.templateInfo.template.dtTableName +
  234. "." +
  235. this.templateInfo.template.primaryKey;
  236. this.$nextTick(async () => {
  237. console.log(this.preRow);
  238. if (this.newsort == 0) {
  239. // 第一张表请求表数据不需要条件
  240. await this.getDataHandler();
  241. } else {
  242. if (this.myContion && this.preRow) {
  243. console.log("111", this.myContion, this.preRow);
  244. await this.getDataHandler(this.myContion, this.preRow);
  245. console.log(this.myContion, this.preRow);
  246. }
  247. }
  248. });
  249. //处理三张不同表的依赖关系
  250. // let k;
  251. // this.tableCondition.forEach((item, i) => {
  252. // let val = item.split('=')
  253. // if(i!==0){
  254. // k =val[0].split('.')
  255. // let str = k[0].trim() + '.' + k[1]
  256. // this.condition.push({
  257. // key: k[0].trim(),
  258. // value: k[1]
  259. // })
  260. // }
  261. // })
  262. }
  263. },
  264. // 获取表格数据
  265. async getDataHandler(contion, list) {
  266. console.log(contion, list);
  267. this.myContion = contion;
  268. this.preRow = list;
  269. // console.log(list);
  270. // 处理请求下一张表数据的依赖条件
  271. let tableLists = {};
  272. console.log(this.newsort);
  273. if (contion && list) {
  274. let key = contion.split(".");
  275. console.log(contion, list, key[1]);
  276. this.queryParams.queryMap["#{" + contion] = "'" + list[key[1]] + "'";
  277. // this.queryParams.queryMap["#{" + contion] = list[key[1]];
  278. this.queryParams.queryMap.sqlkey = this.templateInfo.template.sqlKey;
  279. this.tableList = [];
  280. tableLists = await unionListTableData(this.queryParams);
  281. } else if (this.newsort == 0) {
  282. console.log(contion);
  283. tableLists = await unionListTableData(this.queryParams);
  284. }
  285. if (tableLists.code == 200) {
  286. // 默认根据这张表的第一条数据请求下一张表的数据
  287. this.$emit("getDefaultCardData", this.index);
  288. this.tableList = [];
  289. tableLists.rows.forEach((item) => {
  290. this.tableList.push(item.resultMap);
  291. });
  292. // this.tableList = tableLists.rows.filter((item) => item.resultMap);
  293. // 驼峰转换
  294. this.tableList = this.tableList.map((item) => {
  295. let kv = {};
  296. for (let itemKey in item) {
  297. kv[camelCase(itemKey)] = item[itemKey];
  298. }
  299. return kv;
  300. });
  301. // console.log(this.tableList);
  302. }
  303. this.total = tableLists.total;
  304. this.loading = false;
  305. },
  306. // 处理列表信息
  307. columnsHandler(columns) {
  308. let resArr = [];
  309. columns.forEach((item) => {
  310. for (const key in item) {
  311. let tempObj = {};
  312. tempObj.key = camelCase(key);
  313. tempObj.value = item[key];
  314. resArr.push(tempObj);
  315. }
  316. });
  317. return resArr;
  318. },
  319. // 调用父组件方法更新表格数据
  320. handleCurrentChange(row, event, column, val) {
  321. this.row = row;
  322. // console.log(row);
  323. this.$emit("updateData", row, this.index);
  324. },
  325. /** 新增按钮操作 */
  326. handleAdd(row) {
  327. // this.reset();
  328. this.defaultValue = {};
  329. getInfoBySqlKey(this.templateInfo.template.sqlKey).then(({ data }) => {
  330. if (!data || !data.dfVueTemplate) {
  331. this.$message.error("当前表格未绑定表单!");
  332. return;
  333. }
  334. if (data.dfFormSql) {
  335. let dynamicData = JSON.parse(data.dfFormSql);
  336. Object.assign(this.dynamicData, dynamicData);
  337. }
  338. this.jsonData = JSON.parse(data.dfVueTemplate);
  339. this.open = true;
  340. this.title = "添加信息";
  341. // this.form.password = this.initPassword;
  342. this.$nextTick(() => {
  343. this.$refs.addFromRef.reset();
  344. inputDisableComplete();
  345. });
  346. });
  347. },
  348. // 下一表数据
  349. getNextCardData() {
  350. this.handleCurrentChange(this.row);
  351. },
  352. // 根据表格第一条数据请求下一个表格的数据
  353. getDefaultCard() {
  354. this.handleCurrentChange(this.tableList[0]);
  355. },
  356. // 使用提交数据类型的按钮获取数据
  357. tempSubBtn(getData) {
  358. getData()
  359. .then((values) => {
  360. console.log("验证通过", values);
  361. })
  362. .catch(() => {
  363. console.log("验证未通过,获取失败");
  364. });
  365. },
  366. //提交编辑结果按钮回调
  367. editConfirmHandler() {
  368. this.$refs.addFromRef
  369. .getData()
  370. .then(async (values) => {
  371. let data = {
  372. basicMap: {
  373. tableName: this.tableName,
  374. },
  375. addListMap: [values],
  376. commMap: {},
  377. };
  378. if (Object.keys(this.defaultValue).length) {
  379. let updateData = {
  380. // 基本参数
  381. basicMap: {
  382. // 表名
  383. tableName: this.tableName,
  384. },
  385. conditionMap: {},
  386. commMap: {},
  387. };
  388. // 后台接收需要是表中字段真实的名称,无所谓驼峰。
  389. updateData.conditionMap[this.templateInfo.template?.primaryKey] =
  390. this.defaultValue[
  391. camelCase(this.templateInfo.template?.primaryKey)
  392. ];
  393. Object.keys(values).map((k) => {
  394. updateData.commMap[k] = values[k];
  395. });
  396. let res = await batchEdit(updateData);
  397. if (res.code == 200) {
  398. this.$modal.msgSuccess("修改成功");
  399. } else {
  400. this.$modal.msgError("修改失败");
  401. }
  402. } else {
  403. // data;
  404. let defaultData = this.getDefaultData();
  405. data.addListMap.forEach((item) => {
  406. Object.assign(item, defaultData);
  407. });
  408. let res = await addTableData(data);
  409. if (res.code == 200) {
  410. this.$modal.msgSuccess("添加成功");
  411. } else {
  412. this.$modal.msgError("添加失败");
  413. }
  414. }
  415. // this.tableList = []
  416. if (this.index == 0) {
  417. this.getDataHandler();
  418. } else {
  419. // 判断修改是否点击了上一表格的数据
  420. if (this.row) {
  421. this.$emit("getChildData", this.index);
  422. } else {
  423. this.$emit("getEditHandle", this.index);
  424. }
  425. }
  426. this.defaultValue = {};
  427. this.open = false;
  428. })
  429. .catch((res) => {
  430. console.log(res);
  431. this.$modal.msgError("表单校验失败,请规范填写数据");
  432. });
  433. },
  434. getDefaultData() {
  435. console.log(this.queryFromWhere);
  436. console.log(this.preRow);
  437. let res = {};
  438. this.queryFromWhere.forEach((item) => {
  439. let { conditionDefaultValue, conditionField } = item;
  440. res[camelCase(conditionField.split(".")[1])] =
  441. conditionDefaultValue.includes(".")
  442. ? this.preRow[conditionDefaultValue.split(".")[1]]
  443. : conditionDefaultValue;
  444. });
  445. return res;
  446. },
  447. // 取消按钮
  448. cancel() {
  449. this.open = false;
  450. this.defaultVal = {};
  451. // this.reset();
  452. },
  453. /** 修改按钮操作 */
  454. handleUpdate(row) {
  455. let obj = {};
  456. for (let key in row) {
  457. let modifiedTable = key
  458. .replace(/[A-Z]/g, (match) => `_${match}`)
  459. .toLowerCase();
  460. obj[modifiedTable] = row[key];
  461. }
  462. getInfoBySqlKey(this.templateInfo.template.sqlKey).then(({ data }) => {
  463. if (!data || !data.dfVueTemplate) {
  464. this.$message.error("当前表格未绑定表单!");
  465. return;
  466. }
  467. Object.assign(this.defaultValue, obj);
  468. // console.log(this.defaultValue)
  469. this.jsonData = JSON.parse(data.dfVueTemplate);
  470. this.open = true;
  471. this.title = "修改信息";
  472. // this.form.password = this.initPassword;
  473. this.$nextTick(() => {
  474. this.$refs.addFromRef.setData(obj);
  475. });
  476. });
  477. return;
  478. this.reset();
  479. const userId = row.userId || this.ids;
  480. getUser(userId).then((response) => {
  481. this.form = response.data;
  482. this.postOptions = response.posts;
  483. this.roleOptions = response.roles;
  484. this.$set(this.form, "postIds", response.postIds);
  485. this.$set(this.form, "roleIds", response.roleIds);
  486. this.open = true;
  487. this.title = "修改当前信息";
  488. this.form.password = "";
  489. });
  490. },
  491. // k-form-build表单变化回调
  492. formChangeHandler(value, label) {
  493. // console.log(value, label);
  494. },
  495. /** 删除按钮操作
  496. *
  497. * 删除提示信息语句(标识)
  498. * */
  499. handleDelete(row) {
  500. let delIds = this.ids;
  501. let primary = camelCase(this.templateInfo.template?.primaryKey);
  502. if (row[primary] != undefined && row[primary] != null) {
  503. delIds = [];
  504. delIds.push(row[primary]);
  505. }
  506. let data = {
  507. basicMap: {
  508. tableName: this.tableName,
  509. },
  510. conditionMap: {
  511. // id: delIds,
  512. },
  513. };
  514. data.conditionMap[this.templateInfo.template?.primaryKey] = delIds;
  515. this.$modal
  516. .confirm('是否确认删除"' + delIds + '"的数据项?')
  517. .then(function () {
  518. return delTableData(data);
  519. })
  520. .then(() => {
  521. // 调用子组件查询方法 目的是携带上子组件中的查询参数
  522. // this.$refs.mychild.pageList();
  523. this.$modal.msgSuccess("删除成功");
  524. this.gettableListHandler(this.tableKey);
  525. })
  526. .catch(() => {});
  527. },
  528. },
  529. };
  530. </script>
  531. <style lang="scss" scoped>
  532. ::v-deep .el-card__body {
  533. padding: 0px;
  534. padding-bottom: 10px;
  535. }
  536. .item {
  537. margin-bottom: 18px;
  538. }
  539. .clearfix {
  540. display: flex;
  541. align-items: center;
  542. justify-content: space-between;
  543. }
  544. .box-card {
  545. width: 100%;
  546. }
  547. .title {
  548. display: block;
  549. max-width: 60px;
  550. white-space: nowrap;
  551. overflow: hidden;
  552. text-overflow: ellipsis;
  553. }
  554. </style>