package com.palmnest.webapp.controller.wechat;

import com.palmnest.model.estateserve.WxUser;
import com.palmnest.model.property.TbOrganization;
import com.palmnest.service.estateserve.ScWxuserManager;
import com.palmnest.service.estateserve.StaffManager;
import com.palmnest.service.property.PropertyManager;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 员工端微信前端控制器
 *
 * @author yubing
 * @since 2017-05-27
 */

@Controller
@RequestMapping("/wechat/staff/*")
public class WeChatStaffController {

	@Autowired
	private StaffManager    staffManager;
	@Autowired
	private ScWxuserManager scWxuserManager;
	@Autowired
	private PropertyManager propertyManager;

	@RequestMapping(value = "/staffMsgBind")
	public ModelAndView staffOrderPage(HttpServletRequest request,
									   HttpServletResponse response) throws Exception {

		Model model = new ExtendedModelMap();

		String openId    = request.getParameter("openId");
		String developId = request.getParameter("developId");

		//获取员工微信用户信息
		WxUser wxUser = scWxuserManager.getScWxuserByOpenId(openId);

		if (wxUser.getOrganizationId() != null) {
			TbOrganization organization = propertyManager.getTbOrganizationById(wxUser.getOrganizationId());
			model.addAttribute("organization", organization);
		}

		List<Map<String, Object>> cityList = propertyManager.getOrgExistCityList();

		model.addAttribute("cityList", cityList);
		model.addAttribute("openId", openId);
		model.addAttribute("developId", developId);
		model.addAttribute("wxUser", wxUser);

		return new ModelAndView("wechat/staffUnbind", model.asMap());
	}

	/**
	 * 获取项目集合信息
	 * 根据城市编码
	 */
	@RequestMapping(value = "/getOrganizationListByCityCode", method = {
			RequestMethod.POST, RequestMethod.GET}, produces = "application/json")
	public @ResponseBody
	List<TbOrganization> getOrganizationListByCityCode(
			HttpServletRequest request, HttpServletResponse response) {
		String               cityCode = request.getParameter("cityCode");
		List<TbOrganization> list     = propertyManager.getOrgByCityCode(cityCode);
		return list;
	}

	/**
	 * 员工选择项目
	 */
	@RequestMapping(value = "bindOrganization", method = RequestMethod.POST)
	public @ResponseBody
	String staffBindingProject(HttpServletRequest request,
							   HttpServletResponse response) {
		String openId         = request.getParameter("openId");
		String developId      = request.getParameter("developId");
		String organizationId = request.getParameter("organizationId");

		WxUser wxUser = scWxuserManager.getScWxuserByOpenId(openId);
		wxUser.setOrganizationId(Integer.parseInt(organizationId));
		scWxuserManager.updateModel(wxUser);
		return "success";
	}

}
