package com.goafanti.report.controller;

import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
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.RestController;

import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.AFTConstants;
import com.goafanti.common.constant.ErrorConstants;
import com.goafanti.common.controller.BaseApiController;
import com.goafanti.common.utils.DateUtils;
import com.goafanti.report.enums.OrderBy;
import com.goafanti.report.enums.SalesReportOrderField;
import com.goafanti.report.enums.TimeType;
import com.goafanti.report.service.SalesReportServiceImpl;

@RestController
@RequestMapping(value = "/api/admin/report")
public class ReportApiController extends BaseApiController {
	@Autowired
	SalesReportServiceImpl reportService;

	@RequestMapping(value = "/sales/personal", method = RequestMethod.GET)
	public Result personal(@RequestParam(required = false) String orderField,
			@RequestParam(required = false) String order, @RequestParam(required = false) String pSize,
			@RequestParam(required = false) String pNo, @RequestParam(required = false) String date,
			@RequestParam(required = false) String depName, @RequestParam(required = false) String name,
			@RequestParam(required = false) String position) {
		Date startTime = null;
		try {
			startTime = StringUtils.isNotBlank(date) ? DateUtils.parseDate(date, AFTConstants.YYYYMMDDHHMMSS)
					: new Date();
		} catch (Exception e) {
			startTime = new Date();
		}
		if (StringUtils.isBlank(depName)) {
			depName = null;
		}
		if (StringUtils.isBlank(name)) {
			name = null;
		}
		if (StringUtils.isBlank(position)) {
			position = null;
		}
		Integer pageSize = StringUtils.isBlank(pSize) ? null : handlePageSize(pSize);
		Integer pageNo = StringUtils.isBlank(pNo) ? null : handlePageNo(pNo);
		if (pageSize != null && pageNo != null) {
			pageNo = (pageNo - 1) * pageSize;
		}
		return res().data(reportService.getPersonSalesReports(SalesReportOrderField.getField(orderField),
				OrderBy.getField(order), pageSize, pageNo, startTime, depName, name, position));
	}

	@RequestMapping(value = "/sales/total", method = RequestMethod.GET)
	public Result total(String date, @RequestParam(required = false) String depName,
			@RequestParam(required = false) String name, @RequestParam(required = false) String position) {
		Date startTime = null;
		try {
			startTime = DateUtils.parseDate(date, AFTConstants.YYYYMMDDHHMMSS);
		} catch (Exception e) {
			startTime = new Date();
		}
		if (StringUtils.isBlank(depName)) {
			depName = null;
		}
		if (StringUtils.isBlank(name)) {
			name = null;
		}
		if (StringUtils.isBlank(position)) {
			position = null;
		}
		return res().data(reportService.getReportTotal(startTime, depName, name, position));
	}

	@RequestMapping(value = "/sales/department", method = RequestMethod.GET)
	public Result department(String date) {
		Date startTime = null;
		try {
			startTime = DateUtils.parseDate(date, AFTConstants.YYYYMMDDHHMMSS);
		} catch (Exception e) {
			startTime = new Date();
		}
		return res().data(reportService.getDepartmentSalesReports(startTime));
	}

