فهرست منبع

首页改版商品推荐

zhijiezhao 2 ماه پیش
والد
کامیت
523e50eb74

+ 20 - 0
src/main/java/com/caimei/modules/newhome/dao/NewPageRecommendProductDao.java

@@ -0,0 +1,20 @@
+package com.caimei.modules.newhome.dao;
+
+import com.caimei.modules.product.entity.Product;
+import com.thinkgem.jeesite.common.persistence.annotation.MyBatisDao;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.List;
+
+@MyBatisDao
+public interface NewPageRecommendProductDao {
+    List<Product> findRecommendList(Product product);
+
+    void updateSorts(@Param("sort") String sort, @Param("id") String id);
+
+    List<Product> findProductList(Product product);
+
+    void addProducts(@Param("split") String[] split);
+
+    void delProduct(Integer productId);
+}

+ 38 - 0
src/main/java/com/caimei/modules/newhome/service/NewPageRecommendProductService.java

@@ -0,0 +1,38 @@
+package com.caimei.modules.newhome.service;
+
+import com.caimei.modules.newhome.dao.NewPageRecommendProductDao;
+import com.caimei.modules.product.entity.Product;
+import com.thinkgem.jeesite.common.persistence.Page;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+import java.util.List;
+
+@Service
+public class NewPageRecommendProductService {
+
+    @Resource
+    private NewPageRecommendProductDao newPageRecommendProductDao;
+
+    public List<Product> findRecommendList(Page page) {
+        Product product = new Product();
+        product.setPage(page);
+        return newPageRecommendProductDao.findRecommendList(product);
+    }
+
+    public void saveBatchSort(String sort, String id) {
+        newPageRecommendProductDao.updateSorts(sort, id);
+    }
+
+    public List<Product> findProductList(Product product) {
+        return newPageRecommendProductDao.findProductList(product);
+    }
+
+    public void addProducts(String[] split) {
+        newPageRecommendProductDao.addProducts(split);
+    }
+
+    public void productDel(Integer productId) {
+        newPageRecommendProductDao.delProduct(productId);
+    }
+}

+ 135 - 0
src/main/java/com/caimei/modules/newhome/web/NewPageRecommendProductController.java

