package com.iamberry.wechat.handles.cart;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import com.iamberry.wechat.core.entity.product.*;
import com.iamberry.wechat.face.drp.PlaceInfoService;
import com.iamberry.wechat.tools.QrCodeUtil;
import org.apache.commons.lang.StringUtils;
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.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iamberry.wechat.core.entity.ResultMsg;
import com.iamberry.wechat.core.entity.WechatUtils;
import com.iamberry.wechat.core.entity.cart.ShopSalesOrderDto;
import com.iamberry.wechat.core.entity.cart.UserGiftDto;
import com.iamberry.wechat.core.entity.drp.PlaceInfo;
import com.iamberry.wechat.core.entity.member.Member;
import com.iamberry.wechat.face.admin.SystemService;
import com.iamberry.wechat.face.cart.CartService;
import com.iamberry.wechat.face.cart.ProductInfoService;
import com.iamberry.wechat.face.member.MemberService;
import com.iamberry.wechat.face.porduct.ProductColorService;
import com.iamberry.wechat.face.porduct.ProductImageTextService;
import com.iamberry.wechat.face.porduct.ProductPictureService;
import com.iamberry.wechat.tools.NameUtils;
import com.iamberry.wechat.tools.ResultInfo;
import com.iamberry.wechat.tools.StaticInfo;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

@Controller
@RequestMapping("/wechat/product")
public class ProductInfoHandler {

	@Autowired
	private ProductInfoService productInfoService;
	@Autowired
	private ProductImageTextService productImageTextService;
	@Autowired
	private ProductPictureService productPictureService;
	@Autowired
	private MemberService memberService;
	@Autowired
	private SystemService systemService;
	@Autowired
	private CartService cartService;
	@Autowired
	private ProductColorService productColorService;
	@Autowired
	private PlaceInfoService placeInfoService;

	/**
	 * 首页进入商城
	 * @param request
	 * @return list
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/productIndex",method = RequestMethod.POST)
	public ResultMsg productIndex(HttpServletRequest request,
								  @RequestParam(value= "pageSize",defaultValue= "6" ,required=false) Integer pageSize,
								  @RequestParam(value = "pageNO", defaultValue = "1") Integer pageNO,
								  @RequestParam(value = "productName", defaultValue = "")String productName,
								  @RequestParam(value = "productType", defaultValue = "1") Integer productType,
								  @RequestParam(value = "productRentType", defaultValue = "1") Integer productRentType) throws Exception {

		ResultMsg remsg= new ResultMsg();
		Member member =  WechatUtils.getUserBySession(request);
		Product product = new Product();
		if (null != productName && !"".equals(productName)) {
			product.setProductName(productName);
		}
		product.getProductType().setTypeId(productType);
		product.setProductRentType(productRentType);
		product.getPage().setPageNumber(pageNO);
		product.getPage().setPageSize(pageSize);
		product.getPage().initRecordBegin();
		List<Product> productList = productInfoService.selectPageProduct(product);


		//根据openid查询该用户是否购买过订单
		ShopSalesOrderDto shopSalesOrderDto = new ShopSalesOrderDto();
		shopSalesOrderDto.setSalesOpenid(member.getUserOpenid());
		shopSalesOrderDto.setSalesStatus("2");

		/*//如果是有二维码的商户，进入后显示优惠价
        int sum = productInfoService.getQrCodeByPlaceOpenId(member.getUserOpenid());
        if (sum > 0) {
            QrcodeGift gift = new QrcodeGift();
            List<QrcodeGift> giftList = productInfoService.listQrcodeGift(gift);
            if (giftList != null && giftList.size() > 0 && productList != null && productList.size() > 0) {
                for (Product productInfo : productList) {
                    for (QrcodeGift qrcodeGift : giftList) {
                        if (productInfo.getColorId().intValue() == qrcodeGift.getQrcodeColorId()
                            && qrcodeGift.getQrcodePreDiscount() != null
                            && qrcodeGift.getQrcodePreDiscount() > 0) {
                            productInfo.setProductDiscount(qrcodeGift.getQrcodePreDiscount());
                        }
                    }
                }
            }
        }*/

