package org.jeecg.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.entity.SysUserDepart;
import org.jeecg.modules.system.mapper.SysUserDepartMapper;
import org.jeecg.modules.system.model.DepartIdModel;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.system.service.ISysUserDepartService;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <P>
 * 用户部门表实现类
 * <p/>
 * @Author ZhiLin
 *@since 2019-02-22
 */
@Service
public class SysUserDepartServiceImpl extends ServiceImpl<SysUserDepartMapper, SysUserDepart> implements ISysUserDepartService {
	@Autowired
	private ISysDepartService sysDepartService;
	@Autowired
	private ISysUserService sysUserService;
	

	/**
	 * 根据用户id查询部门信息
	 */
	@Override
	public List<DepartIdModel> queryDepartIdsOfUser(String userId) {
		LambdaQueryWrapper<SysUserDepart> queryUDep = new LambdaQueryWrapper<SysUserDepart>();
		LambdaQueryWrapper<SysDepart> queryDep = new LambdaQueryWrapper<SysDepart>();
		try {
			queryUDep.eq(SysUserDepart::getUserId, userId);
			List<String> depIdList = new ArrayList<>();
			List<DepartIdModel> depIdModelList = new ArrayList<>();
			List<SysUserDepart> userDepList = this.list(queryUDep);
			if(userDepList != null && userDepList.size() > 0) {
			for(SysUserDepart userDepart : userDepList) {
					depIdList.add(userDepart.getDepId());
				}
			queryDep.in(SysDepart::getId, depIdList);
			List<SysDepart> depList = sysDepartService.list(queryDep);
			if(depList != null || depList.size() > 0) {
				for(SysDepart depart : depList) {
					depIdModelList.add(new DepartIdModel().convertByUserDepart(depart));
				}
			}
			return depIdModelList;
			}
		}catch(Exception e) {
			e.fillInStackTrace();
		}
		return null;
		
		
	}


	/**
	 * 根据部门id查询用户信息
	 */
	@Override
	public List<SysUser> queryUserByDepId(String depId) {
		LambdaQueryWrapper<SysUserDepart> queryUDep = new LambdaQueryWrapper<>();
		queryUDep.eq(SysUserDepart::getDepId, depId);
		List<String> userIdList = new ArrayList<>();
		List<SysUserDepart> uDepList = this.list(queryUDep);
		if(uDepList != null && uDepList.size() > 0) {
			for(SysUserDepart uDep : uDepList) {
				userIdList.add(uDep.getUserId());
			}
			List<SysUser> userList = sysUserService.listByIds(userIdList);
			//update-begin-author:taoyan date:201905047 for:接口调用查询返回结果不能返回密码相关信息
//			for (SysUser sysUser : userList) {
//				sysUser.setSalt("");
//				sysUser.setPassword("");
//			}
			//update-end-author:taoyan date:201905047 for:接口调用查询返回结果不能返回密码相关信息
			return userList;
		}
		return new ArrayList<SysUser>();
	}

