package com.sptg.estate.biz.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.PageBean;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.estate.api.dto.ApplyUserDto;
import com.sptg.estate.api.entity.ApplyUser;
import com.sptg.estate.api.entity.MemberAndUser;
import com.sptg.estate.api.feigns.ApplyUserFeign;
import com.sptg.estate.biz.service.ApplyUserInterface;
import com.sptg.estate.biz.service.MemberAndUserInterface;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * title: ApplyUserController
 * package: com.sptg.estate.biz.security
 * description: 住户申请 控制器
 *
 *               
 *                      
 */
@RequiredArgsConstructor
@RestController
public class ApplyUserController implements ApplyUserFeign {

    private final ApplyUserInterface applyUserInterface;

    /**
     * 功能描述: 检测同一个房间下，同一个名字只能有一条待审核申请记录
     * @param {
     *          room_id:12,
     *          name:邓余超
     *          }
     * @return ResultBean<Boolean>
     *               
     *                      
     */
    @Override
    @PostMapping(value = "/applyUser/checkByNameAndRoomId")
    public ResultBean<Boolean> checkByNameAndRoomId(@RequestBody ApplyUser applyUser) {
        return this.applyUserInterface.checkByNameAndRoomId(applyUser);
    }

    /**
     * 功能描述: 会员认证住户申请
     * @param  applyUser
     * @return ResultBean<Boolean>
     *               
     *                      
     */
    @Override
    @PostMapping(value = "/applyUser/authentication")
    public ResultBean<Boolean> authentication(@RequestBody ApplyUser applyUser) {
        return this.applyUserInterface.authentication(applyUser);
    }

    /**
     * 功能描述: 获取住户认证列表
     * @param  applyUserDto
     * @return ResultBean<Boolean>
     *               
     *                      
     */
    @Override
    @PostMapping(value = "/applyUser/showApplyUserList")
    public ResultBean<Page<ApplyUser>> showApplyUserList(@RequestBody ApplyUserDto applyUserDto) {
        Page<ApplyUser> adContentPage = applyUserInterface.showApplyUserList(applyUserDto);
        return new ResultBean<>(ResultEnum.SUCCESS,adContentPage);
    }

    /**
     * 功能描述: 根据id查询
     * @param  id
     * @return ApplyUser
     *               
     *                      
     */
    @Override
    @PostMapping(value = "/applyUser/getApplyUserById")
    public ApplyUser getApplyUserById(@RequestBody Long id) {
        return this.applyUserInterface.getApplyUserById(id);
    }

    /**
     * 功能描述: 根据id查询
     * @param  applyUser
     * @return ApplyUser
     *               
     *                      
     */
    @Override
    @PostMapping(value = "/applyUser/updateCheckState")
    public ResultBean<Boolean> updateCheckState(@RequestBody ApplyUser applyUser) {
        return this.applyUserInterface.updateCheckState(applyUser);
    }

    @Override
    @PostMapping(value = "/applyUser/getAllApplyUserByMemberId")
    public List<ApplyUser> getAllApplyUserByMemberId(@RequestBody ApplyUserDto applyUserDto) {
        return this.applyUserInterface.getAllApplyUserByMemberId(applyUserDto);
    }

    @Override
    @PostMapping(value = "/applyUser/pagingByMemberId")
    public Page<ApplyUser> pagingByMemberId(@RequestBody ApplyUserDto applyUserDto) {
        return this.applyUserInterface.pagingByMemberId(applyUserDto);
    }
    @Override
    @PostMapping(value = "/applyUser/pagingByRoomId")
    public Page<ApplyUser> pagingByRoomId(@RequestBody ApplyUserDto applyUserDto) {
        return this.applyUserInterface.pagingByRoomId(applyUserDto);
    }

    @Override
    @PostMapping(value = "/applyUser/getAllApplyUserByRoomId")
    public List<ApplyUser> getAllApplyUserByRoomId(@RequestBody ApplyUserDto applyUserDto) {
        return this.applyUserInterface.getAllApplyUserByRoomId(applyUserDto);
    }

    @Override
    @PostMapping(value = "/applyUser/applyUserCountForCheckState")
    public Map<String,Object> applyUserCountForCheckState(@RequestBody ApplyUserDto applyUserDto) {
        return this.applyUserInterface.applyUserCountForCheckState(applyUserDto);
    }

