package cn.com.countrygarden.bi.main.controller.erp;

import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.exception.ServiceException;
import cn.com.countrygarden.bi.main.model.erp.JdbcConstants;
import cn.com.countrygarden.bi.main.repository.erp.JdbcPublicRepository;
import cn.com.countrygarden.bi.main.service.erp.ExcelArrearageService;
import cn.com.countrygarden.bi.main.service.erp.ExcelFamilyMemberService;
import cn.com.countrygarden.bi.main.service.erp.ExcelHouseOwnerService;
import cn.com.countrygarden.bi.main.service.erp.ExcelParkingService;
import cn.com.countrygarden.bi.main.service.erp.ExcelPrepaidAmountService;
import cn.com.countrygarden.bi.main.service.erp.ExcelRealAmountService;
import cn.com.countrygarden.bi.main.util.erp.ExcelData;
import cn.com.countrygarden.bi.main.util.erp.ExportExcelUtils;
import cn.com.countrygarden.bi.main.util.erp.ImportExcelUtils;

import org.apache.ibatis.executor.result.ResultMapException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author christ.lai
 * @Description excel导入控制类
 * @Date 13:53 2018/5/20
 */
@RestController
@Scope("prototype")
public class InitialExcelController extends HttpServlet {
    
	@Autowired
	private ExcelArrearageService excelArrearageService;
    
	//房屋资料
	@Autowired
	private ExcelHouseOwnerService excelHouseOwnerService;
    
	//家庭资料
	@Autowired
	private ExcelFamilyMemberService excelFamilyMemberService;
	
	// 车位资料
	@Autowired
	private ExcelParkingService excelParkingService;

	@Autowired
	private ExcelPrepaidAmountService excelPrepaidAmountService;
	
	//实收资料
	@Autowired
	private ExcelRealAmountService excelRealAmountService;

	@GetMapping(path = "/upload")
	public ModelAndView uploadArrearag1() {
		ModelAndView view = new ModelAndView();
		view.setViewName("upload");
		return view;
	}

	// 跳转到上传文件的页面
	@PostMapping(path = "/upload/date")
	public Result goUpload(JdbcConstants jdbcConstants) throws Throwable {
		
		System.out.println("this is initalExcelController : "+jdbcConstants);
		
		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		String tableName = jdbcConstants.getTableName();
		ServletContext application = attributes.getRequest().getServletContext();
		if (tableName.equals("house")) {
			// 初始化导入房屋和业主资料
			try {
				// 获取返回的值，如果是成功就不返回data
				String key = excelHouseOwnerService.getExcel(jdbcConstants, application);
				if (key.equals("success")) {
					return Response.response();
				} else {
					return Response.response(key);
				}

			} catch (Exception e) {
				e.printStackTrace();
				throw new ServiceException(e.getMessage());
			}
		} else if (tableName.equals("member")) {
			// 初始化导入家庭成员
			try {
				String key = excelFamilyMemberService.getExcel(jdbcConstants, application);
				if (key.equals("success")) {
					return Response.response();
				} else {
					return Response.response(key);
				}
			} catch (Exception e) {
				e.printStackTrace();
				throw new ServiceException(e.getMessage());
			}

			// 初始化导入车位
		} else if (tableName.equals("park")) {
			try {
				String key = excelParkingService.getExcel(jdbcConstants, application);
				if (key.equals("success")) {
					return Response.response();
				} else {
					return Response.response(key);
				}
			} catch (Exception e) {
				e.printStackTrace();
				throw new ServiceException(e.getMessage());
			}

			// 导入欠费资料
		} else if (tableName.equals("arrea")) {
			try {
				String key = excelArrearageService.getExcel(jdbcConstants, application);
				if (key.equals("success")) {
					return Response.response();
				} else {
					return Response.response(key);
				}
			} catch (Exception e) {
				e.printStackTrace();
				throw new ServiceException(e.getMessage());
			}

		} else if (tableName.equals("real")) {
			// 导入实收
			try {
				String key = excelRealAmountService.getExcel(jdbcConstants, application);
				if (key.equals("success")) {
					return Response.response();
				} else {
					return Response.response(key);
				}
			} catch (Exception e) {
				e.printStackTrace();
				throw new ServiceException(e.getMessage());
			}
		} else if (tableName.equals("prepaid")) {
			// 导入预存余额
			try {
				String key = excelPrepaidAmountService.getExcel(jdbcConstants, application);
				if (key.equals("success")) {
					return Response.response();
				} else {
					return Response.response(key);
				}
			} catch (Exception e) {
				throw new ServiceException("500,导入异常，请检查数据！{}" + e.getMessage());
			}
		}
		// return ResultUtil.success(ResultEnum.FILD_EROOR);
		return Response.responseFail("请选择正确资料类型");
	}
	
	@GetMapping(path = "/error/log")
	public Result getArrearage(String sheetName) throws Exception {
		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		ServletContext application = attributes.getRequest().getServletContext();
		ExcelData data = (ExcelData) application.getAttribute(sheetName);
		String sheet = "";
//		String tableName = sheetName;
		String tableName = sheetName.substring(0, sheetName.indexOf('-'));
		if ("house".equals(tableName)) {
			sheet = "房屋业主租户资料.xlsx";
		} else if ("member".equals(tableName)) {
			sheet = "家庭成员资料.xlsx";
		} else if ("park".equals(tableName)) {
			sheet = "车位资料.xlsx";
		} else if ("arrea".equals(tableName)) {
			sheet = "欠费资料.xlsx";
		} else if ("real".equals(tableName)) {
			sheet = "实收资料.xlsx";
		} else if ("prepaid".equals(tableName)) {
			sheet = "预收资料.xlsx";
		}
		ExportExcelUtils.exportExcel(attributes.getResponse(), sheet, data);
		// 去除属性
		application.removeAttribute(sheetName);
		return Response.response();
	}
}
