package com.huiju.estateagents.property.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huiju.estateagents.base.BaseController;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.center.taUser.entity.TaUser;
import com.huiju.estateagents.property.model.TaUserVerify;
import com.huiju.estateagents.property.service.IBuildingOwnerInfoService;
import com.huiju.estateagents.property.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.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController

@RequestMapping("/")
@Api(value = "审核用户API", description = "审核用户API")
public class UserVerifyController extends BaseController {

    @Autowired
    private ITaUserVerifyService iTaUserVerifyService;


    @Autowired
    private IBuildingOwnerInfoService iBuildingOwnerInfoService;

    @RequestMapping(value = "/user/verify/all", method = RequestMethod.GET)
    @ApiOperation(value = "获取待审核的用户列表", notes = "获取待审核的用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataTypeClass = String.class, name = "phone", value = "手机号"),
            @ApiImplicitParam(paramType = "query", dataTypeClass = String.class, name = "userName", value = "用户名"),
            @ApiImplicitParam(paramType = "query", dataTypeClass = String.class, name = "idCard", value = "身份证"),
            @ApiImplicitParam(paramType = "query", dataTypeClass = String.class, name = "pageNum", value = "第几页"),
            @ApiImplicitParam(paramType = "query", dataTypeClass = String.class, name = "pageSize", value = "一页多少行"),
            @ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "token"),
    })
    public ResponseBean getAll(@RequestParam(value = "phone", required = false) String phone,
                               @RequestParam(value = "userName", required = false) String userName,
                               @RequestParam(value = "idCard", required = false) String idCard,
                               @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                               @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                               HttpServletRequest request) {

        ResponseBean responseBean = new ResponseBean();
        TaUser userElement = getTaUser(request);
        responseBean = iTaUserVerifyService.selectAll(phone, userName, idCard, pageNum, pageSize, userElement);
        return responseBean;
    }

    @RequestMapping(value = "/user/verify/audit/{userVerifyId}", method = RequestMethod.PUT)
    @ApiOperation(value = "审核用户", notes = "审核用户")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "token"),
            @ApiImplicitParam(paramType = "path", dataTypeClass = String.class, name = "userVerifyId", value = "审核id"),
            @ApiImplicitParam(paramType = "query", dataTypeClass = Boolean.class, name = "verifyStatus", value = "true通过；false不通过"),
            @ApiImplicitParam(paramType = "query", dataTypeClass = String.class, name = "remark", value = "审核不通过的原因"),
    })
    public ResponseBean getAll(@PathVariable("userVerifyId") Integer userVerifyId,
                               @RequestParam("verifyStatus") Boolean verifyStatus,
                               @RequestParam(value = "remark", required = false) String remark,
                               HttpServletRequest request) {

        ResponseBean responseBean = new ResponseBean();
        TaUser userElement = getTaUser(request);
        responseBean = iTaUserVerifyService.auditUser(userElement, userVerifyId, verifyStatus, remark);
        return responseBean;
    }

    @RequestMapping(value = "/ta/user/has", method = RequestMethod.GET)
    @ApiOperation(value = "校验移动端手机号是否注册", notes = "校验移动端手机号是否注册")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "token"),
            @ApiImplicitParam(paramType = "query", dataTypeClass = String.class, name = "ownerTel", value = "手机号"),
    })
    public ResponseBean taUserHas(@RequestParam("ownerTel") String ownerTel,
                               HttpServletRequest request) {

        ResponseBean responseBean = new ResponseBean();
        TaUser userElement = getTaUser(request);
        QueryWrapper<TaUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("login_name", ownerTel);
//        TaUser user = iTaUserService.getOne(queryWrapper);
//        if (null == user) {
//            responseBean.addError("未注册");
//            return responseBean;
//        }

        Map<String, Object> map = Maps.newHashMap();
//        map.put("info", user);

        responseBean.addSuccess(map);
        return responseBean;
    }

    @RequestMapping(value = "/ta/user/hasOwner", method = RequestMethod.GET)
    @ApiOperation(value = "根据 期/栋/单元/楼层/户号 校验是否有业主", notes = "根据 期/栋/单元/楼层/户号 校验是否有业主")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "token"),
            @ApiImplicitParam(paramType = "query", dataTypeClass = String.class, name = "ownerTel", value = "审核id"),
    })
    public ResponseBean taUserHasOwner(@RequestParam("phaseId") Integer phaseId,
                               @RequestParam("buildingId")Integer buildingId,
                               @RequestParam("unitId")Integer unitId,
                               @RequestParam("levelId")Integer levelId,
                               @RequestParam("roomNoId")Integer roomNoId,
                               HttpServletRequest request) {

        ResponseBean responseBean = new ResponseBean();
        TaUser userElement = getTaUser(request);

        Map<String, Object> map = Maps.newHashMap();
        map.put("boolRole", false);

        hasAddress(phaseId, buildingId, unitId, levelId ,roomNoId, map);

        responseBean.addSuccess(map);
        return responseBean;
    }

    /**
     * 校验该 房产是否有房主
     * @param phaseId
     * @param buildingId
     * @param unitId
     * @param levelId
     * @param roomNoId
     * @param map
     * @return
     */
    private Map<String, Object> hasAddress(Integer phaseId,Integer buildingId,Integer unitId,Integer levelId,Integer roomNoId, Map<String, Object> map) {
        map.put("boolRole", false);
        QueryWrapper<TaUserVerify> userVerifyQueryWrapper = new QueryWrapper<>();
        userVerifyQueryWrapper.eq("phase_id", phaseId);
        userVerifyQueryWrapper.eq("building_id", buildingId);
        userVerifyQueryWrapper.eq("unit_id", unitId);
        userVerifyQueryWrapper.eq("level_id", levelId);
        userVerifyQueryWrapper.eq("room_no_id", roomNoId);
        userVerifyQueryWrapper.eq("role_id", 1);
        userVerifyQueryWrapper.eq("verify_status", 1);
        TaUserVerify userVerify = iTaUserVerifyService.getOne(userVerifyQueryWrapper);
        if (null != userVerify) {
            map.put("boolRole", true);
        }
        return map;
    }

    @RequestMapping(value = "/user/verifyinfo/{userVerifyId}", method = RequestMethod.PUT)
    @ApiOperation(value = "楼栋详情页面审核", notes = "楼栋详情页面审核")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "token"),
            @ApiImplicitParam(paramType = "path", dataTypeClass = String.class, name = "userVerifyId", value = "审核id"),
            @ApiImplicitParam(paramType = "path", dataTypeClass = String.class, name = "roleState", value = "1:业主，2:租客，3:家属"),
            @ApiImplicitParam(paramType = "query", dataTypeClass = String.class, name = "verifyStatus", value = "0 是未审核1是审核通过2是审核不通过"),
            @ApiImplicitParam(paramType = "query", dataTypeClass = String.class, name = "remark", value = "审核不通过的原因"),
    })
    public ResponseBean verifyinfoAdd(@PathVariable("userVerifyId") Integer userVerifyId,
                               @RequestParam("verifyStatus") String verifyStatus,
                               @RequestParam("roleState") String roleState,
                               @RequestParam(value = "remark", required = false) String remark,
                               HttpServletRequest request) {
        ResponseBean responseBean = new ResponseBean();
        TaUser userElement = getTaUser(request);
        responseBean = iTaUserVerifyService.verifyinfoAdd(userElement, userVerifyId, verifyStatus, roleState,remark);
        return responseBean;
    }

}