@@ -0,0 +1,135 @@
+package com.caimei.modules.newhome.web;
+
+import com.caimei.modules.newhome.service.NewPageRecommendProductService;
+import com.caimei.modules.product.entity.Product;
+import com.caimei.utils.AppUtils;
+import com.google.common.collect.Maps;
+import com.thinkgem.jeesite.common.config.Global;
+import com.thinkgem.jeesite.common.persistence.Page;
+import com.thinkgem.jeesite.common.utils.StringUtils;
+import org.apache.commons.collections.CollectionUtils;
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.servlet.mvc.support.RedirectAttributes;
+
+import javax.annotation.Resource;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.util.List;
+import java.util.Map;
+
+import static com.caimei.modules.newhome.web.NewPageQualitySupplierController.isInteger;
+
+@Controller
+@RequestMapping(value = "${adminPath}/newhome/recommendProduct")
+public class NewPageRecommendProductController {
+
+    @Resource
+    private NewPageRecommendProductService newPageRecommendProductService;
+
+    @RequestMapping(value = {"list", ""})
+    public String recommendProductList(HttpServletRequest request, HttpServletResponse response, Model model) {
+        Page<Product> page = new Page<Product>(request, response);
+        if (0 == page.getPageSize() || -1 == page.getPageSize()) page.setPageSize(20);
+        if (0 == page.getPageNo()) page.setPageNo(1);
+        //获取组合列表
+        List<Product> list = newPageRecommendProductService.findRecommendList(page);
+        page.setList(list);
+        model.addAttribute("page", page);
+        return "modules/newhome/recommendProductList";
+    }
+
+    @RequestMapping(value = "batchSaveSort")
+    @ResponseBody
+    public Map<String, Object> batchSaveSorts(String newProducSorttList, HttpServletRequest request, Model model, RedirectAttributes redirectAttributes) {
+        Map<String, Object> map = Maps.newLinkedHashMap();
+        try {
+            String[] newPageLists = newProducSorttList.split(",");
+            for (String list : newPageLists) {
+                String[] split = list.split("-");
+                if (split.length == 1 || split.length < 1) {
+                    map.put("success", false);
+                    map.put("msg", "排序值不能为空!");
+                    return map;
+                }
+                String s = split[1];
+                //判断是否是数字,或者为空
+                if (StringUtils.isNotEmpty(s)) {
+                    if (isInteger(s)) {
+                        String id = split[0];
+                        String sort = split[1];
+                        //保存数据,
+                        newPageRecommendProductService.saveBatchSort(sort, id);
+                    } else {
+                        map.put("success", false);
+                        map.put("msg", "排序值只能为数字");
+                        return map;
+                    }
+                } else {
+                    map.put("success", false);
+                    map.put("msg", "排序值不能为空!");
+                    return map;
+                }
+            }
+            map.put("success", true);
+            map.put("msg", "更新排序成功");
+            return map;
+        } catch (Exception e) {
+            map.put("success", false);
+            map.put("msg", "更新排序失败");
+            return map;
+        }
+    }
+
+    @RequestMapping(value = "toAddProduct")
+    public String toAddProduct(Product product, Page page, Model model) {
+        //获取采美所有商品
+        if (0 == page.getPageSize() || -1 == page.getPageSize()) page.setPageSize(30);
+        if (0 == page.getPageNo()) page.setPageNo(1);
+        product.setPage(page);
+        List<Product> productList = newPageRecommendProductService.findProductList(product);
+        if (CollectionUtils.isNotEmpty(productList)) {
+            for (Product p : productList) {
+                p.setMainImage(AppUtils.getProductImageURL(p.getMainImage(), 0, Global.getConfig("wwwServer")));
+            }
+        }
+        page.setList(productList);
+        model.addAttribute("page", page);
+        model.addAttribute("Product", product);
+        return "modules/newhome/toSelectProduct";
+    }
+
+    @ResponseBody
+    @RequestMapping(value = "saveAddProduct")
+    public Map<String, Object> saveAddProduct(String productIds, Integer id) {
+        Map<String, Object> map = Maps.newLinkedHashMap();
+        try {
+            String[] split = productIds.split(",");
+            //保存商品添加的组合
+            newPageRecommendProductService.addProducts(split);
+            map.put("success", true);
+            map.put("msg", "添加成功");
+        } catch (Exception e) {
+            map.put("success", false);
+            map.put("msg", "添加失败");
+        }
+        return map;
+    }
+
+    @RequestMapping(value = "deleteProduct")
+    public String productDel(Integer productId, RedirectAttributes redirectAttributes) {
+        newPageRecommendProductService.productDel(productId);
+        addMessage(redirectAttributes, "删除成功");
+        return "redirect:" + Global.getAdminPath() + "/newhome/recommendProduct/list";
+    }
+
+    protected void addMessage(RedirectAttributes redirectAttributes, String... messages) {
+        StringBuilder sb = new StringBuilder();
+        for (String message : messages) {
+            sb.append(message).append(messages.length > 1 ? "<br/>" : "");
+        }
+        redirectAttributes.addFlashAttribute("message", sb.toString());
+    }
+}

+ 71 - 0
src/main/resources/mappings/modules/newhome/NewPageRecommendProductMapper.xml

