package com.palmnest.webapp.controller.notice;


import cn.jiguang.common.utils.StringUtils;
import com.palmnest.application.bean.AppResultDto;
import com.palmnest.application.core.util.AppResultUtil;
import com.palmnest.application.core.util.StringUtil;
import com.palmnest.model.User;
import com.palmnest.model.estateserve.WxPublicno;
import com.palmnest.service.notice.WxMassmessageSendManager;
import com.palmnest.service.notice.WxPubManager;
import com.palmnest.service.notice.service.NoticePublicnoService;
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.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

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

@Controller
@RequestMapping("/notice/publicno")
public class NoticePublicNoController {

	@Autowired
	private WxMassmessageSendManager wxMassmessageSendManager;
	@Autowired
	private NoticePublicnoService    noticePublicnoService;

	@Autowired
	private WxPubManager wxPubManager;

//	/**
//	 * 新增素材-缩略图(64KB JPG格式)
//	 * @param orgIds
//	 * @return
//	 * @throws Exception
//	 */
//	@RequestMapping(value = "/addThumbImg")
//	@ResponseBody
//	public AppResultDto addThumbImg(@RequestParam("url") String url)throws Exception{
//		
//		return AppResultUtil.createSuccesDto();
//	}
//	
//	
//	/**
//	 * 上传图文消息
//	 * @param appNotice
//	 * @param request
//	 * @param response
//	 * @return
//	 * @throws Exception
//	 */
//	@RequestMapping(value = "/uploadnews")
//	@ResponseBody
//	public AppResultDto uploadnews(@RequestParam("orgIds") String orgIds)throws Exception{
//		//根据项目查找相应的公众号，然后根据公众号找相应的token
//		if(!StringUtil.isNotEmpty(orgIds)){
//			return AppResultUtil.createErrorDto("500", "项目不能为空");
//		}
//		return AppResultUtil.createSuccesDto();
//	}
//	

	/**
	 * 获取部分发送用户
	 *
	 * @param orgIds
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getUserByOrgId")
	@ResponseBody
	public AppResultDto getUserByOrgId(String orgIds, String name) throws Exception {
		if (!StringUtil.isNotEmpty(orgIds)) {
			return AppResultUtil.createErrorDto("500", "未获取到项目");
		}
		System.out.println("getUserByOrgId/orgIds:" + orgIds);
		if (",".equals(orgIds.substring(orgIds.length() - 1, orgIds.length()))) {
			orgIds = orgIds.substring(0, orgIds.length() - 1);
		}
		return AppResultUtil.createSuccesDto(wxMassmessageSendManager.getUserByOrgId(orgIds, name));
	}


	/**
	 * 删除详情
	 *
	 * @param request
	 * @param noticeDetailId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/delDetaile")
	@ResponseBody
	public AppResultDto delDetaile(HttpServletRequest request, Integer noticeDetailId) throws Exception {
		User   user = (User) request.getSession().getAttribute("UserCache");
		String res  = noticePublicnoService.delDetaile(noticeDetailId, user.getId().toString());
		if (StringUtil.isNotEmpty(res)) {
			return AppResultUtil.createErrorDto("500", res);
		}
		return AppResultUtil.createSuccesDto();
	}


	@RequestMapping(value = "/saveImages")
	@ResponseBody
	public AppResultDto saveImages(HttpServletRequest request, Integer noticeDetailId) throws Exception {
		User   user = (User) request.getSession().getAttribute("UserCache");
		String res  = noticePublicnoService.delDetaile(noticeDetailId, user.getId().toString());
		if (StringUtil.isNotEmpty(res)) {
			return AppResultUtil.createErrorDto("500", res);
		}
		return AppResultUtil.createSuccesDto();
	}


	/**
	 * 导入发送的用户
	 *
	 * @param excelFile
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "importUserInfo")
	@ResponseBody
	public AppResultDto importUserInfo(@RequestParam("excelFile") MultipartFile excelFile, HttpServletRequest request, HttpServletResponse response) throws Exception {
		User   user   = (User) request.getSession().getAttribute("UserCache");
		String userId = "";
		if (user != null) {
			userId = String.valueOf(user.getId());
		}
		String orgId = request.getParameter("orgId");
		if (StringUtils.isEmpty(orgId)) {
			return AppResultUtil.createErrorDto("500", "项目必填");
		}
		WxPublicno wxpub = wxPubManager.getPublicByOrgId(orgId);
		if (null == wxpub || StringUtils.isEmpty(wxpub.getDevelopId())) {
			return AppResultUtil.createErrorDto("500", "该项目下未找到公众号");
		}
		AppResultDto dto = noticePublicnoService.importUserInfo(orgId, excelFile.getInputStream(), wxpub.getDevelopId());
		return dto;
	}
}
