package org.springblade.community.controller;

import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.springblade.community.entity.Residential;
import org.springblade.community.excel.controller.ExportExcelController;
import org.springblade.community.excel.controller.ImportExcelController;
import org.springblade.community.excel.model.AgencyImportModule;
import org.springblade.community.excel.model.EnterpriseImportModule;
import org.springblade.community.excel.model.ResidentialImportModule;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * title: ExcelController
 * package: com.sptg.gateway.controller.common
 * description: excel处理控制类
 *
 *
 *
 */
@RestController
@RequestMapping(value = "/common/excel")
//@RequiredArgsConstructor
@AllArgsConstructor
public class ExcelController {

	private final ImportExcelController importExcelController;
	private final ExportExcelController exportExcelController;

	private final static String REGEX_EXCEL2003 = "^.+\\.(?i)(xls)$";
	private final static String REGEX_EXCEL2007 = "^.+\\.(?i)(xlsx)$";

	private final static String RESIDENTIAL_NAME = "小区导入模板";
	private final static String UNIT_NAME = "单元导入模板";
	private final static String ROOM_NAME = "房间导入模板";
	private final static String USER_NAME = "住户导入模板";
	private final static String USER_CARD_NAME = "住户卡导入模板";
	private final static String ACCESS_CARD_NAME = "卡管理导入模板";

	/**
	 * 功能描述： 生成导入数据时产生的错误数据表格
	 * @param data 导入数据产生的错误数据
	 * @param response
	 */
	@RequestMapping(value="/exportErrorImportData")
	public void exportErrorImportResidential(String data,String flag, HttpServletResponse response){
		switch (flag){
			case "1":
//				List<SmartDevice> list = JSONObject.parseArray(data,SmartDevice.class);
//				this.exportExcelController.exportErrorHomeDevice(list,response);
				List<Residential> list = JSONObject.parseArray(data,Residential.class);
				this.exportExcelController.exportErrorResidential(list,response);
				break;
			default:
				break;
		}

	}