@@ -0,0 +1,71 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.caimei.modules.newhome.dao.NewPageRecommendProductDao">
+
+    <insert id="addProducts">
+        insert into cm_page_recommend_product (productId)
+        values
+        <foreach collection="split" item="item" index="index" separator=",">
+            (#{item})
+        </foreach>
+    </insert>
+
+    <update id="updateSorts">
+        update cm_page_recommend_product
+        set sort = #{sort}
+        where productId = #{id}
+    </update>
+
+    <delete id="delProduct">
+        delete from cm_page_recommend_product
+        where productId = #{productId}
+    </delete>
+
+    <select id="findRecommendList" resultType="com.caimei.modules.product.entity.Product">
+        select cpp.sort as sortIndex, p.mainImage, p.name, p.productID, s.name as shopName
+        from cm_page_recommend_product cpp
+        left join product p on cpp.productId = p.productID
+        left join shop s on p.shopId = s.shopId
+        where p.productCategory = 1
+        group by p.productID
+        <choose>
+            <when test="page !=null and page.orderBy != null and page.orderBy != ''">
+                ORDER BY ${page.orderBy},cpp.sort asc
+            </when>
+            <otherwise>
+                order by cpp.sort asc
+            </otherwise>
+        </choose>
+    </select>
+    <select id="findProductList" resultType="com.caimei.modules.product.entity.Product">
+        SELECT a.mainImage, a.name, a.productID,s.name as "shopName"
+        FROM product a
+        left join cm_organize_product_info copi on copi.productId = a.productID
+        left join shop s on a.shopId = s.shopID
+        left join cm_page_recommend_product nsp ON a.productId = nsp.productId
+        <where>
+            nsp.productId is null
+            and
+            copi.validFlag in (2,3,9)
+            <if test="productID !=null and productID !=''">
+                AND a.productID=#{productID}
+            </if>
+            <if test="name != null and name != ''">
+                AND a.name LIKE concat('%',#{name},'%')
+            </if>
+            <if test="shopName != null and shopName != ''">
+                AND s.name LIKE concat('%',#{shopName},'%')
+            </if>
+            and a.productCategory = 1
+        </where>
+        group by a.productID
+        <choose>
+            <when test="page !=null and page.orderBy != null and page.orderBy != ''">
+                ORDER BY ${page.orderBy}
+            </when>
+            <otherwise>
+                order by a.productID desc
+            </otherwise>
+        </choose>
+    </select>
+</mapper>

+ 162 - 0
src/main/webapp/WEB-INF/views/modules/newhome/recommendProductList.jsp

@@ -0,0 +1,162 @@
+<%@ page contentType="text/html;charset=UTF-8" %>
+<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
+<html>
+<head>
+	<title>组合商品列表</title>
+	<meta name="decorator" content="default"/>
+	<style type="text/css">
+		.table th{text-align: center;}
+		.table td{text-align: center;}
+	</style>
+	<script type="text/javascript">
+		$(document).ready(function() {
+
+		});
+		function page(n,s){
+			$("#pageNo").val(n);
+			$("#pageSize").val(s);
+			$("#searchForm").submit();
+        	return false;
+        }
+
+        /**
+         * @param obj
+         * jquery控制input只能输入数字
+         */
+        function onlynum(obj) {
+            obj.value = obj.value.replace(/[^\d]/g, ""); //清除"数字"以外的字符
+        }
+
+
+        //选择添加商品
+        function showSelect(){
+        //获取选中的组合
+        var id=$("#id option:selected").val();
+            top.$.jBox("iframe:${ctx}/newhome/recommendProduct/toAddProduct", {
+                iframeScrolling:'yes',
+                width: $(top.document).width()-400,
+                height: $(top.document).height()-160,
+                persistent: true,
+                title:"选择商品",
+                buttons:{"确定":'1',"关闭":'-1'},
+                submit:function(v, h, f){
+                    //确定
+                    var $jboxFrame = top.$('#jbox-iframe');
+                    var $mainFrame = top.$('#mainFrame');
+                    if('1'==v && 1==$jboxFrame.size() && 1==$mainFrame.size()){
+                        var items = $jboxFrame[0].contentWindow.getCheckedItems();//获取到的数据格式:(含有参数序号拼接,使用数组传入)1o,2e,3t,4y,5u,6p
+                        if(items.length>0){
+                            //添加数据
+                            $.post("${ctx}/newhome/recommendProduct/saveAddProduct",{
+                                "productIds":items,"id":id}, function(data) {
+                                if(true==data.success){
+                                    window.location.href = "${ctx}/newhome/recommendProduct/list";
+                                    $.jBox.tip(data.msg, 'info');
+                                } else {
+                                    $.jBox.tip(data.msg,'error');
+                                }
+                            },"JSON");//这里返回的类型有:json,html,xml,text
+                            return true;
+                        }else{
+                            top.$.jBox.tip("请先勾选商品...");
+                            return false;
+                        }
+                    }
+                    return true;
+                }
+            });
+        }
+
+        //批量保存排序
+        function batchSaveSort() {
+            var items = new Array();
+            var $items = $('.check-item');
+            $items.each(function(){
+                items.push($(this).val());
+            });
+            //保存批量排序
+            $.post("${ctx}/newhome/recommendProduct/batchSaveSort?newProducSorttList="+items, function(data) {
+                if(true==data.success){
+                    $.jBox.tip(data.msg, 'info');
+                    setInterval(g,1000);
+                } else {
+                    $.jBox.tip(data.msg,'error');
+                }
+            },"JSON");//这里返回的类型有:json,html,xml,text
+        }
+
+        function g()
+        {
+            window.location.href="${ctx}/newhome/recommendProduct/list";//刷新页面
+        }
+
+        //修改排序值
+        function changeSort(id,sortThis) {
+            var value = sortThis.value;
+            $("#sort"+id).val(id+"-"+value);
+        }
+
+	</script>
+</head>
+<body>
+	<ul class="nav nav-tabs">
+		<li class="active"><a href="${ctx}/newhome/recommendProduct/list">推荐商品列表</a></li>
+	</ul>
+	<form:form id="searchForm" action="${ctx}/newhome/recommendProduct/list" method="post" class="breadcrumb form-search">
+		<input id="pageNo" name="pageNo" type="hidden" value="${page.pageNo}"/>
+		<input id="pageSize" name="pageSize" type="hidden" value="${page.pageSize}"/>
+		<div class="ul-form">
+			  <input type="button" class="btn btn-primary" value="添加商品" onclick="showSelect()"/>&nbsp&nbsp&nbsp
+              <c:if test="${not empty page.list}">
+                    <input type="button" class="btn btn-primary" value="批量排序" onclick="batchSaveSort()"/>
+              </c:if>
+              <br><font color="#a9a9a9">&nbsp;&nbsp;&nbsp;排序规则:按照排序数值升序排列,数值越小排序越靠前,排序值允许重复。</font>
+			<div class="clearfix"></div>
+		</div>
+	</form:form>
+	<sys:message content="${message}"/>
+	<table id="contentTable" class="table table-striped table-bordered table-condensed">
+		<thead>
+			<tr>
+				<th>商品ID</th>
+				<th>商品图片</th>
+				<th>商品名称</th>
+				<th>供应商</th>
+				<th>排序值</th>
+				<th>操作</th>
+			</tr>
+		</thead>
+		<c:if test="${not empty page.list}">
+            <c:forEach items="${page.list}" var="product">
+            <tbody>
+                <tr>
+                	<input class="check-item" type="hidden" id="sort${product.productID}"
+                       value='${product.productID}-${product.sortIndex}'/>
+                    <td>
+                        ${product.productID}
+                    </td>
+                    <td><img src="${product.mainImage}" width="50px" height="50px"></td>
+                    <td>
+                        <a href="https://www.caimei365.com/product-${product.productID}.html" target="_blank">${product.name}</a>
+                    </td>
+                    <td>
+                        ${product.shopName}
+                    </td>
+                    <td>
+                        <input id="sort" name="sort" style="width:50px;" value="${product.sortIndex}"
+                           onkeyup="onlynum(this)" onchange="changeSort(${product.productID},this)">
+                    </td>
+                    <td>
+                        <a href="${ctx}/newhome/recommendProduct/deleteProduct?productId=${product.productID}" onclick="return confirmx('确认删除吗?', this.href)">删除</a>
+                    </td>
+                </tr>
+			</tbody>
+		</c:forEach>
+        </c:if>
+	</table>
+	<c:if test="${empty page.list}">
+            <p style="text-align: center;"><font  color="#1e90ff">暂无数据……</font></p>
+    </c:if>
+	<div class="pagination">${page}</div>
+</body>
+</html>

+ 126 - 0
src/main/webapp/WEB-INF/views/modules/newhome/toSelectRecommendProduct.jsp

@@ -0,0 +1,126 @@
+<%@ page contentType="text/html;charset=UTF-8" %>
+<%@ include file="/WEB-INF/views/include/taglib.jsp" %>
+<html>
+<head>
+    <title>选择商品</title>
+    <meta name="decorator" content="default"/>
+    <style type="text/css">
+        .table td i {
+            margin: 0 2px;
+        }
+    </style>
+    <script type="text/javascript">
+        $(document).ready(function () {
+            //弹出框去滚动条
+            top.$('#jbox-content').css("overflow-y", "hidden");
+            show_title(30);
+
+//			反选
+            $('body').on('click', 'input[name="info"]', function () {
+                var allInputLength = $('input[name="info"]').length - $('input[name="info"]:disabled').length,
+                    allInputCheckedLength = $('input[name="info"]:checked').length,
+                    checkAllEle = $('.check-all');
+//			    判断选中长度和总长度,如果相等就是全选否则取消全选
+                if (allInputLength === allInputCheckedLength) {
+                    checkAllEle.attr('checked', true);
+                } else {
+                    checkAllEle.attr('checked', false);
+                }
+            })
+        });
+
+        function page(n, s) {
+            $("#pageNo").val(n);
+            $("#pageSize").val(s);
+            $("#searchForm").submit();
+            return false;
+        }
+
+        function getCheckedItems() {
+            var items = "";
+            var $items = $('.check-item:checked');
+            $items.each(function () {
+                //通过拿到的商品ID串(逗号隔开)
+                var productID = $(this).val();
+                items = items + productID + ","
+            });
+            items = items.substring(0, items.length - 1);
+            return items;
+        }
+
+        function allCkbfun(ckb) {
+            var isChecked = ckb.checked;
+            $(".check-item").attr('checked', isChecked);
+        }
+
+        /**
+         * @param obj
+         * jquery控制input只能输入数字
+         */
+        function onlynum(obj) {
+            obj.value = obj.value.replace(/[^\d]/g, ""); //清除"数字"以外的字符
+        }
+
+        /**
+         * @param obj
+         * jquery控制input只能输入数字和两位小数(金额)
+         */
+        function num(obj) {
+            obj.value = obj.value.replace(/[^\d.]/g, ""); //清除"数字"和"."以外的字符
+            obj.value = obj.value.replace(/^\./g, ""); //验证第一个字符是数字
+            obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一个, 清除多余的
+            obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
+            obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); //只能输入两个小数
+        }
+
+    </script>
+</head>
+<body>
+<form:form id="searchForm" modelAttribute="product"
+           action="${ctx}/newhome/recommendProduct/toAddProduct" method="post"
+           class="breadcrumb form-search">
+    <input id="pageNo" name="pageNo" type="hidden" value="${page.pageNo}"/>
+    <input id="pageSize" name="pageSize" type="hidden" value="${page.pageSize}"/>
+    <div class="ul-form">
+        <label>商品ID:</label>
+        <form:input path="productID" htmlEscape="false" maxlength="10" class="input-mini" onchange="onlynum(this)"/>
+        <label>商品名称:</label>
+        <form:input path="productName" htmlEscape="false" class="input-medium" maxlength="20"/>
+        <label>供应商:</label>
+        <form:input path="shopName" htmlEscape="false" class="input-medium" maxlength="20"/>
+        &nbsp;&nbsp;<input id="btnSubmit" class="btn btn-primary" type="submit" value="搜索"/>
+        <div class="clearfix"></div>
+    </div>
+</form:form>
+<sys:message content="${message}"/>
+<table class="table table-striped table-bordered table-condensed table-hover">
+    <tr>
+        <th style="width:20px;"><input class="check-all" type="checkbox" onclick="allCkbfun(this);"/></th>
+        <th>商品ID</th>
+        <th>商品图片</th>
+        <th>商品名称</th>
+        <th>供应商</th>
+    </tr>
+    <tbody>
+    <c:if test="${not empty page.list}">
+        <c:forEach items="${page.list}" var="item">
+            <tr id="${item.productID}" class="itemtr">
+                    <%--已过滤添加过的商品和未上架的商品--%>
+                <th>
+                    <input class="check-item" type="checkbox" name="info" value='${item.productID}'/>
+                </th>
+                <td>${item.productID}</td>
+                <td><img src="${item.mainImage}" width="50px" height="50px"></td>
+                <td>${item.name}</td>
+                <td>${item.shopName}</td>
+            </tr>
+        </c:forEach>
+    </c:if>
+    </tbody>
+</table>
+<c:if test="${empty page.list}">
+    <p style="text-align: center;"><font color="#1e90ff">暂无数据……</font></p>
+</c:if>
+<div class="pagination">${page}</div>
+</body>
+</html>