package org.cs.web.product;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.cs.constants.Constants;
import org.cs.core.ctl.BaseCtl;
import org.cs.mgr.order.service.IMgmOrderService;
import org.cs.mgr.product.model.MgmIntentOrder;
import org.cs.mgr.product.model.MgmProduct;
import org.cs.mgr.product.service.IMgmIntentOrderService;
import org.cs.mgr.product.service.IMgmProductCategoryService;
import org.cs.mgr.product.service.IMgmProductFollowService;
import org.cs.mgr.product.service.IMgmProductService;
import org.cs.mgr.user.model.MgmCustomer;
import org.cs.mgr.user.service.IMgmCustomerService;
import org.cs.util.HttpUtil;
import org.cs.util.Pager;
import org.cs.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
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.alibaba.fastjson.JSONObject;


/** 
 * 
 * @ClassName:  
 * @Description:  
 * @author: sunny_shi
 * @date: 2018-06-09 13:28:55
 */
@Controller
@RequestMapping("web/mgmProduct")
public class MgmProductWebCtl extends BaseCtl {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private IMgmOrderService mgmOrderService;	
	@Autowired
	private IMgmProductService mgmProductService;
	@Autowired
	private IMgmIntentOrderService mgmIntentOrderService;
	@Autowired
	private IMgmProductFollowService mgmProductFollowService;
	@Autowired
	private IMgmCustomerService mgmCustomerService;
	@Autowired
	private IMgmProductCategoryService mgmProductCategoryService;
	/**
	 * 
	 * <p>fenlei chanpin</p>
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("cp")
	public String me_money(String date,ModelMap mm){
		Map<String, String> map = getMapFromJson(date);
		try {
			JSONObject object = this.mgmProductService.categoryProduct(map);
			JSONObject json = new JSONObject();
			String ret = object.getString("ret");
			JSONObject data = new JSONObject();
			if("1".equals(ret)){
				data = object.getJSONObject("data");
				mm.put("category", data.getJSONObject("category"));
				mm.put("pager", data.getJSONObject("pager"));
			}
			
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";	
	}
	
	/**
	 * 
	 * <p>buyNow</p>
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("buyNow")
	public String buyNow(String productId,ModelMap mm){
		Map<String, String> user = (Map<String, String>) this.get(Constants.SESSION_WEB_USER);
		mm.put("productId", productId);
		mm.put("user", user);
		return "app/product/buy_intention";	
	}
	
	/**
	 * 
	 * <p>buyNow</p>
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("bnadd")
	@ResponseBody
	public String bnAdd(MgmIntentOrder mgmIntentOrder, ModelMap mm){
		try{
//			Admin user = (Admin) this.get(Constants.SESSION_USER);
			Map<String, String> user = (Map<String, String>) this.get(Constants.SESSION_WEB_USER);
			if(user == null){
				return JSONObject.toJSONString(getJson(0, "", "请先登录"), features);
			}
			this.mgmIntentOrderService.addMgmIntentOrder(mgmIntentOrder, user);
			return JSONObject.toJSONString(getJson(1, "", "意向购买提交成功"), features);
		}catch(Exception e){
			e.printStackTrace();
			return JSONObject.toJSONString(getJson(0, "", "意向购买提交失败"), features);
		}
	}
	
	/**
	 * 
	 * <p>buyNow</p>
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("now")
	@ResponseBody
	public String now(String categoryid, String num, String type, String twotype, String ismore,String cityCode, String page, String pageSize, ModelMap mm){
		Map<String, String> map = new HashMap<>();
		map.put("categoryid", categoryid);
		map.put("num", num);
		map.put("type", type);
		map.put("twotype", twotype);
		map.put("ismore", ismore);
		map.put("page", page);
		map.put("pageSize", pageSize);
		map.put("cityCode", cityCode);
		
		JSONObject object = this.mgmProductService.categoryProduct(map);
		String ret = object.getString("ret");
		JSONObject data = new JSONObject();
		Object ja = new Object();
		if("1".equals(ret)){
			data = object.getJSONObject("data");
			ja =  data.get("pager");
		}
		return JSONObject.toJSONString(ja, features);
	}
	
	
	/**
	 * 
	 * <p>buyNow</p>
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("more")
	public String more(String categoryid, String num, String type, String twotype, String ismore,String cityCode, String page, String pageSize, ModelMap mm){
		Map<String, String> map = new HashMap<>();
		map.put("categoryid", categoryid);
		map.put("num", num);
		map.put("type", type);
		map.put("twotype", twotype);
		map.put("ismore", ismore);
		map.put("page", page);
		map.put("pageSize", pageSize);
		map.put("cityCode", cityCode);
		
		if("xm".equals(categoryid)){
			mm.put("flname", "地产");
		}else{
			if(StringUtil.isNotBlank(categoryid)){
				mm.put("flname", this.mgmProductCategoryService.findById(Integer.valueOf(categoryid)).getGcName());
			}
		}
		JSONObject object = this.mgmProductService.categoryProduct(map);
		String ret = object.getString("ret");
		JSONObject data = new JSONObject();
		if("1".equals(ret)){
			data = object.getJSONObject("data");
			mm.put("data", data);
		}
		mm.put("data", data);
		if("2".equals(type) && "3".equals(twotype) && "1".equals(num)) {
			return "app/product/carmore";
		}else {
			return "app/product/more";
		}
	}
	
	/**
	 * 
	 * <p>buyNow</p>
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("detail")
	public String detail(String productid, String plat, ModelMap mm, HttpServletRequest request){
		
		try {
			Map<String, String> map = new HashMap<>();
			map.put("plat", plat);
			map.put("productid", productid);
			map.put("ip", HttpUtil.getIP(request));
			Map<String, String> user = (Map<String, String>) this.get(Constants.SESSION_WEB_USER);
			if(user == null){
				map.put("userid", "0");
			}else{
				map.put("userid", user.get("id"));
			}
			mm.put("detail", this.mgmProductService.queryProductDetail(map).get("data"));
			mm.put("groupimg", ((Map<String, Object>)this.mgmProductService.queryProductDetail(map).get("data")).get("product_group_img").toString().split(","));
		}catch(Exception e) {
			e.printStackTrace();
		}
			return "app/product/detail";
	}
	
	
	@RequestMapping(value="addFollow", method = RequestMethod.POST)
	@ResponseBody
	public String add(String productid){
		try {
			Map<String, String> map = new HashMap<>();
			map.put("productid", productid);
			Map<String, String> user = (Map<String, String>) this.get(Constants.SESSION_WEB_USER);
			if(user == null){
				map.put("userid", "0");
			}else{
				map.put("userid", user.get("id"));
			}
			return JSONObject.toJSONString(this.mgmProductFollowService.addMgmProductFollow(map), features);
		}catch(Exception e) {
			e.printStackTrace();
			return JSONObject.toJSONString(getJson(0, "", "关注失败"), features);
		}
	}
	
	@RequestMapping(value="followList")
	public String followList(String page, ModelMap mm){
		try {
			Map<String, String> map = new HashMap<>();
			map.put("page", page);
			Map<String, String> user = (Map<String, String>) this.get(Constants.SESSION_WEB_USER);
			if(user == null){
				return "app/login/login";
			}else{
				map.put("userid", user.get("id"));
			}
			mm.put("flist", this.mgmProductFollowService.queryFollowList(map).get("data"));
		}catch(Exception e) {
			e.printStackTrace();
		}
		return "app/product/attention";
	}
	
	@RequestMapping(value="follows")
	@ResponseBody
	public String followLists(String page, ModelMap mm){
		try {
			Map<String, String> map = new HashMap<>();
			map.put("page", page);
			Map<String, String> user = (Map<String, String>) this.get(Constants.SESSION_WEB_USER);
			if(user == null){
				
			}else{
				map.put("userid", user.get("id"));
			}
			return JSONObject.toJSONString(this.mgmProductFollowService.queryFollowList(map).get("data"), features);
		}catch(Exception e) {
			e.printStackTrace();
			return JSONObject.toJSONString(new Pager(), features);
		}
	}
	
	@RequestMapping(value="delFollow", method = RequestMethod.POST)
	@ResponseBody
	public String delFollow(String ids){
		try {
			Map<String, String> map = new HashMap<>();
			map.put("productids", ids);
			Map<String, String> user = (Map<String, String>) this.get(Constants.SESSION_WEB_USER);
			if(user == null){
				return "app/login/login";
			}else{
				map.put("userid", user.get("id"));
			}
			return JSONObject.toJSONString(this.mgmProductFollowService.delMgmProductFollow(map), features);
		}catch(Exception e) {
			e.printStackTrace();
			return JSONObject.toJSONString(getJson(0, "", "取消关注失败"), features);
		}
	}
	
	///马上推荐
	@RequestMapping(value="recommend")	
	public String recommend(String productids,ModelMap mm){
		List<Map<String, Object>> mlist = new ArrayList<>();
		for(String productid:productids.split(",")) {
			Map<String, Object> map = new HashMap<>();
			MgmProduct mp = this.mgmProductService.findById(Integer.valueOf(Integer.valueOf(productid)));
			map.put("id", mp.getId());
			map.put("name", mp.getProductName());
			mlist.add(map);
		}
		Map<String, String> user = (Map<String, String>) this.get(Constants.SESSION_WEB_USER);
		if(user == null){
			return "app/login/login";
		}else{
			mm.put("userId", user.get("id"));
		}
		JSONObject json= this.mgmCustomerService.queryMyMgmCustomer(new Pager(1, 30), Integer.valueOf(user.get("id").toString()));
		mm.put("custom", json.get("data"));
		mm.put("pids", productids);
		mm.put("mlist", mlist);
		return "app/product/tuijie_peo";
	}
	
	///马上推荐
	@RequestMapping(value="immRecommended")	
	@ResponseBody
	public String immRecommended(String customerid, String productids, String numbers, String remark,ModelMap mm){
		Map<String, String> map = new HashMap<>();
		map.put("customerId", customerid);
		map.put("productId", productids);
		map.put("numbers", numbers);
		Map<String, String> user = (Map<String, String>) this.get(Constants.SESSION_WEB_USER);
		if(user == null){
			return "app/login/login";
		}else{
			map.put("userId", user.get("id"));
		}
		map.put("remark", remark);
		JSONObject json = new JSONObject();	
		json=mgmOrderService.addMgmOrder(map);	
		//mm.put("json", JSONObject.toJSONString(json, features));
		return JSONObject.toJSONString(json, features);
	}
	
	///客户列表
	@RequestMapping(value="getCustomerList")	
	@ResponseBody
	public String getCustomerList(String pageNo,ModelMap mm){
		Map<String, String> user = (Map<String, String>) this.get(Constants.SESSION_WEB_USER);
		if(user == null){
			return "app/login/login";
		}
		JSONObject json = new JSONObject();	
		if(StringUtil.isBlank(pageNo)) {
			pageNo="1";
		}
		json= this.mgmCustomerService.queryMyMgmCustomer(new Pager(Integer.valueOf(pageNo), 30), Integer.valueOf(user.get("id").toString()));
		//mm.put("json", JSONObject.toJSONString(json, features));		
		return JSONObject.toJSONString(json.get("data"), features);
		
	}
	
	/**
	 * 
	 * <p>添加客户</p>
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("me_people_add")
	public String me_people_add(ModelMap mm, String pids){
		Map<String, String> user = (Map<String, String>) this.get(Constants.SESSION_WEB_USER);
		if(user == null){
			return "app/login/login";
		}else{
			mm.put("USER_ID", user.get("id"));
		}
		mm.put("pids", pids);
		return "app/product/me_people_add";	
	}
	
	@ResponseBody
	@RequestMapping(value="addCustomer", method = RequestMethod.POST)	
	public String addCustomer(String data,ModelMap mm){	
		Map<String, String> map = getMapFromJson(data);
		Map<String, String> user = (Map<String, String>) this.get(Constants.SESSION_WEB_USER);
		if(user == null){
			return JSONObject.toJSONString(getJson(0,"", "添加失败"), features);
		}else{
			map.put("recommendId", user.get("id"));
		}
		JSONObject json = new JSONObject();		
		json=mgmCustomerService.addMgmCustomer(map);
		return JSONObject.toJSONString(json, features);
	}
	
	/**
	 * 
	 * <p>buyNow</p>
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("detailfx")
	public String detailfx(String productid, String plat, ModelMap mm, HttpServletRequest request){
		
		try {
			Map<String, String> map = new HashMap<>();
			map.put("plat", plat);
			map.put("productid", productid);
			map.put("ip", HttpUtil.getIP(request));
			Map<String, String> user = (Map<String, String>) this.get(Constants.SESSION_WEB_USER);
			if(user == null){
				map.put("userid", "0");
			}else{
				map.put("userid", user.get("id"));
			}
			mm.put("detail", this.mgmProductService.queryProductDetail(map).get("data"));
			mm.put("groupimg", ((Map<String, Object>)this.mgmProductService.queryProductDetail(map).get("data")).get("product_group_img").toString().split(","));
		}catch(Exception e) {
			e.printStackTrace();
		}
			return "app/product/detailfx";
	}
}

