package com.community.huiju.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.community.commom.mode.ResponseBean;
import com.community.commom.session.UserElement;
import com.community.huiju.common.base.BaseController;
import com.community.huiju.model.TaFace;
import com.community.huiju.model.TaUser;
import com.community.huiju.model.TaUserVerify;
import com.community.huiju.model.TpBuildingOwnerInfo;
import com.community.huiju.service.IBuildingOwnerInfoService;
import com.community.huiju.service.ITaFaceService;
import com.community.huiju.service.ITaUserService;
import com.community.huiju.service.ITaUserVerifyService;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 楼栋业主资料信息表 前端控制器
 * </p>
 *
 * @author weiximei
 * @since 2018-12-18
 */
@RestController
@RefreshScope
@RequestMapping("/")
@Api(value = "楼栋业主资料信息", description = "楼栋业主资料信息 Api")
public class BuildingOwnerInfoController extends BaseController {

    @Autowired
    private IBuildingOwnerInfoService iBuildingOwnerInfoService;

    @Autowired
    private ITaUserVerifyService iTaUserVerifyService;

    @Autowired
    private ITaUserService iTaUserService;

    @Autowired
    private ITaFaceService iTaFaceService;

    @ApiOperation(value = "获取楼栋业主信息", notes = "根据条件获取")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataTypeClass = String.class, name = "parameter",value = "buildingId栋;" +
                    "unitId单元;levelId楼层;roomNoId户号;" +
                    "ownerName业主姓名;" +"ownerTel业主手机号;" +"idCard身份证;" +
                    "pageNum第几页;pageSize 一页多少行;")
    })
    @RequestMapping(value = "/building/list", method = RequestMethod.POST)
    public ResponseBean getList(@RequestBody String parameter, HttpSession session){
        ResponseBean responseBean = new ResponseBean();
        UserElement userElement = getUserElement(session);
        responseBean = iBuildingOwnerInfoService.listQuery(parameter, userElement);
        return responseBean;
    }

    @ApiOperation(value = "获取楼栋业主审核详情", notes = "获取楼栋业主审核详情")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataTypeClass = Integer.class, name = "userVerifyId",value = "审核Id")
    })
    @RequestMapping(value = "/building/verify/{userVerifyId}", method = RequestMethod.GET)
    public ResponseBean getById(@PathVariable(value = "userVerifyId") Integer userVerifyId) {
        ResponseBean responseBean = new ResponseBean();

        TaUserVerify userVerify = iTaUserVerifyService.getById(userVerifyId);
        if (null == userVerify) {
            responseBean.addError("审核数据不存在！");
            return responseBean;
        }
        TaUser user = iTaUserService.getById(userVerify.getUserId());
        Map<String, Object> map = Maps.newHashMap();
        map.put("verify", userVerify);
        map.put("info", user);

        // true 标识业主 false 非业主
        map.put("boolRole", false);

        // 默认人脸未录入
        map.put("face", "unregister");

        QueryWrapper<TaUserVerify> userVerifyQueryWrapper = new QueryWrapper<>();
        userVerifyQueryWrapper.eq("phase_id", userVerify.getPhaseId());
        userVerifyQueryWrapper.eq("building_id", userVerify.getBuildingId());
        userVerifyQueryWrapper.eq("unit_id", userVerify.getUnitId());
        userVerifyQueryWrapper.eq("level_id", userVerify.getLevelId());
        userVerifyQueryWrapper.eq("room_no_id", userVerify.getRoomNoId());
        userVerifyQueryWrapper.eq("user_id", userVerify.getUserId());
        userVerifyQueryWrapper.eq("role_id", 1);
        TaUserVerify userVerifyOne = iTaUserVerifyService.getOne(userVerifyQueryWrapper);
        if (null != userVerifyOne) {
            map.put("boolRole", true);
        }

        // 人脸是否录入
        QueryWrapper<TaFace> faceQueryWrapper = new QueryWrapper<>();
        faceQueryWrapper.eq("ta_user_verify_id", userVerify.getId());
        TaFace face = iTaFaceService.getOne(faceQueryWrapper);
        if (null != face) {
            map.put("face", "register");
        }


        responseBean.addSuccess(map);
        return responseBean;

    }

    @ApiOperation(value = "修改楼栋业务信息", notes = "修改楼栋业主信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataTypeClass = String.class, name = "parameter",value = "userVerifyId审核编号;roleId角色编号;verifyStatus审核 true通过；false不通过;remark备注"),
            @ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "token")
    })
    @RequestMapping(value = "/building/update", method = RequestMethod.PUT)
    public ResponseBean update(@RequestBody String parameter, HttpSession session){
        ResponseBean responseBean = new ResponseBean();
        UserElement userElement = getUserElement(session);
        responseBean = iBuildingOwnerInfoService.update(parameter, userElement);
        return responseBean;
    }

    @ApiOperation(value = "添加楼栋业务信息", notes = "添加楼栋业务信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataTypeClass = String.class, name = "parameter", value = "phaseId期;buildingId栋;" +
                    "unitId单元;levelId楼层;roomNoId户号;" +
                    "ownerName业主姓名;" +
                    "ownerTel业主手机号;roleId角色id;gender性别 1：男   2：女;"),
            @ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "token")
    })
    @RequestMapping(value = "/building/add", method = RequestMethod.POST)
    public ResponseBean add(@RequestBody String parameter, HttpSession session){
        ResponseBean responseBean = new ResponseBean();
        UserElement userElement = getUserElement(session);
        responseBean = iBuildingOwnerInfoService.add(parameter, userElement);
        return responseBean;
    }

    @ApiOperation(value = "批量删除楼栋业主信息", notes = "批量删除楼栋业主信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataTypeClass = String.class, name = "ids",value = "id集合"),
            @ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "token")
    })
    @RequestMapping(value = "/building/delete", method = RequestMethod.DELETE)
    public ResponseBean delete(@RequestBody List<Integer> ids, HttpSession session){
        ResponseBean responseBean = new ResponseBean();
        UserElement userElement = getUserElement(session);
        responseBean = iBuildingOwnerInfoService.deleteIds(ids, userElement.getId());
        return responseBean;
    }

    @ApiOperation(value = "查询 期区", notes = "查询 期区")
    @ApiImplicitParams({
    })
    @RequestMapping(value = "/building/phase",method = RequestMethod.GET)
    public ResponseBean getPhaseList(HttpSession session){
        ResponseBean responseBean = new ResponseBean();
        UserElement userElement = getUserElement(session);
        responseBean = iBuildingOwnerInfoService.getPhaseList(userElement.getCommunityId());
        return responseBean;
    }
    
    @ApiOperation(value = "查询 楼栋", notes = "查询 楼栋")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "phaseId",value = "期区ID")
    })
    @RequestMapping(value = "/building/building",method = RequestMethod.GET)
    public ResponseBean getBuildingList(@RequestParam Integer phaseId, HttpSession session){
        ResponseBean responseBean = new ResponseBean();
        UserElement userElement = getUserElement(session);
        responseBean = iBuildingOwnerInfoService.getBuildingList(phaseId,userElement.getCommunityId());
        return responseBean;
    }
    
    @ApiOperation(value = "查询 单元", notes = "查询 单元")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "buildingId",value = "楼栋ID")
    })
    @RequestMapping(value = "/building/unit",method = RequestMethod.GET)
    public ResponseBean getUnitList(@RequestParam Integer buildingId,HttpSession session){
        ResponseBean responseBean = new ResponseBean();
        UserElement userElement = getUserElement(session);
        responseBean = iBuildingOwnerInfoService.getUnitList(buildingId,userElement.getCommunityId());
        return responseBean;
    }
    
    @ApiOperation(value = "查询 楼层", notes = "查询 楼层")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "unitId",value = "单元ID")
    })
    @RequestMapping(value = "/building/level",method = RequestMethod.GET)
    public ResponseBean getLevelList(@RequestParam Integer unitId,HttpSession session){
        ResponseBean responseBean = new ResponseBean();
        UserElement userElement = getUserElement(session);
        responseBean = iBuildingOwnerInfoService.getLevelList(unitId,userElement.getCommunityId());
        return responseBean;
    }
    
    @ApiOperation(value = "查询 房间号", notes = "查询 房间号")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "levelId",value = "楼层ID")
    })
    @RequestMapping(value = "/building/roomno",method = RequestMethod.GET)
    public ResponseBean getRoomNoList(@RequestParam Integer levelId,HttpSession session){
        ResponseBean responseBean = new ResponseBean();
        UserElement userElement = getUserElement(session);
        responseBean = iBuildingOwnerInfoService.getRoomNoList(levelId,userElement.getCommunityId());
        return responseBean;
    }
    
    @ApiOperation(value = "上传楼栋信息接口", notes = "上传楼栋信息接口")
    @PostMapping(value = "/building/uploadExcel", consumes = "multipart/*", headers = "content-type=multipart/form-data")
    public ResponseBean uploadExcel(@RequestParam("file") MultipartFile file) {
        ResponseBean responseBean = new ResponseBean();
        responseBean = iBuildingOwnerInfoService.getExcelData(file);
        return responseBean;
    }
 
    @ApiOperation(value = "上传楼栋信息并入库", notes = "上传楼栋信息并入库")
    @PostMapping(value = "/building/submitExcel", consumes = "multipart/*", headers = "content-type=multipart/form-data")
    public ResponseBean submitExcel(@RequestParam("file") MultipartFile file, HttpSession session) {
        ResponseBean responseBean = new ResponseBean();
        UserElement userElement = getUserElement(session);
        try {
            responseBean = iBuildingOwnerInfoService.submitExcelData(file,userElement);
        }catch (Exception e){
            responseBean.addError("导入数据失败" + e.getMessage());
            e.printStackTrace();
        }
        
        return responseBean;
    }

    @ApiOperation(value = "获取已认证通过的用户信息",nickname ="获取已认证通过的用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "String", name = "paramets", value ="ownerName:姓名,ownerTel:手机号 ,current:每页显示多少条,current:每页显示多少条,pageSzie:当前页数")})

    @RequestMapping(value="/userPassCertification",method = RequestMethod.POST)
    public ResponseBean userPassCertification(@RequestBody String paramets,HttpSession session){
        UserElement userElement = getUserElement(session);
        ResponseBean allApprove=iBuildingOwnerInfoService.selectUserApprove(paramets,userElement.getCommunityId());
        return allApprove;
    }

    @ApiOperation(value = "修改用户当前认证状态",nickname ="修改用户当前认证状态")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "String", name = "paramets", value ="ownerTel:手机号,verifyStatus:当前状态")})

    @RequestMapping(value="/updataPassCertification",method = RequestMethod.POST)
    public ResponseBean updataPassCertification(@RequestBody String paramets){
        ResponseBean userParent=iBuildingOwnerInfoService.updataApprove(paramets);
        return userParent;
    }

    @ApiOperation(value = "小区楼栋资料表",nickname ="小区楼栋资料表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "String", name = "paramets", value ="phase:期,building:楼栋 ,unit:单元,level:楼层，roomNo:户号， pageNum:每页显示多少条,pageSzie:当前页数"),
            @ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "token")})
    @RequestMapping(value="/buildinglist",method = RequestMethod.POST)
    public ResponseBean buildinglist(@RequestBody String paramets,HttpSession session){
        UserElement userElement = getUserElement(session);
        ResponseBean buildinglist=iBuildingOwnerInfoService.buildinglist(paramets,userElement);
        return buildinglist;
    }

    @ApiOperation(value = "小区楼栋资料表-查询 楼栋/单元/楼层/户号", notes = "小区楼栋资料表-查询 楼栋/单元/楼层/户号")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataTypeClass = String.class, name = "parameter", value = "phase:期;building楼栋;" +
                    "unit单元;level楼层：roomNo:户号") // ;roomNo户号
    })
    @RequestMapping(value = "/buildinghouse",method = RequestMethod.POST)
    public ResponseBean puildinghouse(@RequestBody String parameter, HttpSession session){
        UserElement userElement = getUserElement(session);
        ResponseBean responseBean = iBuildingOwnerInfoService.puildinghouse(parameter, userElement.getCommunityId());
        return responseBean;
    }

    @ApiOperation(value = "小区楼栋资料表-修改 楼栋/单元/楼层/户号", notes = "小区楼栋资料表-修改 楼栋/单元/楼层/户号")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataTypeClass = String.class, name = "parameter", value = "phase:期；building:当前楼栋;" +
                    "unit:当前楼层单元;level:当前楼层楼层;roomNo:当前楼层楼层")
    })
    @RequestMapping(value = "/communitybuilding/update",method = RequestMethod.POST)
    public ResponseBean communitybuildingAdd(@RequestBody String parameter, HttpSession session){
        UserElement userElement = getUserElement(session);
        ResponseBean responseBean = iBuildingOwnerInfoService.communitybuildingUpdate(parameter, userElement.getCommunityId());
        return responseBean;
    }

    @ApiOperation(value = "下载Excel模板(小区楼栋信息)", notes = "下载Excel模板(小区楼栋信息)")
    @ApiImplicitParams({
//            @ApiImplicitParam(dataTypeClass = String.class, paramType = "body", name = "parameter", value = "billExplain收费组说明"),
            @ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token")
    })
    @RequestMapping(value = "/building/downloadExcel", method = RequestMethod.GET)
    public void exportExcel(HttpServletResponse response, HttpSession session) {
        UserElement userElement = getUserElement(session);
        XSSFWorkbook workbook = (XSSFWorkbook) iBuildingOwnerInfoService.downloadExcel(userElement).getData();
        this.setResponseHeader(response, "业主资料库.xlsx");
        //响应到客户端
        try {
            OutputStream os = response.getOutputStream();
            workbook.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @ApiOperation(value = "住户资料详情",nickname ="住户资料详情")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "String", name = "paramets", value ="id:楼栋住户ID"),
            @ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "token")})
    @RequestMapping(value="/building/details",method = RequestMethod.POST)
    public ResponseBean buildingDetails(@RequestBody String paramets,HttpSession session){
        UserElement userElement = getUserElement(session);
        ResponseBean buildinglist=iBuildingOwnerInfoService.buildingDetails(paramets,userElement);
        return buildinglist;
    }
}
