package com.goafanti.ambSystem.controller;

import com.goafanti.ambSystem.bo.InputAmb;
import com.goafanti.ambSystem.bo.InputAmbInvest;
import com.goafanti.ambSystem.bo.InputInvestList;
import com.goafanti.ambSystem.service.AmbInvestService;
import com.goafanti.ambSystem.service.AmbService;
import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.AFTConstants;
import com.goafanti.common.constant.ErrorConstants;
import com.goafanti.common.controller.CertifyApiController;
import com.goafanti.common.enums.ActivityFields;
import com.goafanti.common.utils.ParamUtils;
import com.goafanti.core.shiro.token.TokenManager;
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.validation.Valid;
import java.math.BigDecimal;


@RestController
@RequestMapping(value = "/api/admin/amb/Invest")
public class AmbInvestApiController extends CertifyApiController {
	@Autowired
	private AmbInvestService ambInvestService;

	/**
	 * 投资金额
	 * @return
	 */
	@RequestMapping(value="/addTransfer",method = RequestMethod.POST)
	public Result transfer(@Validated InputAmbInvest 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(!TokenManager.hasRole(AFTConstants.APPROVAL_DECISION)&&!TokenManager.hasRole(AFTConstants.APPROVAL_DECISION_ASSISTANT)){
			in.setRoleType(0);
			if (in.getMyAmbId()==null){
				res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"发起部门","发起部门"));
				return res;
			}
			if (ambInvestService.checkMyAmb(in.getMyAmbId())){
				res.getError().add(buildError("只有当前阿米巴负责人可以发起投资划拨。"));
				return res;
			}
		}else {
			in.setRoleType(1);
		}
		if (in.getStatus()==null){
			in.setStatus(0);
		} else if (in.getStatus()>1){
			res.getError().add(buildError("状态不可大于发起。"));
			return res;
		}
		res.data(ambInvestService.pushInvestTransfer(in));
		return res;
	}


	/**
	 * 投资列表
	 * @return
	 */
	@RequestMapping(value="/InvestList",method = RequestMethod.GET)
	public Result InvestList(InputInvestList in){
		Result res =new Result();
		res.data(ambInvestService.InvestList(in));
		return res;
	}

	/**
	 * 投资日志
	 * @return
	 */
	@RequestMapping(value="/InvestLog",method = RequestMethod.GET)
	public Result InvestLog(String id){
		Result res =new Result();
		res.data(ambInvestService.InvestLog(id));
		return res;
	}

	/**
	 * 修改投资金额
	 * @return
	 */
	@RequestMapping(value="/updateTransfer",method = RequestMethod.POST)
	public Result updateTransfer(InputAmbInvest in){
		Result res =new Result();
		if(!TokenManager.hasRole(AFTConstants.APPROVAL_DECISION)&&!TokenManager.hasRole(AFTConstants.APPROVAL_DECISION_ASSISTANT)){
			in.setRoleType(0);
			if (in.getMyAmbId()==null){
				res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"发起部门","发起部门"));
				return res;
			}
			if (in.getMyAmbId()!=null&&ambInvestService.checkMyAmb(in.getMyAmbId())){
				res.getError().add(buildError("只有当前阿米巴负责人可以发起投资划拨。"));
				return res;
			}
		}else {
			in.setRoleType(1);
		}
		if (in.getStatus()==null){
			in.setStatus(0);
		} else if (in.getStatus()>1){
			res.getError().add(buildError("状态不可大于发起。"));
			return res;
		}

		res.data(ambInvestService.updateInvestTransfer(in));
		return res;
	}

	/**
	 * 修改投资金额
	 * @return
	 */
	@RequestMapping(value="/examineTransfer",method = RequestMethod.POST)
	public Result examineTransfer(InputAmbInvest in){
		Result res =new Result();
		if (in.getStatus()<1){
			res.getError().add(buildError("状态不可小于。"));
			return res;
		}
		res.data(ambInvestService.updateExamineTransfer(in));
		return res;
	}


	/**
	 * 获取可投资列表
	 * @return
	 */
	@RequestMapping(value="/getInvestAmbList",method = RequestMethod.GET)
	public Result getInvestAmbList(Long id){
		Result res =new Result();
		if (TokenManager.hasRole(AFTConstants.APPROVAL_DECISION)||TokenManager.hasRole(AFTConstants.APPROVAL_DECISION_ASSISTANT)){
			if (id==null)id=1L;
		}
		if (id==null){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"当前阿米巴","当前阿米巴"));
			return res;
		}
		res.data(ambInvestService.getInvestAmbList(id));
		return res;
	}





}