		//判断是否为金牌会员下线，如果是，则以优惠价显示，如果不是则按原价显示
		//查询金牌会员模板id
		int templateId = systemService.selectOneShopRuleById(114).getRuleNum().intValue();
		//查询用户信息
		Member user= memberService.getMemberByUserOpenId(member.getUserOpenid());
		if (user == null) {
			remsg.setMessage(ResultInfo.loginOutError);
			remsg.setStatus(false);
			remsg.setMessage(ResultInfo.loginOutError);
			return remsg;
		}
		//如果模板id等于金牌会模板id，则该用户为金牌会员下线
		if (user.getUserDealers() != null && user.getTemplateId() != null &&
				templateId == user.getTemplateId().intValue() && user.getUserResType().intValue() == 3) {
			//若果是金牌会员下线，产品的价格就以优惠价显示
			if (productList != null && productList.size() >= 1) {
				for (Product productInfo : productList) {
					UserGiftDto userGiftDto = new UserGiftDto();
					userGiftDto.setUserLevel(2);
					userGiftDto.setUserProductId(productInfo.getProductId());
					userGiftDto.setUserColorId(productInfo.getColorId());
					//根据商品id和会员等级查询礼品信息
					userGiftDto = productInfoService.selectUserGift(userGiftDto);
					if (userGiftDto != null) {
						productInfo.setProductDiscount(userGiftDto.getUserDiscount());
					}
				}
			}
		}

