contractTemplateCreate.vue 7.47 KB
<route lang="yaml">
name: contractTemplateCreate
</route>

<script lang="ts" setup name="contractTemplateCreate">
import { useValidator } from "@/hooks/useValidator";
import useUserStore from "@/store/modules/user";
import useDataSmartContract from "@/store/modules/dataSmartContract";
import {
  getContractTemplateDetail,
  getActionPolicyList,
  getConstraintPolicyList,
  updateContractTemplate,
  saveContractTemplate,
} from "@/api/modules/dataSmartContract"
import {
  getParamsList
} from "@/api/modules/queryService";
import StrategyTable from "./components/strategyTable.vue";

const { required } = useValidator();
const router = useRouter();
const route = useRoute();
const { proxy } = getCurrentInstance() as any;
const userStore = useUserStore();
const dataSmartContract = useDataSmartContract();
const fullPath = route.fullPath;
const fullscreenLoading = ref(false);
const expandBase = ref(false);
const expandInfo = ref(false);

const baseInfoFormRef = ref();
const baseInfoFormItems = ref([{
  type: 'input',
  label: '合约模板名称',
  field: 'templateName',
  default: '',
  placeholder: '请输入',
  maxlength: 50,
  clearable: true,
  required: true
}, {
  type: 'input',
  label: '版本',
  field: 'version',
  default: 'v1.0.0',
  placeholder: '-',
  disabled: true,
  clearable: true,
  required: false
}, {
  type: 'input',
  label: '合约模板编号',
  field: 'templateId',
  default: '',
  placeholder: '自动生成',
  disabled: true,
  clearable: true,
  required: false
}, {
  label: '启用状态',
  type: 'switch',
  field: 'bizStatus',
  default: 'Y',
  placeholder: '请选择',
  activeValue: 'Y',
  inactiveValue: 'N',
  switchWidth: 32,
}, {
  label: '模板描述',
  type: 'textarea',
  placeholder: '请输入',
  field: 'description',
  default: '',
  block: true,
  maxlength: 500,
  clearable: true,
  required: false,
}]);

const baseInfoFormRules = ref({
  templateName: [required("请填写合约模板名称")],
});

const strategyTableRef = ref();

const cancel = () => {
  proxy.$openMessageBox(
    "当前页面尚未保存,确定放弃修改吗?",
    () => {
      userStore.setTabbar(userStore.tabbar.filter((tab: any) => tab.fullPath !== fullPath));
      router.push({
        name: "contractTemplateManagement",
      });
    },
    () => {
      proxy.$ElMessage.info("已取消");
    }
  );
}

const submit = () => {
  baseInfoFormRef.value.ruleFormRef?.validate((valid, errorItem) => {
    if (valid) {
      let validate = strategyTableRef.value?.validateValue();
      if (!validate) {
        expandInfo.value = true;
        return;
      }
      let params = { ...baseInfoFormRef.value.formInline };
      params.policyRQVOS = strategyTableRef.value.strategyData?.map((d, index) => {
        return Object.assign({}, d, { orderNum: index + 1 })
      });
      if (route.query.guid) {
        params.guid = route.query.guid;
        fullscreenLoading.value = true;
        updateContractTemplate(params).then((res: any) => {
          fullscreenLoading.value = false;
          if (res?.code == proxy.$passCode) {
            proxy.$ElMessage.success('编辑合约模板成功');
            userStore.setTabbar(userStore.tabbar.filter((tab: any) => tab.fullPath !== fullPath));
            router.push({
              name: "contractTemplateManagement",
            });
            dataSmartContract.set(true);
          } else {
            res?.msg && proxy.$ElMessage.error(res?.msg);
          }
        }).catch(() => {
          fullscreenLoading.value = false;
        });
      } else {
        fullscreenLoading.value = true;
        delete params.version;
        saveContractTemplate(params).then((res: any) => {
          fullscreenLoading.value = false;
          if (res?.code == proxy.$passCode) {
            proxy.$ElMessage.success('新建合约模板成功');
            userStore.setTabbar(userStore.tabbar.filter((tab: any) => tab.fullPath !== fullPath));
            router.push({
              name: "contractTemplateManagement",
            });
            dataSmartContract.set(true);
          } else {
            res?.msg && proxy.$ElMessage.error(res?.msg);
          }
        }).catch(() => {
          fullscreenLoading.value = false;
        });
      }
    } else {
      expandBase.value = true;
      var obj = Object.keys(errorItem);
      baseInfoFormRef.value.ruleFormRef?.scrollToField(obj[0]);
    }
  })
}

