package com.yihu.wlyy.web.statistic;

import com.yihu.wlyy.entity.statistics.PopulationBase;
import com.yihu.wlyy.service.app.statistics.StatisticsAllService;
import com.yihu.wlyy.service.app.statistics.StatisticsService;
import com.yihu.wlyy.web.BaseController;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
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.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Set;

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

    @Autowired
    StatisticsService statisticsService;
    @Autowired
    StatisticsAllService statisticsAllService;

    /**
     * 获取统计时间
     *
     * @return
     */
    @RequestMapping(value = "/time")
    @ResponseBody
    public String getStatisticsTime() {
        try {
            return write(200, "查询成功", "data", statisticsService.getStatisticsTime());
        } catch (Exception e) {
            return error(-1, "查询失败");
        }
    }

    /**
     * 指标按间隔统计
     *
     * @param startDate 起始日期
     * @param endDate   结束时间
     * @param interval  时间间隔
     * @param area      区域或机构
     * @param level     级别
     * @param index     指标
     * @return
     */
    @RequestMapping(value = "/interval")
    @ResponseBody
    public String indexInterval(@RequestParam(required = true) String startDate,
                                @RequestParam(required = true) String endDate,
                                @RequestParam(required = true) int interval,
                                @RequestParam(required = true) String area,
                                @RequestParam(required = true) int level,
                                @RequestParam(required = true) 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) {
            return error(-1, tag + "查询失败!");
        }
    }

    /**
     * 指标期间增长量
     *
     * @param startDate
     * @param endDate
     * @param area
     * @param level
     * @param index
     * @return
     */
    @RequestMapping("/increment")
    @ResponseBody
    public String getIndexIncrement(@RequestParam(required = true) String startDate,
                                    @RequestParam(required = true) String endDate,
                                    @RequestParam(required = true) String area,
                                    @RequestParam(required = true) int level,
                                    @RequestParam(required = true) 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(@RequestParam(required = true) String endDate,
                                @RequestParam(required = true) String area,
                                @RequestParam(required = true) int level,
                                @RequestParam(required = true) 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) {
            return error(-1, "查询失败");
        }
    }

    /**
     * 指标期间增长量
     *
     * @param startDate
     * @param endDate
     * @param area
     * @param level
     * @param index
     * @return
     */
    @RequestMapping("/lowlevel_increment")
    @ResponseBody
    public String getIndexLowLevelIncrement(@RequestParam(required = true) String startDate,
                                            @RequestParam(required = true) String endDate,
                                            @RequestParam(required = true) String area,
                                            @RequestParam(required = true) int level,
                                            @RequestParam(required = true) String index,
                                            @RequestParam(required = true) int sort,
                                            @RequestParam(required = false) 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(@RequestParam(required = true) String endDate,
                                        @RequestParam(required = true) String area,
                                        @RequestParam(required = true) int level,
                                        @RequestParam(required = true) String index,
                                        @RequestParam(required = true) int sort,
                                        @RequestParam(required = false) 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) {
            return error(-1, "查询失败");
        }
    }

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

            for (String idx : indexes) {
                result.put("index_" + idx, statisticsAllService.getIndexLevelTwototal(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:团队
     * @return
     */
    @RequestMapping(value = "/sign_info")
    @ResponseBody
    public String getAreaSignInfo(@RequestParam(required = true) String endDate,
                                  @RequestParam(required = true) String area,
                                  @RequestParam(required = true) int level,
                                  @RequestParam(required = false) String lowCode) {
        try {
            JSONObject result = new JSONObject();

            if (StringUtils.isEmpty(lowCode)) {
                long sign = statisticsAllService.getIndexTotal(endDate, area, level, "13");
                long weiJf = statisticsAllService.getWeiJiaoFei(endDate, area, level);
                JSONObject signRate = statisticsAllService.getSignRate(endDate, area, level);
                JSONObject signTaskRate = statisticsAllService.getSignTaskRate(endDate, area, level);

                result.put("sign", sign);
                result.put("expenses", weiJf);
                result.put("signRate", signRate);
                result.put("signTaskRate", signTaskRate);
            } else {
                result = statisticsAllService.getGroupInfo(endDate, lowCode, area, level);
            }

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

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

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

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

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

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

    /**
     * 指标截止日期累积量
     *
     * @param date
     * @param area
     * @param level
     * @param index
     * @return
     */
    @RequestMapping("/lowlevel_all")
    @ResponseBody
    public String getIndexLowLevelTotalSign(@RequestParam(required = true) String date,
                                            @RequestParam(required = true) String area,
                                            @RequestParam(required = true) int level,
                                            @RequestParam(required = true) String index,
                                            @RequestParam(required = true) int sort,
                                            @RequestParam(required = false) String lowLevel,
                                            @RequestParam(required = false) String lowCode) {
        try {
            String[] indexes = index.split(",");
            JSONObject result = new JSONObject();

            if (StringUtils.isNotEmpty(lowCode)) {
                result.put("index_" + index, statisticsAllService.getLevelTwoLowLevelTotal(date, area, level, index, sort, lowLevel, lowCode));
            } else {
                for (String idx : indexes) {
                    if (idx.equals("18") || index.equals("19")) {
                        result.put("index_" + idx, statisticsAllService.getLowLevelTotalSpecial(date, area, level, idx, sort, lowLevel));
                    } else {
                        result.put("index_" + idx, statisticsAllService.getLowLevelTotal(date, area, level, idx, sort, lowLevel));
                    }
                }
            }

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

    /**
     * 获取三师转签或高危人群
     *
     * @param endDate
     * @param area
     * @param level
     * @return
     */
    @RequestMapping(value = "/sszq_qwrq_info")
    @ResponseBody
    public String getSszqAndGwrq(@RequestParam(required = true) String endDate,
                                 @RequestParam(required = true) String area,
                                 @RequestParam(required = true) int level) {
        try {
            JSONObject result = new JSONObject();

            JSONObject sszq = statisticsAllService.getSszqAndGwrq(endDate, area, level, "18");
            JSONObject gwrq = statisticsAllService.getSszqAndGwrq(endDate, area, level, "19");

            result.put("sszq",sszq);
            result.put("gwrq",gwrq);

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

    /**
     * 二级指标到达量
     *
     * @param date
     * @param area
     * @param level
     * @param index
     * @return
     */
    @RequestMapping(value = "/leveltwo_all")
    @ResponseBody
    public String getIndexLevelTwoTotal(@RequestParam(required = true) String date,
                                        @RequestParam(required = true) String area,
                                        @RequestParam(required = true) int level,
                                        @RequestParam(required = true) String index) {
        try {
            String[] indexes = index.split(",");
            JSONObject result = new JSONObject();

            for (String idx : indexes) {
                result.put("index_" + idx, statisticsAllService.getIndexLevelTwototal(date, 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("/index_all")
    @ResponseBody
    public String getIndexAll(@RequestParam(required = true) String endDate,
                              @RequestParam(required = true) String area,
                              @RequestParam(required = true) int level,
                              @RequestParam(required = true) String index) {
        try {
            String[] indexes = index.split(",");
            JSONObject result = new JSONObject();

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

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

}
