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

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.core.ResultGenerator;
import cn.com.countrygarden.bi.main.dto.CustomerDto;
import cn.com.countrygarden.bi.main.model.ExeclTemplateConstant;
import cn.com.countrygarden.bi.main.model.mediaResource.Customer;
import cn.com.countrygarden.bi.main.service.mediaResource.CustomerService;
import cn.com.countrygarden.bi.main.util.DownloadExeclTemplate;

@RestController
@RequestMapping("/api/media/customer")
public class CustomerController {

	@Autowired
	private CustomerService customerService;
	
	@PostMapping("/findCustomerPage")
	public Result findBasicInfoPage(HttpServletRequest request,CustomerDto dto) {
		try {
			String accessToken = request.getParameter("AccessToken");
			return ResultGenerator.genSuccessResult(customerService.findCustomerByPage(accessToken,dto));
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.genFailResult("获取客户信息列表错误！");
		}
	}
	
	@PostMapping("/saveOne")
	public Result saveOne(HttpServletRequest request,Result rt,CustomerDto dto) {
		try {
			String accessToken = request.getParameter("AccessToken");
			return customerService.saveOne(rt,accessToken,dto);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.genFailResult("保存单个客户信息错误！");
		}
	}
	
	@GetMapping("/findOne")
	public Result findOne(Customer po) {
		try {
			return ResultGenerator.genSuccessResult(customerService.findOne(po));
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.genFailResult("获取单个客户信息错误！");
		}
	} 
	
	@PostMapping("/del")
	public Result del(CustomerDto dto) {
		try {
			if (0==dto.getCustomerIds().length || null == dto.getCustomerIds()) {
				return ResultGenerator.genFailResult("客户信息为空！");
			}
			return ResultGenerator.genSuccessResult(customerService.del(dto));
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.genFailResult("获取单个客户信息错误！");
		}
	}  
	
	@GetMapping("/exportOut")
	public void export(HttpServletResponse response) {
		try {
			DownloadExeclTemplate.downloadTemplate(response, ExeclTemplateConstant.mediaCustomerTemplate);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@PostMapping("/exportIn")
	public Result exportIn(HttpServletRequest request,Result rt,@RequestParam("file")MultipartFile file) {
		try {
			String accessToken = request.getParameter("AccessToken");
			return customerService.exportIn(accessToken,rt,file);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.genFailResult("导入客户信息错误");
		}
	}
}
