package org.ccpit.business.coApplication;

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;


@RequestMapping(value="/business/coApplicationManage")
@RestController
public class CoApplicationInfoController extends BaseController {
	
	@Autowired
	private CoApplicationInfoService coApplicationInfoService;
	@Autowired
	private LogInfoService logInfoService;

	@RequestMapping(value ="/goinCoApplicationInfoPage")
	public ModelAndView getPage() {
		ModelAndView mv = new ModelAndView("business/coApplicationInfoPage");
		return mv;
	}
	@RequestMapping(value="/isExistCoApplicationInfo")
	public Object isExistNewsInfo(HttpServletRequest request){
		Map<String,Object> result = new HashMap<String,Object>();
		String ccpitCode = request.getParameter("ccpitCode");
		String registNumber = request.getParameter("registNumber");
		boolean isExist = coApplicationInfoService.queryCoApplicationInfoByData(ccpitCode,registNumber);
		result.put("flag", isExist);
		if(isExist){
			result.put("info", "信息已存在，请不要重复录入...");
		}
		return result;
	}
	
	
	@RequestMapping(value="/addOrUpdateCoApplicationInfo",produces = "application/json;charset=UTF-8")
	public Object addOrUpdateCoApplicationInfo(HttpServletRequest request,HttpServletResponse response, CoApplicationInfo coApplicationInfo){
		String result = "";
		String operate = request.getParameter("operate");
		String coApplicationInfoId = request.getParameter("coApplicationInfoId");
		boolean flag = coApplicationInfoService.addOrUpdateCoApplicationInfo(coApplicationInfo, operate, coApplicationInfoId, (User)getUserSession(request), request);
		if(flag){
			result = "{ \"flag\": "+ flag  +", \"info\": \"数据保存成功！\" }";
		}else {
			result = "{ \"flag\": "+ flag  +", \"info\": \"数据保存失败！\" }";
		}
		return result;
	}
	@RequestMapping(value="/queryAllCoApplicationInfo")
	public Object queryAllCoApplicationInfo(HttpServletRequest request,CoApplicationInfo CoApplicationInfo) {
		PageRequest pageRequest = getPage(request);
		String ccpitCode = request.getParameter("ccpitCode");
		String registNumber = request.getParameter("registNumber");
		Page<CoApplicationInfo> pageInformation = coApplicationInfoService.queryCoApplicationInfos(pageRequest, ccpitCode,registNumber);
		pageInformation.getRows().forEach(coApplicationInfo -> {
			coApplicationInfoService.createCoApplicationInfosIndex(coApplicationInfo);
		});
		return coApplicationInfoService.convert(pageInformation);
	}

	@RequestMapping(value="/deleteCoApplicationInfos")
	public Object deleteCoApplicationInfos(HttpServletRequest request){
		Map<String,Object> result = new HashMap<String,Object>();
		boolean flag = coApplicationInfoService.deleteCoApplicationInfoById(request.getParameter("ids"));
		result.put("flag", flag);
		if(flag){
			result.put("info", "数据删除成功！");
		}else {
			result.put("info", "数据删除失败！");
		}
		return result;
	} 
	
	@RequestMapping(value="/queryCoApplicationInfoById/{id}")
	public CoApplicationInfo queryCoApplicationInfoById(@PathVariable long id){
		return coApplicationInfoService.queryCoApplicationInfoById(id);
	}
	
	
}

