Newer
Older
xc-business-system / src / views / equipement / standard / checkData / detail.vue
dutingting on 14 Nov 2023 22 KB 核查数据管理重构代码
<!-- 核查数据管理详情 -->
<script lang="ts" setup name="checkDataDetail">
import { ref } from 'vue'
import { ElLoading, ElMessage } from 'element-plus'
import dayjs from 'dayjs'
import type { IDetailCheckData, IEquipmentList, IForm } from './checkData-interface'
import selectStandardEquipmentDialog from './dialog/selectStandardEquipmentDialog.vue'
import saveCheckRecord from './dialog/saveCheckRecord.vue'
import checkDataFirst from './components/first/checkDataCom.vue'
import type { TableColumn } from '@/components/NormalTable/table_interface'
import useUserStore from '@/store/modules/user'
import { getDictByCode } from '@/api/system/dict'
import type { dictType } from '@/global'
import multiTable from '@/components/MultiHeaderTable/index.vue'
import { getCheckItemDetail, getJobInstructionList } from '@/api/equipment/standard/book'
import SelectEquipmentDialog from '@/views/business/fieldTest/approve/dialog/selectEquipmentDialog.vue'
import { addCheckData, calculateHandle, getInfo, updateCheckData } from '@/api/equipment/standard/checkData'
const user = useUserStore() // 用户信息
const textMap: { [key: string]: string } = {
  edit: '编辑',
  add: '新建',
  detail: '详情',
}// 字典
const $router = useRouter() // 关闭页面使用
const $route = useRoute() // 路由参数
const pageType = ref('add') // 页面类型: add, edit, detail
const infoId = ref('') // 列表id
const ruleFormRef = ref() // 表单ref
const form = ref<IForm>({
  dataNo: '', // 核查数据编号
  checkDate: '', // 核查日期
  checkAddress: '', // 核查地点
  temperature: 20, // 环境温度
  humidity: 20, // 环境湿度
  checkAccord: '', // 核查依据(即标准装置的作业指导书文件minio文件名,多个分号分割)
  createUserName: '', // 核查员
  stabilityExamine: '', // 是否用于稳定性考核(1/0)
})
// 校验规则
const formRules = ref({
  checkDate: [{ required: true, message: '核查日期不能为空', trigger: ['blur', 'change'] }],
  checkAddress: [{ required: true, message: '核查地点不能为空', trigger: ['blur', 'change'] }],
  temperature: [{ required: true, message: '温度不能为空', trigger: ['blur', 'change'] },
    { pattern: /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/, message: '温度只能为数字', trigger: ['blur', 'change'] }],
  humidity: [{ required: true, message: '相对湿度不能为空', trigger: ['blur', 'change'] },
    { pattern: /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/, message: '相对湿度只能为数字', trigger: ['blur', 'change'] }],
  createUserName: [{ required: true, message: '核查员不能为空', trigger: ['blur', 'change'] }],
  stabilityExamine: [{ required: true, message: '是否用于稳定性考核不能为空', trigger: ['blur', 'change'] }],
})

const saveCheckRecordRef = ref() // 保存核查记录dialog组件ref
const checkDataFirstRef = ref() // 第一套多功能校准源标准装置组件ref
// -------------------------------------------字典------------------------------------------
const deviceTypeList = ref<dictType[]>([])// 设备分类
const standardList = ref<dictType[]>([])// 检校标准装置
const positionList = ref<dictType[]>([]) // 核查地点
const stabilityExamineList = [ // 是否用于稳定性考核
  {
    id: '1',
    name: '是',
  },
  {
    id: '2',
    name: '否',
  },
]
// 核查地点
positionList.value = [
  {
    id: '1',
    name: '地点1',
    value: '1',
  },
  {
    id: '2',
    name: '地点2',
    value: '2',
  },
]
function getDict() {
  // 设备分类
  getDictByCode('bizEquipmentCategory').then((response) => {
    deviceTypeList.value = response.data
  })
  // 检校标准装置
  getDictByCode('bizStandardEquipmentType').then((response) => {
    standardList.value = response.data
  })
}
// ----------------------------------路由参数------------------------------------------------
if ($route.params && $route.params.type) {
  pageType.value = $route.params.type as string
  console.log(pageType.value)

  if ($route.params.id) {
    infoId.value = $route.params.id as string
  }
}

