deviceRun.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <template>
  2. <div>
  3. <!-- 主要运行参数配置全部信息 -->
  4. <el-dialog :title="title" width="70%" top="10vh" :before-close="dialogClose" :visible.sync="visible"
  5. @open="handleOpen" @close="handleClose">
  6. <el-form ref="deviceRunForm" :model="deviceRunForm" :rules="deviceRunRules" label-width="80px"
  7. label-position="right">
  8. <el-row>
  9. <el-col :span="8">
  10. <el-form-item label="名称" prop="dataGroupName">
  11. <el-input v-model="deviceRunForm.dataGroupName" placeholder="名称" maxlength="10" />
  12. </el-form-item>
  13. </el-col>
  14. <el-col :span="8">
  15. <el-form-item label="备注" prop="remark">
  16. <el-input v-model="deviceRunForm.remark" placeholder="请输入备注"
  17. maxlength="100"></el-input>
  18. </el-form-item>
  19. </el-col>
  20. <el-col :span="8">
  21. <el-form-item label="排序号" prop="sortNum">
  22. <el-input v-model="deviceRunForm.sortNum" placeholder="排序号" maxlength="10" />
  23. </el-form-item>
  24. </el-col>
  25. </el-row>
  26. <el-row>
  27. <el-col :span="8">
  28. <el-form-item label="点位">
  29. <el-button size="mini" type="primary" @click="addItemEvent">新增</el-button>
  30. </el-form-item>
  31. </el-col>
  32. </el-row>
  33. <el-row>
  34. <!-- 表格数据信息 -->
  35. <el-table ref="table" :data="deviceRunForm.deviceRunItemList" height="30vh" row-key="id" border>
  36. <el-table-column align="left" label="点位" prop="itemReadName" />
  37. <el-table-column align="left" label="别名" prop="describe" />
  38. <el-table-column align="left" label="数据组" prop="itemGroupName" />
  39. <el-table-column align="left" label="单位" prop="unit" />
  40. <el-table-column align="left" label="序号" prop="sortNum" width="80">
  41. <template slot-scope="scope">
  42. <el-input v-model="scope.row.sortNum"></el-input>
  43. </template>
  44. </el-table-column>
  45. <el-table-column align="left" label="展示" prop="isShow" width="120">
  46. <template slot-scope="scope">
  47. <el-checkbox v-model="scope.row.isShow" :true-label="1" :false-label="0">是</el-checkbox>
  48. </template>
  49. </el-table-column>
  50. <el-table-column align="left" label="创建人" prop="createUserName" width="120"/>
  51. <el-table-column label="操作" align="left" width="200">
  52. <template slot-scope="scope">
  53. <el-button size="mini" v-if="showDelete(scope.row.createUserId)" type="text" icon="el-icon-delete" style="color: red;"
  54. @click="handleDelete(scope.$index, scope.row)">删除</el-button>
  55. </template>
  56. </el-table-column>
  57. </el-table>
  58. </el-row>
  59. </el-form>
  60. <div slot="footer" style="text-align: right">
  61. <el-button size="mini" type="primary" @click="saveDevieRunEvent">确定</el-button>
  62. <el-button size="mini" @click="dialogClose">取消</el-button>
  63. </div>
  64. </el-dialog>
  65. <!-- 选择设备点位 -->
  66. <ChooseItem ref="chooseItem" @acceptChooseItem="acceptChooseItem"></ChooseItem>
  67. </div>
  68. </template>
  69. <script>
  70. import {
  71. addDeviceRun, updateDeviceRun,
  72. getDeviceRunById, getAssignedItemList
  73. } from '@/api/largeScreen/deviceRun'
  74. import ChooseItem from '@/views/device/deviceLedger/chooseItem.vue'
  75. import {getUid, getUsername} from "@/utils/auth";
  76. export default {
  77. components: {
  78. ChooseItem
  79. },
  80. data() {
  81. return {
  82. visible: false,
  83. title: '新增设备运行参数配置',
  84. deviceRunForm: {
  85. id: null,
  86. dataGroupName: null,
  87. sortNum: null,
  88. remark: null,
  89. deviceRunItemList: []
  90. },
  91. deviceRunRules: {
  92. dataGroupName: [
  93. { required: true, message: '数据组名称不能为空', trigger: 'blur' },
  94. ],
  95. sortNum: [
  96. { required: true, message: '排序号不能为空', trigger: 'blur' },
  97. {
  98. validator: (rule, value, callback) => {
  99. if (!(/^[1-9]\d*$/).test(value)) {
  100. callback(new Error('排序号必须输入正整数'))
  101. }
  102. callback()
  103. }
  104. }
  105. ],
  106. }
  107. }
  108. },
  109. methods: {
  110. /** 关闭弹出层 */
  111. dialogClose() {
  112. this.visible = false
  113. },
  114. /** 弹窗打开事件 */
  115. handleOpen() {
  116. if (this.deviceRunForm.id) {
  117. this.getDeviceRunById(this.deviceRunForm.id)
  118. }
  119. this.doLayout()
  120. },
  121. /** 弹窗关闭事件 */
  122. handleClose() {
  123. this.deviceRunForm.id = null
  124. this.deviceRunForm.deviceRunItemList = []
  125. this.resetForm('deviceRunForm')
  126. this.$emit('getDeviceRunPage')
  127. },
  128. /** 重置表单 */
  129. resetForm(formName) {
  130. this.$refs[formName].resetFields();
  131. },
  132. /** 添加点位事件 */
  133. addItemEvent() {
  134. this.getAssignedItemList((data) => {
  135. if (data.data) {
  136. this.$refs.chooseItem.assignedItemIdList = data.data
  137. }
  138. })
  139. this.$refs.chooseItem.title = '添加点位'
  140. this.$refs.chooseItem.visible = true
  141. },
  142. /** 获取所有已经分配的点位id */
  143. getAssignedItemList(callback) {
  144. getAssignedItemList().then(res => {
  145. callback(res)
  146. })
  147. },
  148. /** 保存主要运行参数配置 */
  149. saveDevieRunEvent() {
  150. this.$refs['deviceRunForm'].validate((valid) => {
  151. if (valid) {
  152. if (this.deviceRunForm.deviceRunItemList.length == 0) {
  153. this.$message({
  154. message: '点位不能为空',
  155. type: 'warning'
  156. })
  157. return
  158. }
  159. let showCount = 0
  160. for (let a of this.deviceRunForm.deviceRunItemList) {
  161. if (a.isShow) {
  162. showCount++
  163. }
  164. }
  165. if (showCount != 3) {
  166. this.$message({
  167. message: '点位展示的个数为3个',
  168. type: 'warning'
  169. })
  170. return
  171. }
  172. if (!this.deviceRunForm.id) {
  173. this.addDeviceRun()
  174. } else {
  175. this.updateDeviceRun()
  176. }
  177. }
  178. })
  179. },
  180. /** 通过id获取,设备运行参数配置 */
  181. getDeviceRunById(id) {
  182. getDeviceRunById(id).then(res => {
  183. if (!res || !res.data) {
  184. this.$message({
  185. message: '数据查询失败!',
  186. type: 'warning'
  187. })
  188. return
  189. }
  190. let arr = res.data.deviceRunItemList
  191. let count = 0
  192. for (let a of arr) {
  193. if (!a.itemReadName) {
  194. count++
  195. }
  196. }
  197. if (count != 0) {
  198. this.$message({
  199. message: '存在点位在数据组中被删除,请同步删除没有点位名称的点位',
  200. type: 'warning'
  201. })
  202. }
  203. this.deviceRunForm = res.data
  204. })
  205. },
  206. /** 新增设备运行参数配置 */
  207. addDeviceRun() {
  208. addDeviceRun(this.deviceRunForm).then(res => {
  209. if (!res || !res.data) {
  210. this.$message({
  211. message: '新增设备运行参数配置组失败!',
  212. type: 'warning'
  213. })
  214. return
  215. }
  216. this.$message({
  217. message: res.data,
  218. type: 'success'
  219. })
  220. this.dialogClose()
  221. })
  222. },
  223. /** 修改设备运行参数配置 */
  224. updateDeviceRun() {
  225. let params = JSON.parse(JSON.stringify(this.deviceRunForm));
  226. updateDeviceRun(params).then(res => {
  227. if (!res || !res.data) {
  228. this.$message({
  229. message: '修改设备运行参数配置组失败!',
  230. type: 'warning'
  231. })
  232. return
  233. }
  234. this.$message({
  235. message: res.data,
  236. type: 'success'
  237. })
  238. this.dialogClose()
  239. })
  240. },
  241. /** 重新加载表格,防止样式错乱 */
  242. doLayout() {
  243. this.$nextTick(() => {
  244. this.$refs.table.doLayout()
  245. })
  246. },
  247. /** 接受添加点位过来的数据 */
  248. acceptChooseItem(itemList) {
  249. let arr = []
  250. for (let a of itemList) {
  251. const b = {}
  252. Object.keys(a).forEach(key => {
  253. b[key] = a[key]
  254. })
  255. arr.push(b)
  256. }
  257. //去重,且去重的时候,保留界面的数据,将新数据push进去
  258. let arrF = JSON.parse(JSON.stringify(this.deviceRunForm.deviceRunItemList));
  259. for (let a of arr) {
  260. let flag = false
  261. for (let b of arrF) {
  262. if (a.itemId == b.itemId) {
  263. flag = true;
  264. break;
  265. }
  266. }
  267. if (!flag) {
  268. a["createUserId"] = getUid()
  269. a["createUserName"] = getUsername()
  270. this.deviceRunForm.deviceRunItemList.push(a)
  271. }
  272. }
  273. },
  274. /** 删除点位 */
  275. handleDelete(index, row) {
  276. this.deviceRunForm.deviceRunItemList.splice(index, 1)
  277. },
  278. showDelete(userId){
  279. return !userId || getUid() == userId
  280. }
  281. }
  282. }
  283. </script>
  284. <style rel="stylesheet/scss" lang="scss" scoped>
  285. .dialog-med {
  286. height: 62vh;
  287. .el-dialog__body {
  288. height: 55vh;
  289. overflow: hidden;
  290. }
  291. }
  292. </style>