package com.goafanti.comment.service.impl;


import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.goafanti.comment.bo.CommentDetailResult;
import com.goafanti.comment.bo.CommentInput;
import com.goafanti.comment.service.CommentService;
import com.goafanti.common.dao.JtCommodityCommentMapper;
import com.goafanti.common.dao.JtConsultOrderMapper;
import com.goafanti.common.dao.JtOrderMapper;
import com.goafanti.common.model.JtCommodityComment;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.core.mybatis.BaseMybatisDao;
import com.goafanti.core.mybatis.page.Pagination;
import com.goafanti.core.shiro.token.TokenManager;

@Service
public class CommentServiceImpl extends BaseMybatisDao<JtCommodityCommentMapper> implements CommentService{
	@Autowired
	JtCommodityCommentMapper jtCommodityCommentMapper;
	@Autowired
	JtConsultOrderMapper	jtConsultOrderMapper;
	@Autowired
	JtOrderMapper jtOrderMapper;

	@Override
	public int addNewComment(CommentInput commentInput, String ip) {
		// TODO Auto-generated method stub
		String id=UUID.randomUUID().toString();
		String uid=TokenManager.getUserId();
//		String uid="1180fa62-7c42-44be-bc41-5583814d69f4";
		
		//注释掉必须购买才能评价的条件
		/*
		JtOrder jtOrder= new JtOrder(); 
		if (commentInput.getType()==0){
			jtOrder=jtOrderMapper.selectByPrimaryKey(commentInput.getOrderNo());
			if(jtOrder==null || jtOrder.getBuyerId()==null || !jtOrder.getBuyerId().equals(uid))return -1;
		}
		if (commentInput.getType()==1){
			JtConsultOrder jtConsultOrder=jtConsultOrderMapper.selectByPrimaryKey(commentInput.getOrderNo());
			if(jtConsultOrder==null || jtConsultOrder.getBuyerId()==null || !jtConsultOrder.getBuyerId().equals(uid))return -1;
		}*/
		
		
		JtCommodityComment jtCommodityComment=new JtCommodityComment();
		jtCommodityComment.setCommodityId(commentInput.getCommodityId());
		jtCommodityComment.setContent(commentInput.getContent());
		jtCommodityComment.setCreateTime(new Date());
		jtCommodityComment.setId(id);
		jtCommodityComment.setIp(ip);
		jtCommodityComment.setStar(commentInput.getStar());
		jtCommodityComment.setCommodityType(commentInput.getCommodityType());
		
		//用户id或者订单id如果不存在(匿名评论)
		if(StringUtils.isBlank(commentInput.getOrderNo())){
			commentInput.setOrderNo("");
		}
		if(StringUtils.isBlank(uid)){
			commentInput.setOrderNo("");
			//查看当前ip的游客名称和游客最大名称
			uid = jtCommodityCommentMapper.getUidByIp(ip);
			if(StringUtils.isBlank(uid)){
				uid = jtCommodityCommentMapper.getMaxNameUid();
				uid = StringUtils.isBlank(uid) ? "0" : uid;//如果是第一个游客
				int c = Integer.parseInt(uid) + 1;
				uid = "游客" + String.format("%03d", c);
			}
		}
		jtCommodityComment.setUid(uid);
		jtCommodityComment.setOrderNo(commentInput.getOrderNo());
		jtCommodityCommentMapper.insert(jtCommodityComment);
		return 0;
	}

	/*
	 * 0-好评  1-中评 2-差评
	 * */
	@Override
	public Integer getCommentCount(Integer type,String commodityId) {
		// TODO Auto-generated method stub
		return jtCommodityCommentMapper.getCommentCount( type, commodityId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pagination<CommentDetailResult> searchComment(String commodityId,Integer pageNo,Integer pageSize) {
		Map<String, Object>params=new HashMap<>();
		if(pageNo==null || pageNo<1)pageNo=1;
		if(pageSize == null || pageSize <1)pageSize=10;
		params.put("commodityId", commodityId);
		return (Pagination<CommentDetailResult>) findPage("searchCommentByCommodityId", "searchCommentCountByCommodityId", params, pageNo, pageSize);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pagination<CommentDetailResult> selectExpertsComment(String id,Integer pageNo,Integer pageSize) {
		Map<String, Object>params=new HashMap<>();
		if(pageNo==null || pageNo<1)pageNo=1;
		if(pageSize == null || pageSize <1)pageSize=10;
		params.put("id", id);
		return (Pagination<CommentDetailResult>) findPage("selectExpertsCommentList", "selectExpertsCommentCount", params, pageNo, pageSize);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pagination<CommentDetailResult> searchUnlandedCommentList(
			String commodityId, Integer pageNo, Integer pageSize) {
		Map<String, Object>params=new HashMap<>();
		params.put("commodityId", commodityId);
		return (Pagination<CommentDetailResult>) findPage("searchUnlandedCommentByCommodityId", "searchCommentCountByCommodityId", params, pageNo, pageSize);
	}
	
	
	
	
	
}
