牛刀小试.开胃菜·代码思路·9-5
大约 4 分钟
牛刀小试.开胃菜·代码思路·9-5
9-5-线索管理-添加线索-活动信息-应该只展示 正在活动时间内的活动
1.思路:
- 确定前端页面位置
- F12查看对应的后端接口位置,定位对应的异常信息打印的原因
- 解决BUG
2.解决步骤
提示
- 定位接口
- 查看接口代码
- 确定bug所在位置
- 修改bug
1️⃣ 1.前端发起请求
先找到这部分内容
通过前端页面我们可以看到:

👉访问的接口是:/clues/activity/listselect/
2️⃣ 2.找到后端接口
我们在后端找到对应的接口
使用

定位到代码在TbActivityController中

定位到后端的代码之后
3️⃣ 3.定位BUG位置
后端逻辑:
类名:TbActivityController
/**
* 获取渠道下活动
* @param channel
* @return
*/
@GetMapping("/listselect/{channel}")
public AjaxResult list(@PathVariable("channel") String channel)
{
TbActivity tbActivity =new TbActivity();
tbActivity.setChannel(channel);
tbActivity.setStatus("2");
return AjaxResult.success(tbActivityService.selectTbActivityList(tbActivity));
}
在controller层中我们看到代码中构建了实体类,并封装了一部分属性,对于时间的判断并没有
类名:ITbActivityService
/**
* 查询活动管理列表
*
* @param tbActivity 活动管理
* @return 活动管理集合
*/
public List<TbActivity> selectTbActivityList(TbActivity tbActivity);
类:对应的实现类
/**
* 查询活动管理列表
*
* @param tbActivity 活动管理
* @return 活动管理
*/
@Override
public List<TbActivity> selectTbActivityList(TbActivity tbActivity) {
return tbActivityMapper.selectTbActivityList(tbActivity);
}
在业务层并没有做任何的操作,直接调用Mapper查询数据库
类名:TbActivityMapper
/**
* 查询活动管理列表
*
* @param tbActivity 活动管理
* @return 活动管理集合
*/
public List<TbActivity> selectTbActivityList(TbActivity tbActivity);
TbActivityMapper.xml
<sql id="selectTbActivityVo">
select id, name, channel, info, type, discount, vouchers, status, begin_time, end_time,code,create_time from tb_activity
</sql>
<select id="selectTbActivityList" parameterType="TbActivity" resultMap="TbActivityResult">
<include refid="selectTbActivityVo"/>
<where>
<if test="channel != null and channel != ''"> and channel = #{channel}</if>
<if test="code != null and code != ''"> and code like concat('%', #{code}, '%')</if>
<if test="name != null and name != ''"> and name like concat('%', #{name}, '%')</if>
<if test="info != null and info != ''"> and info = #{info}</if>
<if test="type != null and type != ''"> and type = #{type}</if>
<if test="discount != null "> and discount = #{discount}</if>
<if test="vouchers != null "> and vouchers = #{vouchers}</if>
<if test="status != null and status != ''"> and status = #{status}</if>
<if test="params.beginCreateTime != null and params.beginCreateTime != ''"><!-- 开始创建时间 -->
and date_format(create_time,'%y%m%d') >= date_format(#{params.beginCreateTime},'%y%m%d')
</if>
<if test="params.endCreateTime != null and params.endCreateTime != ''"><!-- -->
and date_format(create_time,'%y%m%d') <= date_format(#{params.endCreateTime},'%y%m%d')
</if>
<if test="params.beginTime != null and params.beginTime != ''"><!-- 活动开始时间 -->
and date_format(begin_time,'%y%m%d') >= date_format(#{params.beginTime},'%y%m%d')
</if>
<if test="params.endTime != null and params.endTime != ''"><!-- 活动结束时间 -->
and date_format(end_time,'%y%m%d') <= date_format(#{params.endTime},'%y%m%d')
</if>
</where>
ORDER BY `create_time` DESC
</select>
4️⃣ 4.bug分析与理解
可以看到在中实际上是有对时间的操作的但是在sql语句中比较的是即 ,我们应该比较的是 来看数据库 👇

有效时间范围应该是begin_time和end_time这两个字段
我们其实只需要比较字段,如果数据库中的字段大于我们的当前时间,则我们可以认为,是在有效期内的 🎉
那么怎么修改呢?❓
5️⃣ 5.修改BUG
- 首先修改controller层,在构建活动类的时候初始化一个当前时间
/**
* 获取渠道下活动
* @param channel
* @return
*/
@GetMapping("/listselect/{channel}")
public AjaxResult list(@PathVariable("channel") String channel)
{
TbActivity tbActivity =new TbActivity();
tbActivity.setChannel(channel);
tbActivity.setStatus("2");
tbActivity.setParams(new HashMap(){
{
put("now",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
}
});
return AjaxResult.success(tbActivityService.selectTbActivityList(tbActivity));
}
- 需要修改sql,判断数据库中的
SELECT id, name, channel, info, type
, discount, vouchers, status, begin_time, end_time
, code, create_time
FROM tb_activity
-- '2019-05-05' 指的是当前的时间
WHERE '2019-05-05' between begin_time and end_time
ORDER BY `create_time` DESC
我们的where条件后应该加上日期的比较,判断是否在有效期范围内
修改xml文件,添加判断条件
<if test="params.now != null and params.now != ''"><!-- 处理当前时间 -->
and #{params.now} between begin_time and end_time
</if>
修改后的xml文件如下所示
<select id="selectTbActivityList" parameterType="TbActivity" resultMap="TbActivityResult">
<include refid="selectTbActivityVo"/>
<where>
<if test="channel != null and channel != ''"> and channel = #{channel}</if>
<if test="code != null and code != ''"> and code like concat('%', #{code}, '%')</if>
<if test="name != null and name != ''"> and name like concat('%', #{name}, '%')</if>
<if test="info != null and info != ''"> and info = #{info}</if>
<if test="type != null and type != ''"> and type = #{type}</if>
<if test="discount != null "> and discount = #{discount}</if>
<if test="vouchers != null "> and vouchers = #{vouchers}</if>
<if test="status != null and status != ''"> and status = #{status}</if>
<if test="params.beginCreateTime != null and params.beginCreateTime != ''"><!-- 开始创建时间 -->
and date_format(create_time,'%y%m%d') >= date_format(#{params.beginCreateTime},'%y%m%d')
</if>
<if test="params.endCreateTime != null and params.endCreateTime != ''"><!-- -->
and date_format(create_time,'%y%m%d') <= date_format(#{params.endCreateTime},'%y%m%d')
</if>
<if test="params.beginTime != null and params.beginTime != ''"><!-- 活动开始时间 -->
and date_format(begin_time,'%y%m%d') >= date_format(#{params.beginTime},'%y%m%d')
</if>
<if test="params.endTime != null and params.endTime != ''"><!-- 活动结束时间 -->
and date_format(end_time,'%y%m%d') <= date_format(#{params.endTime},'%y%m%d')
</if>
<if test="params.now != null and params.now != ''"><!-- 处理当前时间 -->
and #{params.now} between begin_time and end_time
</if>
</where>
ORDER BY `create_time` DESC
</select>
前后端联调结果 🎉 🎉 🎉
