package org.cs.mgr.order.ctl;

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

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Map;

import org.cs.core.ctl.BaseCtl;
import org.cs.util.DateUtil;
import org.cs.util.FastDFSClient;
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.util.PropUtil;
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
 */
@RestController
@RequestMapping("/rest/mgmOrder")
public class MgmOrderApi extends BaseCtl {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private IMgmOrderService mgmOrderService;	
	
	@Autowired
	private IMgmCustomerService mgmCustomerService;
	
	@Autowired
	private IMgmOrderScheduleService mgmOrderScheduleService;
	
	
	///客户列表
	@RequestMapping(value="getCustomerList")	
	public String getCustomerList(String data,ModelMap mm){
		Map<String, String> map = getMapFromJson(data);
		JSONObject json = new JSONObject();	
		String userId= map.get("userId");
		String pageNo=map.get("pageNo");
		if(StringUtil.isBlank(pageNo)) {
			pageNo="1";
		}
		json= this.mgmCustomerService.queryMyMgmCustomer(new Pager(Integer.valueOf(pageNo)), Integer.valueOf(userId));
		//mm.put("json", JSONObject.toJSONString(json, features));		
		return JSONObject.toJSONString(json, features);
		
	}
		
	///马上推荐
	@RequestMapping(value="immRecommended")	
	public String immRecommended(String data,ModelMap mm){
		Map<String, String> map = getMapFromJson(data);
		JSONObject json = new JSONObject();	
		String customerId = map.get("customerId");
		
		//客户信息判断
		if(StringUtil.isBlank(customerId)){
			map.put("recommendId", map.get("userId"));
			JSONObject objj = this.mgmCustomerService.addMgmCustomer(map);
			System.out.print("--ret--"+objj.get("ret").toString());
			if("1".equals(objj.get("ret").toString())){
				map.put("customerId", objj.getString("data"));
			}else{
				return JSONObject.toJSONString(json, features);
			}
		}else{
			map.put("id", customerId);
			this.mgmCustomerService.updateMgmCustomer(map);
		}
		
		
		json=mgmOrderService.addMgmOrder(map);	
		//mm.put("json", JSONObject.toJSONString(json, features));
		return JSONObject.toJSONString(json, features);
	}
	
    	
	///进度管理
	///订单详情--订单列表
	@RequestMapping(value="getOrderList")	
	public String getOrderList(String data,ModelMap mm){
		Map<String, String> map = getMapFromJson(data);
		JSONObject json = new JSONObject();	
		json=mgmOrderService.getOrderList(map);	
		//mm.put("json", JSONObject.toJSONString(json, features));
		return JSONObject.toJSONString(json, features);
	}
	
	
	//进度管理
	//订单进度详情
	@RequestMapping(value="getOrderProgressDeteails")	
	public String getOrderProgressDeteails(String data,ModelMap mm) {
		Map<String, String> map = getMapFromJson(data);
		JSONObject json = new JSONObject();	
		json=this.mgmOrderScheduleService.getOrderProgressDeteails(map);	
		//mm.put("json", JSONObject.toJSONString(json, features));
		return JSONObject.toJSONString(json, features);
	}
	
	
	///进度管理
	///订单详情--客户详情
	@RequestMapping(value="orderDetailsCustomer")	
	public String orderDetailsCustomer(String data,ModelMap mm){
		Map<String, String> map = getMapFromJson(data);
		JSONObject json = new JSONObject();	
		json=mgmOrderService.orderDetailsCustomer(map);	
		//mm.put("json", JSONObject.toJSONString(json, features));
		return JSONObject.toJSONString(json, features);
	}
	
			
	//orderTermination
	//订单终止申请
	@RequestMapping(value="orderTermination")	
	public String orderTermination(String data,ModelMap mm){
		Map<String, String> map = getMapFromJson(data);
		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 data,ModelMap mm){
		Map<String, String> map = getMapFromJson(data);
		JSONObject json = new JSONObject();	
		json=mgmOrderService.orderPruductDetails(map);	
		//mm.put("json", JSONObject.toJSONString(json, features));
		return JSONObject.toJSONString(json, features);
	}	
	
	
	
	///进度填报理
	///订单详情--进度管理--进度填报
	@RequestMapping(value="orderFillProgress")	
	public String orderFillProgress(String data,ModelMap mm,@RequestParam(required = false) MultipartFile[] files){
		Map<String, String> map = getMapFromJson(data);
		JSONObject json = new JSONObject();			
		String imgeUrl="";
		for(MultipartFile file:files ) {
			if(file!=null) {
				try {
					String filename= file.getOriginalFilename();
					String tmUrl=FastDFSClient.uploadFileStreanm((FileInputStream) file.getInputStream(), filename);
					if(StringUtil.isNotBlank(tmUrl)) {
						if(StringUtil.isBlank(imgeUrl)) {
							imgeUrl=PropUtil.getString("file_http")+tmUrl;
						}else {
							imgeUrl+=","+PropUtil.getString("file_http")+tmUrl;
						}
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				//FastDFSClient.uploadFileStreanm(fileInputStream, fileName)
			}
		}
		map.put("imgeUrl", imgeUrl);
		json = this.mgmOrderService.orderFillProgress(map);
		return JSONObject.toJSONString(json, features);
	}
	
	//签约signAcntract
	//签约
	@RequestMapping(value="signAcntract")	
	public String signAcntract(String data,ModelMap mm,@RequestParam(required = false) MultipartFile[] files){
		Map<String, String> map = getMapFromJson(data);
		JSONObject json = new JSONObject();	
		String imgeUrl="";
		for(MultipartFile file:files ) {
			if(file!=null) {
				try {
					String filename= file.getOriginalFilename();
					String tmUrl=FastDFSClient.uploadFileStreanm((FileInputStream) file.getInputStream(), filename);
					if(StringUtil.isNotBlank(tmUrl)) {
						if(StringUtil.isBlank(imgeUrl)) {
							imgeUrl=PropUtil.getString("file_http")+tmUrl;
						}else {
							imgeUrl+=","+PropUtil.getString("file_http")+tmUrl;
						}
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				//FastDFSClient.uploadFileStreanm(fileInputStream, fileName)
			}
				
		}
		
		map.put("imgeUrl", imgeUrl);
		json= this.mgmOrderService.signAcntract(map);
		return JSONObject.toJSONString(json, features);
	}
	
	///进度管理
	///订单详情--订单列表
	@RequestMapping(value="orderList")	
	@ResponseBody
	public String orderList(String data,ModelMap mm){
		Map<String, String> map = getMapFromJson(data);
		if(StringUtil.isBlank(map.get("userId"))){
			return JSONObject.toJSONString(getJson(0,"", "未获取到用户信息"), features);
		}
		JSONObject json = new JSONObject();	
		
		json=mgmOrderService.queryOrderList(map);	
		//mm.put("json", JSONObject.toJSONString(json, features));
		return JSONObject.toJSONString(json, features);
	}
	
	
}

