package com.ssj.sys.controller;

import com.ssj.bean.weixin.libmy.domain.LibVip;
import com.ssj.bean.weixin.order.domain.Order;
import com.ssj.bean.weixin.sales.domain.LibOrderDetails;
import com.ssj.bean.weixin.sales.domain.LibSalesPrice;
import com.ssj.bean.weixin.sales.domain.LibStaffing;
import com.ssj.framework.basic.common.bean.SplitPage;
import com.ssj.framework.basic.utils.DateHelper;
import com.ssj.framework.core.common.controller.BaseController;
import com.ssj.framework.core.util.ResponseEntity;
import com.ssj.service.sys.sort.service.SortService;
import com.ssj.service.weixin.library.service.ILibJoinService;
import com.ssj.service.weixin.library.service.ILibVipService;
import com.ssj.service.weixin.order.service.OrderService;
import com.ssj.service.weixin.sales.service.LibOrderDetailsService;
import com.ssj.service.weixin.sales.service.LibSalesPriceService;
import com.ssj.service.weixin.sales.service.LibStaffingService;
import com.ssj.service.weixin.sales.service.LibVipServiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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 javax.servlet.http.HttpServletRequest;
import java.util.*;

/** 
 * @author  ZhangCaibao 
 * @version 2017年5月8日 上午9:34:52 
 * 类说明  订单管理
 */
@RequestMapping("sys/order")
@Controller
public class SysOrderController extends BaseController {

	@Autowired
	private OrderService orderService;
	@Autowired
	private LibOrderDetailsService libOrderDetailsService;
	
	@Autowired
	private SortService sortService;
	@Autowired
	private ILibJoinService libJoinService;
	@Autowired
	private LibVipServiceService libVipServiceService;
	@Autowired
	private LibStaffingService libStaffingService;
	@Autowired
	private LibSalesPriceService libSalesPriceService;
	@Autowired
    private ILibVipService vipService;
	
	
	/**
	 * 订单管理
	* @param model
	* @param pageSize
	* @param pageNo
	* @return
	* @throws Exception
	* 创建人：ZhangCaibao 
	* 2017年5月8日 上午9:36:49 
	* @String
	 */
	@RequestMapping("/orderList")
	public String orderList(Model model,
			@RequestParam(required = false, defaultValue = "10") int pageSize,
			@RequestParam(required = false, defaultValue = "1") int pageNo)
			throws Exception {
		
		Map<String, Object> params = new HashMap<String, Object>();
		 params.put("order_status", request.getParameter("order_status")==null?"":request.getParameter("order_status"));
		 params.put("order_type", request.getParameter("order_type")==null?"":request.getParameter("order_type"));
		 params.put("beginDate", request.getParameter("beginDate")==null?DateHelper.format(DateHelper.getTheMonthFirstDay(), "yyyy-MM-dd"):request.getParameter("beginDate"));
		 params.put("endDate", request.getParameter("endDate")==null?DateHelper.format(new Date(), "yyyy-MM-dd"):request.getParameter("endDate"));
		 params.put("order_no", request.getParameter("order_no")==null?"":request.getParameter("order_no"));
		SplitPage sp = new SplitPage();
	        sp.setAction(request.getRequestURI().toString());
	        sp.setPageNo(pageNo);
	        sp.setPageSize(pageSize);
	        sp.setParams(params);
	    Page<Map<String, Object>>  page= orderService.findForPageList(params, initPage(pageNo,pageSize));
		sp.setRowCnt((int) page.getTotalElements());
		model.addAttribute("list", page.getContent());
		model.addAttribute("listNavigatHtml", sp.getSysPaginHtml());
		model.addAttribute("page", sp);
		model.addAttribute("search", params);

		return "sys/order/orderList";
	}
	/**
	 * 销售VIP卡的订单记录
	* @param model
	* @param pageSize
	* @param pageNo
	* @return
	* @throws Exception
	* 创建人：ZhangCaibao 
	* 2017年5月8日 上午10:32:18 
	* @String
	 */
	@RequestMapping("/vipOrderList")
	public String vipOrderList(Model model,
			@RequestParam(required = false, defaultValue = "10") int pageSize,
			@RequestParam(required = false, defaultValue = "1") int pageNo)
					throws Exception {
		
		Map<String, Object> params = new HashMap<String, Object>();
		
		 params.put("beginDate", request.getParameter("beginDate")==null? DateHelper.format(DateHelper.getTheMonthFirstDay(), "yyyy-MM-dd"):request.getParameter("beginDate"));
		 params.put("endDate", request.getParameter("endDate")==null?DateHelper.format(new Date(), "yyyy-MM-dd"):request.getParameter("endDate"));
		
		params.put("libId", request.getParameter("libId")==null?"":request.getParameter("libId"));
		params.put("libtype", request.getParameter("libtype")==null?"":request.getParameter("libtype"));
		params.put("order_no", request.getParameter("order_no")==null?"":request.getParameter("order_no"));
		if(!"admin".equals(loginName())){
			params.put("orgCode",orgCodeStr());
		}
		SplitPage sp = new SplitPage();
		sp.setAction(request.getRequestURI().toString());
		sp.setPageNo(pageNo);
		sp.setPageSize(pageSize);
		sp.setParams(params);
		Page<Map<String, Object>>  page= libOrderDetailsService.findForPageList(params, initPage(pageNo,pageSize));
		sp.setRowCnt((int) page.getTotalElements());
		model.addAttribute("list", page.getContent());
		model.addAttribute("listNavigatHtml", sp.getSysPaginHtml());
		model.addAttribute("page", sp);
		model.addAttribute("search", params);
		model.addAttribute("libList",libJoinService.getLibJoinByCode(orgcodeList()));
		return "sys/order/vipOrderList";
	}
	
