package com.goafanti.order.controller;


import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.AFTConstants;
import com.goafanti.common.controller.CertifyApiController;
import com.goafanti.common.utils.excel.NewExcelUtil;
import com.goafanti.core.mybatis.page.Pagination;
import com.goafanti.core.shiro.token.TokenManager;
import com.goafanti.order.bo.*;
import com.goafanti.order.bo.outStatistics.OutOrderNewSignBo;
import com.goafanti.order.bo.outStatistics.OutOrderSalesSourceAmount;
import com.goafanti.order.service.OrderStatisticsService;
import com.goafanti.order.service.ProjectStatisticsService;
import com.goafanti.order.service.UserStaticticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping(value = "/api/admin/statistis")
public class StatisticsApiController extends CertifyApiController {

	@Value(value = "${upload.path}")
	private String			uploadPath	= null;
	@Autowired
	private ProjectStatisticsService	projectStatisticsService;
	@Autowired
	private OrderStatisticsService orderStatisticsService;
	@Autowired
	private UserStaticticsService userStaticticsService;



	/**
	 * 	项目汇总列表
	 * 	项目统计表点击弹出得详情列表
	 */
	@RequestMapping(value="/selectTaskList" ,method = RequestMethod.GET)
	public Result selectTaskList(InputProjectStatistics ps){
		Result res=new Result();
		res.setData(projectStatisticsService.selectTaskList(ps));
		return res;
	}



	/**
	 * 项目汇总列表导出xls
	 * @throws IOException
	 */
	@RequestMapping(value = "/exporTaskList", method = RequestMethod.GET)
	public Result exporTaskList(InputProjectStatistics ib,HttpServletResponse response) {
			Result res = res();
			if (!TokenManager.hasRole(AFTConstants.SUPERADMIN)){
				res.getError().add(buildError("权限不足"));
				return res;
			}
			return projectStatisticsService.exporTaskList(ib,response);
	}


	/**
	 * 	项目省份统计列表
	 */
	@RequestMapping(value="/selectProvincialStatistics" ,method = RequestMethod.GET)
	public Result selectProvincialStatistics(InputProvincialStatistics is){
		Result res=new Result();
		res.setData(projectStatisticsService.selectProvincialStatistics(is));
		return res;
	}


	/**
	 * 	项目省份统计列表导出xls
	 * @throws IOException
	 */
	@RequestMapping(value = "/exporProvincialList", method = RequestMethod.GET)
	public Result exporProvincialList(InputProvincialStatistics ib){
		return projectStatisticsService.exporProvincialList(ib);
		}

	/**
	 * 	订单销售来源统计
	 */
	@RequestMapping(value="/orderSalesSource" ,method = RequestMethod.GET)
	public Result orderSalesSource(InputOrderSalesSource in){
		Result res=new Result();
		res.setData(orderStatisticsService.orderSalesSource(in));
		return res;
	}

	/**
	 * 	订单销售来源统计
	 */
	@RequestMapping(value="/orderSalesSource/export" ,method = RequestMethod.GET)
	public Result orderSalesSourceExport(InputOrderSalesSource in){
		if (in.getSort()==null||in.getSort()==1){
		List<OutOrderSalesSource>  list = (List<OutOrderSalesSource>) orderStatisticsService.orderSalesSource(in);
		NewExcelUtil<OutOrderSalesSource> excelUtil=new NewExcelUtil<>(OutOrderSalesSource.class);
		StringBuffer str = getStringBuffer(in);
		return excelUtil.exportExcel(list,"订单销售来源统计表",uploadPath,str.toString());
		}else if (in.getSort()==0){
			List<OutOrderSalesSourceAmount>  list = (List<OutOrderSalesSourceAmount>) orderStatisticsService.orderSalesSource(in);
			NewExcelUtil<OutOrderSalesSourceAmount> excelUtil=new NewExcelUtil<>(OutOrderSalesSourceAmount.class);
			StringBuffer str = getStringBuffer(in);
			return excelUtil.exportExcel(list,"订单销售来源统计表",uploadPath,str.toString());
		}
		return new Result();
	}

