customerViewCtrl.js 17 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
(function (angular) {
  function customerViewCtrl($scope, $rootScope, $state, toaster, routeConfig, customerService, DTColumnBuilder, DTOptionsBuilder, $uibModal, $http, Utils) {
    // 跳转 活动列表 保养线索 流失线索 拦截
    $scope.activityFlag = false

    function checkPermission(pageTitle, data, name) {
      angular.forEach(data, function (e) {
        if (e.children) {
          checkPermission(pageTitle, e.children, name)
        } else {
          if (e.pageTitle == pageTitle) {
            $scope[name] = true
          }
        }
      })
    }
    $scope.jumpLink = function (name, target) {
      checkPermission(name, $scope.menuList, target)
      if ($scope[target]) {
        //跳往一件营销
        $scope.jumpActivity()
      } else {
        toaster.pop({
          type: 'error',
          body: '您暂未开通该服务,请联系客服处理',
          showCloseButton: true,
          timeout: 3000
        })
      }
    }
    // 页面数据-start
    $scope.custTotal = '--';

    // 出错数据
    var eqpErrorData = {
      shopCount: '--',
      haveDeviceCount: '--',
      noDeviceCount: '--',
      haveDevicePercentage: '--',
      outerImportCount: '--',
      noDevicePercentage: '--'
    }

    var onlineErrorData = {
      online: '--',
      onlinePercent: '--',
      offonline: '--',
      offonlinePercent: '--'
    }

    var locationErrorData = {
      twentyKilometreCount: '--',
      twentyPercentage: '--',
      tenToTwentyKilometreCount: '--',
      tenToTwentyPercentage: '--',
      fiveToTenKilometreCount: '--',
      fiveToTenPercentage: '--',
      fiveKilometreCount: '--',
      fivePercentage: '--'
    }


    // 设备
    $scope.eqpG2Data = [{
      name: '有设备客户',
      value: 0,
      percent: 0,
      valueAttr: 'haveDeviceCount',
      percentAttr: 'haveDevicePercentage'
    }, {
      name: '无设备客户',
      value: 0,
      percent: 100,
      valueAttr: 'noDeviceCount',
      percentAttr: 'noDevicePercentage'
    }];

    // 客户在线率
    $scope.onlineG2Data = [{
      name: '在线率',
      value: 0,
      percent: 0,
      valueAttr: 'online',
      percentAttr: 'onlinePercent'
    }, {
      name: '离线率',
      value: 0,
      percent: 100,
      valueAttr: 'offonline',
      percentAttr: 'offonlinePercent'
    }];

    // 1-广联;2-广汇
    $scope.Gl1Gh2 = $rootScope.loginUser.channelConfig.channelType

    // 客户常驻地-data
    $scope.locationG2Color = ['#01C292', '#3090FF', '#00B0FF', '#FEAB00'];
    $scope.localTotal = '--';
    $scope.locationG2Data = [{
      name: '20公里以外',
      value: 0,
      percent: 0,
      valueAttr: 'twentyKilometreCount',
      percentAttr: 'twentyPercentage'
    }, {
      name: '10~20公里',
      value: 0,
      percent: 0,
      valueAttr: 'tenToTwentyKilometreCount',
      percentAttr: 'tenToTwentyPercentage'
    }, {
      name: '5~10公里',
      value: 0,
      percent: 0,
      valueAttr: 'fiveToTenKilometreCount',
      percentAttr: 'fiveToTenPercentage'
    }, {
      name: '5公里以内(返店意愿高)',
      value: 0,
      percent: 0,
      valueAttr: 'fiveKilometreCount',
      percentAttr: 'fivePercentage'
    }];
    // 页面数据-end

    // 初始获取页面数据-start
    function getArr(val, res) {
      for (i in $scope[val]) {
        $scope[val][i].value = res[$scope[val][i].valueAttr];
        // $scope[val][i].percent && ($scope[val][i].percent = res[$scope[val][i].percentAttr]);
        $scope[val][i].percent = res[$scope[val][i].percentAttr];
      }
    }

    function loadOnline(isOk = true) {
      if (isOk) {
        initG2('onlineG2', 'onlineG2Data', {
          percent: percent
        }, 60, [0, 0], 'theta', thetaRadius);
        $scope.onlineG2.render();
      } else {
        getArr('onlineG2Data', onlineErrorData);
        errorG2('onlineG2', {
          percent: percent
        }, 60, [0, 0], 'theta', thetaRadius);
        $scope.onlineG2.render();
      }
    }

    // 设备;客户来源
    customerService.getMerchantCustomerData(function (res) {
      if (res.returnCode == '0') {
        $scope.custTotal = res.data ? res.data.merchantCustomerCount.toString().replace(/(\d)(?=(\d{3})+$)/g, '$1,') : '0';
        if (res.data) {
          getArr('eqpG2Data', res.data);
          getArr('sourceG2Data', res.data);
        }
        // 设备-G2
        initG2('eqpG2', 'eqpG2Data', {
          percent: percent
        }, 60, [0, 0], 'theta', thetaRadius);
        $scope.eqpG2.render();

        $scope.sourceG2.render();
        // 客户在线率
        // 1-广联;2-广汇
        if ($scope.Gl1Gh2 == 2) {
          customerService.getDeviceOnlineNum(function (result) {
            console.log(result)
            if (result.returnCode == '0') {
              $scope.onlineG2Data[0].value = result.data.onlineActiveDevice;
              $scope.onlineG2Data[0].percent = $scope.eqpG2Data[0].value ? Math.round((result.data.onlineActiveDevice / $scope.eqpG2Data[0].value) * 10000) / 100 : 0;
              $scope.onlineG2Data[1].value = $scope.eqpG2Data[0].value - result.data.onlineActiveDevice;
              $scope.onlineG2Data[1].percent = Math.round((10000 - $scope.onlineG2Data[0].percent * 100)) / 100;
              // 客户在线率-G2
              $scope.onlineG2Data[0].value ? loadOnline() : loadOnline(false)
            } else {
              loadOnline(false)
            }
          });
        }
      } else {
        getArr('eqpG2Data', eqpErrorData);
        errorG2('eqpG2', {
          percent: percent
        }, 60, [0, 0], 'theta', {});
        $scope.eqpG2.render();
        $scope.sourceG2.render();
        loadOnline(false)
      }
    });

    // 客户常驻地
    customerService.getCarPermanentData(function (res) {
      console.log(res)
      if (res.returnCode == '0') {
        // console.log(res.data)
        $scope.localTotal = res.data ? res.data.carPermanentCount.toString().replace(/(\d)(?=(\d{3})+$)/g, '$1,') : 0;
        res.data && getArr('locationG2Data', res.data);

        // 客户常驻地-G2
        if ($scope.localTotal) {
          loadLocation();
        } else {
          loadLocation(false);
        }
      } else {
        getArr('locationG2Data', locationErrorData);
        loadLocation(false)
      }
    });

    function loadLocation(isOk = true) {
      if (isOk) {
        initG2('locationG2', 'locationG2Data', {
          percent: percent
        }, 200, [10, 0], 'theta', thetaRadius, $scope.locationG2Color);
      } else {
        errorG2('locationG2', {
          percent: percent
        }, 200, [10, 0], 'theta', thetaRadius);
      }
      $scope.locationG2.guide().html({
        position: ['50%', '50%'],
        html: '<div class="g2-value">' + $scope.localTotal + '<div class="g2-title">总人数' + '</div></div>',
        alignX: 'middle',
        alignY: 'middle'
      });
      $scope.locationG2.render();
    }

    //客户来源
    $scope.sourceG2Data = [{
      name: '小程序客户',
      value: 0,
      valueAttr: 'webAppOpenIdCount'
    }, {
      name: '到店客户',
      value: 0,
      valueAttr: 'shopCount'
    }, {
      name: '导入客户',
      value: 0,
      valueAttr: 'outerImportCount'
    }];
    // 初始获取页面数据-end

    // init-G2-start
    var thetaRadius = {
      radius: 1,
      innerRadius: 0.75
    }

    var percent = {
      formatter: function formatter(val) {
        val = val + '%';
        return val;
      }
    }

    function initG2(id, sourceData, sourceOptions = {}, height = window.innerHeight, padding, coordType, coordOptions = {}, color = ['#00A479', '#E4E4E4']) {
      let item = {
        container: id,
        forceFit: true,
        height: height,
        options: {
          legends: false,
          tooltip: {
            showTitle: false
          }
        }
      }
      padding && (item.padding = padding)
      $scope[id] = new G2.Chart(item);
      $scope[id].source($scope[sourceData], sourceOptions);
      $scope[id].coord(coordType, coordOptions);
      coordType == 'theta' && $scope[id].intervalStack().position('percent').color('name', color);
    }

    function errorG2(id, sourceOptions = {}, height = window.innerHeight, padding, coordType, coordOptions = {}) {
      var errorArr = [{
        name: '出错',
        value: 0,
        percent: 100,
      }]
      let item = {
        container: id,
        forceFit: true,
        height: height,
        options: {
          legends: false,
          tooltip: false
        }
      }
      padding && (item.padding = padding)
      $scope[id] = new G2.Chart(item);
      $scope[id].source(errorArr, sourceOptions);
      $scope[id].coord(coordType, coordOptions);
      coordType == 'theta' && $scope[id].intervalStack().position('percent').color('name', ['#CFD3D5']);
    }
    // init-G2-end

    // G2
    // 设备-G2
    // initG2('eqpG2', 'eqpG2Data', {
    //   percent: percent
    // }, 60, [0, 0], 'theta');
    // 客户在线率-G2
    // initG2('onlineG2', 'onlineG2Data', {
    //   percent: percent
    // }, 60, [0, 0], 'theta', thetaRadius);
    // 客户常驻地-G2
    // initG2('locationG2', 'locationG2Data', {
    //   percent: percent
    // }, 240, [10, 0], 'theta', thetaRadius, $scope.locationG2Color);
    // 客户来源-G2
    initG2('sourceG2', 'sourceG2Data', {}, 280, [60, 100], 'rect');
    $scope.sourceG2.scale('value', {
      nice: true,
      minTickInterval: 2
    })
    $scope.sourceG2.interval().position('name*value').color('name', ['#14BA8E']).size('24').label('value', {
      formatter: function (val) {
        return val + '人'
      }
    });

    // 表格
    // 搜索项
    $scope.searchForm = [{
      label: '累计领取优惠券',
      firstAttr: 'receivedCouponNumMin',
      secondAttr: 'receivedCouponNumMax'
    }, {
      label: '累计订单数',
      firstAttr: 'successedOrderNumMin',
      secondAttr: 'successedOrderNumMax'
    }, {
      label: '累计核销优惠券',
      firstAttr: 'verifiedCouponNumMin',
      secondAttr: 'verifiedCouponNumMax'
    }, {
      label: '累计消费金额',
      firstAttr: 'successedOrderPaidMin',
      secondAttr: 'successedOrderPaidMax'
    }]
    // formData
    $scope.searchData = {
      receivedCouponNumMin: '',
      receivedCouponNumMax: '',
      successedOrderNumMin: '',
      successedOrderNumMax: '',
      verifiedCouponNumMin: '',
      verifiedCouponNumMax: '',
      successedOrderPaidMin: '',
      successedOrderPaidMax: '',
      selectCondition: '',
      hasOrderd: '',
      hasBindedWeapp: '',
      factoryMerchantId:'',
      provinceMerchantId:'',
      cityMerchantId:'',
      shopMerchantId:'',
    };
    //  配置table列
    $scope.dtColumns = [
      DTColumnBuilder.newColumn('userName').withOption('width', 120).withOption('ellipsis', true).withClass('text-center').withTitle('客户姓名'),
      DTColumnBuilder.newColumn('phone').withOption('width', 100).withClass('text-center').withTitle('客户手机号'),
      DTColumnBuilder.newColumn('merchantName').withOption('width', 200).withOption('ellipsis', true).withClass('text-center').withTitle('所属商户'),
      DTColumnBuilder.newColumn('merchantId').withOption('width', 80).withOption('ellipsis', true).withClass('text-center').withTitle('所属商户号'),
      DTColumnBuilder.newColumn('gender').withOption('width', 80).withOption('ellipsis', true).withClass('text-center').withTitle('性别')
      .renderWith(function (e, dt, rowData, config) {
        return {
          1: '男',
          2: '女'
        } [rowData.gender] || '--'
      }),
      DTColumnBuilder.newColumn('standNo').withOption('width', 120).withClass('text-center').withTitle('车架号'),
      DTColumnBuilder.newColumn('receivedCouponNum').withOption('width', 120).withClass('text-center').withTitle('累计领取优惠券')
      .renderWith(function (e, dt, rowData, config) {
        return rowData.receivedCouponNum || 0
      }),
      DTColumnBuilder.newColumn('verifiedCouponNum').withOption('width', 120).withClass('text-center').withTitle('累计核销优惠券')
      .renderWith(function (e, dt, rowData, config) {
        return rowData.verifiedCouponNum || 0
      }),
      DTColumnBuilder.newColumn('successedOrderNum').withOption('width', 120).withClass('text-center').withTitle('累计订单数')
      .renderWith(function (e, dt, rowData, config) {
        return rowData.successedOrderNum || 0
      }),
      DTColumnBuilder.newColumn('successedOrderPaid').withOption('width', 120).withClass('text-center').withTitle('累计消费金额')
      .renderWith(function (e, dt, rowData, config) {
        return rowData.successedOrderPaid || 0
      }),
      DTColumnBuilder.newColumn('sourcesDetail').withOption('width', 120).withClass('text-center').withTitle('客户来源明细'),
      DTColumnBuilder.newColumn('createdDate').withOption('width', 120).withClass('text-center').withTitle('注册时间/提单时间')
      .renderWith(function (e, dt, rowData, config) {
        return rowData.createdDate || '--'
      }),
      DTColumnBuilder.newColumn('lastLoginTime').withOption('width', 120).withClass('text-center').withTitle('最后活跃时间')
      .renderWith(function (e, dt, rowData, config) {
        return rowData.lastLoginTime || '--'
      }),
    ];

    if (!$rootScope.loginUser.isSuper) {
      $scope.dtColumns[2].sClass = 'display-none';
      $scope.dtColumns[3].sClass = 'display-none';
    }

    //  配置dataTables选项
    $scope.dtOptions = DTOptionsBuilder.newOptions()
      .withOption('num', true) // 序号
      .withFixedColumns({
        leftColumns: 3,
        rightColumns: 0
      })
    $scope.dtInstance = {
      serverData: customerService.getCustomerList
    };

    // 操作
    $scope.addFlag = false
    $scope.handleAdd = function () {
      // 1-广联;2-广汇
      var jump = $scope.Gl1Gh2 != 2 ? 'business.ysAdd' : ['business.add', 'business.addQuRong', 'business.diAdd'];
      var jumpTitle = $scope.Gl1Gh2 != 2 ? '新增服务订单' : ['新增服务订单(广联)', '新增服务订单(QR援疆)', '新增服务订单(迪迪)'];
      var index = -1;
      if (typeof jumpTitle == 'object') {
        for (var i in jumpTitle) {
          checkPermission(jumpTitle[i], $scope.menuList, 'addFlag');
          if ($scope.addFlag) {
            index = i;
            break;
          }
        }
      } else {
        checkPermission(jumpTitle, $scope.menuList, 'addFlag');
      }
      if ($scope.addFlag) {
        // $scope.Gl1Gh2 == 1 ? $state.go(jump) : $state.go(jump[index]);
        localStorage.setItem('customerStaus', $scope.Gl1Gh2 != 2 ? jump : jump[index])
        localStorage.setItem('isNewWindow', 0)
        window.open('#/home/index', '_blank')
      } else {
        toaster.pop({
          type: 'error',
          body: '您暂未开通该服务,请联系客服处理',
          showCloseButton: true,
          timeout: 3000
        })
      }
    }

    $scope.handleImport = function () {
      $uibModal.open({
        templateUrl: 'biz/customer/view/customerImportModal.html',
        controller: 'customerImportModalCtrl',
        size: 'md',
        scope: $scope,
        resolve: {
          importConfig: function () {
            return {
              pageTitle: '客户录入', //弹框标题
              ajaxUrl: 'MerchantCustomer/importMerchantCustomer', //导入服务地址
              downloadType: '16', //下载导入模板类型
              importTemplate: '客户录入-模版', //下载导入模板文件名
              errorDown: '客户录入报错下载.xlsx'
            };
          }
        }
      }).result.then(function () {
        //关闭弹框后执行方法
      }, function () {
        $scope.dtInstance.query();
      });
    }

    $scope.handleSearch = function () {
      $scope.dtInstance.query($scope.searchData);
    }

    $scope.handleReset = function () {
      for (item in $scope.searchData) {
        $scope.searchData[item] = ''
      }
      $scope.searchData.factoryMerchantId = null;
      $scope.searchData.provinceMerchantId = null;
      $scope.searchData.cityMerchantId = null;
      $scope.searchData.shopMerchantId = null;
      $scope.$broadcast("reset");
      $scope.dtInstance.query();
    }

    $scope.handleExport = function () {
      $http({
        url: 'MerchantCustomer/exportCustomerList',
        method: "GET", //接口方法
        params: $scope.searchData,
        headers: {
          'Content-type': 'application/json'
        },
        responseType: 'arraybuffer'
      }).success(function (data, status, headers, config) {
        var blob = new Blob([data], {
          type: "application/vnd.ms-excel"
        });
        var objectUrl = URL.createObjectURL(blob);
        var a = document.createElement('a');
        document.body.appendChild(a);
        a.setAttribute('style', 'display:none');
        a.setAttribute('href', objectUrl);
        fileName = '客户总览' + ".xlsx";
        a.setAttribute('download', fileName);
        a.click();
        URL.revokeObjectURL(objectUrl);
      }).error(function (data, status, headers, config) {});
    }
  }

  angular.module('customer').controller('customerViewCtrl', customerViewCtrl);
})(angular);