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.JtOrderMapper;
import com.goafanti.common.model.JtCommodityComment;
import com.goafanti.common.model.JtOrder;
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
	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=jtOrderMapper.selectByPrimaryKey(commentInput.getOrderNo());
		if(jtOrder==null || jtOrder.getBuyerId()==null || !jtOrder.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.setOrderNo(commentInput.getOrderNo());
		jtCommodityComment.setStar(commentInput.getStar());
		jtCommodityComment.setUid(uid);
		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) {
		// TODO Auto-generated method stub
		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);
	}
	
	
	
	
	
}
