createOrderCarQuotationSimple.js 17.4 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 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
// packageEquity/pages/carEquity/serviceInfo/serviceInfo.js quotationGoods
import {
  // quotationPicture,
  // getQuotationInfo,
  quotationAdd,
  updateQuotation,
  quotationUpgrade,
  getQuotationInfoById,
  listByMerchantIdAndIds,
  quotationRecording,
  errorOrderById
} from '../../../api'
import {
  quotedImgUrl
} from '~/config'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    showTips: true,
    showPopup: false,
    owner: {
      owner: '',
      phone: '',
      certificateNo: '',
      carVin: '',
      seat: '5',
      price: '',
      confirmStatus: false,
      saveStatus: false,
      ownerGender: '男',
      city: '',
      ageRange: '',
      hobby: '',
      companion: '',
      carColor: '',
      carBrandId: '',
      carBrand: '',
      carChildsBrandId: '',
      carChildsBrandName: '',
      carSeriesId: '',
      carSeriesName: ''
    },
    quotationGoodsList: [], //权益商品列表
    // owner: {
    //   owner: 'test2',
    //   phone: 15816539334,
    //   certificateNo: '230182198012251659',
    //   carVin: '12345678912345678',
    //   seat: 5,
    //   price: 230000
    // },
    orderInfo: {
      mode: 1,
      payType: 1,
      insurancePrice: '',
      vdQuotationGoodsList: []
      // 0-商业,1-交强,2-车船,3-增值车损,4-other 对应下面字段名字
      // owner
      // business: {
      //   type: 0,
      //   three: '100万',
      //   driver: '1万',
      //   passenger: '1万'
      // },
      // mutualStrength: {
      //   type: 1,
      //   compensation: '20万'
      // },
      // vehiclesAndVessels: {
      //   type: 2,
      //   compensation: new Date().getFullYear() + '年'
      // },
      // valueAddedVehicleLoss: {
      //   type: 3
      // },
      // otherInsuranceList: []
    },
    // 页面存放表单对象的对象名
    dataAttrName: 'owner',
    // step: 'first',
    step: 'second', // 非融合不要第一步
    needChangeList: ['price', 'base', 'threePremium', 'carInsidePremium', 'passengerPremium', 'payPrice', , 'insurancePrice', 'quotedPriceUpdate', 'minEquityPrice']
  },
  closeTips() {
    this.setData({
      showTips: false
    })
  },
  reWrite(type, data) {
    console.log(type, data)
    let {
      owner
    } = this.data
    if (type == 'vehicleCertificate') {
      let {
        vinNo: carVin,
        seatingCapacity: seat,
        engineNo: engineNumber,
        carColor
      } = data
      owner = Object.assign(owner, {
        carVin,
        seat,
        engineNumber,
        carColor
      })
    }
    if (type == 'idCard') {
      // debugger
      let {
        name,
        sex: ownerGender,
        address: addr,
        cardNo: certificateNo
      } = data
      owner = Object.assign(owner, {
        owner: name,
        ownerGender,
        addr,
        certificateNo
      })
      console.log(owner, 'owner')
    }

    this.setData({
      owner
    })
  },
  handleFirstConfirm(e) {
    // let { formData } = e.detail
    this.setData({
      // ['orderInfo.owner']: formData,
      step: 'second'
    })
  },
  createWithPay(id) {
    return new Promise((resolve, reject) => {
      let para = {
        id,
        customerNameUrl: '',
        orderStatus: 2
      }
      quotationUpgrade(para).then(res => {
        // wx.$util.hideLoading()
        console.log(res, 'res')
        resolve(res)
      })
    })
  },

  setQuotationRecording() {

    // 获取保险信息
    let {
      outTradeNo
    } = this.data
    let orderInfo = this.getInsurance()
    let owner = this.getOwnerInfo()
    orderInfo.owner = owner
    orderInfo.check = true
    orderInfo.outTradeNo = outTradeNo
    wx.showLoading('提交中...')
    quotationRecording(orderInfo)
      .then(result => {
        wx.hideLoading()
        console.log(result)
        let {
          code,
          data,
          message
        } = result
        if (code == 0 && data) {
          this.setData({
            confirmStatus: false
          })
          wx.showToast({
            title: '补录订单成功',
            icon: 'success'
          })
          setTimeout(() => {
            wx.$router.redirectTo('packageEquity.equityPage', {
              tabIndex: 1
            })
          }, 1000)
        } else {
          this.setData({
            confirmStatus: false
          })
          wx.$util.showToast(message)
        }
      })
      .catch(err => {
        console.log(err)
      })
  },
  async handleSecondConfirm() {
    // debugger
    // 先校验
    let status = this.selectComponent(`#FirstStep`).handleConfirm()
    if (!status) return false
    let {
      orderInfo,
      outTradeNo,
      confirmStatus
    } = this.data
    if (!orderInfo.vdQuotationGoodsList.length) return wx.$util.showToast('请选择增值车损险商品')
    // 修改权益报价逻辑
    if (confirmStatus) return false
    this.setData({
      confirmStatus: true
    })

    // 是订单补录直接走另外的补录逻辑
    console.log(outTradeNo, '是订单补录直接走另外的补录逻辑');
    if (outTradeNo && !orderInfo.resetOrder) return this.setQuotationRecording()
    // debugger
    // 如果是门店收款 并且付款凭证没有填
    orderInfo.paymentDocList = orderInfo.paymentDocList || []
    if (orderInfo.payType == 2 && !(orderInfo.transactionNo && orderInfo.paymentDocList.length)) return this.setData({
      showPopup: true,
      confirmStatus: false
    })

    wx.showLoading('报价中...')
    let result = await this.handleSecondConfirmCheck(true)
    let {
      code,
      data: id,
      message
    } = result

    console.log(result)
    // 校验生成告知书是否成功
    if (id) {
      // 门店付款就直接返回列表
      if (orderInfo.payType == 2) return wx.$router.redirectTo('packageEquity.equityPage', {
        tabIndex: 1
      })
      let status = await this.createWithPay(id)
      wx.hideLoading()
      let that = this
      setTimeout(() => {
        that.setData({
          confirmStatus: false
        })
      })
      console.log('id', id)
      // { orderId:data.orderId, id }
      // return
      status.code == 0 ?
        this.navigatorNext({
          id,
          orderId: status.data.orderId
        }) :
        wx.$util.showToast(status.message || '报价失败')
    } else {
      console.log(message)
      wx.hideLoading()
      this.setData({
        confirmStatus: false
      })
      wx.$util.showToast(message || '报价失败')
    }

  },
  getInsurance() {
    let {
      orderInfo
    } = this.data
    orderInfo = wx.$util.deepClone(orderInfo)
    let {
      default4SStoreInfo
    } = wx.$util.getUserInfo()
    orderInfo.merchantId4s = default4SStoreInfo?.merchantId
    orderInfo.merchantName4s = default4SStoreInfo?.merchantName

    orderInfo.insurancePrice = this.multipliedByHundred(orderInfo.insurancePrice)
    orderInfo.quotedPriceUpdate = this.multipliedByHundred(orderInfo.quotedPriceUpdate)
    // console.log(orderInfo.quotedPriceUpdate );
    // debugger
    orderInfo.minEquityPrice = this.multipliedByHundred(orderInfo.minEquityPrice)
    orderInfo.vdQuotationGoodsList = orderInfo.vdQuotationGoodsList.map(item => {
      return {
        goodsId: item.goodsId,
        goodsName: item.goodsName,
        // shopMinPrice: item.shopMinPrice,
        goodsPrice: this.multipliedByHundred(item.price)
      }
    })
    return orderInfo
  },
  getOwnerInfo() {
    let {
      owner
    } = this.data

    owner = wx.$util.deepClone(owner)
    owner.price = parseFloat(owner?.price).toString() != "NaN" ? this.multipliedByHundred(owner?.price) : ''
    // orderInfo.owner = owner
    if (owner) {
      delete owner?.buyCarTime
    }
    return owner
  },
  handleSecondConfirmCheck(check) {
    // 获取保险信息
    let orderInfo = this.getInsurance()
    let owner = this.getOwnerInfo()
    orderInfo.owner = owner
    orderInfo.check = check

    console.log('orderInfo', orderInfo)
    // debugger
    try {
      return new Promise(async (resolve, reject) => {
        // let { orderInfo } = this.data
        console.log('调接口---')
        // 标记错误--补录订单
        if (orderInfo.resetOrder && orderInfo.id) {
          orderInfo.oldQuotationId = orderInfo.id
          orderInfo.transactionNo = orderInfo.transactionNo || orderInfo.outTradeNo
          orderInfo.transactionNo = orderInfo.transactionNo+','
          let result = await errorOrderById(orderInfo)
          return resolve(result)
        }
        // 编辑后重新提交
        if (orderInfo.id) {
          let result = await updateQuotation(orderInfo)
          console.log('result', result)
          // let { code, data } = result
          return resolve(result)
        } else {
          // 新增提交
          let result = await quotationAdd(orderInfo)
          let {
            code,
            data
          } = result
          // console.log('result', result)
          return resolve(result)
        }
      })
    } catch (error) {
      console.log(error)
    }
  },
  navigatorNext(obj) {
    wx.$router.redirectTo('CarQuotationPay', obj)
  },
  getOrderInfo() {
    let {
      id,
      orderInfo
    } = this.data
    // 先写死调试
    getQuotationInfoById({
        id
      })
      .then(result => {
        console.log('result', result)
        let {
          code,
          data: tableInfo
        } = result
        tableInfo.owner.price = this.divideByHundred(tableInfo.owner.price)
        // tableInfo.quotedPrice = this.divideByHundred(tableInfo.quotedPrice)
        tableInfo.insurancePrice = this.divideByHundred(tableInfo.insurancePrice)
        tableInfo.quotedPriceUpdate = this.divideByHundred(tableInfo.quotedPriceUpdate)
        tableInfo.vdQuotationGoodsList = tableInfo.vdQuotationGoodsList.map(item => {
          item.price = this.divideByHundred(item.goodsPrice)
          return item
        })
        if (code == 0) {
          tableInfo.vdQuotationGoodsList = tableInfo.vdQuotationGoodsList.map(item => {
            item.price = this.divideByHundred(item.goodsPrice)
            return item
          })
          //  标识为补录工单
          if (this.resetOrder) {
            orderInfo.resetOrder = this.resetOrder
            tableInfo.payType = 2
          }
          this.setData({
            orderInfo: Object.assign(orderInfo, tableInfo, {
              insureList: []
            }),
            owner: tableInfo.owner
            // ...insuranceTypeObj
          })
          this.getQuotationInfo()

        }
      })
      .catch(err => {})
  },
  totalFee() {
    let {
      orderInfo
    } = this.data
    let total = 0
    orderInfo.vdQuotationGoodsList.map(i => (total += parseFloat(i?.count || 1) * parseFloat(i.price)))

    this.setData({
      ['orderInfo.total']: wx.$util.decimal(total, 2)
    })
  },
  // 缩小100
  divideByHundredObj(obj) {
    let {
      needChangeList
    } = this.data
    for (const key in obj) {
      if (needChangeList.includes(key)) {
        obj[key] = this.divideByHundred(obj[key])
      }
    }
    return obj
  },
  // 放大100
  multipliedByHundredObj(obj) {
    console.log(obj, 'obj')
    let {
      needChangeList
    } = this.data
    for (const key in obj) {
      if (needChangeList.includes(key)) {
        obj[key] = this.multipliedByHundred(obj[key])
      }
    }
    return obj
  },
  // 不需要检验 存草稿
  async handleEditSave() {
    let {
      owner,
      orderInfo,
      saveStatus
    } = this.data
    // 补单点放弃
    if (orderInfo.outTradeNo || orderInfo.resetOrder) {
      return wx.$router.redirectTo('packageEquity.equityPage', {
        tabIndex: 1
      })
    }
    if (saveStatus) return false
    wx.showLoading('保存中...')
    this.setData({
      saveStatus: true
    })
    orderInfo.check = false
    orderInfo.owner = owner
    this.setData({
      orderInfo
    })
    let res = await this.handleSecondConfirmCheck(false)
    this.setData({
      saveStatus: false
    })
    console.log('id', res)
    let {
      code,
      data,
      message
    } = res
    if (code == 0) {
      wx.$router.redirectTo('packageEquity.equityPage', {
        tabIndex: 1
      })
    } else {
      return wx.$util.showToast(res.message || '保存错误')
    }
  },
  resetGoods() {
    this.setData({
      'orderInfo.vdQuotationGoodsList': []
    })
    // this.getQuotationInfo()
  },
  getQuotationInfo() {
    let {
      default4SStoreInfo
    } = wx.$util.getUserInfo()
    // console.log(default4SStoreInfo, 'default4SStoreInfo')
    // debugger
    let {
      owner: {
        price
      },
      orderInfo
    } = this.data
    listByMerchantIdAndIds({
        merchantId: default4SStoreInfo.merchantId,
        suitCarPrice: price ? parseFloat((price / 10000).toFixed(4)) : ''
      })
      .then(result => {
        console.log(result, 'result')
        // debugger
        let {
          data
        } = result

        // let { id: appreciation, goodsInfo, upgradeGoodsName: goodsName, upgradeGoodsServiceItemsList } = data[0]
        data = data.map(item => {
          // console.log(item.upgradeGoodsServiceItemsList,'item.upgradeGoodsServiceItemsList');
          let serveName
          if (item?.upgradeGoodsServiceItemsList && item.upgradeGoodsServiceItemsList.length) {
            serveName = item.upgradeGoodsServiceItemsList.reduce((label, ele, index) => {
              let unit = index == 0 ? '' : '/'
              // debugger
              label = label + unit + ele?.serviceName
              return label
            }, '')
          }
          if (item.position) {
            const fieldMap = {
              "4大件": "燃油车4大件服务",
              "3电": "新能源3电服务",
              "6大件": "新能源6大件服务",
              // 在这里可以添加更多的替换规则
            };
            // 如果输入字段在字段映射中有匹配项,则进行替换,否则保持不变
            fieldMap[item.position] && (serveName = fieldMap[item.position])
          }
          // console.log(serveName, 'serveName');
          return {
            ...item,
            serveName,
            appreciation: item?.appreciation,
            goodsName: item?.upgradeGoodsName,
            quotationGoodsServiceItemsRelationDTOList: item?.upgradeGoodsServiceItemsList,
            titlePictureUrl: item.goodsInfo?.thumbUrl
          }
        })
        this.setData({
          quotationGoodsList: data
        })
        // 判断有没有勾选商品,有的话调用一下
        orderInfo.vdQuotationGoodsList.length && this.totalFee()
      })
      .catch(err => {})
  },
  // 后面改成全局方法
  // 除以100保留两位小数点
  divideByHundred: str => {
    let floatVal = parseFloat(str)
    if (isNaN(floatVal)) {
      return 0
    }
    floatVal = Math.round(str * 100) / 10000
    let strVal = floatVal.toString()
    let searchVal = strVal.indexOf('.')
    if (searchVal < 0) {
      searchVal = strVal.length
      strVal += '.'
    }
    while (strVal.length <= searchVal + 2) {
      strVal += '0'
    }
    return parseFloat(strVal)
  },
  // 乘以100保留两位小数点
  multipliedByHundred: str => {
    let floatVal = parseFloat(str)
    if (isNaN(floatVal)) {
      return 0
    }
    floatVal = Math.round(str * 10000) / 100
    let strVal = floatVal.toString()
    let searchVal = strVal.indexOf('.')
    if (searchVal < 0) {
      searchVal = strVal.length
      strVal += '.'
    }
    while (strVal.length <= searchVal + 2) {
      strVal += '0'
    }
    return parseFloat(strVal)
  },
  back() {
    console.log('back')
    let {
      step,
      isClue
    } = this.data
    console.log('step', step)
    if (isClue) return wx.$router.back()
    if (step == 'first') {
      wx.$router.reLaunch('packageEquity.equityPage')
    } else {
      this.setData({
        step: 'first'
      })
    }
  },
  navbarReady(e) {
    let navBarHeight = e.detail.height
    this.setData({
      navBarHeight
    })
  },
  init() {},
  onRoute(options) {
    console.log(options, 'options')
    // 判断是编辑还是新增
    let {
      outTradeNo,
      totalFee,
      accountDate,
      id,
      resetOrder,
      isClue
    } = options
    // outTradeNo
    if (outTradeNo) {
      this.setData({
        outTradeNo,
        totalFee: totalFee,
        ['orderInfo.outTradeNo']: outTradeNo,
        accountDate
      })
    }
    if (resetOrder) this.resetOrder = resetOrder
    if (id) {
      this.setData({
        id
      })
      this.getOrderInfo()
    } else {
      this.getQuotationInfo()
    }
    this.setData({
      isClue
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const that = this
    wx.$eventbus.on('set_car_info', res => {
      that.setData({
        ['owner.carBrandId']: res.vehicleBrandId,
        ['owner.carBrand']: res.vehicleBrandName,
        ['owner.carChildsBrandId']: res.vehicleSubBrandId,
        ['owner.carChildsBrandName']: res.vehicleSubBrandName,
        ['owner.carSeriesId']: res.vehicleSeriesId,
        ['owner.carSeriesName']: res.vehicleSeriesName
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {}
})