package com.palmnest.webapp.controller.wechat;

import com.palmnest.application.core.util.LogExceptionStackUtil;
import com.palmnest.model.property.TbOrganization;
import com.palmnest.model.property.VirtualArchitecture;
import com.palmnest.service.estateserve.ScWxuserManager;
import com.palmnest.service.property.PropertyManager;
import com.palmnest.webapp.controller.BaseFormController;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
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 java.util.List;
import java.util.Map;

/**
 * 移动端 自助申请项目和企业Controller
 *
 * @author 金牙
 */
@Slf4j
@Controller
@RequestMapping("/wechat/property*")
public class WechatPropertyController extends BaseFormController {


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

	/**
	 * 初始化数据
	 *
	 * @param model
	 */
	private void init(Model model, HttpServletRequest request) {

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

		model.addAttribute("positionStatusGroup", this.getElementGroupManager().getElementsByGroupName("identityGroup"));
		model.addAttribute("openId", openId);
		model.addAttribute("developId", developId);
	}

	/**
	 * 跳转自助申请项目入口
	 *
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "organizationView")
	public ModelAndView organizationView(HttpServletRequest request) throws Exception {
		Model model = new ExtendedModelMap();
		try {
			String userId = request.getParameter("userId");
			init(model, request);
			model.addAttribute("userId", userId);
			return new ModelAndView("wechat/newbuilding", model.asMap());
		} catch (Exception e) {
			LogExceptionStackUtil.LogExceptionStack(e);
		}
		return null;
	}


	/**
	 * 跳转自助申请企业入口
	 *
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "virtualarchitectureView")
	public ModelAndView virtualarchitectureView(HttpServletRequest request) throws Exception {
		Model model = new ExtendedModelMap();
		try {
			String         organizationId = request.getParameter("organizationId");
			TbOrganization tbOrganization = propertyManager.getTbOrganizationById(Integer.parseInt(organizationId));
			String         userId         = request.getParameter("userId");
			model.addAttribute("userId", userId);
			model.addAttribute("tbOrganization", tbOrganization);
			init(model, request);
			return new ModelAndView("wechat/newcompany", model.asMap());
		} catch (Exception e) {
			LogExceptionStackUtil.LogExceptionStack(e);
		}
		return null;
	}


	/**
	 * 根据经纬度获取距离最近的省份和城市
	 *
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getProvidedServiceList", method = {RequestMethod.GET, RequestMethod.POST}, produces = "application/json;charset=UTF-8")
	public @ResponseBody
	String getProvidedServiceList(HttpServletRequest request) throws Exception {
		try {
			String                    province = request.getParameter("province");//省份
			List<Map<String, Object>> listMap  = propertyManager.getProvincecityList(province);
			return JSONArray.fromObject(listMap).toString();
		} catch (Exception e) {
			LogExceptionStackUtil.LogExceptionStack(e);
		}
		return null;
	}

	/**
	 * 根据经纬度获取距离最近的省份和城市
	 *
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getCityServiceList", method = {RequestMethod.GET, RequestMethod.POST}, produces = "application/json;charset=UTF-8")
	public @ResponseBody
	String getCityServiceList(HttpServletRequest request) throws Exception {
		try {
			String              province = request.getParameter("province");//省份
			String              city     = request.getParameter("city");//城市
			Map<String, Object> listMap  = propertyManager.getCityList(city, province);
			String              str      = JSONObject.fromObject(listMap).toString();
			return str;
		} catch (Exception e) {
			LogExceptionStackUtil.LogExceptionStack(e);
		}
		return null;
	}

	/**
	 * 保存或修改项目或企业信息
	 *
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/saveOrganization", method = {RequestMethod.GET, RequestMethod.POST})
	public @ResponseBody
	String saveOrganization(HttpServletRequest request) throws Exception {
		log.info("-saveOrganization begin-");

		String organizationName  = request.getParameter("organizationName");//项目名称
		String address           = request.getParameter("address");//地址
		String provinceName      = request.getParameter("provinceName");//省份名称
		String cityName          = request.getParameter("cityName");//城市名称
		String applicantName     = request.getParameter("applicantName");//申请人名称
		String applicantPhone    = request.getParameter("applicantPhone");//申请人电话
		String applicantIdentity = request.getParameter("applicantIdentity");//职位名称
		String openId            = request.getParameter("openId");//openID
		try {
			log.info("organizationName=" + organizationName);
			log.info("address=" + address);
			log.info("provinceName=" + provinceName);
			log.info("cityName=" + cityName);
			log.info("applicantName=" + applicantName);
			log.info("applicantPhone=" + applicantPhone);
			log.info("applicantIdentity=" + applicantIdentity);
			log.info("openId=" + openId);
			TbOrganization tbOrganization = new TbOrganization();
			tbOrganization.setAddress(address);
			tbOrganization.setName(organizationName);
			tbOrganization.setProvinceName(provinceName);
			tbOrganization.setCityName(cityName);
			tbOrganization.setApplicantName(applicantName);
			tbOrganization.setApplicantPhone(applicantPhone);
			tbOrganization.setApplicantIdentity(applicantIdentity);
			tbOrganization.setWxUserId(scWxuserManager.getScWxuserByOpenId(openId).getId());
			tbOrganization.setCityCode(propertyManager.findProvinceCityByName(cityName, "2").getCityCode());
			tbOrganization.setProvinceCode(propertyManager.findProvinceCityByName(provinceName, "1").getProvinceCode());
			return propertyManager.saveOrganizationFromApply(tbOrganization);
		} catch (Exception e) {
			log.info("saveOrganization occur error." + LogExceptionStackUtil.LogExceptionStack(e));
		}
		return "error";
	}

	/**
	 * 保存或修改项企业信息
	 *
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/saveVirtualArchitecture", method = {RequestMethod.GET, RequestMethod.POST})
	public @ResponseBody
	String saveVirtualArchitecture(HttpServletRequest request) throws Exception {
		String organizationId          = request.getParameter("organizationId");//项目ID
		String address                 = request.getParameter("address");//地址
		String virtualArchitectureName = request.getParameter("virtualArchitectureName");//企业名称
		String employees               = request.getParameter("employees");//企业人数
		String contacts                = request.getParameter("contacts");//申请人名称
		String phone                   = request.getParameter("phone");//申请人电话
		String openId                  = request.getParameter("openId");//openID
		try {
			log.info("organizationId=" + organizationId);
			log.info("address=" + address);
			log.info("virtualArchitectureName=" + virtualArchitectureName);
			log.info("employees=" + employees);
			log.info("contacts=" + contacts);
			log.info("phone=" + phone);
			log.info("openId=" + openId);
			TbOrganization      tbOrganization      = propertyManager.getTbOrganizationById(Integer.parseInt(organizationId));
			VirtualArchitecture virtualArchitecture = new VirtualArchitecture();
			virtualArchitecture.setOrganizationId(Integer.parseInt(organizationId));
			virtualArchitecture.setAddress(tbOrganization.getAddress() + address);
			virtualArchitecture.setName(virtualArchitectureName);
			virtualArchitecture.setContacts(contacts);
			virtualArchitecture.setEmployees(Integer.parseInt(employees));
			virtualArchitecture.setPhone(phone);
			virtualArchitecture.setWxUserId(scWxuserManager.getScWxuserByOpenId(openId).getId());
			return propertyManager.saveVirtualFromApply(virtualArchitecture);
		} catch (Exception e) {
			LogExceptionStackUtil.LogExceptionStack(e);
		}
		return "error";
	}


}
