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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;

import org.cs.core.dao.impl.BaseDao;
import org.cs.util.Pager;
import org.cs.util.StringUtil;
import org.cs.mgr.product.model.MgmProductFollow;
import org.cs.mgr.product.dao.IMgmProductFollowDao;

/**
 * 
 * @ClassName: MgmProductFollowDao
 * @Description:  
 * @author: sunny_shi
 * @date: 2018-06-09 13:29:10
 */
@Repository
public class MgmProductFollowDao extends BaseDao<MgmProductFollow> implements IMgmProductFollowDao {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Override
	public Pager queryMgmProductFollows(Pager page){
		String hql = "from MgmProductFollow where isDelete = false";
		return findPageByHql(hql, page, null);
	}
	@Override
	public Pager queryPagerFollowList(Pager page, Map<String, String> map){
		Map<String, Object> params = new HashMap<>();
		params.put("isdelete", false);
		params.put("state", "1");
		StringBuffer hql = new StringBuffer("select mpf.id as \"id\", mp.product_name as \"productName\", mp.product_img as \"productImg\", mp.product_remark as \"productRemark\", mp.commission_rate as \"commissionRate\", mp.product_price as \"productPrice\", mp.id as \"productId\",  mp.gc as \"gc\" from mgm_product_follow mpf left join mgm_product mp on mp.id=mpf.product_id where mp.product_state=:state and mpf.is_delete =:isdelete");
		if(StringUtil.isNotBlank(map.get("userid"))) {
			hql.append(" and user_id=:userid");
			params.put("userid", map.get("userid"));
		}
		return findPageBySql(hql.toString(), page, params);
	}
	@Override
	public MgmProductFollow queryMagmProductFollow(Map<String, String> map) {
		StringBuffer hql = new StringBuffer("from MgmProductFollow where isDelete = false");
		Map<String, Object> params = new HashMap<>();
		if(StringUtil.isNotBlank(map.get("userid"))) {
			params.put("userid", Integer.valueOf(map.get("userid")));
			hql.append(" and userId=:userid");
		}
		if(StringUtil.isNotBlank(map.get("productid"))) {
			params.put("productid", Integer.valueOf(map.get("productid")));
			hql.append(" and productId=:productid");
		}
		return this.findOneByHql(hql.toString(), params);
	}
	
}
