商品管理分页查询实现

<template>
  <el-card>
    <el-row :gutter="20" class="header">
      <el-col :span="7">
        <el-input placeholder="请输入商品名称..." clearable v-model="queryForm.query"></el-input>
      </el-col>
      <el-button type="primary" :icon="Search" @click="initProductList">搜索</el-button>
      <el-button type="primary" @click="handleDialogValue()">添加商品</el-button>
    </el-row>
    <el-table :data="tableData" stripe style="width: 100%">

      <el-table-column prop="name" label="商品名称" width="200" fixed/>
      <el-table-column prop="image" label="商品图片" width="150" align="center">
        <template v-slot="scope">
          <img :src="getServerUrl()+'/image/product/'+scope.row.proPic" width="80" height="80"/>
        </template>
      </el-table-column>
      <el-table-column prop="price" label="商品价格" width="100" />
      <el-table-column prop="stock" label="商品库存" width="200" />
      <el-table-column prop="type" label="商品类别" width="200"  :formatter="typeNameFormatter"/>
      <el-table-column prop="hot" label="热卖?" width="100" align="center">
        <template v-slot="{row}">
            <el-switch v-model="row.hot"></el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="swiper" label="首页幻灯?" width="100" align="center">
        <template v-slot="{row}">
          <el-switch v-model="row.swiper"></el-switch>
        </template>
      </el-table-column>

      <el-table-column prop="swiperPic" label="幻灯图片" width="150" align="center">
        <template v-slot="scope">
          <img :src="getServerUrl()+'/image/swiper/'+scope.row.swiperPic" width="150" height="75"/>
        </template>
      </el-table-column>
      <el-table-column prop="swiperSort" label="幻灯排序" width="150" align="center"/>
      <el-table-column prop="description" label="商品描述" width="400"/>


      <el-table-column prop="action" label="操作" width="500" fixed="right">
        <template v-slot="scope">
          <el-button type="success"  @click="handleDialogValue(scope.row)">更换图片</el-button>

          <el-button type="primary"  @click="handleDialogValue(scope.row)">幻灯设置</el-button>

          <el-button type="primary" :icon="Edit" @click="handleDialogValue(scope.row)"></el-button>
          <el-button type="danger" :icon="Delete" @click="handleDelete(scope.row.id)"></el-button>

          <el-button type="primary" :icon="Edit" @click="handleDialogValue(scope.row)">轮播图设置</el-button>

        </template>

      </el-table-column>

    </el-table>

    <el-pagination
        v-model:currentPage="queryForm.pageNum"
        :page-sizes="[10, 20, 30, 40,50]"
        :page-size="queryForm.pageSize"
        :small="small"
        :disabled="disabled"
        :background="background"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    >
    </el-pagination>
  </el-card>

  <Dialog v-model="dialogVisible" :dialogTitle="dialogTitle" @initProductList="initProductList"  :dialogValue="dialogValue"/>

</template>

<script setup>

import {Search,Edit,Delete } from '@element-plus/icons-vue'
import { ref } from 'vue'
import  axios,{ getServerUrl }  from '@/util/axios'
import Dialog from './components/dialog'

import {ElMessageBox,ElMessage} from 'element-plus'

const queryForm=ref({
  query:'',
  pageNum:1,
  pageSize:10
})

const total=ref(0)


const tableData=ref([
])

const dialogValue=ref({})

const dialogTitle=ref('')

const initProductList=async()=>{
  console.log('xxx')
  const res=await axios.post("admin/product/list",queryForm.value);
  tableData.value=res.data.productList;
  total.value=res.data.total;
}

initProductList();

const dialogVisible=ref(false)



const handleSizeChange=(pageSize)=>{
  queryForm.value.pageNum=1;
  queryForm.value.pageSize=pageSize;
  initProductList();
}

const handleCurrentChange=(pageNum)=>{
  queryForm.value.pageNum=pageNum;
  initProductList();
}


const handleDialogValue = (row) => {
  if(row){
    dialogValue.value=JSON.parse(JSON.stringify(row));
    dialogTitle.value="商品修改"
  }else{
    dialogValue.value={
      bigType:{
        id:""
      }
    }
    dialogTitle.value="商品添加"
  }
  dialogVisible.value=true;
}

