package com.yihu.wlyy.web.statistic;

import com.yihu.wlyy.entity.statistics.PopulationBase;
import com.yihu.wlyy.service.app.statistics.StatisticsService;
import com.yihu.wlyy.web.BaseController;
import io.swagger.annotations.Api;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by lyr on 2016/08/16.
 */
@Controller
@RequestMapping(value = "/statistics")
@Api(description = "统计")
public class StatisticsController extends BaseController {

    @Autowired
    StatisticsService statisticsService;

    /**
     * 指标按间隔统计
     *
     * @param startDate 起始日期
     * @param endDate   结束时间
     * @param interval  时间间隔
     * @param area      区域或机构
     * @param level     级别
     * @param index     指标
     * @return
     */
    @RequestMapping(value = "/interval")
    @ResponseBody
    public String indexInterval(String startDate, String endDate, int interval, String area, int level, String index) {
        String tag = "";
        try {
            String[] indexes = index.split(",");
            JSONObject result = new JSONObject();

            if (index != null) {
                for (String idx : indexes) {
                    result.put("index_" + idx, statisticsService.getDateIncrementDetail(startDate, endDate, interval, area, level, idx));
                }
            }

            return write(200, "查询成功!", "data", result);
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, tag + "查询失败!");
        }
    }

    /**
     * 指标期间增长量
     *
     * @param startDate
     * @param endDate
     * @param area
     * @param level
     * @param index
     * @return
     */
    @RequestMapping("/increment")
    @ResponseBody
    public String getIndexIncrement(String startDate, String endDate, String area, int level, String index) {
        try {
            String[] indexes = index.split(",");
            JSONObject result = new JSONObject();

            for (String idx : indexes) {
                result.put("index_" + idx, statisticsService.getIntervalIncrement(startDate, endDate, area, level, idx));
            }

            return write(200, "查询成功", "data", result);
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, "查询失败");
        }
    }

    /**
     * 指标截止日期累积量
     *
     * @param endDate
     * @param area
     * @param level
     * @param index
     * @return
     */
    @RequestMapping("/total")
    @ResponseBody
    public String getIndexTotal(String endDate, String area, int level, String index) {
        try {
            String[] indexes = index.split(",");
            JSONObject result = new JSONObject();

            for (String idx : indexes) {
                result.put("index_" + idx, statisticsService.getTotalAmount(endDate, area, level, idx));
            }

            return write(200, "查询成功", "data", result);
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, "查询失败");
        }
    }

    /**
     * 指标期间增长量
     *
     * @param startDate
     * @param endDate
     * @param area
     * @param level
     * @param index
     * @return
     */
    @RequestMapping("/lowlevel_increment")
    @ResponseBody
    public String getIndexLowLevelIncrement(String startDate, String endDate, String area, int level, String index, int sort,String lowLevel) {
        try {
            String[] indexes = index.split(",");
            JSONObject result = new JSONObject();

            for (String idx : indexes) {
                result.put("index_" + idx, statisticsService.getLowLevelIncrementDetail(startDate, endDate, area, level, idx, sort,lowLevel));
            }

            return write(200, "查询成功", "data", result);
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, "查询失败");
        }
    }

    /**
     * 指标截止日期累积量
     *
     * @param endDate
     * @param area
     * @param level
     * @param index
     * @return
     */
    @RequestMapping("/lowlevel_total")
    @ResponseBody
    public String getIndexLowLevelTotal(String endDate, String area, int level, String index, int sort, String lowLevel) {
        try {
            String[] indexes = index.split(",");
            JSONObject result = new JSONObject();

            for (String idx : indexes) {
                result.put("index_" + idx, statisticsService.getLowLevelTotalDetail(endDate, area, level, idx, sort, lowLevel));
            }

            return write(200, "查询成功", "data", result);
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, "查询失败");
        }
    }

    /**
     * 指标期间增长量
     *
     * @param startDate
     * @param endDate
     * @param area
     * @param level
     * @param index
     * @return
     */
    @RequestMapping("/leveltwo_increment")
    @ResponseBody
    public String getIndexLevelTwoIncrement(String startDate, String endDate, String area, int level, String index) {
        try {
            String[] indexes = index.split(",");
            JSONObject result = new JSONObject();

            for (String idx : indexes) {
                result.put("index_" + idx, statisticsService.getLevelTwoIndexIncrement(startDate, endDate, area, level, idx));
            }

            return write(200, "查询成功", "data", result);
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, "查询失败");
        }
    }


    /**
     * 获取签约率、签约完成率
     *
     * @param endDate 截止日期
     * @param area    区域
     * @param level   区域级别 4:城市 3:区 2:社区 1:团队
     * @param type    1:签约率  2:签约完成率
     * @return
     */
    @RequestMapping(value = "/rate")
    @ResponseBody
    public String getAreaRate(String endDate, String area, int level, String type) {
        try {
            JSONObject result = null;

            if (type.equals("1")) {
                result = statisticsService.getSignRate(endDate,area,level);
            } else {
                result = statisticsService.getSignTaskRate(endDate,area,level);
            }

            return write(200, "查询成功", "data", result);
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, "查询失败");
        }
    }

    /**
     * 获取三级指标增量
     *
     * @param startDate
     * @param endDate
     * @param area
     * @param level
     * @return
     */
    @RequestMapping(value = "/sixfive_statistics")
    @ResponseBody
    public String getSixFiveStatistics(String startDate, String endDate, String area, int level){
        try{
            JSONArray result = statisticsService.getSixFiveStatistics(startDate,endDate,area,level);

            return write(200,"查询成功","data",result);
        }catch (Exception e){
            e.printStackTrace();
            return error(-1,"查询失败");
        }
    }
}
