package com.hotent.uc.manager.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hotent.base.query.QueryFilter;
import com.hotent.base.query.QueryOP;
import com.hotent.uc.api.model.IUser;
import com.hotent.uc.dao.OrgDao;
import com.hotent.uc.dao.OrgUserDao;
import com.hotent.uc.manager.OrgAuthManager;
import com.hotent.uc.manager.UserManager;
import com.hotent.uc.model.*;
import com.hotent.uc.util.ContextUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hotent.base.manager.impl.BaseManagerImpl;
import com.hotent.uc.dao.LoginLogDao;
import com.hotent.uc.manager.LoginLogManager;

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

/**
 * 
 * <pre> 
 * 描述：用户登录日志表 处理实现类
 * 构建组：x7
 * 作者:zhangxw
 * 邮箱:zhangxw@jee-soft.cn
 * 日期:2020-06-19 18:42:21
 * 版权：广州宏天软件股份有限公司
 * </pre>
 */
@Service("loginLogManager")
public class LoginLogManagerImpl extends BaseManagerImpl<LoginLogDao, LoginLog> implements LoginLogManager{
	@Autowired
	OrgAuthManager orgAuthService;
	@Autowired
	OrgDao orgDao;
	@Autowired
	OrgUserDao orgUserDao;
	@Autowired
	UserManager userService;
	@Override
	public Integer removePhysical() {
		return baseMapper.removePhysical();
	}

	@Override
	public LoginLog getByAccount(String account) {
		return baseMapper.getByAccount(account);
	}

	@Override
	public List<LoginLog> getLoginLogList() {
		IUser user = ContextUtil.getCurrentUser();
		List<String> orgIdList=new ArrayList<>();
		if(!user.isAdmin()) {
			QueryFilter queryFilter = QueryFilter.build();
			queryFilter.addFilter("b.account_",user.getAccount(), QueryOP.EQUAL);
			IPage<OrgAuth> list = null;
			try {
				list = orgAuthService.getAllOrgAuth(queryFilter);
				List<OrgAuth> listOrg=list.getRecords();
				 orgIdList=listOrg.stream().map(r->r.getOrgId()).collect(Collectors.toList());
			} catch (Exception e) {
				e.printStackTrace();
			}

		}else{
			List<Org> orgList=getAllOrg();
			orgIdList=orgList.stream().map(r->r.getId()).collect(Collectors.toList());
		}

		List<Org> depList=getDeptByOrgId(orgIdList);
		orgIdList.addAll(depList.stream().map(r->r.getId()).collect(Collectors.toList()));
		List<LoginLog> result =this.baseMapper.getLoginLogList(orgIdList);
		result.stream().forEach(item->{
			try {
			User userCur=userService.getByAccount(item.getAccount());
			boolean flag=false;
			if(userCur!=null&&!flag){
				LambdaQueryWrapper<OrgUser> orgUserQw=new LambdaQueryWrapper<>();
				orgUserQw.eq(OrgUser::getUserId,userCur.getId());
				orgUserQw.orderByDesc(OrgUser::getIsMaster);
				List<OrgUser> orgUserList=orgUserDao.selectList(orgUserQw);
				for(int i=0;i<orgUserList.size();i++){
					LambdaQueryWrapper<Org> orgQw=new LambdaQueryWrapper<>();
					orgQw.eq(Org::getId,orgUserList.get(i).getOrgId());
					orgQw.eq(Org::getOrgType,Org.ORG_TYPE_DEP);
					orgQw.eq(Org::getIsDelete,"0");
					List<Org> orgList=orgDao.selectList(orgQw);
					if(orgList.size()>0){
						item.setDeptName(orgList.get(0).getName());
						flag=true;
						break;
					}
				}
			}

			} catch (Exception e) {
				e.printStackTrace();
			}

		});
		return result;
	}
	private List<Org> getAllOrg(){
		LambdaQueryWrapper<Org> orgLambdaQueryWrapper=new LambdaQueryWrapper<>();
		orgLambdaQueryWrapper.eq(Org::getIsDelete,"0");
		orgLambdaQueryWrapper.notIn(Org::getOrgType,Org.ORG_TYPE_DEP);
		List<Org> resultList=orgDao.selectList(orgLambdaQueryWrapper);
		return resultList;
	}

	private List<Org> getDeptByOrgId(List<String> orgIdList){
		LambdaQueryWrapper<Org> orgLambdaQueryWrapper=new LambdaQueryWrapper<>();
		orgLambdaQueryWrapper.eq(Org::getIsDelete,"0");
		orgLambdaQueryWrapper.in(Org::getParentId,orgIdList);
		orgLambdaQueryWrapper.eq(Org::getOrgType,Org.ORG_TYPE_DEP);
		List<Org> resultList=orgDao.selectList(orgLambdaQueryWrapper);
		List<Org> indexList=new ArrayList<>();
		indexList.addAll(resultList);
		findDeptList(indexList,resultList);
		return indexList;
	}

	private void findDeptList(List<Org> indexList,List<Org> list){
		for(int i=0;i<list.size();i++){
			String id=list.get(i).getId();
			LambdaQueryWrapper<Org> orgLambdaQueryWrapper=new LambdaQueryWrapper<>();
			orgLambdaQueryWrapper.eq(Org::getIsDelete,"0");
			orgLambdaQueryWrapper.eq(Org::getParentId,id);
			orgLambdaQueryWrapper.in(Org::getOrgType,Org.ORG_TYPE_DEP);
			List<Org> resultList=orgDao.selectList(orgLambdaQueryWrapper);
			indexList.addAll(resultList);
			if(resultList.size()>0){
				findDeptList(indexList,resultList);
			}

		}
	}

}
