Newer
Older
HuangJiPC / src / pages / views / dataService / dataPushManagement.vue
@zhangdeliang zhangdeliang on 21 Jun 14 KB update
<template>
  <!-- 数据推送任务管理 -->
  <div class="dataPushManagement">
    <div class="searchBoxs">
      <n-space>
        <n-input placeholder="请输入数据共享名称/数据共享编号"
                 v-model:value="searchValue1"
                 clearable
                 style="width: 300px" />
        <n-input placeholder="请输入推送地址"
                 v-model:value="searchValue2"
                 clearable
                 style="width: 200px" />
        <n-select v-model:value="searchValue3"
                  placeholder="请选择任务状态"
                  style="width: 200px"
                  :options="taskStatus"
                  clearable />
        <n-button type="primary"
                  @click="handleClick('search')">
          <template #icon>
            <n-icon>
              <Search />
            </n-icon>
          </template>搜索
        </n-button>
        <n-button type="primary"
                  @click="handleClick('add')">
          <template #icon>
            <n-icon>
              <Add />
            </n-icon>
          </template>新增任务推送
        </n-button>
      </n-space>
    </div>
    <!-- 表格 -->
    <div class="tableBox">
      <n-data-table :bordered="false"
                    :max-height="700"
                    striped
                    :columns="columns"
                    :data="tableData"
                    :loading="tableLoading"
                    :remote="true"
                    :pagination="pagination">
      </n-data-table>
    </div>
    <!-- 新增修改弹窗 -->
    <n-modal :title="modalTitle"
             :mask-closable="false"
             preset="dialog"
             :show-icon="false"
             :style="{ width: '600px' }"
             v-model:show="modalShow">
      <n-form ref="formRef"
              :label-width="120"
              :rules="formInfo.rules"
              :model="formInfo.data"
              label-placement="left">
        <n-form-item label="数据共享名称:"
                     path="proNo">
          <n-select v-model:value="formInfo.data.proNo"
                    placeholder="请选择"
                    :options="groupList"
                    :disabled="modalTitle=='修改'"
                    filterable
                    clearable>
          </n-select>
        </n-form-item>
        <n-form-item label="推送类型:"
                     path="pushType">
          <n-select v-model:value="formInfo.data.pushType"
                    placeholder="请选择"
                    :options="tsType"
                    clearable>
          </n-select>
        </n-form-item>
        <n-form-item label="时间类型:"
                     path="timeType">
          <n-select v-model:value="formInfo.data.timeType"
                    placeholder="请选择"
                    :options="timeTypeList"
                    clearable>
          </n-select>
        </n-form-item>
        <n-form-item label="推送地址:"
                     path="pushAddress">
          <n-input v-model:value="formInfo.data.pushAddress"
                   placeholder="请输入" />
        </n-form-item>
        <n-form-item label="推送频率:"
                     path="pushRate">
          <n-input v-model:value="formInfo.data.pushRate"
                   placeholder="请输入" />
        </n-form-item>
        <n-form-item label="任务描述:"
                     path="description">
          <n-input v-model:value="formInfo.data.description"
                   placeholder="请输入" />
        </n-form-item>

      </n-form>
      <template #action>
        <n-space>
          <n-button @click="() => (modalShow = false)">取消</n-button>
          <n-button type="primary"
                    @click="handleClick('submit')">确定
          </n-button>
        </n-space>
      </template>
    </n-modal>

  </div>
</template>

<script>
import { toRefs, onMounted, reactive, h, ref, nextTick } from "vue"
import { Search, Add } from "@vicons/ionicons5"
import { NButton, } from "naive-ui"
import {
  sjtsrwSearch, sjtsrwAdd, sjtsrwUpdate,
  sjtsrwDelete, sjtsrwRun, sjtsrwStop, sjtsrwList
} from "@/services"
import { resetForm, } from "@/utils/util"

