package com.palmnest.webapp.controller.wechat;

import com.alibaba.fastjson.JSONObject;
import com.palmnest.application.ElementConstant;
import com.palmnest.application.core.util.*;
import com.palmnest.model.Staff;
import com.palmnest.model.estateserve.DepositItemInformationForm;
import com.palmnest.model.estateserve.SignContract;
import com.palmnest.model.estateserve.SignServiceInvoice;
import com.palmnest.model.property.TbBuild;
import com.palmnest.model.property.VirtualArchitecture;
import com.palmnest.service.estateserve.*;
import com.palmnest.service.property.PropertyManager;
import com.palmnest.service.wechat.SignContractManager;
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.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

/**
 * 签约控制类
 *
 * @author xiemin
 * @created
 */
@Slf4j
@Controller
@RequestMapping("/wechat/signContract*")
public class SignContractController extends BaseFormController {


	@Autowired
	private AppUserManager                    appUserManager;
	@Autowired
	private StaffManager                      staffManager;
	@Autowired
	private SignContractManager               signContractManager;
	@Autowired
	private PropertyManager                   propertyManager;
	@Autowired
	private ServicePackManager                servicePackManager;
	@Autowired
	private DepositItemInformationFormManager depositItemInformationFormManager;
	@Autowired
	private ContractIinformationOrderManager  contractIinformationOrderManager;

