package org.cs.web.user;

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

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.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.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.cs.mgr.order.service.IMgmOrderScheduleService;
import org.cs.mgr.order.service.IMgmOrderService;
import org.cs.mgr.user.service.IMgmCustomerService;
import org.cs.util.Pager;
import org.cs.mgr.order.model.MgmOrder;
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("/web/mgmOrder")
public class MgmOrderWeb extends BaseCtl {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private IMgmOrderService mgmOrderService;	
	
	@Autowired
	private IMgmCustomerService mgmCustomerService;
	
	@Autowired
	private IMgmOrderScheduleService mgmOrderScheduleService;
	///进度管理
	///订单详情--订单列表
	@RequestMapping("getOrderList")	
	public String getOrderList(ModelMap mm,HttpServletRequest request){
		Map<String, String> user = (Map<String, String>) request.getSession().getAttribute(Constants.SESSION_WEB_USER);
		Map<String, String> map = new HashMap<String,String>();
		map.put("userId", user.get("id"));
		//map.put("userId", "5000300");
		map.put("page", "1");
		JSONObject json = new JSONObject();	
		json=mgmOrderService.getOrderList(map);	
		List<?> list = (List<?>) json.getJSONObject("data").getJSONArray("results");
		//mm.put("json", JSONObject.toJSONString(json, features));
		mm.put("list", list);
		mm.put("pageTotal", json.getJSONObject("data").get("pageTotal"));
		return "app/order/orderlist";	
	}
	
	@RequestMapping("getOrderListMore")	
	public String getOrderListMore(String page,ModelMap mm,HttpServletRequest request){
		Map<String, String> map = new HashMap<String,String>();
		Map<String, String> user = (Map<String, String>) request.getSession().getAttribute(Constants.SESSION_WEB_USER);
		map.put("userId", user.get("id"));
		map.put("page", page);
		JSONObject json = new JSONObject();	
		json=mgmOrderService.getOrderList(map);	
		List<?> list = (List<?>) json.getJSONObject("data").getJSONArray("results");
		//mm.put("json", JSONObject.toJSONString(json, features));
		mm.put("list", list);
		return "app/order/orderlistmore";	
	}
	
	
	//进度管理
	//订单进度详情
	@RequestMapping(value="getOrderProgressDeteails")	
	public String getOrderProgressDeteails(String orderId,String type,ModelMap mm,HttpServletRequest request) {
		Map<String, String> map = new HashMap<String,String>();
		JSONObject json = new JSONObject();	
		map.put("orderId", orderId);
		Map<String, String> user = (Map<String, String>) request.getSession().getAttribute(Constants.SESSION_WEB_USER);
		map.put("userId", user.get("id"));
		//map.put("userId", "5000300");
		json=this.mgmOrderScheduleService.getOrderProgressDeteails(map);	
		//mm.put("json", JSONObject.toJSONString(json, features));
		List<?> list = (List<?>) json.getJSONObject("data").getJSONArray("results");
		mm.put("orderState", json.getJSONObject("data").get("orderState"));
		mm.put("orderType", json.getJSONObject("data").get("orderType"));
		mm.put("modifyDate", json.getJSONObject("data").get("modifyDate"));
		//mm.put("json", JSONObject.toJSONString(json, features));
		mm.put("list", list);
		mm.put("userId", user.get("id"));
		json=mgmOrderService.orderDetailsCustomer(map);	
		Map<String,Object> maptemp = json.getJSONObject("data");
		mm.put("maptemp", maptemp);
		
		json=mgmOrderService.orderPruductDetails(map);	
		List<?> listproduct = (List<?>) json.getJSONObject("data").getJSONArray("results");
		mm.put("listproduct", listproduct);
		mm.put("pageTotal", json.getJSONObject("data").get("pageTotal"));
		mm.put("orderId", orderId);
		if(StringUtil.isNotBlank(type) && "recommend".equals(type)){
			return "app/order/recommendinfo";
		}
		return "app/order/orderinfo";
	}
	
