package com.ylcm.sys.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.ylcm.sys.common.ExcelUtil;
import com.ylcm.sys.common.Util;
import com.ylcm.sys.domain.AdTv;
import com.ylcm.sys.excel.model.SpecialAnalysisExportDTO;
import com.ylcm.sys.service.AdTvService;
import com.ylcm.sys.vo.*;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ylcm.sys.common.Constants;
import com.ylcm.sys.form.AdTheatreAnalysisDetailQueryForm;
import com.ylcm.sys.form.AdTheatreAnalysisQueryForm;
import com.ylcm.sys.service.AdTheatreTvStatService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Controller
@RequestMapping("/theatre_analysis")
public class TheatreAnalysisController extends BaseController{

	@Resource
	private AdTheatreTvStatService adTheatreTvStatService;
	@Resource
	private AdTvService adTvService;
	
	/**
	 * @param keyword 电视剧名称
	 * @return
	 */
	@RequestMapping(value = "/index.do")
	public String brandAnalysis(ModelMap model,String keyword) {
		/**
		 * 1.跳转到列表页面
		 */
		model.put("keyword", keyword);
		return "analysis/theatre_analysis";
	}
	
	@RequestMapping(value = "/ajax_list.do")
	public void ajaxSearchList(HttpServletResponse response, AdTheatreAnalysisQueryForm queryForm) {
		try {
			
			IPage<AdTheatreAnalysisVO> page = adTheatreTvStatService.getPage(queryForm);
			/**
			 * 3.返回结果
			 */
			JSONObject jsonobj = new JSONObject();
			jsonobj.put("data", page);
			jsonobj.put("code", Constants.CODE_SUCCESS);
			ajaxJson(jsonobj.toString(), response);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			ajaxJson("{\"code\":\"0\",\"msg\":\"获取列表失败\"}", response);
		}
	}

	@RequestMapping(value = "/export_list.do")
	public void exportSearchList(HttpServletResponse response, AdTheatreAnalysisQueryForm queryForm) {
		ExcelUtil.exportExcelToWebsite(response,"央卫视电视剧收视指标",new AdTheatreAnalysisVO(),(currentPage, pageSize) -> {
			queryForm.setPageNo(currentPage);
			queryForm.setPageSize(pageSize);
			return adTheatreTvStatService.getPage(queryForm).getRecords();
		});
	}

	/**
	 *   央卫视电视剧收视明细
	 * @param response
	 * @param queryForm
	 */
	@RequestMapping(value = "/ajax_detail_list.do")
	public void ajaxDetailList(HttpServletResponse response, AdTheatreAnalysisDetailQueryForm queryForm) {
		try {
			
			IPage<AdTheatreAnalysisDetailVO> page = adTheatreTvStatService.getDetailPage(queryForm);
			/**
			 * 3.返回结果
			 */
			JSONObject jsonobj = new JSONObject();
			jsonobj.put("data", page);
			jsonobj.put("code", Constants.CODE_SUCCESS);
			ajaxJson(jsonobj.toString(), response);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			ajaxJson("{\"code\":\"0\",\"msg\":\"获取列表失败\"}", response);
		}
	}

	@RequestMapping(value = "/export_detail_list.do")
	public void exportDetailList(HttpServletResponse response, AdTheatreAnalysisDetailQueryForm queryForm) {
		ExcelUtil.exportExcelToWebsite(response,"央卫视电视剧收视明细",new AdTheatreAnalysisDetailVO(),(currentPage, pageSize) -> {
			queryForm.setPageNo(currentPage);
			queryForm.setPageSize(pageSize);
			return adTheatreTvStatService.getDetailPage(queryForm).getRecords();
		});
	}

	/**
	 * 山东卫视电视剧收视分析
	 */
	@RequestMapping(value = "/ajax_tv_ratio.do")
	public void tvRatio(HttpServletResponse response, AdTheatreAnalysisDetailQueryForm queryForm) {
		try {

			AdTvRatioVO adTvRatioVO = adTheatreTvStatService.getTvRatioVO(queryForm);
			/**
			 * 3.返回结果
			 */
			JSONObject jsonobj = new JSONObject();
			jsonobj.put("data", adTvRatioVO);
			jsonobj.put("code", Constants.CODE_SUCCESS);
			ajaxJson(jsonobj.toString(), response);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			ajaxJson("{\"code\":\"0\",\"msg\":\"获取列表失败\"}", response);
		}
	}

	/**
	 * 电视剧收视区域分布
	 */
	@RequestMapping(value = "/ajax_tv_zone.do")
	public void tvZone(HttpServletResponse response, AdTheatreAnalysisDetailQueryForm queryForm) {
		try {

			AdTvZoneVO adTvZoneVO = adTheatreTvStatService.getTvZoneVO(queryForm);
			/**
			 * 3.返回结果
			 */
			JSONObject jsonobj = new JSONObject();
			jsonobj.put("data", adTvZoneVO);
			jsonobj.put("code", Constants.CODE_SUCCESS);
			ajaxJson(jsonobj.toString(), response);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			ajaxJson("{\"code\":\"0\",\"msg\":\"获取列表失败\"}", response);
		}
	}

