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

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

import com.mysql.fabric.xmlrpc.base.Array;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
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.MgmProduct;
import org.cs.mgr.product.dao.IMgmProductDao;

/**
 * 
 * @ClassName: MgmProductDao
 * @Description:  
 * @author: sunny_shi
 * @date: 2018-06-09 13:29:09
 */
@Repository
public class MgmProductDao extends BaseDao<MgmProduct> implements IMgmProductDao {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Override
	public Pager queryMgmProducts(Pager page){
		String hql = "from MgmProduct where isDelete = false";
		return findPageByHql(hql, page, null);
	}
	
	@Override
	public Pager queryMgmProducts(Pager page, Map<String, String> map){
		StringBuffer hql = new StringBuffer("from MgmProduct where isDelete = false");
		Map<String, Object> params = new HashMap<>();
		if(StringUtil.isNotBlank(map.get("oneid")) && !"0".equals(map.get("oneid"))){
			params.put("oneid", Integer.valueOf(map.get("oneid")));
			hql.append(" and gcId=:oneid");
		}else{
			if(StringUtil.isNotBlank(map.get("gcId")) && !"0".equals(map.get("gcId"))){
				params.put("oneid", Integer.valueOf(map.get("gcId")));
				hql.append(" and gcId=:oneid");
			}
		}
		if(StringUtil.isNotBlank(map.get("twoid")) && !"0".equals(map.get("twoid"))){
			params.put("twoid", Integer.valueOf(map.get("twoid")));
			hql.append(" and gcParentId=:twoid");
		}else{
			if(StringUtil.isNotBlank(map.get("gcParentId")) && !"0".equals(map.get("gcParentId"))){
				params.put("twoid", Integer.valueOf(map.get("gcParentId")));
				hql.append(" and gcParentId=:twoid");
			}
		}
		if(StringUtil.isNotBlank(map.get("threeid")) && !"0".equals(map.get("threeid"))){
			params.put("threeid", Integer.valueOf(map.get("threeid")));
			hql.append(" and gcThreeId=:threeid");
		}else{
			if(StringUtil.isNotBlank(map.get("gcThreeId")) && !"0".equals(map.get("gcThreeId"))){
				params.put("threeid", Integer.valueOf(map.get("gcThreeId")));
				hql.append(" and gcThreeId=:threeid");
			}
		}
		if(StringUtil.isNotBlank(map.get("fourid")) && !"0".equals(map.get("fourid"))){
			params.put("fourid", Integer.valueOf(map.get("fourid")));
			hql.append(" and gcFourId=:fourid");
		}else{
			if(StringUtil.isNotBlank(map.get("gcFourId")) && !"0".equals(map.get("gcFourId"))){
				params.put("fourid", Integer.valueOf(map.get("gcFourId")));
				hql.append(" and gcFourId=:fourid");
			}
		}
		
		if(StringUtil.isNotBlank(map.get("name"))){
			params.put("name", "%"+map.get("name")+"%");
			hql.append(" and productName like :name");
		}
		if(StringUtil.isNotBlank(map.get("productState"))){
			params.put("productState", map.get("productState"));
			hql.append(" and productState=:productState");
		}
		if(StringUtil.isNotBlank(map.get("hotline"))){
			params.put("hotline","%"+ map.get("hotline")+"%");
			hql.append(" and hotline like :hotline");
		}
		if(StringUtil.isNotBlank(map.get("isHome"))){
			params.put("isHome", map.get("isHome"));
			hql.append(" and isHome=:isHome");
		}
		if(StringUtil.isNotBlank(map.get("xmCode"))){
			params.put("xmCode", map.get("xmCode"));
			hql.append(" and xmCode=:xmCode");
		}
		return findPageByHql(hql.toString(), page, params);
	}
	
