package com.iamberry.wechat.handles.binding;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import org.apache.commons.lang.StringUtils;
import com.iamberry.wechat.core.entity.bindingApp.BindingMacInfo;
import com.iamberry.wechat.face.bindingapp.BindingMacService;
import com.iamberry.wechat.tools.DateTimeUtil;
import com.iamberry.wechat.tools.NameUtils;
import com.iamberry.wechat.tools.ObjectExcelView;

/**
 * 扫码绑定用户、机器号 后台
 * create date 2016年6月13日
 * @author 穆再兴
 *
 */
@Controller
@RequestMapping("/admin/binding")
public class BindingHandler {
	@Autowired
	private BindingMacService bindingMacService;
	
	/**
	 * 后台根据查询条件 获取所有绑定关系列表集合
	 * create date 2016年6月13日
	 * @author 穆再兴
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView getBingInfoList(BindingMacInfo info,
			@RequestParam(value= "pageSize",defaultValue= "15" ,required=false) Integer pageSize, 
			@RequestParam(value = "pageNO", defaultValue = "1",required=false) Integer pageNO){
		ModelAndView mv = new ModelAndView("admin/binding/bindingList");
		info.getPage().setPageSize(pageSize);
		info.getPage().setPageNumber(pageNO);
		info.getPage().initRecordBegin();
		mv.addObject("macId", info.getMacId());
		mv.addObject("beginDate", info.getBeginDate());
		mv.addObject("endDate", info.getEndDate());
		if(StringUtils.isNotEmpty(info.getEndDate())){
			info.setEndDate(info.getEndDate() + " 23:59:59");
		}
		List<BindingMacInfo> list = bindingMacService.selectAll(info);
		String userNickName = "";
		for (BindingMacInfo macInfo : list) {
			if(macInfo.getUserNickName() != null){
				try {
					userNickName = URLDecoder.decode(macInfo.getUserNickName(), "UTF-8");
					macInfo.setUserNickName(userNickName);
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
			}
		}
		mv.addObject("list", list);
		
		int count = bindingMacService.getAllCount(info);
		int pageCount =  count % info.getPage().getPageSize() == 0? count/info.getPage().getPageSize():(count/info.getPage().getPageSize())+1;
		//分页用
		mv.addObject("pageNO", pageNO);
		mv.addObject("url", "/admin/binding/list?pageSize=" + pageSize + "&pageNO=");
		mv.addObject("pageSize", pageCount);
		return mv;
	}
	
	/**
	 * 导出Excel
	 * 2016年6月13日
	 * @author 穆再兴
	 * @return
	 */
	@RequestMapping("/excel")
	public ModelAndView downloadBindingExcel(BindingMacInfo info){
		// 准备model
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("fileName", "用户机器码关系列表");	// 下载文件名称
		
		// 标题
		List<String> titles = new ArrayList<String>();
		titles.add("用户昵称");
		titles.add("机器码");
		titles.add("代理商");
		titles.add("代理商电话");
		titles.add("绑定时间");
		model.put("titles", titles);
		
		// 内容
		List<List<Object>> countexts = new ArrayList<List<Object>>();
		info.getPage().setPageSize(5000);
		if(StringUtils.isNotEmpty(info.getEndDate())){
			info.setEndDate(info.getEndDate() + " 23:59:59");
		}
		List<BindingMacInfo> list = bindingMacService.selectAll(info);
		for (BindingMacInfo macInfo : list) {
			List<Object> row = new ArrayList<Object>();
			try {
				if(macInfo.getUserNickName() != null){
					row.add(URLDecoder.decode(macInfo.getUserNickName(), "UTF-8"));
				}
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			row.add(macInfo.getMacId());
			row.add(macInfo.getDrpName());
			row.add(macInfo.getDrpPhone());
			if(macInfo.getBindTime() != null){
				row.add(DateTimeUtil.format(macInfo.getBindTime(), NameUtils.getConfig("DATE_TIME_FORMAT")));
			}
			countexts.add(row);
		}
		model.put("varList", countexts);

		ObjectExcelView erv = new ObjectExcelView();
		ModelAndView mv = new ModelAndView(erv,model);
		return mv;
	}
	
	/**
	 * 删除一个binding信息
	 * create date 2016年6月14日
	 * @author 穆再兴
	 * @return
	 */
	@RequestMapping("/delete")
	public String deleteBinding(@RequestParam(value = "id", defaultValue = "",required=false) String id){
		if(StringUtils.isEmpty(id)){
			return "{\"status\":\"删除失败！\"}" ;
		}
		int i = bindingMacService.deleteBindingById(Integer.parseInt(id));
		if(i > 0 ){
			return "{\"status\":\"删除成功！\"}";
		}else{
			return "{\"status\":\"删除失败！\"}" ;
		}
	}
}
