package com.xintong.visualinspection.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import com.xintong.visualinspection.bean.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xintong.system.err.BusinessException;
import com.xintong.visualinspection.bean.ClassCode;
import com.xintong.visualinspection.bean.Team;
import com.xintong.visualinspection.bean.TeamClass;
import com.xintong.visualinspection.service.TeamClassService;
import com.xintong.visualinspection.service.TeamService;

/**
 * 文件名：TestController
 * 版本信息：日期：2017/3/30 Copyright 江苏省交通规划设计院 Corporation 2017 版权所有.
 */
@RestController
@RequestMapping("/teamClass")
public class TeamClassController extends BaseController {

    @Autowired
    private TeamClassService teamClassService;
    
    @Autowired
    private TeamService teamService;


   
    /**
     * 添加班次
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/add")
    public String add(@Valid @RequestBody TeamClass teamClass){
//    	Calendar cal = Calendar.getInstance();
//    	cal.setTime(teamClass.getWork_date());
//    	if(teamClass && teamClass.getWork_date())
    	teamClassService.insert(teamClass);
    	return super.returnSuccessResult("添加成功");
    }
    
    /**
     * 添加班次 list
     */
    @RequestMapping(value = "/add/list")
    public String addList(@Valid @RequestBody List<TeamClass> teamClasses,HttpServletRequest request){
    	User u =getCurrentUser(request);
    	for(int i=0;i<teamClasses.size();i++ ){
    		TeamClass teamClass = teamClasses.get(i);
    		teamClasses.get(i).setDept_id(u.getOrganid()+0L);
    		//直接从后台获取当班的人员
    		if(teamClass.getUser_type()!=null && teamClass.getUser_type()==1 && teamClass.getTeam_id()!=null){
    			Team t = teamService.getById(teamClass.getTeam_id());
    			teamClass.setUser_ids(t.getUser_ids());
    		}
    		teamClassService.insert(teamClasses.get(i));  
    	}
    	return super.returnSuccessResult("添加成功");
    }
    
    /**
     * 修改班次
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/update")
    public String update(@Valid @RequestBody TeamClass teamClass){
    	teamClassService.update(teamClass);
    	return super.returnSuccessResult("修改成功");
    }
    
    /**
     * 更新班次
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/update/list")
    public String update(@Valid @RequestBody List<TeamClass> teamClasses,HttpServletRequest request){
    	User u = getCurrentUser(request);
    	for(TeamClass teamClass : teamClasses){
    		if(teamClass.getDept_id()==null){
    			teamClass.setDept_id(u.getOrganid()+0L);
    		}
    		//直接从后台获取当班的人员
    		if(teamClass.getUser_type()!=null && teamClass.getUser_type()==1 && teamClass.getTeam_id()!=null){
    			Team t = teamService.getById(teamClass.getTeam_id());
    			teamClass.setUser_ids(t.getUser_ids());
    		}
    	}
    	teamClassService.updateList(teamClasses);
    	return super.returnSuccessResult("修改成功");
    }
    
    /**
     * 删除班次
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/delete")
    public String delete(@RequestBody TeamClass teamClass){
    	teamClassService.delete(teamClass.getId());
    	return super.returnSuccessResult("删除成功");
    }
    
    /**
     * 通过id获取班次
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/getClassById")
    public String getClassById(@RequestBody TeamClass teamClass){
    	if(teamClass.getId()==null){
    		throw new BusinessException(20002);
    	}
    	TeamClass tc = teamClassService.getById(teamClass.getId());
    	return super.returnSuccessResult(tc);
    }
    
    /**
     * 通过多个条件查询班次
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/getClassList")
    public String getClassList(@RequestBody TeamClass teamClass,HttpServletRequest request){
    	if(teamClass.getDept_id()==null){
    		User u = getCurrentUser(request);
    		teamClass.setDept_id(u.getOrganid()+0L);
    	}
    	if(teamClass.getStart_time()==null){
    		throw new BusinessException(20902);
    	}
    	if(teamClass.getEnd_time()==null){
    		throw new BusinessException(20903);
    	}
    	List<TeamClass> tcList = teamClassService.getClassList(teamClass);
    	return super.returnSuccessResult(tcList);
    }
    
    
    /**
     * 获取人员的排班
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/getUserClasss")
    public String getClassList(@RequestBody com.xintong.visualinspection.pojo.TeamClass teamClass){
    	
    	if(teamClass.getStart_time()==null){
    		throw new BusinessException(20902);
    	}
    	if(teamClass.getEnd_time()==null){
    		throw new BusinessException(20903);
    	}
    	if(teamClass.getUser_id()==null){
    		throw new BusinessException(20903);
    	}
    	return super.returnSuccessResult(teamClassService.getLaneClassListByUserAndTime(teamClass));
    }
    /**
     * 查询值班信息和班组内的人员
     * @param teamClass
     * @param request
     * @return
     */
    @RequestMapping(value = "/getClassListAndUsers")
    public String getClassListAndUsers(@RequestBody TeamClass teamClass,HttpServletRequest request){
    	if(teamClass.getDept_id()==null){
    		User u = getCurrentUser(request);
    		teamClass.setDept_id(u.getOrganid()+0L);
    	}
    	if(teamClass.getStart_time()==null){
    		throw new BusinessException(20902);
    	}
    	if(teamClass.getEnd_time()==null){
    		throw new BusinessException(20903);
    	}
    	List<TeamClass> tcList = teamClassService.getClassListAndUsers(teamClass);
    	return super.returnSuccessResult(tcList);
    }
    
    /**
     * 获取team_class 信息
     * @param teamClass
     * @param request
     * @return
     */
    @RequestMapping(value = "/getClassCode")
    public String getClassCodeInfo(@RequestBody ClassCode classCode){
    	
    	List<TeamClass> tcList = teamClassService.getClassCode(classCode);
    	return super.returnSuccessResult(tcList);
    }
    
    
}