	@RequestMapping(value = "/sales/doStatistics", method = RequestMethod.GET)
	public Result doStatistics(String date) {
		Date dateTime = null;
		try {
			dateTime = DateUtils.parseDate(date, AFTConstants.YYYYMMDDHHMMSS);
		} catch (Exception e) {
			dateTime = new Date();
		}
		Calendar now = Calendar.getInstance();
		now.setTime(dateTime);
		now.set(Calendar.MILLISECOND, 0);
		now.set(Calendar.SECOND, 0);
		now.set(Calendar.MINUTE, 0);
		now.set(Calendar.HOUR_OF_DAY, 17);
		Date start = new Calendar.Builder()
				.setDate(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH) - 1)
				.setTimeOfDay(17, 0, 0, 0).build().getTime();
		// 统计从指定日期前一天的17点到指定日期17点的数据，算作改日期数据
		return res().data(reportService.insertDailyReports(start, now.getTime()));
	}
	/**
	 * 客户营销统计表
	 * @param depId
	 * @param type
	 * @return
	 */
	@RequestMapping(value = "/sales/marketingStatistics", method = RequestMethod.GET)
	public Result marketingStatistics(String depId,Integer type) {
		Result res=new Result();

		if (null==type||type>1) {
			 res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "参数错误", "参数"));
	            return res;
		}
		if (Integer.valueOf(type)==0) {
			return res.data(reportService.marketingStatistics(depId));
		}else {
			return res.data(reportService.marketingDepStatistics(depId));
		}
	}
	/**
	 * 客户营销统计表(总计)
	 * @param depId
	 * @param type
	 * @return
	 */
	@RequestMapping(value = "/sales/countMarketingStatistics", method = RequestMethod.GET)
	public Result countMarketingStatistics(String depId,Integer type) {
		Result res=new Result();

		if (null==type||type>1) {
			 res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "参数错误", "参数"));
	            return res;
		}
		if (Integer.valueOf(type)==0) {
			return res.data(reportService.countMarketingStatistics(depId));
		}else {
			return res.data(reportService.countMarketingDepStatistics(depId));
		}
	}

	/**
	 * 客户营销时段统计表
	 * @param depId
	 * @param type
	 * @return
	 */
	@RequestMapping(value = "/sales/sometimeMarketingStatistics", method = RequestMethod.GET)
	public Result sometimeMarketingStatistics(String depId,Integer timeType,Integer type,String startTime,String endTime) {
		Result res=new Result();
		if (null==type||type>1) {
			 res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "参数错误", "参数"));
	            return res;
		}
		if (null==timeType||timeType>4) {
			 res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "日期类型参数参数错误", "日期类型参数"));
	            return res;
	    }
	   if (timeType==4&&null==startTime&&null==endTime) {
			 res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "日期类型参数参数错误", "日期类型参数"));
	            return res;
		}
		if (Integer.valueOf(type)==0) {
			return res.data(reportService.sometimeMarketingStatistics(depId,timeType,startTime,endTime));
		}else {
			return res.data(reportService.sometimeMarketingDepStatistics(depId,timeType,startTime,endTime));
		}
	}
	/**
	 * 客户营销时段统计表(总计)
	 * @param depId
	 * @param type
	 * @return
	 */
	@RequestMapping(value = "/sales/countsometimeMarketingStatistics", method = RequestMethod.GET)
	public Result countsometimeMarketingStatistics(String depId,Integer timeType,Integer type,String startTime,String endTime) {
		Result res=new Result();
		if (null==type||type>TimeType.ZS.getCode()) {
			 res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "参数错误", "参数"));
	            return res;
		}
		if (null==timeType||timeType>TimeType.ZS.getCode()) {
			 res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "日期类型参数参数错误", "日期类型参数"));
	            return res;
	    }
	   if (timeType==TimeType.ZS.getCode()&&null==startTime&&null==endTime) {
			 res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "日期类型参数参数错误", "日期类型参数"));
	            return res;
		}
	  return res.data(reportService.countSometimeMarketingStatistics(depId,timeType,startTime,endTime));

	}
	/**
	 * 派单报表统计
	 */
	@RequestMapping(value="/technicalDepStatistics" , method=RequestMethod.GET)
	public Result technicalDepStatistics(String depId,Integer timeType,String startTime,String endTime,String publishStatus,String taskStatus){
		Result res=new Result();
		if (null==timeType||timeType>TimeType.ZS.getCode()) {
			 res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "日期类型参数参数错误", "日期类型参数"));
	            return res;
	    }
	   if (timeType==TimeType.ZS.getCode()&&null==startTime&&null==endTime) {
			 res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "日期类型参数参数错误", "日期类型参数"));
	            return res;
		}
	   return res.data(reportService.technicalDepStatistics(depId,timeType,startTime,endTime,publishStatus,taskStatus));

	}

	/**
	 * 客户资料统计报表
	 */
	@RequestMapping(value="/userDataStatistics" , method=RequestMethod.GET)
	public Result userDataStatistics(String deps){
		Result res=new Result();
		return res.data(reportService.userDataStatistics(deps));

	}

	/**
	 * 客户资料统计报表总计
	 */
	@RequestMapping(value="/userDataStatisticsCount" , method=RequestMethod.GET)
	public Result userDataStatisticsCount(String deps){
		Result res=new Result();
		return res.data(reportService.userDataStatisticsCount(deps));

	}


}
