index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. <template>
  2. <d2-container>
  3. <template slot="header"
  4. style="padding: 5px;">
  5. <el-form :model="dutyDetail">
  6. <el-row>
  7. <el-col :span="4">
  8. <el-form-item label="标题"
  9. label-width="120px">
  10. {{dutyDetail.Title}}
  11. </el-form-item>
  12. </el-col>
  13. <el-col :span="4">
  14. <el-form-item label="学年"
  15. label-width="120px">
  16. {{dutyDetail.Year}}年
  17. </el-form-item>
  18. </el-col>
  19. <el-col :span="4">
  20. <el-form-item label="学期"
  21. label-width="120px">
  22. {{this.termName}}
  23. </el-form-item>
  24. </el-col>
  25. </el-row>
  26. <el-row>
  27. <el-form-item label="地点"
  28. size="mini"
  29. label-width="120px">
  30. <el-checkbox-group v-model="selectLocal">
  31. <el-checkbox-button v-for="item in LocalList"
  32. :label="item.ItemValue"
  33. :key="item.ItemValue"
  34. style="float:left">{{item.ItemName}}</el-checkbox-button>
  35. </el-checkbox-group>
  36. </el-form-item>
  37. </el-row>
  38. <el-row>
  39. <el-form-item label="值班时间"
  40. size="mini"
  41. label-width="120px">
  42. <el-checkbox-group v-model="selectTime">
  43. <el-checkbox-button v-for="item in TimeList"
  44. :label="item.ItemValue"
  45. :key="item.ItemValue"
  46. style="float:left">{{item.ItemName}}</el-checkbox-button>
  47. </el-checkbox-group>
  48. </el-form-item>
  49. </el-row>
  50. <el-row>
  51. <el-form-item label="值班人员"
  52. size="mini"
  53. label-width="120px">
  54. <el-button @click="personClick(item.Id)"
  55. v-for="item in PeopleList"
  56. :key="item.Id"
  57. :label="item.Id"
  58. style="float:left">{{item.PersonnelName}}</el-button>
  59. </el-form-item>
  60. </el-row>
  61. <el-row :gutter="24">
  62. <el-col :span="16">
  63. &nbsp;
  64. </el-col>
  65. <el-col :span="2">
  66. <el-button size="mini"
  67. @click="addList()"
  68. type="success">生成值班表</el-button>
  69. </el-col>
  70. <el-col :span="2">
  71. <el-button size="mini"
  72. type="primary"
  73. @click="addDuytDetail()">保存值班表</el-button>
  74. </el-col>
  75. <el-col :span="2">
  76. <el-button size="mini"
  77. type="warning"
  78. @click="exportExcel()">导出</el-button>
  79. </el-col>
  80. <el-col :span="1">
  81. <el-button size="mini"
  82. type="danger"
  83. Updated
  84. upstream
  85. @click="back()">关闭</el-button>
  86. </el-col>
  87. </el-row>
  88. </el-form>
  89. </template>
  90. <el-table :data="list"
  91. border
  92. fit
  93. tooltip-effect="dark"
  94. style="width: 100%"
  95. height="100%"
  96. @cell-click="cellclick"
  97. @header-click="headclick"
  98. :cell-class-name="cellBg"
  99. :key="refresh"
  100. id="out-table">
  101. <el-table-column fit
  102. prop="Local"
  103. min-width="160px"
  104. label="地点"
  105. align="center"
  106. show-overflow-tooltip
  107. :formatter="formatLocal"></el-table-column>
  108. <el-table-column prop="Time"
  109. label="时间段"
  110. align="center"
  111. min-width="120px"
  112. show-overflow-tooltip
  113. :formatter="formatTime"></el-table-column>
  114. <el-table-column prop="Monday"
  115. label="周一"
  116. align="center"
  117. min-width="120px"
  118. show-overflow-tooltip
  119. :formatter="formatPerson"></el-table-column>
  120. <el-table-column prop="Tuesday"
  121. label="周二"
  122. align="center"
  123. min-width="120px"
  124. show-overflow-tooltip
  125. :formatter="formatPerson"></el-table-column>
  126. <el-table-column prop="Wednesday"
  127. label="周三"
  128. align="center"
  129. min-width="120px"
  130. show-overflow-tooltip
  131. :formatter="formatPerson"></el-table-column>
  132. <el-table-column prop="Thursday"
  133. label="周四"
  134. align="center"
  135. min-width="120px"
  136. show-overflow-tooltip
  137. :formatter="formatPerson"></el-table-column>
  138. <el-table-column prop="Friday"
  139. label="周五"
  140. align="center"
  141. min-width="120px"
  142. show-overflow-tooltip
  143. :formatter="formatPerson"></el-table-column>
  144. <el-table-column prop="Saturday"
  145. label="周六"
  146. align="center"
  147. min-width="120px"
  148. show-overflow-tooltip
  149. :formatter="formatPerson"></el-table-column>
  150. <el-table-column prop="Sunday"
  151. label="周日"
  152. align="center"
  153. min-width="120px"
  154. show-overflow-tooltip
  155. :formatter="formatPerson"></el-table-column>
  156. <el-table-column label="操作"
  157. width="80px"
  158. align="center">
  159. <template slot-scope="scope">
  160. <el-button size="mini"
  161. type="danger"
  162. title="删除"
  163. @click="deleteRow(scope.$index,list)"
  164. style="margin-left:3px;"
  165. icon="el-icon-delete"
  166. circle></el-button>
  167. </template>
  168. </el-table-column>
  169. </el-table>
  170. </d2-container>
  171. </template>
  172. <script>
  173. // 总列数
  174. import { mapState, mapActions } from 'vuex'
  175. import DutyApi from '@/api/duty'
  176. import itemDetailApi from '@/api/sysadmin/itemdetail'
  177. import PersonnelApi from '@/api/personnel'
  178. import FileSaver from 'file-saver'
  179. import XLSX from 'xlsx'
  180. const columnNum = 9
  181. // 固定列数
  182. const fixRowHeadNum = 2
  183. // todo 欠优化 列属性对应
  184. const columnProperty = [
  185. 'Local',
  186. 'Time',
  187. 'Monday',
  188. 'Tuesday',
  189. 'Wednesday',
  190. 'Thursday',
  191. 'Friday',
  192. 'Saturday',
  193. 'Sunday'
  194. ]
  195. export default {
  196. name: 'dutyEdit',
  197. data () {
  198. return {
  199. LocalList: [],
  200. PeopleList: [],
  201. TimeList: [],
  202. termList: [],
  203. termName: '',
  204. selectLocal: [],
  205. selectTime: [],
  206. dutyDetail: {
  207. Id: -1,
  208. Year: -1,
  209. Term: -1
  210. },
  211. year: '',
  212. years: [],
  213. dialogvisible: false,
  214. // 刷新标志 刷新表格样式
  215. refresh: 123,
  216. // 已选列表
  217. selectcell: new Map(),
  218. // 数据列表
  219. list: []
  220. }
  221. },
  222. computed: {
  223. ...mapState('d2admin/page', [
  224. 'opened',
  225. 'current' // 用户获取当前页面的地址,用于关闭
  226. ])
  227. },
  228. mounted () {
  229. this.getLocal()
  230. this.getPeople()
  231. this.getDutyTime()
  232. let _this = this
  233. _this.dutyDetail = _this.$route.query.duty
  234. _this.termList = _this.$route.query.term
  235. this.getTerm()
  236. this.getDetailData()
  237. },
  238. methods: {
  239. // 导出excel
  240. exportExcel () {
  241. /* out-table关联导出的dom节点 */
  242. var wb = XLSX.utils.table_to_book(document.querySelector('#out-table'))
  243. /* get binary string as output */
  244. var wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array' })
  245. try {
  246. FileSaver.saveAs(new Blob([wbout], { type: 'application/octet-stream' }), '值班表.xlsx')
  247. } catch (e) { if (typeof console !== 'undefined') console.log(e, wbout) }
  248. return wbout
  249. },
  250. // 初始化单选框
  251. selectCheckBox () {
  252. let selectLocal = new Map()
  253. let selectTime = new Map()
  254. for (var i = 0; i < this.list.length; i++) {
  255. if (selectLocal.get(this.list[i].Local + '') !== true) {
  256. this.selectLocal.push(this.list[i].Local + '')
  257. }
  258. if (selectTime.get(this.list[i].Time + '') !== true) {
  259. this.selectTime.push(this.list[i].Time + '')
  260. }
  261. selectLocal.set(this.list[i].Local + '', true)
  262. selectTime.set(this.list[i].Time + '', true)
  263. }
  264. },
  265. // 获取值班子表表格
  266. getDetailData () {
  267. let _this = this
  268. DutyApi.getDetailByDutyId({
  269. DutyId: _this.dutyDetail.Id
  270. }).then(res => {
  271. if (res.length > 0) {
  272. _this.list = res
  273. _this.selectCheckBox()
  274. }
  275. })
  276. },
  277. // 新增表格
  278. addList () {
  279. if (this.selectLocal == 0 && this.selectTime.length == 0) {
  280. return
  281. }
  282. // 清空已选择
  283. this.selectcell = new Map()
  284. let _this = this; let listMap = new Map(); let newList = []
  285. this.list.forEach(row => {
  286. listMap.set(row.Local + '_' + row.Time, row)
  287. })
  288. this.selectLocal.forEach(function (value, key) {
  289. _this.selectTime.forEach(function (valuee, keyy) {
  290. if (listMap.get(value + '_' + valuee)) {
  291. newList.push(listMap.get(value + '_' + valuee))
  292. } else {
  293. newList.push({
  294. DutyId: _this.dutyDetail.Id,
  295. Local: value,
  296. Time: valuee,
  297. Monday: 0,
  298. Tuesday: 0,
  299. Wednesday: 0,
  300. Thursday: 0,
  301. Friday: 0,
  302. Saturday: 0,
  303. Sunday: 0
  304. })
  305. }
  306. })
  307. })
  308. this.list = newList
  309. },
  310. // 保存值班子表信息
  311. addDuytDetail () {
  312. if (this.dutyDetail.Id) {
  313. DutyApi.Saves({ DataList: this.list, DutyId: this.dutyDetail.Id })
  314. .then(res => {
  315. })
  316. .catch(err => {
  317. // handle error
  318. console.error(err)
  319. })
  320. } else {
  321. console.log('error submit!!')
  322. return false
  323. }
  324. },
  325. // 删除一行
  326. deleteRow (index, rows) {
  327. rows.splice(index, 1)
  328. },
  329. // 获取字典表地点
  330. getLocal () {
  331. let _this = this
  332. itemDetailApi.getItemDetailByItemCode({ ItemCode: 'Local' })
  333. .then(res => {
  334. _this.LocalList = res
  335. })
  336. .catch(err => {
  337. console.error(err)
  338. })
  339. },
  340. // 多选框回显
  341. change (index, item) {
  342. },
  343. // 获取字典表值班人员
  344. getPeople () {
  345. let _this = this
  346. PersonnelApi.getAllPersonnel({ current: 1, size: 999 })
  347. .then(res => {
  348. _this.PeopleList = res.records
  349. })
  350. .catch(err => {
  351. console.error(err)
  352. })
  353. },
  354. // 获取字典表课程时间段
  355. getDutyTime () {
  356. let _this = this
  357. itemDetailApi.getItemDetailByItemCode({ ItemCode: 'DutyTime' })
  358. .then(res => {
  359. _this.TimeList = res
  360. })
  361. .catch(err => {
  362. console.error(err)
  363. })
  364. },
  365. // 单元格、行选中
  366. cellclick (row, column, cell, event) {
  367. // 第3列开始可以选中
  368. // 单击单元格选中
  369. // 取消操作行选中
  370. if (cell.cellIndex >= columnNum) {
  371. return
  372. }
  373. if (cell.cellIndex >= fixRowHeadNum) {
  374. if (this.selectcell.get(row.Local + '_' + row.Time + '_' + column.property)) {
  375. this.selectcell.set(row.Local + '_' + row.Time + '_' + column.property, false)
  376. } else {
  377. this.selectcell.set(row.Local + '_' + row.Time + '_' + column.property, this.list[row.index])
  378. }
  379. }
  380. // 第三列之前选中整行
  381. // 行选中
  382. var currentcolumnindex = column.index
  383. if (cell.cellIndex < fixRowHeadNum) {
  384. for (var i = cell.cellIndex; i < columnNum - 1; i++) {
  385. currentcolumnindex = currentcolumnindex + 1
  386. var nextSibling = cell.nextSibling
  387. cell = nextSibling
  388. if (i >= fixRowHeadNum - 1) {
  389. if (this.selectcell.get(row.Local + '_' + row.Time + '_' + columnProperty[currentcolumnindex])) {
  390. this.selectcell.set(row.Local + '_' + row.Time + '_' + columnProperty[currentcolumnindex], false)
  391. } else {
  392. this.selectcell.set(row.Local + '_' + row.Time + '_' + columnProperty[currentcolumnindex], this.list[row.index])
  393. }
  394. }
  395. }
  396. }
  397. this.refresh = Math.random()
  398. },
  399. // 列选中
  400. headclick (column, event) {
  401. // 判断是否为可选列
  402. // 取消操作列选中
  403. if (column.index >= columnNum) {
  404. return
  405. }
  406. if (column.index > fixRowHeadNum - 1) {
  407. for (var i = 0; i < this.list.length; i++) {
  408. if (this.selectcell.get(this.list[i].Local + '_' + this.list[i].Time + '_' + column.property)) {
  409. this.selectcell.set(this.list[i].Local + '_' + this.list[i].Time + '_' + column.property, false)
  410. } else {
  411. this.selectcell.set(this.list[i].Local + '_' + this.list[i].Time + '_' + column.property, this.list[i])
  412. }
  413. }
  414. }
  415. this.refresh = Math.random()
  416. },
  417. // 选择值班人员
  418. personClick (person) {
  419. this.selectcell.forEach(function (value, key) {
  420. if (value) {
  421. var property = key.split('_')
  422. value[property[2]] = person
  423. }
  424. })
  425. this.selectcell = new Map()
  426. },
  427. // 更新背景
  428. cellBg ({ row, column, rowIndex, columnIndex }) {
  429. row.index = rowIndex
  430. column.index = columnIndex
  431. let _this = this
  432. // 注意这里是解构
  433. // 利用单元格的 className 的回调方法,给行列索引赋值
  434. if (_this.selectcell.get(row.Local + '_' + row.Time + '_' + column.property)) {
  435. return 'selectedCell'
  436. }
  437. },
  438. // 匹配学期
  439. formatTerm (row, column) {
  440. for (var i = 0; i < this.termList.length; i++) {
  441. if (parseInt(this.termList[i].ItemValue) === parseInt(row.Term)) {
  442. return this.termList[i].ItemName
  443. }
  444. }
  445. },
  446. // 获取学期名称
  447. getTerm () {
  448. let _this = this
  449. _this.termList.forEach(function (value, key) {
  450. if (_this.dutyDetail.Term == value.ItemValue) {
  451. _this.termName = value.ItemName
  452. }
  453. })
  454. },
  455. formatPerson (row, column, cellValue, index) {
  456. let label = '休息'
  457. for (var i = 0; i < this.PeopleList.length; i++) {
  458. if (this.PeopleList[i].Id == cellValue) {
  459. return this.PeopleList[i].PersonnelName
  460. }
  461. }
  462. return label
  463. },
  464. formatLocal (row, column, cellValue, index) {
  465. for (var i = 0; i < this.LocalList.length; i++) {
  466. if (this.LocalList[i].ItemValue == cellValue) {
  467. return this.LocalList[i].ItemName
  468. }
  469. }
  470. },
  471. formatTime (row, column, cellValue, index) {
  472. for (var i = 0; i < this.TimeList.length; i++) {
  473. if (this.TimeList[i].ItemValue == cellValue) {
  474. return this.TimeList[i].ItemName
  475. }
  476. }
  477. },
  478. // 关闭页面
  479. ...mapActions('d2admin/page', [
  480. 'close_return'
  481. ]),
  482. // 关闭
  483. back () {
  484. this.close_return({
  485. tagName: this.$route.fullPath,
  486. returnTagName: '/duty'
  487. })
  488. }
  489. }
  490. }
  491. </script>
  492. <style>
  493. .selectedCell {
  494. transition: background-color 2s;
  495. -webkit-transition: background-color 2s; /* Safari */
  496. background-color: green !important;
  497. color: white;
  498. }
  499. </style>