		int orderNum = cartService.selectOrderCount(shopSalesOrderDto);
		//如果购买过订单，则滤芯的价格可按折扣价购买
		if (orderNum > 0) {
			if (productList != null && productList.size() >= 1) {
				for (Product productInfo : productList) {
					if (productInfo.getProductType().getTypeId().intValue() == 2) {
						UserGiftDto userGiftDto = new UserGiftDto();
						userGiftDto.setUserLevel(3);
						userGiftDto.setUserProductId(productInfo.getProductId());
						userGiftDto.setUserColorId(productInfo.getColorId());
						//根据商品id和会员等级查询礼品信息
						userGiftDto = productInfoService.selectUserGift(userGiftDto);
						if (userGiftDto != null) {
							productInfo.setProductDiscount(userGiftDto.getUserDiscount());
						}
					}
				}
			}
		}
		int num = this.productInfoService.selectProductCount(product);
		int count = num % pageSize == 0? num/pageSize:(num/pageSize)+1;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("productList", productList);
		map.put("total", count);
		remsg.setMessage(NameUtils.getConfig("SUCCESSINFO"));
		remsg.setResultCode(ResultInfo.SUCCESSCODE);
		remsg.setStatus(true);
		remsg.setData(map);
		return remsg;
	}

	/**
	 * 进入产品详细信息页面
	 * @param request
	 * @param response
	 * @param productId
	 * @return list
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/getProductByProductId")
	public ResultMsg getProductByProductId(HttpServletRequest request,HttpServletResponse response,
										   @RequestParam(value = "productId",defaultValue = "") String productId,
										   @RequestParam("colorId") Integer colorId
	) throws Exception {

		ResultMsg remsg = new ResultMsg();
		Member member =  WechatUtils.getUserBySession(request);
		Map<String, Object> map=null;
		/*
		 * 判断用户信息及其参数
		 */
		if (member == null || member.getUserOpenid() == null) {
			remsg.setMessage(ResultInfo.loginOutError);
			remsg.setStatus(false);
			remsg.setMessage(ResultInfo.loginOutError);
			return remsg;
		}
		if (productId == null) {
			remsg.setStatus(false);
			remsg.setResultCode(ResultInfo.ERRORCODE);
			remsg.setMessage(NameUtils.getConfig("ERRORINFO"));
			return remsg;
		}

		if (StaticInfo.pattern.matcher(productId).find()) {
			Product product = this.productInfoService.getProductByProductId(Integer.parseInt(productId),colorId);

			String[] machineIds = {};
			boolean codeFlag = false;
			//获取水机产品类型
			String machineId = systemService.selectOneShopRuleById(237).getRuleDesc();
			if (machineId != null) {
				String machineStr = machineId.split(":")[1];
				machineIds = machineStr.split(",");
			}
			if (machineIds != null && machineIds.length > 0) {
				for (int i = 0;i < machineIds.length;i++) {
					if (product.getProductType().getTypeId().intValue() == Integer.valueOf(machineIds[i])) {
						codeFlag = true;
					}
				}
			}
			Map<String,Object> qrCodeMap = createQrCode(productId,colorId,member,codeFlag);

            /*//如果是有二维码的商户，进入后显示优惠价
            int num = productInfoService.getQrCodeByPlaceOpenId(member.getUserOpenid());
            if (num > 0) {
                QrcodeGift gift = new QrcodeGift();
                List<QrcodeGift> giftList = productInfoService.listQrcodeGift(gift);
                if (giftList != null && giftList.size() > 0 && product != null) {
                    for (QrcodeGift qrcodeGift : giftList) {
                        if (product.getColorId().intValue() == qrcodeGift.getQrcodeColorId()
                            && qrcodeGift.getQrcodePreDiscount() != null
                            && qrcodeGift.getQrcodePreDiscount() > 0) {
                            product.setProductDiscount(qrcodeGift.getQrcodePreDiscount());
                        }
                    }
                }
            }*/

			//如果用户是扫描二维码进入，则显示优惠价
			String userQrcodeType = request.getParameter("userQrcodeType");
			if (userQrcodeType != null && !"".equals(userQrcodeType) && !"null".equals(userQrcodeType)) {
				QrcodeGift gift = new QrcodeGift();
				gift.setQrcodeStatus(1);
				List<QrcodeGift> giftList = productInfoService.listQrcodeGift(gift);
				if (giftList != null && giftList.size() > 0 && product != null) {
					for (QrcodeGift qrcodeGift : giftList) {
						if (product.getColorId().intValue() == qrcodeGift.getQrcodeColorId()) {
							switch (userQrcodeType) {
								case "1":
									if (qrcodeGift.getQrcodePreDiscount() != null && qrcodeGift.getQrcodePreDiscount() > 0) {
										product.setProductDiscount(qrcodeGift.getQrcodePreDiscount());
									}
									break;
								case "2":
									if (qrcodeGift.getQrcodeSalesDiscount() != null && qrcodeGift.getQrcodeSalesDiscount() > 0) {
										product.setProductDiscount(qrcodeGift.getQrcodeSalesDiscount());
									}
									break;
							}

						}
					}
				}
			}

			//判断是否为金牌会员下线，如果是，则以优惠价显示，如果不是则按原价显示
			//查询金牌会员模板id
			int templateId = systemService.selectOneShopRuleById(114).getRuleNum().intValue();
			//查询用户信息
			Member user= memberService.getMemberByUserOpenId(member.getUserOpenid());
			if (user == null) {
				remsg.setMessage(ResultInfo.loginOutError);
				remsg.setStatus(false);
				remsg.setMessage(ResultInfo.loginOutError);
				return remsg;
			}
			//如果模板id等于金牌会模板id，则该用户为金牌会员下线
			if (user.getUserDealers() != null && user.getTemplateId() != null
					&& templateId == user.getTemplateId().intValue() && user.getUserResType().intValue() == 3) {
				//若果是金牌会员下线，产品的价格就以优惠价显示
				UserGiftDto userGiftDto = new UserGiftDto();
				userGiftDto.setUserLevel(2);
				userGiftDto.setUserProductId(product.getProductId());
				userGiftDto.setUserColorId(product.getColorId());
				//根据商品id和会员等级查询礼品信息
				userGiftDto = productInfoService.selectUserGift(userGiftDto);
				if (userGiftDto != null) {
					product.setProductDiscount(userGiftDto.getUserDiscount());
				}

			}

			//根据openid查询该用户是否购买过订单
			ShopSalesOrderDto shopSalesOrderDto = new ShopSalesOrderDto();
			shopSalesOrderDto.setSalesOpenid(member.getUserOpenid());
			shopSalesOrderDto.setSalesStatus("2");
			//如果购买过订单，则滤芯的价格可按折扣价购买
			if (product != null) {
				if (product.getProductType().getTypeId().intValue() == 2) {
					//若果是金牌会员下线，产品的价格就以优惠价显示
					UserGiftDto userGiftDto = new UserGiftDto();
					userGiftDto.setUserLevel(3);
					userGiftDto.setUserProductId(product.getProductId());
					userGiftDto.setUserColorId(product.getColorId());
					//根据商品id和会员等级查询礼品信息
					userGiftDto = productInfoService.selectUserGift(userGiftDto);
					if (userGiftDto != null) {
						product.setProductDiscount(userGiftDto.getUserDiscount());
					}
				}
			}
			List<ProductPicture> pictureList = productPictureService.getProductPictureByproductId(Integer.parseInt(productId));
			product.setPictureList(pictureList);
			/*
			 * 组装数据
			 */
			map = new HashMap<String, Object>();

			if ((boolean)qrCodeMap.get("flag")) {
				map.put("qrCode", qrCodeMap.get("qrCode"));
				map.put("shopQrCode", qrCodeMap.get("shopQrCode"));
			}
			map.put("flag", (boolean)qrCodeMap.get("flag"));	//判断是否有商户个人二维码
			map.put("isShow", ResultInfo.ISSHOW);
			map.put("cartDesc", ResultInfo.CARTDESC);
			map.put("cart_url", ResultInfo.CART_URL);
			map.put("product", product);
			map.put("shareOpenid", member.getUserOpenid());
			remsg.setData(map);
			remsg.setMessage(NameUtils.getConfig("SUCCESSINFO"));
			remsg.setResultCode(ResultInfo.SUCCESSCODE);
			remsg.setStatus(true);
			return remsg;
		}

		try {
			String productIdString = StaticInfo.md5.decrypt(productId);
			Product product = this.productInfoService.getProductByProductId(Integer.parseInt(productIdString),colorId);
			List<ProductPicture> pictureList = productPictureService.getProductPictureByproductId(Integer.parseInt(productIdString));
			product.setPictureList(pictureList);
			/*
			 * 组装数据
			 */
			map = new HashMap<String, Object>();
			map.put("isShow", ResultInfo.ISSHOW);
			map.put("cartDesc", ResultInfo.CARTDESC);
			map.put("cart_url", ResultInfo.CART_URL);
			map.put("product", product);
			map.put("shareOpenid", member.getUserOpenid());
			remsg.setData(map);
			remsg.setMessage(NameUtils.getConfig("SUCCESSINFO"));
			remsg.setResultCode(ResultInfo.SUCCESSCODE);
		} catch (Exception e) {
			remsg.setMessage(NameUtils.getConfig("ERRORINFO"));
			remsg.setResultCode(ResultInfo.ERRORCODE);
			remsg.setStatus(false);
		}
		return remsg;
	}

	/**
	 * 生成分销商个人二维码
	 * @param productId
	 * @param colorId
	 * @param member
	 * @throws Exception
	 */
	public Map<String,Object> createQrCode(String productId,Integer colorId,Member member,boolean codeFlag) throws Exception{

		//获取项目地址
		WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
		ServletContext servletContext = webApplicationContext.getServletContext();
		String realPath = servletContext.getRealPath(ResultInfo.QRCODE_PLACE_IMAGE);
		Map<String,Object> map = new HashMap<String,Object>();

		if (!codeFlag) {
			map.put("flag",false);
			return map;
		}

		//查询该分销商信息
		PlaceInfo placeInfo = placeInfoService.getPlaceInfoByOpenId(member.getUserOpenid());
		//如果是商户，则生成个人二维码
		if (placeInfo != null) {
			int num = placeInfoService.getIsQrCode(String.valueOf(placeInfo.getId()));
			if (num <= 0) {
				map.put("flag",false);
				return map;
			}
			File files = new File(realPath);
			if (!files.exists()) {
				files.mkdir();
			}
			//优惠二维码
			StringBuilder url = new StringBuilder(NameUtils.getConfig("WECHAT_PRODUCT_DESC"));
			url.append("?productId=");
			url.append(productId);
			url.append("&colorId=");
			url.append(colorId);
			url.append("&userQrcodeType=");
			url.append("1");
			url.append("&placeOpenId=");
			url.append(member.getUserOpenid());
			StringBuilder name = new StringBuilder(realPath);
			name.append("\\");
			name.append(member.getUserOpenid());
			name.append("_1_");
			name.append(colorId);
			name.append(".");

			//购买二维码
			StringBuilder shopUrl = new StringBuilder(NameUtils.getConfig("WECHAT_PRODUCT_DESC"));
			shopUrl.append("?productId=");
			shopUrl.append(productId);
			shopUrl.append("&colorId=");
			shopUrl.append(colorId);
			shopUrl.append("&userQrcodeType=");
			shopUrl.append("2");
			shopUrl.append("&placeOpenId=");
			shopUrl.append(member.getUserOpenid());
			StringBuilder shopName = new StringBuilder(realPath);
			shopName.append("\\");
			shopName.append(member.getUserOpenid());
			shopName.append("_2_");
			shopName.append(colorId);
			shopName.append(".");
			//生成优惠二维码
			String qrCode = QrCodeUtil.createQrCode(url.toString(),name.toString());
			//生成购买二维码
			String shopQrCode = QrCodeUtil.createQrCode(shopUrl.toString(),shopName.toString());
			map.put("qrCode",ResultInfo.QRCODE_PLACE_IMAGE + "/" + member.getUserOpenid() + "_1_" + colorId + ".png");
			map.put("shopQrCode",ResultInfo.QRCODE_PLACE_IMAGE + "/" + member.getUserOpenid() + "_2_" + colorId + ".png");
			map.put("flag",true);
		} else {
			map.put("flag",false);
		}
		return map;
	}

	/**
	 * 获取商品详情（图文）
	 * @param request
	 * @param response
	 * @param productId
	 * @return list
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/getPictureByProductId")
	public ResultMsg getPictureByProductId(HttpServletRequest request,HttpServletResponse response,
										   @RequestParam(value = "productId",defaultValue = "") String productId) throws Exception {
		ResultMsg remsg = new ResultMsg();
		if (productId == null) {
			remsg.setStatus(false);
			remsg.setResultCode(ResultInfo.ERRORCODE);
			remsg.setMessage(NameUtils.getConfig("ERRORINFO"));
			return remsg;
		}
		if (StaticInfo.pattern.matcher(productId).find()) {
			ProductImageText productImageText = productImageTextService.getProductImageTextByProduectId(Integer.parseInt(productId));
			remsg.setData(productImageText);
			remsg.setMessage(NameUtils.getConfig("SUCCESSINFO"));
			remsg.setResultCode(ResultInfo.SUCCESSCODE);
			remsg.setStatus(true);
			return remsg;
		}
		try {
			String productIdString = StaticInfo.md5.decrypt(productId);
			ProductImageText productImageText = productImageTextService.getProductImageTextByProduectId(Integer.parseInt(productId));
			List<ProductPicture> pictureList = productPictureService.getProductPictureByproductId(Integer.parseInt(productIdString));
			productImageText.setPictureList(pictureList);
			remsg.setData(productImageText);
			remsg.setMessage(NameUtils.getConfig("SUCCESSINFO"));
			remsg.setResultCode(ResultInfo.SUCCESSCODE);
		} catch (Exception e) {
			remsg.setMessage(NameUtils.getConfig("ERRORINFO"));
			remsg.setResultCode(ResultInfo.ERRORCODE);
			remsg.setStatus(false);
		}
		return remsg;
	}

	/**
	 * 根据商品名称查询商品信息
	 * @param request
	 * @param response
	 * @param productName
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/getProductByProductName")
	public ResultMsg getProductByProductName(HttpServletRequest request,HttpServletResponse response,
											 @RequestParam(value = "productName", defaultValue = "")String productName) throws Exception {

		List<Product> productList = new ArrayList<Product>();
		ResultMsg remsg=new ResultMsg();
		Member member =  WechatUtils.getUserBySession(request);

		/*
		 * 判断用户信息及其参数
		 */
		if (member == null || member.getUserOpenid() == null) {
			remsg.setMessage(ResultInfo.loginOutError);
			remsg.setStatus(false);
			remsg.setMessage(ResultInfo.loginOutError);
			return remsg;
		}

		if (null != productName && !"".equals(productName)) {
			productName = "%" + productName + "%";
		}
		productList = this.productInfoService.getProductByProductName(productName);

		if (productList != null && productList.size() >= 1) {
			/*
			 * 优惠价计算
			 */
			boolean isPrice = false;
			if (member.getUserIdentity() != null && member.getUserIdentity() == 2) { 									// 微代理:优惠价
				isPrice = true;
			} else {
				if (member.getUserResType() != null && (member.getUserResType() == 2 || member.getUserResType() == 3)) { // 来源他人二维码:优惠价
					isPrice = true;
				} else {
					Member tempMember = memberService.selectUserResAndIdSByOpenId(member.getUserOpenid());
					member.setUserIdentity(tempMember.getUserIdentity());
					member.setUserResType(tempMember.getUserResType());
					if (tempMember.getUserIdentity() == 2) {// 微代理：优惠价
						isPrice = true;
					} else {
						if (tempMember.getUserResType() == 2 || tempMember.getUserResType() == 3) { // 来源他人二维码:优惠价
							isPrice = true;
						}
					}
				}
			}
			if (isPrice) {
				int price = systemService.selectOneShopRuleById(22).getRuleNum().intValue();
				for (Product product2 : productList) {
					product2.setProductPrice(product2.getProductDiscount());
					product2.setProductDiscount(price);
				}
			}
		}

		remsg.setMessage(NameUtils.getConfig("SUCCESSINFO"));
		remsg.setResultCode(ResultInfo.SUCCESSCODE);
		remsg.setStatus(true);
		remsg.setData(productList);
		return remsg;
	}

	/**
	 * 为当前用户获取产品分享URL
	 * @param productId
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/shareProductUrl", method = RequestMethod.POST)
	public ResultMsg shareProductUrl(
			@RequestParam(value = "productId", required = false) Integer productId,
			HttpServletRequest request
	) throws Exception {
		ResultMsg msg = new ResultMsg();
		msg.setResultCode(ResultInfo.ERRORCODE);

		Member member = WechatUtils.getUserBySession(request);
		if (member == null || member.getUserOpenid() == null) {
			msg.setMessage(ResultInfo.loginOutError);
			return msg;
		}
		if (productId == null) {
			msg.setMessage(ResultInfo.paramFormatError);
			return msg;
		}

		StringBuffer buffer = new StringBuffer(NameUtils.getConfig("WECHAT_PRODUCT_SHARE_URL"));
		buffer.append("?shareUser=" + StaticInfo.md5.encrypt(member.getUserOpenid()));
		buffer.append("&productId=" + StaticInfo.md5.encrypt(productId.toString()));
		buffer.append("&isShare=true&isProduct=true&isEnable=true"); // 是否分享,是否为产品,是否启动

		msg.setData(buffer.toString());
		msg.setStatus(true);
		msg.setMessage(NameUtils.getConfig("SUCCESSINFO"));
		msg.setResultCode(ResultInfo.SUCCESSCODE);
		return msg;
	}

	/**
	 * 商城内内部跳转产品信息详情页面
	 * @param productId
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping(value = "/desc", method = RequestMethod.GET)
	public void productDesc(
			@RequestParam("productId") Integer productId,
			HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 校验数据
		if (productId == null || "".equals(productId)) {
			request.getRequestDispatcher(NameUtils.getConfig("WECHAT_SHOP_PRODUCT_LIST_PAGE")).forward(request, response);return;
		}

		//如果是分享 则绑定关系
		Member member = WechatUtils.getUserBySession(request);

		//1：扫描优惠二维码；2：扫描购买二维码
		String userQrcodeType = request.getParameter("userQrcodeType");
		if (userQrcodeType != null && !"".equals(userQrcodeType) && !"null".equals(userQrcodeType)) {
			String placeOpenId = request.getParameter("placeOpenId");
			memberService.buildChildUser(placeOpenId, member,2,1);
			member = memberService.getMemberByUserOpenId(member.getUserOpenid());
			//只有关系没有完全绑定，才可以修改扫码类型
			if (member.getUserIsFlag() != null && member.getUserIsFlag() == 1) {
				Member memberInfo = new Member();
				memberInfo.setUserId(member.getUserId());
				memberInfo.setUserQrcodeType(Integer.parseInt(userQrcodeType));
				memberService.updateMemberByUserId(memberInfo);
			}
		} else {
			String shareOpenid = request.getParameter("placeOpenId");
			if(StringUtils.isNotEmpty(shareOpenid)){
				memberService.buildChildUser(shareOpenid, member,1,1);
			}
		}
		request.getRequestDispatcher(NameUtils.getConfig("WECHAT_SHOP_PRODUCT_DESC_PAGE")).forward(request, response);return;
	}


	/**
	 * 进入租赁产品详情页面
	 * @param productId
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/toLeasepProductDetails", method = RequestMethod.GET)
	public void toLeasepProductDetails(
			@RequestParam("productId") Integer productId,
			HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 校验数据
		if (productId == null || "".equals(productId)) {
			request.getRequestDispatcher(NameUtils.getConfig("WECHAT_SHOP_PRODUCT_LIST_PAGE")).forward(request, response);return;
		}
		request.getRequestDispatcher(NameUtils.getConfig("WECHAT_SHOP_PRODUCT_DESC_PAGE")).forward(request, response);return;
	}
	/**
	 * 获取租赁产品详情
	 */
	@RequestMapping(value = "/leasepProductDetails", method = RequestMethod.POST)
	@ResponseBody
	public ResultMsg leasepProductDetails(@RequestParam("productId") Integer productId,
										  HttpServletRequest request, HttpServletResponse response){
		String openid="";
		ResultMsg remsg = new ResultMsg();
		Map<String, Object> map = new HashMap<String, Object>();
		openid = WechatUtils.getUserBySession(request).getUserOpenid();
		List<ProductColor> productColorlist = null;
		// 校验数据
		if (productId == null || "".equals(productId)) {
			remsg.setStatus(false);
		}
		//查询产品信息
		Product leaseProduct = productInfoService.getLeaseProduct(productId);
		//查询产品颜色
		productColorlist =	productColorService.getLeaseProductColor(leaseProduct.getProductId());
		map.put("leaseProduct", leaseProduct);
		map.put("productColorlist", productColorlist);
		remsg.setStatus(true);
		remsg.setData(map);
		return remsg;
	}
}