	private StringBuffer getStringBuffer(InputOrderSalesSource in) {
		StringBuffer str=new StringBuffer("搜索条件=》");
		if (in.getDepName()!=null){
			str=str.append("部门：").append(in.getDepName()).append(";");
		}
		if(in.getProvinceName()!=null){
			str=str.append("省份：").append(in.getProvinceName()).append(";");
		}
		if (in.getSort()!=null){
			str=str.append("排序：").append(in.getSort()==1?"签单数":"签单金额").append(";");
		}
		if (in.getStartDate()!=null && in.getEndDate()!=null){
			str=str.append("时间：").append(in.getStartDate()).append("～")
					.append(in.getEndDate()).append(";");
		}
		return str;
	}

	/**
	 * 签单客户汇总
	 */
	@RequestMapping(value = "/signSummary",method = RequestMethod.GET)
	public Result signSummary(InputSignSummary in){
		Result res = new Result();
		res.data(userStaticticsService.signSummary(in));
		return res;
	}

	/**
	 * 签单客户汇总导出
	 */
	@RequestMapping(value = "/signSummary/export",method = RequestMethod.GET)
	public Result signSummaryExport(InputSignSummary in){
		List<OutSignSummary>  list = userStaticticsService.signSummaryList(in);
		NewExcelUtil<OutSignSummary> excelUtil=new NewExcelUtil<>(OutSignSummary.class);
		return excelUtil.exportExcel(list,"签单客户汇总表",uploadPath);
	}

	/**
	 * 签单客户统计表
	 */
	@RequestMapping(value = "/signStatistics",method = RequestMethod.GET)
	public Result signStatistics(InputSignStatistics in){
		Result res = new Result();
		res.data(userStaticticsService.signStatistics(in));
		return res;
	}

	/**
	 * 签单客户统计表导出
	 */
	@RequestMapping(value = "/signStatistics/export",method = RequestMethod.GET)
	public Result signStatisticsExport(InputSignStatistics in){
		List<OutSignStatistics> list = userStaticticsService.signStatisticsGetList(in);
		NewExcelUtil<OutSignStatistics> excelUtil=new NewExcelUtil<>(OutSignStatistics.class);
		return excelUtil.exportExcel(list,"签单客户统计表",uploadPath);
	}

	/**
	 * 销售来源订单列表
	 * @param in
	 * @return
	 */
	@RequestMapping(value="/signStatistics/orderList", method = RequestMethod.GET)
	public Result orderNewList(InputSignStatistics in ){
		Result res=new Result();
		res.data(userStaticticsService.signStatisticsOrderList(in));
		return res;
	}

	/**
	 * 订单项目数据统计
	 * @param in
	 * @return
	 */
	@RequestMapping(value="/orderProject", method = RequestMethod.GET)
	public Result<Pagination<OutOrderProject>> orderProject(InputOrderProject in ){
		Result res=new Result();
		res.data(userStaticticsService.orderProject(in));
		return res;
	}


	/**
	 * 订单项目数据统计导出
	 */
	@RequestMapping(value = "/orderProject/export",method = RequestMethod.GET)
	public Result orderProjectExport(InputOrderProject in){
		in.setPageSize(999999);
		in.setPageNo(1);
		Pagination<OutOrderProject> p = userStaticticsService.orderProject(in);
		List<OutOrderProject> list = (List<OutOrderProject>) p.getList();

		NewExcelUtil<OutOrderProject> excelUtil=new NewExcelUtil<>(OutOrderProject.class);
		return excelUtil.OrderMergeAndExportExcel(list,"订单项目数据统计表",uploadPath);
//		return excelUtil.TestExportExcel(list,"订单项目数据统计表",uploadPath);
	}


	/**
	 * 新老订单统计导出
	 */
	@RequestMapping(value = "/orderNewSign/export",method = RequestMethod.GET)
	public Result orderNewSignExport(InputOrderNewSignBo in,HttpServletResponse response){
		in.setPageSize(999999);
		in.setPageNo(1);
		String str=userStaticticsService.getOrderNewSignTime(in);
		List<OutOrderNewSignBo> list = userStaticticsService.orderNewSignToList(in);
		NewExcelUtil<OutOrderNewSignBo> excelUtil=new NewExcelUtil<>(OutOrderNewSignBo.class);


		return excelUtil.exportExcel(list,str,response);
//		return excelUtil.TestExportExcel(list,"订单项目数据统计表",uploadPath);
	}




}
