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

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

import java.math.BigDecimal;
import java.util.HashMap;
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.mgr.order.service.IMgmOrderCommissionService;
import org.cs.mgr.order.service.IMgmOrderScheduleService;
import org.cs.mgr.user.model.MgmMessage;
import org.cs.mgr.order.dao.IMgmOrderCommissionDao;
import org.cs.mgr.order.dao.IMgmOrderScheduleDao;
import org.cs.mgr.order.model.MgmOrderCommission;
import org.cs.mgr.order.model.MgmOrderSchedule;

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

	@Autowired
	private IMgmOrderCommissionDao mgmOrderCommissionDao;
	
	@Override
	@Transactional(readOnly = true)
	public Pager getMgmOrderCommission(Pager page){
		return this.mgmOrderCommissionDao.queryMgmOrderCommission(page);
	}
	
	/**
	 * 自动生成
	 * @Title: delMgmOrderSchedule 
	 * @Description: TODO
	 * @param id						主键id
	 * @return
	 * @return: boolean
	 */
	@Override
	@Transactional(readOnly = false)
	public boolean delMgmOrderCommission(Integer id){
	
		MgmOrderCommission mgmOrderCommission = this.mgmOrderCommissionDao.findById(id);
		if(mgmOrderCommission != null){
			mgmOrderCommission.setIsDelete(true);				
			this.mgmOrderCommissionDao.update(mgmOrderCommission);
		}
		return true;
	}
	
	
	/**
	 * 自动生成
	 * @Title: addMgmOrderSchedule 
	 * @Description: TODO
	 * @param mgmOrderSchedule						对象
	 * @return
	 * @return: boolean
	 */
	@Override
	@Transactional(readOnly = false)
	public boolean addMgmOrderCommission(MgmOrderCommission mgmOrderCommission){
	
		if(mgmOrderCommission != null){
			this.mgmOrderCommissionDao.insert(mgmOrderCommission);
		}
		return true;
	}
	
	
	/**
	 *  自动生成
	 * @Title: updateMgmOrderSchedule 
	 * @Description: TODO
	 * @param mgmOrderSchedule						对象
	 * @return
	 * @return: boolean
	 */
	@Override
	@Transactional(readOnly = false)
	public boolean updateMgmOrderCommission(MgmOrderCommission mgmOrderCommission){
	
		MgmOrderCommission mgmOrderCommission2 = this.mgmOrderCommissionDao.findById(mgmOrderCommission.getId());
		if(mgmOrderCommission2 != null){
			mgmOrderCommission.mergeTo(mgmOrderCommission2);
			this.mgmOrderCommissionDao.update(mgmOrderCommission2);
		}
		return true;
	}
	/**
	 *  
	 * @Title: getMgmOrderCommission  
	 * @Description: 佣金记录
	 * @param: @param map
	 * @param: @return         
	 * @see org.cs.mgr.user.service.IMgmOrderCommissionService#getMgmOrderCommissionList(java.util.Map)
	 */
	@Override
	public JSONObject getMgmOrderCommissionList(Map<String,String> map){
		String userId=map.get("userId");
		Integer page = Integer.valueOf(map.get("page"));
		Map<String, Object> maps = new HashMap<String, Object>();
		Pager pages=this.mgmOrderCommissionDao.queryMgmOrderCommissionByUserId(new Pager(page),userId);
		
		BigDecimal sum=this.mgmOrderCommissionDao.getMgmOrderCommissionNum(Integer.parseInt(userId));
		
		maps.put("pageTotal", pages.getPageTotal());
		maps.put("pageSize", pages.getPageSize());
		maps.put("page", pages.getPage());
		maps.put("sum", sum);
		maps.put("results", pages.getResults());
		return getJson(1, maps, "获取佣金记录成功");
	}
	
	/**
	 * 
	 * @Title:    
	 * @Description:佣金记录列表
	 * @param: @param map
	 * @param: @return         
	 * @see  
	 */
	@Override
	public List<Map<String,Object>> getMgmOrderCommissionList(String userId){
		return this.mgmOrderCommissionDao.queryMgmOrderCommissionByUserId(userId); 
	} 
}
