package com.goafanti.ambSystem.controller;

import com.goafanti.ambSystem.bo.InputAmb;
import com.goafanti.ambSystem.service.AmbService;
import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.ErrorConstants;
import com.goafanti.common.controller.CertifyApiController;
import com.goafanti.common.utils.*;
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;


@RestController
@RequestMapping(value = "/api/admin/amb")
public class AmbApiController extends CertifyApiController {
	@Autowired
	private AmbService ambService;

	/**
	 * 新增巴
	 * @param in
	 * @param bindingResult
	 * @return
	 */
	@RequestMapping(value="/add",method = RequestMethod.POST)
	public Result addAmb(@Validated InputAmb 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;
		}
		int i=ambService.checkAmb(in,0);
		if (i==-1){
			res.getError().add(buildError(String.format("[%s]已存在，请确认后再新增",in.getName())));
			return res;
		}
		res.data(ambService.addAmb(in));
		return res;
	}

	/**
	 * 修改巴
	 * @param in
	 * @return
	 */
	@RequestMapping(value="/update",method = RequestMethod.POST)
	public Result updateAmb( InputAmb in){
		Result res =new Result();
		if (in.getId()==null){
			res.getError().add(buildErrorMessageParams(ErrorConstants.PARAM_EMPTY_ERROR,"巴编号"));
			return res;
		}
		res.data(ambService.updateAmb(in));
		return res;
	}

	/**
	 * 删除巴
	 * @param in
	 * @return
	 */
	@RequestMapping(value="/delete",method = RequestMethod.POST)
	public Result deleteAmb( InputAmb in){
		Result res =new Result();
		if (in.getId()==null){
			res.getError().add(buildErrorMessageParams(ErrorConstants.PARAM_EMPTY_ERROR,"巴编号"));
			return res;
		}
		int i=ambService.checkAmb(in,2);
		if (i==-1){
			res.getError().add(buildError("巴存在子项目，请先删除所有子项"));
			return res;
		}
		res.data(ambService.deleteAmb(in));
		return res;
	}

	/**
	 * 巴详情
	 * @param in
	 * @return
	 */
	@RequestMapping(value="/details",method = RequestMethod.GET)
	public Result detailsAmb( InputAmb in){
		Result res =new Result();
		if (in.getId()==null){
			res.getError().add(buildErrorMessageParams(ErrorConstants.PARAM_EMPTY_ERROR,"巴编号"));
			return res;
		}
		res.data(ambService.detailsAmb(in));
		return res;
	}

	/**
	 * 巴列表
	 * @param in
	 * @return
	 */
	@RequestMapping(value="/select",method = RequestMethod.GET)
	public Result selectAmb( InputAmb in){
		Result res =new Result();
		res.data(ambService.selectAmb(in));
		return res;
	}

	/**
	 * 下拉把列表
	 * @return
	 */
	@RequestMapping(value="/selectAll",method = RequestMethod.GET)
	public Result selectAll( ){
		Result res =new Result();
		res.data(ambService.selectAll());
		return res;
	}

	/**
	 * 下拉把列表
	 * @return
	 */
	@RequestMapping(value="/getMyambDtails",method = RequestMethod.GET)
	public Result getMyambDtails( ){
		Result res =new Result();
		res.data(ambService.getMyambDtails());
		return res;
	}

}
