src/views/CNAS/resourceDemand/device/component/checkRecord.vue
@@ -1,219 +1,68 @@
<template>
  <div>
    <el-dialog :close-on-click-modal="false" :close-on-press-escape="false" v-model="dialogVisible"
      title="仪器设备期间核查比对记录表" width="80%" @close="resetForm">
      <el-steps :active="currentStep" align-center finish-status="success">
        <el-step title="填写设备核查对比记录"></el-step>
      :title="currentStep === 0 ? '新增核查记录' : '设备核查记录'" width="50%" @close="resetForm">
      <el-steps v-if="currentStep === 1" :active="1" align-center finish-status="success">
        <el-step title="填写核查记录"></el-step>
        <el-step title="审核"></el-step>
      </el-steps>
      <el-row>
        <el-col :span="8">
          <div class="form-item">
            <span style="width: 110px">仪器名称:</span>
            <el-select v-model="form.deviceName" :disabled="currentStep !== 0" class="table_input" clearable filterable
              placeholder="设备名称" size="small" style="width:200px" @change="(val) => changeMachineName(val)">
              <el-option v-for="item in equipOptions" :key="item.value" :label="item.label" :value="item.label">
                {{ item.label + '-' + item.value }}
              </el-option>
            </el-select>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="form-item">
            <span style="width: 130px">仪器编号:</span>
            <el-input v-model="form.deviceNumber" :disabled="currentStep !== 0" clearable size="small"></el-input>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="form-item">
            <span style="width: 130px">精度等级:</span>
            <el-input v-model="form.accuracyGrade" :disabled="currentStep !== 0" clearable size="small"></el-input>
          </div>
        </el-col>
      </el-row>
      <div style="margin: 10px">核查使用物质</div>
      <el-row>
        <el-col :span="8">
          <div class="form-item">
            <span style="width: 130px">名称:</span>
            <el-input v-model="form.materialName" :disabled="currentStep !== 0" clearable size="small"></el-input>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="form-item">
            <span style="width: 130px">编号:</span>
            <el-input v-model="form.materialNumber" :disabled="currentStep !== 0" clearable size="small"></el-input>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="form-item">
            <span style="width: 130px">精度/不确定度:</span>
            <el-input v-model="form.materialAccuracyUncertainty" :disabled="currentStep !== 0" clearable
              size="small"></el-input>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="form-item">
            <span style="width: 130px">型号:</span>
            <el-input v-model="form.materialModel" :disabled="currentStep !== 0" clearable size="small"></el-input>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="form-item">
            <span style="width: 130px">核查方式:</span>
            <el-input v-model="form.materialCheckMethod" :disabled="currentStep !== 0" clearable
              size="small"></el-input>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="form-item">
            <span style="width: 130px">核查项目:</span>
            <el-input v-model="form.materialCheckItems" :disabled="currentStep !== 0" clearable size="small"></el-input>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="form-item">
            <span style="width: 130px">温度:</span>
            <el-input v-model="form.temperature" :disabled="currentStep !== 0" clearable size="small"></el-input>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="form-item">
            <span style="width: 130px">湿度:</span>
            <el-input v-model="form.humidity" :disabled="currentStep !== 0" clearable size="small"></el-input>
          </div>
        </el-col>
      </el-row>
      <el-button size="small" style="margin: 10px 0" type="primary" @click="addRow">添加</el-button>
      <el-table :data="recordDetailList" height="300" style="width: 100%" border>
        <el-table-column label="测试点" min-width="130" prop="testPoint">
          <template #default="scope">
            <el-input v-model="scope.row.testPoint" clearable size="small"></el-input>
          </template>
        </el-table-column>
        <el-table-column min-width="180">
          <template #header="scope">
            <el-input v-model="form.dataValue1" :disabled="currentStep !== 0" size="small" />
          </template>
          <template #default="scope">
            <el-input v-model="scope.row.dataValue1" :disabled="currentStep !== 0" clearable size="small"></el-input>
          </template>
        </el-table-column>
        <el-table-column min-width="180">
          <template #header="scope">
            <el-input v-model="form.dataValue2" :disabled="currentStep !== 0" size="small" />
          </template>
          <template #default="scope">
            <el-input v-model="scope.row.dataValue2" :disabled="currentStep !== 0" clearable size="small"></el-input>
          </template>
        </el-table-column>
        <el-table-column min-width="180">
          <template #header="scope">
            <el-input v-model="form.dataValue3" :disabled="currentStep !== 0" size="small" />
          </template>
          <template #default="scope">
            <el-input v-model="scope.row.dataValue3" :disabled="currentStep !== 0" clearable size="small"></el-input>
          </template>
        </el-table-column>
        <el-table-column min-width="180">
          <template #header="scope">
            <el-input v-model="form.dataValue4" :disabled="currentStep !== 0" size="small" />
          </template>
          <template #default="scope">
            <el-input v-model="scope.row.dataValue4" :disabled="currentStep !== 0" clearable size="small"></el-input>
          </template>
        </el-table-column>
        <el-table-column min-width="180">
          <template #header="scope">
            <el-input v-model="form.dataValue5" :disabled="currentStep !== 0" size="small" />
          </template>
          <template #default="scope">
            <el-input v-model="scope.row.dataValue5" :disabled="currentStep !== 0" clearable size="small"></el-input>
          </template>
        </el-table-column>
        <el-table-column min-width="180">
          <template #header="scope">
            <el-input v-model="form.dataValue6" :disabled="currentStep !== 0" size="small" />
          </template>
          <template #default="scope">
            <el-input v-model="scope.row.dataValue6" :disabled="currentStep !== 0" clearable size="small"></el-input>
          </template>
        </el-table-column>
      </el-table>
      <table border="1" cellspacing="10" class="table">
        <tr>
          <td>最大偏差</td>
          <th>
            <el-input v-model="form.maximun1" :disabled="currentStep !== 0" clearable size="small"></el-input>
          </th>
          <th>
            <el-input v-model="form.maximun2" :disabled="currentStep !== 0" clearable size="small"></el-input>
          </th>
          <th>
            <el-input v-model="form.maximun3" :disabled="currentStep !== 0" clearable size="small"></el-input>
          </th>
          <th>
            <el-input v-model="form.maximun4" :disabled="currentStep !== 0" clearable size="small"></el-input>
          </th>
          <th>
            <el-input v-model="form.maximun5" :disabled="currentStep !== 0" clearable size="small"></el-input>
          </th>
          <th>
            <el-input v-model="form.maximun6" :disabled="currentStep !== 0" clearable size="small"></el-input>
          </th>
        </tr>
        <tr>
          <td>相对偏差</td>
          <th>
            <el-input v-model="form.relative1" :disabled="currentStep !== 0" clearable size="small"></el-input>
          </th>
          <th>
            <el-input v-model="form.relative2" :disabled="currentStep !== 0" clearable size="small"></el-input>
          </th>
          <th>
            <el-input v-model="form.relative3" :disabled="currentStep !== 0" clearable size="small"></el-input>
          </th>
          <th>
            <el-input v-model="form.relative4" :disabled="currentStep !== 0" clearable size="small"></el-input>
          </th>
          <th>
            <el-input v-model="form.relative5" :disabled="currentStep !== 0" clearable size="small"></el-input>
          </th>
          <th>
            <el-input v-model="form.relative6" :disabled="currentStep !== 0" clearable size="small"></el-input>
          </th>
        </tr>
      </table>
      <el-row>
        <el-col :span="24">
          <div class="form-item">
            <span style="width: 80px">判定:</span>
            <el-input v-model="form.determine" :disabled="currentStep !== 0" clearable size="small"></el-input>
          </div>
        </el-col>
        <el-col :span="24">
          <div class="form-item">
            <span style="width: 80px">备注:</span>
            <el-input v-model="form.remark" :disabled="currentStep !== 0" clearable size="small"
              type="textarea"></el-input>
          </div>
        </el-col>
        <el-col :span="24">
          <div class="form-item">
            <span style="width: 116px">审核人:</span>
            <el-select v-model="form.reviewUserId" :disabled="currentStep !== 0" placeholder="请选择" size="small"
              style="width: 100%">
              <el-option v-for="item in userList" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </div>
        </el-col>
      </el-row>
      <template #footer><span  class="dialog-footer">
      <el-form ref="recordForm" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="设备编号:" prop="deviceNumber">
          <el-input v-model="form.deviceNumber" disabled size="small"></el-input>
        </el-form-item>
        <el-form-item label="设备名称:" prop="deviceName">
          <el-select v-if="currentStep === 0" v-model="selectedDeviceId" clearable filterable
            placeholder="请选择设备" size="small" style="width: 100%" @change="changeMachineName">
            <el-option v-for="item in equipOptions" :key="item.id" :label="item.label + '-' + item.value"
              :value="item.id"></el-option>
          </el-select>
          <el-input v-else v-model="form.deviceName" disabled size="small"></el-input>
        </el-form-item>
        <el-form-item label="核查时间:" prop="checkTime">
          <el-date-picker v-model="form.checkTime" :disabled="currentStep !== 0" clearable format="YYYY-MM"
            placeholder="选择日期" size="small" style="width: 100%" type="month" value-format="YYYY-MM">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="核查指标:" prop="checkIndex">
          <el-input v-model="form.checkIndex" :disabled="currentStep !== 0" clearable size="small"></el-input>
        </el-form-item>
        <el-form-item label="核查方法:" prop="checkMethod">
          <el-input v-model="form.checkMethod" :disabled="currentStep !== 0" clearable size="small"
            type="textarea"></el-input>
        </el-form-item>
        <el-form-item label="结果如何判定:" prop="howResults">
          <el-input v-model="form.howResults" :disabled="currentStep !== 0" clearable size="small"
            type="textarea"></el-input>
        </el-form-item>
        <el-form-item label="核查责任人:" prop="checkChargerUserId">
          <el-select v-if="currentStep === 0" v-model="form.checkChargerUserId" clearable filterable
            placeholder="请选择" size="small" style="width: 100%" @change="setCheckChargerUser">
            <el-option v-for="item in chargerOptions" :key="item.id" :label="item.name"
              :value="item.id"></el-option>
          </el-select>
          <el-input v-else v-model="form.checkChargerUser" disabled size="small"></el-input>
        </el-form-item>
        <el-form-item label="备注:" prop="remark">
          <el-input v-model="form.remark" :disabled="currentStep !== 0" clearable size="small"></el-input>
        </el-form-item>
        <el-form-item label="附件:">
          <el-upload v-if="currentStep === 0" :action="javaApi + '/deviceScope/uploadFile'" :headers="uploadHeader"
            :limit="1" :on-exceed="handleUploadExceed" :before-upload="beforeAttachmentUpload"
            :on-success="handleAttachmentSuccess" :on-remove="handleAttachmentRemove">
            <el-button size="small" type="primary">选择附件</el-button>
          </el-upload>
          <el-link v-if="form.fileUrl" type="primary" @click="openAttachment">
            {{ form.fileName || '查看附件' }}
          </el-link>
          <span v-else-if="currentStep !== 0">—</span>
        </el-form-item>
      </el-form>
      <template #footer><span class="dialog-footer">
        <el-button @click="resetForm">取 消</el-button>
        <el-button v-if="currentStep === 0 && checkChargerUserId == userId" :loading="submitFormLoading" type="primary" @click="submitCheck">确
          定</el-button>
        <el-button v-if="currentStep === 1 && userId == reviewUserId" :loading="submitFormLoading" @click="examine(0)">不通过</el-button>
        <el-button v-if="currentStep === 1 && userId == reviewUserId" :loading="submitFormLoading" type="primary" @click="examine(1)">通
        <el-button v-if="currentStep === 0" :loading="submitFormLoading" type="primary" @click="submitCheck">确
          认</el-button>
        <el-button v-if="currentStep === 1" :loading="submitFormLoading" @click="examine(0)">不通过</el-button>
        <el-button v-if="currentStep === 1" :loading="submitFormLoading" type="primary" @click="examine(1)">通
          过</el-button>
      </span></template>
    </el-dialog>
