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 org.apache.axis.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.com.countrygarden.bi.main.model.investment.InvestProjectAccount;
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.InvestRegionInvestmentMapper;
import cn.com.countrygarden.bi.main.dto.investment.InvestProjectAppVO;
import cn.com.countrygarden.bi.main.exception.ServiceException;
import cn.com.countrygarden.bi.main.service.investment.InvestRegionInvestmentService;
import cn.com.countrygarden.bi.main.util.ReadPropertiesUtils;
import cn.com.countrygarden.bi.main.util.TokenUtils;
import okhttp3.Request;

/**
 * 个人区域投资-业务逻辑层实现
 *
 * @author 汤婷
 * @email www.justintoforest@gamail.com
 * @url
 * @date 2018-08-20
 */
@RestController("InvestRegionInvestmentController")
@RequestMapping("/api/invest/region/investment")
public class InvestRegionInvestmentController extends BaseController {
	private final String LTPA_TOKEN = "LtpaToken";
	
	@Value("${yuanbao.appkey}")
	private String YUANBAO_APP_KEY;
	
	@Autowired
	private InvestRegionInvestmentService investRegionInvestmentService;

	@PostMapping(value = "/validateBIP")
	public Result validateBIP(InvestRegionInvestment investRegionInvestment, HttpServletRequest request) {

		boolean hasRole = hasRole("investRegionAccounts");
		if (!hasRole) {
			// 权限不足
			return Response.response("noAuthority");
		}
		return investRegionInvestmentService.validateBIP(investRegionInvestment);
	}

	@GetMapping(value = "/app", produces = "application/json;charset=utf-8")
	public Result list(@CookieValue(value = LTPA_TOKEN, defaultValue = "") String ltpatoken, HttpServletRequest req) {
		
		String bipUserAccount = null;
		if (StringUtils.isNoneBlank(ltpatoken)) {
			try {
				bipUserAccount = TokenUtils.parseLtpaToken(ltpatoken, YUANBAO_APP_KEY);
			} catch (Exception e) {
				e.printStackTrace();
				throw new ServiceException("InvestRegionInvestmentService接口查询出错");
			}

		}

		if (bipUserAccount != null) {
			return Response.response(investRegionInvestmentService.findApp(bipUserAccount));

		} else {
			return Response.response("用户不存在");
		}
	}

	@GetMapping(value = "/list", produces = "application/json;charset=utf-8")
	public Result list(InvestRegionInvestment investRegionInvestment,
			@RequestParam(value = "size", required = false, defaultValue = "10") int size,
			@RequestParam(value = "page", required = false, defaultValue = "1") int page, HttpServletRequest request) {
		boolean hasRole = hasRole("investHQAccounts");
		boolean hasRole2 = hasRole("investRegionAccounts");
		if (!hasRole && !hasRole2) {
			// 权限不足
			return Response.response("noAuthority");
		}
		return investRegionInvestmentService.queryPageList(investRegionInvestment, getAccessToken(request));
	}
	@GetMapping(value = "/investmentList", produces = "application/json;charset=utf-8")
	public Result investmentList(InvestProjectInvestment investProjectInvestment,
			@RequestParam(value = "size", required = false, defaultValue = "10") int size,
			@RequestParam(value = "page", required = false, defaultValue = "1") int page, HttpServletRequest request) {
		boolean hasRole = hasRole("investHQAccounts");
		boolean hasRole2 = hasRole("investRegionAccounts");
		if (!hasRole && !hasRole2) {
			// 权限不足
			return Response.response("noAuthority");
		}
		return investRegionInvestmentService.findRegionInvestmentInfo(investProjectInvestment);
	}

	@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 investRegionInvestmentService.queryById(id);
	}

	@PostMapping(value = "/add")
	public Result add(InvestRegionInvestment investRegionInvestment,@RequestParam(value="followCapitalAccountStr")String followCapitalAccountStr, HttpServletRequest request) {
		boolean hasRole = hasRole("investRegionAccounts");
		if (!hasRole) {
			// 权限不足
			return Response.response("noAuthority");
		}
		return investRegionInvestmentService.add(investRegionInvestment, followCapitalAccountStr,getAccessToken(request));
	}

	@PostMapping(value = "/edit")
	public Result edit(InvestRegionInvestment investRegionInvestment,@RequestParam(value="followCapitalAccountStr",required=false)String followCapitalAccountStr, HttpServletRequest request) {
		boolean hasRole = hasRole("investRegionAccounts");
		if (!hasRole) {
			// 权限不足
			return Response.response("noAuthority");
		}
		List<InvestProjectAccount>  investProjectAccountList=null;
		if(followCapitalAccountStr!=null&&followCapitalAccountStr!="") {
		  investProjectAccountList = JSON.parseObject(followCapitalAccountStr,new TypeReference<ArrayList<InvestProjectAccount>>() {});

		}
		return investRegionInvestmentService.edit(investRegionInvestment,investProjectAccountList, getAccessToken(request));
	}

	@DeleteMapping(value = "/del")
	public Result del(@RequestParam(value = "id", required = true) Integer id) {
		boolean hasRole = hasRole("investRegionAccounts");
		if (!hasRole) {
			// 权限不足
			return Response.response("noAuthority");
		}
		return investRegionInvestmentService.del(id);
	}
	
	
	@GetMapping(value = "/investmentAppInfo", produces = "application/json;charset=utf-8")
	public Result investmentAppInfo(@RequestParam(value = "id",required = true) Integer id) {
		return investRegionInvestmentService.findInvestmentAppInfo(id);
	}

}