|
|
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.PeisongxinxiEntity;
|
|
|
import com.entity.view.PeisongxinxiView;
|
|
|
|
|
|
import com.service.PeisongxinxiService;
|
|
|
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("/peisongxinxi") // 定义基础请求路径
|
|
|
public class PeisongxinxiController {
|
|
|
@Autowired // 自动注入配送信息服务
|
|
|
private PeisongxinxiService peisongxinxiService;
|
|
|
|
|
|
/**
|
|
|
* 后端列表 - 分页查询配送信息(需要认证)
|
|
|
* @param params 请求参数,包含分页和查询条件
|
|
|
* @param peisongxinxi 配送信息实体对象,用于封装查询条件
|
|
|
* @param request HTTP请求对象
|
|
|
* @return 返回分页数据
|
|
|
*/
|
|
|
@RequestMapping("/page")
|
|
|
public R page(@RequestParam Map<String, Object> params, PeisongxinxiEntity peisongxinxi,
|
|
|
HttpServletRequest request){
|
|
|
// 从session中获取表名,用于权限控制
|
|
|
String tableName = request.getSession().getAttribute("tableName").toString();
|
|
|
|
|
|
// 如果是普通用户,只能查看自己的配送信息
|
|
|
if(tableName.equals("yonghu")) {
|
|
|
peisongxinxi.setYonghuzhanghao((String)request.getSession().getAttribute("username"));
|
|
|
}
|
|
|
// 如果是快递员,只能查看自己负责的配送信息
|
|
|
if(tableName.equals("kuaidiyuan")) {
|
|
|
peisongxinxi.setYuangongzhanghao((String)request.getSession().getAttribute("username"));
|
|
|
}
|
|
|
|
|
|
// 创建MyBatis Plus的实体包装器,用于构建查询条件
|
|
|
EntityWrapper<PeisongxinxiEntity> ew = new EntityWrapper<PeisongxinxiEntity>();
|
|
|
|
|
|
// 构建分页查询:模糊查询/等值查询 -> 范围查询 -> 排序 -> 执行分页查询
|
|
|
PageUtils page = peisongxinxiService.queryPage(params,
|
|
|
MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, peisongxinxi), params), params));
|
|
|
|
|
|
return R.ok().put("data", page); // 返回成功响应和分页数据
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 前端列表 - 分页查询配送信息(无需认证)
|
|
|
*/
|
|
|
@IgnoreAuth // 忽略认证,允许未登录访问
|
|
|
@RequestMapping("/list")
|
|
|
public R list(@RequestParam Map<String, Object> params, PeisongxinxiEntity peisongxinxi,
|
|
|
HttpServletRequest request){
|
|
|
EntityWrapper<PeisongxinxiEntity> ew = new EntityWrapper<PeisongxinxiEntity>();
|
|
|
|
|
|
PageUtils page = peisongxinxiService.queryPage(params,
|
|
|
MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, peisongxinxi), params), params));
|
|
|
return R.ok().put("data", page);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 列表查询 - 查询所有匹配条件的配送信息
|
|
|
*/
|
|
|
@RequestMapping("/lists")
|
|
|
public R list(PeisongxinxiEntity peisongxinxi){
|
|
|
EntityWrapper<PeisongxinxiEntity> ew = new EntityWrapper<PeisongxinxiEntity>();
|
|
|
// 将所有非空属性转换为查询条件
|
|
|
ew.allEq(MPUtil.allEQMapPre(peisongxinxi, "peisongxinxi"));
|
|
|
return R.ok().put("data", peisongxinxiService.selectListView(ew));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 条件查询 - 根据条件查询单个配送信息
|
|
|
*/
|
|
|
@RequestMapping("/query")
|
|
|
public R query(PeisongxinxiEntity peisongxinxi){
|
|
|
EntityWrapper<PeisongxinxiEntity> ew = new EntityWrapper<PeisongxinxiEntity>();
|
|
|
// 将所有非空属性转换为查询条件
|
|
|
ew.allEq(MPUtil.allEQMapPre(peisongxinxi, "peisongxinxi"));
|
|
|
PeisongxinxiView peisongxinxiView = peisongxinxiService.selectView(ew);
|
|
|
return R.ok("查询配送信息成功").put("data", peisongxinxiView);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 后端详情 - 根据ID查询配送信息详情(需要认证)
|
|
|
*/
|
|
|
@RequestMapping("/info/{id}")
|
|
|
public R info(@PathVariable("id") Long id){
|
|
|
PeisongxinxiEntity peisongxinxi = peisongxinxiService.selectById(id);
|
|
|
return R.ok().put("data", peisongxinxi);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 前端详情 - 根据ID查询配送信息详情(无需认证)
|
|
|
*/
|
|
|
@IgnoreAuth
|
|
|
@RequestMapping("/detail/{id}")
|
|
|
public R detail(@PathVariable("id") Long id){
|
|
|
PeisongxinxiEntity peisongxinxi = peisongxinxiService.selectById(id);
|
|
|
return R.ok().put("data", peisongxinxi);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 后端保存 - 新增配送信息(需要认证)
|
|
|
*/
|
|
|
@RequestMapping("/save")
|
|
|
public R save(@RequestBody PeisongxinxiEntity peisongxinxi, HttpServletRequest request){
|
|
|
// 生成唯一ID:当前时间戳 + 随机数
|
|
|
peisongxinxi.setId(new Date().getTime() + new Double(Math.floor(Math.random() * 1000)).longValue());
|
|
|
// ValidatorUtils.validateEntity(peisongxinxi); // 可选的参数验证(已注释)
|
|
|
peisongxinxiService.insert(peisongxinxi);
|
|
|
return R.ok(); // 返回成功响应
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 前端保存 - 新增配送信息(无需认证)
|
|
|
*/
|
|
|
@RequestMapping("/add")
|
|
|
public R add(@RequestBody PeisongxinxiEntity peisongxinxi, HttpServletRequest request){
|
|
|
// 生成唯一ID:当前时间戳 + 随机数
|
|
|
peisongxinxi.setId(new Date().getTime() + new Double(Math.floor(Math.random() * 1000)).longValue());
|
|
|
// ValidatorUtils.validateEntity(peisongxinxi); // 可选的参数验证(已注释)
|
|
|
peisongxinxiService.insert(peisongxinxi);
|
|
|
return R.ok();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 修改 - 更新配送信息
|
|
|
*/
|
|
|
@RequestMapping("/update")
|
|
|
@Transactional // 添加事务管理,确保数据一致性
|
|
|
public R update(@RequestBody PeisongxinxiEntity peisongxinxi, HttpServletRequest request){
|
|
|
// ValidatorUtils.validateEntity(peisongxinxi); // 可选的参数验证(已注释)
|
|
|
peisongxinxiService.updateById(peisongxinxi); // 根据ID全字段更新
|
|
|
return R.ok();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 删除 - 批量删除配送信息
|
|
|
*/
|
|
|
@RequestMapping("/delete")
|
|
|
public R delete(@RequestBody Long[] ids){
|
|
|
// 将数组转换为List,执行批量删除
|
|
|
peisongxinxiService.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);
|
|
|
|
|
|
// 如果是按日期类型统计
|
|
|
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<PeisongxinxiEntity> wrapper = new EntityWrapper<PeisongxinxiEntity>();
|
|
|
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 = peisongxinxiService.selectCount(wrapper);
|
|
|
return R.ok().put("count", count); // 返回统计结果
|
|
|
}
|
|
|
} |