package com.sptg.estate.biz.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.estate.api.dto.MemberDto;
import com.sptg.estate.api.entity.Member;
import com.sptg.estate.api.entity.MemberLoginStatus;
import com.sptg.estate.api.entity.VideoCallAccount;
import com.sptg.estate.api.feigns.MemberFeign;
import com.sptg.estate.biz.service.MemberServiceInterface;
import com.sptg.estate.biz.service.VideoCallAccountServiceInterface;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * title: MemberController
 * package: com.sptg.estate.biz.consumer
 * description: 会员 - 控制器
 *
 *               
 *                
 */
@RestController
@RequiredArgsConstructor
public class MemberController implements MemberFeign {

    private final MemberServiceInterface memberServiceInterface;
    private final VideoCallAccountServiceInterface videoCallAccountServiceInterface;

    @Override
    @PostMapping(value = "/member/getMemberByOpenid")
    public ResultBean<Member> getMemberByOpenid(@RequestBody MemberDto memberDto) {
        return memberServiceInterface.getMemberByOpenid(memberDto);
    }

    @Override
    @PostMapping(value = "/member/getMemberById")
    public ResultBean<Member> getMemberById(@RequestBody MemberDto memberDto) {
        return memberServiceInterface.getMemberById(memberDto);
    }

    @Override
    @PostMapping(value = "/member/memberRegister")
    public ResultBean<Boolean> memberRegister(@RequestBody Member member) {
        return memberServiceInterface.memberRegister(member);
    }

    @Override
    @PostMapping(value = "/member/checkMemberTel")
    public ResultBean<Boolean> checkMemberTel(@RequestBody Member member) {
        return memberServiceInterface.checkMemberTel(member);
    }

    @Override
    @PostMapping(value = "/member/updateMemberOpenId")
    public ResultBean<Boolean> updateMemberOpenId(@RequestBody Member member) {
        return memberServiceInterface.updateMemberOpenId(member);
    }

    @Override
    @PostMapping(value = "/member/getMemberAccount")
    public ResultBean<Map<String, Integer>> getMemberAccount(@RequestBody MemberDto memberDto) {
        Map<String,Integer> map = new HashMap<>(2);
        memberDto.setIsAllType(OperationConstant.ONE);
        int totalCount = this.memberServiceInterface.getMemberAccount(memberDto);
        map.put("totalCount",totalCount);
        memberDto.setIsAllType(OperationConstant.TWO);
        int monthCount = this.memberServiceInterface.getMemberAccount(memberDto);
        map.put("monthCount",monthCount);
        return new ResultBean<>(ResultEnum.SUCCESS,map);
    }

    @Override
    public Member getMemberByMemberId(@RequestBody Long memberId) {
        Member m = this.memberServiceInterface.selectById(memberId);
        //查询会员的视频呼叫账号
        if(m!=null && m.getState()==2){
            VideoCallAccount v = this.videoCallAccountServiceInterface.selectByCustomerId(m.getId());
            m.setVideoCallAccount(v);
        }
        return m;
    }

    @Override
    @PostMapping(value = "/member/getMemberTypeCount")
    public ResultBean<Map<String, Object>> getMemberTypeCount(@RequestBody MemberDto memberDto) {
        return this.memberServiceInterface.getMemberTypeCount(memberDto);
    }

    @Override
    @PostMapping(value = "/member/updateMemberPassword")
    public ResultBean<Boolean> updateMemberPassword(@RequestBody Member member) {
        return this.memberServiceInterface.updateMemberPassword(member);
    }

    @Override
    @PostMapping(value = "/member/changeTel")
    public ResultBean<Boolean> changeTel(@RequestBody Member member) {
        return this.memberServiceInterface.changeTel(member);
    }

    @Override
    @PostMapping(value = "/member/findMemberByTel")
    public Member findMemberByTel(@RequestBody Member member) {
        return this.memberServiceInterface.findMemberByTel(member);
    }

    @Override
    @PostMapping(value = "/member/showMemberList")
    public ResultBean<Page<Member>> showMemberList(@RequestBody MemberDto memberDto) {
        Page<Member> adContentPage = memberServiceInterface.showMemberList(memberDto);
        return new ResultBean<>(ResultEnum.SUCCESS,adContentPage);
    }

    @Override
    @PostMapping(value = "/member/memberRegisterNew")
    public ResultBean<Member> memberRegisterNew(@RequestBody Member member) {
        return memberServiceInterface.memberRegisterNew(member);
    }

    @Override
    @GetMapping(value = "/member/getByOldId/{oldId}")
    public Member getByOldId(@PathVariable(value = "oldId") String oldId) {
        Wrapper<Member> wrapper = new EntityWrapper<>();
        wrapper.eq("old_id",oldId);
        return this.memberServiceInterface.selectOne(wrapper);
    }

    @Override
    @PostMapping(value = "/member/updateMember")
    public ResultBean<Boolean> updateMember(@RequestBody Member member) {
        return this.memberServiceInterface.updateMember(member);
    }

    @Override
    @PostMapping(value = "/member/activation")
    public ResultBean<Boolean> activation(@RequestBody MemberDto memberDto) {
        return this.memberServiceInterface.activation(memberDto);
    }

    @Override
    @PostMapping(value = "/member/selectMember")
    public Member getMember(@RequestBody Member member){
        return this.memberServiceInterface.getMember(member);
    }

    @Override
    @PostMapping(value = "/member/delete")
    public ResultBean<Boolean> delete(@RequestParam(value = "memberId") Long memberId) {
        boolean result = this.memberServiceInterface.deleteById(memberId);
        if(result){
            return new ResultBean<>(ResultEnum.SUCCESS);
        }else{
            return new ResultBean<>(ResultEnum.FAILURE);
        }
    }
}
