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.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xintong.system.err.BusinessException;
import com.xintong.visualinspection.bean.Team;
import com.xintong.visualinspection.service.TeamService;

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

    @Autowired
    private TeamService teamService;

   
    /**
     * 添加班组
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/add")
    public String add(@Valid @RequestBody Team team){
    	teamService.insert(team);
    	return super.returnSuccessResult("添加成功");
    }
    
    /**
     * 修改班组
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/update")
    public String update(@Valid @RequestBody Team team){
    	teamService.update(team);
    	return super.returnSuccessResult("修改成功");
    }
    
    /**
     * 删除班组
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/delete")
    public String delete(@RequestBody Team team){
    	teamService.delete(team.getId());
    	return super.returnSuccessResult("删除成功");
    }
    
    /**
     * 通过id获取班组
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/getTeamById")
    public String getTeamById(@RequestBody Team team){
    	if(team.getId()==null){
    		throw new BusinessException(20002);
    	}
    	Team t = teamService.getById(team.getId());
    	return super.returnSuccessResult(t);
    }
    
    /**
     * 根据team_id 获取班组详情
     * @param team
     * @return
     */
    @RequestMapping(value = "/getDetailById")
    public String getTeamPersonsById(@RequestBody Team team){
    	if(team.getId()==null){
    		throw new BusinessException(20002);
    	}
    	Team t = teamService.getDetailsById(team);
    	return super.returnSuccessResult(t);
    }
    
    /**
     * 根据team_id 获取班组详情
     * @param team
     * @return
     */
    @RequestMapping(value = "/getTeamPersonsByUserClassId/{user_calss_id}")
    public String getTeamPersonsByUserClassId(@PathVariable Long user_calss_id){
    	if(user_calss_id==null){
    		throw new BusinessException(20002);
    	}
    	List<User> userList= teamService.getTeamPersonByUserClassid(user_calss_id);
    	return super.returnSuccessResult(userList);
    }
    
    /**
     * 通过部门id获取班组
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/getTeamByDeptId/{page}/{size}")
    public String getTeamByDeptId(@RequestBody Team team,@PathVariable Integer page,@PathVariable Integer size,HttpServletRequest request ){
    	if(team.getDept_id()==null){
    		User u = getCurrentUser(request);
    		team.setDept_id(u.getOrganid()+0L);
    	}
    	PageHelper.startPage(page, size);
    	List<Team> teamList = teamService.getList(team) ;
    	return super.returnSuccessResult(new PageInfo(teamList));
    }
    
    @RequestMapping(value = "/getTeamByName/{page}/{size}")
    public String getTeamByName(@RequestBody Team team,@PathVariable Integer page,@PathVariable Integer size){
    	PageHelper.startPage(page, size);
    	List<Team> teamList = teamService.getTeamByName(team);
    	return super.returnSuccessResult(new PageInfo(teamList));
    }
}