const handleDelete = (id) => {

  ElMessageBox.confirm(
      '您确定要删除这条记录吗?',
      '系统提示',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(async() => {
        console.log("id="+id)
        let res=await axios.get("admin/product/delete/"+id);
        if(res.data.code==0){
          ElMessage({
            type: 'success',
            message: '删除成功!',
          });
          initProductList();
        }else{
          ElMessage({
            type: 'error',
            message: res.data.msg,
          });
        }

      })
      .catch(() => {

      })
}


const typeNameFormatter=(row)=>{
  return row.type.name
}


</script>

<style lang="scss" scoped>

.header{
  padding-bottom: 16px;
  box-sizing: border-box;
}

.el-pagination{
  padding-top: 15px;
  box-sizing: border-box;
}


</style>
<?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.java1234.mapper.SmallTypeMapper">

    <resultMap id="smallTypeResult" type="com.java1234.entity.SmallType">
        <association property="bigType" column="bigTypeId" select="com.java1234.mapper.BigTypeMapper.findById">

        </association>
    </resultMap>


    <select id="findById" parameterType="Integer" resultMap="smallTypeResult">
        select * from t_smallType where id=#{id}
    </select>


    <select id="list" parameterType="Map" resultMap="smallTypeResult">
        select * from t_smallType
        <where>
            <if test="name!=null and name!='' ">
                and name like concat('%',#{name},'%')
            </if>
        </where>
        <if test="start!=null and pageSize!=null">
            limit #{start},#{pageSize}
        </if>
    </select>

    <select id="getTotal" parameterType="Map" resultType="Long">
        select count(*) from t_smallType
        <where>
            <if test="name!=null and name!='' ">
                and name like concat('%',#{name},'%')
            </if>
        </where>
    </select>


    <insert id="add" parameterType="com.java1234.entity.SmallType">
        insert into t_smallType values(null,#{name},#{remark},#{bigType.id});
    </insert>

    <update id="update" parameterType="com.java1234.entity.SmallType">
        update t_smallType
        <set>
            <if test="name!=null and name!=''">
                name=#{name},
            </if>
            <if test="remark!=null and remark!=''">
                remark=#{remark},
            </if>
            <if test="bigType.id!=null">
                bigTypeId=#{bigType.id},
            </if>
        </set>
        where id=#{id}
    </update>
</mapper>
<?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.java1234.mapper.ProductMapper">

    <resultMap id="productResult" type="com.java1234.entity.Product">
        <association property="type" column="typeId" select="com.java1234.mapper.SmallTypeMapper.findById">

        </association>
    </resultMap>

    <select id="list" parameterType="Map" resultMap="productResult">
        select * from t_product
        <where>
            <if test="name!=null and name!='' ">
                and name like concat('%',#{name},'%')
            </if>
        </where>
        <if test="start!=null and pageSize!=null">
            limit #{start},#{pageSize}
        </if>
    </select>

    <select id="getTotal" parameterType="Map" resultType="Long">
        select count(*) from t_product
        <where>
            <if test="name!=null and name!='' ">
                and name like concat('%',#{name},'%')
            </if>
        </where>
    </select>


    <insert id="add" parameterType="com.java1234.entity.Product">
        insert into t_product values(null,#{name},#{remark},#{bigType.id});
    </insert>

    <update id="update" parameterType="com.java1234.entity.Product">
        update t_product
        <set>
            <if test="name!=null and name!=''">
                name=#{name},
            </if>
            <if test="remark!=null and remark!=''">
                remark=#{remark},
            </if>
            <if test="bigType.id!=null">
                bigTypeId=#{bigType.id},
            </if>
        </set>
        where id=#{id}
    </update>
</mapper>
package com.java1234.controller.admin;

import com.java1234.entity.PageBean;
import com.java1234.entity.Product;
import com.java1234.entity.R;
import com.java1234.entity.SmallType;
import com.java1234.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 管理端-商品Controller控制器
 * @author java1234_小锋
 * @site www.java1234.com
 * @company 南通小锋网络科技有限公司
 * @create 2022-02-14 6:54
 */
@RestController
@RequestMapping("/admin/product")
public class AdminProductController {


    @Autowired
    private IProductService productService;
    /**
     * 根据条件分页查询
     * @param pageBean
     * @return
     */
    @RequestMapping("/list")
    public R list(@RequestBody PageBean pageBean){
        System.out.println(pageBean);
        Map<String,Object> map=new HashMap<>();
        map.put("name",pageBean.getQuery().trim());
        map.put("start",pageBean.getStart());
        map.put("pageSize",pageBean.getPageSize());
        List<Product> productList = productService.list(map);
        Long total = productService.getTotal(map);

        Map<String,Object> resultMap=new HashMap<>();
        resultMap.put("productList",productList);
        resultMap.put("total",total);
        return R.ok(resultMap);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值