package org.cs.web.user;

import java.math.BigDecimal;
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.user.model.MgmCustomer;
import org.cs.mgr.user.model.MgmInviteRecord;
import org.cs.mgr.user.model.MgmUser;
import org.cs.mgr.user.service.IMgmCustomerService;
import org.cs.mgr.user.service.IMgmInviteRecordService;
import org.cs.mgr.user.service.IMgmUserService;
import org.cs.util.Pager;
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.util.StringUtils;
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: MgmCustomer 
 * @Description:  
 * @author: sunny_shi
 * @date: 2018-06-09 13:28:55
 */
@Controller
@RequestMapping("web/mgmCustomerWebCtl")
public class MgmCustomerWebCtl extends BaseCtl {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	public IMgmCustomerService mgmCustomerService;
	
	/**
	 * 
	 * <p>客户列表</p>
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("me_people")
	public String me_people(@RequestParam(required=false)String userId,ModelMap mm){
		List<MgmCustomer> lists;
		BigDecimal sum;
		try {
			lists = mgmCustomerService.getMgmCustomerList(userId);
			mm.put("lists", lists);
			mm.put("USER_ID", userId);
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "app/me/me_people";	
	}
	
	
	/**
	 * 
	 * <p>查看客户信息</p>
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("me_people_details")
	public String me_people_details(@RequestParam(required=false)String id,ModelMap mm, String type, String pids){
		MgmCustomer customer;
		BigDecimal sum;
		try {
			customer = mgmCustomerService.findById(Integer.parseInt(id));
			System.out.println(customer.getRecommendId());
			mm.put("CUSTOMER", customer); 
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//如果是从我的推介中跳转的，返回我的推介,type =1
		mm.put("ty", type);
		mm.put("pids", pids);
		return "app/me/me_people_details";	
	} 
	
	/**
	 * 
	 * <p>添加客户</p>
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("me_people_add")
	public String me_people_add(@RequestParam(required=false)String userId,ModelMap mm){
		mm.put("USER_ID", userId);
		return "app/me/me_people_add";	
	}
	
	
	@ResponseBody
	@RequestMapping(value="addCustomer", method = RequestMethod.POST)	
	public String addCustomer(String data,ModelMap mm){		
		Map<String, String> map = getMapFromJson(data);
		logger.info("map::" + map);
		JSONObject json = new JSONObject();		
		json=mgmCustomerService.addMgmCustomer(map);
		return JSONObject.toJSONString(json, features);
	}

	@ResponseBody
	@RequestMapping(value="updateCustomer", method = RequestMethod.POST)	
	public String updateCustomer(String data,ModelMap mm){		
		Map<String, String> map = getMapFromJson(data);
		System.out.println("map::" + map);
		JSONObject json = new JSONObject();		
		json=mgmCustomerService.updateMgmCustomer(map);
		return JSONObject.toJSONString(json, features);
	}
	
	@ResponseBody
	@RequestMapping(value="deleteCustomer", method = RequestMethod.POST)	
	public String deleteCustomer(String data,ModelMap mm){		
		Map<String, String> map = getMapFromJson(data);
		System.out.println("map::" + map);
		JSONObject json = new JSONObject();		
		json=mgmCustomerService.deleteMgmCustomer(map);
		//mm.put("json", JSONObject.toJSONString(json, features));
		return JSONObject.toJSONString(json, features);
	}
}

