<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="统计日期" prop="statDate">
        <el-date-picker clearable
          v-model="queryParams.statDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择统计日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="仓库ID" prop="warehouseId">
        <el-input
          v-model="queryParams.warehouseId"
          placeholder="请输入仓库ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作员ID" prop="operatorId">
        <el-input
          v-model="queryParams.operatorId"
          placeholder="请输入操作员ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作方式" prop="operationMethod">
        <el-input
          v-model="queryParams.operationMethod"
          placeholder="请输入操作方式"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总操作次数" prop="totalOperations">
        <el-input
          v-model="queryParams.totalOperations"
          placeholder="请输入总操作次数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总操作数量" prop="totalQuantity">
        <el-input
          v-model="queryParams.totalQuantity"
          placeholder="请输入总操作数量"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="成功次数" prop="successfulOperations">
        <el-input
          v-model="queryParams.successfulOperations"
          placeholder="请输入成功次数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="失败次数" prop="failedOperations">
        <el-input
          v-model="queryParams.failedOperations"
          placeholder="请输入失败次数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总耗时" prop="totalDuration">
        <el-input
          v-model="queryParams.totalDuration"
          placeholder="请输入总耗时"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="平均每次操作耗时" prop="avgDurationPerOp">
        <el-input
          v-model="queryParams.avgDurationPerOp"
          placeholder="请输入平均每次操作耗时"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="平均每单位数量耗时" prop="avgDurationPerQty">
        <el-input
          v-model="queryParams.avgDurationPerQty"
          placeholder="请输入平均每单位数量耗时"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作准确率" prop="accuracyRate">
        <el-input
          v-model="queryParams.accuracyRate"
          placeholder="请输入操作准确率"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="错误次数" prop="errorCount">
        <el-input
          v-model="queryParams.errorCount"
          placeholder="请输入错误次数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备ID" prop="deviceId">
        <el-input
          v-model="queryParams.deviceId"
          placeholder="请输入设备ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备使用时长" prop="deviceUsageDuration">
        <el-input
          v-model="queryParams.deviceUsageDuration"
          placeholder="请输入设备使用时长"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="批量操作次数" prop="batchOperationCount">
        <el-input
          v-model="queryParams.batchOperationCount"
          placeholder="请输入批量操作次数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="批量操作数量" prop="batchOperationQuantity">
        <el-input
          v-model="queryParams.batchOperationQuantity"
          placeholder="请输入批量操作数量"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="平均批量大小" prop="avgBatchSize">
        <el-input
          v-model="queryParams.avgBatchSize"
          placeholder="请输入平均批量大小"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="RFID读取次数" prop="rfidReadCount">
        <el-input
          v-model="queryParams.rfidReadCount"
          placeholder="请输入RFID读取次数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="RFID批量读取次数" prop="rfidBatchReadCount">
        <el-input
          v-model="queryParams.rfidBatchReadCount"
          placeholder="请输入RFID批量读取次数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="平均RFID读取时间" prop="avgRfidReadTime">
        <el-input
          v-model="queryParams.avgRfidReadTime"
          placeholder="请输入平均RFID读取时间"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扫码次数" prop="scanCount">
        <el-input
          v-model="queryParams.scanCount"
          placeholder="请输入扫码次数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扫码成功率" prop="scanSuccessRate">
        <el-input
          v-model="queryParams.scanSuccessRate"
          placeholder="请输入扫码成功率"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="平均扫码时间" prop="avgScanTime">
        <el-input
          v-model="queryParams.avgScanTime"
          placeholder="请输入平均扫码时间"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['ware:wmsOperationStatistics:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ware:wmsOperationStatistics:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ware:wmsOperationStatistics:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['ware:wmsOperationStatistics:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="wmsOperationStatisticsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="统计ID" align="center" prop="statId" />
      <el-table-column label="统计日期" align="center" prop="statDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.statDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="仓库ID" align="center" prop="warehouseId" />
      <el-table-column label="操作员ID" align="center" prop="operatorId" />
      <el-table-column label="操作类型" align="center" prop="operationType" />
      <el-table-column label="操作方式" align="center" prop="operationMethod" />
      <el-table-column label="总操作次数" align="center" prop="totalOperations" />
      <el-table-column label="总操作数量" align="center" prop="totalQuantity" />
      <el-table-column label="成功次数" align="center" prop="successfulOperations" />
      <el-table-column label="失败次数" align="center" prop="failedOperations" />
      <el-table-column label="总耗时" align="center" prop="totalDuration" />
      <el-table-column label="平均每次操作耗时" align="center" prop="avgDurationPerOp" />
      <el-table-column label="平均每单位数量耗时" align="center" prop="avgDurationPerQty" />
      <el-table-column label="操作准确率" align="center" prop="accuracyRate" />
      <el-table-column label="错误次数" align="center" prop="errorCount" />
      <el-table-column label="错误类型分布" align="center" prop="errorTypes" />
      <el-table-column label="设备ID" align="center" prop="deviceId" />
      <el-table-column label="设备使用时长" align="center" prop="deviceUsageDuration" />
      <el-table-column label="批量操作次数" align="center" prop="batchOperationCount" />
      <el-table-column label="批量操作数量" align="center" prop="batchOperationQuantity" />
      <el-table-column label="平均批量大小" align="center" prop="avgBatchSize" />
      <el-table-column label="RFID读取次数" align="center" prop="rfidReadCount" />
      <el-table-column label="RFID批量读取次数" align="center" prop="rfidBatchReadCount" />
      <el-table-column label="平均RFID读取时间" align="center" prop="avgRfidReadTime" />
      <el-table-column label="扫码次数" align="center" prop="scanCount" />
      <el-table-column label="扫码成功率" align="center" prop="scanSuccessRate" />
      <el-table-column label="平均扫码时间" align="center" prop="avgScanTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['ware:wmsOperationStatistics:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['ware:wmsOperationStatistics:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改操作效率统计对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="wmsOperationStatisticsRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="统计日期" prop="statDate">
          <el-date-picker clearable
            v-model="form.statDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择统计日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="仓库ID" prop="warehouseId">
          <el-input v-model="form.warehouseId" placeholder="请输入仓库ID" />
        </el-form-item>
        <el-form-item label="操作员ID" prop="operatorId">
          <el-input v-model="form.operatorId" placeholder="请输入操作员ID" />
        </el-form-item>
        <el-form-item label="操作方式" prop="operationMethod">
          <el-input v-model="form.operationMethod" placeholder="请输入操作方式" />
        </el-form-item>
        <el-form-item label="总操作次数" prop="totalOperations">
          <el-input v-model="form.totalOperations" placeholder="请输入总操作次数" />
        </el-form-item>
        <el-form-item label="总操作数量" prop="totalQuantity">
          <el-input v-model="form.totalQuantity" placeholder="请输入总操作数量" />
        </el-form-item>
        <el-form-item label="成功次数" prop="successfulOperations">
          <el-input v-model="form.successfulOperations" placeholder="请输入成功次数" />
        </el-form-item>
        <el-form-item label="失败次数" prop="failedOperations">
          <el-input v-model="form.failedOperations" placeholder="请输入失败次数" />
        </el-form-item>
        <el-form-item label="总耗时" prop="totalDuration">
          <el-input v-model="form.totalDuration" placeholder="请输入总耗时" />
        </el-form-item>
        <el-form-item label="平均每次操作耗时" prop="avgDurationPerOp">
          <el-input v-model="form.avgDurationPerOp" placeholder="请输入平均每次操作耗时" />
        </el-form-item>
        <el-form-item label="平均每单位数量耗时" prop="avgDurationPerQty">
          <el-input v-model="form.avgDurationPerQty" placeholder="请输入平均每单位数量耗时" />
        </el-form-item>
        <el-form-item label="操作准确率" prop="accuracyRate">
          <el-input v-model="form.accuracyRate" placeholder="请输入操作准确率" />
        </el-form-item>
        <el-form-item label="错误次数" prop="errorCount">
          <el-input v-model="form.errorCount" placeholder="请输入错误次数" />
        </el-form-item>
        <el-form-item label="设备ID" prop="deviceId">
          <el-input v-model="form.deviceId" placeholder="请输入设备ID" />
        </el-form-item>
        <el-form-item label="设备使用时长" prop="deviceUsageDuration">
          <el-input v-model="form.deviceUsageDuration" placeholder="请输入设备使用时长" />
        </el-form-item>
        <el-form-item label="批量操作次数" prop="batchOperationCount">
          <el-input v-model="form.batchOperationCount" placeholder="请输入批量操作次数" />
        </el-form-item>
        <el-form-item label="批量操作数量" prop="batchOperationQuantity">
          <el-input v-model="form.batchOperationQuantity" placeholder="请输入批量操作数量" />
        </el-form-item>
        <el-form-item label="平均批量大小" prop="avgBatchSize">
          <el-input v-model="form.avgBatchSize" placeholder="请输入平均批量大小" />
        </el-form-item>
        <el-form-item label="RFID读取次数" prop="rfidReadCount">
          <el-input v-model="form.rfidReadCount" placeholder="请输入RFID读取次数" />
        </el-form-item>
        <el-form-item label="RFID批量读取次数" prop="rfidBatchReadCount">
          <el-input v-model="form.rfidBatchReadCount" placeholder="请输入RFID批量读取次数" />
        </el-form-item>
        <el-form-item label="平均RFID读取时间" prop="avgRfidReadTime">
          <el-input v-model="form.avgRfidReadTime" placeholder="请输入平均RFID读取时间" />
        </el-form-item>
        <el-form-item label="扫码次数" prop="scanCount">
          <el-input v-model="form.scanCount" placeholder="请输入扫码次数" />
        </el-form-item>
        <el-form-item label="扫码成功率" prop="scanSuccessRate">
          <el-input v-model="form.scanSuccessRate" placeholder="请输入扫码成功率" />
        </el-form-item>
        <el-form-item label="平均扫码时间" prop="avgScanTime">
          <el-input v-model="form.avgScanTime" placeholder="请输入平均扫码时间" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="WmsOperationStatistics">
import { listWmsOperationStatistics, getWmsOperationStatistics, delWmsOperationStatistics, addWmsOperationStatistics, updateWmsOperationStatistics } from "@/api/ware/wmsOperationStatistics"

const { proxy } = getCurrentInstance()

const wmsOperationStatisticsList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    statDate: null,
    warehouseId: null,
    operatorId: null,
    operationType: null,
    operationMethod: null,
    totalOperations: null,
    totalQuantity: null,
    successfulOperations: null,
    failedOperations: null,
    totalDuration: null,
    avgDurationPerOp: null,
    avgDurationPerQty: null,
    accuracyRate: null,
    errorCount: null,
    errorTypes: null,
    deviceId: null,
    deviceUsageDuration: null,
    batchOperationCount: null,
    batchOperationQuantity: null,
    avgBatchSize: null,
    rfidReadCount: null,
    rfidBatchReadCount: null,
    avgRfidReadTime: null,
    scanCount: null,
    scanSuccessRate: null,
    avgScanTime: null,
  },
  rules: {
    statDate: [
      { required: true, message: "统计日期不能为空", trigger: "blur" }
    ],
    warehouseId: [
      { required: true, message: "仓库ID不能为空", trigger: "blur" }
    ],
    operationType: [
      { required: true, message: "操作类型不能为空", trigger: "change" }
    ],
    operationMethod: [
      { required: true, message: "操作方式不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询操作效率统计列表 */
function getList() {
  loading.value = true
  listWmsOperationStatistics(queryParams.value).then(response => {
    wmsOperationStatisticsList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    statId: null,
    statDate: null,
    warehouseId: null,
    operatorId: null,
    operationType: null,
    operationMethod: null,
    totalOperations: null,
    totalQuantity: null,
    successfulOperations: null,
    failedOperations: null,
    totalDuration: null,
    avgDurationPerOp: null,
    avgDurationPerQty: null,
    accuracyRate: null,
    errorCount: null,
    errorTypes: null,
    deviceId: null,
    deviceUsageDuration: null,
    batchOperationCount: null,
    batchOperationQuantity: null,
    avgBatchSize: null,
    rfidReadCount: null,
    rfidBatchReadCount: null,
    avgRfidReadTime: null,
    scanCount: null,
    scanSuccessRate: null,
    avgScanTime: null,
    createTime: null,
    updateTime: null
  }
  proxy.resetForm("wmsOperationStatisticsRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.statId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加操作效率统计"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _statId = row.statId || ids.value
  getWmsOperationStatistics(_statId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改操作效率统计"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["wmsOperationStatisticsRef"].validate(valid => {
    if (valid) {
      if (form.value.statId != null) {
        updateWmsOperationStatistics(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addWmsOperationStatistics(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _statIds = row.statId || ids.value
  proxy.$modal.confirm('是否确认删除操作效率统计编号为"' + _statIds + '"的数据项？').then(function() {
    return delWmsOperationStatistics(_statIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('ware/wmsOperationStatistics/export', {
    ...queryParams.value
  }, `wmsOperationStatistics_${new Date().getTime()}.xlsx`)
}

getList()
</script>