	/**
	 * 电视剧产品投放分析
	 */
	@RequestMapping(value = "/ajax_tv_product.do")
	public void tvProduct(HttpServletResponse response, AdTheatreAnalysisDetailQueryForm queryForm) {
		try {
			List<AdTheatreProductDistVO> productVO = adTheatreTvStatService.getProductVO(queryForm);
			/**
			 * 3.返回结果
			 */
			JSONObject jsonobj = new JSONObject();
			jsonobj.put("data", productVO);
			jsonobj.put("code", Constants.CODE_SUCCESS);
			ajaxJson(jsonobj.toString(), response);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			ajaxJson("{\"code\":\"0\",\"msg\":\"获取列表失败\"}", response);
		}
	}

	/**
	 * 电视剧体裁结构分析
	 */
	@RequestMapping(value = "/ajax_tv_theme.do")
	public void tvTheme(HttpServletResponse response, AdTheatreAnalysisDetailQueryForm queryForm) {
		try {

			List<AdThemeDistVO> themeVOs = adTheatreTvStatService.getThemeDistVO(queryForm);
			if (CollectionUtils.isNotEmpty(themeVOs)) {
				float ratio = 0.0f;
				for (AdThemeDistVO adThemeDistVO:themeVOs) {
					ratio += adThemeDistVO.getRatio();
				}
				ratio = Util.newScale(ratio/themeVOs.size(), 3);
				themeVOs.add(new AdThemeDistVO("平均",0,ratio));
			}
			/**
			 * 3.返回结果
			 */
			JSONObject jsonobj = new JSONObject();
			jsonobj.put("data", themeVOs);
			jsonobj.put("code", Constants.CODE_SUCCESS);
			ajaxJson(jsonobj.toString(), response);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			ajaxJson("{\"code\":\"0\",\"msg\":\"获取列表失败\"}", response);
		}
	}

	/**
	 * 剧场电视剧收视率排名
	 * @param response
	 * @param startDate
	 * @param endDate
	 */
	@RequestMapping(value = "/ajax_tvTop_list.do")
	public void ajaxTvTopList(HttpServletResponse response, Integer theatreTime, String startDate, String endDate) {
		try {

			List<AdTheatreTopVO> list = adTheatreTvStatService.getTvTop(theatreTime, startDate, endDate);
			/**
			 * 3.返回结果
			 */
			JSONObject jsonobj = new JSONObject();
			jsonobj.put("data", list);
			jsonobj.put("code", Constants.CODE_SUCCESS);
			ajaxJson(jsonobj.toString(), response);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			ajaxJson("{\"code\":\"0\",\"msg\":\"获取列表失败\"}", response);
		}
	}

	/**
	 * 卫视剧场排名
	 * @param name 剧名
	 * @param theatreTime 剧场：0日间 1黄金2午夜3凌晨
	 * @param startDate 播放时间
	 * @param endDate 播放时间
	 */
	@RequestMapping(value = "/ajax_mediaTop_list.do")
	public void ajaxMediaTopList(HttpServletResponse response, String name, Integer theatreTime, String startDate, String endDate, Integer limitSize) {
		try {
			limitSize = (limitSize == null ? 30 : limitSize);
			List<AdTheatreTopVO> list = adTheatreTvStatService.getMediaTop(name, theatreTime, startDate, endDate, limitSize);
			/**
			 * 3.返回结果
			 */
			JSONObject jsonobj = new JSONObject();
			jsonobj.put("data", list);
			jsonobj.put("code", Constants.CODE_SUCCESS);
			ajaxJson(jsonobj.toString(), response);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			ajaxJson("{\"code\":\"0\",\"msg\":\"获取列表失败\"}", response);
		}
	}

	@RequestMapping(value = "/ajax_topList.do")
	public void ajaxTopList(HttpServletResponse response, Integer theatreTime, String startDate, String endDate) {
		try {
			List<AdTheatreTopVO> tvList = adTheatreTvStatService.getTvTop(theatreTime, startDate, endDate);
			List<AdTheatreTopVO> mediaList = adTheatreTvStatService.getMediaTop(null, theatreTime, startDate, endDate, null);
			JSONObject listJson = new JSONObject();
			listJson.put("tv", tvList);
			listJson.put("media", mediaList);
			/**
			 * 3.返回结果
			 */
			JSONObject jsonobj = new JSONObject();
			jsonobj.put("data", listJson);
			jsonobj.put("code", Constants.CODE_SUCCESS);
			ajaxJson(jsonobj.toString(), response);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			ajaxJson("{\"code\":\"0\",\"msg\":\"获取列表失败\"}", response);
		}
	}

	/**
	 * to 对标页
	 * @param ids 对标的剧id
	 * @param startDate 日期范围
	 * @param endDate 日期范围
	 * @param day 日期范围的天数
	 */
	@RequestMapping(value = "/benchmarking.do")
	public String benchmarking(@RequestParam String ids, String startDate, String endDate, Integer day, HttpServletRequest request){
		request.setAttribute("ids", ids);
		request.setAttribute("startDate", startDate);
		request.setAttribute("endDate", endDate);
		request.setAttribute("day", day);
		// 参数转型
		List<Integer> idList = Arrays.stream(ids.split(",")).map(s -> Integer.parseInt(s.trim()))
				.collect(Collectors.toList());
		// 获取剧列表
		List<AdTv> tvList = adTvService.getListByIds(idList);
		request.setAttribute("tvList", tvList);
		return "analysis/tv_benchmarking";
	}

	@RequestMapping(value = "/add_benchmarking_tv.do")
	public String addTv(ModelMap model, String ids, String startDate, String endDate, Integer day) {
		model.put("ids", ids);
		model.put("startDate", startDate);
		model.put("endDate", endDate);
		model.put("day", day);
		return "analysis/benchmark_tv_choose";
	}
}
