package com.sptg.estate.biz.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.estate.api.entity.MemberAndUser;
import com.sptg.estate.biz.mapper.MemberAndUserMapper;
import com.sptg.estate.biz.service.MemberAndUserInterface;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * title: MemberAndUserServicelmpl
 * package: com.sptg.estate.biz.security.impl
 * description: 会员和用户  服务层实现类
 *
 *               
 *                
 */
@Service
public class MemberAndUserServicelmpl extends ServiceImpl<MemberAndUserMapper, MemberAndUser> implements MemberAndUserInterface {

    @Override
    public ResultBean<Boolean> addMemberAndUsers(List<MemberAndUser> list) {
        return new ResultBean<>(ResultEnum.SUCCESS,this.insertBatch(list));
    }

    @Override
    public List<MemberAndUser> getByCondition(MemberAndUser memberAndUser){
        //构建查询条件
        Wrapper<MemberAndUser> wrapper = new EntityWrapper<>();
        //会员id
        if(memberAndUser.getMemberId()!=null){
            wrapper.eq("l_member_id", memberAndUser.getMemberId());
        }
        if(memberAndUser.getUserId()!=null){
            wrapper.eq("l_user_id", memberAndUser.getUserId());
        }
        if(memberAndUser.getRoomId()!=null){
            wrapper.eq("l_room_id", memberAndUser.getRoomId());
        }
        return this.selectList(wrapper);
    }

    @Override
    public ResultBean<Boolean> checkRelationship(MemberAndUser memberAndUser) {
        //构建查询条件
        Wrapper<MemberAndUser> wrapper = new EntityWrapper<>();
        //会员id
        wrapper.eq("l_member_id", memberAndUser.getMemberId());
        //住户id
        wrapper.eq("l_user_id", memberAndUser.getUserId());
        //统计
        int count = this.selectCount(wrapper);
        if(count>0){
            return new ResultBean<>(ResultEnum.SUCCESS,true);
        }
        return new ResultBean<>(ResultEnum.SUCCESS,false);
    }

    @Override
    public int countByCondition (MemberAndUser memberAndUser) {
        //构建查询条件
        Wrapper<MemberAndUser> wrapper = new EntityWrapper<>();
        //会员id
        if(memberAndUser.getMemberId()!=null){
            wrapper.eq("l_member_id", memberAndUser.getMemberId());
        }
        //住户id
        if(memberAndUser.getUserId()!=null){
            wrapper.eq("l_user_id", memberAndUser.getUserId());
        }
        //统计
        int count = this.selectCount(wrapper);
        return count;
    }

    @Override
    public List<MemberAndUser> getListByMemberId(Long memberId) {
        //构建查询条件
        Wrapper<MemberAndUser> wrapper = new EntityWrapper<>();
        wrapper.eq("l_member_id", memberId);
        return this.selectList(wrapper);
    }

    @Override
    public boolean deleteMemberAndUser(MemberAndUser memberAndUser) {
        //构建查询条件
        Wrapper<MemberAndUser> wrapper = new EntityWrapper<>();
        if(memberAndUser.getMemberId() != null){
            wrapper.eq("l_member_id", memberAndUser.getMemberId());
        }
        if(memberAndUser.getRoomId() != null){
            wrapper.eq("l_room_id", memberAndUser.getRoomId());
        }
        return this.delete(wrapper);
    }
}
