package org.cs.mgr.order.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Map;
import org.cs.core.service.impl.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;

import org.cs.util.Pager;
import org.cs.util.PropUtil;
import org.cs.util.StringUtil;
import org.cs.mgr.order.service.IMgmOrderScheduleService;
import org.cs.mgr.order.dao.IMgmOrderBreakDao;
import org.cs.mgr.order.dao.IMgmOrderDao;
import org.cs.mgr.order.dao.IMgmOrderScheduleDao;
import org.cs.mgr.order.model.MgmOrder;
import org.cs.mgr.order.model.MgmOrderBreak;
import org.cs.mgr.order.model.MgmOrderSchedule;

/**
 * 
 * @ClassName: IMgmOrderScheduleService
 * @Description:  
 * @author: sunny_shi
 * @date: 2018-06-09 13:29:03
 */
@Service("mgmOrderScheduleService")
public class MgmOrderScheduleService extends BaseService<MgmOrderSchedule> implements IMgmOrderScheduleService{
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private IMgmOrderScheduleDao mgmOrderScheduleDao;
	
	@Autowired
	private IMgmOrderDao mgmOrderDao;
	
	
	@Autowired
	private  IMgmOrderBreakDao mgmOrderBreakDao;
	
	
	
	
	@Override
	@Transactional(readOnly = true)
	public Pager getMgmOrderSchedules(Pager page){
		return this.mgmOrderScheduleDao.queryMgmOrderSchedules(page);
	}
	
	/**
	 * 自动生成
	 * @Title: delMgmOrderSchedule 
	 * @Description: TODO
	 * @param id						主键id
	 * @return
	 * @return: boolean
	 */
	@Override
	@Transactional(readOnly = false)
	public boolean delMgmOrderSchedule(Integer id){
	
		MgmOrderSchedule mgmOrderSchedule = this.mgmOrderScheduleDao.findById(id);
		if(mgmOrderSchedule != null){
			mgmOrderSchedule.setIsDelete(true);				
			this.mgmOrderScheduleDao.update(mgmOrderSchedule);
		}
		return true;
	}
	
	
	/**
	 * 自动生成
	 * @Title: addMgmOrderSchedule 
	 * @Description: TODO
	 * @param mgmOrderSchedule						对象
	 * @return
	 * @return: boolean
	 */
	@Override
	@Transactional(readOnly = false)
	public boolean addMgmOrderSchedule(MgmOrderSchedule mgmOrderSchedule){
	
		if(mgmOrderSchedule != null){
			this.mgmOrderScheduleDao.insert(mgmOrderSchedule);
		}
		return true;
	}
	
	
	/**
	 *  自动生成
	 * @Title: updateMgmOrderSchedule 
	 * @Description: TODO
	 * @param mgmOrderSchedule						对象
	 * @return
	 * @return: boolean
	 */
	@Override
	@Transactional(readOnly = false)
	public boolean updateMgmOrderSchedule(MgmOrderSchedule mgmOrderSchedule){
	
		MgmOrderSchedule mgmOrderSchedule2 = this.mgmOrderScheduleDao.findById(mgmOrderSchedule.getId());
		if(mgmOrderSchedule2 != null){
			mgmOrderSchedule.mergeTo(mgmOrderSchedule2);
			this.mgmOrderScheduleDao.update(mgmOrderSchedule2);
		}
		
		return true;
	}

	//订单进度详情
	@Override
	public JSONObject getOrderProgressDeteails(Map<String, String> map) {
		// TODO Auto-generated method stub
		String orderId = map.get("orderId");
		String userId = map.get("userId");
		JSONObject json = new JSONObject();	
		Integer pageNo =1;
		if(StringUtil.isNotBlank(map.get("pageNo"))) {			
			pageNo = Integer.parseInt(map.get("pageNo"));
		}		
		MgmOrder order = this.mgmOrderDao.findById(Integer.valueOf(orderId)) ;
		map.put("type", order.getOrderType());
		map.put("orderState", order.getOrderState());
		
		///订单终止
		if(StringUtil.isNotBlank(order.getBreakState())) {
			map.put("orderBreak", "1");
			map.put("applyState", order.getBreakState());
		}else {
			map.put("orderBreak", "0");
		}
		
		
//		///终止请状态
//		MgmOrderBreak mgmOrderBreak= null;
//		Pager page =this.mgmOrderBreakDao.queryMgmOrderBreaksById(new Pager(1), map);
//		if(page.getTotal()>0) {			
//			mgmOrderBreak= (MgmOrderBreak) page.getResults().get(0);
//			if(mgmOrderBreak!=null) {
//				map.put("orderBreak", "1");
//				map.put("applyState", mgmOrderBreak.getApplyState());
//			}else {
//				map.put("orderBreak", "0");
//			}
//		}else {
//			map.put("orderBreak", "0");
//		}
		
		
		if(order.getSalerId()!=null){
			String sid = String.valueOf(order.getSalerId());
			if(StringUtil.isNotBlank(userId)) {
				if(userId.equals(sid)) {				
					map.put("fillflg", "1");
				}else {
					map.put("fillflg", "0");
				}
			}else {
				map.put("fillflg", "0");
			}
			
		}else {
			map.put("fillflg", "0");
		}
		
		
		
		if(StringUtil.isNotBlank(order.getOrderState())) {		
				Integer fillcode = Integer.valueOf(order.getOrderState()).intValue()+1;				
				if(order.getOrderType().equals("1")) {
					
					if(fillcode.equals(1)||  fillcode.equals(6)) {
						map.put("fillcode", "-1");
					}else{					
						map.put("fillcode", String.valueOf(fillcode));
					}
				}else {
					if(fillcode.equals(1)||  fillcode.equals(3)) {
						map.put("fillcode", "-1");
					}else{					
						map.put("fillcode", String.valueOf(fillcode));
					}
				}
			
		}else {
			map.put("fillcode", "-1");
		}
		
		
		if(StringUtil.isNotBlank(orderId)) {
			Pager pager = this.mgmOrderScheduleDao.queryMgmOrderSchedulesByOrderId(new Pager(pageNo), map);

			json.put("results", pager.getResults());
			json.put("total", pager.getTotal());
			json.put("page", pager.getPage());
			json.put("start", pager.getStart());
			json.put("pageSize", pager.getPageSize());
			json.put("end", pager.getEnd());
//			json.put("orderState", order.getOrderState());
			json.put("modifyDate", order.getModifyDate());
			json.put("orderState", StringUtil.converNull(order.getOrderState()));
			json.put("orderType", StringUtil.converNull(order.getOrderType()));
			json.put("orderBreak", StringUtil.converNull(order.getBreakState()));
			return getJson(1,json, "获取进度列表成功");
		}else {
			return getJson(0,"", "订单不存在");
		}
	}
	
	
	
}
