猿实战是一个原创系列文章,通过实战的方式,采用前后端分离的技术结合SpringMVC Spring Mybatis,手把手教你撸一个完整的电商系统,变身猿人找到工作不是问题。还等什么呢?关注公号,取基础代码,一起实战吧。
上一个章节,猿人君教会了你如何去设计和实现运费的基础数据——管理和维护承运商信息。今天我们一起来学习,如何实现运费计算的支柱模块——运费模板。
功能概览
运费模板,的出现是为了解决繁杂的商品运费计算问题,它的功能比较复杂,系统需要提供设置默认运费模板功能,也可以新增运费模板,一般而言,运费模板分为系统默认模板和自定义模板两类。
系统的默认模板用于处理未设置运费模板的商品运费计算。
自定义模板,用于计算设置地区的运费计算,其中计费方式支持按计费类型来支持按重量、体积、数量的计费方式(当运费类型为自定义运费时才支持这一选项)。
在列表页面,系统模板不允许新增和修改操作。
数据库设计
基于之前的设计文章猿设计14——真电商之运费模板,我们可以快速地整理运费模板的基本信息,并落地为数据库表,如上图所示。
后端功能实现
运费模板的后台管理功能相对传统,提供新增/修改/停用/启用删除/分页列表的功能。
/**
* Copyright(c) 2004-2020 pangzi
* com.pz.basic.mall.controller.freight.MallFreightTempleteController.java
*/
package com.pz.basic.mall.controller.freight;
import com.pz.basic.mall.domain.base.Result;
import com.pz.basic.mall.domain.base.enums.DataStatusEnum;
import com.pz.basic.mall.domain.freight.MallFreightTemplete;
import com.pz.basic.mall.domain.freight.query.QueryMallFreightTemplete;
import com.pz.basic.mall.domain.sys.AreaSelectedVo;
import com.pz.basic.mall.service.freight.MallFreightTempleteService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
*
* @author pangzi
* @date 2020-06-22 20:47:27
*
*
*/
@RestController
@RequestMapping("/freightTemplete")
public class MallFreightTempleteController {
private MallFreightTempleteService mallFreightTempleteService;
public void setMallFreightTempleteService(MallFreightTempleteService mallFreightTempleteService) {
this.mallFreightTempleteService = mallFreightTempleteService;
}
/**
* 新增标准运费
* @param mallFreightTemplete
* @return
*/
@RequestMapping("/addMallFreightTemplete")
public Result<MallFreightTemplete> addMallFreightTemplete(@RequestBody MallFreightTemplete mallFreightTemplete){
try{
return mallFreightTempleteService.addMallFreightTemplete(mallFreightTemplete);
}catch(Exception e){
e.printStackTrace();
return new Result(false);
}
}
/**
* 根据ID查找标准运费
* @param id
* @return
*/
@RequestMapping("/findMallFreightTempleteById")
public Result<MallFreightTemplete> findMallFreightTempleteById(Long id){
return mallFreightTempleteService.getMallFreightTempleteById(id);
}
/**
* 修改标准运费
* @param mallFreightTemplete
* @return
*/
@RequestMapping("/updateMallFreightTemplete")
public Result updateMallFreightTemplete(@RequestBody MallFreightTemplete mallFreightTemplete){
try{
return mallFreightTempleteService.updateMallFreightTempleteById(mallFreightTemplete);
}catch(Exception e){
e.printStackTrace();
return new Result(false);
}
}
/**
* 启用标准运费
* @param mallFreightTemplete
* @return
*/
@RequestMapping("/enableMallFreightTemplete")
public Result enableMallFreightTemplete(@RequestBody MallFreightTemplete mallFreightTemplete){
try{
MallFreightTemplete modifiedData =new MallFreightTemplete ();
modifiedData.setTempleteId(mallFreightTemplete.getTempleteId());
modifiedData.setStatus(DataStatusEnum.STATUS_ENABLE.getStatusValue());
return mallFreightTempleteService.updateMallFreightTempleteById(modifiedData);
}catch(Exception e){
e.printStackTrace();
return new Result(false);
}
}
/**
* 停用标准运费
* @param mallFreightTemplete
* @return
*/
@RequestMapping("/disableMallFreightTemplete")
public Result disableMallFreightTemplete(@RequestBody MallFreightTemplete mallFreightTemplete){
try{
MallFreightTemplete modifiedData =new MallFreightTemplete ();
modifiedData.setTempleteId(mallFreightTemplete.getTempleteId());
modifiedData.setStatus(DataStatusEnum.STATUS_DISABLE.getStatusValue());
return mallFreightTempleteService.updateMallFreightTempleteById(modifiedData);
}catch(Exception e){
e.printStackTrace();
return new Result(false);
}
}
/**
* 删除标准运费
* @param mallFreightTemplete
* @return
*/
@RequestMapping("/deleteMallFreightTemplete")
public Result deleteMallFreightTemplete(@RequestBody MallFreightTemplete mallFreightTemplete){
try{
MallFreightTemplete modifiedData =new MallFreightTemplete ();
modifiedData.setTempleteId(mallFreightTemplete.getTempleteId());
modifiedData.setStatus(DataStatusEnum.STATUS_DELETED.getStatusValue());
return mallFreightTempleteService.updateMallFreightTempleteById(modifiedData);
}catch(Exception e){
e.printStackTrace();
return new Result(false);
}
}
/**
* 分页返回标准运费列表
* @param queryMallFreightTemplete
* @return
*/
@RequestMapping("/findByPage")
public Result<List<MallFreightTemplete>> findByPage(@RequestBody QueryMallFreightTemplete queryMallFreightTemplete){
return mallFreightTempleteService.getMallFreightTempletesByPage(queryMallFreightTemplete);
}
/**
* 获取默认运费模板
* @param queryMallFreightTemplete
* @return
*/
@RequestMapping("/findDefaultlFreightTemplete")
public Result<MallFreightTemplete> findDefaultlFreightTemplete(@RequestBody QueryMallFreightTemplete queryMallFreightTemplete){
return mallFreightTempleteService.findDefaultlFreightTemplete(queryMallFreightTemplete);
}
@RequestMapping("/selectForFreightTemplete")
public Result<List<AreaSelectedVo>> selectForFreightTemplete(@RequestBody QueryMallFreightTemplete queryMallFreightTemplete){
return mallFreightTempleteService.selectForFreightTemplete(queryMallFreightTemplete);
}
}
考虑到很多朋友编写mapper文件比较困难,这个章节的mapper就先给到你吧,不过做人不要太懒了,domain 和dao 以及service的实现,还是自己动手搞一下吧。
<?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.pz.basic.mall.dao.freight.MallFreightTempleteDao">
<resultMap id="ResultMap" type="MallFreightTemplete">
<id property="templeteId" column="templete_id"/>
<id property="templeteName" column="templete_name"/>
<id property="logisticsCode" column="logistics_code"/>
<id property="logisticsName" column="logistics_name"/>
<id property="templeteType" column="templete_type"/>
<id property="serviceType" column="service_type"/>
<id property="freightType" column="freight_type"/>
<id property="chargeType" column="charge_type"/>
<id property="numMin" column="num_min"/>
<id property="numMax" column="num_max"/>
<id property="fristNumPrice" column="frist_num_price"/>
<id property="nextNumPrice" column="next_num_price"/>
<id property="weightMin" column="weight_min"/>
<id property="weightMax" column="weight_max"/>
<id property="fristWeightPrice" column="frist_weight_price"/>
<id property="nextWeightPrice" column="next_weight_price"/>
<id property="volumeMin" column="volume_min"/>
<id property="volumeMax" column="volume_max"/>
<id property="fristVolumePrice" column="frist_volume_price"/>
<id property="nextVolumePrice" column="next_volume_price"/>
<id property="status" column="status"/>
<id property="createUser" column="create_user"/>
<id property="modifyUser" column="modify_user"/>
<id property="created" column="created"/>
<id property="modified" column="modified"/>
</resultMap>
<sql id="ALL_TABLE_COLOUM">
templete_id,
templete_name,
logistics_code,
logistics_name,
templete_type,
service_type,
freight_type,
charge_type,
num_min,
num_max,
frist_num_price,
next_num_price,
weight_min,
weight_max,
frist_weight_price,
next_weight_price,
volume_min,
volume_max,
frist_volume_price,
next_volume_price,
status,
create_user,
modify_user,
created,
modified
</sql>
<sql id="Query_Where_Clause" >
<where >
status>-1
<if test="templeteId != null and templeteId != ''">
and templete_id = #{templeteId}
</if>
<if test="templeteName != null and templeteName != ''">
and templete_name = #{templeteName}
</if>
<if test="logisticsCode != null and logisticsCode != ''">
and logistics_code = #{logisticsCode}
</if>
<if test="logisticsName != null and logisticsName != ''">
and logistics_name = #{logisticsName}
</if>
<if test="templeteType != null and templeteType != ''">
and templete_type = #{templeteType}
</if>
<if test="serviceType != null and serviceType != ''">
and service_type = #{serviceType}
</if>
<if test="freightType != null and freightType != ''">
and freight_type = #{freightType}
</if>
<if test="chargeType != null and chargeType != ''">
and charge_type = #{chargeType}
</if>
<if test="numMin != null and numMin != ''">
and num_min = #{numMin}
</if>
<if test="numMax != null and numMax != ''">
and num_max = #{numMax}
</if>
<if test="fristNumPrice != null and fristNumPrice != ''">
and frist_num_price = #{fristNumPrice}
</if>
<if test="nextNumPrice != null and nextNumPrice != ''">
and next_num_price = #{nextNumPrice}
</if>
<if test="weightMin != null and weightMin != ''">
and weight_min = #{weightMin}
</if>
<if test="weightMax != null and weightMax != ''">
and weight_max = #{weightMax}
</if>
<if test="fristWeightPrice != null and fristWeightPrice != ''">
and frist_weight_price = #{fristWeightPrice}
</if>
<if test="nextWeightPrice != null and nextWeightPrice != ''">
and next_weight_price = #{nextWeightPrice}
</if>
<if test="volumeMin != null and volumeMin != ''">
and volume_min = #{volumeMin}
</if>
<if test="volumeMax != null and volumeMax != ''">
and volume_max = #{volumeMax}
</if>
<if test="fristVolumePrice != null and fristVolumePrice != ''">
and frist_volume_price = #{fristVolumePrice}
</if>
<if test="nextVolumePrice != null and nextVolumePrice != ''">
and next_volume_price = #{nextVolumePrice}
</if>
<if test="status != null and status != ''">
and status = #{status}
</if>
<if test="createUser != null and createUser != ''">
and create_user = #{createUser}
</if>
<if test="modifyUser != null and modifyUser != ''">
and modify_user = #{modifyUser}
</if>
<if test="created != null and created != ''">
and created = #{created}
</if>
<if test="modified != null and modified != ''">
and modified = #{modified}
</if>
</where>
</sql>
<select id="selectMallFreightTempleteByQuery" resultMap="ResultMap" parameterType="QueryMallFreightTemplete" >
select
<include refid="ALL_TABLE_COLOUM" />
from mall_freight_templete
<if test="page != null" >
<include refid="Query_Where_Clause" />
</if>
</select>
<select id="selectMallFreightTempleteByPage" resultMap="ResultMap" parameterType="QueryMallFreightTemplete" >
select
<include refid="ALL_TABLE_COLOUM" />
from mall_freight_templete
<if test="page != null" >
<include refid="Query_Where_Clause" />
</if>
LIMIT #{startRow},#{pageS