package com.xintong.visualinspection.service.impl;

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

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xintong.visualinspection.bean.ClassCode;
import com.xintong.visualinspection.bean.TeamClass;
import com.xintong.visualinspection.bean.User;
import com.xintong.visualinspection.dao.master.TeamClassDao;
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;


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

	@Override
	public List<TeamClass> getClassListAndUsers(TeamClass teamClass) {
		List<TeamClass> lists = teamClassDao.getClassList(teamClass);
		for(TeamClass obj : lists){
			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);
	}

}
