package com.bjsxt.service.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.CloudSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;
import redis.clients.jedis.JedisCluster;
import com.xt.service.CarItemService;
import com.xt.utils.ObjectUtils;
import com.xt.vo.CarItem;
import com.xt.vo.ProdcutVo;
@Service
public class CarItemServiceImpl implements CarItemService {
//注入
@Autowired
private CloudSolrServer cloudSolrServer;
@Autowired
private JedisCluster jedis;
public ProdcutVo loadProductByIdService(String id) {
try{
cloudSolrServer.setDefaultCollection("bjsxt");
// TODO Auto-generated method stub
SolrQuery solrQuery=new SolrQuery();
//封装查询条件
solrQuery.set("q","id:"+id);
//设置需要查询的字段
solrQuery.setFields("id","product_isbn","product_name","product_price","product_current_price");
//获得查询响应
QueryResponse response=cloudSolrServer.query(solrQuery);
//获得查询结果
SolrDocument doc=response.getResults().get(0);
//创建vo对象
ProdcutVo vo=new ProdcutVo();
vo.setFlowerid(Integer.parseInt(String.valueOf(doc.get("id"))));
vo.setFlowername(String.valueOf(doc.get("product_name")));
vo.setFlowerisbn(String.valueOf(doc.get("product_isbn")));
vo.setPrice(Double.parseDouble(String.valueOf(doc.get("product_price"))));
vo.setCurrentprice(Double.parseDouble(String.valueOf(doc.get("product_current_price"))));
return vo;
}catch(Exception ex){
ex.printStackTrace();
}
return null;
}
@Override
public void addProductToCarService(String uid,ProdcutVo pvo) {
// TODO Auto-generated method stub
//声明购物车
CarItem carItem=null;
//声明map集合
Map
Integer flowerid=pvo.getFlowerid();//获得需要购买的商品的编号
//将商品的编号转化为byte[]
byte[] key = ObjectUtils.objectToByte(flowerid);
//将用户id转化为byte[]
byte[] id=ObjectUtils.objectToByte(uid);
//判断该用户是否第一次买东西
//获得该用户对应的购物车集合(map)
map = jedis.hgetAll(id);
if(map==null){
//创建Map对象
map= new HashMap
}
//判断该商品是否第一次购买
if(map.containsKey(key)){//gaikey对应的商品已经购买过
carItem=(CarItem) ObjectUtils.byteToObject(map.get(key));//获得该商品对应的购物车对象
carItem.setCount(carItem.getCount()+1); //数量加一
carItem.setPvo(pvo);
}else{
//创建购物车对象
carItem=new CarItem();
//封装需要购买的商品
carItem.setPvo(pvo);
carItem.setCount(1);
}
//将购物车放入map集合中
map.put(key,ObjectUtils.objectToByte(carItem));
//将购车集合保存到redis
jedis.hmset(id, map);
}
@Override
public List
// TODO Auto-generated method stub
//获得用户的购物车集合
Map
List
//遍历Map集合
for(byte[] b:map.values()){
CarItem carItem = (CarItem) ObjectUtils.byteToObject(b);
//System.out.println(carItem.getPvo().getFlowername()+"==============");
list.add(carItem);
}
return list;
}
@Override
public Map
String uid) {
System.out.println("id-----------------------"+id);
// TODO Auto-generated method stub
//通过用户id获得购物车集合
Map
//获得商品对应的购物车对象
CarItem carItem = (CarItem) ObjectUtils.byteToObject(map.get(ObjectUtils.objectToByte(id)));
carItem.setCount(count);
//carItem.setPvo(pvo);
//将carItem对象进行序列化保存到map中
map.put(ObjectUtils.objectToByte(id), ObjectUtils.objectToByte(carItem));
//将map中的数据保存到redis中
jedis.hmset(ObjectUtils.objectToByte(uid), map);
//计算小计
Double xj=count*carItem.getPvo().getCurrentprice();
Double total=0.0;
//计算总计
for(byte[] b:map.values()){
CarItem item = (CarItem) ObjectUtils.byteToObject(b);
if(item.getPvo().getCurrentprice()!=null){
total=total+(item.getCount()*item.getPvo().getCurrentprice());
}
}
//将小计和总计封装为map对象
Map
rmap.put("xj",xj);
rmap.put("total", total);
return rmap;
}
@Override
public void delCarItemService(Integer id, String uid) {
// TODO Auto-generated method stub
//通过用户id获得购物车集
jedis.hdel(ObjectUtils.objectToByte(uid),ObjectUtils.objectToByte(id));
}
@Test
public void testLoad(){
ApplicationContext ac=
new ClassPathXmlApplicationContext("service_bean.xml");
CarItemService cs = (CarItemService) ac.getBean("carItemServiceImpl");
cs.delCarItemService(10005, "123233");
/*cs.updateCountService(10003, 2, "123233");
ProdcutVo pvo=cs.loadProductByIdService("10006");
System.out.println(pvo.getFlowername());;
List
for(CarItem c:list){
System.out.println(c.getPvo().getFlowername());
}*/
}
}