index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <template>
  2. <div class="qualityControl">
  3. <div v-if="!readonly && shiFouGuoShen" class="btn">
  4. <el-button type="primary" icon="ibps-icon-plus" @click="onAddClick">添加</el-button>
  5. <el-button type="danger" icon="ibps-icon-close" @click="onRemoveClick">删除</el-button>
  6. <el-button type="primary" icon="ibps-icon-import" @click="onImportClick">导入</el-button>
  7. <el-button type="primary" icon="ibps-icon-export" @click="onExportClick">导出</el-button>
  8. </div>
  9. <div class="table">
  10. <el-table :data="tableData" border @selection-change="handleSelectionChange">
  11. <el-table-column
  12. type="selection"
  13. />
  14. <el-table-column
  15. label="序号"
  16. width="50"
  17. type="index"
  18. />
  19. <el-table-column label="项目" prop="xiangMu" width="160" />
  20. <el-table-column label="质控批号" prop="zhiKongPinPiHao" width="100" />
  21. <el-table-column label="批号开始时间" prop="piHaoKaiShiShiJia" width="100" />
  22. <el-table-column label="质控图的界限">
  23. <el-table-column label="单位" prop="zhiKongTuDanWei" />
  24. <el-table-column label="水平" prop="zhiKongTuShuiPing" />
  25. <el-table-column label="均值" prop="zhiKongTuJunZhi" />
  26. <el-table-column label="SD" prop="zhiKongTuSd" />
  27. <el-table-column label="CV%" prop="zhiKongTuCv" />
  28. </el-table-column>
  29. <el-table-column label="原始测定数据统计">
  30. <el-table-column label="均值" prop="yuanShiJunZhi" />
  31. <el-table-column label="SD" prop="yuanShiSd" />
  32. <el-table-column label="CV%" prop="yuanShiCv" />
  33. <el-table-column label="N" prop="yuanShiN" />
  34. <el-table-column label="失控数" prop="shiKongShu" />
  35. </el-table-column>
  36. <el-table-column label="除失控数据后的数据统计">
  37. <el-table-column label="均值" prop="chuJunZhi" />
  38. <el-table-column label="SD" prop="chuSd" />
  39. <el-table-column label="CV%" prop="chuCv" />
  40. </el-table-column>
  41. <el-table-column label="累积数据统计">
  42. <el-table-column label="均值" prop="leiJunZhi" />
  43. <el-table-column label="SD" prop="leiSd" />
  44. <el-table-column label="CV%" prop="leiCv" />
  45. <el-table-column label="N" prop="leiN" />
  46. <el-table-column label="在控率%" prop="zaiKongLv" />
  47. </el-table-column>
  48. <el-table-column label="CV%控制范围">
  49. <el-table-column label="%" prop="cvKongZhiFanWei" />
  50. </el-table-column>
  51. <el-table-column v-if="!readonly && shiFouGuoShen" label="操作栏位" fixed="right">
  52. <template slot-scope="{row,$index}">
  53. <el-button type="text" icon="ibps-icon-edit" @click="onEditClick(row,$index)">编辑</el-button>
  54. </template>
  55. </el-table-column>
  56. </el-table>
  57. </div>
  58. <input id="" ref="file1" style="display:none" type="file" name="" accept=".xlsx,.xls" @change="handleUploadChange1">
  59. </div>
  60. </template>
  61. <script>
  62. import xlsx from 'xlsx'
  63. import fs from 'file-saver'
  64. import dayjs from 'dayjs'
  65. export default {
  66. props: {
  67. formData: {
  68. type: Object,
  69. default: () => {}
  70. },
  71. readonly: {
  72. type: Boolean,
  73. default: false
  74. }
  75. },
  76. data () {
  77. return {
  78. tableData: [],
  79. multipleSelection: [],
  80. columns: {
  81. xiangMu: '项目',
  82. zhiKongPinPiHao: '质控批号',
  83. piHaoKaiShiShiJia: '批号开始时间',
  84. zhiKongTuDanWei: '质控图的界限单位',
  85. zhiKongTuShuiPing: '质控图的界限水平',
  86. zhiKongTuJunZhi: '质控图的界限均值',
  87. zhiKongTuSd: '质控图的界限SD',
  88. zhiKongTuCv: '质控图的界限CV%',
  89. yuanShiJunZhi: '原始测定数据统计均值',
  90. yuanShiSd: '原始测定数据统计SD',
  91. yuanShiCv: '原始测定数据统计CV%',
  92. yuanShiN: '原始测定数据统计N',
  93. shiKongShu: '原始测定数据统计失控数',
  94. chuJunZhi: '除失控数据后的数据统计均值',
  95. chuSd: '除失控数据后的数据统计SD',
  96. chuCv: '除失控数据后的数据统计CV%',
  97. leiJunZhi: '累积数据统计均值',
  98. leiSd: '累积数据统计SD',
  99. leiCv: '累积数据统计CV%',
  100. leiN: '累积数据统计N',
  101. zaiKongLv: '累积数据统计在控率%',
  102. cvKongZhiFanWei: 'CV%控制范围'
  103. }
  104. }
  105. },
  106. computed: {
  107. shiFouGuoShen () {
  108. if (!this.formData.shiFouGuoShen || this.formData.shiFouGuoShen === '已退回' || this.formData.shiFouGuoShen === '已暂存') {
  109. return true
  110. }
  111. return false
  112. }
  113. },
  114. watch: {
  115. 'formData.dlxmsnzkyfxzb': {
  116. handler (val) {
  117. if (val && val.length) {
  118. // console.log(this.formData)
  119. this.tableData = val
  120. }
  121. },
  122. immediate: true
  123. },
  124. tableData: {
  125. handler (val) {
  126. this.$emit('change-data', 'dlxmsnzkyfxzb', val)
  127. },
  128. deep: true
  129. }
  130. },
  131. methods: {
  132. xlsx (json, fields, filename = '.xlsx') { // 导出xlsx
  133. json.forEach(item => {
  134. for (const i in item) {
  135. if (fields.hasOwnProperty(i)) {
  136. item[fields[i]] = item[i]
  137. }
  138. delete item[i] // 删除原先的对象属性
  139. }
  140. })
  141. const sheetName = filename // excel的文件名称
  142. const wb = xlsx.utils.book_new() // 工作簿对象包含一SheetNames数组,以及一个表对象映射表名称到表对象。XLSX.utils.book_new实用函数创建一个新的工作簿对象。
  143. const ws = xlsx.utils.json_to_sheet(json, { header: Object.values(fields) }) // 将JS对象数组转换为工作表。
  144. wb.SheetNames.push(sheetName)
  145. wb.Sheets[sheetName] = ws
  146. const defaultCellStyle = { font: { name: 'Verdana', sz: 13, color: 'FF00FF88' }, fill: { fgColor: { rgb: 'FFFFAA00' }}}// 设置表格的样式
  147. const wopts = { bookType: 'xlsx', bookSST: false, type: 'binary', cellStyles: true, defaultCellStyle: defaultCellStyle, showGridLines: false } // 写入的样式
  148. const wbout = xlsx.write(wb, wopts)
  149. const blob = new Blob([this.s2ab(wbout)], { type: 'application/octet-stream' })
  150. fs.saveAs(blob, filename + '.xlsx')
  151. },
  152. s2ab (s) {
  153. let buf
  154. if (typeof ArrayBuffer !== 'undefined') {
  155. buf = new ArrayBuffer(s.length)
  156. const view = new Uint8Array(buf)
  157. for (let i = 0; i !== s.length; ++i) view[i] = s.charCodeAt(i) & 0xff
  158. return buf
  159. } else {
  160. buf = new Array(s.length)
  161. for (let i = 0; i !== s.length; ++i) buf[i] = s.charCodeAt(i) & 0xFF
  162. return buf
  163. }
  164. },
  165. handleSelectionChange (val) {
  166. this.multipleSelection = val
  167. },
  168. onAddClick () {
  169. const btn = document.querySelector('.dynamic-form-table .ibps-icon-add')
  170. btn.click()
  171. },
  172. onImportClick () {
  173. // const btn = document.querySelector('.dynamic-form-table .ibps-icon-import')
  174. // btn.click()
  175. this.$refs.file1.click()
  176. console.log('导入')
  177. },
  178. /* 读取文件 将文件转换为二进制 */
  179. readFile (file) {
  180. return new Promise(resolve => {
  181. const reader = new FileReader()
  182. reader.readAsBinaryString(file)
  183. reader.onload = ev => {
  184. resolve(ev.target.result)
  185. }
  186. })
  187. },
  188. // 转换对象的key
  189. switchDeviceObj (data, originalObj) {
  190. const result = []
  191. data.forEach(item => {
  192. const obj = {}
  193. for (const key in originalObj) {
  194. // 对日期格式的数据做兼容处理
  195. if (item[originalObj[key]] instanceof Date) {
  196. obj[key] = dayjs(item[originalObj[key]]).add(8, 'hour').format('YYYY-MM-DD') || ''
  197. } else {
  198. obj[key] = String(item[originalObj[key]] || '')
  199. }
  200. }
  201. result.push(obj)
  202. })
  203. return result
  204. },
  205. async switchXmToId (list) {
  206. const sql = `select id_,jian_yan_xiang_mu from t_nlfwb`
  207. const { variables: { data = {}} = {}} = await this.$common.request('sql', sql)
  208. for (let i = 0; i < list.length; i++) {
  209. const row = list[i]
  210. const t = data.find(item => item.jian_yan_xiang_mu.trim() === row.xiangMu.trim())
  211. row.xuanZeXiangMu = t?.id_ || ''
  212. }
  213. },
  214. checkDate (data) {
  215. // 校验日期字段是否符合要求
  216. const dateRegex = /^(\d{4})[-/](0[1-9]|1[0-2])[-/](0[1-9]|[12]\d|3[01])$/
  217. for (let i = 0; i < data.length; i++) {
  218. const row = data[i]
  219. if (row.piHaoKaiShiShiJia && !dateRegex.test(row.piHaoKaiShiShiJia)) {
  220. throw new Error(`第${i + 1}行时间格式错误!`)
  221. }
  222. }
  223. },
  224. async handleUploadChange1 (file) {
  225. try {
  226. const dataBinary = await this.readFile(file.target.files[0])
  227. file.target.value = null // 注意上传后要将input的值设为空
  228. const workBook = xlsx.read(dataBinary, { type: 'binary', cellDates: true })
  229. const workSheet = workBook.Sheets[workBook.SheetNames[0]]
  230. const data = xlsx.utils.sheet_to_json(workSheet)
  231. const importData = this.switchDeviceObj(data, this.columns)
  232. // console.log(JSON.parse(JSON.stringify(importData)))
  233. this.checkDate(importData)
  234. await this.switchXmToId(importData)
  235. this.tableData.push(...importData)
  236. // console.log(JSON.parse(JSON.stringify(this.tableData)))
  237. } catch (error) {
  238. this.$message.warning(error?.message || '导入失败')
  239. }
  240. },
  241. getTimeStamp () {
  242. return dayjs().format('YYYYMMDDHHmmss')
  243. },
  244. onExportClick () {
  245. // const btn = document.querySelector('.dynamic-form-table .ibps-icon-export')
  246. // btn.click()
  247. const copyData = JSON.parse(JSON.stringify(this.tableData))
  248. this.xlsx(copyData, this.columns, '定量项目室内质控月分析表' + this.getTimeStamp())
  249. this.$message.success('导出成功!')
  250. },
  251. onRemoveClick () {
  252. if (this.multipleSelection.length === 0) {
  253. return this.$message.warning('请先选择需要删除的数据!')
  254. }
  255. this.$confirm('是否确认删除所选项?', '提示', {
  256. confirmButtonText: '确认',
  257. cancelButtonText: '取消',
  258. type: 'warning'
  259. }).then(() => {
  260. this.tableData = this.tableData.filter(item => !this.multipleSelection.includes(item))
  261. }).catch(() => {
  262. })
  263. },
  264. onEditClick (row, $index) {
  265. const btns = document.querySelectorAll('.dynamic-form-table .ibps-icon-edit')
  266. btns[Math.floor(btns.length / 2) + $index].click()
  267. }
  268. }
  269. }
  270. </script>
  271. <style lang="scss" scoped>
  272. .qualityControl{
  273. margin-top: 20px;
  274. .btn{
  275. padding: 0;
  276. display: flex;
  277. justify-content: flex-end;
  278. .el-button{
  279. margin: 0;
  280. }
  281. }
  282. }
  283. </style>