	/**
	 * 跳转签约企业信息页面
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/signEnterprise")
	public String signEnterprise(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		String userId = request.getParameter("userId");
		modelMap.put("userId", userId);
		return "wechat/signEnterprise";
	}

	/**
	 * 查询大使签约权限--企业
	 *
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/queryEnterpriseList")
	public String queryEnterpriseList(HttpServletRequest request, HttpServletResponse response) {
		JSONObject                json        = new JSONObject();
		List<VirtualArchitecture> list        = new ArrayList<VirtualArchitecture>();
		String                    userId      = request.getParameter("userId");
		String                    virtualName = request.getParameter("virtualName");
		if (StringUtils.isNotEmpty(userId)) {
			Staff staff          = staffManager.get(Integer.parseInt(userId));
			int   organizationId = staff.getDeptId();

			list = signContractManager.findVirtualArchitectureList(virtualName, organizationId, ElementConstant.USER_ORG_COMPANY);
//    		staffManager.getStaffRelationBuildTree(Integer.valueOf(userId),staff.getDeptId());
		}
		json.put("list", list);
		StringUtil.charSet(json, request, response);
		return null;
	}

	/**
	 * 查询大厦
	 *
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/queryOrganizationList")
	public String queryOrganizationList(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		JSONObject    json             = new JSONObject();
		List<TbBuild> organizationList = new ArrayList<TbBuild>();
		String        userId           = request.getParameter("userId");
//    	if(StringUtils.isNotEmpty(userId)){
//    		appUser=appUserManager.get(Integer.parseInt(userId));
//    	}
		if (StringUtils.isNotEmpty(userId)) {
//    		tb = propertyManager.getTbOrganizationById(appUser.getOrganizationId());
			organizationList = signContractManager.findTbOrganizationById(userId);
		}
		log.info("=====organizationList====:" + organizationList);
		json.put("list", organizationList);
		StringUtil.charSet(json, request, response);
		return null;
	}

	/**
	 * 根据服务项id查询押金信息
	 *
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/queryDepositItemList")
	public String queryDepositItemList(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		JSONObject                       json              = new JSONObject();
		String                           serviceTypeInfoId = request.getParameter("serviceId");
		List<DepositItemInformationForm> list              = null;
		try {
			if (StringUtils.isNotEmpty(serviceTypeInfoId)) {
				list = depositItemInformationFormManager.getDepositItemInformationFormList(Integer.parseInt(serviceTypeInfoId));
			}
		} catch (Exception e) {
			log.error(LogExceptionStackUtil.LogExceptionStack(e));
		}
		json.put("list", list);
		StringUtil.charSet(json, request, response);
		return null;
	}

	/**
	 * 根据服务项id查询商品分类信息
	 *
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/queryCategoriesList")
	public String queryCategoriesList(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		JSONObject                json              = new JSONObject();
		String                    serviceTypeInfoId = request.getParameter("serviceId");
		List<Map<String, String>> list              = null;
		try {
			if (StringUtils.isNotEmpty(serviceTypeInfoId)) {
				list = servicePackManager.queryCategoriesList(serviceTypeInfoId);
			}
		} catch (Exception e) {
			log.error(LogExceptionStackUtil.LogExceptionStack(e));
		}
		json.put("list", list);
		StringUtil.charSet(json, request, response);
		return null;
	}

	/**
	 * 根据分类id查询商品信息
	 *
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/queryServicePackDetailsList")
	public String queryServicePackDetailsList(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		JSONObject                json         = new JSONObject();
		String                    categoriesId = request.getParameter("categoriesId");
		List<Map<String, String>> list         = null;
		try {
			if (StringUtils.isNotEmpty(categoriesId)) {
				list = servicePackManager.queryServicePackDetailsList(categoriesId);
			}
		} catch (Exception e) {
			log.error(LogExceptionStackUtil.LogExceptionStack(e));
		}
		json.put("list", list);
		StringUtil.charSet(json, request, response);
		return null;
	}

	/**
	 * 根据分类id查询商品信息
	 *
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/queryServiceDetailsList")
	public String queryServiceDetailsList(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		JSONObject                json      = new JSONObject();
		String                    dataIdarr = request.getParameter("dataIdarr");
		List<Map<String, String>> list      = null;
		try {
			if (null != dataIdarr) {
				list = servicePackManager.queryServiceDetailsList(dataIdarr);
			}
		} catch (Exception e) {
			log.error(LogExceptionStackUtil.LogExceptionStack(e));
		}
		json.put("list", list);
		StringUtil.charSet(json, request, response);
		return null;
	}

	/**
	 * 查询服务项
	 *
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/queryServiceList")
	public String queryServiceList(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		JSONObject          json                  = new JSONObject();
		Map<String, String> map                   = new HashMap<String, String>();
		String              userId                = request.getParameter("userId");
		String              virtualArchitectureId = request.getParameter("virtualArchitectureId");
		map.put("userId", userId);
		map.put("virtualArchitectureId", virtualArchitectureId);
		map.put("pageNo", "1");
		map.put("status", "1");
		map.put("pageSize", "1000000");
		map.put("isRenewalContract", ElementConstant.YESORNO_0);
		map.put("userId", "");//不根据用户id查询企业签约服务项目

		Pages<Map<String, String>> page      = signContractManager.findContract(map);
		String                     serviceId = "";
		if (null != page.getPageList() && page.getPageList().size() > 0) {
			for (int i = 0; i < page.getPageList().size(); i++) {
				serviceId += String.valueOf(page.getPageList().get(i).get("serviceTypeInfoId")) + ",";
			}
		}
		if (StringUtils.isNotEmpty(serviceId)) {
			serviceId = serviceId.subSequence(0, serviceId.length() - 1).toString();
		}

		List<Map<String, String>> list = servicePackManager.queryServiceList(serviceId);

		json.put("list", list);
		StringUtil.charSet(json, request, response);
		return null;
	}

	/**
	 * 查询服务项详情
	 *
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/queryServiceDetails")
	public String queryServiceDetails(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		JSONObject          json      = new JSONObject();
		String              serviceId = request.getParameter("serviceId");
		Map<String, String> map       = servicePackManager.queryServiceDetails(serviceId);
		json.put("map", map);
		StringUtil.charSet(json, request, response);
		return null;
	}

	/**
	 * 添加企业信息
	 *
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/addVirtualArchitecture")
	public String addVirtualArchitecture(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		JSONObject          json           = new JSONObject();
		String              name           = request.getParameter("name");
		String              employees      = request.getParameter("employees");
		String              contacts       = request.getParameter("contacts");
		String              phone          = request.getParameter("phone");
		String              organizationId = request.getParameter("organizationId");
		String              address        = request.getParameter("address");
		Map<String, String> map            = new HashMap<String, String>();
		map.put("name", name);
		map.put("employees", employees);
		map.put("contacts", contacts);
		map.put("phone", phone);
		map.put("organizationId", organizationId);
		map.put("address", address);
		map.put("isEnabled", ElementConstant.YESORNO_1);
		try {
			json = signContractManager.addVirtualArchitecture(map);
		} catch (Exception e) {
			log.error(LogExceptionStackUtil.LogExceptionStack(e));
		}
		StringUtil.charSet(json, request, response);
		return null;
	}

	/**
	 * 跳转到签约详情
	 *
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/toOrderSeviceselectitem")
	public String toOrderSeviceselectitem(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws IOException {
		String renewalContractId = request.getParameter("renewalContractId");//续约专用id(签约id)
		if (StringUtils.isNotEmpty(renewalContractId)) {//根据续约id查询
			SignContract sc = signContractManager.findSignContract(renewalContractId);
			if (null != sc) {
				PrintWriter out = response.getWriter();
				response.setContentType("text/html;charset=UTF-8");
				String referer = request.getHeader("referer");
				if (sc.getStatus() == 0 || sc.getStatus() == 1) {
					out.print("<script>alert('该服务未到期，返回');window.location.href='" + referer + "';</script>");
					return null;
				}
			}
			String dataIdarr = "";//意向商品id
			//根据签约id查询签约商品列表
			List<Map<String, Object>> signBasicsList = signContractManager.findProductSign(renewalContractId);
			if (null != signBasicsList && signBasicsList.size() > 0) {
				for (int i = 0; i < signBasicsList.size(); i++) {
					dataIdarr += signBasicsList.get(i).get("id") + ",";
				}

			}

			String deposititem   = request.getParameter("deposititem");//押金项数量
			String deposititemId = request.getParameter("deposititemId");//押金项id
			//根据签约id查询签约押金列表
			List<Map<String, Object>> sroductSignList = signContractManager.findSignBasics(renewalContractId);
			if (null != sroductSignList && sroductSignList.size() > 0) {
				for (int i = 0; i < sroductSignList.size() - 1; i++) {
					deposititem += sroductSignList.get(i).get("number") + ",";
					deposititemId += sroductSignList.get(i).get("basicsId") + ",";
				}
			}

			Map<String, String> map = new HashMap<String, String>();
			if (null != sc) {
				//查询服务项信息
				map = servicePackManager.queryServiceDetails(sc.getServiceTypeInfoId() + "");
				modelMap.put("serviceId", sc.getServiceTypeInfoId());
				modelMap.put("code", sc.getServiceTypeInCode());
				modelMap.put("startTime", sc.getStartTime());
				modelMap.put("endTime", sc.getEndTime());
				modelMap.put("dataIdarr", dataIdarr);
				modelMap.put("organizationId", sc.getOrganizationId());
				modelMap.put("remarks", sc.getRemarks());
				modelMap.put("deposititem", deposititem);
				modelMap.put("deposititemId", deposititemId);
				modelMap.put("paymentType", sc.getPaymentMethod());
				modelMap.put("userId", sc.getStaffId());
				modelMap.put("signContractId", renewalContractId);
				modelMap.put("typeCode", sc.getServiceTypeInCode());
				modelMap.put("virtualArchitectureId", sc.getVirtualArchitectureId());

			}

			if (null != map) {
				modelMap.put("name", map.get("name"));
				modelMap.put("serviceIcon", map.get("serviceIcon"));
			}
			modelMap.put("visitPrefix", FileUploadUtils.getVisitPrefix());
			modelMap.put("renewalContractId", renewalContractId);
		} else {//用于返回的时候选择的参数
			String startTime = request.getParameter("startTime");
			String endTime   = request.getParameter("endTime");
			if (StringUtils.isEmpty(startTime)) {
				startTime = DateUtils.getDate(new Date(), "");
			}
			if (StringUtils.isEmpty(endTime)) {
				endTime = DateUtils.endDateYMD(new Date(), 1);
			}
			String serviceId             = request.getParameter("serviceId");
			String dataIdarr             = request.getParameter("dataIdarr");//意向商品id
			String deposititem           = request.getParameter("deposititem");//押金项数量
			String deposititemId         = request.getParameter("deposititemId");//押金项id
			String code                  = request.getParameter("code");
			String paymentType           = request.getParameter("paymentType");
			String organizationId        = request.getParameter("organizationId");
			String virtualArchitectureId = request.getParameter("virtualArchitectureId");
			String remarks               = request.getParameter("remarks");
			String serviceIcon           = request.getParameter("serviceIcon");
			String typeCode              = request.getParameter("typeCode");
			String name                  = request.getParameter("name");
			name = StringUtil.getUTF_8(name);
			remarks = StringUtil.getUTF_8(remarks);
			String userId         = request.getParameter("userId");
			String signContractId = request.getParameter("signContractId");
			modelMap.put("serviceId", serviceId);
			modelMap.put("code", code);
			modelMap.put("startTime", startTime);
			modelMap.put("endTime", endTime);
			modelMap.put("dataIdarr", dataIdarr);
			modelMap.put("organizationId", organizationId);
			modelMap.put("remarks", remarks);
			modelMap.put("deposititem", deposititem);
			modelMap.put("deposititemId", deposititemId);
			modelMap.put("paymentType", paymentType);
			modelMap.put("serviceIcon", serviceIcon);
			modelMap.put("name", name);
			modelMap.put("userId", userId);
			modelMap.put("signContractId", signContractId);
			modelMap.put("typeCode", typeCode);
			modelMap.put("virtualArchitectureId", virtualArchitectureId);
			modelMap.put("visitPrefix", FileUploadUtils.getVisitPrefix());
		}

		return "wechat/orderSeviceselectitem";
	}

	/**
	 * 添加签约信息
	 *
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/addSignContract")
	public String addSignContract(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		JSONObject          json                  = new JSONObject();
		String              startTime             = request.getParameter("startTime");
		String              endTime               = request.getParameter("endTime");
		String              serviceId             = request.getParameter("serviceId");
		String              dataIdarr             = request.getParameter("dataIdarr");//意向商品id
		String              deposititem           = request.getParameter("deposititem");//押金项数量
		String              deposititemId         = request.getParameter("deposititemId");//押金项id
		String              code                  = request.getParameter("code");
		String              paymentType           = request.getParameter("paymentType");
		String              organizationId        = request.getParameter("organizationId");
		String              virtualArchitectureId = request.getParameter("virtualArchitectureId");
		String              remarks               = request.getParameter("remarks");
		String              staffId               = request.getParameter("staffId");
		String              signContractId        = request.getParameter("signContractId");
		String              renewalContractId     = request.getParameter("renewalContractId");//续约专用id(签约id)
		Map<String, String> map                   = new HashMap<String, String>();
		map.put("serviceId", serviceId);
		map.put("code", code);
		map.put("remarks", remarks);
		map.put("paymentType", paymentType);
		map.put("serviceId", serviceId);
		map.put("dataIdarr", dataIdarr);
		map.put("deposititemId", deposititemId);
		map.put("deposititem", deposititem);
		map.put("startTime", startTime);
		map.put("endTime", endTime);
		map.put("organizationId", organizationId);
		map.put("virtualArchitectureId", virtualArchitectureId);
		map.put("staffId", staffId);
		map.put("signContractId", signContractId);
		map.put("renewalContractId", renewalContractId);
		map.put("status", ElementConstant.YESORNO_1);//签名之后更新状态
		map.put("signUserType", ElementConstant.YESORNO_1);//0 前台用户，1 后台管理员
		String userName = "";
		try {
			if (StringUtils.isNotEmpty(staffId)) {
// 				AppUser user = appUserManager.get(Integer.valueOf(staffId));
				Staff staff = staffManager.get(Integer.parseInt(staffId));
				userName = staff.getName();
			}
			map.put("userName", userName);
			json = signContractManager.addSignContract(map);

		} catch (Exception e) {
			log.info(LogExceptionStackUtil.LogExceptionStack(e));
			json.put("code", -1);
			json.put("msg", "签约添加失败");
		}
		StringUtil.charSet(json, request, response);
		return null;
	}

	/**
	 * 确认签约信息
	 *
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/ordersevice")
	public String ordersevice(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		String startTime             = request.getParameter("startTime");
		String endTime               = request.getParameter("endTime");
		String serviceId             = request.getParameter("serviceId");
		String dataIdarr             = request.getParameter("dataIdarr");//意向商品id
		String deposititem           = request.getParameter("deposititem");//押金项数量
		String deposititemId         = request.getParameter("deposititemId");//押金项id
		String code                  = request.getParameter("code");
		String paymentType           = request.getParameter("paymentType");
		String organizationId        = request.getParameter("organizationId");
		String virtualArchitectureId = request.getParameter("virtualArchitectureId");
		String remarks               = request.getParameter("remarks");
		String serviceIcon           = request.getParameter("serviceIcon");
		String typeCode              = request.getParameter("typeCode");
		String name                  = request.getParameter("name");
		name = StringUtil.getUTF_8(name);
		remarks = StringUtil.getUTF_8(remarks);
		String userId            = request.getParameter("userId");
		String signContractId    = request.getParameter("signContractId");
		String renewalContractId = request.getParameter("renewalContractId");
		modelMap.put("serviceId", serviceId);
		modelMap.put("code", code);
		modelMap.put("startTime", startTime);
		modelMap.put("endTime", endTime);
		modelMap.put("dataIdarr", dataIdarr);
		modelMap.put("organizationId", organizationId);
		modelMap.put("remarks", remarks);
		modelMap.put("deposititem", deposititem);
		modelMap.put("deposititemId", deposititemId);
		modelMap.put("paymentType", paymentType);
		modelMap.put("serviceIcon", serviceIcon);
		modelMap.put("name", name);
		modelMap.put("userId", userId);
		modelMap.put("signContractId", signContractId);
		modelMap.put("typeCode", typeCode);
		modelMap.put("renewalContractId", renewalContractId);
		modelMap.put("virtualArchitectureId", virtualArchitectureId);
		modelMap.put("visitPrefix", FileUploadUtils.getVisitPrefix());
		return "wechat/ordersevice";
	}

	/**
	 * 跳转到签约协议
	 *
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/toSigningconfirmed")
	public String toSigningconfirmed(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		String              signContractId        = request.getParameter("signContractId");
		String              serviceId             = request.getParameter("serviceId");
		String              virtualArchitectureId = request.getParameter("virtualArchitectureId");
		String              userId                = request.getParameter("userId");
		String              organizationId        = request.getParameter("organizationId");
		String              renewalContractId     = request.getParameter("renewalContractId");//续约专用
		VirtualArchitecture va                    = new VirtualArchitecture();
		if (StringUtils.isNotEmpty(virtualArchitectureId)) {
			va = propertyManager.getVirtualArchitectureById(Integer.valueOf(virtualArchitectureId));
		}

		//查询签约详情
		if (StringUtils.isNotEmpty(renewalContractId)) {
			SignContract sc = signContractManager.findSignContract(renewalContractId);
			if (null != sc) {
				serviceId = sc.getServiceTypeInfoId() + "";
			}
		}
		Map<String, String> map = new HashMap<String, String>();
		if (StringUtils.isNotEmpty(serviceId)) {
			map = servicePackManager.queryServiceDetails(serviceId);
		}
		modelMap.put("map", map);
		modelMap.put("va", va);
		modelMap.put("renewalContractId", renewalContractId);
		modelMap.put("signContractId", signContractId);
		modelMap.put("organizationId", organizationId);
		modelMap.put("userId", userId);
		return "wechat/signingconfirmed";
	}

	/**
	 * 添加签名信息
	 *
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/addSigningconfirmed")
	public String addSigningconfirmed(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		JSONObject json              = new JSONObject();
		String     imageUrl          = request.getParameter("imageUrl");
		String     signContractId    = request.getParameter("signContractId");
		String     renewalContractId = request.getParameter("renewalContractId");
		try {
			if (StringUtils.isNotEmpty(imageUrl)) {
				imageUrl = imageUrl.substring(imageUrl.indexOf(",") + 1, imageUrl.length());
			}
			imageUrl = FileUploadUtils.getBase64ImageUrl(imageUrl);
			json = signContractManager.updateSignContract(signContractId, imageUrl, renewalContractId);
			if (StringUtils.isEmpty(renewalContractId)) {
				SignContract sc = signContractManager.findSignContract(signContractId);
				contractIinformationOrderManager.assignContractIinformation(sc.getVirtualArchitectureId(), sc.getServiceTypeInCode());
			}
			if (StringUtils.isNotEmpty(renewalContractId) && StringUtils.isNotEmpty(signContractId) && !renewalContractId.equals(signContractId)) {
				SignContract sc = signContractManager.findSignContract(renewalContractId);
				contractIinformationOrderManager.assignContractIinformation(sc.getVirtualArchitectureId(), sc.getServiceTypeInCode());
			}
		} catch (Exception e) {
			log.error(LogExceptionStackUtil.LogExceptionStack(e));
		}
		StringUtil.charSet(json, request, response);
		return null;
	}

	/**
	 * 添加发票信息
	 *
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/addSignServiceInvoice")
	public String addSignServiceInvoice(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		JSONObject          json                  = new JSONObject();
		String              signServiceInvoiceId  = request.getParameter("signServiceInvoiceId");
		String              organizationId        = request.getParameter("organizationId");
		String              virtualArchitectureId = request.getParameter("virtualArchitectureId");
		String              type                  = request.getParameter("type");
		String              raise                 = request.getParameter("raise");
		String              address               = request.getParameter("address");
		String              identificationNumber  = request.getParameter("identificationNumber");
		String              mobile                = request.getParameter("mobile");
		String              bank                  = request.getParameter("bank");
		String              bankCard              = request.getParameter("bankCard");
		String              userId                = request.getParameter("userId");
		String              userName              = "";
		Map<String, String> map                   = new HashMap<String, String>();
		map.put("signServiceInvoiceId", signServiceInvoiceId);
		map.put("userName", userName);
		map.put("organizationId", organizationId);
		map.put("virtualArchitectureId", virtualArchitectureId);
		map.put("type", type);
		map.put("raise", raise);
		map.put("address", address);
		map.put("identificationNumber", identificationNumber);
		map.put("mobile", mobile);
		map.put("bank", bank);
		map.put("bankCard", bankCard);
		try {
			if (StringUtils.isNotEmpty(userId)) {
// 				AppUser user = appUserManager.get(Integer.valueOf(userId));
				Staff staff = staffManager.get(Integer.parseInt(userId));
				userName = staff.getName();
			}

			json = signContractManager.addSignServiceInvoice(map);
		} catch (Exception e) {
			log.error(LogExceptionStackUtil.LogExceptionStack(e));
		}
		StringUtil.charSet(json, request, response);
		return null;
	}

	/**
	 * 我的签约页面
	 *
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/mycontract")
	public String mycontract(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		String userId                = request.getParameter("userId");
		String pageNo                = request.getParameter("pageNo");
		String organizationId        = request.getParameter("organizationId");//没有签约的时候，跳转签约的时候用到
		String virtualArchitectureId = request.getParameter("virtualArchitectureId");//没有签约的时候，跳转签约的时候用到
		if (StringUtils.isEmpty(pageNo)) {
			pageNo = "1";
		}
		//根据用户id查询项目id
		List<TbBuild> tbBuildList = new ArrayList<TbBuild>();
		if (StringUtils.isNotEmpty(userId) && StringUtils.isEmpty(organizationId)) {
			tbBuildList = signContractManager.findTbOrganizationById(userId);
		}

		if (null != tbBuildList && tbBuildList.size() > 0) {
			organizationId = tbBuildList.get(0).getOrganizationId() + "";
		}
		List<VirtualArchitecture> virtualArchitectureList = new ArrayList<VirtualArchitecture>();
		//根据项目id查询企业id
		if (StringUtils.isEmpty(virtualArchitectureId) && StringUtils.isNotEmpty(organizationId)) {
			virtualArchitectureList = propertyManager.getVirtualArchitectureList(Integer.parseInt(organizationId), "1");
		}

		if (null != virtualArchitectureList && virtualArchitectureList.size() > 0) {
			virtualArchitectureId = virtualArchitectureList.get(0).getId() + "";
		}
		modelMap.put("userId", userId);
		modelMap.put("pageNo", pageNo);
		modelMap.put("organizationId", organizationId);
		modelMap.put("virtualArchitectureId", virtualArchitectureId);
		modelMap.put("visitPrefix", FileUploadUtils.getVisitPrefix());
		return "wechat/mycontract";
	}

	/**
	 * 我的签约--续约页面
	 *
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/serviceQueryResults")
	public String serviceQueryResults(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		String userId                = request.getParameter("userId");
		String pageNo                = request.getParameter("pageNo");
		String organizationId        = request.getParameter("organizationId");//没有签约的时候，跳转签约的时候用到
		String virtualArchitectureId = request.getParameter("virtualArchitectureId");//没有签约的时候，跳转签约的时候用到
		if (StringUtils.isEmpty(pageNo)) {
			pageNo = "1";
		}
		//根据用户id查询项目id
		List<TbBuild> tbBuildList = new ArrayList<TbBuild>();
		if (StringUtils.isNotEmpty(userId) && StringUtils.isEmpty(organizationId)) {
			tbBuildList = signContractManager.findTbOrganizationById(userId);
		}

		if (null != tbBuildList && tbBuildList.size() > 0) {
			organizationId = tbBuildList.get(0).getOrganizationId() + "";
		}
		/*List<VirtualArchitecture> virtualArchitectureList = new ArrayList<VirtualArchitecture>();
		//根据项目id查询企业id
		if(StringUtils.isEmpty(virtualArchitectureId) && StringUtils.isNotEmpty(organizationId)){
			virtualArchitectureList = propertyManager.getVirtualArchitectureList(Integer.parseInt(organizationId),"1");
		}
		
		if(null != virtualArchitectureList && virtualArchitectureList.size() > 0){
			virtualArchitectureId = virtualArchitectureList.get(0).getId()+"";
		}*/
		modelMap.put("userId", userId);
		modelMap.put("pageNo", pageNo);
		modelMap.put("organizationId", organizationId);
		modelMap.put("virtualArchitectureId", virtualArchitectureId);
		modelMap.put("visitPrefix", FileUploadUtils.getVisitPrefix());
		return "wechat/serviceQueryResults";
	}

	/**
	 * 我的签约列表
	 *
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/contractList")
	public String contractList(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		String     userId = request.getParameter("userId");
		String     pageNo = request.getParameter("pageNo");
		String     status = request.getParameter("status");
		JSONObject json   = new JSONObject();
		if (StringUtils.isEmpty(pageNo)) {
			pageNo = "1";
		}
		Map<String, String> map = new HashMap<String, String>();
		map.put("userId", userId);
		map.put("pageNo", pageNo);
		map.put("status", status);
		map.put("pageSize", "10");
		try {
			Pages<Map<String, String>> page = signContractManager.findContract(map);
			json.put("page", page);
			StringUtil.charSet(json, request, response);
		} catch (Exception e) {
			log.error(LogExceptionStackUtil.LogExceptionStack(e));
		}
		modelMap.put("userId", userId);
		modelMap.put("pageNo", pageNo);
		return null;
	}

	/**
	 * 我的发票信息
	 *
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/findSignServiceInvoiceObj")
	public String findSignServiceInvoiceObj(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		JSONObject         json                  = new JSONObject();
		String             type                  = request.getParameter("type");
		String             virtualArchitectureId = request.getParameter("virtualArchitectureId");
		SignServiceInvoice ssi                   = new SignServiceInvoice();
		try {
			if (StringUtils.isNotEmpty(virtualArchitectureId) && StringUtils.isNotEmpty(type)) {
				ssi = signContractManager.getSignServiceInvoiceObj(Integer.valueOf(virtualArchitectureId), type);
			}
			json.put("ssi", ssi);
			StringUtil.charSet(json, request, response);
		} catch (Exception e) {
			log.error(LogExceptionStackUtil.LogExceptionStack(e));
		}
		return null;
	}

	/**
	 * 企业签约查询
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/enterpriseContractInquiry")
	public ModelAndView enterpriseContractInquiry(HttpServletRequest request,
												  HttpServletResponse response) throws Exception {
		Model model = new ExtendedModelMap();
		try {
			String userId = request.getParameter("userId");//物业员工唯一ID
			model.addAttribute("userId", userId);
			String paramValue = request.getParameter("paramValue");//查询参数
			if (StringUtils.isBlank(paramValue)) {
				List<Map<String, Object>> staffByOrganztionList = signContractManager.
						getStaffByOrganztion(Integer.parseInt(userId));//获取物业员工管辖的项目列表
				for (int i = 0; i < staffByOrganztionList.size(); i++) {
					Map<String, Object> map            = staffByOrganztionList.get(i);
					String              organizationId = staffByOrganztionList.get(i).get("organizationId").toString();
					map.put("virtualArchitectureList", propertyManager.getVirtualArchitectureList(Integer.parseInt(organizationId), "1"));
					staffByOrganztionList.set(i, map);
				}
				model.addAttribute("staffByOrganztionList", staffByOrganztionList);
			} else {
				String orgIds = "";
				List<Map<String, Object>> staffByOrganztionList = signContractManager.
						getStaffByOrganztion(Integer.parseInt(userId));//获取物业员工管辖的项目列表
				for (int i = 0; i < staffByOrganztionList.size(); i++) {
					String organizationId = staffByOrganztionList.get(i).get("organizationId").toString();
					orgIds += organizationId + ",";
				}
				if (StringUtils.isNotBlank(orgIds)) {
					orgIds = orgIds.substring(0, orgIds.length() - 1);
				}
				staffByOrganztionList = propertyManager.getVirtualarchitectureList(paramValue, orgIds);
				for (int i = 0; i < staffByOrganztionList.size(); i++) {
					Map<String, Object> map            = staffByOrganztionList.get(i);
					String              organizationId = staffByOrganztionList.get(i).get("organizationId").toString();
					map.put("virtualArchitectureList", propertyManager.getVirtualArchitectureList(Integer.parseInt(organizationId), "1", paramValue));
					staffByOrganztionList.set(i, map);
				}
				model.addAttribute("staffByOrganztionList", staffByOrganztionList);
				model.addAttribute("paramValue", paramValue);
			}
			model.addAttribute("paramValue", paramValue);
			return new ModelAndView("wechat/selectcompany", model.asMap());

		} catch (Exception e) {
			log.error(LogExceptionStackUtil.LogExceptionStack(e));
		}
		return null;
	}

	/**
	 * 查询单个企业的基本信息和签约信息发票信息
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getVirtualArchitectureBySignContract")
	public ModelAndView getVirtualArchitectureBySignContract(HttpServletRequest request,
															 HttpServletResponse response) throws Exception {
		Model model = new ExtendedModelMap();
		try {
			String              virtualArchitectureId = request.getParameter("virtualArchitectureId");
			String              userId                = request.getParameter("userId");
			VirtualArchitecture virtualArchitecture   = propertyManager.getVirtualArchitectureById(Integer.parseInt(virtualArchitectureId));
			model.addAttribute("virtualArchitecture", virtualArchitecture);//企业信息
			List<SignServiceInvoice> signServiceInvoiceList = signContractManager.
					getSignServiceInvoiceList(Integer.parseInt(virtualArchitectureId));//发票信息
			model.addAttribute("signServiceInvoiceList", signServiceInvoiceList);//企业信息
			List<Map<String, Object>> signContractList = signContractManager.
					getSignContractList(Integer.parseInt(virtualArchitectureId));//获取签约服务信息
			if (signContractList != null) {
				for (int i = 0; i < signContractList.size(); i++) {
					Map<String, Object> map = signContractList.get(i);
					Integer             id  = Integer.parseInt(map.get("ID").toString());
					map.put("intentionalGoodsLisy", signContractManager.getIntentionalGoods(id));//意向商品集合
					map.put("signBasicsList", signContractManager.getSignBasicsList(map.get("ID").toString()));//押金项集合
					signContractList.set(i, map);
				}
			}
			model.addAttribute("signContractList", signContractList);
			model.addAttribute("visitPrefix", FileUploadUtils.getVisitPrefix());
			model.addAttribute("userId", userId);
			return new ModelAndView("wechat/individualenterprise", model.asMap());
		} catch (Exception e) {
			log.error(LogExceptionStackUtil.LogExceptionStack(e));
		}
		return null;
	}

	/**
	 * 根据id查询签约商品列表
	 *
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/findProductSign")
	public String findProductSign(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		JSONObject                json           = new JSONObject();
		String                    signContractId = request.getParameter("signContractId");
		List<Map<String, Object>> list           = null;
		try {
			if (null != signContractId) {
				list = signContractManager.findProductSign(signContractId);
			}
		} catch (Exception e) {
			log.error(LogExceptionStackUtil.LogExceptionStack(e));
		}
		json.put("list", list);
		StringUtil.charSet(json, request, response);
		return null;
	}

	/**
	 * 根据id查询签约押金列表
	 *
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/findSignBasics")
	public String findSignBasics(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		JSONObject                json           = new JSONObject();
		String                    signContractId = request.getParameter("signContractId");
		List<Map<String, Object>> list           = null;
		try {
			if (null != signContractId) {
				list = signContractManager.findSignBasics(signContractId);
			}
		} catch (Exception e) {
			log.error(LogExceptionStackUtil.LogExceptionStack(e));
		}
		json.put("list", list);
		StringUtil.charSet(json, request, response);
		return null;
	}

	@RequestMapping(value = "/signServiceInvoiceView")
	public ModelAndView signServiceInvoiceView(HttpServletRequest request, HttpServletResponse response) {
		Model model = new ExtendedModelMap();
		try {
			String userId                = request.getParameter("userId");
			String organizationId        = request.getParameter("organizationId");
			String virtualArchitectureId = request.getParameter("virtualArchitectureId");
			String type                  = request.getParameter("type");
			model.addAttribute("organizationId", organizationId);
			model.addAttribute("userId", userId);
			model.addAttribute("virtualArchitectureId", virtualArchitectureId);
			model.addAttribute("sianType", "1");
			model.addAttribute("type", type);
			return new ModelAndView("/wechat/signServiceInvoice", model.asMap());
		} catch (Exception e) {
			log.error(LogExceptionStackUtil.LogExceptionStack(e));
		}
		return null;
	}

}