const detailInfo: any = ref({});
/** 约束运算符字典下拉 */
const operatorOptionList: any = ref([]);

/** 约束行为下拉列表 */
const constraintOptionsList: any = ref([]);

/** 策略操作行为下拉列表 */
const actionOptionsList: any = ref([]);

onBeforeMount(() => {
  if (route.query.guid) {
    fullscreenLoading.value = true;
    getContractTemplateDetail({ guid: route.query.guid }).then((res: any) => {
      fullscreenLoading.value = false;
      if (res?.code == proxy.$passCode) {
        detailInfo.value = res.data || {};
        baseInfoFormItems.value.forEach(item => {
          item.default = detailInfo.value[item.field] || "";
        });
      } else {
        res?.msg && proxy.$ElMessage.error(res?.msg);
      }
    });
  }
  getParamsList({ dictType: '约束运算符' }).then((res: any) => {
    if (res?.code == proxy.$passCode) {
      const data = res.data || [];
      operatorOptionList.value = data;
    } else {
      res?.msg && proxy.$ElMessage.error(res?.msg);
    }
  })
  getActionPolicyList().then((res: any) => {
    if (res?.code == proxy.$passCode) {
      const data = res.data || [];
      actionOptionsList.value = data;
    } else {
      res?.msg && proxy.$ElMessage.error(res?.msg);
    }
  })
  getConstraintPolicyList().then((res: any) => {
    if (res?.code == proxy.$passCode) {
      const data = res.data || [];
      constraintOptionsList.value = data;
    } else {
      res?.msg && proxy.$ElMessage.error(res?.msg);
    }
  })
})

</script>

<template>
  <div class="container_wrap full" v-loading="fullscreenLoading">
    <div class="content_main panel">
      <ContentWrap title="模板信息" expandSwicth style="margin-top: 15px" :isExpand="expandBase"
        @expand="(v) => (expandBase = v)" description="">
        <Form ref="baseInfoFormRef" formId="base-info-form" :itemList="baseInfoFormItems" :rules="baseInfoFormRules"
          col="col3" />
      </ContentWrap>
      <ContentWrap title="策略信息" expandSwicth style="margin-top: 15px" :isExpand="expandInfo"
        @expand="(v) => (expandInfo = v)" description="">
        <StrategyTable ref="strategyTableRef" :value="detailInfo.policyRSVOS || (route.query.guid ? [] : [{
          index: 1,
          action: '',
          children: [{ childIndex: 1 }]
        }])" :operatorOptionList="operatorOptionList" :actionOptionsList="actionOptionsList"
          :constraintOptionsList="constraintOptionsList"></StrategyTable>
      </ContentWrap>
    </div>
    <div class="tool_btns">
      <div class="btns">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.container_wrap {
  overflow: hidden;

  .content_main {
    height: calc(100% - 45px);
    overflow: hidden auto;

    &.panel {
      padding: 0 16px 16px;
    }
  }
}

.tool_btns {
  height: 44px;
  margin: 0 -8px;
  display: flex;
  justify-content: center;
  align-items: center;
  border-top: 1px solid #d9d9d9;
}

:deep(.strategyTable.el-table) {
  .cell {
    padding: 0px 10px;
  }

  & td.el-table__cell {
    padding: 2px 0px;
    height: 36px;
  }
}
</style>