addMapping.vue 11.1 KB
<!--
 * @Author: yangming yangming@didihu.com.cn
 * @Date: 2023-01-04 14:00:10
 * @LastEditors: yangming yangming@didihu.com.cn
 * @LastEditTime: 2023-01-06 14:18:09
 * @FilePath: \scrm-micro-mapping\src\views\mapping\addMapping.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div class="mapping-main">
    <div class="mapping-container" v-loading="loading">
      <div class="box-title">新增标签映射</div>
      <div class="box-form gl-form-page">
        <el-form ref="ruleForm" :model="addForm" :rules="rules" label-width="154px">
          <div class="el-form-item el-form-item--small is-required">
            <label class="el-form-item__label" style="width: 154px;">选择门店:</label>
            <div class="el-form-item__content" style="margin-left: 154px;">
              <el-select v-model="merchantId" placeholder="请选择" filterable @change="queryGroupListByMerchantId" size="small" :disabled="editStatus">
                <el-option v-for="item in selectMerchant" :key="item.merchantNo" :label="item.merchantName" :value="item.merchantNo"></el-option>
              </el-select>
            </div>
          </div>
          <el-form-item label="所属工厂:" size="small" v-if="topMerchantName">
            <el-input v-model="topMerchantName" :disabled="true"></el-input>
          </el-form-item>
          <div class="el-form-item el-form-item--small is-required">
            <label class="el-form-item__label" style="width: 154px;">标签:</label>
            <div class="el-form-item__content" style="margin-left: 154px;">
              <div class="tag-panel">
                <div class="tag-item" v-for="(item, index) in groupList" :key="item">
                  <div class="tag-item-select">
                    <el-select v-model="item.groupValue_l" placeholder="请选择" filterable @change="changeGroup_l(item)" size="small">
                      <el-option v-for="item in groupList_l" :key="item" :label="item" :value="item"></el-option>
                    </el-select>
                    <el-select v-model="item.sysLabelName" placeholder="请选择" filterable @change="changeTag_l(item)" size="small">
                      <el-option v-for="i in item.tagList_l" :key="i.labelName" :label="i.labelName" :value="i.labelName"></el-option>
                    </el-select>
                  </div>
                  <div class="tag-img"><img src="@/assets/mapping.png" alt="" /></div>
                  <div class="tag-item-select">
                    <el-select v-model="item.groupValue_r" placeholder="请选择" filterable @change="changeGroup_r(item)" size="small">
                      <el-option v-for="item in groupList_r" :key="item" :label="item" :value="item"></el-option>
                    </el-select>
                    <el-select v-model="item.envLabelName" placeholder="请选择" filterable @change="changeTag_r(item)" size="small">
                      <el-option v-for="i in item.tagList_r" :key="i.labelName" :label="i.labelName" :value="i.labelName"></el-option>
                    </el-select>
                  </div>
                  <div class="tag-icon" @click="delTag(index)" v-if="index > 0">
                    <i class="el-icon-delete"></i>
                  </div>
                </div>
                <div class="tag-add" @click="addTag">
                  <i class="el-icon-circle-plus-outline"></i>
                  新增标签
                </div>
              </div>
            </div>
          </div>
        </el-form>
      </div>
    </div>
    <GLFooter>
      <el-button @click="handleCancel" size="medium">取消</el-button>
      <el-button type="primary" @click="handleConfirm" size="medium" :loading="loading">提交</el-button>
    </GLFooter>
  </div>
</template>

