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.mysql.jdbc.StringUtils;
import com.xintong.visualinspection.bean.TeamClass;
import com.xintong.visualinspection.bean.UnregularUserClass;
import com.xintong.visualinspection.bean.User;
import com.xintong.visualinspection.bean.UserClass;
import com.xintong.visualinspection.dao.master.TeamClassDao;
import com.xintong.visualinspection.dao.master.UserClassDao;
import com.xintong.visualinspection.service.BaseService;
import com.xintong.visualinspection.service.UserClassService;
import com.xintong.visualinspection.util.CacheUtil;

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

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

    @Autowired
    UserClassDao userClassDao ;
    
    @Autowired
    TeamClassDao teamClassDao;
    
	@Override
	public void insertList(List<UserClass> list) {
		// TODO Auto-generated method stub
		StringBuffer nightClassStuff = new StringBuffer();
		StringBuffer middleClassStuff = new StringBuffer();
		StringBuffer mooringClassStuff = new StringBuffer();
		Long mooringTeamId = 0L , middleTeamId =0L , nightTeamId =0L;
		for(int i=0;i<list.size();i++){
			if(list.get(i).getList()!=null && list.get(i).getList().size()>0 ){
				list.get(i).setClass_flag(1);				
			}
			UserClass objTemp = list.get(i);
			if(objTemp.getClass_type() ==1){
				mooringTeamId = objTemp.getTeam_id();
				mooringClassStuff.append(objTemp.getUser_id()+",");
			}else if(objTemp.getClass_type() ==2){
				middleTeamId = objTemp.getTeam_id();
				middleClassStuff.append(objTemp.getUser_id()+",");
			}else if(objTemp.getClass_type() ==3){
				nightTeamId = objTemp.getTeam_id();
				nightClassStuff.append(objTemp.getUser_id()+",");
			}
			userClassDao.insert(list.get(i));
			List<UnregularUserClass> obj = list.get(i).getList() ;
			if(obj!=null && obj.size()>0){
				for(int j=0;j<obj.size();j++){
					obj.get(j).setClass_type(list.get(i).getClass_type());
					obj.get(j).setDept_id(list.get(i).getDept_id());
					obj.get(j).setWork_date(list.get(i).getWork_date());
					obj.get(j).setLane(list.get(i).getLane());
					obj.get(j).setUser_id(obj.get(j).getId());
					obj.get(j).setTeam_id(list.get(i).getTeam_id());
					obj.get(j).setClass_id(list.get(i).getId());
				}
				userClassDao.insert_detail(obj);
			}
		}
		if(nightClassStuff.toString()!=""){
			// 更新晚班人员
			TeamClass teamClass = new TeamClass();
			teamClass.setTeam_id(nightTeamId);
			teamClass.setWork_date(list.get(0).getWork_date());
			teamClass.setUser_ids(nightClassStuff.toString());
			teamClassDao.updateUserIds(teamClass);
		}
		if(middleClassStuff.toString()!=""){
			TeamClass teamClass = new TeamClass();
			teamClass.setTeam_id(middleTeamId);
			teamClass.setWork_date(list.get(0).getWork_date());
			teamClass.setUser_ids(middleClassStuff.toString());
			teamClassDao.updateUserIds(teamClass);
		}
		if(mooringClassStuff.toString()!=""){
			TeamClass teamClass = new TeamClass();
			teamClass.setTeam_id(mooringTeamId);
			teamClass.setWork_date(list.get(0).getWork_date());
			teamClass.setUser_ids(mooringClassStuff.toString());
			teamClassDao.updateUserIds(teamClass);
		}
	}
	
	@Override
	public void updateList(List<UserClass> list) {
		userClassDao.delete(list.get(0));
		userClassDao.deleteDetail(list.get(0));
		
		StringBuffer nightClassStuff = new StringBuffer();
		StringBuffer middleClassStuff = new StringBuffer();
		StringBuffer mooringClassStuff = new StringBuffer();
		Long mooringTeamId = 0L , middleTeamId =0L , nightTeamId =0L;
		
		for(int i=0;i<list.size();i++){
			if(list.get(i).getList()!=null && list.get(i).getList().size() >0 ){
				list.get(i).setClass_flag(1);				
			}
			else{
				list.get(i).setClass_flag(0);				
			}
			UserClass objTemp = list.get(i);
			if(objTemp.getClass_type() ==1){
				mooringTeamId = objTemp.getTeam_id();
				mooringClassStuff.append(objTemp.getUser_id()+",");
			}else if(objTemp.getClass_type() ==2){
				middleTeamId = objTemp.getTeam_id();
				middleClassStuff.append(objTemp.getUser_id()+",");
			}else if(objTemp.getClass_type() ==3){
				nightTeamId = objTemp.getTeam_id();
				nightClassStuff.append(objTemp.getUser_id()+",");
			}
			userClassDao.insert(list.get(i));
			List<UnregularUserClass> obj = list.get(i).getList() ;
			if(obj!=null && obj.size() > 0){
				for(int j=0;j<obj.size();j++){
					obj.get(j).setClass_type(list.get(i).getClass_type());
					obj.get(j).setDept_id(list.get(i).getDept_id());
					obj.get(j).setWork_date(list.get(i).getWork_date());
					obj.get(j).setLane(list.get(i).getLane());
					obj.get(j).setUser_id(obj.get(j).getId());
					obj.get(j).setTeam_id(list.get(i).getTeam_id());
					obj.get(j).setClass_id(list.get(i).getId());
				}
				userClassDao.insert_detail(obj);
			}
		}
		if(nightClassStuff.toString()!=""){
			// 更新晚班人员
			TeamClass teamClass = new TeamClass();
			teamClass.setWork_date(list.get(0).getWork_date());
			teamClass.setTeam_id(nightTeamId);
			teamClass.setUser_ids(nightClassStuff.toString());
			teamClassDao.updateUserIds(teamClass);
		}
		if(middleClassStuff.toString()!=""){
			TeamClass teamClass = new TeamClass();
			teamClass.setWork_date(list.get(0).getWork_date());
			teamClass.setTeam_id(middleTeamId);
			teamClass.setUser_ids(middleClassStuff.toString());
			teamClassDao.updateUserIds(teamClass);
		}
		if(mooringClassStuff.toString()!=""){
			TeamClass teamClass = new TeamClass();
			teamClass.setWork_date(list.get(0).getWork_date());
			teamClass.setTeam_id(mooringTeamId);
			teamClass.setUser_ids(mooringClassStuff.toString());
			teamClassDao.updateUserIds(teamClass);
		}
	}

	@Override
	public List<UserClass> selectList(UserClass obj) {
		return userClassDao.selectList(obj);
	}

	@Override
	public List<User> getUsersByTeamId(Integer user_class_id) {
		// TODO Auto-generated method stub
		String users= userClassDao.getUsersByTeamId(user_class_id);
		if(!StringUtils.isNullOrEmpty(users)) {
			String[] userArr = users.split(",");
			List<User> userList = new ArrayList<User>();
			for(String s:userArr) {
				userList.add(CacheUtil.userMap.get(Long.parseLong(s)));
			}
			return userList;
		}
		return null;
	}
   
}
