social.vue 18.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
<!-- 社招绩效配置 -->
<template>
  <div class="social-config">
    <el-button type="primary" class="add_product_btn" size="small" :disabled="computedProductConfigList.length >= 10" plain @click="addProduct">新增产品</el-button>
    <glsx-combi-form class="edit-form-container"
                ref="editFormRef"
                label-width="150px"
                :is-block="true"
                form-ref="editChildRef"
                :form-obj="configInfo"
                :form-data="performanceFormData"
                :form-rules="formRules">
      <div :slot="item" :key="index" v-for="(item, index) in configItems">
        <template>
          <el-table class="award-wrap" :data="configInfo[item + 'List']" :style="{ 'width': item == 'outputBonus' ? '100%' : '60%' }" :header-cell-style="rowClass">
            <el-table-column label="序号" type="index" width="50" align="center"></el-table-column>
            <el-table-column prop="startValue" :label="`区间开始值/元`" width="140">
              <template slot-scope="scope">
                <el-form-item style="margin-bottom: 0px;margin-right: 0;" :prop="`${item}List[${scope.$index}].startValue`" :rules="formRules.startValue">
                  <el-input class="input-w-95" @input="handleInput($event, 'startValue', scope, item)"  v-model="scope.row.startValue" placeholder="请输入">
                  </el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column prop="endValue" :label="`区间结束值/元`" width="140">
              <template slot-scope="scope">
                <el-form-item style="margin-bottom: 0px;margin-right: 0;" :prop="`${item}List[${scope.$index}].endValue`"
                  :rules="checkEndValue(scope.row)">
                  <el-input class="input-w-95" @input="handleInput($event, 'endValue', scope, item)" v-model="scope.row.endValue" placeholder="请输入">
                  </el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column v-if="item != 'outputBonus'" prop="calculateValue" :label="'提成系数'" min-width="130">
              <template slot-scope="scope">
                <el-form-item style="margin-bottom: 0px;margin-right: 0;" :prop="`${item}List[${scope.$index}].calculateValue`" :rules="formRules.calculateValue">
                  <el-input class="input-w-95" 
                    v-model="scope.row.calculateValue"
                    @input="handleInput($event, 'calculateValue', scope, item)"
                    :placeholder="item == 'outputBonus' ? '请输入' : '最大为10'">
                  </el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <!-- 空白列占位 -->
            <el-table-column min-width="580" v-if="(!configInfo['productConfigList'] || configInfo['productConfigList'].length == 0) && item == 'outputBonus'"></el-table-column>
            <template v-if="item == 'outputBonus'">
              <template v-for="(v, colIndex) in computedProductConfigList">
                <el-table-column :key="`${colIndex}_${v.productType}`" min-width="270" prop="select">
                  <template slot="header" slot-scope="scope">
                    <div class="custom-header">
                      <el-form-item :prop="`productConfigList[${colIndex}].productType`" :rules="formRules.calculateValue">
                        <el-cascader style="width: 136px" :ref="`cascader${colIndex}`" :options="v.productTypeFocus ? productGoodsTypeList : baseData.goodsTypeList" filterable
                          v-model="v['productType']"
                          @visible-change="handleVisibleChange($event, colIndex)" aria-hidden="false"
                          @change="changeGoodsType($event, colIndex)"
                          :props="{ checkStrictly: true, expandTrigger: 'hover', label: 'categoryName', value: 'categoryType' }" :show-all-levels="false">
                        </el-cascader>
                      </el-form-item>
                      <span style="margin: 0 6px 0 4px;">设提成比例%</span>
                      <i class="el-icon-close" style="cursor: pointer;" v-if="computedProductConfigList.length != 1" @click="delProduct(colIndex)"></i>
                    </div>
                  </template>
                  <template slot-scope="scope">
                    <el-form-item :prop="`${item}List[${scope.$index}].productConfig[${colIndex}].calculateValue`" :rules="formRules.calculateValue">
                      <el-input class="input-w-95" @input="handleInputProduct($event, scope.$index, colIndex, item)" 
                        v-model="configInfo[item + 'List'][scope.$index].productConfig[colIndex].calculateValue" placeholder="请输入">
                        <span slot="suffix" class="suffix_text">%</span>
                      </el-input>
                    </el-form-item>
                  </template>
                </el-table-column>
              </template>
            </template>
            <el-table-column label="操作" width="125">
              <template slot-scope="scope">
                <div class="table-btns">
                  <el-button @click="handleMove(configInfo[item + 'List'], scope, 'up', item)" :disabled="scope.$index == 0" type="text" size="small">上移</el-button>
                  <el-button @click="handleMove(configInfo[item + 'List'], scope, 'down', item)" :disabled="scope.$index == (configInfo[item + 'List'].length - 1)" type="text" size="small">下移</el-button>
                  <el-button @click="handleDelete(item, scope.$index)" type="text" size="small">删除</el-button>
                </div>
              </template>
            </el-table-column>
          </el-table>
          <div class="add-txt">
            <el-button icon="el-icon-plus" size="small" @click="addLine(item)">新增一行</el-button>
          </div>
        </template>
      </div>
    </glsx-combi-form>
  </div>
