/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.xrtygis.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.common.oauth.exception.ToflyDeniedException;
import com.tofly.xrtygis.entity.Userandplot;
import com.tofly.xrtygis.entity.dto.UserandplotDto;
import com.tofly.xrtygis.mapper.UserandplotMapper;
import com.tofly.xrtygis.service.UserandplotService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 小区-用户关联表
 *
 * @author chenhao
 * @date Thu Mar 31 00:00:00 CST 2022
 */
@Service("userandplotService")
public class UserandplotServiceImpl extends ServiceImpl<UserandplotMapper, Userandplot> implements UserandplotService {

    /**
     * 获取未绑定小区用户
     * @param page page
     * @return page
     */
    @Override
    public Page<Map> getUserInfo(Page page,String dto) {
        return baseMapper.queryUserInfo(page,dto);
    }

    @Override
    public boolean bindUserAndPlot(UserandplotDto dto) {
        if (dto == null){
            throw new ToflyDeniedException("传入数据为空");
        }
        List<Userandplot> uap = dto.getUap();
        if (uap.isEmpty()){
            throw new ToflyDeniedException("不存在绑定关系");
        }

        for (Userandplot userandplot : uap) {
            baseMapper.insert(userandplot);
        }
        return true;
    }

    @Override
    public List<Map> plotCount(String plotIds) {
        return baseMapper.plotCount(StringUtils.isEmpty(plotIds) ? null : Arrays.asList(plotIds.split(",")));
    }
}
