<repo_name>MiSeWang/taotao-parent<file_sep>/taotao-manager/taotao-manager-web/src/main/java/com/mrlv/taotao/controller/IndexController.java
package com.mrlv.taotao.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 主页面
 * @author Administrator
 *
 */
@Controller
public class IndexController {
	@RequestMapping("/{itemId}")
	public String Index(@PathVariable String itemId) {
		return itemId;
	}
	@RequestMapping("/")
	public String Index() {
		return "index";
	}
}
<file_sep>/taotao-manager/taotao-manager-service/src/main/java/com/mrlv/taotao/service/Impl/ItemCatServiceImpl.java
package com.mrlv.taotao.service.Impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.mrlv.taotao.common.pojo.EasyUITreeNode;
import com.mrlv.taotao.mapper.ItemCatMapper;
import com.mrlv.taotao.pojo.ItemCat;
import com.mrlv.taotao.pojo.ItemCatExample;
import com.mrlv.taotao.pojo.ItemCatExample.Criteria;
import com.mrlv.taotao.service.IItemCatService;

/**
 * 商品分类
 * @author Administrator
 *
 */
@Service("itemCatService")
public class ItemCatServiceImpl implements IItemCatService {

	@Resource
	private ItemCatMapper itemCatMapper;
	
	@Override
	public List<EasyUITreeNode> getItemCatList(Long parentId) {
		
		ItemCatExample ice = new ItemCatExample();
		
		Criteria iceCriteria = ice.createCriteria();
		
		//设置查询条件，根据parentid查询子节点
		iceCriteria.andParentIdEqualTo(parentId);
		
		//返回子节点列表
		List<ItemCat> itemCats = itemCatMapper.selectByExample(ice);
		
		List<EasyUITreeNode> list = new ArrayList<EasyUITreeNode>();
		
		for (ItemCat itemCat : itemCats) {
			EasyUITreeNode node = new EasyUITreeNode();
			node.setId(itemCat.getId());
			node.setText(itemCat.getName());
			//如果是父节点的话就设置成关闭状态，如果是叶子节点就是open状态
			node.setState(itemCat.getIsParent()?"closed":"open");
			list.add(node);
		}
		return list;
	}

}
<file_sep>/taotao-rest/src/main/java/com/mrlv/taotao/rest/service/impl/ContentServiceImpl.java
package com.mrlv.taotao.rest.service.impl;

import java.util.List;
import javax.annotation.Resource;
import com.mrlv.taotao.common.utils.JsonUtils;
import com.mrlv.taotao.rest.dao.JedisClient;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.mrlv.taotao.mapper.ContentMapper;
import com.mrlv.taotao.pojo.Content;
import com.mrlv.taotao.pojo.ContentExample;
import com.mrlv.taotao.pojo.ContentExample.Criteria;
import com.mrlv.taotao.rest.service.IContentService;

@Service("contentService")
public class ContentServiceImpl implements IContentService {
	
	@Resource
	private ContentMapper contentMapper;

	@Resource
	private JedisClient jedisClient;

	@Value("${INDEX_CONTENT_REDIS_KEY}")
	private String INDEX_CONTENT_REDIS_KEY;

	public List<Content> getContentList(long contentCid){
		//从缓存中取内容
		try {
			String result = jedisClient.hget(INDEX_CONTENT_REDIS_KEY, contentCid + "");
			//判断是否为空
			if (!StringUtils.isBlank(result)){
				//将字符转为list
				List<Content> contents = JsonUtils.jsonToList(result, Content.class);
				return contents;
            }
		} catch (Exception e) {
			e.printStackTrace();
		}

		//根据内容分类id查询内容列表
		ContentExample contentExample = new ContentExample();
		Criteria criteria = contentExample.createCriteria();
		criteria.andCategoryIdEqualTo(contentCid);
		//执行查询
		List<Content> list = contentMapper.selectByExample(contentExample);

		//向缓存中添加内容
		try {
			//将list转换为字符串
			String cacheString = JsonUtils.objectToJson(list);
			jedisClient.hset(INDEX_CONTENT_REDIS_KEY, contentCid + "", cacheString);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return list;
	}
}
<file_sep>/taotao-manager/taotao-manager-service/src/main/java/com/mrlv/taotao/service/Impl/PictureServiceImpl.java
package com.mrlv.taotao.service.Impl;

import java.io.IOException;

import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.mrlv.taotao.common.pojo.PictureResult;
import com.mrlv.taotao.common.utils.FtpUtil;
import com.mrlv.taotao.common.utils.IDUtils;
import com.mrlv.taotao.service.IPictureService;
/**
 * 图片上传
 * @author Administrator
 *
 */
@Service("pictureService")
public class PictureServiceImpl implements IPictureService {