	/**
	 * 功能描述:导入Excel
	 * @param flag 1 智家设备 2 企业信息 3 员工信息
	 * @return
	 *
	 *
	 */
	@RequestMapping(value = "/importExcel")
	public R importExcel(@RequestParam("file") MultipartFile multipartFile, @RequestParam("flag") String flag, String isUpdate, HttpServletResponse response){
		if(null == flag || multipartFile == null){
			return R.data("参数错误！");
		}
		String fileName = multipartFile.getOriginalFilename();
		if (!fileName.matches(REGEX_EXCEL2003) && !fileName.matches(REGEX_EXCEL2007)) {
			return R.data("文件格式不合法！");
		}
		switch (flag){
//			case "1":
//				this.importExcelController.readHomeDeviceExcel(multipartFile);
//				break;
//			case "2":
//				this.importExcelController.readEnterpriseExcel(multipartFile);
//				break;
//			case "3":
//				this.importExcelController.readEnterpriseStaffExcel(multipartFile);
//				break;
			case "1":
				this.importExcelController.readAgencyExcel(multipartFile);
				break;
			case "2":
				return this.importExcelController.readResidentialExcel(multipartFile, response);
//			case "6":
//				this.importExcelController.readCarExcel(multipartFile, response);
//				break;
			default:
				break;
		}
		return null;
	}
//    /**
//     * 功能描述:导入Excel(旧版本)
//     * @param flag 1 小区 2 单元 3 房间 4 住户 5 住户卡 6 卡管理
//     * @return
//     *
//     *
//     */
//    @RequestMapping(value = "/importExcel")
//    public ResultBean<Boolean> importExcel_old(@RequestParam("file") MultipartFile multipartFile, String flag, String isUpdate, HttpServletResponse response) throws IOException {
//        if(null == flag || multipartFile == null){
//            return new ResultBean<>(ExcelResultEnum.PARAMETER_ANOMALY);
//        }
//        String fileName = multipartFile.getOriginalFilename();
//        if (!fileName.matches(REGEX_EXCEL2003) && !fileName.matches(REGEX_EXCEL2007)) {
//            return new ResultBean<>(ExcelResultEnum.FILE_TYPE_ERROR);
//        }
//        Account onLineUser = this.securityServiceInterface.getOnLineUser();
//        ResultBean<Boolean> resultBean ;
//        switch (flag){
//            case "1":
//                ResultBean<Object> residentialDtoResultBean = this.importExcelController.readResidentialExcel(multipartFile,onLineUser);
//                if(residentialDtoResultBean.getList() != null && residentialDtoResultBean.getCode().equals(ResultEnum.SUCCESS.getCode())){
//                    resultBean = this.residentialFeign.importResidentialList(residentialDtoResultBean.getList());
//                }else{
//                    resultBean = new ResultBean<>(residentialDtoResultBean.getCode(),residentialDtoResultBean.getMsg());
//                }
//                break;
//
//            case "2":
//                ResultBean<UnitDto> unitDtoResultBean = this.importExcelController.readUnitExcel(multipartFile,onLineUser);
//                if(unitDtoResultBean.getData() != null && unitDtoResultBean.getCode().equals(ResultEnum.SUCCESS.getCode())){
//                    resultBean = this.unitFeign.importUnitList(unitDtoResultBean.getData());
//                }else{
//                    resultBean = new ResultBean<>(unitDtoResultBean.getCode(),unitDtoResultBean.getMsg());
//                }
//                break;
//            case "3":
//                ResultBean<RoomDto> roomDtoResultBean = this.importExcelController.readRoomExcel(multipartFile,onLineUser);
//                if(roomDtoResultBean.getData() != null && roomDtoResultBean.getCode().equals(ResultEnum.SUCCESS.getCode())){
//                    resultBean = this.roomFeign.importRoomList(roomDtoResultBean.getData());
//                }else{
//                    resultBean = new ResultBean<>(roomDtoResultBean.getCode(),roomDtoResultBean.getMsg());
//                }
//                break;
//            case "4":
//                ResultBean<UserDto> userDtoResultBean = this.importExcelController.readUserExcel(multipartFile,onLineUser,isUpdate);
//                if(userDtoResultBean.getData() != null && userDtoResultBean.getCode().equals(ResultEnum.SUCCESS.getCode())){
//                    resultBean = this.userFeign.importUserList(userDtoResultBean.getData());
//                }else{
//                    resultBean = new ResultBean<>(userDtoResultBean.getCode(),userDtoResultBean.getMsg());
//                }
//                break;
//            case "5":
//                ResultBean<AccessCard> userCardResultBean = this.importExcelController.readUserCardExcel(multipartFile,onLineUser);
//                if(userCardResultBean.getList() != null && userCardResultBean.getCode().equals(ResultEnum.SUCCESS.getCode())){
//                    resultBean = this.accessCardFeign.importAccessCardList(userCardResultBean.getList());
//                }else{
//                    resultBean = new ResultBean<>(userCardResultBean.getCode(),userCardResultBean.getMsg());
//                }
//                break;
//            case "6":
//                ResultBean<AccessCard> accessCardResultBean = this.importExcelController.readAccessCardExcel(multipartFile,onLineUser);
//                if(accessCardResultBean.getList() != null && accessCardResultBean.getCode().equals(ResultEnum.SUCCESS.getCode())){
//                    resultBean = this.accessCardFeign.importAccessCardList(accessCardResultBean.getList());
//                }else{
//                    resultBean = new ResultBean<>(accessCardResultBean.getCode(),accessCardResultBean.getMsg());
//                }
//                break;
//            default:
//                resultBean = new ResultBean<>(ExcelResultEnum.PARAMETER_ANOMALY);
//                break;
//        }
//        return resultBean;
//    }
//