// --------------------------------核查标准设备-----------------------------------------------
const equipmentColumns = [ // 表头
  { text: '设备名称', value: 'equipmentName', align: 'center', width: '240' },
  { text: '型号规格', value: 'model', align: 'center' },
  { text: '出厂编号', value: 'manufactureNo', align: 'center' },
  // { text: '准确度等级', value: 'accuracyLevel', align: 'center' },
  // { text: '测量范围', value: 'measureRange', align: 'center' },
  { text: '制造厂家', value: 'manufacturer', align: 'center' },
  { text: '检定有效期', value: 'measureValidDate', align: 'center', width: '120' },
  { text: '备注', value: 'remark', align: 'center' },
]
const equipmentList = ref<IEquipmentList[]>([])// 表格数据
const checkoutEquipmentList = ref([]) as any // 选中数据
const selectEquipmentDialogRef = ref() // 选择设备组件ref
// 选中
const handleSelectionEquipmentChange = (e: any) => {
  checkoutEquipmentList.value = e
}
// 批量添加
const multiAddEquipment = () => {
  selectEquipmentDialogRef.value.initDialog()
}

// 确定选择设备
const confirmSelectEquipment = (list = []) => {
  list.forEach((item: IEquipmentList) => {
    // 只添加列表里不存在的
    const index = equipmentList.value.findIndex((i: { id: string }) => item.id === i.id)
    if (index === -1) {
      item.measureValidDate = item.measureValidDate ? dayjs(item.measureValidDate).format('YYYY-MM-DD') : item.measureValidDate
      equipmentList.value.push(item)
    }
  })
}
// 删除
const delEquipmentRow = () => {
  if (!checkoutEquipmentList.value.length) {
    ElMessage.warning('请选中要删除的行')
  }
  else {
    equipmentList.value = equipmentList.value.filter((item: any) => {
      return !checkoutEquipmentList.value.includes(item)
    })
  }
}
// ------------------------------------------被核查设备---------------------------------------
const selectStandardId = ref('') // 被核查设备选择的标准装置id
const selectStandardName = ref('') // 被核查设备选择的标准装置名称
const belongStandardEquipment = ref('') // 检校标准装置code
const itemCategoryId = ref('') // 核查项分类id
const itemCategoryName = ref('') // 核查项分类名称
itemCategoryId
const chekedEquipmentList = ref<IEquipmentList[]>([{
  id: '',
  equipmentNo: '', // 设备编号
  equipmentName: '', // 设备名称
  model: '', // 型号规格
  manufactureNo: '', // 出厂编号
  accuracyLevel: '', // 准确度等级
  measureRange: '', // 测量范围
  manufacturer: '', // 制造厂(生产厂家)
  measureValidDate: '', // 检定有效期
  remark: '', // 备注
}])// 表格数据
const selectStandardEquipmentDialogRef = ref() // 选择标准装置组件ref

// 点击选择被核查设备
const selectStandardEquipment = () => {
  selectStandardEquipmentDialogRef.value.initDialog()
}

// 选好配套设备
const confirmSelectedStandardEquipmentDialog = (val: any, standardId: string, standardName: string, belongStandardEquipmentParams: string) => {
  selectStandardId.value = standardId // 被核查设备选择的标准装置id
  selectStandardName.value = standardName // 被核查设备选择的标准装置name
  belongStandardEquipment.value = belongStandardEquipmentParams // 检校标准装置code
  itemCategoryId.value = val[0].itemCategoryId // 核查项分类id
  itemCategoryName.value = val[0].itemCategoryName // 核查项分类名称
  let measureRange = '' // 测量范围
  let accuracyLevel = '' // 准确度等级
  if (val[0].technicalTargetList.length) {
    val[0].technicalTargetList.forEach((i: any, index: number) => {
      if (index === val[0].technicalTargetList.length - 1) {
        measureRange = `${measureRange}${i.measureRange} `
        accuracyLevel = `${accuracyLevel}${i.uncertainty} `
      }
      else {
        measureRange = `${measureRange}${i.measureRange}; `
        accuracyLevel = `${accuracyLevel}${i.uncertainty}; `
      }
    })
  }

  chekedEquipmentList.value = [{
    id: val[0].id,
    equipmentId: val[0].id,
    equipmentNo: val[0].equipmentNo, // 设备编号
    equipmentName: val[0].equipmentName, // 设备名称
    model: val[0].model, // 型号规格
    manufactureNo: val[0].manufactureNo, // 出厂编号
    accuracyLevel, // 准确度等级
    measureRange, // 测量范围
    manufacturer: val[0].manufacturer, // 制造厂(生产厂家)
    measureValidDate: val[0].measureValidDate, // 检定有效期
    remark: val[0].remark, // 备注
    itemCategoryId: val[0].itemCategoryId, // 核查项分类id
    itemCategoryName: val[0].itemCategoryName, // 核查项分类名称
  }]
  checkDataFirstRef.value.fetchCheckItemDetail(val[0].id, belongStandardEquipment.value, itemCategoryId.value, itemCategoryName.value) // 获取核查数据
}

