package com.community.huiju.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.community.commom.mode.ResponseBean;
import com.community.commom.session.UserElement;
import com.community.huiju.dao.*;
import com.community.huiju.enums.UserVerifyEnum;
import com.community.huiju.model.*;
import com.community.huiju.service.IBuildingOwnerInfo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 楼栋资料库实现
 * @author weiximei
 */
@Slf4j
@Service
public class BuildingOwnerInfoImpl implements IBuildingOwnerInfo {

    @Autowired
    private TaUserMapper taUserMapper;

    /**
     * 栋
     */
    @Autowired
    private TpBuildingMapper tpBuildingMapper;

    /**
     *  楼栋资料库
     */
    @Autowired
    private TpBuildingOwnerInfoMapper tpBuildingOwnerInfoMapper;

    @Autowired
    private TaUserVerifyMapper taUserVerifyMapper;

    /**
     * 期
     */
    @Autowired
    private TpPhaseMapper tpPhaseMapper;

    /**
     * 单元
     */
    @Autowired
    private TpUnitMapper tpUnitMapper;

    /**
     * 楼层
     */
    @Autowired
    private TpLevelMapper tpLevelMapper;

    /**
     * 户号
     */
    @Autowired
    private TpRoomNoMapper tpRoomNoMapper;


    @Override
    public ResponseBean selectBuildList(Integer communityId, Integer phaseId, Integer buildingId, Integer unitId, Integer levelId) {
        ResponseBean responseBean = new ResponseBean();

        // 查询 期 -> 栋 -> 单元 -> 楼层 -> 户号
        if (null == phaseId && null == buildingId && null == unitId && null == levelId) {
            List<TpPhase> phaseList = tpPhaseMapper.selectByAddress(communityId);
            responseBean.addSuccess(phaseList);
        } else if (null == buildingId && null == unitId && null == levelId) {
            List<TpBuilding> buildingList = tpBuildingMapper.selectByAddress(communityId, phaseId);
            responseBean.addSuccess(buildingList);
        } else if (null == unitId && null == levelId) {
            List<TpUnit> unitList = tpUnitMapper.selectByAddress(communityId, phaseId, buildingId);
            responseBean.addSuccess(unitList);
        } else if (null == levelId) {
            List<TpLevel> levelList = tpLevelMapper.selectByAddress(communityId, phaseId, buildingId, unitId);
            responseBean.addSuccess(levelList);
        } else {
            List<TpRoomNo> roomNoList = tpRoomNoMapper.selectByAddress(communityId, phaseId, buildingId, unitId, levelId);
            responseBean.addSuccess(roomNoList);
        }

        return responseBean;
    }

    @Override
    public ResponseBean hasBuild(UserElement userElement, Integer communityId ,Integer phaseId, Integer buildingId, Integer unitId, Integer levelId, Integer roomNoId) {
        ResponseBean responseBean = new ResponseBean();
        // 1.检验该用户关联有没有这个 户号，审核通过的
       List<TaUserVerify> taUserVerifyEqVerifyStatus1 = taUserVerifyMapper.selectCommunityAndAddress(userElement.getId(),userElement.getCommunityId(), phaseId, buildingId, unitId, levelId, roomNoId, 1);
        // List<TaUserVerify> taUserVerify = taUserVerifyMapper.selectCommunityAndAddressAndNotVerifyStatus2(userElement.getId(),userElement.getCommunityId(), phaseId, buildingId, unitId, levelId, roomNoId);
        if (null != taUserVerifyEqVerifyStatus1 && taUserVerifyEqVerifyStatus1.size() > 0) {
            responseBean.addError("你已关联此房间，无法再次关联！");
            return  responseBean;
        }

        // 1.检验该用户关联有没有这个 户号，待审核
        List<TaUserVerify> taUserVerifyEqVerifyStatus0 = taUserVerifyMapper.selectCommunityAndAddress(userElement.getId(),userElement.getCommunityId(), phaseId, buildingId, unitId, levelId, roomNoId, 0);
        if (null != taUserVerifyEqVerifyStatus0 && taUserVerifyEqVerifyStatus0.size() > 0) {
            responseBean.addError("您已提交了这个房产的审核！请等待户主或者物业审核后，再进行下一步操作！");
            return responseBean;
        }


        Map<String,Object> result = Maps.newHashMap();

        // 2.检验这个户号是否关联了 户主
        TaUserVerify ownerUserVerify = taUserVerifyMapper.selectCommunityAndAddressAndRoleId(communityId, phaseId, buildingId, unitId, levelId, roomNoId);
        if (null != ownerUserVerify) {
            TaUser user = taUserMapper.selectByPrimaryKey(ownerUserVerify.getUserId());
            result.put("ownerName",user.getUserName());
            result.put("roleName", "OWNER");
            responseBean.addSuccess(result);
            return responseBean;
        }

        result.put("ownerName",null);
        result.put("roleName", null);
        responseBean.addSuccess(UserVerifyEnum.NO_OWNER.getMsg(), result, null);
        return responseBean;
    }
}
