package org.cs.mgr.user.ctl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

import org.cs.core.ctl.BaseCtl;
import org.cs.util.DateUtil;
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 org.cs.mgr.user.service.IMgmCustomerService;
import org.cs.mgr.user.service.IMgmUserService;
import org.cs.util.Pager;
import org.cs.mgr.order.model.MgmOrder;
import org.cs.mgr.order.model.MgmOrderBreak;
import org.cs.mgr.order.service.IMgmOrderBreakService;
import org.cs.mgr.order.service.IMgmOrderService;
import org.cs.mgr.user.model.MgmCustomer;
import org.cs.mgr.user.model.MgmUser;
import org.cs.util.StringUtil;
import com.alibaba.fastjson.JSONObject;


/**
 * 
 * @ClassName: MgmCustomer 
 * @Description:  
 * @author: sunny_shi
 * @date: 2018-06-09 13:28:56
 */
@Controller
@RequestMapping("/mgmCustomerCtl")
public class MgmCustomerCtl extends BaseCtl {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private IMgmCustomerService mgmCustomerService;
	
	@Autowired
	private IMgmOrderService mgmOrderService;
	//推荐人信息服务
	@Autowired
	private IMgmUserService mgmUserService;
	
	//终止申请
	@Autowired
	private IMgmOrderBreakService mgmOrderBreakService;
	
	@RequestMapping("list")
	public String list(ModelMap mm, @RequestParam(required=false)String menuCode){
		mm.put(MENU_CODE, menuCode);
		this.set(MENU_CODE, menuCode);
		return "mgr/customer/list";	
	} 
	
	@RequestMapping(value="list2", method = RequestMethod.GET, produces="application/json;charset=UTF-8")
	@ResponseBody
	public String list2(int page, int pageSize, String search){
		Map<String, String> map = getMapFromJson(search);
		Pager pager = this.mgmCustomerService.getMgmCustomers(new Pager(page, pageSize),map);
		JSONObject json = new JSONObject();
		json.put("rows", pager.getResults());
		json.put("total", pager.getTotal());
		return JSONObject.toJSONString(json, features);
	}
	
	@RequestMapping("transfer")
	public String transfer(Integer id,Integer type,  String action, ModelMap mm){
		MgmOrder mgmOrder = null;
		if(id != null && type==1){
			mgmOrder = this.mgmOrderService.findById(id);
			mm.put("mgmOrder", mgmOrder);
			
			//MgmCustomer
			//客户信息
			Long customerId=mgmOrder.getCustomerId();
			if(customerId!=null) {
				MgmCustomer mgmCustomer = this.mgmCustomerService.findById(Integer.valueOf(customerId.toString()));
			  
			    mm.put("mgmCustomer", mgmCustomer);
			   
			}
			//推荐人员信息
			Long userId = mgmOrder.getUserId();
			if(userId!=null) {
				MgmUser mgmUser = this.mgmUserService.findById(Integer.valueOf(userId.toString()));
				mm.put("mgmUserTj", mgmUser);
			}
			//MgmUser
			//销售人员信息
			Long sealid = mgmOrder.getSalerId();
			if(sealid!=null) {
				MgmUser mgmUserSalers = this.mgmUserService.findById(Integer.valueOf(sealid.toString()));
				mm.put("mgmUserSaler", mgmUserSalers);
			}
			
			///订单终止申请			
			 Map<String, String> map = new HashMap<String, String>();
			 map.put("orderId", id.toString());
			 Pager pager = this.mgmOrderBreakService.queryMgmOrderBreaksById(new Pager(1), map);
			 if(pager.getTotal() >0) {
				 MgmOrderBreak mgmOrderBreak =(MgmOrderBreak) pager.getResults().get(0);
				 mm.put("mgmOrderBreak", mgmOrderBreak);
			 }
			
		}else{
			//客户信息
			MgmCustomer mgmCustomer = this.mgmCustomerService.findById(id);
			mm.put("mgmCustomer", mgmCustomer);
			
			MgmUser mgmUser = this.mgmUserService.findById(Integer.valueOf(mgmCustomer.getRecommendId().toString()));
			mm.put("mgmUserTj", mgmUser);
		}
		
		return "mgr/customer/" + action;
	}
	
	@RequestMapping(value="add", method = RequestMethod.POST)
	@ResponseBody
	public String add(MgmCustomer mgmCustomer){
		if(mgmCustomer != null ){
			this.mgmCustomerService.addMgmCustomer(mgmCustomer);
		}
		return AJAX_SUCCESS;
	}

    @RequestMapping(value="edit", method = RequestMethod.POST)
    @ResponseBody
    public String edit(MgmCustomer mgmCustomer){
        if(mgmCustomer != null ){
            this.mgmCustomerService.updateMgmCustomer(mgmCustomer);
        }
        return AJAX_SUCCESS;
    }

	
	
	
	@RequestMapping("del")
	@ResponseBody
	public String del(Integer id){
		if(id != null){
			this.mgmCustomerService.delMgmCustomer(id);
		}
		
		return AJAX_SUCCESS;
	}
	
	
	
		
}

