index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. <template>
  2. <wrapper
  3. fill
  4. margin
  5. padding
  6. background
  7. >
  8. <div class="l-flex--row">
  9. <button
  10. class="o-button c-sibling-item"
  11. @click="onTransferSchedule"
  12. >
  13. 迁移设备排期
  14. </button>
  15. <button
  16. class="o-button c-sibling-item"
  17. @click="onTransferHistory"
  18. >
  19. 迁移发布历史
  20. </button>
  21. </div>
  22. <el-dialog
  23. title="迁移设备排期"
  24. :visible.sync="scheduleTransfering"
  25. custom-class="c-dialog"
  26. >
  27. <c-table
  28. v-if="scheduleTransfering"
  29. :options="deviceOptions"
  30. @pagination="getDevices"
  31. >
  32. <template #header>
  33. <button
  34. v-if="deviceOptions.totalCount"
  35. class="o-button c-sibling-item"
  36. @click="transferPageDevices"
  37. >
  38. 迁移当前页
  39. </button>
  40. <button
  41. v-if="deviceOptions.totalCount > deviceOptions.params.pageSize"
  42. class="o-button c-sibling-item"
  43. @click="transferAllDevices"
  44. >
  45. 迁移所有
  46. </button>
  47. </template>
  48. <el-table-column
  49. prop="id"
  50. label="ID"
  51. align="center"
  52. />
  53. <el-table-column
  54. prop="name"
  55. label="设备名称"
  56. align="center"
  57. />
  58. <el-table-column
  59. label="操作"
  60. align="center"
  61. >
  62. <template v-slot="scope">
  63. <div
  64. class="c-table__btn u-pointer"
  65. @click="transformOneDevice(scope.row)"
  66. >
  67. 迁移至事件模式
  68. </div>
  69. <div
  70. class="c-table__btn u-pointer"
  71. @click="clearDevice(scope.row)"
  72. >
  73. 清除
  74. </div>
  75. </template>
  76. </el-table-column>
  77. </c-table>
  78. </el-dialog>
  79. <el-dialog
  80. title="迁移发布历史"
  81. :visible.sync="historyTransfering"
  82. custom-class="c-dialog"
  83. >
  84. <c-table
  85. v-if="historyTransfering"
  86. :options="historyOptions"
  87. @pagination="getHistoriesByOptions"
  88. >
  89. <template #header>
  90. <button
  91. v-if="historyOptions.totalCount"
  92. class="o-button c-sibling-item"
  93. @click="transferPageHistories"
  94. >
  95. 迁移当前页
  96. </button>
  97. <button
  98. v-if="historyOptions.totalCount > historyOptions.params.pageSize"
  99. class="o-button c-sibling-item"
  100. @click="transferAllHistories"
  101. >
  102. 迁移所有
  103. </button>
  104. </template>
  105. <el-table-column
  106. prop="id"
  107. label="ID"
  108. align="center"
  109. />
  110. <el-table-column
  111. prop="programCalendarName"
  112. label="名称"
  113. align="center"
  114. />
  115. <el-table-column
  116. prop="createTime"
  117. label="审核时间"
  118. align="center"
  119. />
  120. <el-table-column
  121. label="操作"
  122. align="center"
  123. >
  124. <template v-slot="scope">
  125. <div
  126. class="c-table__btn u-pointer"
  127. @click="transferOneHistory(scope.row)"
  128. >
  129. 迁移
  130. </div>
  131. </template>
  132. </el-table-column>
  133. </c-table>
  134. </el-dialog>
  135. </wrapper>
  136. </template>
  137. <script>
  138. import { getDevices } from '@/api/device'
  139. import request from '@/utils/request'
  140. import { toEvent } from '@/utils/event'
  141. import { createListOptions } from '@/utils'
  142. import {
  143. State,
  144. PublishType
  145. } from '@/constant'
  146. export default {
  147. name: 'Transfer',
  148. data () {
  149. return {
  150. scheduleTransfering: false,
  151. deviceOptions: null,
  152. historyTransfering: false,
  153. historyOptions: null
  154. }
  155. },
  156. methods: {
  157. onTransferSchedule () {
  158. this.deviceOptions = createListOptions()
  159. this.getDevices()
  160. this.scheduleTransfering = true
  161. },
  162. getDevices () {
  163. const options = this.deviceOptions
  164. options.error = false
  165. options.loading = true
  166. getDevices(options.params).then(
  167. ({ data, totalCount }) => {
  168. options.list = data.map(({ id, name }) => {
  169. return { id, name }
  170. })
  171. options.totalCount = totalCount
  172. },
  173. () => {
  174. options.error = true
  175. options.list = []
  176. }
  177. ).finally(() => {
  178. options.loading = false
  179. })
  180. },
  181. setDeviceCalendar (id, events) {
  182. return request({
  183. url: `/content/deviceCalender/${id}`,
  184. method: 'POST',
  185. data: events,
  186. custom: true
  187. })
  188. },
  189. clearDevice (device) {
  190. const loading = this.$showLoading()
  191. this.setDeviceCalendar(device.id, []).then(
  192. () => {
  193. this.$message({
  194. type: 'success',
  195. message: '数据已清除'
  196. })
  197. },
  198. () => {
  199. this.$message({
  200. type: 'warning',
  201. message: '数据清除失败'
  202. })
  203. }
  204. ).finally(() => {
  205. this.$closeLoading(loading)
  206. })
  207. },
  208. async transferDevice ({ id }) {
  209. const { data } = await request({
  210. url: `/content/deviceCalender/${id}`,
  211. method: 'GET',
  212. custom: true
  213. })
  214. const schedules = JSON.parse(data.eventDetail) || []
  215. if (schedules.length && !schedules[0].freq) {
  216. const events = []
  217. for (let i = 0; i < schedules.length; i++) {
  218. const event = toEvent(schedules[i])
  219. await event.invoke()
  220. events.push(event)
  221. }
  222. await this.setDeviceCalendar(id, events)
  223. return true
  224. }
  225. return false
  226. },
  227. async transferDevices (devices) {
  228. const length = devices.length
  229. for (let i = 0; i < length; i++) {
  230. const device = devices[i]
  231. try {
  232. await this.transferDevice(device)
  233. } catch (e) {
  234. console.warn(e)
  235. this.$message({
  236. type: 'warning',
  237. message: `${device.name}迁移失败`
  238. })
  239. return
  240. }
  241. }
  242. this.$message({
  243. type: 'success',
  244. message: '数据迁移成功'
  245. })
  246. },
  247. async transformOneDevice (device) {
  248. const loading = this.$showLoading()
  249. try {
  250. const has = await this.transferDevice(device)
  251. if (has) {
  252. this.$message({
  253. type: 'success',
  254. message: '数据迁移成功'
  255. })
  256. } else {
  257. this.$message({
  258. type: 'warning',
  259. message: '数据不需迁移或已迁移'
  260. })
  261. }
  262. } catch (e) {
  263. console.warn(e)
  264. this.$message({
  265. type: 'warning',
  266. message: '数据迁移失败'
  267. })
  268. }
  269. this.$closeLoading(loading)
  270. },
  271. async transferPageDevices () {
  272. const loading = this.$showLoading()
  273. await this.transferDevices(this.deviceOptions.list)
  274. this.$closeLoading(loading)
  275. },
  276. async transferAllDevices () {
  277. if (this.deviceOptions.totalCount <= this.deviceOptions.params.pageSize) {
  278. this.transferPageDevices()
  279. } else {
  280. const loading = this.$showLoading()
  281. try {
  282. const { data } = await getDevices({
  283. pageNum: 1,
  284. pageSize: this.deviceOptions.totalCount
  285. })
  286. await this.transferDevices(data)
  287. } catch (e) {
  288. console.warn(e)
  289. this.$message({
  290. type: 'warning',
  291. message: '获取迁移数据失败'
  292. })
  293. }
  294. this.$closeLoading(loading)
  295. }
  296. },
  297. onTransferHistory () {
  298. this.historyOptions = createListOptions()
  299. this.getHistoriesByOptions()
  300. this.historyTransfering = true
  301. },
  302. getHistories ({ pageNum: pageIndex, pageSize }) {
  303. return request({
  304. url: '/orchestration/calendarRelease/page',
  305. method: 'GET',
  306. params: {
  307. pageIndex,
  308. pageSize,
  309. status: State.RESOLVED
  310. }
  311. })
  312. },
  313. getHistoriesByOptions () {
  314. const options = this.historyOptions
  315. options.error = false
  316. options.loading = true
  317. this.getHistories(options.params).then(
  318. ({ data, totalCount }) => {
  319. options.list = data
  320. options.totalCount = totalCount
  321. },
  322. () => {
  323. options.error = true
  324. options.list = []
  325. }
  326. ).finally(() => {
  327. options.loading = false
  328. })
  329. },
  330. transferHistory ({ id, programCalendarId }) {
  331. return request({
  332. url: `/orchestration/calendarRelease/${id}/save`,
  333. method: 'POST',
  334. data: {
  335. type: PublishType.CALENDAR,
  336. detail: programCalendarId
  337. },
  338. custom: true
  339. })
  340. },
  341. async transferHistories (histories) {
  342. const length = histories.length
  343. for (let i = 0; i < length; i++) {
  344. const history = histories[i]
  345. try {
  346. await this.transferHistory(history)
  347. } catch (e) {
  348. console.warn(e)
  349. this.$message({
  350. type: 'warning',
  351. message: `${history.programCalendarName}迁移失败`
  352. })
  353. return
  354. }
  355. }
  356. this.$message({
  357. type: 'success',
  358. message: '数据迁移成功'
  359. })
  360. const options = this.historyOptions
  361. if (options.params.pageNum > 1 && options.list.length <= length) {
  362. if (options.params.pageSize >= length) {
  363. options.params.pageNum -= 1
  364. } else {
  365. options.params.pageNum = 1
  366. }
  367. }
  368. this.getHistoriesByOptions()
  369. },
  370. async transferOneHistory (history) {
  371. const loading = this.$showLoading()
  372. await this.transferHistories([history])
  373. this.$closeLoading(loading)
  374. },
  375. async transferPageHistories () {
  376. const loading = this.$showLoading()
  377. await this.transferHistories(this.historyOptions.list)
  378. this.$closeLoading(loading)
  379. },
  380. async transferAllHistories () {
  381. if (this.historyOptions.totalCount <= this.historyOptions.params.pageSize) {
  382. this.transferPageHistories()
  383. } else {
  384. const loading = this.$showLoading()
  385. try {
  386. const { data } = await this.getHistories({
  387. pageNum: 1,
  388. pageSize: this.historyOptions.totalCount
  389. })
  390. await this.transferHistories(data)
  391. } catch (e) {
  392. console.warn(e)
  393. this.$message({
  394. type: 'warning',
  395. message: '获取迁移数据失败'
  396. })
  397. }
  398. this.$closeLoading(loading)
  399. }
  400. }
  401. }
  402. }
  403. </script>
  404. <style lang="scss" scoped>
  405. </style>