	@RequestMapping("/addLineOrder")
	public String lineOrder(Model model)throws Exception {
		model.addAttribute("libList",libJoinService.getNoLocationJoin(1));
		return "sys/order/addLineOrder";
	}
	
	 /**
	    * 线下购卡，财务手动添加order
	    * @param request
		* @param model
		* @return
		* 创建人：wuwen 
		* 2017年8月28日14:05:15
		* @ResponseEntity
	    */
	@RequestMapping(value="/saveLineOrderDetails",method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity saveLineOrderDetails(HttpServletRequest request, Model model, LibOrderDetails libOrderDetails, String code){
		ResponseEntity response = new ResponseEntity();
		Date date = new Date();
		logger.info("从财务手动添加添加销售订单--"+DateHelper.getDateTime(date));
		
		LibVip libVip =  vipService.findByCode(code);
		if(libVip==null){
			response.failure("会员卡号不存在！");
			return response;
		}
		String orderNo = "ssj"+System.currentTimeMillis();//生成订单号//生成订单保存到数据库
		Order o = new Order();
		o.setOrderMoney(libOrderDetails.getPrice());
		o.setOrderNo(orderNo);
		o.setOrderStatus(1);
		o.setPayTime(new Date());
		o.setCreateTime(new Date());
		o.setIsClosed(0);
		o.setOrderType(4);
		o=orderService.save(o);
		
		libOrderDetails.setOrderId(o.getId());
		libOrderDetails.setOrderNo(orderNo);
		libOrderDetails.setVipId(libVip.getId());	
		libOrderDetails.setCreateTime(date);
		libOrderDetails.setOrderStatus(3);
		libOrderDetails.setIsBuy(2);
		Integer count=libOrderDetailsService.getUserHistory(libOrderDetails.getSalesPriceId(),libOrderDetails.getVipId() ,libOrderDetails.getLibId(),libOrderDetails.getPriceSpecies());
		libOrderDetails.setType(count);
		if("C001".equals(libOrderDetails.getPriceType())){
			libOrderDetails.setEndTime(DateHelper.getMonthDate(date, 12));
		}else if("C002".equals(libOrderDetails.getPriceType())){
			libOrderDetails.setEndTime(DateHelper.getMonthDate(date, 6));
		}else if("C003".equals(libOrderDetails.getPriceType())){
			libOrderDetails.setEndTime(DateHelper.getMonthDate(date, 1));
		}else if("C004".equals(libOrderDetails.getPriceType())){
			libOrderDetails.setEndTime(DateHelper.getMonthDate(date, 1));
		}else if("C005".equals(libOrderDetails.getPriceType())){
			libOrderDetails.setEndTime(DateHelper.getMonthDate(date, 6));
		}else if("C006".equals(libOrderDetails.getPriceType())){
			libOrderDetails.setEndTime(DateHelper.getMonthDate(date, 12));
		}else if("C007".equals(libOrderDetails.getPriceType())){
			libOrderDetails.setEndTime(DateHelper.getMonthDate(date, 12));
		}
		libOrderDetailsService.save(libOrderDetails);
		logger.info("从财务手动添加销售订单--"+libOrderDetails.getId());
		
		//插入服务
		if(libOrderDetails.getVipId() !=null){
			libVipServiceService.saveLibVipService(libOrderDetails.getLibId(), libOrderDetails.getPriceSpecies(), libOrderDetails.getPriceType(), libOrderDetails.getVipId(), libOrderDetails.getCreateTime(), libOrderDetails.getEndTime(),libOrderDetails.getId());
		}
		Map<String, Object> data = new  HashMap<String, Object>();
		data.put("id", libOrderDetails.getId());
		response.success(data,"获取成功");
		return response;
	}
	
	
	@RequestMapping(value="/libStaffingList",method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity libStaffingList(HttpServletRequest request, Model model,String libId){
		ResponseEntity response = new ResponseEntity();
		List<LibStaffing> staffingList =libStaffingService.findByLibId(libId);
		List<Map<String,Object>> staffingMap=new ArrayList<Map<String,Object>>();
		Map<String,Object> map=null;
		for (LibStaffing libStaffing : staffingList) {
			map=new HashMap<String, Object>();
			map.put("id", libStaffing.getId());
			map.put("userId", libStaffing.getUserId());
			map.put("name", libStaffing.getName());
			map.put("type", libStaffing.getType());
			staffingMap.add(map);
		}
		Map<String, Object> data = new  HashMap<String, Object>();
		data.put("data",staffingMap);
		response.success(data,"获取成功");
		return response;
	}
	
	@RequestMapping(value="/salesPriceList",method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity salesPriceList(HttpServletRequest request, Model model,String libId){
		ResponseEntity response = new ResponseEntity();
		List<LibSalesPrice> salesPriceList =libSalesPriceService.findByLibId(libId);
		List<Map<String,Object>> salesPriceMap=new ArrayList<Map<String,Object>>();
		Map<String,Object> map=null;
		for (LibSalesPrice libSalesPrice : salesPriceList) {
			map=new HashMap<String, Object>();
			map.put("id", libSalesPrice.getId());
			map.put("species", libSalesPrice.getSpecies());
			map.put("type", libSalesPrice.getType());
			map.put("price", libSalesPrice.getPrice());
			map.put("discount", libSalesPrice.getDiscount());
			map.put("name", libSalesPrice.getName());
			map.put("aliases", libSalesPrice.getAliases());
			
			salesPriceMap.add(map);
		}
		Map<String, Object> data = new  HashMap<String, Object>();
		data.put("data",salesPriceMap);
		response.success(data,"获取成功");
		return response;
	}
	
	
	
	
	
	/**
	 * 一对一订单管理
	* @param model
	* @param pageSize
	* @param pageNo
	* @return
	* @throws Exception
	* 创建人：wuwen 
	* 2018年7月3日16:02:34
	* @String
	 */
	@RequestMapping("/lessonOrderList")
	public String lessonOrderList(Model model,
			@RequestParam(required = false, defaultValue = "10") int pageSize,
			@RequestParam(required = false, defaultValue = "1") int pageNo)
			throws Exception {
		
		Map<String, Object> params = new HashMap<String, Object>();
		 params.put("order_status", request.getParameter("order_status")==null?"":request.getParameter("order_status"));
		 
		 params.put("beginDate", request.getParameter("beginDate")==null?DateHelper.format(DateHelper.getTheMonthFirstDay(), "yyyy-MM-dd"):request.getParameter("beginDate"));
		 params.put("endDate", request.getParameter("endDate")==null?DateHelper.format(new Date(), "yyyy-MM-dd"):request.getParameter("endDate"));
		
		 params.put("order_no", request.getParameter("order_no")==null?"":request.getParameter("order_no"));
		SplitPage sp = new SplitPage();
	        sp.setAction(request.getRequestURI().toString());
	        sp.setPageNo(pageNo);
	        sp.setPageSize(pageSize);
	        sp.setParams(params);
	    Page<Map<String, Object>>  page= orderService.findForPageLessonServiceList(params, initPage(pageNo,pageSize));
		sp.setRowCnt((int) page.getTotalElements());
		model.addAttribute("list", page.getContent());
		model.addAttribute("listNavigatHtml", sp.getSysPaginHtml());
		model.addAttribute("page", sp);
		model.addAttribute("search", params);

		return "sys/order/lessonOrderList";
	}
	
	
	/**
	 * 一对一上课明细的订单记录
	* @param model
	* @param pageSize
	* @param pageNo
	* @return
	* @throws Exception
	* 创建人：wuwen 
	* 2018年7月3日16:02:24
	* @String
	 */
	@RequestMapping("/lessonCourseOrderList")
	public String lessonCourseOrderList(Model model,
			@RequestParam(required = false, defaultValue = "10") int pageSize,
			@RequestParam(required = false, defaultValue = "1") int pageNo)
					throws Exception {
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("beginDate", request.getParameter("beginDate")==null?DateHelper.format(DateHelper.getTheMonthFirstDay(), "yyyy-MM-dd"):request.getParameter("beginDate"));
		 params.put("endDate", request.getParameter("endDate")==null?DateHelper.format(new Date(), "yyyy-MM-dd"):request.getParameter("endDate"));
		
		params.put("libId", request.getParameter("libId")==null?"":request.getParameter("libId"));
		params.put("order_no", request.getParameter("order_no")==null?"":request.getParameter("order_no"));
		
		SplitPage sp = new SplitPage();
		sp.setAction(request.getRequestURI());
		sp.setPageNo(pageNo);
		sp.setPageSize(pageSize);
		sp.setParams(params);
		Page<Map<String, Object>>  page= orderService.findForPageLessonServiceByCourseList(params, initPage(pageNo,pageSize));
		sp.setRowCnt((int) page.getTotalElements());
		model.addAttribute("list", page.getContent());
		model.addAttribute("listNavigatHtml", sp.getSysPaginHtml());
		model.addAttribute("page", sp);
		model.addAttribute("search", params);
		model.addAttribute("libList",libJoinService.getLibJoinByCode(orgcodeList()));
		return "sys/order/lessonCourseOrderList";
	}
	
	
	
	
	
	
	
}