<script>
const tagType = [
  {
    name: '系统标签',
    type: 1
  },
  {
    name: '企业标签',
    type: 2
  }
]
const tagJson = {
  groupValue_l: '',
  groupValue_r: '',
  tagList_l: [],
  tagList_r: [],
  envLabelId: '',
  sysLabelId: '',
  sysGroupId: '',
  envGroupId: '',
  sysLabelName: '',
  envLabelName: ''
}
export default {
  name: 'addMapping',
  data() {
    return {
      editStatus: false,
      loading: false,
      addForm: {},
      topMerchantName: '',
      selectMerchant: [],
      groupList: [{ ...tagJson }],
      groupList_l: [],
      groupList_r: [],
      merchantId: '',
      tagList: []
    }
  },
  watch: {
    merchantId() {
      this.findTopMerchantInfo()
    }
  },
  methods: {
    addTag() {
      this.groupList.push({ ...tagJson })
    },
    delTag(index) {
      this.groupList.splice(index, 1)
    },
    handleCancel() {
      this.$router.push('/mapping/index')
    },
    handleConfirm() {
      if (!this.merchantId) {
        this.$message.error('请选择门店')
        return
      }
      const labelRelation = this.groupList.map(item => {
        return {
          envLabelId: item.envLabelId,
          sysLabelId: item.sysLabelId,
          sysGroupId: item.sysGroupId,
          envGroupId: item.envGroupId
        }
      })
      for (let label of labelRelation) {
        if (!label.envLabelId || !label.sysLabelId || !label.sysGroupId || !label.envGroupId) {
          this.$message.error('请选择标签')
          return
        }
      }
      const params = {
        req: {
          merchantId: this.merchantId,
          labelRelation: JSON.stringify(labelRelation)
        }
      }
      let relation = 'addLabelRelation'
      if (this.editStatus) {
        relation = 'editLabelRelation'
      }
      this.$apis.mapping[relation](params).then(res => {
        if (res.code == 0) {
          this.$message.success('操作成功')
          this.handleCancel()
        }
      })
      //   this.$refs.ruleFormy.validate(valid => {
      //     if (valid) {
      //       console.log(this.groupList)
      //       const params = {
      //         req: {
      //           merchantId: this.merchantId,
      //           labelRelation: [
      //             { envGroupId: 389, envLabelId: 1611, sysGroupId: 383, sysLabelId: 1622 },
      //             { envGroupId: 389, envLabelId: 1612, sysGroupId: 383, sysLabelId: 1623 }
      //           ]
      //         }
      //       }
      //       this.$apis.mapping.addLabelRelation(params).then(res => {
      //         this.groupList_r = res.groupList
      //       })
      //     }
      //   })
    },
    //改变系统标签
    changeGroup_l(group) {
      this.queryLabelListByGroupName(group, 1)
    },
    //改变系统标签
    changeGroup_r(group) {
      this.queryLabelListByGroupName(group, 2)
    },
    //改变系统标签
    changeTag_l(group) {
      this.queryLabelListByTagName(group, 1)
    },
    //改变系统标签
    changeTag_r(group) {
      this.queryLabelListByTagName(group, 2)
    },
    queryLabelListByTagName(group, type) {
      if (type == 1) {
        group.sysLabelId = group.tagList_l.find(tag => tag.labelName == group.sysLabelName)?.labelId
      }
      if (type == 2) {
        group.envLabelId = group.tagList_r.find(tag => tag.labelName == group.envLabelName)?.labelId
      }
    },
    queryGroupListByMerchantId() {
      const params = {
        req: {
          merchantId: this.merchantId,
          groupType: '系统标签'
        }
      }
      this.$apis.mapping.queryGroupListByMerchantId(params).then(res => {
        this.groupList_l = res.groupList
      })
      const params2 = {
        req: {
          merchantId: this.merchantId,
          groupType: '企业标签'
        }
      }
      this.$apis.mapping.queryGroupListByMerchantId(params2).then(res => {
        this.groupList_r = res.groupList
        console.log(this.groupList_r)
      })
    },
    queryLabelListByGroupName(item, type) {
      let groupType = tagType.find(i => i.type == type).name,
        groupName
      if (type == 1) {
        groupName = item.groupValue_l
        item.tagList_l = []
        item.sysGroupId = ''
      } else {
        groupName = item.groupValue_r
        item.tagList_r = []
        item.envGroupId = ''
      }
      const params = {
        req: {
          merchantId: this.merchantId,
          groupType,
          groupName
        }
      }
      this.$apis.mapping.queryLabelListByGroupName(params).then(res => {
        if (type == 1) {
          item.tagList_l = res.labelUnitList
          item.sysGroupId = res.groupId
        } else {
          item.tagList_r = res.labelUnitList
          item.envGroupId = res.groupId
        }
      })
    },
    findTopMerchantInfo() {
      this.$apis.mapping.findTopMerchantInfo({ merchantNoStr: this.merchantId }).then(res => {
        this.topMerchantName = res && res[0]?.topMerchantName
      })
    },
    async findMerchantListByPage() {
      this.loading = true
      const selectMerchant = await this.$apis.mapping.findMerchantListByPage({ merchantAccountLevel: [4], channelType: 1, pageNum: 1, pageSize: 10000 })
      this.loading = false
      this.selectMerchant = selectMerchant.data
      const { merchantId } = this.$route.query
      if (merchantId) {
        this.editStatus = true
        this.merchantId = +merchantId
        this.queryGroupListByMerchantId()
        this.$apis.mapping.queryLabelRelation({ req: { merchantId } }).then(res => {
          this.groupList = res.relationUnitList.map(item => {
            return {
              groupValue_l: item.sysGroupName,
              groupValue_r: item.envGroupName,
              envLabelName: item.envLabelName,
              sysLabelName: item.sysLabelName,
              tagList_l: [],
              tagList_r: [],
              envLabelId: item.envLabelId,
              sysLabelId: item.sysLabelId,
              sysGroupId: item.sysGroupId,
              envGroupId: item.envGroupId
            }
          })
          console.log(this.groupList)
          this.groupList.forEach(item => {
            this.changeGroup_l(item)
            this.changeGroup_r(item)
          })
        })
      }
    }
  },

  mounted() {
    // this.queryGroupListByMerchantId()

    this.findMerchantListByPage()
  }
}
</script>
<style lang="scss" scoped>
.mapping-container {
  background: #ffffff;
  min-height: calc(100vh - 136px);
  margin-bottom: 16px;
  .box-title {
    height: 56px;
    line-height: 56px;
    padding-left: 20px;
    border-bottom: 1px solid #e5e5e5;
    font-weight: 600;
    font-size: 16px;
    color: #333333;
  }
  .box-form {
    padding: 20px;
    .tag-panel {
      .tag-add {
        color: #5850ff;
        cursor: pointer;
        i {
          font-size: 18px;
          vertical-align: middle;
        }
      }
      .tag-item {
        display: flex;
        align-items: center;
        margin-bottom: 10px;
        .tag-icon {
          color: #999;
          cursor: pointer;
        }
        .tag-img {
          img {
            width: 16px;
            height: 16px;
            margin-right: 10px;
          }
        }
        .tag-item-select {
          display: flex;
          /deep/ {
            .el-select,
            .el-input {
              width: 166px;
            }
          }
        }
      }
    }
  }
}
</style>