package com.xintong.visualinspection.service.impl;

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

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

import com.alibaba.druid.util.StringUtils;
import com.xintong.visualinspection.bean.Team;
import com.xintong.visualinspection.dao.master.TeamDao;
import com.xintong.visualinspection.service.BaseService;
import com.xintong.visualinspection.service.TeamService;
import com.xintong.visualinspection.util.CacheUtil;

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

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


    @Autowired
    private TeamDao teamDao;
    @Autowired
    private UserService userService;

	@Override
	public List<Team> getAll() {
		// TODO Auto-generated method stub
		return teamDao.getAll();
	}


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


	@Override
	public List<Team> getByDeptId(Long deptId) {
		// TODO Auto-generated method stub
		return teamDao.getByDeptId(deptId);
	}

	@Override
	public List<Team> getList(Team team) {
		// TODO Auto-generated method stub
		return teamDao.getList(team);
	}

	@Override
	public void insert(Team team) {
		// TODO Auto-generated method stub
		teamDao.insert(team);
	}


	@Override
	public void update(Team team) {
		// TODO Auto-generated method stub
		teamDao.update(team);
	}


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


	@Override
	public List<Team> getTeamByName(Team team) {
		// TODO Auto-generated method stub
		return getList(teamDao.getTeamByName(team));
	}
	
	public List<Team> getList(List<Team> teamlist){
		for (Team team : teamlist) {
			if(CacheUtil.userMap.get(team.getMonitor().longValue()) == null){
				team.setMonitor_name("-");
			}else{
				team.setMonitor_name(CacheUtil.userMap.get(team.getMonitor().longValue()).getTruename());
			}
			if(CacheUtil.userMap.get(team.getSub_monitor().longValue())==null){
				team.setSub_monitor_name("-");
			}else{
				team.setSub_monitor_name(CacheUtil.userMap.get(team.getSub_monitor().longValue()).getTruename());
			}
			String[] user_ids = team.getUser_ids().split(",");
			String user_ids_name = "";
			for(int i=0;i<user_ids.length;i++){
				if(CacheUtil.userMap.get(Long.valueOf(user_ids[i]))==null){
					if(i == 0){
						user_ids_name = user_ids_name + "-";
					}else{
						user_ids_name = user_ids_name + "、-";
					}
				}else{
					if(i==0){
						user_ids_name = user_ids_name + CacheUtil.userMap.get(Long.valueOf(user_ids[i])).getTruename();
					}else{
						user_ids_name = user_ids_name + "、" + CacheUtil.userMap.get(Long.valueOf(user_ids[i])).getTruename();
					}
				}
			}
			team.setUser_ids_name(user_ids_name);
		}
		return teamlist;
	}


	@Override
	public Team getDetailsById(Team team) {
		team = teamDao.getOne(team.getId());
		if(team != null){
			String[] user_ids = team.getUser_ids().split(",");
			StringBuffer user_names = new StringBuffer();
			for(int i=0;i<user_ids.length;i++){
				String userNames = CacheUtil.userMap.get(Long.valueOf(user_ids[i])).getTruename();
				user_names.append(userNames!=null?userNames:"" );
				user_names.append(",");
			}
			team.setUser_ids_name(user_names.toString());
		}
		return team;
	}


	@Override
	public List<User> getTeamPersonByUserClassid(Long id) {
		Team team = teamDao.getTeamByUserClassid(id);
		List<User> userList = new ArrayList<User>();
		if(team!=null) {
			String user_ids = team.getUser_ids();
			String[] userArr = user_ids.split(",");
			for(String user:userArr) {
				if(!StringUtils.isEmpty(user)){
					User u = CacheUtil.getUserFromMap(Long.parseLong(user));
					if(u!=null){
						if(user.equals(team.getMonitor()+"")){
							u.setPositionid(4);//收费班长，参考职位表
						}
						userList.add(u);
					}
				}
			}
		}
		return userList;
	}
}
