CompanyManagement.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. <template>
  2. <!-- 公司管理 -->
  3. <div class="home">
  4. <actionBar :operateList="operateList" :formList="formList" :ruleForm="searchRuleForm" @openModel="openModel"
  5. @searchProtocol="searchProtocol"></actionBar>
  6. <div class="card_content">
  7. <!-- 表单 -->
  8. <treeTable :treeData="treeData" :treeDataList="treeDataList" @buttonData="buttonData"></treeTable>
  9. <!-- 分页 -->
  10. <div v-if="Total">
  11. <pagination :total="Total" :currentPage="Pagination.PageIndex" @changeSize="changeSize"
  12. @changeCurrent="changeCurrent">
  13. </pagination>
  14. </div>
  15. </div>
  16. <el-dialog :title="staffTitle" :visible.sync="staffDialogVisible" width="600px" :close-on-click-modal="false"
  17. @close="closeDialog">
  18. <forms ref="childRules" :formNewList="formRuleList" :ruleForm="ruleForm" labelWidth="150px"></forms>
  19. <span slot="footer" class="dialog-footer">
  20. <el-button plain @click="staffDialogVisible = false">取 消</el-button>
  21. <el-button type="primary" :loading="confirmLoading" @click="handleAdd">确 定</el-button>
  22. </span>
  23. </el-dialog>
  24. <!-- 导入公司对话框 -->
  25. <el-dialog title="导入公司" :visible.sync="importDialogVisible" width="600px" :close-on-click-modal="false"
  26. @close="closeImportDialog">
  27. <div style="max-height: 500px; overflow-y: auto;">
  28. <el-tree
  29. ref="companyTree"
  30. :data="thirdTreeData"
  31. :props="treeProps"
  32. show-checkbox
  33. node-key="Id"
  34. :default-expand-all="true"
  35. :check-strictly="false"
  36. @check="handleTreeCheck">
  37. </el-tree>
  38. </div>
  39. <span slot="footer" class="dialog-footer">
  40. <el-button plain @click="importDialogVisible = false">取 消</el-button>
  41. <el-button type="primary" :loading="importLoading" @click="handleImport">确 定</el-button>
  42. </span>
  43. </el-dialog>
  44. </div>
  45. </template>
  46. <script>
  47. import {
  48. getCompany,
  49. getCompanyDetails,
  50. addCompany,
  51. putCompany,
  52. delCompany,
  53. getThirdTree,
  54. importThirdTree
  55. } from '@/api/company'
  56. import actionBar from '@/components/actionBar'
  57. import treeTable from '@/components/treeTable'
  58. import pagination from '@/components/pagination'
  59. import forms from '@/components/forms'
  60. import {
  61. formRules,
  62. employee
  63. } from "./company.js";
  64. export default {
  65. name: 'UserManagement',
  66. components: {
  67. actionBar,
  68. treeTable,
  69. pagination,
  70. forms,
  71. },
  72. data() {
  73. return {
  74. operateList: [{
  75. type: 'add',
  76. title: '添加公司',
  77. icon: 'el-icon-plus',
  78. }, {
  79. type: 'import',
  80. title: '导入公司',
  81. icon: 'el-icon-upload2',
  82. }],
  83. formList: [{
  84. type: 'input',
  85. label: '名称',
  86. field: 'name',
  87. placeholder: '名称',
  88. }],
  89. searchRuleForm: {
  90. name: '',
  91. },
  92. searchValue: {},
  93. Pagination: {
  94. PageIndex: 1,
  95. PageSize: 10,
  96. },
  97. Total: 0,
  98. operationType: '',
  99. tableList: employee(),
  100. tableData: [],
  101. staffTitle: '添加',
  102. staffDialogVisible: false,
  103. formRuleList: [],
  104. ruleForm: {
  105. name: '',
  106. appName: '',
  107. companyCoordinate: '',
  108. companyAddress: '',
  109. coldKey: '',
  110. isIceReleaseCold: false,
  111. isCoolerReleaseCold: false,
  112. isOutStorage: false,
  113. iceRaftManage: false,
  114. remark: '',
  115. },
  116. confirmLoading: false,
  117. treeData: employee(),
  118. treeDataList: [],
  119. parentId: null,
  120. // 导入公司相关
  121. importDialogVisible: false,
  122. thirdTreeData: [],
  123. treeProps: {
  124. children: 'Children',
  125. label: 'T_name'
  126. },
  127. selectedCompanyNodes: [],
  128. importLoading: false,
  129. }
  130. },
  131. mounted() {
  132. const dataList = formRules();
  133. this.formRuleList = dataList;
  134. this.getList()
  135. },
  136. methods: {
  137. // 搜索
  138. searchProtocol(value) {
  139. this.searchValue = value
  140. this.getList()
  141. },
  142. // 获取公司列表
  143. getList() {
  144. var params = {
  145. page: this.Pagination.PageIndex,
  146. pageSize: this.Pagination.PageSize,
  147. ...this.searchValue
  148. }
  149. getCompany(params).then(res => {
  150. if (res.code == 200) {
  151. this.treeDataList = res.data
  152. this.Total = res.data.count
  153. }
  154. })
  155. },
  156. // 弹窗表单添加
  157. handleAdd() {
  158. let flag = this.$refs['childRules'].validateForm();
  159. if (flag) {
  160. if (this.operationType == 'add' || this.operationType == 'subAdd') {
  161. this.confirmLoading = true
  162. var params = {
  163. ...this.ruleForm
  164. }
  165. if (this.operationType == 'subAdd') {
  166. params.parentId = this.parentId
  167. }
  168. addCompany(params).then(res => {
  169. if (res.code == 200) {
  170. this.$message({
  171. message: '操作成功',
  172. type: 'success'
  173. });
  174. this.getList()
  175. }
  176. this.staffDialogVisible = false
  177. this.confirmLoading = false
  178. }).catch(() => {
  179. this.confirmLoading = false
  180. })
  181. } else if (this.operationType == 'edit') {
  182. this.confirmLoading = true
  183. var params = {
  184. id: this.ruleForm.id,
  185. name: this.ruleForm.name,
  186. appName: this.ruleForm.appName,
  187. remark: this.ruleForm.remark,
  188. coldKey: this.ruleForm.coldKey,
  189. companyAddress: this.ruleForm.companyAddress,
  190. isIceReleaseCold: this.ruleForm.isIceReleaseCold,
  191. isCoolerReleaseCold: this.ruleForm.isCoolerReleaseCold,
  192. isOutStorage: this.ruleForm.isOutStorage,
  193. iceRaftManage: this.ruleForm.iceRaftManage,
  194. }
  195. putCompany(params).then(res => {
  196. if (res.code == 200) {
  197. this.$message({
  198. message: '操作成功',
  199. type: 'success'
  200. });
  201. this.getList()
  202. }
  203. this.staffDialogVisible = false
  204. this.confirmLoading = false
  205. }).catch(() => {
  206. this.confirmLoading = false
  207. })
  208. }
  209. } else {
  210. this.$message.error('表单信息不完整,请继续填写完整');
  211. }
  212. },
  213. buttonData(row, type) {
  214. this.operationType = type
  215. if (type == 'edit') {
  216. this.staffTitle = '编辑'
  217. this.staffDialogVisible = true
  218. setTimeout(() => {
  219. this.$nextTick(() => {
  220. this.ruleForm = JSON.parse(JSON.stringify(row))
  221. })
  222. })
  223. } else if (type == 'del') {
  224. this.deleteUser(row.id)
  225. } else if (type == 'subAdd') {
  226. this.staffTitle = '添加子级'
  227. this.staffDialogVisible = true
  228. this.parentId = row.id
  229. }
  230. },
  231. openModel(type) {
  232. if (type === 'import') {
  233. this.openImportDialog()
  234. } else {
  235. this.staffTitle = '添加'
  236. this.staffDialogVisible = true
  237. this.operationType = type
  238. }
  239. },
  240. // 打开导入公司对话框
  241. openImportDialog() {
  242. this.importDialogVisible = true
  243. this.thirdTreeData = []
  244. this.selectedCompanyNodes = []
  245. this.getThirdTreeData()
  246. },
  247. // 获取第三方公司树形结构
  248. getThirdTreeData() {
  249. getThirdTree().then(res => {
  250. if (res.code == 200) {
  251. this.thirdTreeData = res.data || []
  252. }
  253. }).catch(() => {
  254. this.$message.error('获取公司树形结构失败')
  255. })
  256. },
  257. // 树节点选择变化
  258. handleTreeCheck(data, checkedInfo) {
  259. // 保存选中的节点ID
  260. this.selectedCompanyNodes = checkedInfo.checkedKeys || []
  261. },
  262. // 处理导入
  263. handleImport() {
  264. const checkedKeys = this.$refs.companyTree.getCheckedKeys()
  265. if (checkedKeys.length === 0) {
  266. this.$message.warning('请至少选择一个公司')
  267. return
  268. }
  269. // 构建需要导入的数据结构
  270. const importData = this.buildImportData(checkedKeys)
  271. if (importData.length === 0) {
  272. this.$message.warning('请选择要导入的公司')
  273. return
  274. }
  275. this.importLoading = true
  276. importThirdTree(importData).then(res => {
  277. if (res.code == 200) {
  278. this.$message({
  279. message: '导入成功',
  280. type: 'success'
  281. })
  282. this.importDialogVisible = false
  283. this.getList()
  284. }
  285. this.importLoading = false
  286. }).catch(() => {
  287. this.importLoading = false
  288. })
  289. },
  290. // 构建导入数据结构
  291. buildImportData(checkedKeys) {
  292. // 从原始数据中查找所有选中的节点
  293. const findNodeById = (nodes, id) => {
  294. for (const node of nodes) {
  295. if (node.Id === id) {
  296. return node
  297. }
  298. if (node.Children && node.Children.length > 0) {
  299. const found = findNodeById(node.Children, id)
  300. if (found) return found
  301. }
  302. }
  303. return null
  304. }
  305. // 递归构建树形结构,只包含选中的节点及其选中的子节点
  306. const buildTree = (node) => {
  307. const result = {
  308. Id: node.Id,
  309. T_mid: node.T_mid,
  310. T_name: node.T_name,
  311. T_key: node.T_key || '',
  312. T_Address: node.T_Address || node.companyAddress || ''
  313. }
  314. // 处理子节点
  315. if (node.Children && node.Children.length > 0) {
  316. const selectedChildren = node.Children
  317. .filter(child => checkedKeys.includes(child.Id))
  318. .map(child => buildTree(child))
  319. if (selectedChildren.length > 0) {
  320. result.Children = selectedChildren
  321. }
  322. }
  323. return result
  324. }
  325. // 找出所有根节点(T_mid为0或父节点未被选中)
  326. const rootNodes = []
  327. for (const id of checkedKeys) {
  328. const node = findNodeById(this.thirdTreeData, id)
  329. if (node) {
  330. // 检查是否是根节点
  331. const isRoot = node.T_mid === 0 || !checkedKeys.includes(node.T_mid)
  332. if (isRoot) {
  333. rootNodes.push(node)
  334. }
  335. }
  336. }
  337. // 构建树形结构
  338. return rootNodes.map(node => buildTree(node))
  339. },
  340. // 关闭导入对话框
  341. closeImportDialog() {
  342. this.thirdTreeData = []
  343. this.selectedCompanyNodes = []
  344. if (this.$refs.companyTree) {
  345. this.$refs.companyTree.setCheckedKeys([])
  346. }
  347. },
  348. // 删除公司
  349. deleteUser(id) {
  350. this.$confirm('此操作将永久删除该公司, 是否继续?', '提示', {
  351. confirmButtonText: '确定',
  352. cancelButtonText: '取消',
  353. type: 'warning'
  354. }).then(() => {
  355. delCompany({
  356. id: id,
  357. }).then(res => {
  358. if (res.code == 200) {
  359. this.$message({
  360. message: '操作成功',
  361. type: 'success'
  362. });
  363. this.getList()
  364. }
  365. })
  366. }).catch(() => {});
  367. },
  368. changeSize(val) {
  369. this.Pagination.PageSize = val
  370. this.getList()
  371. },
  372. changeCurrent(val) {
  373. this.Pagination.PageIndex = val
  374. this.getList()
  375. },
  376. // 清空表单
  377. closeDialog() {
  378. // this.ruleForm = {}
  379. this.ruleForm.name = ''
  380. this.ruleForm.appName = ''
  381. this.ruleForm.companyAddress = ''
  382. this.ruleForm.coldKey = ''
  383. this.ruleForm.isIceReleaseCold = false
  384. this.ruleForm.isCoolerReleaseCold = false
  385. this.ruleForm.isOutStorage = false
  386. this.ruleForm.iceRaftManage = false
  387. this.ruleForm.remark = ''
  388. this.$refs.childRules.resetCheck();
  389. }
  390. }
  391. }
  392. </script>