package com.platform.controller;

import java.util.List;
import java.util.Map;

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

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.base.BaseController;
import com.platform.entities.CheckoutEntity;
import com.platform.entities.PreDataInfo;
import com.platform.service.ICheckoutService;

/** 信息系统--校验
 * @author chen
 *
 */
@Controller
@RequestMapping("/checkout")
public class CheckoutController extends BaseController {
	//日志
	public final static Logger log = Logger.getLogger(CheckoutController.class);
	
	@Resource(name = "checkoutService")
	private ICheckoutService checkoutService;
	
	/**
	 * 信息系统--校验--查看所有系统的 标准表情况
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/findAll")
	public ModelMap findAll() throws Exception {
		log.info("---------/findAll--- ");
		ModelMap modelMap = new ModelMap();
		List<CheckoutEntity> result = checkoutService.findAll();
		modelMap.addAttribute("data", result);
		modelMap.addAttribute("length", result.size());
		return modelMap;
	}
	
	/**
	 * 信息系统--校验--查看所有系统的 标准表情况
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/findByCity")
	public ModelMap findByCity(@RequestBody String cityName, HttpServletRequest req, HttpServletResponse res) throws Exception {
		log.info("---------/findByCity--- "+cityName);
		ModelMap modelMap = new ModelMap();
		List<CheckoutEntity> result = checkoutService.findByCity(cityName);
		modelMap.addAttribute("data", result);
		modelMap.addAttribute("length", result.size());
		return modelMap;
	}
	
	/**
	 * 信息系统--校验--校验勾选的数据
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/checkList")
	public ModelMap checkList(@RequestBody List<CheckoutEntity> form, HttpServletRequest req, HttpServletResponse res) throws Exception {
		ModelMap modelMap = new ModelMap();
		if (null != form) {
			log.info("---------/checkList--- "+ form.size());
			List<CheckoutEntity> result = checkoutService.checkAll(form);
			modelMap.addAttribute("data", result);
			modelMap.addAttribute("length", result.size());
		}
		else {
			log.info("---------/checkList--- "+ form);
		}
		return modelMap;
	}

	/**
	 * 信息系统--校验--查看单条数据的修改详情
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/findDetails")
	public ModelMap findDetails(@RequestBody CheckoutEntity form, HttpServletRequest req, HttpServletResponse res) throws Exception {
		ModelMap modelMap = new ModelMap();
		if (null != form) {
			log.info("---------/findDetails--- "+ form);
			
			Map<String, PreDataInfo> result = checkoutService.findDetail(form);
			modelMap.addAllAttributes(result);
		}
		else {
			log.info("---------/findDetails--- "+ form);
		}
		return modelMap;
	}
	
	/**
	 *  信息系统--校验--查看后 -> 单条数据的修改详情
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/update")
	public ModelMap update(@RequestBody PreDataInfo form, HttpServletRequest req, HttpServletResponse res) throws Exception {
		ModelMap modelMap = new ModelMap();
		if (null != form) {
			log.info("---------/update--- "+ form);
			int result = checkoutService.updateStandardInfo(form);
			if (result == 0) {
				res.setStatus(500);
			}
			else {
				List<CheckoutEntity> data = checkoutService.findByCity(form.getCityName());
				modelMap.addAttribute("data", data);
				modelMap.addAttribute("length", data.size());
			}
		}
		else {
			log.info("---------/update--- "+ form);
			res.setStatus(500);
		}
		return modelMap;
	}
	
	/**
	 *  信息系统--校验--删除勾选的数据
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/deleteList")
	public ModelMap deleteList(@RequestBody List<CheckoutEntity> form, HttpServletRequest req, HttpServletResponse res) throws Exception {
		ModelMap modelMap = new ModelMap();
		if (null != form) {
			log.info("---------/deleteList--- "+ form.size());
			List<CheckoutEntity> result = checkoutService.deleteAll(form);
			modelMap.addAttribute("data", result);
			modelMap.addAttribute("length", result.size());
		}
		else {
			log.info("---------/deleteList--- "+ form);
		}
		return modelMap;
	}
	
	/** 信息系统--校验--抽取标准表（sqlserver）
	 * @param form
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/extractList")
	public ModelMap extractList(@RequestBody List<CheckoutEntity> form, HttpServletRequest req, HttpServletResponse res) throws Exception {
		ModelMap modelMap = new ModelMap();
		if (null != form) {
			log.info("---------/extractList--- "+ form.size());
			List<CheckoutEntity> result = checkoutService.extractSqlServer(form);
			modelMap.addAttribute("data", result);
			modelMap.addAttribute("length", result.size());
		}
		else {
			log.info("---------/extractList--- "+ form);
		}
		return modelMap;
	}
}
