package com.xintong.visualinspection.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

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

import com.xintong.system.err.BusinessException;
import com.xintong.visualinspection.bean.FeeStation;
import com.xintong.visualinspection.bean.Organ;
import com.xintong.visualinspection.bean.WreckerGroup;
import com.xintong.visualinspection.service.DepartmentService;
import com.xintong.visualinspection.util.CacheUtil;

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

    @Autowired
    private DepartmentService departmentService;
    
    /**
     * 添加部门
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/addDept",method=RequestMethod.POST,produces="application/json;charset=UTF-8")
    public String addDept(@RequestBody Organ organ) throws Exception{
        
    	departmentService.insert(organ);
    	
    	CacheUtil.refreshDeptMap(departmentService);
    	return returnResult(0, "添加成功", null);
    }
    
    /**
     * 修改部门
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/updateDept/{organid}",method=RequestMethod.PUT,produces="application/json;charset=UTF-8")
    public String updateDept(@RequestBody Organ organ,@PathVariable int organid){
        try{
        	organ.setId(organid);
        	departmentService.update(organ);
        	CacheUtil.refreshDeptMap(departmentService);
        	return super.returnResult(0, "修改成功", null);
        }catch(Exception e){
        	throw new BusinessException(20003);
        }
    }
    
    /**
     * 删除部门（软删除）
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/deleteDept/{organid}",method=RequestMethod.DELETE,produces="application/json;charset=UTF-8")
    public String deleteDept(@PathVariable Integer organid){
        try{
        	departmentService.delete((organid));
        	CacheUtil.refreshDeptMap(departmentService);
        	return returnResult(0, "删除成功", null);
        }catch(Exception e){
        	throw new BusinessException(20002);
        }
    }
    
    /**
     * 获取所用部门
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "/get/all",method=RequestMethod.POST,produces="application/json;charset=UTF-8")
    public String getAll(){
        try{
        	Organ organ=new Organ();
        	organ.setStatus(0);
        	List<Organ> organs= departmentService.getAll();
        	return returnResult(0, "获取成功", organs);
        }catch(Exception e){
        	throw new BusinessException(20001);
        }
    }
    @RequestMapping(value = "/get/all",method=RequestMethod.GET,produces="application/json;charset=UTF-8")
    public String getAllDept(HttpServletRequest request){
        try{
        	User u = getCurrentUser(request);
        	Organ organ=new Organ();
        	organ.setStatus(0);
        	List<Organ> organs= departmentService.getOrgans(organ,u);
        	return returnResult(0, "获取成功", organs);
        }catch(Exception e){
        	throw new BusinessException(20001);
        }
    }
    
    @RequestMapping(value = "/getDeptByParent/{pid}",method=RequestMethod.GET,produces="application/json;charset=UTF-8")
    public String getDeptByParent(@PathVariable Integer pid){
        try{
   
        	Organ organ = new Organ();
        	organ.setParentid(pid);
        	List<Organ> organs= departmentService.getOrgans(organ,null);
        	return returnResult(0, "获取成功",(organs));
        }catch(Exception e){
        	throw new BusinessException(20001);
        }
    }
    
    @RequestMapping(value = "/getDeptById/{organid}",method=RequestMethod.GET,produces="application/json;charset=UTF-8")
    public String getDeptById(@PathVariable Integer organid){
        try{
        	Organ organ = new Organ();
        	organ.setId(organid);
        	
        	List<Organ> organs = departmentService.getOrgans(organ,null);
        	if(organs==null||organs.size()<1 ) throw  new BusinessException(20001);
        	return returnResult(0, "获取成功", organs.get(0));
        }catch(Exception e){
        	throw new BusinessException(20001);
        }
    }
   
    @RequestMapping(value = "/getAllFs",method=RequestMethod.GET,produces="application/json;charset=UTF-8")
    public String getAllFs(){
    	List<FeeStation> organs = departmentService.getAllFS();
    	return returnResult(0, "获取成功", organs);
    }
    
    @RequestMapping(value = "/getAllRM",method=RequestMethod.GET,produces="application/json;charset=UTF-8")
    public String getAllRM(){
    	List<FeeStation> organs = departmentService.getAllRM();
    	return returnResult(0, "获取成功", organs);
    }

    @RequestMapping(value = "/getAllSA",method=RequestMethod.GET,produces="application/json;charset=UTF-8")
    public String getAllSA(){
        List<FeeStation> organs = departmentService.getAllSA();
        return returnResult(0, "获取成功", organs);
    }
    /**
     * 获取所有排障大队
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/getAllWG",method=RequestMethod.GET,produces="application/json;charset=UTF-8")
    public String getAllWG(){
    	List<WreckerGroup> organs = departmentService.getAllWG();
    	return returnResult(0, "获取成功", organs);
    }
    
    @RequestMapping(value = "/getFsByDept",method=RequestMethod.GET,produces="application/json;charset=UTF-8")
    public String getAFsByDeptId(HttpServletRequest request){
    	User u = getCurrentUser(request);
    	FeeStation feestation = departmentService.getFSByDeptId(u.getOrganid());
    	return returnResult(0, "获取成功", feestation);
    }
    
    @RequestMapping(value = "/getDeptInfosByDeptId",method=RequestMethod.POST,produces="application/json;charset=UTF-8")
    public String getDeptInfosByDeptId(HttpServletRequest request,@RequestBody Organ organ){
    	List<Organ> organs = departmentService.getOrgans(organ);
    	return returnResult(0, "获取成功", organs);
    }
    
    
}