	@Value("${FTP_ADDRESS}")
	private String FTP_ADDRESS;
	@Value("${FTP_PORT}")
	private Integer FTP_PORT;
	@Value("${FTP_USERNAME}")
	private String FTP_USERNAME;
	@Value("${FTP_PASSWORD}")
	private String FTP_PASSWORD;
	@Value("${FTP_BASE_PATH}")
	private String FTP_BASE_PATH;
	@Value("${IMAGE_BASE_URL}")
	private String IMAGE_BASE_URL;
	
	@Override
	public PictureResult uploadPicture(MultipartFile uploadFile) {
		try {
			//生成一个新的文件名，也可以用UUID
			String newName = IDUtils.genImageName();
			//取原始文件名
			String oldName = uploadFile.getOriginalFilename();
			newName =  newName + oldName.substring(oldName.lastIndexOf("."));
			//获取日期路径名
			String imagePath = new DateTime().toString("/yyyy/MM/dd");
			System.out.println(imagePath + "/" + newName);
			//图片上传
			boolean result = FtpUtil.uploadFile(FTP_ADDRESS, FTP_PORT, FTP_USERNAME, FTP_PASSWORD, 
					FTP_BASE_PATH, imagePath, newName, uploadFile.getInputStream());
			if(!result) {
				PictureResult pictureResult = new PictureResult(1, null, "文件上传失败");
				return pictureResult;
			} else {
				PictureResult pictureResult = new PictureResult(0, IMAGE_BASE_URL + imagePath + "/" + newName, null);
				return pictureResult;
			}
			
		} catch (IOException e) {
			PictureResult pictureResult = new PictureResult(1, null, "文件上传发生异常");
			return pictureResult;
		}
	}

}
<file_sep>/taotao-manager/taotao-manager-web/src/main/java/com/mrlv/taotao/controller/ContentCategoryController.java
package com.mrlv.taotao.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mrlv.taotao.common.pojo.EasyUITreeNode;
import com.mrlv.taotao.common.pojo.TaotaoResult;
import com.mrlv.taotao.service.IContentCategroyService;

@Controller
public class ContentCategoryController {

	@Resource
	private IContentCategroyService contentCategroyService;
	
	@RequestMapping("/content/category/list")
	@ResponseBody
	public List<EasyUITreeNode> getContentCatList(@RequestParam(value="id", defaultValue="0")Long parentId){
		return contentCategroyService.getCategoryList(parentId);
	}
	
	@RequestMapping("/content/category/create")
	@ResponseBody
	public TaotaoResult createContentCategory(Long parentId, String name) {
		return contentCategroyService.insertContentCategory(parentId, name);
	}
	
	@RequestMapping("/content/category/delete")
	@ResponseBody
	public TaotaoResult delectContentCategory(Long parentId, Long id) {
		return contentCategroyService.delectContentCategory(id);
	}
}
<file_sep>/taotao-rest/src/main/java/com/mrlv/taotao/rest/service/impl/RedisServiceImpl.java
package com.mrlv.taotao.rest.service.impl;

import com.mrlv.taotao.common.pojo.TaotaoResult;
import com.mrlv.taotao.common.utils.ExceptionUtil;
import com.mrlv.taotao.rest.dao.JedisClient;
import com.mrlv.taotao.rest.service.IRedisService;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.Resource;

public class RedisServiceImpl implements IRedisService{

    @Resource
    private JedisClient jedisClient;

    @Value("${INDEX_CONTENT_REDIS_KEY}")
    private String INDEX_CONTENT_REDIS_KEY;

