package com.goafanti.organization.controller;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.ErrorConstants;
import com.goafanti.common.controller.BaseApiController;
import com.goafanti.common.dao.OrganizationManagementMapper;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.organization.bo.OrganizationListOut;
import com.goafanti.organization.service.OrganizationService;

@RestController
@RequestMapping("/api/admin/organization")
public class AdminOrganizationController extends BaseApiController{
	@Resource
	private OrganizationService organizationService;
	@Autowired
	private OrganizationManagementMapper organizationManagementMapper;
	/**部门组织管理列表 **/
	@RequestMapping(value = "/listOrganizationManagement" , method = RequestMethod.POST)
	public Result listOrganizationManagement(OrganizationListOut olo, Integer pageNo,
			Integer pageSize){
		Result res = new Result();
		res.setData(organizationService.listOrganizationManagement(olo, pageNo, pageSize));
		return res;		
	}
	/**部门组织管理上级组织查询 **/
	@RequestMapping(value = "/selectSuperId" , method = RequestMethod.GET)
	public Result selectsuperId(){
		Result res = new Result();
		res.setData(organizationService.selectSuperId(0));
		return res;
	}
	/**部门组织管理上级组织查询 **/
	@RequestMapping(value = "/selectSuperIdS" , method = RequestMethod.GET)
	public Result selectsuperIdS(){
		Result res = new Result();
		
		res.setData(organizationService.selectSuperId(1));
		return res;
	}
	/**部门组织管理新增**/
	@RequestMapping(value = "/addOrganization" , method = RequestMethod.POST)
	public Result addOrganization(String name, String managerId, String type, String superId, String remarks) throws Exception{
		Result res = new Result();
		if(StringUtils.isBlank(name) || StringUtils.isBlank(type) || StringUtils.isBlank(superId)){
			res.getError().add(buildError("","组织名称、组织类型、上级组织不能为空"));
			return res;
		}
		String olo=organizationManagementMapper.selectDepNoByName(name);
		if(olo!=null){
			res.getError().add(buildError("","部门已存在"));
			return res;
		}		
		String sdepNo=organizationManagementMapper.selectDepNo(superId);
		int Count=organizationManagementMapper.selectDepNoCount(superId);
		if((Count+1)>99){
			res.getError().add(buildError("","每层最多存在99个子类"));
			return res;
		}
		if(Count<10){
			String dep=sdepNo+"0"+Count;
			String sid=organizationManagementMapper.selectIdByDepNo(dep);
			while(sid!=null){
				Count=(Count+1)%100;				
				if(Count>=10){
					dep=sdepNo+Count;	
				}else{
					dep=sdepNo+"0"+Count;
				}
				sid=organizationManagementMapper.selectIdByDepNo(dep);				
			}
			organizationService.addOrganization(name, managerId, type, superId, remarks,dep);
		}
		if(Count>=10){
			String dep=sdepNo+Count;
			String sid=organizationManagementMapper.selectIdByDepNo(dep);
			while(sid!=null){
				Count=(Count+1)%100;				
				dep=sdepNo+Count;
				sid=organizationManagementMapper.selectIdByDepNo(dep);				
			}
			organizationService.addOrganization(name, managerId, type, superId, remarks,dep);
		}
		
		return res;
	}
	/**负责人自动补全查询**/
	@RequestMapping(value = "/selectName" , method = RequestMethod.POST)
	public Result selectName(String name){
		Result res = new Result();
		res.setData(organizationService.selectName(name));
		return res;
	}
	/**编辑页面数据读取**/
	@RequestMapping(value = "/selectAllById" , method = RequestMethod.POST)
	public OrganizationListOut selectAllById(String id){
		OrganizationListOut res = organizationService.selectAllById(id);
		return res;
	}
	/**修改信息**/
	@RequestMapping(value = "/updateOrganization" , method = RequestMethod.POST)
	public Result updateOrganization(String name, String type, String managerId, String superId, String status,
			String remarks,String id,String abbreviation,String financeId){
		Result res = new Result();
		if(StringUtils.isBlank(name) || StringUtils.isBlank(type)
				|| StringUtils.isBlank(managerId)|| StringUtils.isBlank(superId)
				|| StringUtils.isBlank(status)|| StringUtils.isBlank(remarks)){
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR,""));
			return res;
		}		
		String sdepNo=organizationManagementMapper.selectDepNo(superId);//No
		int Count=organizationManagementMapper.selectDepNoCount(superId);//14
		if((Count+1)>99){
			res.getError().add(buildError("","每层最多存在99个子类"));
			return res;
		}
		int i=0;
		if(Count<10){
			String dep=sdepNo+"0"+Count;
			String sid=organizationManagementMapper.selectIdByDepNo(dep);
			while(sid!=null){
				Count=(Count+1)%100;				
				if(Count>=10){
					dep=sdepNo+Count;	
				}else{
					dep=sdepNo+"0"+Count;
				}
				sid=organizationManagementMapper.selectIdByDepNo(dep);				
			}
			i=organizationService.updateOrganization(name, type, managerId, superId, status, remarks,id,dep,abbreviation,financeId);
		}
		if(Count>=10){
			String dep=sdepNo+Count;
			String sid=organizationManagementMapper.selectIdByDepNo(dep);
			while(sid!=null){
				Count=(Count+1)%100;				
				dep=sdepNo+Count;
				sid=organizationManagementMapper.selectIdByDepNo(dep);				
			}
			i=organizationService.updateOrganization(name, type, managerId, superId, status, remarks,id,dep,abbreviation,financeId);
		}
		
		return res.data(i);
	}
	
	
	/**删除列表信息**/
	@RequestMapping(value = "/deleteById" , method = RequestMethod.POST)
	public Result deleteById(String id){
		Result res = new Result();
		if(StringUtils.isBlank(id)){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "id"));
			return res;
		}
		/*OrganizationListOut olo=organizationManagementMapper.selectAllById(id);
		String superId=olo.getName();*/
		int subordinate=organizationManagementMapper.selectCountBySuperId(id);
		if(subordinate>0){
			res.getError().add(buildError("","存在下级组织不能删除"));
			return res;
		}
		int adminCount=organizationManagementMapper.selectUserById(id);
		if(adminCount>0){
			res.getError().add(buildError("","部门被用户选定不能删除"));
			return res;
		}
		organizationService.deleteById(id);
		return res;
	}
	
}
