package org.cs.ws.user.ctl;

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

import javax.servlet.http.HttpServletRequest;

import org.cs.core.ctl.BaseCtl;
import org.cs.core.model.Dic;
import org.cs.core.service.IDicService;
import org.cs.mgr.product.model.MgmProductCategory;
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.product.service.IMgmProductViewService;
import org.cs.mgr.user.model.MgmUser;
import org.cs.mgr.user.service.IMgmUserService;
import org.cs.util.HttpUtil;
import org.cs.util.Pager;
import org.cs.util.StringUtil;
import org.cs.ws.inteceptor.Valid;
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.ResponseBody;

import com.alibaba.fastjson.JSONObject;

@Controller
@RequestMapping("/rest/project")
public class ProjectApi extends BaseCtl{

	@Autowired
	private IMgmProductCategoryService mgmProductCategoryService;
	@Autowired
	private IDicService dicService;
	@Autowired
	private IMgmProductService mgmProductService;
	@Autowired
	private IMgmProductFollowService mgmProductFollowService;
	@Autowired
	private IMgmProductViewService mgmProductViewService;
	@Autowired
	private IMgmIntentOrderService mgmIntentOrderService;
	@Autowired
	private IMgmUserService mgmUserService;
	
	@RequestMapping(value="/category",method = RequestMethod.POST)
	@ResponseBody
	public String category(String categoryid, String num, String type, String ismore, String page, String data){
		
		Map<String, String> data1 = getMapFromJson(data);
		/*categoryid = data1.get("categoryid");
		num = data1.get("num");
		type = data1.get("type");
		ismore = data1.get("ismore");
		page = data1.get("page");*/
		
		//增加用户类别过滤产品列表
		MgmUser user=null;
		String userId=data1.get("userId");
		if(StringUtil.isNotBlank(userId)){
			user = this.mgmUserService.findById(Integer.valueOf(userId));
			if(user!=null){
				data1.put("userType", user.getType());
			}
		}
		
		
		JSONObject object = this.mgmProductService.categoryProduct(data1);
		
		/*JSONObject json = new JSONObject();
		//分类
		List<Map<String, String>> clist = new ArrayList<>();
		//商品分类集合
		List<Map<String, Object>> productlist = new ArrayList<>();
		Map<String, String> map = new HashMap<>();
		//1 下一页
		if("1".equals(ismore)){
			
			if("xm".equals(categoryid)){
				//地产的
				List<Dic> dlist = dicService.getByType(categoryid);
				for(Dic dic: dlist){
					map = new HashMap<>();
					map.put("name", dic.getName());
					map.put("type", "1");
					map.put("num", "1");
					map.put("twotype", "");
					map.put("id", dic.getCode());
					clist.add(map);
				}
				Map<String, String> pmap = new HashMap<>();
				pmap.put("type", "1");
				pmap.put("num", "1");
				pmap.put("categoryid", dlist.get(0).getCode());
				if(StringUtil.isBlank(page)){
					json.put("pager", this.mgmProductService.queryProductPager(new Pager(1), pmap));
				}else{
					json.put("pager", this.mgmProductService.queryProductPager(new Pager(Integer.valueOf(page)), pmap));
				}
				
				Map<String, Object> cate = new HashMap<>();
				cate.put("name", dlist.get(0).getName());
				cate.put("type", "1");
				cate.put("num", "1");
				cate.put("twotype", "");
				cate.put("id", dlist.get(0).getCode().toString());
				
				Map<String, String> maps = new HashMap<>();
				maps.put("type", "1");
				maps.put("num", "1");
				maps.put("categoryid", dlist.get(0).getCode().toString());
				if(StringUtil.isBlank(page)){
					cate.put("pager", this.mgmProductService.queryProductPager(new Pager(1), maps));
				}else{
					cate.put("pager", this.mgmProductService.queryProductPager(new Pager(Integer.valueOf(page)), maps));
				}
				productlist.add(cate);//商品分类集合
				json.put("pager", productlist);
				
			}else{
				//非地产的
				Map<String, String> map1 = new HashMap<>();
				map1.put("id", categoryid);
				List<MgmProductCategory> mlist = this.mgmProductCategoryService.queryShowCategory(map1);
				for(MgmProductCategory mpc:mlist){
					map = new HashMap<>();
					map.put("name", mpc.getGcName());
					map.put("type", mpc.getType());
					map.put("num", mpc.getNum().toString());
					map.put("twotype", mpc.getTwoType());
					map.put("id", mpc.getId().toString());
					clist.add(map);
				}
				
				
				Map<String, String> pmap = new HashMap<>();
				pmap.put("type", mlist.get(0).getType());
				pmap.put("num", mlist.get(0).getNum().toString());
				pmap.put("categoryid", mlist.get(0).getId().toString());
				if(StringUtil.isBlank(page)){
					json.put("pager", this.mgmProductService.queryProductPager(new Pager(1), pmap));
				}else{
					json.put("pager", this.mgmProductService.queryProductPager(new Pager(Integer.valueOf(page)), pmap));
				}
				
				if("3".equals(mlist.get(0).getTwoType()) && 2 == mlist.get(0).getNum().intValue()){//汽车类别2级
					Map<String, String> map2 = new HashMap<>();
					map2.put("id", mlist.get(0).getId().toString());
					List<MgmProductCategory> mplist = this.mgmProductCategoryService.queryShowCategory(map2);
					for(MgmProductCategory mpc:mplist){
						Map<String, Object> cate = new HashMap<>();
						cate.put("name", mpc.getGcName());
						cate.put("type", mpc.getType());
						cate.put("num", mpc.getNum().toString());
						cate.put("twotype", mpc.getTwoType());
						cate.put("id", mpc.getId().toString());
						
						Map<String, String> maps = new HashMap<>();
						maps.put("type", mpc.getType());
						maps.put("num", mpc.getNum().toString());
						maps.put("categoryid", mpc.getId().toString());
						if(StringUtil.isBlank(page)){
							cate.put("pager", this.mgmProductService.queryProductPager(new Pager(1,3), maps));
						}else{
							cate.put("pager", this.mgmProductService.queryProductPager(new Pager(Integer.valueOf(page),3), maps));
						}
						productlist.add(cate);//商品分类集合
					}
					json.put("pager", productlist);
				}else{//2级和其他类别
					Map<String, Object> cate = new HashMap<>();
					cate.put("name", mlist.get(0).getGcName());
					cate.put("type", mlist.get(0).getType());
					cate.put("num", mlist.get(0).getNum().toString());
					cate.put("twotype", mlist.get(0).getTwoType());
					cate.put("id", mlist.get(0).getId().toString());
					
					Map<String, String> maps = new HashMap<>();
					maps.put("type", mlist.get(0).getType());
					maps.put("num", mlist.get(0).getNum().toString());
					maps.put("categoryid", mlist.get(0).getId().toString());
					if(StringUtil.isBlank(page)){
						cate.put("pager", this.mgmProductService.queryProductPager(new Pager(1), maps));
					}else{
						cate.put("pager", this.mgmProductService.queryProductPager(new Pager(Integer.valueOf(page)), maps));
					}
					productlist.add(cate);//商品分类集合
					json.put("pager", productlist);
				}
			}
			
		}else if("2".equals(ismore)){
			if(StringUtil.isBlank(categoryid) || "xm".equals(categoryid)){
				type = "1";
				num = "0";
				//首页菜单
				map = new HashMap<>();
				map.put("name", "地产");
				map.put("type", "1");
				map.put("num", "0");
				map.put("twotype", "");
				map.put("id", "xm");
				clist.add(map);
				List<MgmProductCategory> mlist = this.mgmProductCategoryService.queryShowCategory(new HashMap<>());
				for(MgmProductCategory mpc: mlist){
					map = new HashMap<>();
					map.put("name", mpc.getGcName());
					map.put("type", mpc.getType());
					map.put("num", mpc.getNum().toString());
					map.put("twotype", mpc.getTwoType());
					map.put("id", mpc.getId().toString());
					clist.add(map);
				}
				Map<String, String> pmap = new HashMap<>();
				pmap.put("type", "1");
				pmap.put("num", "0");
				pmap.put("categoryid", "dc");
				if(StringUtil.isBlank(page)){
					json.put("pager", this.mgmProductService.queryProductPager(new Pager(1), pmap));
				}else{
					json.put("pager", this.mgmProductService.queryProductPager(new Pager(Integer.valueOf(page)), pmap));
				}
				
				Map<String, Object> cate = new HashMap<>();
				cate.put("name", "地产");
				cate.put("type", "1");
				cate.put("num", "0");
				cate.put("twotype", "");
				cate.put("id", "xm");
				
				Map<String, String> maps = new HashMap<>();
				maps.put("type", "1");
				maps.put("num", "0");
				maps.put("categoryid", "xm");
				if(StringUtil.isBlank(page)){
					cate.put("pager", this.mgmProductService.queryProductPager(new Pager(1), maps));
				}else{
					cate.put("pager", this.mgmProductService.queryProductPager(new Pager(Integer.valueOf(page)), maps));
				}
				productlist.add(cate);//商品分类集合
				json.put("pager", productlist);
				
			}else{
				Map<String, String> pmap = new HashMap<>();
				pmap.put("type", type);
				pmap.put("num", num);
				pmap.put("categoryid", categoryid);
				if(StringUtil.isBlank(page)){
					json.put("pager", this.mgmProductService.queryProductPager(new Pager(1), pmap));
				}else{
					json.put("pager", this.mgmProductService.queryProductPager(new Pager(Integer.valueOf(page)), pmap));
				}
				MgmProductCategory mpc = this.mgmProductCategoryService.findById(Integer.valueOf(categoryid));
				Dic dic = this.dicService.getByTypeAndCode("xm", categoryid);
				if("1".equals(type)){//地产
					Map<String, Object> cate = new HashMap<>();
					cate.put("name", dic.getName());
					cate.put("type", type);
					cate.put("num", num);
					cate.put("twotype", "");
					cate.put("id", categoryid);
					
					Map<String, String> maps = new HashMap<>();
					maps.put("type",type );
					maps.put("num", num);
					maps.put("categoryid", categoryid);
					if(StringUtil.isBlank(page)){
						cate.put("pager", this.mgmProductService.queryProductPager(new Pager(1), maps));
					}else{
						cate.put("pager", this.mgmProductService.queryProductPager(new Pager(Integer.valueOf(page)), maps));
					}
					productlist.add(cate);//商品分类集合
					json.put("pager", productlist);
				}else
				if("3".equals(mpc.getTwoType()) && mpc.getNum().intValue() == 2){
					Map<String, String> map2 = new HashMap<>();
					map2.put("id", categoryid);
					List<MgmProductCategory> mplist = this.mgmProductCategoryService.queryShowCategory(map2);
					for(MgmProductCategory mpc1:mplist){
						Map<String, Object> cate = new HashMap<>();
						cate.put("name", mpc1.getGcName());
						cate.put("type", mpc1.getType());
						cate.put("num", mpc1.getNum().toString());
						cate.put("twotype", mpc1.getTwoType());
						cate.put("id", mpc1.getId().toString());
						
						Map<String, String> maps = new HashMap<>();
						maps.put("type", mpc1.getType());
						maps.put("num", mpc1.getNum().toString());
						maps.put("categoryid", mpc1.getId().toString());
						if(StringUtil.isBlank(page)){
							cate.put("pager", this.mgmProductService.queryProductPager(new Pager(1,3), maps));
						}else{
							cate.put("pager", this.mgmProductService.queryProductPager(new Pager(Integer.valueOf(page),3), maps));
						}
						productlist.add(cate);//商品分类集合
					}
					json.put("pager", productlist);
				}else{
					Map<String, Object> cate = new HashMap<>();
					cate.put("name", mpc.getGcName());
					cate.put("type", type);
					cate.put("num", num);
					cate.put("twotype", mpc.getTwoType());
					cate.put("id", categoryid);
					
					Map<String, String> maps = new HashMap<>();
					maps.put("type",type );
					maps.put("num", num);
					maps.put("categoryid", categoryid);
					if(StringUtil.isBlank(page)){
						cate.put("pager", this.mgmProductService.queryProductPager(new Pager(1), maps));
					}else{
						cate.put("pager", this.mgmProductService.queryProductPager(new Pager(Integer.valueOf(page)), maps));
					}
					productlist.add(cate);//商品分类集合
					json.put("pager", productlist);
				}
			}
			
		}else{
			return JSONObject.toJSONString(getJson(0, "", "未知查询"), features);
		}
		
		Map<String, String> pmap = new HashMap<>();
		pmap.put("type", type);
		pmap.put("num", num);
		pmap.put("categoryid", categoryid);
		if(StringUtil.isBlank(page)){
			json.put("pager", this.mgmProductService.queryProductPager(new Pager(1), pmap));
		}else{
			json.put("pager", this.mgmProductService.queryProductPager(new Pager(Integer.valueOf(page)), pmap));
		}
		
		json.put("category", clist);*/
		
		return JSONObject.toJSONString(object, features);
	}
	
	
	@RequestMapping(value="addFollow", method = RequestMethod.POST)
	@ResponseBody
	public String add(String data){
		try {
			Map<String, String> map = getMapFromJson(data);
			return JSONObject.toJSONString(this.mgmProductFollowService.addMgmProductFollow(map), features);
		}catch(Exception e) {
			e.printStackTrace();
			return JSONObject.toJSONString(getJson(0, "", "关注失败"), features);
		}
	}
	
