package cn.com.countrygarden.bi.main.controller.manager.map;


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

import cn.com.countrygarden.bi.main.dao.twerp.hm.ManagerMapForErpMapper;
import cn.com.countrygarden.bi.main.dto.HouseKeeperDTO;
import cn.com.countrygarden.bi.main.dto.HouseKeeperParamVo;
import cn.com.countrygarden.bi.main.model.dup.DupOrgan;
import cn.com.countrygarden.bi.main.model.manager.map.ManagerMapCache;
import cn.com.countrygarden.bi.main.service.UserRoleService;
import cn.com.countrygarden.bi.main.service.dup.impl.ExcelHelper;
import cn.com.countrygarden.bi.main.service.manager.map.ManagerMapCacheService;
import cn.com.countrygarden.bi.main.service.manager.map.util.ManagerMapUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import cn.com.countrygarden.bi.main.controller.BaseController;
import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.model.manager.map.FengHuangManagerData;
import cn.com.countrygarden.bi.main.service.manager.map.ManagerMapService;

import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

@RestController("ManagerMapIndexsController")
@RequestMapping("/api/manager/map")
public class ManagerMapController extends BaseController{

	@Autowired
	private ManagerMapService managerMapInexService;
	@Autowired
	private ManagerMapCacheService managerMapCacheService;
	@Autowired
	private ManagerMapUtil managerMapUtil;

	@RequestMapping(value="/cache/map",produces="application/json;charset=utf-8")
	public void cacheManagerMapData() throws InterruptedException {
		managerMapUtil.cacheManagerMapData();
	}

	@RequestMapping(value="/cache/houseKeeper",produces="application/json;charset=utf-8")
	public void cacheHouseKeeperData() throws InterruptedException {
		managerMapUtil.cacheHouseKeeperData();
	}

	@RequestMapping(value="/cache/households",produces="application/json;charset=utf-8")
	public void cacheHouseholdsData() throws InterruptedException {
		managerMapUtil.cacheHouseholdsData();
	}



	@GetMapping(value="/list",produces="application/json;charset=utf-8")
	public Result list(@RequestParam(value="organName" , required = false , defaultValue = "") String organName,
							@RequestParam(value="page" , required = false , defaultValue = "1") Integer page,
							@RequestParam(value="size" , required = false , defaultValue = "10") Integer size) throws InterruptedException {
		ManagerMapCache cache=new ManagerMapCache();
		cache.setOrganName(organName);
		return managerMapCacheService.queryPageList(cache,getAccessToken().getUserCode(), page,size);
	}


	@GetMapping(value="/organ/tree",produces="application/json;charset=utf-8")
	public Result organTree(){
		return managerMapInexService.getOrganTreeData(getAccessToken().getUserCode());
	}

	@GetMapping(value="/comm/tree",produces="application/json;charset=utf-8")
	public Result commTree(@RequestParam(value="organName",required=true) String organName){
		return managerMapInexService.getCommTreeDataByOrgan(organName,getAccessToken().getUserCode());
	}

	
	@GetMapping(value="/one",produces="application/json;charset=utf-8")
	public Result findById(@RequestParam(value="id",required=true) Integer id,HttpServletRequest request) {
		return managerMapInexService.findById(id);
	}
	
	@GetMapping(value="/code",produces="application/json;charset=utf-8")
	public Result findByOrgancode(FengHuangManagerData managerData,
			@RequestParam(value="page" , required = false , defaultValue = "1") Integer page,
			@RequestParam(value="size" , required = false , defaultValue = "10") Integer size,
			HttpServletRequest request) {
		
		return managerMapInexService.findByOrgancode(managerData, page, size);
	}
	
	@GetMapping(value="/count",produces="application/json;charset=utf-8")
	public Result queryHouseKeeperCount(HttpServletRequest request) {
		return managerMapInexService.countHouseKeeper();
	}

	/**
	 * 导出管家地图首页
	 *
	 * @param requestData
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/export", produces = "application/json; charset=utf-8", method = RequestMethod.GET)
	public Result outputSummary(ManagerMapCache cache,
								@RequestBody(required = false) JSONObject requestData,
								HttpServletRequest request, HttpServletResponse response) {
//        Result result = managerMapService.exportList(houseKeeperParamVo);
		List<ManagerMapCache> list = managerMapCacheService.queryManagerMapExcelList(cache, getAccessToken().getUserCode());
		ExcelHelper helper = new ExcelHelper();
		Workbook workbook = new XSSFWorkbook();
		Sheet sheet = workbook.createSheet();
		//创建头部
		helper.createManagerMapExcelHeader(workbook, sheet);
		//创建正文体
		helper.createManagerMapExcelBody(list, workbook, sheet);
		response.setContentType("multipart/form-data");
		String fileName = "export.xlsx";
		try {
			fileName = URLEncoder.encode("管家地图首页.xlsx", "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
		OutputStream outputStream = null;
		try {
			outputStream = response.getOutputStream();
			workbook.write(outputStream);
		} catch (Exception ex) {

		} finally {
			try {
				workbook.close();
			} catch (Exception ex) {

			}
		}
		return null;
	}
}