    @Override
    public TaotaoResult syncContent(long contentId) {
        try {
            jedisClient.hdel(INDEX_CONTENT_REDIS_KEY, contentId+"");
        } catch (Exception e) {
            e.printStackTrace();
            return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
        }
        return TaotaoResult.ok();
    }
}
<file_sep>/taotao-manager/taotao-manager-service/src/main/java/com/mrlv/taotao/service/Impl/ItemServiceImpl.java
package com.mrlv.taotao.service.Impl;

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

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mrlv.taotao.common.pojo.EasyUIResult;
import com.mrlv.taotao.common.pojo.TaotaoResult;
import com.mrlv.taotao.common.utils.IDUtils;
import com.mrlv.taotao.mapper.ItemDescMapper;
import com.mrlv.taotao.mapper.ItemMapper;
import com.mrlv.taotao.pojo.Item;
import com.mrlv.taotao.pojo.ItemDesc;
import com.mrlv.taotao.pojo.ItemExample;
import com.mrlv.taotao.service.IItemService;

/**
 * 商品
 * @author Administrator
 *
 */
@Service("itemService")
public class ItemServiceImpl implements IItemService {

	@Resource
	private ItemMapper itemMapper;
	
	@Resource
	private ItemDescMapper itemDescMapper;
	
	@Override
	public Item getItemById(Long id) {
		return itemMapper.selectByPrimaryKey(id);
	}

	@Override
	public EasyUIResult getItemList(Integer page, Integer rows) {
		//分页处理
		PageHelper.startPage(page, rows);
		
		List<Item> items = itemMapper.selectByExample(new ItemExample());
		
		EasyUIResult eugr = new EasyUIResult();
		
		eugr.setRows(items);
		
		//取总记录数
		PageInfo<Item> info = new PageInfo<Item>(items);
		
		eugr.setTotal(info.getTotal());
		
		return eugr;
	}

	@Override
	public TaotaoResult createItem(Item item, String desc) throws Exception {
		//生成商品id
		Long itemId = IDUtils.genItemId();
		item.setId(itemId);
		//商品状态：1-正常，2-下架，3-删除
		item.setStatus((byte)1);
		item.setCreated(new Date());
		item.setUpdated(new Date());

		itemMapper.insert(item);
		TaotaoResult descResult = insertItemDesc(itemId, desc);
		if(descResult.getStatus() != 200) {
			throw new Exception();
		}
		return TaotaoResult.ok();
	}

	public ItemDesc getItemDesc(Long itemId){
		ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(itemId);
		return itemDesc;
	}
	
	/**
	 * 添加商品描述
	 * @param desc
	 * @return
	 */
	private TaotaoResult insertItemDesc(Long itemId, String desc) {
		ItemDesc itemDesc = new ItemDesc();
		itemDesc.setItemId(itemId);
		itemDesc.setItemDesc(desc);
		itemDesc.setCreated(new Date());
		itemDesc.setUpdated(new Date());
		itemDescMapper.insert(itemDesc);
		return TaotaoResult.ok();
	}
}
<file_sep>/taotao-rest/src/test/java/com/mrlv/taotao/jedis/JedisTest.java
package com.mrlv.taotao.jedis;

import org.junit.Test;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.HashSet;

public class JedisTest {

    /**
     * 单机版
     */
    @Test
    public void testJedisSingle(){
        //创建一个jedis的对象
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        //调用jedis对象的方法，方法名和redis一样
        jedis.set("one", "初号机");
        //关闭jedis
        String one = jedis.get("one");
        System.out.println(one);
        jedis.close();
    }

    /**
     * 使用连接池
     */
    @Test
    public void testJedisPool(){
        //创建jedis的连接池对象
        JedisPool jedisPool = new JedisPool("127.0.0.1", 6379);
        //从连接池中获得Jedis对象
        Jedis jedis = jedisPool.getResource();
        jedis.set("two", "二号机");
        String two = jedis.get("two");
        System.out.println(two);
        //关闭jedis对象
        jedis.close();
        jedisPool.close();
    }

    /**
     * 集群版测试
     */
//    @Test
    public void testJedisCluster(){
        //创建节点集合
        HashSet<HostAndPort> nodes = new HashSet<>();
        //添加节点
        nodes.add(new HostAndPort("127.0.0.1", 6379));
        nodes.add(new HostAndPort("127.0.0.1", 6379));
        nodes.add(new HostAndPort("127.0.0.1", 6379));
        nodes.add(new HostAndPort("127.0.0.1", 6379));

        JedisCluster cluster = new JedisCluster(nodes);

        cluster.set("three", "三号机");

        String three = cluster.get("three");
        System.out.println(three);
        cluster.close();
    }
}

<file_sep>/taotao-manager/taotao-manager-service/src/main/java/com/mrlv/taotao/service/IPictureService.java
package com.mrlv.taotao.service;


import org.springframework.web.multipart.MultipartFile;

import com.mrlv.taotao.common.pojo.PictureResult;


public interface IPictureService {
	