export default {
  name: "dataPushManagement",
  components: { Search, Add, NButton },
  setup() {
    const allData = reactive({
      searchValue1: null,
      searchValue2: null,
      searchValue3: null,
      modalTitle: "新增",
      modalShow: false,
      uploadList: [],
      taskStatus: [
        { value: "1", label: "启用" },
        { value: "0", label: "停止" },
      ],
      tsType: [
        { value: "0", label: "实时数据" },
        { value: "1", label: "历史数据" },
        { value: "2", label: "设备故障" },
        { value: "3", label: "报警信息" },
        { value: "4", label: "离线信息" },
      ],
      timeTypeList: [
        { value: "0", label: "秒" },
        { value: "1", label: "分" },
        { value: "2", label: "小时" },
        { value: "3", label: "日" },
        { value: "4", label: "月" },
        { value: "5", label: "每天固定时间" },
      ],
      groupList: [],
      formInfo: {
        data: {
          description: "",
          proName: "",
          proNo: "",
          pushAddress: "",
          pushRate: "",
          pushType: "",
          timeType: "",
          id: "",
        },
        rules: {
          description: {
            required: true,
            trigger: ["blur"],
            message: "请输入",
          },
          proNo: {
            required: true,
            trigger: ["change"],
            message: "请选择",
          },
          pushType: {
            required: true,
            trigger: ["change"],
            message: "请选择",
          },
          timeType: {
            required: true,
            trigger: ["change"],
            message: "请选择",
          },
          pushAddress: {
            required: true,
            trigger: ["blur"],
            message: "请输入",
          },
          pushRate: {
            required: true,
            trigger: ["blur"],
            message: "请输入",
          },
        },
      },
      tableLoading: false,
      tableData: [],
      columns: [
        {
          title: "序号",
          align: "center",
          width: "80",
          render(row, index) {
            return (paginationReactive.page - 1) * paginationReactive.pageSize + index + 1;
          }
        },
        { title: "数据共享名称", align: "center", key: "proName" },
        { title: "数据共享编号", align: "center", key: "proNo" },
        { title: "任务描述", align: "center", key: "description" },
        {
          title: "任务状态", align: "center", key: "jobStatus",
          render(row) {
            return row.jobStatus == "0" ? "停止" : "启用";
          }
        },
        {
          title: "推送类型", align: "center", key: "pushType",
          render(row) {
            let arr = allData.tsType.filter(item => item.value == row.pushType);
            return arr[0].label;
          }
        },
        { title: "推送频率", align: "center", key: "pushRate" },
        {
          title: "时间类型", align: "center", key: "timeType",
          render(row) {
            let arr = allData.timeTypeList.filter(item => item.value == row.timeType);
            return arr[0].label;
          }
        },
        { title: "推送地址", align: "center", key: "pushAddress" },
        { title: "更新时间", align: "center", key: "updateTime" },
        {
          title: "操作",
          key: "actions",
          width: "280",
          align: "center",
          render(row) {
            let params = [...allData.actionColumn];
            if (row.jobStatus == "0") {
              params.push({
                size: "small",
                btnType: "primary",
                type: "work",
                default: "运行任务",
              })
              params.push({
                size: "small",
                btnType: "error",
                type: "delete",
                default: "删除任务",
              })
            } else {
              params.push({
                size: "small",
                btnType: "primary",
                type: "stop",
                default: "停止任务",
              })
            }
            const btn = params.map((item, index) => {
              return h(
                NButton,
                {
                  text: true,
                  size: item.size,
                  style: {
                    margin: "10px",
                  },
                  type: item.btnType,
                  onClick: () => handleClick(item.type, row),
                },
                { default: () => item.default }
              );
            });
            return btn;
          },
        },
      ],
      actionColumn: [
        {
          size: "small",
          btnType: "primary",
          type: "edit",
          default: "修改任务",
        },
      ],
    })
    //分页
    const paginationReactive = reactive({
      page: 1,
      pageSize: 10,
      showSizePicker: true,
      pageSizes: [10, 20, 50],
      showQuickJumper: true,
      pageCount: 0,
      itemCount: 0,
      prefix: () => {
        return "共 " + paginationReactive.itemCount + " 项";
      },
      onChange: (page) => {
        paginationReactive.page = page;
        getTableData();
      },
      onPageSizeChange: (pageSize) => {
        paginationReactive.pageSize = pageSize;
        paginationReactive.page = 1;
        getTableData();
      },
    });
    const getTableData = async () => {
      allData.tableLoading = true;
      let pramas = {
        current: paginationReactive.page,
        size: paginationReactive.pageSize,
        data: {
          condition: allData.searchValue1,
          pushAddress: allData.searchValue2,
          jobStatus: allData.searchValue3,
        }
      };
      let res = await sjtsrwSearch(pramas);
      if (res && res.code == 200) {
        allData.tableData = res.data.list;
        paginationReactive.pageCount = res.data.pages;
        paginationReactive.itemCount = res.data.total;
      }
      allData.tableLoading = false;
    };
    const formRef = ref(null);
    // 点击事件
    const handleClick = async (type, row) => {
      switch (type) {
        case "search":
          getTableData();
          break;
        case "add":
          allData.modalTitle = "新增";
          resetForm(allData.formInfo.data);
          allData.modalShow = true;
          break;
        case "edit":
          allData.modalTitle = "修改";
          allData.formInfo.data = { ...row };
          allData.modalShow = true;
          allData.formInfo.data.timeType = row.timeType ? String(row.timeType) : "";
          allData.formInfo.data.pushRate = row.pushRate ? String(row.pushRate) : "";
          break;
        case "work":
          // 运行任务
          $dialog.info({
            title: "提示",
            content: `确定运行该任务吗?`,
            positiveText: '确定',
            negativeText: '取消',
            onPositiveClick: async () => {
              let res = await sjtsrwRun(`?id=${row.id}`);
              if (res && res.code == 200) {
                getTableData();
              }
            }
          })
          break;
        case "stop":
          // 停止任务
          $dialog.info({
            title: "提示",
            content: `确定运行该任务吗?`,
            positiveText: '确定',
            negativeText: '取消',
            onPositiveClick: async () => {
              let res = await sjtsrwStop(`?id=${row.id}`);
              if (res && res.code == 200) {
                getTableData();
              }
            }
          })
          break;
        case "submit2":
          // 关联站点 数据提交
          console.log(allData.checkedVal);
          break;
        case "submit":
          formRef.value.validate((errors) => {
            if (!errors) {
              submitData();
            } else {
              $message.error("验证失败,请检查必填项");
            }
          });
          break;
        case "delete":
          $dialog.info({
            title: "提示",
            content: `确定删除该数据吗?`,
            positiveText: '确定',
            negativeText: '取消',
            onPositiveClick: () => {
              dataDel(row.id)
            }
          })
          break;
      }
    }
    // 删除数据
    async function dataDel(id) {
      let res = await sjtsrwDelete('?id=' + id);
      if (res && res.code === 200) {
        $message.success("删除成功");
        getTableData();
      }
    }
    // 提交数据
    async function submitData() {
      let params = { ...allData.formInfo.data };
      if (allData.modalTitle == "新增") {
        let arrs = allData.groupList.filter(item => item.value == params.proNo);
        params.proName = arrs[0].label;
        if (params.id) delete params.id;
        let res = await sjtsrwAdd(params);
        if (res && res.code == 200) {
          $message.success("操作成功");
          getTableData();
          allData.modalShow = false;
        }
      } else {
        let res = await sjtsrwUpdate(params);
        if (res && res.code == 200) {
          $message.success("操作成功");
          getTableData();
          allData.modalShow = false;
        } else {
          $message.error(res.msg);
        }
      }
    }
    // 获取共享名称
    async function getGroup() {
      allData.groupList = [];
      let params = { current: 0, size: 100 }
      let res = await sjtsrwList(params);
      if (res && res.code == 200) {
        let datas = res.data;
        if (datas.length == 0) return;
        datas.map(item => {
          allData.groupList.push({
            value: item.proNo,
            label: item.proName
          })
        })
      }
    }
    onMounted(() => {
      getGroup();
      getTableData();
    })
    return {
      ...toRefs(allData),
      pagination: paginationReactive,
      handleClick,
      getTableData,
      formRef,
    }
  }
}
</script>

<style lang="less" >
.dataPushManagement {
  width: 100%;
  .searchBoxs {
    margin: 10px;
  }
}
</style>