// -------------------------------------------表单--请求核查依据(作业指导书)----------------------------------------
const technologyFile = ref([]) // 核查依据
// 查询条件(作业指导书列表)
const listQueryJobInstruction = ref({
  id: '',
  offset: 1,
  limit: 999999,
})
// // 请求作业指导书列表
function fetchJobInstruction(isNowPage = false, StandardId = '') {
  if (!isNowPage) {
    // 是否显示当前页,否则跳转第一页
    listQueryJobInstruction.value.offset = 1
  }
  listQueryJobInstruction.value.id = StandardId // 标准装置id
  getJobInstructionList(listQueryJobInstruction.value).then((response) => {
    technologyFile.value = response.data.rows.map((item: { file: string }) => item.file)
  })
}

// ------------------------------------------------------------------------------------------
// 关闭新增页面的回调
const close = () => {
  $router.back()
}

// 处理保存数据
const solveSaveParams = () => {
  let params
  if (belongStandardEquipment.value === '1') {
    const tempCheckDataCalibratorList = checkDataFirstRef.value.bestList.concat(checkDataFirstRef.value.bestListStability, checkDataFirstRef.value.worstList, checkDataFirstRef.value.worstListStability, checkDataFirstRef.value.modelList, checkDataFirstRef.value.modelListStability)
    params = {
      ...form.value,
      ...checkDataFirstRef.value.form,
      checkAccord: technologyFile.value.join(','), // 核查依据
      checkDataCalibratorList: tempCheckDataCalibratorList, // 核查数据
      checkEquipmentIdList: equipmentList.value.map(item => item.id), // 核查标准设备
      equipmentId: chekedEquipmentList.value[0].equipmentId, // 被核查标准设备id
      createUserId: user.id, //	核查员id
      createUserName: user.name, //	核查员
      // temperature: Number(form.value.temperature), // 环境温度
      // humidity: Number(form.value.humidity), // 环境湿度
      itemCategoryId: chekedEquipmentList.value[0].itemCategoryId, // 核查项分类id
      standardEquipmentId: selectStandardId.value, // 被核查标准装置id
      id: infoId.value,
      belongStandardEquipment: belongStandardEquipment.value, // 检校标准装置
    }
  }
  return params
}

// 保存
const save = () => {
  if (!equipmentList.value.length) {
    ElMessage.warning('核查标准设备不能为空')
    return false
  }
  if (!chekedEquipmentList.value[0].equipmentNo) {
    ElMessage.warning('被核查设备不能为空')
    return false
  }
  ruleFormRef.value!.validate((valid: boolean) => {
    if (valid) {
      const loading = ElLoading.service({
        lock: true,
        background: 'rgba(255, 255, 255, 0.8)',
      })
      const params = solveSaveParams()// 处理保存入参
      if (pageType.value === 'add') { // 新建
        addCheckData(params).then((res) => {
          ElMessage.success('保存成功')
          // form.value.dataNo = res.data.dataNo // 核查数据编号
          infoId.value = res.data
          pageType.value = 'detail'
          fetchInfo()
          loading.close()
        }).catch(() => {
          loading.close()
        })
      }
      // 保存
      else if (pageType.value === 'edit') { // 编辑
        updateCheckData(params).then((res) => {
          ElMessage.success('保存成功')
          pageType.value = 'detail'
          loading.close()
        }).catch(() => {
          loading.close()
        })
      }
    }
    else {
      console.log('表单校验不通过')
    }
  })
}
// 点击编辑
const edit = () => {
  pageType.value = 'edit'
}

