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

import java.util.Date;
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.IMgmProductViewDao;
import org.cs.mgr.product.model.MgmProduct;
import org.cs.mgr.product.model.MgmProductView;
import org.cs.mgr.product.service.IMgmProductViewService;
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("mgmProductViewService")
public class MgmProductViewService extends BaseService<MgmProductView> implements IMgmProductViewService{
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private IMgmProductViewDao mgmProductViewDao;
	@Autowired
	private IMgmProductDao mgmProductDao;
	
	@Override
	@Transactional(readOnly=false)
	public JSONObject addMgmProductView(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, "", "未获取到关注的产品信息");
		}
		MgmProductView mpv = new MgmProductView();
		mpv.setCreateDate(new Date());
		mpv.setIsDelete(false);
		mpv.setModifyDate(new Date());
		mpv.setProductId(Integer.valueOf(productid));
		mpv.setUserId(Integer.valueOf(userid));
		mpv.setViewTime(new Date());
		mpv = this.mgmProductViewDao.insert(mpv);
		
		MgmProduct mp = this.mgmProductDao.findById(Integer.valueOf(productid));
		if(mp.getViewTotal() != null)
		{
			mp.setViewTotal(mp.getViewTotal() + 1);
			mp = this.mgmProductDao.update(mp);
		}else {
			mp.setViewTotal(1);
			mp = this.mgmProductDao.update(mp);
		}
		return getJson(1, "", "");
	}
	
}
