indexChuanBei.vue 12 KB

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