	@RequestMapping(value="delFollow", method = RequestMethod.POST)
	@ResponseBody
	public String delFollow(String data){
		try {
			Map<String, String> map = getMapFromJson(data);
			return JSONObject.toJSONString(this.mgmProductFollowService.delMgmProductFollow(map), features);
		}catch(Exception e) {
			e.printStackTrace();
			return JSONObject.toJSONString(getJson(0, "", "取消关注失败"), features);
		}
	}
	
	@RequestMapping(value="followList", method = RequestMethod.POST)
	@ResponseBody
	public String followList(String data){
		try {
			Map<String, String> map = getMapFromJson(data);
			return JSONObject.toJSONString(this.mgmProductFollowService.queryFollowList(map), features);
		}catch(Exception e) {
			e.printStackTrace();
			return JSONObject.toJSONString(getJson(0, "", "获取关注列表失败"), features);
		}
	}
	
	@RequestMapping(value="addView", method = RequestMethod.POST)
	@ResponseBody
	public String addView(String data){
		try {
			Map<String, String> map = getMapFromJson(data);
			return JSONObject.toJSONString(this.mgmProductViewService.addMgmProductView(map), features);
		}catch(Exception e) {
			e.printStackTrace();
			return JSONObject.toJSONString(getJson(0, "", "关注失败"), features);
		}
	}
	
	@RequestMapping(value="productDetail", method = RequestMethod.POST)
	@ResponseBody
	public String productDetail(String data, String plat, HttpServletRequest request){
		try {
			Map<String, String> map = getMapFromJson(data);
			map.put("plat", plat);
			map.put("ip", HttpUtil.getIP(request));
			return JSONObject.toJSONString(this.mgmProductService.queryProductDetail(map), features);
		}catch(Exception e) {
			e.printStackTrace();
			return JSONObject.toJSONString(getJson(0, "", "获取详情失败"), features);
		}
	}
	
	@RequestMapping(value="addIntent", method = RequestMethod.POST)
	@ResponseBody
	public String addIntent(String data){
		try {
			Map<String, String> map = getMapFromJson(data);
			return JSONObject.toJSONString(this.mgmIntentOrderService.addMgmIntentOrder(map), features);
		}catch(Exception e) {
			e.printStackTrace();
			return JSONObject.toJSONString(getJson(0, "", "关注失败"), features);
		}
	}
	
}
