summary.js 10.9 KB
// packageOutput/pages/summary/summary.js
import outputApi from '~/api/outputApi/index'
import { IMG_URL } from '~/utils/constantData'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    IMGURL: IMG_URL,
    sceneCodeList: ['SC001', 'SC002', 'SC003', 'SC004'],
    merchantInfo: {},
    sceneObj: {
      scene1: null,
      scene2: null,
      scene3: null,
      scene4: null
    },
    sceneActive: 1,
    scenePriceList: [{
      total: 0
    }, {
      total: 0
    }, {
      total: 0
    }, {
      total: 0
    }],
    // 活动内容
    scrollLeft: 0,
    activityActive: 0,
    activityList: [],
    sceneActivityObj: {
      scene1: [],
      scene2: [],
      scene3: [],
      scene4: []
    },
    // 产值
    searchForm: {
      scene2Idx: 0,
      scene3Idx: 0,
      scene4Idx: 0
    },
    baseData: {
      timeList: ['全部', '本月', '本日'],
      actStatusList: ['全部', '申请中', '活动中', '已结束']
    }
  },

  // 场景切换
  handleSceneChange(e) {
    var idx = e.detail.sceneIdx
    this.setData({
      sceneActive: idx,
      activityActive: 0
    })
    if (!this.data.sceneActivityObj['scene' + this.data.sceneActive].length) {
      this.getSceneProStatus(this.data.sceneCodeList[this.data.sceneActive - 1])
      return true
    }
    this.setCurActivity()
    // console.log('换内容---')
  },

  // 活动切换
  handleActChange(e) {
    var idx = e.currentTarget.dataset.idx
    this.setData({
      activityActive: idx
    })
    // this.getCustomerDetail()
    // console.log('换内容--')
  },

  setCurActivity() {
    this.setData({
      activityList: JSON.parse(JSON.stringify(this.data.sceneActivityObj['scene' + (this.data.sceneActive)]))
    })
    // activityActive为0时,触发置顶(左)
    if (this.data.activityActive == 0) {
      this.setData({
        scrollLeft: 0
      })
    }
  },

  // 搜索条件切换
  handleSearch() {
    wx.showActionSheet({
      itemList: this.data.sceneActive != 4 ? this.data.baseData.timeList : this.data.baseData.actStatusList,
      success: res => {
        this.setData({
          ['searchForm.scene' + this.data.sceneActive + 'Idx']: res.tapIndex
        })
        this.data.sceneActive != 4 ? this.getCustomerDetail(false, false) : this.getActivityRevenue()
      }
    })
  },

  handleJump(e) {
    // console.log(this.data.activityList[this.data.activityActive])
    var params = {
      merchantInfo: this.data.merchantInfo,
      sceneActive: this.data.sceneActive
      // sceneCodeList: this.data.sceneCodeList
    }
    wx.$router.go('packageOutput.openBusiness', params)
  },

  getSceneProStatus(sceneCode, isFirst = false) {
    var params = {
      serviceMerchantId: this.data.merchantInfo.pMerchantId,
      merchant4sId: this.data.merchantInfo.merchantId,
      sceneCode: sceneCode
    }
    outputApi.getSceneProStatus(params).then(res => {
      // var proList = res.find(item => item.sceneCode == sceneCode)
      // 删---
      // proList.productList[1].productStatus = 2
      this.setData({
        ['sceneActivityObj.scene1']: res[0].productList,
        ['sceneActivityObj.scene2']: res[1].productList,
        ['sceneActivityObj.scene3']: res[2].productList,
        ['sceneActivityObj.scene4']: res[3].productList
      })
      if ([2, 3].includes(+this.data.sceneActive)) {
        this.getCustomerDetail()
      } else if (this.data.sceneActive == 4) {
        this.getActivityRevenue()
      } else {
        if (isFirst) {
          this.getCustomerDetail(isFirst)
          this.getActivityRevenue(isFirst)
        }
        this.setCurActivity()
      }
    })
  },

  dealSearchTime() {
    var params = {}
    switch (this.data.searchForm['scene' + this.data.sceneActive + 'Idx']) {
      // 本月
      case 1:
        params = {
          startTime: new Date(new Date().setDate(1)).toLocaleDateString().replace(/\//g, '-') + ' 00:00:00',
          endTime: new Date().toLocaleDateString().replace(/\//g, '-') + ' 23:59:59'
        }
        break
      // 本日
      case 2:
        params = {
          startTime: new Date().toLocaleDateString().replace(/\//g, '-') + ' 00:00:00',
          endTime: new Date().toLocaleDateString().replace(/\//g, '-') + ' 23:59:59'
        }
        break
    }
    return params
  },

  /** 1.1和1.2场景产品数据处理
   * 调同一个接口,返回两个服务下面的全部产品的数据
   */
  getCustomerDetail(isFirst = false, isChangeOtherTotal = true) {
    var params = this.dealSearchTime()
    outputApi.getCustomerDetail({ merchantId: this.data.merchantInfo.merchantId, ...params }).then(res => {
      var actList = this.data.sceneActivityObj['scene' + (isFirst ? 2 : this.data.sceneActive)]
      var actCodeList = actList.map(item => item.productCode)
      var curTotal = 0
      var otherTotal = 0
      var otherScene = isFirst ? 2 : (this.data.sceneActive == 2 ? 3 : 2)
      res.forEach(item => {
        var idx = actCodeList.findIndex(i => i == item.productCode)
        if (idx != -1) {
          var orderTotal = item.orderTotal
          if (item.orderTotal) {
            item.orderTotal.calCustomerUnitPrice = Math.round(orderTotal.totalSubOrderOrderMoney / orderTotal.upgradeCount).toFixed(0)
            item.orderTotal.calExperience = Math.round((orderTotal.experienceCount * 100) / orderTotal.toShopCount).toFixed(0)
            item.orderTotal.calTransform = Math.round((orderTotal.upgradeCount * 100) / orderTotal.experienceCount).toFixed(0)
          }
          actList[idx] = {
            ...actList[idx],
            orderTotal: item.orderTotal
          }
          curTotal = curTotal + (item.orderTotal?.totalSubOrderOrderMoney || 0)
        } else {
          otherTotal = otherTotal + (item.orderTotal?.totalSubOrderOrderMoney || 0)
        }
      })
      if (!isFirst) {
        this.setData({
          ['sceneActivityObj.scene' + this.data.sceneActive]: actList
        })
      }
      // 总金额
      this.setData({
        ['scenePriceList[' + (isFirst ? 1 : (this.data.sceneActive - 1)) + '].total']: curTotal
      })
      if(isChangeOtherTotal) {
        this.setData({
          ['scenePriceList[' + (isFirst ? 2 : (otherScene - 1)) + '].total']: otherTotal
        })
      }
      !isFirst && this.setCurActivity()
    })
  },

  dealSearchStatus() {
    var params = {}
    switch (this.data.searchForm['scene' + this.data.sceneActive + 'Idx']) {
      case 0:
        params.activityStatuList = [1, 2]
        break
      // 申请中
      case 1:
        params.activityStatuList = [1]
        break
      // 活动中
      case 2:
        params.activityStatuList = [2]
        break
      // 已结束
      case 3:
        params.activityStatuList = [3]
        break
    }
    return params
  },

  /** 查询场景4的产值 */
  getActivityRevenue(isFirst = false) {
    !isFirst && wx.showLoading()
    var params = this.dealSearchStatus()
    outputApi.getActivityRevenue({ merchantId: this.data.merchantInfo.merchantId, ...params }).then(res => {
      // console.log(res)
      // console.log(this.data.sceneActivityObj, '++++++')
      var actList = this.data.sceneActivityObj['scene' + (isFirst ? 4 : this.data.sceneActive)]
      var total = 0
      res.forEach(item => {
        total = total + item.totalAmount
      })
      actList.forEach((item, idx) => {
        var curItem = res.find(k => k.goodsCategoryCode == item.productCode)
        if (curItem) {
          curItem.totalNum = curItem.activityList.length
          curItem.activityList.forEach(k => {
            // 删---
            // var list = k.staffList
            // for (var i = 0; i < 10; i++) {
            //   k.staffList = k.staffList.concat(list)
            // }
            // 时间
            k.time = k.startTime.slice(5).replace(/-/g, '/') + '~' + k.endTime.slice(5).replace(/-/g, '/')
            // 0-未开始;1-活动中;2-已结束
            if (new Date(k.startTime) > new Date()) {
              k.actStatus = 0
              k.interDays = parseInt((new Date(k.startTime).getTime() - new Date().getTime()) / (1000 * 60 * 60 * 24)) + 1
            } else if (new Date(k.startTime) < new Date() < new Date(k.endTime)) {
              k.actStatus = 1
              k.interDays = parseInt((new Date().getTime() - new Date(k.startTime).getTime()) / (1000 * 60 * 60 * 24)) + 1
              k.toEndDays = parseInt((new Date(k.endTime).getTime() - new Date().getTime()) / (1000 * 60 * 60 * 24)) + 1
            } else if (new Date(k.endTime) < new Date()) {
              k.actStatus = 2
              k.interDays = parseInt((new Date(k.endTime).getTime() - new Date().getTime()) / (1000 * 60 * 60 * 24)) + 1
            }
            k.suggestTxtList = []
            k.suggestList = []
            if (k.actStatus == 1) {
              if (k.visitCount < 200) {
                if ((k.actStatus == 2 && k.interDays < 8) || k.actStatus != 2) {
                  k.suggestTxtList.push('距离活动结束剩余' + k.toEndDays + '天')
                }
                k.suggestList.push('触达车主的数量太少,建议与4S店关键人沟通下注册员工的转发执行情况')
              }
              if (k.shareRate < 0.4) {
                k.suggestTxtList.push('分享员工占比低于40%')
                k.suggestList.push('建议与4S店关键人沟通下注册员工的转发执行情况')
              }
              if (k.conversionRate < 0.05) {
                k.suggestTxtList.push('点击转化率低于5%')
                k.suggestList.push('建议调整该产品的品类话术和营销内容')
              }
              k.suggestTxt = k.suggestTxtList.join(',') + '。'
            }
          })
          actList[idx].outputData = curItem
        } else {
          actList[idx].outputData = []
        }
      })
      if (!isFirst) {
        this.setData({
          ['sceneActivityObj.scene' + this.data.sceneActive]: actList
        })
      }
      this.setData({
        ['scenePriceList[3].total']: total
      })
      !isFirst && this.setCurActivity()
    }).finally(() => {
      wx.hideLoading()
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    // 删---
    // this.setData({
    //   merchantInfo: {
    //     pMerchantId: 44209845,
    //     merchantId: 44209390,
    //     merchantName: '????'
    //   },
    //   sceneCodeList: ['SC001', 'SC002', 'SC003', 'SC004']
    // })
    // this.getSceneProStatus(this.data.sceneCodeList[this.data.sceneActive - 1])

    wx.$router.getData(obj => {
      var scene = {}
      Object.keys(this.data.sceneObj).forEach(item => {
        scene[item] = obj[item]
      })
      this.setData({
        merchantInfo: obj,
        sceneObj: scene,
        sceneCodeList: obj.sceneCodeList
      })

      this.getSceneProStatus(this.data.sceneCodeList[this.data.sceneActive - 1], true)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    wx.setNavigationBarTitle({
      title: this.data.merchantInfo.merchantName,
    })
  }
})