package com.sptg.gateway.controller.common.excel;

import com.alibaba.fastjson.JSONObject;
import com.sptg.common.core.beans.ExcelResultEnum;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.module.*;
import com.sptg.common.security.service.SecurityServiceInterface;
import com.sptg.estate.api.dto.*;
import com.sptg.estate.api.feigns.*;
import com.sptg.gateway.controller.common.excel.model.OrgExcelTemplate;
import com.sptg.permissions.api.entity.Account;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

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

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

    private final ImportExcelController importExcelController;
    private final ExportExcelController exportExcelController;
    private final ResidentialFeign residentialFeign;
    private final UnitFeign unitFeign;
    private final RoomFeign roomFeign;
    private final UserFeign userFeign;
    private final AccessCardFeign accessCardFeign;
    private final SecurityServiceInterface<Account> securityServiceInterface;

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

    private final static String AGENCY_NAME = "社区导入模板";
    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<ResidentialDto> list=JSONObject.parseArray(data,ResidentialDto.class);
                this.exportExcelController.exportErrorResidential(list,response);
                break;
            case "2":
                List<UnitDto> unitDtoList=JSONObject.parseArray(data,UnitDto.class);
                this.exportExcelController.exportErrorUnit(unitDtoList,response);
                break;
            case "3":
                List<RoomDto> roomDtoList=JSONObject.parseArray(data,RoomDto.class);
                this.exportExcelController.exportErrorRoom(roomDtoList,response);
                break;
            case "4":
                List<UserDto> userDtoList=JSONObject.parseArray(data,UserDto.class);
                this.exportExcelController.exportErrorUser(userDtoList,response);
                break;
            case "5":
                List<AccessCardDto> userCardDtoList=JSONObject.parseArray(data,AccessCardDto.class);
                this.exportExcelController.exportErrorUserCard(userCardDtoList,response);
                break;
            case "6":
                List<AccessCardDto> accessCardDtoList=JSONObject.parseArray(data,AccessCardDto.class);
                this.exportExcelController.exportErrorAccessCard(accessCardDtoList,response);
                break;
            case "8":
                List<ThirdUserDto> thirdUserDtoList = JSONObject.parseArray(data, ThirdUserDto.class);
                this.exportExcelController.exportErrorThirdUser(thirdUserDtoList, response);
                break;
            default:
                break;
        }

    }

    /**
     * 功能描述:导入Excel
     * @param flag 0 社区 1 小区 2 单元 3 房间 4 住户 5 住户卡 6 卡管理 8 第三方用户导入
     * @return
     *
     *
     */
    @RequestMapping(value = "/importExcel")
    public ResultBean<Object> importExcel(@RequestParam("file") MultipartFile multipartFile, String flag, String isUpdate){
        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<Object> resultBean ;
        switch (flag){
            case "0":
                 resultBean = this.importExcelController.readAgencyExcel(multipartFile,onLineUser);
                break;
            case "1":
                 resultBean = this.importExcelController.readResidentialExcel(multipartFile,onLineUser);
                break;
            case "2":
                resultBean = this.importExcelController.readUnitExcel(multipartFile,onLineUser);
                break;
            case "3":
                resultBean = this.importExcelController.readRoomExcel(multipartFile,onLineUser);
                break;
            case "4":
                resultBean = this.importExcelController.readUserExcel(multipartFile,onLineUser,isUpdate);
                break;
            case "5":
                resultBean = this.importExcelController.readUserCardExcel(multipartFile,onLineUser);
                break;
            case "6":
                resultBean = this.importExcelController.readAccessCardExcel(multipartFile,onLineUser);
                break;
            case "7":
                resultBean = this.importExcelController.readOrgExcel(multipartFile,onLineUser);
                break;
            case "8":
                resultBean = this.importExcelController.readThirdUserExcel(multipartFile,onLineUser);
                break;
            default:
                resultBean = new ResultBean<>(ExcelResultEnum.PARAMETER_ANOMALY);
                break;
        }
        return resultBean;
    }
//    /**
//     * 功能描述:导入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 0 社区 1 小区 2 单元 3 房间 4 住户  5 住户卡 6 卡管理
     *
     *
     */
    @RequestMapping(value = "/exportExcelTemplet")
    public void exportExcelTemplet(HttpServletResponse response, String flag) {
        switch (flag) {
            case "0":
                this.exportExcelController.writeExcelWithArea(response,new ArrayList<>(),AGENCY_NAME,null,new AgencyImportModule(), true);
                break;
            case "1":
                this.exportExcelController.writeExcel(response,new ArrayList<>(),RESIDENTIAL_NAME,null,new ResidentialImportModule());
                break;
            case "2":
                this.exportExcelController.writeExcel(response,new ArrayList<>(),UNIT_NAME,null,new UnitImportModule());
                break;
            case "3":
                this.exportExcelController.writeExcel(response,new ArrayList<>(),ROOM_NAME,null,new RoomImportModule());
                break;
            case "4":
                this.exportExcelController.writeExcelWithArea(response,new ArrayList<>(),USER_NAME,null,new UserImportModule(), false);
                break;
            case "5":
                this.exportExcelController.writeExcel(response,new ArrayList<>(),USER_CARD_NAME,null,new UserAccessImportModule());
                break;
            case "6":
                this.exportExcelController.writeExcel(response,new ArrayList<>(),ACCESS_CARD_NAME,null,new AccessCardImportModule());
                break;
            case "7":
                this.exportExcelController.writeExcel(response,new ArrayList<>(),"机构导入模板",null,new OrgExcelTemplate());
            default:
                break;
        }
    }

    /**
     * 功能描述: 导出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;
        }
    }


}
