package cn.com.countrygarden.bi.main.controller.investment;

import cn.com.countrygarden.bi.main.controller.BaseController;
import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

import cn.com.countrygarden.bi.main.model.AccessToken;
import cn.com.countrygarden.bi.main.model.investment.InvestProjectInvestment;
import cn.com.countrygarden.bi.main.model.investment.InvestRegionInvestment;
import cn.com.countrygarden.bi.main.dao.investment.InvestProjectInvestmentMapper;
import cn.com.countrygarden.bi.main.service.investment.InvestProjectInvestmentService;

/**
 * 项目投资-业务逻辑层实现
 *
 * @author 汤婷
 * @email www.justintoforest@gamail.com
 * @url
 * @date 2018-08-15
 */
@RestController("InvestProjectInvestmentController")
@RequestMapping("/api/invest/project")
public class InvestProjectInvestmentController extends BaseController {
	@Autowired
	private InvestProjectInvestmentService investProjectInvestmentService;

	@GetMapping(value = "/findProjectAll", produces = "application/json;charset=utf-8")
	public Result findProjectAll(@RequestParam(value = "regionId", required = false) String regionId) {
		boolean hasRole = hasRole("investHQAccounts");
		boolean hasRole2 = hasRole("investRegionAccounts");
		if (!hasRole && !hasRole2) {
			// 权限不足
			return Response.response("noAuthority");
		}
		return investProjectInvestmentService.findProjectAll(regionId);
	}
	@GetMapping(value = "/getProjectInfo", produces = "application/json;charset=utf-8")
	public Result getProjectInfo(@RequestParam(value = "regionId", required = false) String regionId) {
		boolean hasRole = hasRole("investHQAccounts");
		boolean hasRole2 = hasRole("investRegionAccounts");
		if (!hasRole && !hasRole2) {
			// 权限不足
			return Response.response("noAuthority");
		}
		return investProjectInvestmentService.getProjectInfo(regionId);
	}

	@PostMapping(value = "/list", produces = "application/json;charset=utf-8")
	public Result list(@RequestParam(value = "regionId", required = false) String regionId,
			@RequestParam(value = "projectName", required = false) String projectName,
			@RequestParam(value = "regionIdStr", required = false) String regionIdStr, HttpServletRequest request) {
		boolean hasRole = hasRole("investHQAccounts");
		boolean hasRole2 = hasRole("investRegionAccounts");
		if (!hasRole && !hasRole2) {
			// 权限不足
			return Response.response("noAuthority");
		}

		return investProjectInvestmentService.queryPageList(regionId, projectName, regionIdStr, getAccessToken(request));
	}

	@GetMapping(value = "/regions", produces = "application/json;charset=utf-8")
	public Result regionsList() {
		
		return investProjectInvestmentService.queryRegionsList();
	}

	@GetMapping(value = "/one", produces = "application/json;charset=utf-8")
	public Result queryById(@RequestParam(value = "id", required = true) Integer id, HttpServletRequest request) {
		boolean hasRole = hasRole("investHQAccounts");
		boolean hasRole2 = hasRole("investRegionAccounts");
		if (!hasRole && !hasRole2) {
			// 权限不足
			return Response.response("noAuthority");
		}
		return investProjectInvestmentService.queryById(id);
	}
	
	
	
	@PostMapping(value = "/isRepeat", produces = "application/json;charset=utf-8")
	public Result regionsList(InvestProjectInvestment investProjectInvestment,HttpServletRequest request) {
		return investProjectInvestmentService.projectNameIsRepeat(investProjectInvestment,getAccessToken(request));
	}

	@PostMapping(value = "/add")
	public Result add(InvestProjectInvestment investProjectInvestment, HttpServletRequest request) {
		
		boolean hasRole = hasRole("investRegionAccounts");
		if (!hasRole) {
			// 权限不足
			return Response.response("noAuthority");
		}

		return investProjectInvestmentService.add(investProjectInvestment, getAccessToken());
	}

	@PutMapping(value = "/edit")
	public Result edit(InvestProjectInvestment investProjectInvestment, HttpServletRequest request) {
		boolean hasRole = hasRole("investRegionAccounts");
		if (!hasRole) {
			// 权限不足
			return Response.response("noAuthority");
		}
		if (investProjectInvestment.getId() == null) {
			throw new ServiceException("编辑异常，请刷新浏览器后重新发起编辑请求！");
		}
		return investProjectInvestmentService.edit(investProjectInvestment, getAccessToken());
	}

	@DeleteMapping(value = "/del")
	public Result del(@RequestParam(value = "id", required = true) Integer id) {
		boolean hasRole = hasRole("investRegionAccounts");
		if (!hasRole) {
			// 权限不足
			return Response.response("noAuthority");
		}
		return investProjectInvestmentService.del(id);
	}

	@GetMapping(value = "/regions/tree", produces = "application/json;charset=utf-8")
	public Result organTree() {
		return investProjectInvestmentService.getOrganTreeData();
	}

}