residenceAnalysis.js 18.2 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
(function (angular) {
    function residenceAnalysis($scope, customerService, localStorageService, DTOptionsBuilder, DTColumnBuilder, $location, $compile, $rootScope, Utils, $uibModal, $filter, SweetAlertX) {
        /**
         * 定义常量
         * */
        $scope.warnInfoBox = false;
        var curMerchantId = $rootScope.loginUser.merchantId;
        var checkFlag = localStorageService.get('address.check' + curMerchantId);
        if ("Y" != checkFlag) {
            customerService.getAddressCheck(function (response) {
                if (response.returnCode == 0) {
                    if (response.data) {
                        $scope.warnInfoBox = false;
                        localStorageService.set('address.check' + curMerchantId, "Y");
                    } else {
                        $scope.warnInfoBox = true;
                    }
                } else {
                    $scope.warnInfoBox = true;
                }
            });
        }

        $scope.hasAddress = true;
        $scope.mid = null;
        customerService.getUserMerchant(function (response) {
            if (response.returnCode == 0) {
                $scope.mid = response.data.detail.id;
                if (response.data.address == null || response.data.address == "") {
                    $scope.hasAddress = false;
                }
            }
        });
        $scope.merchant = {
            lat: '',
            lng: '',
            cityName: '',
            provinceName: '',
            areaName: '',
            address: ''
        }

        $scope.totalItems = 0;
        $scope.currentPage = 1;
        $scope.totalPage = '';
        $scope.maxSize = 2000;
        $scope.pageType = 0;

        $scope.displayTable = true;
        $scope.timePicked = '4';
        $scope.timePick = function (timePicked) {
            this.timePicked = timePicked;
            $scope.tableSearch();

        };
        $scope.loginUserUserTypeCss = '';
        $scope.ngModelFlag = false;

        $scope.ngModel = true;
        $scope.isNgModel = true;

        //常驻地标点
        var LngLat = [];
        $scope.carPermanentInfo = [];
        $scope.dtColumns = [
            DTColumnBuilder.newColumn('name').withOption('width', '10%').withTitle('姓名'),
            DTColumnBuilder.newColumn('standNo').withOption('width', '20%').withTitle('车架号'),
            DTColumnBuilder.newColumn('sn').withOption('width', '20%').withTitle('设备号'),
            DTColumnBuilder.newColumn('mobile').withOption('width', '20%').withTitle('手机号'),
            DTColumnBuilder.newColumn('distance').withOption('width', '17%').withTitle('常驻地到门店距离'),
            DTColumnBuilder.newColumn('azimuthDesc').withOption('width', 101).withTitle('方位')
        ];
        $scope.dtOptions = DTOptionsBuilder.newOptions().withOption('num', true);

        $scope.dtInstance = {
            serverData: customerService.getCarPermanentInfo
        };

        // 这边定义一个 init 方法
        $scope.umcInstance = {
            init: function () {
                // $scope.umcInstance.setMapFeatures();
                var shopPoint, info;
                //点
                customerService.getLngLatShop(function (response) {
                    if (response.data && response.data.lng) {
                        if ($rootScope.loginUser.merchantUserType == 2) {
                            $scope.loginUserUserTypeCss = 'display: flex;';
                            $scope.isShop = true;
                            $scope.ngModel = true;
                            $scope.isNgModel = true;
                            $scope.isLineXY = function () {
                                if (!$scope.ngModel) {
                                    $scope.isNgModel = false
                                } else {
                                    $scope.isNgModel = true
                                }
                            };
                            if (null != response.data.lng && null != response.data.lat) {
                                var lngLatShopStr = response.data.lng + "," + response.data.lat;
                                shopPoint = lngLatShopStr;
                                info = response.data.store_name;
                                //drawXY()
                            }
                            $scope.umcInstance.residenceCircle(shopPoint, info, $scope, true); // 为true显示范围,为false不显示范围
                        } else if ($rootScope.loginUser.merchantUserType == 3) {
                            customerService.getChildMerchant(function (res) {
                                $scope.isShop = true;
                                $scope.ngModel = true;
                                if (res.returnCode == 0) {
                                    if (res.data == null || res.data.length == 0) {
                                        $scope.loginUserUserTypeCss = 'display: flex;';
                                        $scope.isShop = true;
                                        if (null != response.data.lng && null != response.data.lat) {
                                            var lngLatShopStr = response.data.lng + "," + response.data.lat;
                                            shopPoint = lngLatShopStr;
                                            info = response.data.store_name;
                                            //drawXY()
                                        }
                                        $scope.umcInstance.residenceCircle(shopPoint, info, $scope, true); // 为true显示范围,为false不显示范围
                                    } else {
                                        $scope.loginUserUserTypeCss = 'display: none;';
                                        $scope.isShop = false;
                                        $scope.ngModel = false;
                                        $scope.isNgModel = false;
                                        if (null != response.data.lng && null != response.data.lat) {
                                            var lngLatShopStr = response.data.lng + "," + response.data.lat;
                                            shopPoint = lngLatShopStr;
                                            info = response.data.store_name;
                                            //drawXY()
                                        }
                                        $scope.umcInstance.residenceCircle(shopPoint, info, $scope, false);
                                    }
                                } else {
                                    $scope.isShop = false;
                                    $scope.ngModel = false;
                                    $scope.isNgModel = false;
                                    if (null != response.data.lng && null != response.data.lat) {
                                        var lngLatShopStr = response.data.lng + "," + response.data.lat;
                                        shopPoint = lngLatShopStr;
                                        info = response.data.store_name;
                                    }
                                    $scope.umcInstance.residenceCircle(shopPoint, info, $scope, false);
                                }

                                $scope.isLineXY = function () {
                                    if (!$scope.ngModel) {
                                        $scope.isNgModel = false
                                    } else {
                                        $scope.isNgModel = true
                                    }
                                };
                            })
                        } else {
                            $scope.loginUserUserTypeCss = 'display: none;';
                            $scope.isShop = false;
                            $scope.ngModel = false;
                            $scope.isNgModel = false;
                            $scope.isLineXY = function () {
                                if (!$scope.ngModel) {
                                    $scope.isNgModel = false
                                } else {
                                    $scope.isNgModel = true
                                }
                            };
                            if (null != response.data.lng && null != response.data.lat) {
                                var lngLatShopStr = response.data.lng + "," + response.data.lat;
                                shopPoint = lngLatShopStr;
                                info = response.data.store_name;
                                //drawXY()
                            }
                            $scope.umcInstance.residenceCircle(shopPoint, info, $scope, false);
                        }
                    }
                });

                //画点
                getHotPointInfo();
                //getCarPermanentInfoWay();

                $scope.tableSearch = function () {
                    $scope.currentPage = 1;
                    LngLat = [];
                    $scope.carPermanentInfo = [];
                    $scope.umcInstance.clearMarker();
                    getHotPointInfo();
                    var params = getParams();
                    params = paramsChange(params);
                    $scope.dtInstance.query(params);
                    //$scope.doMore = new scrollLoadData();
                    // getCarPermanentInfoWay();
                };
            }
        };

        $scope.warnInfoBoxHidden = function () {
            $scope.warnInfoBox = false;
            var mid = $rootScope.loginUser.merchantId;
            localStorageService.set('address.check' + mid, "Y");
            customerService.addAddressCheck(function (response) {
                location.reload();
            });
        }

        function open(url, controller, defaultData, result, size) {
            $uibModal.open({
                templateUrl: url,
                controller: controller,
                size: size || 'md',
                resolve: {
                    data: () => {
                        return {
                            defaultData
                        }
                    }
                }
            }).result.then(result)
        }
        $scope.openMapDialog = function () {
            var position = {
                lat: $scope.merchant.lat,
                lng: $scope.merchant.lng
            }
            var oAddr = {
                city: $scope.merchant.cityName,
                province: $scope.merchant.provinceName,
                district: $scope.merchant.areaName,
                address: $scope.merchant.address,
                mid: $scope.mid,
                position: position
            }
            open('biz/AIniche/view/chooseAddress.html', 'chooseAddress', {
                oAddress: oAddr
            }, function (oAddr) {
                $scope.merchant.cityName = oAddr.city;
                $scope.merchant.provinceName = oAddr.province;
                $scope.merchant.areaName = oAddr.district;
                $scope.merchant.lat = oAddr.position.lat;
                $scope.merchant.lng = oAddr.position.lng;
                $scope.merchant.address = oAddr.address; //concatAddr(province,city,district,address)

                var obj = {};
                obj.merchantId = $rootScope.loginUser.merchantId;
                obj.address = oAddr.address;
                obj.lat = oAddr.position.lat;
                obj.lng = oAddr.position.lng;
                obj.id = $scope.mid;
                customerService.addOrUpdateMerchant(obj, function (res) {
                    if (res.returnCode == 0) {
                        swal({
                            type: 'success',
                            title: '',
                            html: true,
                            text: '操作成功',
                            timer: 1000
                        });
                        $uibModalInstance.close('cancel');
                    } else {
                        swal({
                            type: 'error',
                            title: '',
                            html: true,
                            text: '操作失败 ' + (res.message || ''),
                            confirmButtonText: "确定"
                        });
                    }
                });

                $scope.hasAddress = true;
                $scope.warnInfoBox = false;
                var mid = $rootScope.loginUser.merchantId;
                localStorageService.set('address.check' + mid, "Y");
                customerService.addAddressCheck(function (response) {
                    location.reload();
                });
            }, 'lg')
        };

        $scope.getAddress = function () {
            customerService.getUserMerchant(function (response) {
                if (response.returnCode == 0) {
                    $scope.mid = response.data.detail.id;
                    $scope.merchant.lat = response.data.detail.lat;
                    $scope.merchant.lng = response.data.detail.lng;
                    $scope.merchant.cityName = response.data.detail.cityName;
                    $scope.merchant.provinceName = response.data.detail.provinceName;
                    $scope.merchant.areaName = response.data.detail.areaName;
                    $scope.merchant.address = response.data.detail.address;

                    customerService.addAddressCheck(function (response) {
                        var mid = $rootScope.loginUser.merchantId;
                        localStorageService.set('address.check' + mid, "Y");
                        $scope.warnInfoBox = false;
                    });
                    $scope.openMapDialog();
                }
            });
        }

        $scope.gotoCreateActivity = function () {
            $location.path('/marketing/coupon/createActivityAdd/');
        }

        function paramsChange(params) {
            $scope.isAll ?
                $scope.isAll.split(',').map(function (item) {
                    params[item] ? delete params[item] : ''
                }) :
                $scope.merchantId && (params.merchantId = $scope.merchantId)
            return params
        }

        var getHotPointInfo = function () {
            var params = getParams();
            params = paramsChange(params);
            $scope.loading = true;
            LngLat = [];

            // var list = {
            //     distance: 2.31,
            //     lnglat: [121.1995822616206, 37.532067333693156],
            //     location: "121.1995822616206,37.532067333693156",
            //     mobile: "18562246555",
            //     name: "曾汉英",
            //     standNo: "LBVTZ0101KST96039",
            //     style: 0
            // }
            // let p = window.mapapi.invoke('Point', list.lnglat[0], list.lnglat[1])
            // p = {
            //     ...p,
            //     ...list
            // }
            // LngLat.push(p);
            // $scope.umcInstance.residenceMarkers(LngLat);
            // $scope.loading = false;
            
            customerService.getHotPointInfo(params, function (res) {
                $scope.points = res.data;
                console.log(res.data)
                LngLat = [];
                for (var i = 0; i < res.data.length; i++) {
                    var thisItem = res.data[i];
                    let p = window.mapapi.invoke('Point', thisItem.lnglat[0], thisItem.lnglat[1])
                    p = {
                        ...p,
                        ...thisItem
                    }
                    LngLat.push(p);
                    // LngLat.push(thisItem);
                }
                $scope.umcInstance.residenceMarkers(LngLat);
                $scope.loading = false;
            })
        };

        function getParams() {
            var obj = {};

            if ($scope.timePicked == 4) {
                obj.minDistance = 0;
                obj.maxDistance = 5;
            } else if ($scope.timePicked == 3) {
                obj.minDistance = 5;
                obj.maxDistance = 10;
            } else if ($scope.timePicked == 2) {
                obj.minDistance = 10;
                obj.maxDistance = 20;
            } else if ($scope.timePicked == 1) {
                obj.minDistance = 20;
                obj.maxDistance = 300;
            }
            var factoryMerchantId = $scope.factoryMerchantId;
            var provinceMerchantId = $scope.provinceMerchantId;
            var cityMerchantId = $scope.cityMerchantId;
            var shopMerchantId = $scope.shopMerchantId;
            if (factoryMerchantId) {
                obj.factoryMerchantId = $scope.factoryMerchantId;
            }
            if (provinceMerchantId) {
                obj.provinceMerchantId = $scope.provinceMerchantId;
            }
            if (cityMerchantId) {
                obj.cityMerchantId = $scope.cityMerchantId;
            }
            if (shopMerchantId) {
                obj.merchantId = $scope.shopMerchantId;
            }

            if ($scope.minDistance) {
                obj.minDistance = $scope.minDistance
            }
            if ($scope.maxDistance) {
                obj.maxDistance = $scope.maxDistance
            }
            return obj;
        }

        $scope.exportExcel = function () {
            if ($scope.isExporting) return;
            var params = getParams();
            $scope.isExporting = true;
            var customerTitle = '客户热力图';
            Utils.exportExcel3("carPermanent/exportCarPermanent", params, customerTitle, 'GET', function () {
                $scope.isExporting = false;
            });
        };

        $scope.curcentPage = '客户热力图';
        $rootScope.$on('$stateChangeStart', function (evt, toState, toParams, fromState, fromParams) {
            $scope.curcentPage = toState.data.pageTitle;
        });

        // 级联-选中自动搜索
        $scope.isAll = '';
        $scope.merchantId = '';
        $rootScope.$on('$merchantSelectChange', function (even, isAll, merchantId) {
            if ($scope.curcentPage == '客户热力图') {
                $scope.isAll = isAll;
                $scope.merchantId = merchantId;
                $scope.tableSearch();
            }
        });
    }

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