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

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.cs.core.dao.impl.BaseDao;
import org.cs.mgr.order.dao.IMgmOrderCommissionDao;
import org.cs.mgr.order.model.MgmOrderCommission;
import org.cs.mgr.user.model.MgmMessage;
import org.cs.mgr.user.model.MgmUser;
import org.cs.util.Pager;
import org.cs.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

/**
 * 
 * @ClassName: MgmOrderProductDao
 * @Description:  
 * @author: sunny_shi
 * @date: 2018-06-09 13:29:03
 */
@Repository
public class MgmOrderCommissionDao extends BaseDao<MgmOrderCommission> implements IMgmOrderCommissionDao {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Override
	public Pager queryMgmOrderCommission(Pager page){
		String hql = "from MgmOrderCommission where isDelete = false";
		return findPageByHql(hql, page, null);
	}
	
	@Override
	public MgmOrderCommission getByPropertie(String value,String entry) {
		Map<String, Object> params = new HashMap<>();
		if(entry.equals("orderId")) {
			
			params.put("value", Integer.valueOf(value));
		}else {
			params.put("value", value);
		}
		
		return this.findOneByHql("from MgmOrderCommission where "+entry+"=:value", params);
	}
	
	@Override
	public Pager queryMgmOrderCommissionByUserId(Pager page,String userId){
		//String hql = "from MgmOrderCommission where isDelete = false and state=1 ";
		StringBuilder sql = new StringBuilder("select t.id as \"id\",t.commission as \"commission\",t.finish_time as \"finish_time\",mo.customer_name as \"customer_name\",mo.id as \"order_id\",mo.order_type as \"orderType\"" +
				" from MGM_ORDER_COMMISSION t left join mgm_order mo on t.order_id=mo.id where t.is_delete=0 and t.state=1");
		if(StringUtil.isNotBlank(userId)){
			sql.append("and t.user_id="+userId);
		}
		return findPageBySql(sql.toString(), page, null);
	}
	
	//根绝用户id获取所有佣金
	@Override
	public BigDecimal getMgmOrderCommissionNum(Integer userId){
		String sql = "select  to_char(SUM(commission),'FM9999999999999999.00') as sum  from mgm_order_commission where state=1 and user_Id='"+userId+"'";
		List<Map<String, Object>> list=findBySql(sql);
		if(list!=null){ 
			Map<String, Object> map=list.get(0);
			
			String sum=map.get("sum")==null?"0":map.get("sum").toString();
			return new BigDecimal(sum).setScale(2,   BigDecimal.ROUND_HALF_UP);
		}
		return  new BigDecimal("0");
	}
	
	@Override
	public List<Map<String,Object>> queryMgmOrderCommissionByUserId(String userId){
		StringBuilder sql = new StringBuilder("select t.id as \"id\",t.commission as \"commission\",t.finish_time as \"finish_time\",mo.customer_name as \"customer_name\",mo.id as \"order_id\",mo.order_type as \"orderType\"" +
				" from MGM_ORDER_COMMISSION t left join mgm_order mo on t.order_id=mo.id where t.is_delete=0 and t.state=1");
		if(StringUtil.isNotBlank(userId)){
			sql.append("and t.user_id="+userId);
		}
		sql.append(" order by t.CREATE_DATE desc");
		return this.findBySql(sql.toString());
	}
}
