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.TaUser;
import com.community.huiju.model.TaUserVerify;
import com.community.huiju.model.TpBuildingOwnerInfo;
import com.community.huiju.service.IBuildingOwnerInfoService;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

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

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

    @Autowired
    private ITaUserVerifyService iTaUserVerifyService;

    @Autowired
    private ITaUserService iTaUserService;

    @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 = "一页多少行"),
    })
    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) {

        ResponseBean responseBean = new ResponseBean();
        responseBean = iTaUserVerifyService.selectAll(phone, userName, idCard, pageNum, pageSize);
        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不通过"),
    })
    public ResponseBean getAll(@PathVariable("userVerifyId") Integer userVerifyId,
                               @RequestParam("verifyStatus") Boolean verifyStatus,
                               HttpSession session) {

        ResponseBean responseBean = new ResponseBean();
        UserElement userElement = getUserElement(session);
        responseBean = iTaUserVerifyService.auditUser(userElement, userVerifyId, verifyStatus);
        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,
                               HttpSession session) {

        ResponseBean responseBean = new ResponseBean();
        UserElement userElement = getUserElement(session);
        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,
                               HttpSession session) {

        ResponseBean responseBean = new ResponseBean();
        UserElement userElement = getUserElement(session);

        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;
    }

}
