<commit_before>/**
 * Created by Lijingjing on 15/12/16.
 */
'use strict';

angular.module("myApp.reservation", ['ngRoute', 'smart-table', 'ui-notification', 'ngDialog'])
    .config(['$routeProvider', function ($routeProvider) {
        $routeProvider.when('/reservation', {
            templateUrl: 'view_reservation/reservation.html',
            controller: 'ReservationCtrl'
        });
    }])
    .config([
        'ngDialogProvider',
        function (ngDialogProvider) {
            ngDialogProvider.setDefaults({
                className: 'ngdialog-theme-default',
                ariaAuto: false,
                ariaRole: false
            });
        }
    ])
    .factory('OrderService', ['$http', function ($http) {
        var restaurantBaseUrl = "http://202.120.40.175:21104";

        var getOderInfosRequest = function (restaurantId) {
            return $http({
                method: 'GET',
                url: restaurantBaseUrl + '/order/infoByrestaurantid?restaurantId=' + restaurantId
            });
        };

        var updateOrderStateRequest = function (orderId) {
            return $http({
                method: 'GET',
                url: restaurantBaseUrl + '/order/finishOrder?orderId=' + orderId
            });
        };

        var acceptOrderRequest = function (orderId, opt) {
            return $http({
                method: 'GET',
                url: restaurantBaseUrl + '/order/confirmOrder?orderId=' + orderId + "&opt=" + opt
            });
        };
        return {
            getOrderInfo: function (restaurantId) {
                return getOderInfosRequest(restaurantId);
            },
            updateOrderState: function (orderId) {
                return updateOrderStateRequest(orderId);
            },
            acceptOrder: function (orderId, opt) {
                return acceptOrderRequest(orderId, opt);
            }
        }

    }])
    .controller("ReservationCtrl", ["$scope", "Notification", "ngDialog", "OrderService", "$location", function ($scope, Notification, ngDialog, OrderService, $location) {

        if ($.cookie("restaurantId") == null || $.cookie("restaurantId") == "" || $.cookie("restaurantId") == undefined) {
            window.location = "/";
        }
        //copy the references (you could clone ie angular.copy but then have to go through a dirty checking for the matches)
        //$scope.displayedCollection = [].concat($scope.rowCollection);
        $scope.rowCollection = [];

        OrderService.getOrderInfo($.cookie("restaurantId"))
            .success(function (data) {
                //console.log("所有数据---" + data);
                $scope.myNotifications = [];
                $scope.removeIndex = [];
                for (var i = 0; i < data.length; i++) {

                    var dateTime = data[i].orderTime.split(" ");
                    if (data[i].gender == 0) {
                        data[i].gender = "女";
                    }
                    else {
                        data[i].gender = "男";
                    }
                    data[i].orderDate = dateTime[0];
                    data[i].orderTimer = dateTime[1].substr(0, 5);

                    if (data[i].state == "已完成") {
                        data[i].orderHandled = true;
                        //console.log("已完成——" + data[i]);
                    }
                    else if (data[i].state == "未确认") {
                        var index = data.indexOf(data[i]);
                        $scope.myNotifications.push(data[i]);
                        $scope.removeIndex.push(index);
                        //console.log("未确认——" + data[i]);
                        continue;
                    }
                    else if (data[i].state == "已拒绝") {
                        data[i].orderHandled = true;
                    }
                    else {
                        data[i].orderHandled = false;
                        //console.log("未完成-" + data[i].orderHandled);
                    }

                    $scope.rowCollection.push(data[i]);
                }

                for (var j = 0; j < $scope.removeIndex.length; j++) {
                    var index = $scope.removeIndex[j];
                    var dateTime = data[index].orderTime.split(" ");
                    $scope.notify(data[index]);
                }

            });


        //remove to the real data holder
        $scope.removeItem = function removeItem(row) {
            alert(JSON.stringify(row));
            var index = $scope.rowCollection.indexOf(row);
            if (index !== -1) {
                $scope.rowCollection.splice(index, 1);
            }
        };

        //完成订单
        $scope.completeOrder = function (row) {
            var index = $scope.rowCollection.indexOf(row);
            if (index !== -1) {
                $scope.rowCollection.splice(index, 1);
                //console.log($scope.rowCollection);
            }
            OrderService.updateOrderState(row.orderId)
                .success(function (data) {
                    var dateTime = data.orderTime.split(" ");
                    data.orderDate = dateTime[0];
                    data.orderTimer = dateTime[1].substr(0, 5);
                    data.orderHandled = true;
                    $scope.rowCollection.push(data);
                });
        };

        $scope.notifications = [];
        $scope.notificationNum = 1;
        $scope.close = function () {
            //console.log($scope.notifications.length);
            $scope.notifications[$scope.notifications.length - 1].then(function (notification) {
                notification.kill(true);
                $scope.notificationNum--;
                $scope.notifications.splice($scope.notifications.length - 1, 1)
            });
        };

        //推送开始
        var appId = '8d3bm5jtWzUowvOw2GJLqUxI-gzGzoHsz';
        var appKey = '4dTvIzaFOoHU6Xatd7rDIrvw';
        var push = AV.push({
            appId: appId,
            appKey: appKey
        });


        push.subscribe([$.cookie("restaurantId").toString()], function () {
            console.log('订阅成功！');
        });

        push.open(function () {
            console.log('连接服务器成功，可以接收推送');
        });

        // 监听推送消息
        push.on('message', function (data) {
            console.log(JSON.stringify(data));
            if (data.order.state == '未确认')
                $scope.notify(data.order);
            else {

                data.order.orderDate = data.order.orderTime.split(" ")[0];
                data.order.orderTimer = data.order.orderTime.split(" ")[1].substr(0, 5);
                data.order.orderHandled = false;
                $scope.rowCollection.push(data.order);
                $scope.$apply();
            }
        });


        $scope.notify = function (order) {
            var newScope = $scope.$new(true);
            newScope.name = order.userLastName + order.userFirstName;
            newScope.phone = order.phoneId;
            newScope.level = order.userVipLevel;
            newScope.date = order.orderTime.split(" ")[0];
            newScope.time = order.orderTime.split(" ")[1].substr(0, 5);
            newScope.peopleNum = order.dinerNum;
            newScope.money = order.dorderSum;
            newScope.remark = order.more;

            newScope.cancel = function () {
                //发送请求,将订单ID作为参数,取消订单的接口
                OrderService.acceptOrder(order.orderId, 0)
                    .success(function (data) {
                        var dateTime = data.orderTime.split(" ");
                        data.orderDate = dateTime[0];
                        data.orderTimer = dateTime[1].substr(0, 5);
                        data.orderHandled = true;
                        data.state = "已拒绝";

                        $scope.rowCollection.push(data);<commit_msg>推送信息姓名显示bug fixed<commit_after>/**
 * Created by Lijingjing on 15/12/16.
 */
'use strict';

angular.module("myApp.reservation", ['ngRoute', 'smart-table', 'ui-notification', 'ngDialog'])
    .config(['$routeProvider', function ($routeProvider) {
        $routeProvider.when('/reservation', {
            templateUrl: 'view_reservation/reservation.html',
            controller: 'ReservationCtrl'
        });
    }])
    .config([
        'ngDialogProvider',
        function (ngDialogProvider) {
            ngDialogProvider.setDefaults({
                className: 'ngdialog-theme-default',
                ariaAuto: false,
                ariaRole: false
            });
        }
    ])
    .factory('OrderService', ['$http', function ($http) {
        var restaurantBaseUrl = "http://192.168.127.12:21104";

        var getOderInfosRequest = function (restaurantId) {
            return $http({
                method: 'GET',
                url: restaurantBaseUrl + '/order/infoByrestaurantid?restaurantId=' + restaurantId
            });
        };

        var updateOrderStateRequest = function (orderId) {
            return $http({
                method: 'GET',
                url: restaurantBaseUrl + '/order/finishOrder?orderId=' + orderId
            });
        };

        var acceptOrderRequest = function (orderId, opt) {
            return $http({
                method: 'GET',
                url: restaurantBaseUrl + '/order/confirmOrder?orderId=' + orderId + "&opt=" + opt
            });
        };
        return {
            getOrderInfo: function (restaurantId) {
                return getOderInfosRequest(restaurantId);
            },
            updateOrderState: function (orderId) {
                return updateOrderStateRequest(orderId);
            },
            acceptOrder: function (orderId, opt) {
                return acceptOrderRequest(orderId, opt);
            }
        }

    }])
    .controller("ReservationCtrl", ["$scope", "Notification", "ngDialog", "OrderService", "$location", function ($scope, Notification, ngDialog, OrderService, $location) {

        if ($.cookie("restaurantId") == null || $.cookie("restaurantId") == "" || $.cookie("restaurantId") == undefined) {
            window.location = "/";
        }
        //copy the references (you could clone ie angular.copy but then have to go through a dirty checking for the matches)
        //$scope.displayedCollection = [].concat($scope.rowCollection);
        $scope.rowCollection = [];

        OrderService.getOrderInfo($.cookie("restaurantId"))
            .success(function (data) {
                //console.log("所有数据---" + data);
                $scope.myNotifications = [];
                $scope.removeIndex = [];
                for (var i = 0; i < data.length; i++) {

                    var dateTime = data[i].orderTime.split(" ");
                    if (data[i].gender == 0) {
                        data[i].gender = "女";
                    }
                    else {
                        data[i].gender = "男";
                    }
                    data[i].orderDate = dateTime[0];
                    data[i].orderTimer = dateTime[1].substr(0, 5);

                    if (data[i].state == "已完成") {
                        data[i].orderHandled = true;
                        //console.log("已完成——" + data[i]);
                    }
                    else if (data[i].state == "未确认") {
                        var index = data.indexOf(data[i]);
                        $scope.myNotifications.push(data[i]);
                        $scope.removeIndex.push(index);
                        //console.log("未确认——" + data[i]);
                        continue;
                    }
                    else if (data[i].state == "已拒绝") {
                        data[i].orderHandled = true;
                    }
                    else {
                        data[i].orderHandled = false;
                        //console.log("未完成-" + data[i].orderHandled);
                    }

                    $scope.rowCollection.push(data[i]);
                }

                for (var j = 0; j < $scope.removeIndex.length; j++) {
                    var index = $scope.removeIndex[j];
                    var dateTime = data[index].orderTime.split(" ");
                    $scope.notify(data[index]);
                }

            });


        //remove to the real data holder
        $scope.removeItem = function removeItem(row) {
            alert(JSON.stringify(row));
            var index = $scope.rowCollection.indexOf(row);
            if (index !== -1) {
                $scope.rowCollection.splice(index, 1);
            }
        };

        //完成订单
        $scope.completeOrder = function (row) {
            var index = $scope.rowCollection.indexOf(row);
            if (index !== -1) {
                $scope.rowCollection.splice(index, 1);
                //console.log($scope.rowCollection);
            }
            OrderService.updateOrderState(row.orderId)
                .success(function (data) {
                    var dateTime = data.orderTime.split(" ");
                    data.orderDate = dateTime[0];
                    data.orderTimer = dateTime[1].substr(0, 5);
                    data.orderHandled = true;
                    $scope.rowCollection.push(data);
                });
        };

        $scope.notifications = [];
        $scope.notificationNum = 1;
        $scope.close = function () {
            //console.log($scope.notifications.length);
            $scope.notifications[$scope.notifications.length - 1].then(function (notification) {
                notification.kill(true);
                $scope.notificationNum--;
                $scope.notifications.splice($scope.notifications.length - 1, 1)
            });
        };

        //推送开始
        var appId = '<KEY>';
        var appKey = '4dTvIzaFOoHU6Xatd7rDIrvw';
        var push = AV.push({
            appId: appId,
            appKey: appKey
        });


        push.subscribe([$.cookie("restaurantId").toString()], function () {
            console.log('订阅成功！');
        });

        push.open(function () {
            console.log('连接服务器成功，可以接收推送');
        });

        // 监听推送消息
        push.on('message', function (data) {
            console.log(JSON.stringify(data));
            if (data.order.state == '未确认')
                $scope.notify(data.order);
            else {

                data.order.orderDate = data.order.orderTime.split(" ")[0];
                data.order.orderTimer = data.order.orderTime.split(" ")[1].substr(0, 5);
                data.order.orderHandled = false;
                $scope.rowCollection.push(data.order);
                $scope.$apply();
            }
        });


        $scope.notify = function (order) {
            var newScope = $scope.$new(true);
            newScope.name = order.userFirstName;
            newScope.phone = order.phoneId;
            newScope.level = order.userVipLevel;
            newScope.date = order.orderTime.split(" ")[0];
            newScope.time = order.orderTime.split(" ")[1].substr(0, 5);
            newScope.peopleNum = order.dinerNum;
            newScope.money = order.dorderSum;
            newScope.remark = order.more;

            newScope.cancel = function () {
                //发送请求,将订单ID作为参数,取消订单的接口
                OrderService.acceptOrder(order.orderId, 0)
                    .success(function (data) {
                        var dateTime = data.orderTime.split(" ");
                        data.orderDate = dateTime[0];
                        data.orderTimer = dateTime[1].substr(0, 5);
                        data.orderHandled = true;
                        data.state = "已拒绝";

                        $scope.rowCollection.push(data);
{'fraction_non_alphanumeric': 0.1333717136825945, 'fraction_numerical': 0.007548135354698394, 'mean_word_length': 1.4268860602297424, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4607392', 'n_tokens_mistral': 4387, 'n_tokens_neox': 4162, 'n_words': 715}