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.dto.ManagerMapOrganTreeDTO;
import cn.com.countrygarden.bi.main.dto.investment.InvestRegionTotalDTO;
import cn.com.countrygarden.bi.main.exception.ServiceException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.com.countrygarden.bi.main.model.investment.InvestRegionTotal;
import cn.com.countrygarden.bi.main.service.investment.InvestRegionTotalService;
import cn.com.countrygarden.bi.main.service.investment.impl.InvestExcelServiceImpl;

/**
 * 区域投资-业务逻辑层实现
 *
 * @author 汤婷
 * @email www.justintoforest@gamail.com
 * @url
 * @date 2018-08-15
 */
@RestController("InvestRegionTotalController")
@RequestMapping("/api/invest/regional")
public class InvestRegionTotalController extends BaseController {

	@Autowired
	private InvestRegionTotalService investRegionTotalService;
	@Autowired
	private InvestExcelServiceImpl investExcelService;

	@GetMapping(value = "/list", produces = "application/json;charset=utf-8")
	public Result list(InvestRegionTotalDTO investRegionTotalDTO,
			@RequestParam(value = "regionId", required = false) String regionId,
			@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 investRegionTotalService.queryPageList(investRegionTotalDTO, getAccessToken(request));
	}
    
	@GetMapping(value = "/valuedetailPrint", produces = "application/json;charset=utf-8")
	public Result valuedetailPrint(HttpServletResponse response,HttpServletRequest request,
			@RequestParam(value = "regionId", required = false) String regionId) {
		boolean hasRole = hasRole("investHQAccounts");
		boolean hasRole2 = hasRole("investRegionAccounts");
		if (!hasRole && !hasRole2) {
			// 权限不足
			return Response.response("noAuthority");
		}
		try {
			return investExcelService.importInvest(response,getAccessToken(request), regionId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}

	@GetMapping(value = "/getRegionList", produces = "application/json;charset=utf-8")
	public Result getRegionList(HttpServletRequest request) {
		boolean hasRole = hasRole("investHQAccounts");
		boolean hasRole2 = hasRole("investRegionAccounts");
		if (!hasRole && !hasRole2) {
			// 权限不足
			return Response.response("noAuthority");
		}
		return investRegionTotalService.getRegionList(getAccessToken(request));
	}

	@GetMapping(value = "/getProjectSelect", produces = "application/json;charset=utf-8")
	public Result getProjectSelect(ManagerMapOrganTreeDTO managerMapOrganTreeDTO) {
		return investRegionTotalService.getProjectSelect(managerMapOrganTreeDTO);
	}

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

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

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

		return investRegionTotalService.edit(investRegionTotal, 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 investRegionTotalService.del(id);
	}

	@GetMapping(value = "/findAllComm", produces = "application/json;charset=utf-8")
	public Result findAllComm(HttpServletRequest request) {
		boolean hasRole = hasRole("investHQAccounts");
		boolean hasRole2 = hasRole("investRegionAccounts");
		if (!hasRole && !hasRole2) {
			// 权限不足
			return Response.response("noAuthority");
		}
		return investRegionTotalService.findAllCommByUserCode(getAccessToken(request));
	}

	@GetMapping(value = "/investRegionTotalregions/tree", produces = "application/json;charset=utf-8")
	public Result organTree() {
		return investRegionTotalService.getOrganTreeInvestmentNameAndRegionId();
	}
	/**
	 * 导入文件
	 * 
	 * @param personnelSharppathevaluation
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/export", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
	public Result upload(@RequestParam(value = "name", defaultValue = "1", required = true) String name,			
			@RequestParam(value = "uploadfiles", defaultValue = "", required = true) MultipartFile multipartFile,
			HttpServletRequest request)
			throws ServiceException {
		boolean hasRole = hasRole("investExcelImportAdmin");
		if(!hasRole) {
			// 权限不足
			return Response.response("权限不足");
		}

		if (multipartFile == null) {
			throw new ServiceException("请选择需要上传的文件");
		} else if (name == null) {
			throw new ServiceException("请输入文件名");
		}
		CommonsMultipartFile commonsMultipartFile = (CommonsMultipartFile) multipartFile;
		if (commonsMultipartFile.isEmpty()) {
			return Response.responseFail("上传文件为空文件");
		}
		String fileName = commonsMultipartFile.getOriginalFilename();
		if (fileName.indexOf(".xlsx") <= 0 || fileName.indexOf(".xls") <= 0) {
			return Response.responseFail("上传文件应为excel文件格式为：xlsx 或 xls");
		}

		return Response.response(investExcelService.exportExcel(fileName, multipartFile,
				getAccessToken(request)));

	}
}