/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.iim.web;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

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

import com.jeeplus.common.oss.OSSClientUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jeeplus.common.config.Global;
import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.MyBeanUtils;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.common.web.BaseController;
import com.jeeplus.modules.iim.dao.LayGroupUserDao;
import com.jeeplus.modules.iim.entity.LayGroup;
import com.jeeplus.modules.iim.entity.LayGroupUser;
import com.jeeplus.modules.iim.service.LayGroupService;
import com.jeeplus.modules.sys.utils.UserUtils;

/**
 * 群组Controller
 * @author lgf
 * @version 2016-08-07
 */
@Controller
@RequestMapping(value = "${adminPath}/iim/layGroup")
public class LayGroupController extends BaseController {

	@Autowired
	private LayGroupService layGroupService;
	
	@Autowired
	private LayGroupUserDao layGroupUserDao;

	@ModelAttribute
	public LayGroup get(@RequestParam(required=false) String id) {
		LayGroup entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = layGroupService.get(id);
		}
		if (entity == null){
			entity = new LayGroup();
		}
		return entity;
	}
	
	/**
	 * 群组列表页面
	 */
	@RequestMapping(value = {"list", ""})
	public String list(LayGroup layGroup, HttpServletRequest request, HttpServletResponse response, Model model) {
		//查找我属于的群组
		List<LayGroup> memberLayGroupList = layGroupService.findGroupList(UserUtils.getUser());
		model.addAttribute("layGrouplist", memberLayGroupList);
		return "modules/iim/layGroupList";
	}

	/**
	 * 查看，增加，编辑群组表单页面
	 */
	@RequestMapping(value = "form")
	public String form(LayGroup layGroup, Model model) {
		model.addAttribute("layGroup", layGroup);
		return "modules/iim/layGroupForm";
	}
	
	/**
	 * 添加群组成员--->常用联系人
	 */
	@RequestMapping(value = "addUser")
	public String addUser(String ids, String groupid, RedirectAttributes redirectAttributes) {
		int c = ids.split(",").length;
		int count = layGroupService.saveUsers(groupid,ids);
		if (c == count){
			addMessage(redirectAttributes, "添加组员成功");
		}else {
			addMessage(redirectAttributes, "新增成员共" + ids.split(",").length + "个," + count + "个成员新增成功! " + "," + (ids.split(",").length-count) + "个成员已存在!");
		}
		return "redirect:"+Global.getAdminPath()+"/iim/layGroup/?repage";
	}
	/**
	 * 保存群组
	 */
	@RequestMapping(value = "save")
	public String save(LayGroup layGroup, Model model,
					   @RequestParam(value = "this_upload_files",required = false)MultipartFile[] this_upload_files,HttpServletRequest request, RedirectAttributes redirectAttributes) throws Exception{
		if (!beanValidator(model, layGroup)){
			return form(layGroup, model);
		}
		if(layGroup==null || StringUtils.isBlank(layGroup.getAvatar()) || this_upload_files==null){
			layGroup.setAvatar("https://gangwan-app.oss-cn-hangzhou.aliyuncs.com/app-img/photo/static/qunzhu.png");
		}
		if(!layGroup.getIsNewRecord()){//编辑表单保存
			LayGroup t = layGroupService.get(layGroup.getId());//从数据库取出记录的值
			MyBeanUtils.copyBeanNotNull2Bean(layGroup, t);//将编辑表单中的非NULL值覆盖数据库记录中的值
            /*String avatar = layGroup.getAvatar();
            if (avatar!=null && !avatar.equals("") && avatar.contains("userfiles")){
                avatar = layGroup.getAvatar().replace("|","");
                InputStream inputStream = null;
                HttpURLConnection httpURLConnection;
                try {
                    URL url = new URL(request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+ avatar);
                    httpURLConnection = (HttpURLConnection) url.openConnection();
                    // 设置网络连接超时时间
                    httpURLConnection.setConnectTimeout(3000);
                    // 设置应用程序要从网络连接读取数据
                    httpURLConnection.setDoInput(true);

                    httpURLConnection.setRequestMethod("GET");
                    int responseCode = httpURLConnection.getResponseCode();
                    if (responseCode == 200) {
                        // 从服务器返回一个输入流
                        inputStream = httpURLConnection.getInputStream();

                    }
                    OSSClientUtil ossUtil =new OSSClientUtil();
                    String[] arr = avatar.split("/");
                    logger.info("arr[arr.length-1]="+arr[arr.length-1]);
                    String photoName = System.currentTimeMillis()+arr[arr.length-1];
                    ossUtil.uploadFile2OSS(inputStream, Global.getRqcode(),photoName);
                    layGroup.setAvatar(Global.getAliyunUrl()+"/"+Global.getRqcode() + photoName);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    inputStream.close();
                }
            }*/
			layGroupService.save(t,this_upload_files);//保存
		}else{//新增表单保存
			layGroupService.save(layGroup,this_upload_files);//保存
		}
		addMessage(redirectAttributes, "保存群组成功");
		return "redirect:"+Global.getAdminPath()+"/iim/layGroup/?repage";
	}
	
	/**
	 * 删除群组
	 */
	@RequestMapping(value = "delete")
	public String delete(LayGroup layGroup, RedirectAttributes redirectAttributes) {
		layGroupService.delete(layGroup);
		addMessage(redirectAttributes, "解散群组成功");
		return "redirect:"+Global.getAdminPath()+"/iim/layGroup/?repage";
	}
	
	/**
	 * 退出群组
	 */
	@RequestMapping(value = "logout")
	public String logout(LayGroupUser layGroupUser, RedirectAttributes redirectAttributes) {
		layGroupUserDao.delete(layGroupUserDao.findList(layGroupUser).get(0));
		addMessage(redirectAttributes, "退出群组成功");
		return "redirect:"+Global.getAdminPath()+"/iim/layGroup/?repage";
	}
	
	/**
	 * 批量删除群组
	 */
	@RequestMapping(value = "deleteAll")
	public String deleteAll(String ids, RedirectAttributes redirectAttributes) {
		String idArray[] =ids.split(",");
		for(String id : idArray){
			layGroupService.delete(layGroupService.get(id));
		}
		addMessage(redirectAttributes, "删除群组成功");
		return "redirect:"+Global.getAdminPath()+"/iim/layGroup/?repage";
	}
	
	/**
	 * 导出excel文件
	 */
    @RequestMapping(value = "export", method=RequestMethod.POST)
    public String exportFile(LayGroup layGroup, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String fileName = "群组"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
            Page<LayGroup> page = layGroupService.findPage(new Page<LayGroup>(request, response, -1), layGroup);
    		new ExportExcel("群组", LayGroup.class).setDataList(page.getList()).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出群组记录失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/iim/layGroup/?repage";
    }

	/**
	 * 导入Excel数据

	 */
    @RequestMapping(value = "import", method=RequestMethod.POST)
    public String importFile(MultipartFile file, RedirectAttributes redirectAttributes) {
		try {
			int successNum = 0;
			int failureNum = 0;
			StringBuilder failureMsg = new StringBuilder();
			ImportExcel ei = new ImportExcel(file, 1, 0);
			List<LayGroup> list = ei.getDataList(LayGroup.class);
			for (LayGroup layGroup : list){
				try{
					layGroupService.save(layGroup);
					successNum++;
				}catch(ConstraintViolationException ex){
					failureNum++;
				}catch (Exception ex) {
					failureNum++;
				}
			}
			if (failureNum>0){
				failureMsg.insert(0, "，失败 "+failureNum+" 条群组记录。");
			}
			addMessage(redirectAttributes, "已成功导入 "+successNum+" 条群组记录"+failureMsg);
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入群组失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/iim/layGroup/?repage";
    }
	
	/**
	 * 下载导入群组数据模板
	 */
    @RequestMapping(value = "import/template")
    public String importFileTemplate(HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String fileName = "群组数据导入模板.xlsx";
    		List<LayGroup> list = Lists.newArrayList(); 
    		new ExportExcel("群组数据", LayGroup.class, 1).setDataList(list).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入模板下载失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/iim/layGroup/?repage";
    }
	
    @RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(@RequestParam(required=false) String extId, HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<LayGroup> list = new ArrayList<LayGroup>();
		LayGroup layGroup = new LayGroup();
		List<LayGroup> ownerLayGroupList = layGroupService.findList(layGroup);
		List<LayGroup> memberLayGroupList = layGroupService.findGroupList(UserUtils.getUser());	
		list.addAll(ownerLayGroupList);
		list.addAll(memberLayGroupList);
		   for (int i=0; i<list.size(); i++){
			LayGroup g = list.get(i);
			if (StringUtils.isBlank(extId) || (extId!=null && !extId.equals(g.getId()))){
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", g.getId());
				map.put("name", g.getGroupname());
				mapList.add(map);
			}
		  }
		return mapList;
	}

	

}