@@ -222,140 +71,185 @@
<script>
import {
  getExamineRecord,
  addExamineRecord,
  reviewExamineRecord,
  deviceScopeSearch,
  resolveExaminePlanDetail,
  updateDeviceExaminePlanDetail,
} from '@/api/cnas/resourceDemand/device.js'
import { selectUserCondition } from "@/api/performance/class";
import {mapGetters} from "@/utils/store-compat";
import draftSave from '@/mixins/draftSave'
const createEmptyForm = () => ({
  deviceId: '',
  deviceName: '',
  deviceNumber: '',
  checkTime: '',
  checkIndex: '',
  checkMethod: '',
  howResults: '',
  checkChargerUserId: '',
  checkChargerUser: '',
  remark: '',
  fileName: '',
  fileUrl: '',
  planDetailsId: '',
})
export default {
  name: "check-record",
  // import 引入的组件需要注入到对象中才能使用
  components: {},
  mixins: [draftSave],
  data() {
    // 这里存放数据
    return {
      dialogVisible: false,
      recordDetailList: [],
      search: "",
      form: {
        deviceName: '',
        deviceNumber: '',
        accuracyGrade: '',
        materialName: '',
        materialNumber: '',
        materialAccuracyUncertainty: '',
        materialModel: '',
        materialCheckMethod: '',
        materialCheckItems: '',
        temperature: '',
        humidity: '',
        recordDetailList: [],
        dataValue1: '',
        dataValue2: '',
        dataValue3: '',
        dataValue4: '',
        dataValue5: '',
        dataValue6: '',
        maximun1: '',
        maximun2: '',
        maximun3: '',
        maximun4: '',
        maximun5: '',
        maximun6: '',
        relative1: '',
        relative2: '',
        relative3: '',
        relative4: '',
        relative5: '',
        relative6: '',
        determine: '',
        remark: '',
        planDetailsId: '',
        reviewUserId: '',
      },
      form: createEmptyForm(),
      currentStep: 0,
      distributionDetailList: [
        { label: '最大偏差', value: 1 },
        { label: '相对偏差', value: 2 },
      ],
      equipOptions: [],
      selectedDeviceId: '',
      submitFormLoading: false,
      userList: [],
      reviewUserId: '',
      checkChargerUserId: '',
      rules: {
        checkMethod: [{ required: true, message: '请填写核查方法', trigger: 'blur' }],
      },
    }
  },
  mounted() {
  },
  computed: {
    ...mapGetters(["userId"]),
  },
  // 方法集合
  methods: {
    // 审核已提交的核查记录
    openDialog(row) {
      this.dialogVisible = true
      this.form.planDetailsId = row.planDetailsId
      this.checkChargerUserId = row.checkChargerUserId
      this.currentStep = 1
      this.selectedDeviceId = row.deviceId || ''
      this.form = {
        deviceId: row.deviceId || '',
        deviceName: row.deviceName || '',
        deviceNumber: row.deviceNumber || '',
        checkTime: (row.checkTime || '').substring(0, 7),
        checkIndex: row.checkIndex || '',
        checkMethod: row.checkMethod || '',
        howResults: row.howResults || '',
        checkChargerUserId: row.checkChargerUserId || '',
        checkChargerUser: row.checkChargerUser || '',
        remark: row.remark || '',
        fileName: row.fileName || '',
        fileUrl: row.fileUrl || '',
        planDetailsId: row.planDetailsId,
      }
    },
    // 新增核查记录:自选设备,提交时自动匹配/创建本轮计划明细
    openAddDialog() {
      this.dialogVisible = true
      this.currentStep = 0
      this.form = createEmptyForm()
      this.selectedDeviceId = ''
      const now = new Date()
      this.form.checkTime = now.getFullYear() + '-' + String(now.getMonth() + 1).padStart(2, '0')
      this.getEquipOptions()
      this.getUserList()
      this.searchInfo()
    },
    searchInfo() {
      getExamineRecord({ planDetailsId: this.form.planDetailsId }).then(res => {
        if (res.code === 200) {
          let planDetailsId = this.form.planDetailsId
          this.form = { ...res.data }
          this.form.planDetailsId = planDetailsId
          this.recordDetailList = this.form.recordDetailList || []
          if (!this.form.reviewUserId) {
            this.currentStep = 0
          } else {
            this.currentStep = 1
          }
          if (this.form.reviewStatus === 1) {
            this.currentStep = 2
          }
          this.reviewUserId = this.form.reviewUserId
      this.$nextTick(() => {
        if (this.$refs.recordForm) {
          this.$refs.recordForm.clearValidate()
        }
      }).catch(error => {
        console.error(error)
        this.draftRestore('checkRecordForm', 'form', 'new', 'dialogVisible')
        if (this.form.deviceId) {
          this.selectedDeviceId = this.form.deviceId
        }
      })
    },
    // 赋值仪器编号
    // 按设备 id 回填名称和编号,避免同名设备选错
    changeMachineName(value) {
      const index = this.equipOptions.findIndex(item => item.deviceName === value)
      this.form.deviceNumber = this.equipOptions[index].managementNumber
      const item = this.equipOptions.find(device => device.id === value)
      if (item) {
        this.form.deviceId = item.id
        this.form.deviceName = item.deviceName
        this.form.deviceNumber = item.managementNumber
      } else {
        this.form.deviceId = ''
        this.form.deviceName = ''
        this.form.deviceNumber = ''
      }
    },
    addRow() {
      this.recordDetailList.push({ testPoint: '' })
    setCheckChargerUser(value) {
      const item = this.userList.find(user => user.id === value)
      this.form.checkChargerUser = item ? item.name : ''
    },
    // 提交表单
    // 提交核查记录
    submitCheck() {
      console.log(333, this.form)
      this.form.recordDetailList = this.HaveJson(this.recordDetailList)
      this.submitFormLoading = true
      addExamineRecord(this.form).then(res => {
        if (res.code == 200) {
          this.$message.success('新增成功')
          this.resetForm()
      if (!this.selectedDeviceId) {
        this.$message.error('请选择设备')
        return
      }
      if (!this.form.checkTime) {
        this.$message.error('请选择核查时间')
        return
      }
      this.$refs.recordForm.validate(valid => {
        if (!valid) {
          return
        }
        this.submitFormLoading = false
      }).catch(err => {
        this.submitFormLoading = true
        resolveExaminePlanDetail({ deviceId: this.selectedDeviceId, checkTime: this.form.checkTime }).then(res => {
          if (res.code !== 200) {
            this.$message.error(res.msg || '获取核查计划失败')
            this.submitFormLoading = false
            return
          }
          this.saveRecord(res.data.planDetailsId)
        }).catch(() => {
          this.submitFormLoading = false
        })
      })
    },
    saveRecord(planDetailsId) {
      this.form.planDetailsId = planDetailsId
      const params = {
        planDetailsId,
        deviceId: this.form.deviceId,
        deviceName: this.form.deviceName,
        deviceNumber: this.form.deviceNumber,
        checkTime: this.form.checkTime,
      }
      if (this.form.checkIndex) params.checkIndex = this.form.checkIndex
      if (this.form.checkMethod) params.checkMethod = this.form.checkMethod
      if (this.form.howResults) params.howResults = this.form.howResults
      if (this.form.checkChargerUserId) params.checkChargerUserId = this.form.checkChargerUserId
      if (this.form.checkChargerUser) params.checkChargerUser = this.form.checkChargerUser
      if (this.form.remark) params.remark = this.form.remark
      if (this.form.fileName) params.fileName = this.form.fileName
      if (this.form.fileUrl) params.fileUrl = this.form.fileUrl
      updateDeviceExaminePlanDetail(params).then(res => {
        if (res.code !== 200) {
          this.$message.error(res.msg || '保存核查记录失败')
          this.submitFormLoading = false
          return
        }
        addExamineRecord({ planDetailsId }).then(recordRes => {
          if (recordRes.code == 200) {
            this.$message.success('核查记录已提交,等待审核')
            this.draftClear('checkRecordForm')
            this.resetForm()
          } else {
            this.$message.error(recordRes.msg || '提交核查记录失败')
          }
          this.submitFormLoading = false
        }).catch(() => {
          this.submitFormLoading = false
        })
      }).catch(() => {
        this.submitFormLoading = false
      })
    },
    // 提交审核
    examine(statue) {
      this.submitFormLoading = true
      const params = {
        planDetailsId: this.form.planDetailsId,
        reviewStatus: statue,
      }
      reviewExamineRecord(params).then(res => {
        if (res.code == 200) {
          this.$message.success('审核成功')
          this.$message.success(statue === 1 ? '审核通过' : '已退回')
          this.draftClear('checkRecordForm')
          this.resetForm()
        }
        this.submitFormLoading = false
@@ -364,6 +258,7 @@
      })
    },
    resetForm() {
      this.draftStop('checkRecordForm')
      this.dialogVisible = false
      this.$emit('closeDialog1')
    },
@@ -384,37 +279,52 @@
      })
    },
    getUserList() {
      selectUserCondition().then(res => {
      selectUserCondition({ type: 4 }).then(res => {
        this.userList = res.data
      })
    },
    // 附件
    beforeAttachmentUpload(file) {
      if (file.size > 1024 * 1024 * 10) {
        this.$message.error('上传文件不超过10M')
        return false
      }
      return true
    },
    handleUploadExceed() {
      this.$message.warning('仅支持一个附件,请先移除已上传的附件')
    },
    handleAttachmentSuccess(response) {
      if (response.code == 200) {
        this.form.fileName = response.data.name
        this.form.fileUrl = response.data.url
        this.$message.success('附件上传成功')
      } else {
        this.$message.error(response.msg || '附件上传失败')
      }
    },
    handleAttachmentRemove() {
      this.form.fileName = ''
      this.form.fileUrl = ''
    },
    openAttachment() {
      if (!this.form.fileUrl) {
        return
      }
      const isImage = /\.(jpg|jpeg|png|gif)$/i.test(this.form.fileUrl)
      window.open(this.javaApi + (isImage ? '/img/' : '/word/') + this.form.fileUrl, '_blank')
    },
  },
  computed: {
    // 责任人下拉:已保存的责任人不在候选列表时补一条,避免回显成用户ID
    chargerOptions() {
      const list = this.userList || []
      const id = this.form.checkChargerUserId
      if (id && !list.some(item => String(item.id) === String(id))) {
        return [...list, { id, name: this.form.checkChargerUser || String(id) }]
      }
      return list
    },
  },
}
</script>
<style scoped>
.table td {
  padding: 10px;
}
.table th {
  padding: 10px;
}
.form {
  display: flex;
  align-items: center;
}
.form-item {
  display: flex;
  align-items: center;
  margin-right: 10px;
  margin-top: 10px;
}
:deep(.el-dialog__body) {
  max-height: 650px;
  overflow-y: auto;
}
</style>