package org.ccpit.business.visaAgencyManage;

import java.util.HashMap;
import java.util.Map;

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

import org.ccpit.base.controller.BaseController;
import org.ccpit.base.controller.Page;
import org.ccpit.base.controller.PageRequest;
import org.ccpit.base.logManage.LogInfoService;
import org.ccpit.base.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
/**
 * 
 * @author wangyanxin
 *
 */
@RestController
@RequestMapping(value="/business/visaAgencyManage")
public class VisaAgencyController extends BaseController {
	@Autowired
	private VisaAgencyService visaAgencyService;
	@Autowired
	private LogInfoService logInfoService;
	@RequestMapping(value ="/goinVisaAgencyPage")
	public ModelAndView getPage() {
		ModelAndView mv = new ModelAndView("business/visaAgencyPage");
		return mv;
	}
	
	
	@RequestMapping(value="/queryAllVisaAgency")
	public Object queryAllVisaAgency(HttpServletRequest request,VisaAgency visaAgency) {
		PageRequest pageRequest = getPage(request);
		String visaAgencyName = request.getParameter("visaAgencyName");
		Page<VisaAgency> pageInformation = visaAgencyService.queryVisaAgencys(pageRequest, visaAgencyName);
		/*pageInformation.getRows().forEach(visaAgency -> {
			visaAgencyService.createVisaAgencysIndex(visaAgency);
		});*/
		return visaAgencyService.convert(pageInformation);
	}
	
	@RequestMapping(value="/isExistVisaAgency")
	public Object isExistVisaAgency(HttpServletRequest request){
		Map<String,Object> result = new HashMap<String,Object>();
		String visaAgencyName = request.getParameter("visaAgencyName");
		boolean isExist = visaAgencyService.queryVisaAgencyByTitle(visaAgencyName);
		result.put("flag", isExist);
		if(isExist){
			result.put("info", "签证机构信息已存在，请不要重复录入...");
		}
		return result;
	}
	
	
	@RequestMapping(value="/addOrUpdateVisaAgency",produces = "application/json;charset=UTF-8")
	public Object addOrUpdateVisaAgency(HttpServletRequest request,HttpServletResponse response, VisaAgency visaAgency){
		String result = "";
		String operate = request.getParameter("operate");
		String visaAgencyId = request.getParameter("visaAgencyId");
		boolean flag = visaAgencyService.addOrUpdateVisaAgency(visaAgency, operate, visaAgencyId, (User)getUserSession(request), request);
		if(flag){
			result = "{ \"flag\": "+ flag  +", \"info\": \"数据保存成功！\" }";
		}else {
			result = "{ \"flag\": "+ flag  +", \"info\": \"数据保存失败！\" }";
		}
		return result;
	}
	
	
	@RequestMapping(value="/deleteVisaAgencys")
	public Object deleteVisaAgencys(HttpServletRequest request){
		Map<String,Object> result = new HashMap<String,Object>();
		boolean flag = visaAgencyService.deleteVisaAgencyById(request.getParameter("ids"));
		result.put("flag", flag);
		if(flag){
			result.put("info", "数据删除成功！");
		}else {
			result.put("info", "数据删除失败！");
		}
		return result;
	} 
	@RequestMapping(value="/queryVisaAgencyById/{id}")
	public VisaAgency queryVisaAgencyById(@PathVariable long id){
		return visaAgencyService.queryVisaAgencyById(id);
	}
}