// 获取详情
function fetchInfo() {
  const loading = ElLoading.service({
    lock: true,
    background: 'rgba(255, 255, 255, 0.8)',
  })
  getInfo({ id: infoId.value }).then((res) => {
    form.value = { ...res.data }
    // =========================================================
    chekedEquipmentList.value = [{ // 被检设备
      id: '',
      equipmentId: res.data.equipmentInfo.id, // 设备id
      equipmentNo: res.data.equipmentInfo.equipmentNo, // 设备编号
      equipmentName: res.data.equipmentInfo.equipmentName, // 设备名称
      model: res.data.equipmentInfo.model, // 型号规格
      manufactureNo: res.data.equipmentInfo.manufactureNo, // 出厂编号
      accuracyLevel: res.data.accuracyLevel, // 准确度等级
      measureRange: res.data.measureRange, // 测量范围
      manufacturer: res.data.equipmentInfo.manufacturer, // 制造厂(生产厂家)
      measureValidDate: res.data.equipmentInfo.measureValidDate, // 证书有效期
      remark: res.data.equipmentInfo.remark, // 备注
      itemCategoryId: res.data.itemCategoryId, // 核查项分类id
      itemCategoryName: res.data.itemCategoryName, // 核查项分类名称
    }]
    technologyFile.value = res.data.checkAccord ? res.data.checkAccord.split(',') : '无' // 核查依据
    equipmentList.value = res.data.checkEquipmentList // 核查标准设备
    selectStandardId.value = res.data.standardEquipmentId // 被核查标准装置id
    loading.close()
  })
}

// 点击生成核查记录
const createCheckRecord = () => {
  const params = {
    ...form.value,
    checkAccord: technologyFile.value.join(','), // 核查依据
    checkDataCalibratorList: checkDataFirstRef.value.bestList.concat(checkDataFirstRef.value.bestListStability, checkDataFirstRef.value.worstList, checkDataFirstRef.value.worstListStability, checkDataFirstRef.value.modelList, checkDataFirstRef.value.modelListStability),
    checkEquipmentIdList: equipmentList.value.map(item => item.id), // 核查标准设备
    equipmentId: chekedEquipmentList.value[0].equipmentId, // 被核查标准设备id
    createUserId: user.id, //	核查员id
    createUserName: user.name, //	核查员
    // temperature: Number(form.value.temperature), // 环境温度
    // humidity: Number(form.value.humidity), // 环境湿度
    itemCategoryId: chekedEquipmentList.value[0].itemCategoryId, // 核查项分类id
    standardEquipmentId: selectStandardId.value, // 被核查标准装置id
    id: infoId.value,
  }
  saveCheckRecordRef.value.initDialog(params)
}

// -------------------------------------钩子-----------------------------------------------------
watch(() => selectStandardId.value, (newValue) => {
  if (newValue) {
    fetchJobInstruction(false, newValue) // 请求作业指导书
  }
})
onMounted(async () => {
  belongStandardEquipment.value = $route.query.belongStandardEquipment as string // 检校标准装置
  getDict()
  if (pageType.value === 'add') {
    form.value.createUserName = user.name // 核查员
  }
  else {
    fetchInfo()
  }
})
</script>

