package com.cloudcross.ssp.web.back.main.ap;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cloudcross.ssp.base.web.SimpleController;
import com.cloudcross.ssp.common.utils.Pager;
import com.cloudcross.ssp.common.utils.SqlHelper;
import com.cloudcross.ssp.model.Place;
import com.cloudcross.ssp.model.PlaceClass;
import com.cloudcross.ssp.model.PlaceClassInformation;
import com.cloudcross.ssp.model.PlaceOperator;
import com.cloudcross.ssp.service.IPlaceClassInformationService;
import com.cloudcross.ssp.service.IPlaceClassService;
import com.cloudcross.ssp.service.IPlaceOperatorService;
import com.cloudcross.ssp.service.IPlaceService;

/**
 * 场景相关控制器
 * @author chenyou
 *
 */
@Controller("back.placeController")
@RequestMapping("/back/main/ap/place")
public class PlaceController extends SimpleController {
	@Autowired
	private IPlaceService placeService;
	@Autowired
	private IPlaceOperatorService placeOperatorService;
	@Autowired
	private IPlaceClassInformationService placeClassInformationService;
	@Autowired
	private IPlaceClassService placeClassService;
	
	/**
	 * 新增数聚场景页
	 * @param model
	 * @return
	 */
	@RequestMapping("/unclassfiedPlaceName")
	public String unclassfiedPlaceName(Model model) {
		//待分类数聚子场景
		List<Place> placeList = placeService.findUnclassfiedPlace();
		
		//查找所有的数聚场景
		List<PlaceClassInformation> placeClassInformationList = placeClassInformationService.findAll();
		
		//所有的数聚子场景
		List<Place> allplaceList = placeService.findAll();
		
		model.addAttribute("placeList", placeList);
		model.addAttribute("placeClassInformationList", placeClassInformationList);
		model.addAttribute("allplaceList", allplaceList);
		return page("unclassfiedPlaceName");
	}
	
	/**
	 * 将待分类数聚子场景添加到数聚场景里面
	 * @param placeId 数聚子场景的id组成的字符串，格式为"1-2-3"
	 * @param placeClassId 数聚场景的id
	 * @return
	 */
	@RequestMapping("/add")
	public String add(@RequestParam String placeId, @RequestParam Long placeClassId) {
		//判断传递过来的参数是不是为空
		if(null == placeId || "".equals(placeId) || null == placeClassId) {
			System.out.println("传入的参数有错");
		} else {
			String[] placeIdString = placeId.split("-");
			
			for(int i = 0; i < placeIdString.length; i++) {
				PlaceClass placeClass = new PlaceClass();
				placeClass.setClassId(placeClassId);
				placeClass.setPlaceId(Long.parseLong(placeIdString[i]));
				
				placeClassService.add(placeClass);
			}
			
			
		}
		
		
		return redirect(page("unclassfiedPlaceName"));
	}
	
	/**
	 * 得到所有的数聚子场景
	 * @param placeId
	 * @return
	 */
	@RequestMapping("/get-place")
	public @ResponseBody
	List<Place> getPlace() {
		
		List<Place> placeList = placeService.findAll();
		return placeList;
	}
	
	/**
	 * 删除数聚场景下的数聚子场景
	 * @param model
	 * @param id 数聚子场景的id数组
	 * @return
	 */
	@RequestMapping("/delete-place")
	public @ResponseBody String deletePlace(HttpServletRequest request, Long[] id) {
		//传递的参数是不是为空
		if(null == id) {
			System.out.println("传递的参数有误");
			return ERROR;
		}
		
		for(int i = 0; i < id.length; i++) {
			placeClassService.deletePlaceClass(id[i]);
		}
		
		return OK;
	}
	
	
	/**
	 * 用户场景定义页面
	 * @param model
	 * @param paramMap
	 * @param page
	 * @return
	 */
	@RequestMapping("place-sort")
	public String placeSort(Model model, @RequestParam HashMap<String, Object> paramMap,
			@RequestParam(defaultValue = "1") int page) {
		String place = null;
		String place1 = null;
		// 对传递过来的场景名称做处理，如果传递的为空或者是空字符串，则让它等于空
		if (null == paramMap.get("place")
				|| "".equals(paramMap.get("place"))) {
			paramMap.put("place", null);
		} else {
			try {
				place = new String(String.valueOf(paramMap.get("place")).trim().getBytes("ISO-8859-1"), "UTF-8");
				place1 = place;
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			paramMap.put("place", SqlHelper.doLike(place));
		}

		int totalRow = 0;
		Pager pager = null;
		pager = new Pager();
		// 查询符合条件的总的条数
		totalRow = placeOperatorService.countUnclassfiedPlace(paramMap);
		pager.setPage(page);
		pager.setTotalRow(totalRow);
		
		paramMap.put("pager", pager);
		List<PlaceOperator> placeOperatorList = placeOperatorService.findUnclassfiedPlace(paramMap);
		
		//将传递过来的搜索参数回传
		if(null != place) {
			model.addAttribute("place", place1);
		}
		model.addAttribute("placeOperatorList", placeOperatorList);
		model.addAllAttributes(paramMap);
		return page("place-sort");
	}
	
	/**
	 * 根据数聚场景id得到其对应的所有数聚子场景
	 * @return
	 */
	@RequestMapping("/placeName")
	public @ResponseBody List<Place> findAllPlaceName(Long placeClassId) {
		List<Place> placeList = placeService.findPlaceByPlaceClassId(placeClassId);
		return placeList;
	}
	
	
	/**
	 * 用户场景分类的保存
	 * 这里要传递两个变量，一个是t_place_operator的id字符串(变量名为id)，另一个就是数聚场景id(变量名叫placeId)
	 * 注：字符串格式像"1-4-5",就是id以-号划分，当有多个id的时候;
	 * @param model
	 * @param paramMap
	 * @return
	 */
	@RequestMapping("/save")
	public String placeSort(Model model, @RequestParam HashMap<String, Object> paramMap) {
		Long[] id = null;
		Long placeId = null;
		//当传递过来的是空值或者是空字符串时，则什么也不做
		if(null == paramMap.get("id") || "".equals(paramMap.get("id"))
				|| null == paramMap.get("placeId") || "".equals(paramMap.get("placeId"))) {
			return redirect(page("place-sort"));
		} else {
			String idString = String.valueOf(paramMap.get("id"));
			String[] idArray = idString.split("-");
			placeId = Long.parseLong(String.valueOf(paramMap.get("placeId")));
			id = new Long[idArray.length];
			
			for(int i = 0; i < id.length; i++) {
				id[i] = Long.parseLong(idArray[i]);
			}
 		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("idList", id);
		map.put("placeId", placeId);
		placeOperatorService.update(map);
		
		return redirect(page("place-sort"));
	}
	
}
