package com.palmnest.webapp.controller.wechat;

import com.palmnest.application.ElementConstant;
import com.palmnest.application.core.util.FileUploadUtils;
import com.palmnest.model.estateserve.Announcement;
import com.palmnest.service.estateserve.AnnouncementManager;
import com.palmnest.service.estateserve.AppUserManager;
import com.palmnest.service.estateserve.PublicNoManager;
import com.palmnest.webapp.controller.BaseFormController;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 信息类操作前端控制器
 * 通知公告、校园活动
 *
 * @author yubing
 * @since 2017-1-21
 */
@Slf4j
@Controller
@RequestMapping(value = "/wechat/info*")
public class InfoOperationController extends BaseFormController {


	@Autowired
	private AppUserManager      appUserManager;
	@Autowired
	private PublicNoManager     publicNoManager;
	@Autowired
	private AnnouncementManager announcementManager;

	/**
	 * 信息详情页
	 */
	@RequestMapping(value = "/getDetail/{type}&{id}&{userId}&{openId}&{developId}")
	public ModelAndView getAnnouncementDetail(@PathVariable String type, @PathVariable Integer id, @PathVariable Integer userId,
											  @PathVariable String openId, @PathVariable String developId, HttpServletRequest request) throws Exception {
		Model model = new ExtendedModelMap();
		if (ElementConstant.InformationTypeNotice.equals(type)) {
			//查询通知公告
			//查询阅读数、点赞数
			Announcement anno = announcementManager.get(id);
			anno.setReaderNum(anno.getReaderNum() + 1);
			announcementManager.updateModel(anno);

			//图片
			List<String> images = new ArrayList<String>();
			if (StringUtils.isNotBlank(anno.getCoverImageUrl())) {
				images.add(anno.getCoverImageUrl());
			}
			model.addAttribute("model", anno);
			model.addAttribute("images", images);
		}

		//查询留言信息
		model.addAttribute("userId", userId);
		model.addAttribute("openId", openId);
		model.addAttribute("developId", developId);
		model.addAttribute("type", type);
		model.addAttribute("infoId", id);
		model.addAttribute("visitPrefix", FileUploadUtils.getVisitPrefix());
		return new ModelAndView("wechat/informationDetails", model.asMap());
	}

}