</template>

<script>
import { deepClone } from '@/common/util'
import BigNumber from 'bignumber.js'
import { limitPrice } from '@/common/format'
export default {
  name: 'SocialConfig',
  data() {
    function checkCalculateValue (rule, value, callback) {
      if (value == 0) {
        callback('不能为0')
      } else {
        callback()
      }
    }
    return {
      configItems: ['outputBonus', 'upgradeRateBonus'],
      performanceFormData: [{
        label: '个人月均产值提成',
        name: 'outputBonus',
        addHtml: 'outputBonus',
        isHide: false
      }, {
        label: '客单价提成系数',
        name: 'upgradeRateBonus',
        addHtml: 'upgradeRateBonus',
        isHide: false,
        tips: '客单价=个人在单店的产值/个人轻改售前触客台次'
      }],
      formRules: {
        startValue: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        productType: [
          { required: true, message: '请选择', trigger: ['blur', 'change'] }
        ],
        calculateValue: [
          { required: true, message: '请输入', trigger: 'blur' },
          { validator: checkCalculateValue, trigger: 'blur' }
        ]
      },
      configInfo: {},
      baseData: {
        goodsTypeList: []
      }
    }
  },

  props: {
    perConfigInfo: {
      type: Object,
      default: () => ({})
    },
    level: {
      type: String,
    },
    goodsTypeList: {
      type: Array,
      default: () => []
    }
  },

  computed: {
    productGoodsTypeList() {
      let list = []
      let goodsTypeList = this.initGoodsTypeList()
      this.configInfo?.productConfigList?.forEach(item => {
        list.push(item.productType)
      })
      goodsTypeList = this.setGoodsTypeList(list, goodsTypeList)
      return goodsTypeList
    },
    computedProductConfigList() {
      return this.configInfo['productConfigList'] || []
    }
  },

  watch: {
    perConfigInfo: {
      handler(val) {
        this.$set(this, 'configInfo', deepClone(val))
      }
    }
  },

  mounted() {
    this.configInfo = deepClone(this.perConfigInfo)
    this.initCommission()
    this.baseData.goodsTypeList = deepClone(this.goodsTypeList)
  },

  methods: {
    // 初始化数据
    initCommission() {
      for (let item of this.configItems) {
        if (!this.configInfo?.[item + 'List']) this.$set(this.configInfo, item + 'List', [])
        if (!this.configInfo?.['productConfigList']) this.$set(this.configInfo, 'productConfigList', [])
        this.$set(this.configInfo, item + 'List', this.configInfo[item + 'List'])
      }
    },
    rowClass() {
      return 'background:#F2F4F7;color:#333333;font-size:14px;font-weight:600;'
    },
    initGoodsTypeList() {
      let goodsTypeList = deepClone(this.baseData.goodsTypeList)
      // 初始化disabled值
      goodsTypeList.forEach(item => {
        item.disabled = false
        item?.children?.forEach(val => {
          val.disabled = false
          val?.children?.forEach(v => {
            v.disabled = false
          })
        })
      })
      return goodsTypeList
    },
    // 设置产品类目可选状态
    setGoodsTypeList(list, goodsTypeList) {
      list?.forEach(item => {
        goodsTypeList = goodsTypeList?.map(item1 => {
          if (item.length === 1) {
            if (item[0] === item1?.categoryType) {
              item1.disabled = true
              item1?.children?.forEach((item2) => {
                item2.disabled = true
                item2?.children?.forEach((item3) => {
                  item3.disabled = true
                })
              })
            }
          } 
          if (item.length === 2) {
            if (item[0] === item1?.categoryType) {
              item1.disabled = true
            }
            item1?.children?.forEach((item2) => {
              if (item[1] === item2?.categoryType) item2.disabled = true
              item2?.children?.forEach((item3) => {
                if (item[0] === item1?.categoryType) {
                  item3.disabled = true
                }
              })
            })
          } 
          if (item.length === 3) {
            if (item[0] === item1?.categoryType) item1.disabled = true
            item1?.children?.forEach((item2) => {
              if (item[0] === item1.categoryType) item2.disabled = true
              item2?.children?.forEach(item3 => {
                if (item[2] == item3?.categoryType) item3.disabled = true
              })
            })
          }
          return item1
        })
      })
      return goodsTypeList
    },
    // 新增产品选择
    addProduct() {
      this.configInfo['productConfigList'].push({ productType: [], productTypeFocus: false })
      this.configInfo['outputBonusList']?.forEach(item => {
        item.productConfig?.push({calculateValue: ''})
      })
    },
    // 删除产品选择
    delProduct(index) {
      this.configInfo['productConfigList']?.splice(index, 1)
      this.configInfo['outputBonusList']?.map(item => {
        item.productConfig?.splice(index, 1)
        return item
      })
    },
    // 产品选择
    changeGoodsType(e, colIndex) {
      let [firstGoodsObj, secondGoodsObj, thirdGoodsObj]= [{}, {}, {}]
      this.baseData.goodsTypeList?.forEach(val => {
        if (val.categoryType === e?.[0]) firstGoodsObj = val
        val.children?.find(item => {
          if (item.categoryType === e?.[1]) secondGoodsObj = item
          item.children?.find(v => {
            if (v.categoryType === e?.[2]) thirdGoodsObj = v
          })
        })
      })
      let list = this.configInfo?.productConfigList
      let keyArr = ['goodsType', 'secondaryGoodsType', 'thirdLevelGoodsType']
      keyArr.forEach((item, i) => {
        this.$set(list[colIndex], item, e?.[i] || '')
        this.$set(list[colIndex], item + 'Name', e?.[i] ? (i == 0 ? firstGoodsObj.categoryName : i == 1 ? secondGoodsObj.categoryName : thirdGoodsObj.categoryName) : '')
      })
    },
    handleVisibleChange(e, index) {
      this.$set(this.configInfo.productConfigList[index], 'productTypeFocus', e)
    },
    // 产品选择提成比例
    handleInputProduct(e, rowIndex, colIndex, item) {
      let formData = this.configInfo[item + 'List']
      e = e.replace(/[^\d.]/g, '')
      let oldValue = `${formData[rowIndex].productConfig[colIndex]['oldcalculateValue']}`
      this.$set(formData[rowIndex].productConfig[colIndex], 'calculateValue', limitPrice(e, oldValue, 3, 2, 0, 100))
      this.$set(formData[rowIndex].productConfig[colIndex], 'oldcalculateValue', formData[rowIndex].productConfig[colIndex]['calculateValue'])
    },
    // 移动行
    handleMove(list, scope, moveType, item) {
      const { $index } = scope
      if (moveType === 'up') {
        if ($index === 0) return
        this.handleChangeList(list, $index, $index - 1, item)
      } else if (moveType === 'down') {
        if ($index === list.length - 1) return
        this.handleChangeList(list, $index, $index + 1, item)
      }
    },
    handleChangeList(arr, indexAdd, indexDel, item) {
      arr[indexAdd] = arr.splice(indexDel, 1, arr[indexAdd])[0]
      return arr
    },
    // 新增一行
    addLine(item) {
      if (!Array.isArray(this.configInfo[item + 'List'])) {
        this.configInfo[item + 'List'] = []
      }
      const formData = this.configInfo[item + 'List']
      const obj = {
        startValue: '',
        endValue: '',
      }
      const arr = this.configInfo?.productConfigList.map(item => {
        return { calculateValue: ''}
      })
      // 根据 item 的不同推送不同的对象
      const newEntry = item === 'outputBonus' 
        ? { ...obj, productConfig: arr || [] }
        : { ...obj, calculateValue: '' }
      formData.push(newEntry)
      this.$set(this.configInfo, item + 'List', formData)
    },

    // 删除
    handleDelete(item, index) {
      this.$confirm('确认删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const data = this.configInfo
        data[item + 'List']?.splice(index, 1)
        this.$set(this.configInfo, item + 'List', data[item + 'List'])
        this.$nextTick(() => {
          this.$refs['editFormRef'].$refs['editChildRef'].clearValidate()
        })
      }).catch(() => {
      })
    },
    // 自定义校验区间结束值
    checkEndValue(row) {
      return [{
        required: true,
        validator: (rule, value, callback) => {
          if (!value) {
            callback(new Error("请输入"))
          } else if (+value <= row.startValue) {
            callback(new Error('需大于开始值'))
          } else if (+value <= 0) {
            callback(new Error('不能小于等于0'))
          } else {
            callback()
          }
        },
        trigger: 'blur'
      }]
    },
    // 输入校验
    handleInput(e, key, scope, item) {
      let formData = this.configInfo[item + 'List']
      e = e.replace(/[^\d.]/g, '')
      let index = scope.$index
      let oldValue = `${formData[index]['old' + key]}`
      let intergerDigit = key == 'calculateValue' ? 3 : 6
      let maxValue = key == 'calculateValue' && ['upgradeRateBonus'].includes(item) ? 10 : key == 'calculateValue' ? 100 : 999999
      this.$set(formData[index], key, limitPrice(e, oldValue, intergerDigit, key == 'calculateValue' ? 2 : 0, 0, maxValue))
      this.$set(formData[index], 'old' + key, formData[index][key])
    },
    // 获取配置数据
    getFormData() {
      return new Promise((resolve, reject) => {
        resolve(this.configInfo)
      })
    },
    validateCustomForm() {
      return new Promise((resolve) => {
        this.$refs['editFormRef'].$refs['editChildRef'].validate(async (valid) => {
          if (valid) {
            let rangeFlag = await this.checkRange()
            if (!rangeFlag) {
              resolve('error1')
              return
            }
            let outputBonusFlag = await this.checkOutputBonus()
            if (!outputBonusFlag) {
              resolve('error2')
              return
            }
            let outputBonusProductFlag = await this.checkOutputBonusProduct()
            if (!outputBonusProductFlag) {
              resolve('error3')
              return
            }
            resolve(this.configInfo)
          } else {
            resolve('error')
          }
        })
      })
    },
    // 校验区间值
    checkRange() {
      return new Promise((resolve) => {
        for (let i = 0; i < this.configItems?.length; i++) {
          const v = this.configItems[i]
          const itemList = deepClone(this.configInfo[`${v}List`])
          itemList?.sort((a, b) => a.startValue - b.startValue)
          let inputValue = []
          itemList?.forEach(v => {
            inputValue.push(v.endValue)
            inputValue.push(v.startValue)
          })
          inputValue.sort((a, b) => a - b)
          // 区间值存在重复
          for (var j = 1; j < itemList?.length; j++) {
            const start = new BigNumber(itemList[j].startValue || 0)
            const end = new BigNumber(itemList[j - 1]?.endValue || 0)
            if (start.toNumber() <= end.toNumber()) {
              resolve(false)
              return
            }
          }
        }
        resolve(true)
      })
    },
    // 校验个人月均产值提成是否必填
    checkOutputBonus() {
      return new Promise((resolve) => {
        if ((!this.configInfo.outputBonusList.length) && (this.configInfo.upgradeRateBonusList?.length)) {
          resolve(false)
        } else {
          resolve(true)
        }
      })
    },
    // 校验个人月均产值提成是否配置了产品
    checkOutputBonusProduct() {
      return new Promise((resolve) => {
        if (this.configInfo.outputBonusList?.length && !this.configInfo?.productConfigList?.length) {
          resolve(false)
        } else {
          resolve(true)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep #outputBonus{
  .el-form-item__label:before {
    content: '*';
    color: #F56C6C;
    margin-right: 4px;
  }
}
.edit-form-container {
  ::v-deep .el-form-item__content {
    width: calc(100% - 150px);
    .el-form-item{
      display: inline-block;
      margin: 0;
      .el-form-item__content{
        width: 100%;
      }
    }
    .el-table__body-wrapper,.el-table__fixed-body-wrapper {
      .el-table__row td:nth-child(1) {
        border-left: 1px solid #ebeef5;
      }

      .el-table__row td:nth-last-child(1) {
        border-right: 1px solid #ebeef5;
      }
      .el-table__row .cell {
        padding: 13px 10px;
      }
    }
  }
  .custom-header {
    display: flex;
    align-items: center;
  }
  .input-w-95 {
    width: 95px;
  }
  .rate_wrap {
    display: flex;
    align-items: center;
    .rate_input {
      display: inline-block;
      width: 90px;
      margin: 0 5px;
      .suffix_text {
        display: flex;
        align-items: center;
        height: 100%;
      }
    }
  }
  .add-txt{
    margin-top: 10px;
  }
}
.add_product_btn{
  position: absolute;
  right: 160px;
  transform: translateY(-55px);
}
</style>