|
|
package com.controller;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.Calendar;
|
|
|
import java.util.Map;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Iterator;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
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.format.annotation.DateTimeFormat;
|
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
import com.baomidou.mybatisplus.mapper.EntityWrapper;
|
|
|
import com.baomidou.mybatisplus.mapper.Wrapper;
|
|
|
import com.annotation.IgnoreAuth;
|
|
|
|
|
|
import com.entity.QujianxinxiEntity;
|
|
|
import com.entity.view.QujianxinxiView;
|
|
|
|
|
|
import com.service.QujianxinxiService;
|
|
|
import com.service.TokenService;
|
|
|
import com.utils.PageUtils;
|
|
|
import com.utils.R;
|
|
|
import com.utils.MD5Util;
|
|
|
import com.utils.MPUtil;
|
|
|
import com.utils.CommonUtil;
|
|
|
import java.io.IOException;
|
|
|
|
|
|
/**
|
|
|
* 取件信息
|
|
|
* 后端接口
|
|
|
* @author
|
|
|
* @email
|
|
|
* @date 2023-02-17 16:59:28
|
|
|
*/
|
|
|
@RestController // 标识为RESTful控制器,返回JSON数据
|
|
|
@RequestMapping("/qujianxinxi") // 定义基础请求路径
|
|
|
public class QujianxinxiController {
|
|
|
@Autowired // 自动注入取件信息服务
|
|
|
private QujianxinxiService qujianxinxiService;
|
|
|
|
|
|
/**
|
|
|
* 后端列表 - 分页查询取件信息(需要认证)
|
|
|
* @param params 请求参数,包含分页和查询条件
|
|
|
* @param qujianxinxi 取件信息实体对象,用于封装查询条件
|
|
|
* @param request HTTP请求对象
|
|
|
* @return 返回分页数据
|
|
|
*/
|
|
|
@RequestMapping("/page")
|
|
|
public R page(@RequestParam Map<String, Object> params, QujianxinxiEntity qujianxinxi,
|
|
|
HttpServletRequest request){
|
|
|
// 从session中获取表名,用于权限控制
|
|
|
String tableName = request.getSession().getAttribute("tableName").toString();
|
|
|
|
|
|
// 如果是普通用户,只能查看自己的取件信息
|
|
|
if(tableName.equals("yonghu")) {
|
|
|
qujianxinxi.setYonghuzhanghao((String)request.getSession().getAttribute("username"));
|
|
|
}
|
|
|
// 如果是快递员,只能查看自己负责的取件信息
|
|
|
if(tableName.equals("kuaidiyuan")) {
|
|
|
qujianxinxi.setYuangongzhanghao((String)request.getSession().getAttribute("username"));
|
|
|
}
|
|
|
|
|
|
// 创建MyBatis Plus的实体包装器,用于构建查询条件
|
|
|
EntityWrapper<QujianxinxiEntity> ew = new EntityWrapper<QujianxinxiEntity>();
|
|
|
|
|
|
// 构建分页查询:模糊查询/等值查询 -> 范围查询 -> 排序 -> 执行分页查询
|
|
|
PageUtils page = qujianxinxiService.queryPage(params,
|
|
|
MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, qujianxinxi), params), params));
|
|
|
|
|
|
return R.ok().put("data", page); // 返回成功响应和分页数据
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 前端列表 - 分页查询取件信息(无需认证)
|
|
|
*/
|
|
|
@IgnoreAuth // 忽略认证,允许未登录访问
|
|
|
@RequestMapping("/list")
|
|
|
public R list(@RequestParam Map<String, Object> params, QujianxinxiEntity qujianxinxi,
|
|
|
HttpServletRequest request){
|
|
|
EntityWrapper<QujianxinxiEntity> ew = new EntityWrapper<QujianxinxiEntity>();
|
|
|
|
|
|
PageUtils page = qujianxinxiService.queryPage(params,
|
|
|
MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, qujianxinxi), params), params));
|
|
|
return R.ok().put("data", page);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 列表查询 - 查询所有匹配条件的取件信息
|
|
|
*/
|
|
|
@RequestMapping("/lists")
|
|
|
public R list(QujianxinxiEntity qujianxinxi){
|
|
|
EntityWrapper<QujianxinxiEntity> ew = new EntityWrapper<QujianxinxiEntity>();
|
|
|
// 将所有非空属性转换为查询条件
|
|
|
ew.allEq(MPUtil.allEQMapPre(qujianxinxi, "qujianxinxi"));
|
|
|
return R.ok().put("data", qujianxinxiService.selectListView(ew));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 条件查询 - 根据条件查询单个取件信息
|
|
|
*/
|
|
|
@RequestMapping("/query")
|
|
|
public R query(QujianxinxiEntity qujianxinxi){
|
|
|
EntityWrapper<QujianxinxiEntity> ew = new EntityWrapper<QujianxinxiEntity>();
|
|
|
// 将所有非空属性转换为查询条件
|
|
|
ew.allEq(MPUtil.allEQMapPre(qujianxinxi, "qujianxinxi"));
|
|
|
QujianxinxiView qujianxinxiView = qujianxinxiService.selectView(ew);
|
|
|
return R.ok("查询取件信息成功").put("data", qujianxinxiView);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 后端详情 - 根据ID查询取件信息详情(需要认证)
|
|
|
*/
|
|
|
@RequestMapping("/info/{id}")
|
|
|
public R info(@PathVariable("id") Long id){
|
|
|
QujianxinxiEntity qujianxinxi = qujianxinxiService.selectById(id);
|
|
|
return R.ok().put("data", qujianxinxi);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 前端详情 - 根据ID查询取件信息详情(无需认证)
|
|
|
*/
|
|
|
@IgnoreAuth
|
|
|
@RequestMapping("/detail/{id}")
|
|
|
public R detail(@PathVariable("id") Long id){
|
|
|
QujianxinxiEntity qujianxinxi = qujianxinxiService.selectById(id);
|
|
|
return R.ok().put("data", qujianxinxi);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 后端保存 - 新增取件信息(需要认证)
|
|
|
*/
|
|
|
@RequestMapping("/save")
|
|
|
public R save(@RequestBody QujianxinxiEntity qujianxinxi, HttpServletRequest request){
|
|
|
// 生成唯一ID:当前时间戳 + 随机数(0-999)
|
|
|
qujianxinxi.setId(new Date().getTime() + new Double(Math.floor(Math.random() * 1000)).longValue());
|
|
|
// ValidatorUtils.validateEntity(qujianxinxi); // 可选的参数验证(已注释)
|
|
|
qujianxinxiService.insert(qujianxinxi);
|
|
|
return R.ok(); // 返回成功响应
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 前端保存 - 新增取件信息(无需认证)
|
|
|
*/
|
|
|
@RequestMapping("/add")
|
|
|
public R add(@RequestBody QujianxinxiEntity qujianxinxi, HttpServletRequest request){
|
|
|
// 生成唯一ID:当前时间戳 + 随机数(0-999)
|
|
|
qujianxinxi.setId(new Date().getTime() + new Double(Math.floor(Math.random() * 1000)).longValue());
|
|
|
// ValidatorUtils.validateEntity(qujianxinxi); // 可选的参数验证(已注释)
|
|
|
qujianxinxiService.insert(qujianxinxi);
|
|
|
return R.ok();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 修改 - 更新取件信息
|
|
|
*/
|
|
|
@RequestMapping("/update")
|
|
|
@Transactional // 添加事务管理,确保数据一致性
|
|
|
public R update(@RequestBody QujianxinxiEntity qujianxinxi, HttpServletRequest request){
|
|
|
// ValidatorUtils.validateEntity(qujianxinxi); // 可选的参数验证(已注释)
|
|
|
qujianxinxiService.updateById(qujianxinxi); // 根据ID全字段更新
|
|
|
return R.ok();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 删除 - 批量删除取件信息
|
|
|
*/
|
|
|
@RequestMapping("/delete")
|
|
|
public R delete(@RequestBody Long[] ids){
|
|
|
// 将数组转换为List,执行批量删除
|
|
|
qujianxinxiService.deleteBatchIds(Arrays.asList(ids));
|
|
|
return R.ok();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 提醒接口 - 根据条件统计取件信息数量,用于提醒功能
|
|
|
* @param columnName 统计的列名(如创建时间、取件时间等)
|
|
|
* @param type 统计类型
|
|
|
* @param map 包含统计条件的参数映射
|
|
|
* @param request HTTP请求对象
|
|
|
* @return 返回统计结果
|
|
|
*/
|
|
|
@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 c = Calendar.getInstance();
|
|
|
Date remindStartDate = null;
|
|
|
Date remindEndDate = null;
|
|
|
|
|
|
// 处理开始日期
|
|
|
if(map.get("remindstart") != null) {
|
|
|
Integer remindStart = Integer.parseInt(map.get("remindstart").toString());
|
|
|
c.setTime(new Date());
|
|
|
c.add(Calendar.DAY_OF_MONTH, remindStart); // 计算相对当前日期的开始日期
|
|
|
remindStartDate = c.getTime();
|
|
|
map.put("remindstart", sdf.format(remindStartDate)); // 格式化日期
|
|
|
}
|
|
|
// 处理结束日期
|
|
|
if(map.get("remindend") != null) {
|
|
|
Integer remindEnd = Integer.parseInt(map.get("remindend").toString());
|
|
|
c.setTime(new Date());
|
|
|
c.add(Calendar.DAY_OF_MONTH, remindEnd); // 计算相对当前日期的结束日期
|
|
|
remindEndDate = c.getTime();
|
|
|
map.put("remindend", sdf.format(remindEndDate)); // 格式化日期
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 构建查询条件包装器
|
|
|
Wrapper<QujianxinxiEntity> wrapper = new EntityWrapper<QujianxinxiEntity>();
|
|
|
// 添加开始条件(大于等于)
|
|
|
if(map.get("remindstart") != null) {
|
|
|
wrapper.ge(columnName, map.get("remindstart"));
|
|
|
}
|
|
|
// 添加结束条件(小于等于)
|
|
|
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", (String)request.getSession().getAttribute("username"));
|
|
|
}
|
|
|
if(tableName.equals("kuaidiyuan")) {
|
|
|
// 快递员只能查看自己负责的取件信息
|
|
|
wrapper.eq("yuangongzhanghao", (String)request.getSession().getAttribute("username"));
|
|
|
}
|
|
|
|
|
|
// 执行统计查询,返回符合条件的记录数量
|
|
|
int count = qujianxinxiService.selectCount(wrapper);
|
|
|
return R.ok().put("count", count); // 返回统计结果
|
|
|
}
|
|
|
} |