package com.xintong.visualinspection.service.impl;

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

import com.xintong.visualinspection.bean.User;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.util.StringUtil;
import com.xintong.visualinspection.bean.ClassCode;
import com.xintong.visualinspection.bean.TeamClass;
import com.xintong.visualinspection.dao.cluster.UserInfoDao;
import com.xintong.visualinspection.dao.master.TeamClassDao;
import com.xintong.visualinspection.dao.master.UserCountDao;
import com.xintong.visualinspection.pojo.UserClassCheck;
import com.xintong.visualinspection.service.BaseService;
import com.xintong.visualinspection.service.TeamClassService;
import com.xintong.visualinspection.util.CacheUtil;

/**
 * 文件名：UserServiceImpl
 * 版本信息：日期：2017/3/30 Copyright 江苏省交通规划设计院 Corporation 2017 版权所有.
 */
@Service
public class TeamClassServiceImpl extends BaseService implements TeamClassService {

    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(TeamClassServiceImpl.class);


    @Autowired
    private TeamClassDao teamClassDao;
    
    @Autowired
    private UserCountDao userCountDao;
    
    @Autowired
    private UserInfoDao userInfoDao;


	@Override
	public List<TeamClass> getClassList(TeamClass teamClass) {
		// TODO Auto-generated method stub
		List<TeamClass> list = teamClassDao.getClassList(teamClass);
		for(TeamClass obj:list){
			if(StringUtil.isEmpty(obj.getUser_ids()))
				continue;
			StringBuffer user_names = new StringBuffer();
			String[] user_ids = obj.getUser_ids().split(",");
			for(int i=0;i<user_ids.length;i++){
				try {
					User user = CacheUtil.userMap.get(Long.valueOf(user_ids[i]));
					String userNames = user!=null?user.getTruename():"";
					user_names.append(userNames);
					user_names.append(",");
				}catch(Exception e) {
					System.out.println("user_ids错误");
				}
			}
			obj.setUser_ids_name(user_names.toString());
		}
		return list;
	}

	@Override
	public List<TeamClass> getClassListAndUsers(TeamClass teamClass) {
		
		List<TeamClass> lists = teamClassDao.getClassListForCharge(teamClass);
		
//		List<TeamClass> lists1 = teamClassDao.getClassListForChargeUnRegular(teamClass);
//		Map<Long,List<TeamClass>> tcMap = new HashMap<Long,List<TeamClass>>();
//		for(TeamClass tc:lists1) {
//			if(tcMap.containsKey(tc.getTeam_id()))
//		}
//		
		
		for(TeamClass obj : lists){
			if(obj.getUser_ids()!=null){				
				String[] user_ids = obj.getUser_ids().split(",");
				List<User> userList = new ArrayList<>();
				for(int i=0;i<user_ids.length;i++){
					User u = CacheUtil.userMap.get(Integer.valueOf(user_ids[i]) +0L); 
					if( u != null){
						User user =new User();
						user.setId(u.getId());
						user.setTruename(u.getTruename());
						userList.add(user);
					}
				}
				obj.setUsers(userList);
			}
		}
		return lists;
	}
	

	@Override
	public TeamClass getById(Long id) {
		// TODO Auto-generated method stub
		return teamClassDao.getOne(id);
	}


	@Override
	public void insert(TeamClass teamClass) {
		// TODO Auto-generated method stub
		teamClassDao.insert(teamClass);
	}


	@Override
	public void update(TeamClass teamClass) {
		// TODO Auto-generated method stub
		teamClassDao.update(teamClass);
	}

	@Override
	public void updateList(List<TeamClass> teamClasses) {
		// 先删除原来的数据，再添加
		if(teamClasses.size()>0){
			teamClassDao.deleteByDeptWorkdate(teamClasses.get(0));
			
			for(int i =0;i<teamClasses.size();i++){
				teamClassDao.insert(teamClasses.get(i));
			}			
		}
	}

	@Override
	public void delete(Long id) {
		// TODO Auto-generated method stub
		teamClassDao.delete(id);
	}

	@Override
	public List<TeamClass> getClassCode(ClassCode teamClass) {
		// TODO Auto-generated method stub
		return teamClassDao.getClassCode(teamClass);
	}
	
	
	@Override
	public UserClassCheck getLaneClassListByUserAndTime(com.xintong.visualinspection.pojo.TeamClass teamClass) {
		// TODO Auto-generated method stub
		UserClassCheck u = new UserClassCheck();
		
		u.setUserClassCountList(userCountDao.getAll(teamClass));
		u.setTeamClasseList(teamClassDao.getLaneClassListByUserAndTime(teamClass));
		
		User us = new User();
		us.setOrganid(29);
		List<Integer> pos = new ArrayList<>();
		pos.add(2);
		us.setPosition_ids(pos);
		u.setUserList(userInfoDao.getUsers(us));
		
		return u;
	}

}
