|
|
package com.controller;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.*;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
|
import com.utils.ValidatorUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
import com.baomidou.mybatisplus.mapper.EntityWrapper;
|
|
|
import com.baomidou.mybatisplus.mapper.Wrapper;
|
|
|
import com.annotation.IgnoreAuth;
|
|
|
import com.entity.JijianxinxiEntity;
|
|
|
import com.entity.view.JijianxinxiView;
|
|
|
import com.service.JijianxinxiService;
|
|
|
import com.utils.PageUtils;
|
|
|
import com.utils.R;
|
|
|
import com.utils.MPUtil;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 寄件信息管理控制器
|
|
|
* 核心功能:提供寄件信息的全生命周期管理(增删改查)
|
|
|
* 数据隔离:实现用户级数据权限控制
|
|
|
* 技术栈:Spring Boot + MyBatis Plus
|
|
|
*/
|
|
|
@RestController
|
|
|
@RequestMapping("/jijianxinxi")
|
|
|
public class JijianxinxiController {
|
|
|
|
|
|
@Autowired
|
|
|
private JijianxinxiService jijianxinxiService;
|
|
|
|
|
|
// ======================== 分页查询接口 ========================
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 后端分页列表(需登录)
|
|
|
* 路径:GET /jijianxinxi/page
|
|
|
* 特性:
|
|
|
* - 自动根据用户角色过滤数据
|
|
|
* - 支持排序/筛选/分页参数
|
|
|
* 参数:
|
|
|
*
|
|
|
* @RequestParam Map<String, Object> params - 分页参数(page,limit)及筛选条件
|
|
|
* JijianxinxiEntity jijianxinxi - 实体对象(封装查询条件)
|
|
|
* HttpServletRequest request - 获取会话信息
|
|
|
*/
|
|
|
@RequestMapping("/page")
|
|
|
public R page(@RequestParam Map<String, Object> params, JijianxinxiEntity jijianxinxi,
|
|
|
HttpServletRequest request) {
|
|
|
// 从会话获取用户身份(实现数据隔离)
|
|
|
|
|
|
String tableName = request.getSession().getAttribute("tableName").toString();
|
|
|
|
|
|
|
|
|
// 普通用户只能查看自己的寄件信息
|
|
|
if (tableName.equals("yonghu")) {
|
|
|
jijianxinxi.setYonghuzhanghao((String) request.getSession().getAttribute("username"));
|
|
|
}
|
|
|
|
|
|
|
|
|
// 构建查询条件
|
|
|
EntityWrapper<JijianxinxiEntity> ew = new EntityWrapper<>();
|
|
|
|
|
|
|
|
|
// 执行分页查询(自动处理排序/筛选)
|
|
|
PageUtils page = jijianxinxiService.queryPage(
|
|
|
params,
|
|
|
MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, jijianxinxi), params), params)
|
|
|
);
|
|
|
|
|
|
return R.ok().put("data", page);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 前端分页列表(开放接口)
|
|
|
* 路径:GET /jijianxinxi/list
|
|
|
* 特性:无需认证,适合小程序/H5调用
|
|
|
*/
|
|
|
@IgnoreAuth
|
|
|
@RequestMapping("/list")
|
|
|
public R list(@RequestParam Map<String, Object> params, JijianxinxiEntity jijianxinxi,
|
|
|
HttpServletRequest request) {
|
|
|
EntityWrapper<JijianxinxiEntity> ew = new EntityWrapper<>();
|
|
|
PageUtils page = jijianxinxiService.queryPage(
|
|
|
params,
|
|
|
MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, jijianxinxi), params), params)
|
|
|
);
|
|
|
return R.ok().put("data", page);
|
|
|
}
|
|
|
|
|
|
|
|
|
// ======================== 数据操作接口 ========================
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 新增寄件信息
|
|
|
* 路径:POST /jijianxinxi/save
|
|
|
* 特性:自动生成分布式ID(时间戳+随机数)
|
|
|
*/
|
|
|
@RequestMapping("/save")
|
|
|
public R save(@RequestBody JijianxinxiEntity jijianxinxi, HttpServletRequest request) {
|
|
|
// 生成唯一ID(避免并发冲突)
|
|
|
jijianxinxi.setId(System.currentTimeMillis() + (long) (Math.random() * 1000));
|
|
|
|
|
|
// 参数校验(需配置Validator规则)
|
|
|
// ValidatorUtils.validateEntity(jijianxinxi);
|
|
|
|
|
|
jijianxinxiService.insert(jijianxinxi);
|
|
|
return R.ok();
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 更新寄件信息
|
|
|
* 路径:PUT /jijianxinxi/update
|
|
|
* 事务:@Transactional 保证操作原子性
|
|
|
*/
|
|
|
@RequestMapping("/update")
|
|
|
@Transactional
|
|
|
public R update(@RequestBody JijianxinxiEntity jijianxinxi, HttpServletRequest request) {
|
|
|
// ValidatorUtils.validateEntity(jijianxinxi);
|
|
|
jijianxinxiService.updateById(jijianxinxi); // 全字段更新
|
|
|
return R.ok();
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 批量删除寄件信息
|
|
|
* 路径:DELETE /jijianxinxi/delete
|
|
|
* 参数:@RequestBody Long[] ids - ID数组
|
|
|
*/
|
|
|
@RequestMapping("/delete")
|
|
|
public R delete(@RequestBody Long[] ids) {
|
|
|
jijianxinxiService.deleteBatchIds(Arrays.asList(ids));
|
|
|
return R.ok();
|
|
|
}
|
|
|
|
|
|
|
|
|
// ======================== 高级查询接口 ========================
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 条件查询(无分页)
|
|
|
* 路径:GET /jijianxinxi/lists
|
|
|
* 用途:导出Excel/生成报表
|
|
|
*/
|
|
|
@RequestMapping("/lists")
|
|
|
public R list(JijianxinxiEntity jijianxinxi) {
|
|
|
EntityWrapper<JijianxinxiEntity> ew = new EntityWrapper<>();
|
|
|
ew.allEq(MPUtil.allEQMapPre(jijianxinxi, "jijianxinxi")); // 自动转换查询条件
|
|
|
return R.ok().put("data", jijianxinxiService.selectListView(ew));
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 单条查询
|
|
|
* 路径:GET /jijianxinxi/query
|
|
|
*/
|
|
|
@RequestMapping("/query")
|
|
|
public R query(JijianxinxiEntity jijianxinxi) {
|
|
|
EntityWrapper<JijianxinxiEntity> ew = new EntityWrapper<>();
|
|
|
ew.allEq(MPUtil.allEQMapPre(jijianxinxi, "jijianxinxi"));
|
|
|
JijianxinxiView view = jijianxinxiService.selectView(ew);
|
|
|
return R.ok().put("data", view);
|
|
|
}
|
|
|
|
|
|
|
|
|
// ======================== 详情接口 ========================
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 后端详情(需登录)
|
|
|
* 路径:GET /jijianxinxi/info/{id}
|
|
|
*/
|
|
|
@RequestMapping("/info/{id}")
|
|
|
public R info(@PathVariable("id") Long id) {
|
|
|
return R.ok().put("data", jijianxinxiService.selectById(id));
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 前端详情(开放)
|
|
|
* 路径:GET /jijianxinxi/detail/{id}
|
|
|
*/
|
|
|
@IgnoreAuth
|
|
|
@RequestMapping("/detail/{id}")
|
|
|
public R detail(@PathVariable("id") Long id) {
|
|
|
return R.ok().put("data", jijianxinxiService.selectById(id));
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 条件统计提醒(如:未来7天需要处理的寄件数量)
|
|
|
* 路径:GET /jijianxinxi/remind/{columnName}/{type}
|
|
|
* 示例:/remind/jijianriqi/2?remindstart=3&remindend=7 → 统计寄件日期在3天后到7天内的记录数
|
|
|
* 实现逻辑:
|
|
|
* 1. 解析时间参数(动态范围或固定日期)
|
|
|
* 2. 构建MyBatis Plus查询条件
|
|
|
* 3. 附加用户权限过滤(普通用户只能查自己的寄件)
|
|
|
* 4. 返回统计数量
|
|
|
*/
|
|
|
|
|
|
@RequestMapping("/remind/{columnName}/{type}")
|
|
|
public R remindCount(@PathVariable("columnName") String columnName, HttpServletRequest request,
|
|
|
@PathVariable("type") String type, @RequestParam Map<String, Object> map) {
|
|
|
map.put("column", columnName);
|
|
|
map.put("type", type);
|
|
|
|
|
|
|
|
|
// 动态日期范围处理(type=2时生效)
|
|
|
if (type.equals("2")) {
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
Calendar cal = Calendar.getInstance(); // 当前日期
|
|
|
|
|
|
|
|
|
// 处理提醒起始日(remindstart参数)
|
|
|
if (map.get("remindstart") != null) {
|
|
|
int remindStart = Integer.parseInt(map.get("remindstart").toString());
|
|
|
cal.add(Calendar.DAY_OF_MONTH, remindStart); // 计算起始日期
|
|
|
Date remindStartDate = cal.getTime();
|
|
|
map.put("remindstart", sdf.format(remindStartDate)); // 存入map供查询使用
|
|
|
}
|
|
|
|
|
|
|
|
|
// 处理提醒截止日(remindend参数)
|
|
|
if (map.get("remindend") != null) {
|
|
|
int remindEnd = Integer.parseInt(map.get("remindend").toString());
|
|
|
cal.setTime(new Date()); // 重置为当前日期
|
|
|
cal.add(Calendar.DAY_OF_MONTH, remindEnd);
|
|
|
Date remindEndDate = cal.getTime();
|
|
|
map.put("remindend", sdf.format(remindEndDate));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
// 构建查询条件
|
|
|
Wrapper<JijianxinxiEntity> wrapper = new EntityWrapper<>();
|
|
|
|
|
|
// 起始日条件:字段值 >= remindstart
|
|
|
if (map.get("remindstart") != null) {
|
|
|
wrapper.ge(columnName, map.get("remindstart"));
|
|
|
}
|
|
|
|
|
|
// 截止日条件:字段值 <= remindend
|
|
|
if (map.get("remindend") != null) {
|
|
|
wrapper.le(columnName, map.get("remindend"));
|
|
|
}
|
|
|
|
|
|
|
|
|
// 用户级数据权限控制
|
|
|
String tableName = request.getSession().getAttribute("tableName").toString();
|
|
|
if (tableName.equals("yonghu")) { // 普通用户
|
|
|
|
|
|
wrapper.eq("yonghuzhanghao", request.getSession().getAttribute("username"));
|
|
|
}
|
|
|
|
|
|
|
|
|
// 执行统计查询
|
|
|
int count = jijianxinxiService.selectCount(wrapper);
|
|
|
return R.ok().put("count", count);
|
|
|
|
|
|
}
|
|
|
}
|