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

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.cs.core.service.impl.BaseService;
import org.cs.mgr.product.dao.IMgmProductDao;
import org.cs.mgr.product.dao.IMgmProductFollowDao;
import org.cs.mgr.product.model.MgmProduct;
import org.cs.mgr.product.model.MgmProductFollow;
import org.cs.mgr.product.service.IMgmProductFollowService;
import org.cs.mgr.product.service.IMgmProductService;
import org.cs.util.Pager;
import org.cs.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;

/**
 * 
 * @ClassName: IMgmProductFollowService
 * @Description:  
 * @author: sunny_shi
 * @date: 2018-06-09 13:29:10
 */
@Service("mgmProductFollowService")
public class MgmProductFollowService extends BaseService<MgmProductFollow> implements IMgmProductFollowService{
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private IMgmProductFollowDao mgmProductFollowDao;
	@Autowired
	private IMgmProductDao mgmProductDao;
	
	@Override
	@Transactional(readOnly = true)
	public Pager getMgmProductFollows(Pager page){
		return this.mgmProductFollowDao.queryMgmProductFollows(page);
	}
	
	/**
	 * 自动生成
	 * @Title: delMgmProductFollow 
	 * @Description: TODO
	 * @param id						主键id
	 * @return
	 * @return: boolean
	 */
	@Override
	@Transactional(readOnly = false)
	public boolean delMgmProductFollow(Integer id){
	
		MgmProductFollow mgmProductFollow = this.mgmProductFollowDao.findById(id);
		if(mgmProductFollow != null){
			mgmProductFollow.setIsDelete(true);				
			this.mgmProductFollowDao.update(mgmProductFollow);
		}
		return true;
	}
	
	
	/**
	 * 自动生成
	 * @Title: addMgmProductFollow 
	 * @Description: TODO
	 * @param mgmProductFollow						对象
	 * @return
	 * @return: boolean
	 */
	@Override
	@Transactional(readOnly = false)
	public boolean addMgmProductFollow(MgmProductFollow mgmProductFollow){
	
		if(mgmProductFollow != null){
			this.mgmProductFollowDao.insert(mgmProductFollow);
		}
		return true;
	}
	
	
	/**
	 *  自动生成
	 * @Title: updateMgmProductFollow 
	 * @Description: TODO
	 * @param mgmProductFollow						对象
	 * @return
	 * @return: boolean
	 */
	@Override
	@Transactional(readOnly = false)
	public boolean updateMgmProductFollow(MgmProductFollow mgmProductFollow){
	
		MgmProductFollow mgmProductFollow2 = this.mgmProductFollowDao.findById(mgmProductFollow.getId());
		if(mgmProductFollow2 != null){
			mgmProductFollow.mergeTo(mgmProductFollow2);
			this.mgmProductFollowDao.update(mgmProductFollow2);
		}
		
		return true;
	}
	
	@Override
	@Transactional(readOnly = false)
	public JSONObject addMgmProductFollow(Map<String, String> map) {
		String productid = map.get("productid");
		String userid = map.get("userid");
		if(StringUtils.isBlank(userid)) {
			return getJson(0, "", "未获取到用户信息");
		}
		if(StringUtils.isBlank(productid.trim())) {
			return getJson(0, "", "未获取到关注的产品信息");
		}
		MgmProductFollow mpfold = this.mgmProductFollowDao.queryMagmProductFollow(map);
		if(mpfold != null && StringUtil.isNotBlank(mpfold.getId().toString())) {
			return getJson(0, "", "已关注");
		}
		MgmProductFollow mpf = new MgmProductFollow();
		mpf.setCreateDate(new Date());
		mpf.setFollowTime(new Date());
		mpf.setIsDelete(false);
		mpf.setModifyDate(new Date());
		mpf.setProductId(Integer.valueOf(productid));
		mpf.setUserId(Integer.valueOf(userid));
		mpf = this.mgmProductFollowDao.insert(mpf);
		
		MgmProduct mp = this.mgmProductDao.findById(Integer.valueOf(productid));
		if(mp.getFollowTotal() != null)
		{
			mp.setFollowTotal(mp.getFollowTotal() + 1);
			mp = this.mgmProductDao.update(mp);
		}else {
			mp.setFollowTotal(1);
			mp = this.mgmProductDao.update(mp);
		}
		return getJson(1, "", "关注成功");
	}
	
	@Override
	@Transactional(readOnly = false)
	public JSONObject delMgmProductFollow(Map<String, String> map) {
		String productids = map.get("productids");
		String userid = map.get("userid");
		if(StringUtils.isBlank(userid)) {
			return getJson(0, "", "未获取到用户信息");
		}
		if(productids == null || StringUtils.isBlank(productids.replaceAll(",", ""))) {
			return getJson(0, "", "未获取到关注的产品信息");
		}
		for(String productid:productids.split(",")) {
			if(StringUtil.isNotBlank(productid)) {
				String where = " and productId="+productid+" and userId="+userid;
				this.mgmProductFollowDao.deleteByWhere(where);
			}
			
		}
		return getJson(1, "", "已取消关注");
	}
	
	@Override
	public JSONObject queryFollowList(Map<String, String> map){
		String userid = map.get("userid");
		if(StringUtils.isBlank(userid)) {
			return getJson(0, "", "未获取到用户信息");
		}
		String page = map.get("page");
		if(StringUtil.isBlank(page)) {
			page="1";
		}
		Pager pager = this.mgmProductFollowDao.queryPagerFollowList(new Pager(Integer.valueOf(page)), map);
		
		/*for(Object mp:pager.getResults()){
			 Map<String, Object> m = (Map<String, Object>) mp;
			 String reg = "^[0-9]+(.[0-9]+)?$";
			if(m.get("commissionRate").toString().matches(reg)){
				m.put("commissionRate", m.get("commissionRate").toString() +"%");
			}
		 }*/
		return getJson(1, pager, "关注列表");
	}
}
