package com.usky.fire.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.fire.domain.BaseDevice;
import com.usky.fire.domain.BaseUserCompany;
import com.usky.fire.mapper.BaseUserCompanyMapper;
import com.usky.fire.service.BaseUserCompanyService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户单位关联 服务实现类
 * </p>
 *
 * @author JCB
 * @since 2022-08-18
 */
@Service
public class BaseUserCompanyServiceImpl extends AbstractCrudService<BaseUserCompanyMapper, BaseUserCompany> implements BaseUserCompanyService {

    @Override
    public List<String> companyIdList() {
        Long userId = SecurityUtils.getUserId();
        LambdaQueryWrapper<BaseUserCompany> QueryWrapper = Wrappers.lambdaQuery();
        QueryWrapper.select(BaseUserCompany::getCompanyId)
                .eq(BaseUserCompany::getUserId, SecurityUtils.getUserId());
        List<BaseUserCompany> comoanyList = this.list(QueryWrapper);
        List<String> companyIdList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(comoanyList)) {
            for (int i = 0; i < comoanyList.size(); i++) {
                companyIdList.add(comoanyList.get(i).getCompanyId());
            }
        }
        return companyIdList;
    }

    @Override
    public List<BaseUserCompany> userCompanyList(String companyId) {
        LambdaQueryWrapper<BaseUserCompany> QueryWrapper = Wrappers.lambdaQuery();
        QueryWrapper.select(BaseUserCompany::getUserId)
                .eq(BaseUserCompany::getCompanyId, companyId);
        List<BaseUserCompany> comoanyList = this.list(QueryWrapper);
        return comoanyList;
    }

    @Override
    public String companyId() {
        Long userId = SecurityUtils.getUserId();
        LambdaQueryWrapper<BaseUserCompany> QueryWrapper = Wrappers.lambdaQuery();
        QueryWrapper.select(BaseUserCompany::getCompanyId)
                .eq(BaseUserCompany::getUserId, userId);
        List<BaseUserCompany> comoanyList = this.list(QueryWrapper);
        String companyId = null;
        if (CollectionUtils.isNotEmpty(comoanyList)) {
            companyId = comoanyList.get(0).getCompanyId();
        }
        return companyId;
    }

}
