package com.goafanti.organization.controller;

import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.ErrorConstants;
import com.goafanti.common.controller.BaseApiController;
import com.goafanti.common.dao.DepartmentMapper;
import com.goafanti.common.utils.ParamUtils;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.organization.bo.InputDep;
import com.goafanti.organization.bo.OrganizationListOut;
import com.goafanti.organization.service.OrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Objects;

@RestController
@RequestMapping("/api/admin/organization")
public class AdminOrganizationController extends BaseApiController{
	@Resource
	private OrganizationService organizationService;
	@Autowired
	private DepartmentMapper departmentMapper;

	/**部门组织管理列表 **/
	@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 = "/getAllDep" , method = RequestMethod.GET)
	public Result getAllDep(Integer hideSign){
		Result res = new Result();
		res.setData(organizationService.getAllDep(hideSign));
		return res;
	}

	/**获取所有部门 **/
	@RequestMapping(value = "/selectSuperId" , method = RequestMethod.GET)
	public Result selectSuperId(Integer hideSign){
		Result res = new Result();
		res.setData(organizationService.selectSuperId(hideSign));
		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,
								  Integer workingHoursType,Integer hideSign,String depNo) {
		Result res = new Result();
		if(StringUtils.isBlank(name) || StringUtils.isBlank(type) || StringUtils.isBlank(superId)||StringUtils.isBlank(depNo)){
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"",""));
			return res;
		}
		String olo=departmentMapper.selectDepNoByName(name);
		if(olo!=null){
			res.getError().add(buildError("","部门已存在"));
			return res;
		}
		if (organizationService.checkDepNo(depNo,0)){
			res.getError().add(buildError("","部门编号已存在"));
			return res;
		}
		organizationService.addOrganization(name, managerId, type, superId, remarks,workingHoursType,hideSign,depNo);

		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 Result selectAllById(String id){
		Result res = new Result();
		res.data(organizationService.selectAllById(id));
		return res;
	}
	/**
	 * 修改部门信息
	 **/
	@RequestMapping(value = "/updateOrganization" , method = RequestMethod.POST)
	public Result updateOrganization(@Validated InputDep in, BindingResult bindingResult){
		Result res = new Result();
		if (bindingResult.hasErrors()) {
			res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
					ParamUtils.getParamName(in,bindingResult.getFieldError().getField())));
			return res;
		}
		if (Objects.equals(in.getId(), in.getSuperId())){
			res.getError().add(buildError("不能设置自己为上级"));
			return res;
		}
		if (organizationService.checkDepNo(in.getDepNo(),1)){
			res.getError().add(buildError("","部门编号已存在"));
			return res;
		}
		if (in.getApproval()==null||(in.getApproval()==1&&StringUtils.isBlank(in.getApprovalAid()))){
			res.getError().add(buildError("特批审核开启必须设置特批人"));
			return res;
		}
		if (StringUtils.isBlank(in.getFinanceId())){
			res.getError().add(buildError("部门财务审核必须设置"));
			return res;
		}
		int i=organizationService.updateOrganization(in);
		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=departmentMapper.selectAllById(id);
		String superId=olo.getName();*/
		int subordinate=departmentMapper.selectCountBySuperId(id);
		if(subordinate>0){
			res.getError().add(buildError("","存在下级组织不能删除"));
			return res;
		}
		int adminCount=departmentMapper.selectUserById(id);
		if(adminCount>0){
			res.getError().add(buildError("","部门被用户选定不能删除"));
			return res;
		}
		organizationService.deleteById(id);
		return res;
	}
}
