package org.cs.mgr.order.ctl;

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

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

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


/**
 * 
 * @ClassName: MgmOrder 
 * @Description:  
 * @author: sunny_shi
 * @date: 2018-06-09 13:29:02
 */
@Controller
@RequestMapping("/mgmOrderCtl")
public class MgmOrderCtl extends BaseCtl {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private IMgmOrderService mgmOrderService;
	///客户信息服务
	@Autowired
	private IMgmCustomerService mgmCustomerService;
	//推荐人信息服务
	@Autowired
	private IMgmUserService mgmUserService;
	
	
	
	//进度跟踪
	@Autowired
	private IMgmOrderScheduleService mgmOrderScheduleService;
		
	//终止申请
	@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/order/list";	
	}
	//预警
	@RequestMapping("warningList")
	public String warningList(ModelMap mm, @RequestParam(required=false)String menuCode){
		mm.put(MENU_CODE, menuCode);
		this.set(MENU_CODE, menuCode);
		return "mgr/order/warningList";	
	}
	//apply
	//预警
	@RequestMapping("applyList")
	public String applyList(ModelMap mm, @RequestParam(required=false)String menuCode){
		mm.put(MENU_CODE, menuCode);
		this.set(MENU_CODE, menuCode);
		return "mgr/order/applyList";	
	}
	
	@RequestMapping(value="warningList2", method = RequestMethod.GET, produces="application/json;charset=UTF-8")
	@ResponseBody
	public String warningList2(int page, int pageSize,String  search){
		Map<String, String> map = getMapFromJson(search);		
		map.put("wanging", "1");
		Pager pager = this.mgmOrderService.getMgmOrdersBySearch(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(value="applyList2", method = RequestMethod.GET, produces="application/json;charset=UTF-8")
	@ResponseBody
	public String applyList2(int page, int pageSize,String  search){
		Map<String, String> map = getMapFromJson(search);
		
		// 初始化左侧菜单及权限信息 
				Admin user = (Admin) this.get(Constants.SESSION_USER);
				List<Map<String,Object>> permissions =(List<Map<String, Object>>) this.get(Constants.SESSION_PERMISSIONS);
				
				if(permissions!=null) {
					if(permissions.size()>0) {
						Map<String,Object> permission=permissions.get(0);
						if(permission.get("num")!=null) {
							map.put("roleCodeN",permission.get("num").toString());
							if(permission.get("one_category_id")!=null) {						
								map.put("one_category_id",permission.get("one_category_id").toString());
							}else {
								map.put("one_category_id","");
							}
							if(permission.get("two_category_id")!=null) {						
								map.put("two_category_id",permission.get("two_category_id").toString());
							}else {
								map.put("two_category_id","");
							}
							if(permission.get("three_category_id")!=null) {						
								map.put("three_category_id",permission.get("three_category_id").toString());
							}else {
								map.put("three_category_id","");
							}
							
						}else {
							map.put("roleCodeN","");
						}
					}else {
						map.put("roleCodeN","");
					}
				}else {
					map.put("roleCodeN","");
				}
				
		map.put("apply", "1");
		Pager pager=this.mgmOrderBreakService.getMgmOrderBreaksByS(new Pager(page, pageSize), map);
//		Pager pager = this.mgmOrderService.getMgmOrdersBySearch(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(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);
		
		// 初始化左侧菜单及权限信息 
		Admin user = (Admin) this.get(Constants.SESSION_USER);
		List<Map<String,Object>> permissions =(List<Map<String, Object>>) this.get(Constants.SESSION_PERMISSIONS);
		
		if(permissions!=null) {
			if(permissions.size()>0) {
				Map<String,Object> permission=permissions.get(0);
				if(permission.get("num")!=null) {
					map.put("roleCodeN",permission.get("num").toString());
					if(permission.get("one_category_id")!=null) {						
						map.put("one_category_id",permission.get("one_category_id").toString());
					}else {
						map.put("one_category_id","");
					}
					if(permission.get("two_category_id")!=null) {						
						map.put("two_category_id",permission.get("two_category_id").toString());
					}else {
						map.put("two_category_id","");
					}
					if(permission.get("three_category_id")!=null) {						
						map.put("three_category_id",permission.get("three_category_id").toString());
					}else {
						map.put("three_category_id","");
					}
					
				}else {
					map.put("roleCodeN","");
				}
			}else {
				map.put("roleCodeN","");
			}
		}else {
			map.put("roleCodeN","");
		}
		
		Pager pager = this.mgmOrderService.getMgmOrdersBySearch(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,@RequestParam(required = false)String break_id, String action, ModelMap mm){
		MgmOrder mgmOrder = null;
		if(id != null){
			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);
//			 }
//			 
			 if(StringUtil.isNotBlank(break_id)) {
				 MgmOrderBreak mgmOrderBreak =this.mgmOrderBreakService.findById(Integer.valueOf(break_id));
				 mm.put("mgmOrderBreak", mgmOrderBreak);
			 }
			
		}
		
		
		return "mgr/order/" + action;
	}
	
	@RequestMapping("salesmen")
	public String salesmen(String id, ModelMap mm){		
		
		mm.put("orderId", id);
		return "mgr/order/selectMgmUser";
	}
	
	@RequestMapping("salesmenTj")
	public String salesmenTj(String id,String custid,ModelMap mm){		
		mm.put("orderId", id);
		mm.put("customerId", custid);
		return "mgr/order/selectMgmUserTj";
	}
	
	@RequestMapping("transferOder")
	public String transferOder(String id, ModelMap mm){		
		
		mm.put("orderId", id);
		return "mgr/order/transferOder";
	}
	
	@RequestMapping("transfera")
	public String transfera(String id, ModelMap mm){	
		MgmOrder mgmOrder = null;		
		if(id != null){
			mgmOrder = this.mgmOrderService.findById(Integer.valueOf(id));
			mm.put("mgmOrder", mgmOrder);
			Long userId = mgmOrder.getSalerId();
			if(userId!=null) {
				MgmUser mgmUser = this.mgmUserService.findById(Integer.valueOf(String.valueOf(userId)));
				mm.put("mgmSalerUser", mgmUser);
			}
			
		}
		return "mgr/order/transfer";
	}
	
	@RequestMapping(value="pruductList", method = RequestMethod.GET, produces="application/json;charset=UTF-8")
	@ResponseBody
	public String pruductList(String  orderid){
		
		Map<String, String> map = new HashMap();
		map.put("orderId", orderid);
		//map.put("pageNo", String.valueOf(1));
		
		JSONObject json = this.mgmOrderService.orderPruductDetails(map);
		
		JSONObject datajoson=(JSONObject) json.get("data");
		
		JSONObject jsond = new JSONObject();
		jsond.put("rows", datajoson.get("results"));
		jsond.put("total", datajoson.get("total"));
		
		return JSONObject.toJSONString(jsond, features);
	
	}
	
	@RequestMapping(value="scheduleList", method = RequestMethod.GET, produces="application/json;charset=UTF-8")
	@ResponseBody
	public String scheduleList(String  orderid){
		
		Map<String, String> map = new HashMap();
		map.put("orderId", orderid);
		JSONObject json = this.mgmOrderScheduleService.getOrderProgressDeteails(map);
		JSONObject datajoson=(JSONObject) json.get("data");
		JSONObject jsond = new JSONObject();
		jsond.put("rows", datajoson.get("results"));
		jsond.put("total", datajoson.get("total"));
		return JSONObject.toJSONString(jsond, features);
	
	}
	
	
	@RequestMapping(value="mgmUserList", method = RequestMethod.GET, produces="application/json;charset=UTF-8")
	@ResponseBody
	public String mgmUserList(int page, int pageSize,String  search){		
		Map<String, String> map = getMapFromJson(search);		
		Pager pager = this.mgmUserService.getMgmUsersBySeach(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(value="mgmUserListTj", method = RequestMethod.GET, produces="application/json;charset=UTF-8")
	@ResponseBody
	public String mgmUserListTj(int page, int pageSize,String  customerId){		
		Map<String, String> map = new HashMap<String, String>();
		map.put("customerId", customerId);
		Pager pager = this.mgmUserService.getMgmUsersBySeachTj(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(value="add", method = RequestMethod.POST)
	@ResponseBody
	public String add(MgmOrder mgmOrder){
		if(mgmOrder != null ){
			this.mgmOrderService.addMgmOrder(mgmOrder);
		}
		return AJAX_SUCCESS;
	}

	
	
    @RequestMapping(value="edit", method = RequestMethod.POST)
    @ResponseBody
    public String edit(MgmOrder mgmOrder){
        if(mgmOrder != null ){
            this.mgmOrderService.updateMgmOrder(mgmOrder);
        }
        return AJAX_SUCCESS;
    }

	
    @RequestMapping(value="blApply", method = RequestMethod.POST)
    @ResponseBody
    public String blApply(MgmOrderBreak mgmOrderBreak){
        if(mgmOrderBreak != null ){
        	Admin admin = (Admin) this.get(Constants.SESSION_USER);        	
        	MgmOrderBreak mgmOrderBreaknew= this.mgmOrderBreakService.findById(mgmOrderBreak.getId());        	
        	
        	if(mgmOrderBreak.getApplyState().equals("2")) {
        		mgmOrderBreaknew.setIsDelete(true);
        	}
        	
        	mgmOrderBreaknew.setApproverId(admin.getId());
        	mgmOrderBreaknew.setApprover(admin.getName());
        	mgmOrderBreaknew.setApproveReamrk(mgmOrderBreak.getApproveReamrk());
        	mgmOrderBreaknew.setApplyState(mgmOrderBreak.getApplyState());
        	this.mgmOrderBreakService.update(mgmOrderBreaknew);
        	MgmOrder mgmOrder = this.mgmOrderService.findById(mgmOrderBreak.getOrderId());
        	if(mgmOrderBreak.getApplyState().equals("1")) {
	        	mgmOrder.setModifyDate(new Date());
	        	mgmOrder.setBreakTime(new Date());	  
	        	mgmOrder.setBreakState("31");
        	}else {
        		mgmOrder.setModifyDate(new Date());
        		mgmOrder.setBreakTime(null);
        		mgmOrder.setBreakState("");
        	}
        	this.mgmOrderService.update(mgmOrder);
        }
        return AJAX_SUCCESS;
    }
	
	
	@RequestMapping("del")
	@ResponseBody
	public String del(Integer id){
		if(id != null){
			this.mgmOrderService.delMgmOrder(id);
		}
		
		return AJAX_SUCCESS;
	}
	
	
//	@RequestMapping(value="transferAction", method = RequestMethod.POST)
//    @ResponseBody
//    public String transferAction(MgmOrderTransfer mgmOrderTransfer, ModelMap mm){
//        
//		JSONObject json = new JSONObject();
//		
//		json = this.mgmOrderService.transferAction(mgmOrderTransfer);
//		
//		
//		return JSONObject.toJSONString(json, features);
//    }

	@RequestMapping(value="transferAction", method = RequestMethod.POST)
	@ResponseBody
	public String add(MgmOrderTransfer mgmOrderTransfer, ModelMap mm){
		if(mgmOrderTransfer != null ){
			this.mgmOrderService.transferAction(mgmOrderTransfer);
		}
		return AJAX_SUCCESS;
	}
	
	
	
    //指派销售人员
	@RequestMapping(value="assignAction", method = RequestMethod.POST)
    @ResponseBody
    public String assignAction(String userId,String orderId){
        
		JSONObject json = new JSONObject();		
		json = this.mgmOrderService.assignAction(userId, orderId);		
		return JSONObject.toJSONString(json, features);
    }
	
	  //指派推荐人员
	@RequestMapping(value="assignActionTj", method = RequestMethod.POST)
    @ResponseBody
    public String assignActionTj(String userId,String orderId){
        
		JSONObject json = new JSONObject();		
		json = this.mgmOrderService.assignActionTj(userId, orderId);		
		return JSONObject.toJSONString(json, features);
    }
		
	
	
	
	//签约
	@RequestMapping(value="signAction", method = RequestMethod.POST)
    @ResponseBody
    public String signAction(String actionName,String orderId){
		
		JSONObject json = new JSONObject();		
		json = this.mgmOrderService.signAction(actionName, orderId);		
		return JSONObject.toJSONString(json, features);
//		 return AJAX_SUCCESS;
    }
		
	//交房
	
	@RequestMapping(value="deliveryAction", method = RequestMethod.POST)
    @ResponseBody
    public String deliveryAction(String orderId){
		JSONObject json = new JSONObject();		
		json = this.mgmOrderService.deliveryAction(orderId);		
		return JSONObject.toJSONString(json, features);
    }
	
	//订单继续
	@RequestMapping(value="orderContinuation", method = RequestMethod.POST)
    @ResponseBody
    public String orderContinuation(String orderId){
		JSONObject json = new JSONObject();		
		json = this.mgmOrderService.orderContinuation(orderId);		
		return JSONObject.toJSONString(json, features);
    }
	//订单终止
	@RequestMapping(value="orderEnd", method = RequestMethod.POST)
    @ResponseBody
    public String orderEnd(String orderId){
		JSONObject json = new JSONObject();	
		Admin admin = (Admin) this.get(Constants.SESSION_USER); 		
		json = this.mgmOrderService.orderEnd(orderId,admin.getId());		
		return JSONObject.toJSONString(json, features);
    }
	
}