    @Override
    @PostMapping(value = "/applyUser/getApplyUserCount")
    public Long getApplyUserCount(@RequestBody ApplyUserDto applyUserDto) {
        return this.applyUserInterface.getApplyUserCount(applyUserDto);
    }

    private final MemberAndUserInterface memberAndUserInterface;

    @Override
    @PostMapping(value = "/applyUser/addApplyUser")
    @Transactional(rollbackFor = Exception.class)
    public ResultBean<Boolean> addApplyUser(@RequestBody ApplyUser applyUser) {
        boolean result = this.applyUserInterface.insert(applyUser);
        if (result){
            //  保存住户与会员的绑定关系
            if (null != applyUser.getCheckState() && applyUser.getCheckState().equals(1)){
                MemberAndUser memberAndUser = new MemberAndUser();
                memberAndUser.setUserId(applyUser.getUserId());
                memberAndUser.setRoomId(applyUser.getRoomId());
                memberAndUser.setMemberId(applyUser.getMemberId());
                this.memberAndUserInterface.addMemberAndUsers(Collections.singletonList(memberAndUser));
            }
        }
        return new ResultBean<>(ResultEnum.SUCCESS,result);
    }

    @Override
    @PostMapping(value = "/applyUser/addApplyUser/returnUser")
    @Transactional(rollbackFor = Exception.class)
    public ResultBean<Long> addApplyUserReturnUser(@RequestBody ApplyUser applyUser) {
        Long l = this.applyUserInterface.addApplyUserReturnUser(applyUser);
        Long id = applyUser.getId();
        if (l > 0){
            //  保存住户与会员的绑定关系
            if (null != applyUser.getCheckState() && applyUser.getCheckState().equals(1)){
                MemberAndUser memberAndUser = new MemberAndUser();
                memberAndUser.setUserId(applyUser.getUserId());
                memberAndUser.setRoomId(applyUser.getRoomId());
                memberAndUser.setMemberId(applyUser.getMemberId());
                this.memberAndUserInterface.addMemberAndUsers(Collections.singletonList(memberAndUser));
            }
        }
        return new ResultBean<>(ResultEnum.SUCCESS,id);
    }

    @Override
    @PostMapping(value = "/applyUser/updateApplyUserById")
    public ResultBean<Boolean> updateApplyUserById(@RequestBody ApplyUser applyUser) {
        return new ResultBean<>(ResultEnum.SUCCESS,this.applyUserInterface.updateById(applyUser));
    }

    @Override
    @PostMapping(value = "/applyUser/getApplyUserCountByState")
    public Integer getApplyUserCountByState(@RequestBody ApplyUserDto applyUserDto) {
        return this.applyUserInterface.getApplyUserCountByState(applyUserDto);
    }

    @Override
    @PostMapping(value = "/applyUser/getApplyUserPage")
    public ResultBean<Page> getApplyUserPage(@RequestBody PageBean<ApplyUser, ApplyUserDto> pageBean) {
        return this.applyUserInterface.getApplyUserPage(pageBean);
    }

    @Override
    @PostMapping(value = "/applyUser/getApplyUserByCondition")
    public List<ApplyUser> getApplyUserByCondition(@RequestBody ApplyUserDto applyUserDto) {
        return this.applyUserInterface.getApplyUserByCondition(applyUserDto);
    }

    @Override
    @PostMapping(value = "/applyUser/getAuthRoomByMemberId")
    public ResultBean<Page<ApplyUser>> getAuthRoomByMemberId(@RequestBody ApplyUserDto applyUserDto) {
        return this.applyUserInterface.getAuthRoomByMemberId(applyUserDto);
    }

    @Override
    @PostMapping(value = "/applyUser/updateApplyUserList")
    public boolean updateApplyUserList(@RequestBody List<ApplyUser> applyUserList) {
        return this.applyUserInterface.updateBatchById(applyUserList);
    }

    @Override
    @PostMapping(value = "/applyUser/deleteApplyUser")
    public boolean deleteApplyUser(@RequestBody ApplyUserDto applyUserDto) {
        return this.applyUserInterface.deleteApplyUser(applyUserDto);
    }
}