	/**
	 * 功能描述: 导出excel模板
	 * @param flag 1 智家设备 2 企业信息 3 员工信息 4 园区/园区  5 小区/园区分区
	 *
	 *
	 */
	@PostMapping(value = "/exportExcelTemplet")
	public void exportExcelTemplet(HttpServletResponse response, String flag, BladeUser bladeUser) throws IOException {
		switch (flag) {
//			case "1":
//				this.exportExcelController.writeExcel(response,new ArrayList<>(),RESIDENTIAL_NAME,null,new HomeDeviceImportModule());
//				break;
//			case "2":
//				this.exportExcelController.writeExcelWithResidential(response,new ArrayList<>(), new EnterpriseImportModule(), bladeUser);
//				this.exportExcelController.writeExcel(response,new ArrayList<>(),RESIDENTIAL_NAME,null,new EnterpriseImportModule());
//				break;
//			case "3":
//				this.exportExcelController.writeExcel(response,new ArrayList<>(),RESIDENTIAL_NAME,null,new EnterpriseStaffImportModule());
//				break;
			case "1":
				this.exportExcelController.writeExcelWithArea(response,new ArrayList<>(), new AgencyImportModule());
				break;
			case "2":
				this.exportExcelController.writeExcel(response,new ArrayList<>(),RESIDENTIAL_NAME,null,new ResidentialImportModule());
				break;
			default:
				break;
		}
	}

//	@PostMapping(value = "/exportExcelTemplet2")
//	public void testExportExcel(HttpServletResponse response, String flag){
//		MakeExcel me = new MakeExcel();
//		me.testCascade2007(response);
//	}

//	@PostMapping(value = "/getAreaMapDataWithCode")
//	public List<Map<String, Object>> getMap(){
//		List<SysArea> province = sysAreaService.getCityOrAreaByParentCode(100000L);
//		List<Map<String, Object>> list = new ArrayList<>();
//
//		province.forEach(provi -> {
//			Map<String, Object> proviMap = new HashMap<>();
//			proviMap.put("name", provi.getSName() + "_" + provi.getSCode());
//			List<SysArea> citys = sysAreaService.getCityOrAreaByParentCode(provi.getSCode());
//			List<Map<String, Object>> cityList = new ArrayList<>();
//			citys.forEach(city -> {
//				Map<String, Object> map = new HashMap<>();
//				map.put("name", city.getSName() + "_" + city.getSCode());
//				cityList.add(map);
//
//				List<SysArea> areas = sysAreaService.getCityOrAreaByParentCode(city.getSCode());
//				List<String> areaList = new ArrayList<>();
//				areas.forEach(area -> {
//					areaList.add(area.getSName() + "_" + area.getSCode());
//				});
//				map.put("areaList", areaList);
//			});
//			proviMap.put("cityList", cityList);
//
//			list.add(proviMap);
//		});
//
//		return list;
//	}

	/**
	 * 功能描述: 导出excel
	 * @param flag 1 小区 2 单元 3 房间 4 住户  5 广告观看统计 6 广告观看统计排行 7 小区住户统计
	 *             8 住户出入管理导出  9 卡管理导出 10 实施项目导出 11设备定位导出 12 设备安装导出
	 *            13 设备检测导出
	 *
	 *
	 */
	@RequestMapping(value = "/exportExcel/{flag}")
	public void exportExcel(HttpServletResponse response, @PathVariable("flag") String flag) {
        /*Account onLineUser = this.securityServiceInterface.getOnLineUser();
        switch (flag){
            case "1":
                this.exportExcelController.exportResidential(response,onLineUser);
                break;
            case "2":
                this.exportExcelController.exportUnit(response,onLineUser);
                break;
            case "3":
                this.exportExcelController.exportRoom(response,onLineUser);
                break;
            case "4":
                this.exportExcelController.exportUser(response,onLineUser);
                break;
            case "5":
                this.exportExcelController.exportAdWatch(response,onLineUser);
                break;
            case "6":
                this.exportExcelController.exportAdWatchRank(response,onLineUser);
                break;
            case "7":
                this.exportExcelController.exportStatisticsUser(response,onLineUser);
                break;
            case "8":
                this.exportExcelController.exportUserAccess(response,onLineUser);
                break;
            case "9":
                this.exportExcelController.exportAccessCard(response,onLineUser);
                break;
            case "10":
                this.exportExcelController.exportProject(response,onLineUser);
                break;
            case "11":
                this.exportExcelController.exportPosition(response,onLineUser);
                break;
            case "12":
                this.exportExcelController.exportDeviceInstallation(response,onLineUser);
                break;
            case "13":
                this.exportExcelController.exportDeviceTest(response,onLineUser);
                break;
            default:
                break;
        }*/
	}


}