	@Override
	public Pager queryProductPager(Pager page, Map<String, String> map){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("isdelete", false);
		params.put("state", "1");
		StringBuffer sb = new StringBuffer("from MgmProduct where productState=:state and isDelete = :isdelete");
		String type = map.get("type");
		String categoryid = map.get("categoryid");
		String num = map.get("num");
		String isHome = map.get("isHome");
		
		///非集团用户只能看公开产品
		String userType = map.get("userType");
		if(StringUtil.isNotBlank(userType)){
			if("1".equals(userType)){				
				params.put("openFlag", true);
				sb.append(" and openFlag=:openFlag");
			}			
		}
		
		
		if(StringUtil.isNotBlank(type)){
			if("1".equals(type)){
				if ("0".equals(num)) {
					params.put("type", type);
					sb.append(" and productType=:type");
				}else if("1".equals(num)){
					params.put("type", type);
					params.put("categoryid", categoryid);
					sb.append(" and productType=:type and xmCode=:categoryid");
				}
				if(StringUtil.isNotBlank(map.get("cityCode"))){
					params.put("citycode", map.get("cityCode"));
					sb.append(" and cityCode=:citycode");
				}
				if(StringUtil.isNotBlank(map.get("city"))){
					params.put("city", "%"+map.get("city")+"%");
					sb.append(" and (city like :city or province like :city)");
				}
				if(StringUtil.isNotBlank(isHome)){
					sb.append(" and is_home = :isHome");
					params.put("isHome", isHome);
				}
			}else if("2".equals(type)){
				if ("0".equals(num)){
					params.put("type", type);
					params.put("categoryid", Integer.valueOf(categoryid));
					sb.append(" and productType=:type and gcId=:categoryid");
				}else if ("1".equals(num)){
					params.put("type", type);
					params.put("categoryid", Integer.valueOf(categoryid));
					sb.append(" and productType=:type and gcParentId=:categoryid");
				}else if ("2".equals(num)){
					params.put("type", type);
					params.put("categoryid", Integer.valueOf(categoryid));
					sb.append(" and productType=:type and gcThreeId=:categoryid");
				}else if ("3".equals(num)){
					params.put("type", type);
					params.put("categoryid", Integer.valueOf(categoryid));
					sb.append(" and productType=:type and gcFourId=:categoryid");
				}
				if(StringUtil.isNotBlank(isHome)){
					sb.append(" and is_home = :isHome");
					params.put("isHome", isHome);
				}
			}
		}
		
		sb.append(" order by isHome desc, weight asc, createDate desc");
		return this.findPageByHql(sb.toString(), page, params) ;
	}
	
	@Override
	 public Map<String, Object> queryProductDetail(Map<String, String> map){
		StringBuffer sb = new StringBuffer("select decode(mpf.id, null , 0, 1) as \"isFollow\", mp.id as \"id\", mp.product_img as \"productImg\", mp.product_group_img as \"product_group_img\", mp.product_name as \"productName\", mp.product_price as \"productPrice\", mp.product_remark as \"productRemark\", mp.commission_rate as \"commissionRate\", mp.unit as \"unit\", mp.hotline as \"hotline\", mp.province as \"province\", mp.city as \"city\", mp.product_body as \"productBody\", mp.product_attr as \"productAttr\", mp.policy as \"policy\", mp.product_state as \"productState\", mp.product_type as \"productType\" from mgm_product mp left join mgm_product_follow mpf on mpf.product_id=mp.id ");
		List<Map<String, Object>> a = new ArrayList<>();
		if(StringUtil.isBlank(map.get("userid"))){
			sb.append(" where mp.id=? ");
			a = this.findBySql(sb.toString(), new Object[]{Integer.valueOf(map.get("productid"))});
		}else{
			sb.append(" and mpf.user_id=? where mp.id=? ");
			a = this.findBySql(sb.toString(), new Object[]{Integer.valueOf(map.get("userid")), Integer.valueOf(map.get("productid"))});
		}
		if(a != null && !a.isEmpty()){
			return a.get(0);
		}
		 return new HashMap<String, Object>();
	 }
}
