package io.renren.modules.sys.service.impl;

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

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import io.renren.common.constant.Constant;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.utils.ConvertUtils;
import io.renren.modules.security.user.SecurityUser;
import io.renren.modules.security.user.UserDetail;
import io.renren.modules.sys.dao.CompaniesDao;
import io.renren.modules.sys.dto.CompaniesDTO;
import io.renren.modules.sys.entity.CompaniesEntity;
import io.renren.modules.sys.enums.SuperAdminEnum;
import io.renren.modules.sys.service.CompaniesService;

/**
 * 公司表
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2022-05-29
 */
@Service
public class CompaniesServiceImpl extends CrudServiceImpl<CompaniesDao, CompaniesEntity, CompaniesDTO> implements CompaniesService {

    @Override
    public QueryWrapper<CompaniesEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<CompaniesEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);
        wrapper.eq("status", Constant.Status.ENABLE.getValue());
        UserDetail user = SecurityUser.getUser();
    	//如果不是超级管理员，则只能查询当前自己部门的公司
        if(SuperAdminEnum.NO.value() == user.getSuperAdmin()) {
        	wrapper.in("dept_id", user.getDeptIdList());
        }
        return wrapper;
    }
    
    @Override
	public List<CompaniesDTO> getCompaniesListByUser() {
        QueryWrapper<CompaniesEntity> qw = new QueryWrapper<>();
        //qw.eq("status", Constant.Status.ENABLE.getValue());
        qw.isNull("delete_time");
        UserDetail user = SecurityUser.getUser();
        //如果不是超级管理员，则进行数据过滤
        if(SuperAdminEnum.NO.value() == user.getSuperAdmin()) {
        	qw.in("dept_id", user.getDeptIdList());
        }
		return ConvertUtils.sourceToTarget(baseDao.selectList(qw), currentDtoClass());
	}

	@Override
	public List<Long> getCompaniesListByUserLong() {
		List<Long> returnList = new ArrayList<>();
		List<CompaniesDTO> list = getCompaniesListByUser();
    	for (CompaniesDTO dto : list) {
    		returnList.add(dto.getId());
		}
		return returnList;
	}
}