<template>
  <app-container>
    <detail-page :title="`核查项分类管理-${textMap[pageType]}`">
      <template #btns>
        <el-button v-if="pageType === 'detail'" type="primary" @click="createCheckRecord">
          生成核查记录
        </el-button>
        <!-- <el-button v-if="pageType === 'detail'" type="primary" @click="edit">
          编辑
        </el-button> -->
        <el-button v-if="pageType !== 'detail'" type="primary" @click="save">
          保存
        </el-button>
        <el-button type="info" @click="close">
          关闭
        </el-button>
      </template>
      <el-form
        ref="ruleFormRef"
        :model="form"
        label-width="130"
        label-position="right"
        :rules="formRules"
      >
        <el-row :gutter="24">
          <el-col :span="6">
            <el-form-item label="核查数据编号:" prop="dataNo">
              <el-input v-model="form.dataNo" class="full-width-input" disabled placeholder="系统自动生成" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="核查日期:" prop="checkDate">
              <el-date-picker
                v-model="form.checkDate"
                type="date"
                format="YYYY-MM-DD"
                value-format="YYYY-MM-DD"
                :placeholder="pageType === 'detail' ? ' ' : '请选择核查日期'"
                :disabled="pageType === 'detail'"
                class="full-width-input"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="核查地点:" prop="checkAddress">
              <el-select
                v-model="form.checkAddress"
                placeholder="核查地点"
                filterable
                :disabled="pageType === 'detail'"
              >
                <el-option v-for="item in positionList" :key="item.id" :label="item.name" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item label="温度(℃):" prop="temperature">
              <el-input-number
                v-model="form.temperature"
                :disabled="pageType === 'detail'"
                :placeholder="pageType === 'detail' ? ' ' : '请输入温度'"
                :max="99.99"
                :precision="2"
              />
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item label="相对湿度(%):" prop="humidity">
              <el-input-number
                v-model="form.humidity"
                :disabled="pageType === 'detail'"
                :placeholder="pageType === 'detail' ? ' ' : '请输入相对湿度'"
                :max="99.99"
                :precision="2"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="核查员:" prop="dacreateUserNametaNo">
              <el-input v-model="form.createUserName" disabled placeholder="检定员" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="是否用于稳定性考核:" label-width="160" prop="stabilityExamine">
              <el-select
                v-model="form.stabilityExamine"
                placeholder="请选择"
                class="short-input"
                filterable
                :disabled="pageType === 'detail'"
              >
                <el-option v-for="item in stabilityExamineList" :key="item.id" :label="item.name" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24" class="marg">
          <el-col :span="24">
            <el-form-item label="核查依据:" prop="technologyFile">
              <div v-for="(item, index) in technologyFile" :key="index" style="display: flex;">
                <show-photo :minio-file-name="item" style="margin-right: 10px;" />
              </div>
              <span v-if="pageType === 'detail' && !technologyFile">无</span>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </detail-page>
    <detail-block title="核查标准设备">
      <template v-if="pageType !== 'detail'" #btns>
        <el-button type="primary" @click="multiAddEquipment">
          批量添加
        </el-button>
        <el-button type="info" @click="delEquipmentRow">
          删除行
        </el-button>
      </template>
      <el-table
        :data="equipmentList"
        border
        style="width: 100%;"
        @selection-change="handleSelectionEquipmentChange"
      >
        <el-table-column v-if="pageType !== 'detail'" type="selection" width="55" />
        <el-table-column align="center" label="序号" width="80" type="index" />
        <el-table-column
          v-for="item in equipmentColumns"
          :key="item.value"
          :prop="item.value"
          :label="item.text"
          :width="item.width"
          align="center"
        />
      </el-table>
    </detail-block>
    <detail-block title="被核查设备">
      <el-table
        :data="chekedEquipmentList"
        border
        style="width: 100%;"
      >
        <el-table-column align="center" label="序号" width="80" type="index" />
        <el-table-column
          v-for="item in equipmentColumns"
          :key="item.value"
          :prop="item.value"
          :label="item.text"
          :width="item.width"
          align="center"
        >
          <template #default="scope">
            <el-input
              v-if="pageType !== 'detail' && item.value === 'equipmentName'"
              v-model="scope.row[item.value]"
              placeholder="请选择"
              disabled
            >
              <template #append>
                <el-button size="small" @click="selectStandardEquipment">
                  选择
                </el-button>
              </template>
            </el-input>
          </template>
        </el-table-column>
      </el-table>
    </detail-block>

    <check-data-first
      ref="checkDataFirstRef"
    />

    <!-- 选择设备台账 -->
    <select-equipment-dialog ref="selectEquipmentDialogRef" :is-multi="true" @confirm="confirmSelectEquipment" />
    <!-- 选择标准装置设备 -->
    <select-standard-equipment-dialog ref="selectStandardEquipmentDialogRef" @confirm="confirmSelectedStandardEquipmentDialog" />
    <!-- 保存核查记录  -->
    <save-check-record ref="saveCheckRecordRef" />
  </app-container>
</template>

<style lang="scss" scoped>
.link {
  text-decoration: underline;
  color: #3d7eff;
  cursor: pointer;
}

.file-area {
  display: flex;
  align-items: center;
  font-size: 14px;
  color: #60627f;
  margin-bottom: 10px;
  margin-left: 40px;
  white-space: nowrap;

  .tech-file {
    display: flex;
    align-items: center;
    margin-left: 20px;

    .file-text {
      margin-right: 10px;
    }
  }
}
</style>