package com.storlead.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.storlead.user.mapper.SubCompanyMapper;
import com.storlead.user.pojo.entity.SubCompanyEntity;
import com.storlead.user.service.ISubCompanyService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class SubCompanyServiceImpl extends ServiceImpl<SubCompanyMapper, SubCompanyEntity> implements ISubCompanyService {

    @Resource
    private SubCompanyMapper subCompanyMapper;
    @Override
    public List<SubCompanyEntity> getSubCompanyList(String keyword) {
        LambdaQueryWrapper<SubCompanyEntity> query = new LambdaQueryWrapper<SubCompanyEntity>();
        query.select(SubCompanyEntity::getId,SubCompanyEntity::getName);
        query.like(StringUtils.isNotEmpty(keyword),SubCompanyEntity::getName, keyword);
        query.eq(SubCompanyEntity::getEnabled, 1);
        return this.list(query);
    }


    @Override
    public void truncateTable() {
        subCompanyMapper.truncateTable();
    }

    @Override
    public Map<Long, SubCompanyEntity> getSubCompanyMap(List<Long> subCompanyIdList) {
        Map<Long, SubCompanyEntity> subCompanyMap = new HashMap<>();
        List<SubCompanyEntity> subCompanyEntities = subCompanyMapper.selectList(new LambdaQueryWrapper<SubCompanyEntity>().in(SubCompanyEntity::getId, subCompanyIdList));
        if (!CollectionUtils.isEmpty(subCompanyEntities)){
            subCompanyMap = subCompanyEntities.stream().collect(Collectors.toMap(SubCompanyEntity::getId, Function.identity()));
        }
        return subCompanyMap;
    }

    @Override
    public List<SubCompanyEntity> getHasSublist(Long roleId) {
        return baseMapper.getHasSublist(roleId);
    }

}