	//也可以使用Map
	public PictureResult uploadPicture(MultipartFile uploadFile);
}
<file_sep>/taotao-manager/taotao-manager-web/src/main/java/com/mrlv/taotao/controller/ContentController.java
package com.mrlv.taotao.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mrlv.taotao.common.pojo.EasyUIResult;
import com.mrlv.taotao.common.pojo.PictureResult;
import com.mrlv.taotao.common.pojo.TaotaoResult;
import com.mrlv.taotao.pojo.Content;
import com.mrlv.taotao.service.IContentService;

@Controller
public class ContentController {
	
	@Resource
	private IContentService contentService;
	
	@RequestMapping("/content/query/list")
	@ResponseBody
	public EasyUIResult list(Integer page, Integer rows, long categoryId) {
		EasyUIResult result = contentService.getContent(page, rows, categoryId);
		return result;
	}
	
	@RequestMapping("/content/save")
	@ResponseBody
	public TaotaoResult insertContent(Content content) {
		TaotaoResult result = contentService.insertContent(content);
		return result;
	}
}
<file_sep>/taotao-manager/taotao-manager-web/src/main/java/com/mrlv/taotao/controller/ItemCatController.java
package com.mrlv.taotao.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mrlv.taotao.common.pojo.EasyUITreeNode;
import com.mrlv.taotao.service.IItemCatService;

/**
 * 商品分类
 * @author Administrator
 *
 */
@Controller
public class ItemCatController {

	@Resource
	private IItemCatService itemCatService;
	
	@RequestMapping("/item/cat/list")
	@ResponseBody
	//defaultValue为默认值，value为name
	public List<EasyUITreeNode> categoryList(@RequestParam(value="id",defaultValue="0") Long parentId) {
		List<EasyUITreeNode> itemCatList = itemCatService.getItemCatList(parentId);
		return itemCatList;
	}
}
<file_sep>/taotao-manager/taotao-manager-service/src/main/java/com/mrlv/taotao/service/IItemParamItemService.java
package com.mrlv.taotao.service;

public interface IItemParamItemService {
	
	public String getItemParamByItemId(Long itemId);
}
<file_sep>/taotao-manager/taotao-manager-web/src/main/java/com/mrlv/taotao/controller/ItemParamItemController.java
package com.mrlv.taotao.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mrlv.taotao.service.IItemParamItemService;

@Controller
public class ItemParamItemController {

	
	@Resource
	private IItemParamItemService itemParamItemService;
	
	@RequestMapping("/item/1111/{itemId}")
	public String showItemParam(@PathVariable Long itemId, Model model) {
		String string = itemParamItemService.getItemParamByItemId(itemId);
		model.addAttribute("itemParam", string);
		return "item";
	}


}
<file_sep>/taotao-rest/src/main/java/com/mrlv/taotao/rest/controller/ContentController.java
package com.mrlv.taotao.rest.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mrlv.taotao.common.pojo.TaotaoResult;
import com.mrlv.taotao.common.utils.ExceptionUtil;
import com.mrlv.taotao.pojo.Content;
import com.mrlv.taotao.rest.service.IContentService;

@Controller
public class ContentController {
	
	@Resource
	private IContentService contentService;
	
	@RequestMapping(value="/content/list/{contentCategoryId}"
			,produces=MediaType.APPLICATION_JSON_VALUE + ";charset=utf-8")
	@ResponseBody
	public TaotaoResult getContentList(@PathVariable Long contentCategoryId) {
		try {
			List<Content> contentList = contentService.getContentList(contentCategoryId);
			return TaotaoResult.ok(contentList);
		} catch (Exception e) {
			e.printStackTrace();
			return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
		}
	}
}
<file_sep>/taotao-manager/taotao-manager-web/src/main/java/com/mrlv/taotao/controller/ItemParamController.java
package com.mrlv.taotao.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mrlv.taotao.common.pojo.EasyUIResult;
import com.mrlv.taotao.common.pojo.TaotaoResult;
import com.mrlv.taotao.pojo.ItemParam;
import com.mrlv.taotao.service.IItemParamService;

/**
 * 商品规格参数
 * @author Administrator
 *
 */
@Controller
public class ItemParamController {
	
