/**
 * Created by wh on 2015/7/24.
 */
/**
 * Created by liz on 2015/3/28.
 */
var co = require('co');
var bizTestDrive = require('@gopalroy/biz-testdrive');
var express = require('express');
var ApiHelper = require('./ApiHelper');
var debug = require('debug')('@gopalroy/test-drive');

var StatistcsAPI = (function(){
    function StatistcsAPI(){
        var router = new express();
        router.get('/mileage', this.StaMileage);
        router.get('/speedmax', this.StaSpeedMax);
        router.get('/speedupdown', this.StaSpeedUpDown);
        router.get('/oilused', this.StaOilUsed);
        router.get('/starttimes',this.StaStartTimes);


        //以司机为对象的分析
        router.get('/staffMileage', this.StaStaffMileage);
        router.get('/staffSpeedmax', this.StaStaffSpeedMax);
        router.get('/staffSpeedupdown', this.StaStaffSpeedUpDown);
        router.get('/staffOilused', this.StaStaffOilUsed);
        router.get('/staffStarttimes',this.StaStaffStartTimes);


        this.router = router;
    }

    StatistcsAPI.prototype.getRouter = function(){
        return this.router;
    };

    StatistcsAPI.prototype.resolveStatistics = function(req, res, next){
        co(function*(){
            this.tdriveId = req.incar.testDrive.id;
            next();
        }).catch(function(ex){
            debug(ex.stack);
            res.status(500).send(ex.stack);
        });
    };

    //启动次数
    StatistcsAPI.prototype.StaStartTimes = function(req,res){
        co(function*(){
            var data = req.query;
            var tm_start = "";
            var tm_end = "";
            var isChart = data['isChart'];
            if(data['tm_start']) tm_start = data['tm_start'];
            if(data['tm_end']) tm_end = data['tm_end'];
            var tdriveId = this.tdriveId;
            var statisticsData  = yield bizTestDrive.Statistics.StaStartTimesG(tdriveId,tm_start,tm_end,isChart);
            if(isChart == "true"){
                var count = yield bizTestDrive.Statistics.StaStartTimesCountG(tdriveId,tm_start,tm_end,isChart);
                res.json({status:'ok',total:count,statisticsData:statisticsData});
            }else{
                res.json({status:'ok',allStartTimes :statisticsData});
            }

        }).catch(function(ex){
            res.status(500).send(ex.stack);
        });
    };


    //里程
    StatistcsAPI.prototype.StaMileage = function(req,res){
        co(function*(){
            var data = req.query;
            var tm_start = "";
            var tm_end = "";
            var isChart = data['isChart'];
            if(data['tm_start']) tm_start = data['tm_start'];
            if(data['tm_end']) tm_end = data['tm_end'];
            var tdriveId = this.tdriveId;
            var statisticsData  = yield bizTestDrive.Statistics.StaMileageG(tdriveId,tm_start,tm_end,isChart);
            if(isChart == "true"){
                var count = yield bizTestDrive.Statistics.StaMileageCountG(tdriveId,tm_start,tm_end);
                res.json({status:'ok',total:count,statisticsData:statisticsData});
            }else{
                res.json({status:'ok',allMileage:statisticsData});
            }
        }).catch(function(ex){
            res.status(500).send(ex.stack);
        });
    };

    //超速
    StatistcsAPI.prototype.StaSpeedMax = function(req,res){
        co(function*(){
            var data = req.query;
            var tm_start = "";
            var tm_end = "";
            var isChart = data['isChart'];
            if(data['tm_start']) tm_start = data['tm_start'];
            if(data['tm_end']) tm_end = data['tm_end'];
            var tdriveId = this.tdriveId;
            var statisticsData  = yield bizTestDrive.Statistics.StaSpeedMaxG(tdriveId,tm_start,tm_end,isChart);
            if(isChart == "true"){
                var count = yield bizTestDrive.Statistics.StaSpeedMaxCountG(tdriveId,tm_start,tm_end);
                res.json({status:'ok',total:count,statisticsData:statisticsData});
            }else{
                res.json({status:'ok',allSpeedMax:statisticsData});
            }
        }).catch(function(ex){
            res.status(500).send(ex.stack);
        });
    };

    //急加减速
    StatistcsAPI.prototype.StaSpeedUpDown = function(req,res){
        co(function*(){
            var data = req.query;
            var tm_start = "";
            var tm_end = "";
            var isChart = data['isChart'];
            if(data['tm_start']) tm_start = data['tm_start'];
            if(data['tm_end']) tm_end = data['tm_end'];
            var tdriveId = this.tdriveId;
            var statisticsData  = yield bizTestDrive.Statistics.StaSpeedUpDownG(tdriveId,tm_start,tm_end,isChart);
            if(isChart == "true"){
                var count = yield bizTestDrive.Statistics.StaSpeedUpDownCountG(tdriveId,tm_start,tm_end);
                res.json({status:'ok',total:count,statisticsData:statisticsData});
            }else{
                res.json({status:'ok',allSpeedUpDown:statisticsData});
            }
        }).catch(function(ex){
            res.status(500).send(ex.stack);
        });
    };

    //油耗
    StatistcsAPI.prototype.StaOilUsed = function(req,res){
        co(function*(){
            var data = req.query;
            var tm_start = "";
            var tm_end = "";
            var isChart = data['isChart'];
            if(data['tm_start']) tm_start = data['tm_start'];
            if(data['tm_end']) tm_end = data['tm_end'];
            var tdriveId = this.tdriveId;
            var statisticsData  = yield bizTestDrive.Statistics.StaOilUsedG(tdriveId,tm_start,tm_end,isChart);
            if(isChart == "true"){
                var count = yield bizTestDrive.Statistics.StaOilUsedCountG(tdriveId,tm_start,tm_end);
                res.json({status:'ok',total:count,statisticsData:statisticsData});
            }else{
                res.json({status:'ok',allOilUsed:statisticsData});
            }
        }).catch(function(ex){
            res.status(500).send(ex.stack);
        });
    };

    //---------------司机------------------------------------------------------------

    //里程
    StatistcsAPI.prototype.StaStaffMileage = function(req,res){
        co(function*(){
            var data = req.query;
            var tm_start = "";
            var tm_end = "";
            var isChart = data['isChart'];
            if(data['tm_start']) tm_start = data['tm_start'];
            if(data['tm_end']) tm_end = data['tm_end'];
            var tdriveId = this.tdriveId;
            var statisticsData  = yield bizTestDrive.Statistics.StaStaffMileageG(tdriveId,tm_start,tm_end,isChart);
            if(isChart == "true"){
                var count = yield bizTestDrive.Statistics.StaStaffMileageCountG(tdriveId,tm_start,tm_end);
                res.json({status:'ok',total:count,statisticsData:statisticsData});
            }else{
                res.json({status:'ok',allMileage:statisticsData});
            }

        }).catch(function(ex){
            res.status(500).send(ex.stack);
        });
    };

    //超速
    StatistcsAPI.prototype.StaStaffSpeedMax = function(req,res){
        co(function*(){
            var data = req.query;
            var tm_start = "";
            var tm_end = "";
            var isChart = data['isChart'];
            if(data['tm_start']) tm_start = data['tm_start'];
            if(data['tm_end']) tm_end = data['tm_end'];
            var tdriveId = this.tdriveId;
            var statisticsData  = yield bizTestDrive.Statistics.StaStaffSpeedMaxG(tdriveId,tm_start,tm_end,isChart);
            if(isChart == "true"){
                var count = yield bizTestDrive.Statistics.StaStaffSpeedMaxCountG(tdriveId,tm_start,tm_end);
                res.json({status:'ok',total:count,statisticsData:statisticsData});
            }else{
                res.json({status:'ok',allSpeedMax:statisticsData});
            }

        }).catch(function(ex){
            res.status(500).send(ex.stack);
        });
    };

    //启动次数
    StatistcsAPI.prototype.StaStaffStartTimes = function(req,res){
        co(function*(){
            var data = req.query;
            var tm_start = "";
            var tm_end = "";
            var isChart = data['isChart'];
            if(data['tm_start']) tm_start = data['tm_start'];
            if(data['tm_end']) tm_end = data['tm_end'];
            var tdriveId = this.tdriveId;
            var statisticsData  = yield bizTestDrive.Statistics.StaStaffStartTimesG(tdriveId,tm_start,tm_end,isChart);
            if(isChart == "true"){
                var count = yield bizTestDrive.Statistics.StaStaffStartTimesCountG(tdriveId,tm_start,tm_end);
                res.json({status:'ok',total:count,statisticsData:statisticsData});
            }else{
                res.json({status:'ok',allStartTimes:statisticsData});
            }
        }).catch(function(ex){
            res.status(500).send(ex.stack);
        });
    };

    //急加减速
    StatistcsAPI.prototype.StaStaffSpeedUpDown = function(req,res){
        co(function*(){
            var data = req.query;
            var tm_start = "";
            var tm_end = "";
            var isChart = data['isChart'];
            if(data['tm_start']) tm_start = data['tm_start'];
            if(data['tm_end']) tm_end = data['tm_end'];
            var tdriveId = this.tdriveId;
            var statisticsData  = yield bizTestDrive.Statistics.StaStaffSpeedUpDownG(tdriveId,tm_start,tm_end,isChart);
            if(isChart == "true"){
                var count = yield bizTestDrive.Statistics.StaStaffSpeedUpDownCountG(tdriveId,tm_start,tm_end);
                res.json({status:'ok',total:count,statisticsData:statisticsData});
            }else{
                res.json({status:'ok',allSpeedUpDown:statisticsData});
            }
        }).catch(function(ex){
            res.status(500).send(ex.stack);
        });
    };

    //油耗
    StatistcsAPI.prototype.StaStaffOilUsed = function(req,res){
        co(function*(){
            var data = req.query;
            var tm_start = "";
            var tm_end = "";
            var isChart = data['isChart'];
            if(data['tm_start']) tm_start = data['tm_start'];
            if(data['tm_end']) tm_end = data['tm_end'];
            var tdriveId = this.tdriveId;
            var statisticsData  = yield bizTestDrive.Statistics.StaStaffOilUsedG(tdriveId,tm_start,tm_end,isChart);
            if(isChart == "true"){
                var count = yield bizTestDrive.Statistics.StaStaffOilUsedCountG(tdriveId,tm_start,tm_end);
                res.json({status:'ok',total:count,statisticsData:statisticsData});
            }else{
                res.json({status:'ok',allOilUsed:statisticsData});
            }
        }).catch(function(ex){
            res.status(500).send(ex.stack);
        });
    };


    return StatistcsAPI;
})();

module.exports = StatistcsAPI;