	/**
	 * 根据部门code，查询当前部门和下级部门的 用户信息
	 * @return
	 */
	@Override
	public Page<SysUser> queryUserByDepCode(String depCode, String realname, String username, String phone, String idCard, Integer pageNo, Integer pageSize) {
		LambdaQueryWrapper<SysDepart> queryByDepCode = new LambdaQueryWrapper<SysDepart>();
		queryByDepCode.eq(SysDepart::getId,depCode);
		List<SysDepart> sysDepartList = sysDepartService.list(queryByDepCode);

		List<String> depIds = sysDepartList.stream().map(SysDepart::getId).collect(Collectors.toList());

		LambdaQueryWrapper<SysUserDepart> queryUDep = new LambdaQueryWrapper<SysUserDepart>();
		queryUDep.in(SysUserDepart::getDepId, depIds);
		List<String> userIdList = new ArrayList<>();

		List<SysUserDepart> uDepList = this.list(queryUDep);
		if(uDepList != null && uDepList.size() > 0) {
			for(SysUserDepart uDep : uDepList) {
				userIdList.add(uDep.getUserId());
			}
			LambdaQueryWrapper<SysUser> queryUser = new LambdaQueryWrapper<SysUser>();
			queryUser.in(SysUser::getId,userIdList);
			if(oConvertUtils.isNotEmpty(realname)){
				queryUser.like(SysUser::getRealname,realname.trim());
			}
			if (oConvertUtils.isNotEmpty(username)){
				queryUser.like(SysUser::getUsername,username.trim());
			}
			if (oConvertUtils.isNotEmpty(phone)){
				queryUser.eq(SysUser::getPhone,phone.trim());
			}
			if (oConvertUtils.isNotEmpty(idCard)){
				queryUser.like(SysUser::getClearCard,idCard.trim());
			}
			Page<SysUser> page = new Page<>(pageNo, pageSize);
			Page<SysUser> page1 = sysUserService.page(page, queryUser);
			//List<SysUser> userList = (List<SysUser>) sysUserService.list(queryUser);
			//update-begin-author:taoyan date:201905047 for:接口调用查询返回结果不能返回密码相关信息
			for (SysUser sysUser : page1.getRecords()) {
				sysUser.setSalt("");
				sysUser.setPassword("");
			}
//			for (SysUser sysUser : page1.getRecords()) {
//				sysUser.setSalt("");
//				sysUser.setPassword("");
//			}
			//update-end-author:taoyan date:201905047 for:接口调用查询返回结果不能返回密码相关信息
			return page1;
		}
		//Page<SysUserOVo> result = new Page<>(pageList.getCurrent(),pageList.getPages(),pageList.getTotal());
		return new Page<>(pageNo, pageSize);
	}

	@Override
	public Page<SysUser> queryUserByDepCode1(String depCode, String realname, Integer pageNo, Integer pageSize) {
		LambdaQueryWrapper<SysDepart> queryByDepCode = new LambdaQueryWrapper<SysDepart>();
		queryByDepCode.eq(SysDepart::getId,depCode);
		List<SysDepart> sysDepartList = sysDepartService.list(queryByDepCode);

		List<String> depIds = sysDepartList.stream().map(SysDepart::getId).collect(Collectors.toList());

		LambdaQueryWrapper<SysUserDepart> queryUDep = new LambdaQueryWrapper<SysUserDepart>();
		queryUDep.in(SysUserDepart::getDepId, depIds);
		List<String> userIdList = new ArrayList<>();

		List<SysUserDepart> uDepList = this.list(queryUDep);
		if(uDepList != null && uDepList.size() > 0) {
			for(SysUserDepart uDep : uDepList) {
				userIdList.add(uDep.getUserId());
			}
			LambdaQueryWrapper<SysUser> queryUser = new LambdaQueryWrapper<SysUser>();
			queryUser.in(SysUser::getId,userIdList);
			if(oConvertUtils.isNotEmpty(realname)){
				queryUser.like(SysUser::getRealname,realname.trim());
			}
			Page<SysUser> page = new Page<SysUser>(pageNo, pageSize);
			Page<SysUser> page1 = sysUserService.page(page, queryUser);
			//List<SysUser> userList = (List<SysUser>) sysUserService.list(queryUser);
			//update-begin-author:taoyan date:201905047 for:接口调用查询返回结果不能返回密码相关信息
//			for (SysUser sysUser : page1.getRecords()) {
//				sysUser.setSalt("");
//				sysUser.setPassword("");
//			}
//			for (SysUser sysUser : page1.getRecords()) {
//				sysUser.setSalt("");
//				sysUser.setPassword("");
//			}
			//update-end-author:taoyan date:201905047 for:接口调用查询返回结果不能返回密码相关信息
			return page1;
		}
		//Page<SysUserOVo> result = new Page<>(pageList.getCurrent(),pageList.getPages(),pageList.getTotal());
		return new Page<SysUser>(pageNo, pageSize);
	}
    @Override
    public SysUserDepart getbyUserId(String id) {
		//List<SysUserDepart> userDepartByUid = this.baseMapper.getUserDepartByUid(id);
		SysUserDepart byUserId = this.baseMapper.getByUserId(id);
		return byUserId;
    }

	@Override
	public List<SysUser> getbyDeptId(String id) {
		return null;
	}

    @Override
    public String getByUserName(String username) {
		String DeptId = baseMapper.getByUserName(username);
        return DeptId;
    }


}
