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

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

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

import cn.com.countrygarden.bi.main.dto.HouseKeeperDTO;
import cn.com.countrygarden.bi.main.dto.HouseKeeperDataVo;
import cn.com.countrygarden.bi.main.dto.HouseKeeperParamVo;
import cn.com.countrygarden.bi.main.model.manager.map.ManagerHousekeeperCache;
import cn.com.countrygarden.bi.main.service.AccessTokenService;
import cn.com.countrygarden.bi.main.service.dup.impl.ExcelHelper;
import cn.com.countrygarden.bi.main.service.manager.map.ManagerHousekeeperCacheService;
import cn.com.countrygarden.bi.main.service.manager.map.ManagerMapService;

import org.apache.commons.lang3.StringUtils;
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.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
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.alibaba.fastjson.JSONObject;

import cn.com.countrygarden.bi.main.controller.BaseController;
import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.dao.twerp.hm.TbHsprCommunityMapper;
import cn.com.countrygarden.bi.main.model.manager.map.FenghuangHouseKeeperCommunication;

@RestController("HouseKeeperController")
@RequestMapping("/api/manager/houseKeeper")
public class HouseKeeperController extends BaseController{
	@Autowired
	private ManagerMapService managerMapService;
	@Autowired
	private ManagerHousekeeperCacheService managerHousekeeperCacheService;
	@Autowired
    private AccessTokenService accessTokenService;
	@Resource
	private TbHsprCommunityMapper tbHsprCommunityMapper;


	@GetMapping(value="/list",produces="application/json;charset=utf-8")
	public Result findPageList(HouseKeeperParamVo houseKeeperParamVo,
							   @RequestParam(value="page",required=false,defaultValue="1") Integer page,
								@RequestParam(value="size",required=false,defaultValue="10") Integer size,
			HttpServletRequest request){
		houseKeeperParamVo.setUserCode(getAccessToken().getUserCode());
		return managerHousekeeperCacheService.queryPageList(houseKeeperParamVo, page, size,getAccessToken());
	}
	@GetMapping(value="/name/tree",produces="application/json;charset=utf-8")
	public Result nameTree(){
		return managerMapService.getHouseKeeperNameTree();
	}


	@GetMapping(value="queryByName",produces="application/json;charset=utf-8")
	public Result queryByName( @RequestParam(value="name") String name,
							   @RequestParam(value="regionName") String regionName){
		return managerMapService.queryByName(regionName,name);
	}

	@GetMapping(value="queryByPhoneNum",produces="application/json;charset=utf-8")
	public Result queryByPhoneNum( @RequestParam(value="phoneNum") String phoneNum,
							   @RequestParam(value="regionName") String regionName){
		return managerMapService.queryByPhoneNum(regionName,phoneNum);
	}

	@GetMapping(value="/garden/tree",produces="application/json;charset=utf-8")
	public Result gardenDistrictTree(){
		return managerMapService.getGardenDistrictTree();
	}
	
	@GetMapping(value="/development/tree",produces="application/json;charset=utf-8")
	public Result developmentSubjectTree(){
		return managerMapService.getDevelopmentSubjectTree();
	}
	
	@GetMapping(value="/allCommName/tree",produces="application/json;charset=utf-8")
	public Result getAllCommNameTree(){
		return managerMapService.getAllCommNameTree(getAccessToken().getUserCode());
	}

	@PostMapping(value="/addOrUpdate")
	public Result addOrUpdateHouseKeeperInfo(ManagerHousekeeperCache managerHousekeeperCache
			, FenghuangHouseKeeperCommunication houseKeeperCommunication
			, HouseKeeperDataVo vo) {
		managerHousekeeperCacheService.editData(managerHousekeeperCache);
		return managerMapService.addOrUpdateHouseKeeperById(houseKeeperCommunication,vo);
	}
	
	/**
     * 导出管家信息
     *
     * @param requestData
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/export", produces = "application/json; charset=utf-8", method = RequestMethod.GET)
    public Result outputSummary(HouseKeeperParamVo houseKeeperParamVo,
                                @RequestBody(required = false) JSONObject requestData,
                                HttpServletRequest request, HttpServletResponse response) {
//        Result result = managerMapService.exportList(houseKeeperParamVo);
		List<HouseKeeperDTO> dtos = managerHousekeeperCacheService.queryDataList(houseKeeperParamVo,getAccessToken());
		ExcelHelper helper = new ExcelHelper();
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet();
        //创建头部
        helper.createHouseKeeperInfoHeader(workbook, sheet);
        //创建正文体
        helper.createHouseKeeperInfoBody(dtos, 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;
    }
}