	//管理
	//订单进度详情
	@RequestMapping(value="me_recommendinfo")	
	public String me_recommendinfo(String orderId,String type,ModelMap mm,HttpServletRequest request) {
		Map<String, String> map = new HashMap<String,String>();
		JSONObject json = new JSONObject();	
		map.put("orderId", orderId);
		Map<String, String> user = (Map<String, String>) request.getSession().getAttribute(Constants.SESSION_WEB_USER);
		map.put("userId", user.get("id"));
		//map.put("userId", "5000300");
		json=this.mgmOrderScheduleService.getOrderProgressDeteails(map);	
		//mm.put("json", JSONObject.toJSONString(json, features));
		List<?> list = (List<?>) json.getJSONObject("data").getJSONArray("results");
		mm.put("orderState", json.getJSONObject("data").get("orderState"));
		mm.put("orderType", json.getJSONObject("data").get("orderType"));
		mm.put("modifyDate", json.getJSONObject("data").get("modifyDate"));
		mm.put("orderBreak", json.getJSONObject("data").get("orderBreak"));
		//mm.put("json", JSONObject.toJSONString(json, features));
		mm.put("list", list);
		mm.put("userId", user.get("id"));
		json=mgmOrderService.orderDetailsCustomer(map);	
		Map<String,Object> maptemp = json.getJSONObject("data");
		mm.put("maptemp", maptemp);
		
		json=mgmOrderService.orderPruductDetails(map);	
		List<?> listproduct = (List<?>) json.getJSONObject("data").getJSONArray("results");
		mm.put("listproduct", listproduct);
		mm.put("pageTotal", json.getJSONObject("data").get("pageTotal"));
		mm.put("orderId", orderId);
		if(StringUtil.isNotBlank(type) && "recommend".equals(type)){
			return "app/me/me_recommendinfo";
		}
		return "app/me/me_recommendinfo";
	}
	
	@RequestMapping("terminated")	
	public String terminated(String orderId,ModelMap mm,HttpServletRequest request){
		Map<String, String> map = new HashMap<String,String>();
		mm.put("orderId", orderId);
		return "app/order/terminated";	
	}
	
	//orderTermination
	//订单终止申请
	@RequestMapping(value="orderTermination")	
	@ResponseBody
	public String orderTermination(String orderId,String apply_reason,ModelMap mm,HttpServletRequest request){
		Map<String, String> map = new HashMap<String,String>();
		map.put("orderId", orderId);
		map.put("apply_reason", apply_reason);
		Map<String, String> user = (Map<String, String>) request.getSession().getAttribute(Constants.SESSION_WEB_USER);
		map.put("userId", user.get("id"));
		JSONObject json = new JSONObject();	
		json=mgmOrderService.orderTermination(map);	
		//mm.put("json", JSONObject.toJSONString(json, features));
		return JSONObject.toJSONString(json, features);
	}
	///进度管理
	///订单详情--产品详情
	@RequestMapping(value="orderPruductDetails")	
	public String orderPruductDetails(String page,ModelMap mm){
		Map<String, String> map = new HashMap<String,String>();
		map.put("page", page);
		JSONObject json = new JSONObject();	
		json=mgmOrderService.orderPruductDetails(map);	
		List<?> list = (List<?>) json.getJSONObject("data").getJSONArray("results");
		//mm.put("json", JSONObject.toJSONString(json, features));
		return "app/order/orderpruductmore";	
	}	
	