	@Resource
	private IItemParamService itemParamService;
	
	@RequestMapping("/item/param/list")
	@ResponseBody
	public EasyUIResult getItemParamList(Integer page, Integer rows) {
		EasyUIResult result = itemParamService.getItemParamList(page, rows);
		return result;
	}
	
	/**
	 * 根据商品分类id查询规格参数
	 * @param itemCatId
	 * @return
	 */
	@RequestMapping("/item/param/query/itemcatid/{itemCatId}")
	@ResponseBody
	public TaotaoResult getItemParamByCid(@PathVariable Long itemCatId) {
		TaotaoResult result = itemParamService.getItemParamByCid(itemCatId);
		return result;
	}
	
	/**
	 * 添加规格参数
	 * <p>Title: insertItemParamItem</p>
	 * <p>Description: </p>
	 * @return
	 */
	@RequestMapping("/item/param/save/{itemCatId}")
	@ResponseBody
	public TaotaoResult insertItemParam(@PathVariable Long itemCatId, String paramData) {
		ItemParam itemParam = new ItemParam();
		itemParam.setItemCatId(itemCatId);
		itemParam.setParamData(paramData);
		TaotaoResult result = itemParamService.insertItemParam(itemParam);
		return result;
	}
}
<file_sep>/taotao-search/src/main/java/com/mrlv/taotao/search/controller/ItemController.java
package com.mrlv.taotao.search.controller;

import com.mrlv.taotao.common.pojo.TaotaoResult;
import com.mrlv.taotao.search.service.IItemService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * 索引库维护
 */
@Controller
@RequestMapping("/manager")
public class ItemController {

    @Resource
    private IItemService itemService;

    /**
     * 导入商品入索引库
     */
    @RequestMapping("/importall")
    @ResponseBody
    public TaotaoResult importAllItem(){
        TaotaoResult result  = itemService.importAllItem();
        return result;
    }

}
<file_sep>/taotao-portal/src/main/java/com/mrlv/taotao/portal/controller/IndexController.java
package com.mrlv.taotao.portal.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mrlv.taotao.portal.service.IContentService;

@Controller
public class IndexController {
	
	@Resource
	private IContentService contentService;
	
	@RequestMapping("/index")
	public String showIndex(Model model) {
		String adJson = contentService.getContentList();
		model.addAttribute("ad1", adJson);
		return "index";
	}
}
<file_sep>/taotao-search/src/main/java/com/mrlv/taotao/search/service/impl/ItemServiceImpl.java
package com.mrlv.taotao.search.service.impl;

import com.mrlv.taotao.common.pojo.TaotaoResult;
import com.mrlv.taotao.search.mapper.ItemSearchMapper;
import com.mrlv.taotao.search.pojo.Item;
import com.mrlv.taotao.search.service.IItemService;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("itemService")
public class ItemServiceImpl implements IItemService{

    @Resource
    private ItemSearchMapper itemSearchMapper;

    @Resource
    private HttpSolrClient httpSolrClient;

    @Override
    public TaotaoResult importAllItem() {
        httpSolrClient = new HttpSolrClient.Builder("http://localhost:8983/solr/taotao_core").build();
        //查询商品列表
        List<Item> itemList = itemSearchMapper.getItemList();
        //将商品信息写入索引库
        try {
            for (Item item : itemList){
                //创建一个SolrInputDocument
                SolrInputDocument document = new SolrInputDocument();
                document.setField("id", item.getId());
                document.setField("item_title", item.getTitle());
                document.setField("item_sell_point", item.getSell_point());
                document.setField("item_price", item.getPrice());
                document.setField("item_image", item.getImage());
                document.setField("item_category_name", item.getCategory_name());
                //一旦出现不存在的索引字段，则无法创建索引
//              document.setField("item_desc", item.getItem_des());
                httpSolrClient.add(document);
            }
            httpSolrClient.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return TaotaoResult.ok();
    }
}