	///进度管理
	///订单详情--进度管理--进度填报
	@RequestMapping(value="orderFillProgress")	
	@ResponseBody
	public String orderFillProgress(String orderId,String orderState ,ModelMap mm,HttpServletRequest request){
		Map<String, String> map = new HashMap<String,String>();
		JSONObject json = new JSONObject();	
		String[] friend_imgs = request.getParameterValues("friend_imgs");
		String presentationCondition = request.getParameter("presentationCondition");
		map.put("orderId", orderId);
		map.put("remark", presentationCondition);
		Map<String, String> user = (Map<String, String>) request.getSession().getAttribute(Constants.SESSION_WEB_USER);
		map.put("userId", user.get("id"));
		map.put("orderState", orderState);
		map.put("imgeUrl", this.arrayToString(friend_imgs));
		try{
			json = this.mgmOrderService.orderFillProgress(map);
		}catch(Exception e){
			json= getJson(0,"", "进度填报失败");
		}
		return JSONObject.toJSONString(json, features);
	}
	//签约signAcntract
	//签约
	@RequestMapping(value="signAcntract")	
	@ResponseBody
	public String signAcntract(String orderId,String orderState ,ModelMap mm,HttpServletRequest request){
		Map<String, String> map = new HashMap<String,String>();
		JSONObject json = new JSONObject();	
		String[] friend_imgs = request.getParameterValues("friend_imgs");
		String presentationCondition = request.getParameter("presentationCondition");
		Map<String, String> user = (Map<String, String>) request.getSession().getAttribute(Constants.SESSION_WEB_USER);
		map.put("userId", user.get("id"));
		map.put("orderId", orderId);
		map.put("remark", presentationCondition);
		map.put("orderState", orderState);
		
		String commissionMeans = request.getParameter("commissionMeans");//佣金计算方式
		String[] productId = request.getParameterValues("productId");//产品id
		String[] commissionRate = request.getParameterValues("commissionRate");//佣金比例（多产品用'，'分开）
		String[] commissionRateYc = request.getParameterValues("commissionRateYc");//溢出佣金比例（多产品用'，'分开）
		String[] numbers = request.getParameterValues("numbers");//认购数量，同产品id多产品数量直接用','
		String[] standardAmount = request.getParameterValues("standardAmount");//标准金额，同产品id多产品数量直接用','
		String[] volumeTransaction = request.getParameterValues("volumeTransaction");//成交金额，同产品id多产品数量直接用','
		String contractGrandTotal= request.getParameter("contractGrandTotal");//	合同总额
		
		map.put("commissionMeans", commissionMeans);
		map.put("productId", this.arrayToString(productId));
		map.put("commissionRate", this.arrayToString(commissionRate));
		map.put("commissionRateYc", this.arrayToString(commissionRateYc));
		map.put("numbers", this.arrayToString(numbers));
		map.put("standardAmount", this.arrayToString(standardAmount));
		map.put("volumeTransaction", this.arrayToString(volumeTransaction));
		map.put("contractGrandTotal", contractGrandTotal);
		map.put("imgeUrl", this.arrayToString(friend_imgs));
		try{
			json= this.mgmOrderService.signAcntract(map);
		}catch(Exception e){
			json= getJson(0,"", "签约失败");
		}
		//json=mgmOrderService.addMgmOrder(map);	
		//mm.put("json", JSONObject.toJSONString(json, features));
		
//		commissionMeans 佣金计算方式
//		productId 产品id
//      numbers 认购数量
//      standardAmount 标准金额
//      volumeTransaction 成交金额
//		contractGrandTotal 合同
//		userId 填报id
//		orderId 订单id
		
		return JSONObject.toJSONString(json, features);
	}
	///进度管理
	///订单详情--订单列表
	@RequestMapping(value="recommendlist")	
	public String orderList(String data,String customerId,ModelMap mm,HttpServletRequest request, String type){
		Map<String, String> map = new HashMap<String,String>();
		Map<String, String> user = (Map<String, String>) request.getSession().getAttribute(Constants.SESSION_WEB_USER);
		map.put("userId", user.get("id"));
		map.put("page", "1");
		map.put("customerId", customerId);
		JSONObject json = new JSONObject();	
		json=mgmOrderService.queryOrderList(map);	
		List<?> list = (List<?>) json.getJSONObject("data").getJSONArray("results");
		mm.put("list", list);
		//mm.put("json", JSONObject.toJSONString(json, features));
		mm.put("ty", type);
		return "app/order/recommendlist";
	}
	@RequestMapping("getRecommendlistMore")	
	public String getRecommendlistMore(String page,ModelMap mm,HttpServletRequest request){
		Map<String, String> map = new HashMap<String,String>();
		Map<String, String> user = (Map<String, String>) request.getSession().getAttribute(Constants.SESSION_WEB_USER);
		map.put("userId", user.get("id"));
		map.put("page", page);
		JSONObject json = new JSONObject();	
		json=mgmOrderService.queryOrderList(map);	
		List<?> list = (List<?>) json.getJSONObject("data").getJSONArray("results");
		//mm.put("json", JSONObject.toJSONString(json, features));
		mm.put("list", list);
		return "app/order/recommendlistmore";	
	}
	/**
	 * 数组转字符串，以逗号分割
	 * @param arrays
	 * @return
	 */
	public String arrayToString(String[] arrays){
		StringBuffer sb = new StringBuffer();
		if(arrays!=null && arrays.length>0){
		  for(int i = 0;i<arrays.length;i++){
			 sb.append(arrays[i]+ ",");       
		   }
		  return sb.toString().substring(0,sb.length()-1);
		}
		return sb.toString();
	}
}

