package com.ruoyi.business.controller; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.domain.AddTreeSelect; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.core.domain.TreeSelect; import com.ruoyi.common.core.domain.entity.SysDept; import com.ruoyi.common.core.domain.entity.SysUser; import com.ruoyi.common.core.page.TableDataInfo; import com.ruoyi.common.core.redis.RedisCache; import com.ruoyi.common.utils.spring.SpringUtils; import com.ruoyi.database.domain.ApprovalProcess; import com.ruoyi.database.domain.BaseAddressInfo; import com.ruoyi.database.domain.BusinessTripApproval; import com.ruoyi.database.domain.dto.ApprovaltBusinessDto; import com.ruoyi.database.domain.dto.ApprovaltDto; import com.ruoyi.database.domain.gwglLog; import com.ruoyi.database.service.ApprovalProcessService; import com.ruoyi.database.service.BaseAddressInfoService; import com.ruoyi.database.service.BusinessTripApprovalService; import com.ruoyi.database.service.gwglLogService; import com.ruoyi.system.service.ISysDeptService; import com.ruoyi.system.service.ISysUserService; import com.sun.org.apache.xpath.internal.operations.Bool; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.*; import java.text.SimpleDateFormat; import java.util.*; import java.util.stream.Collectors; /** * @Description BusinessTripApprovalController * @Author lijingtong * @Date 2025-05-29 */ @RestController @RequestMapping("/BusinessTripApproval") @RequiredArgsConstructor @Api(tags = "出差审批单") public class BusinessTripApprovalController extends BaseController { private final BusinessTripApprovalService businessTripApprovalService; private final ISysUserService sysUserService; private final ISysDeptService sysDeptService; private final BaseAddressInfoService baseAddressInfoService; private final ApprovalProcessService approvalsProcessService; private final gwglLogService gwglLogService; @PostMapping("/list") @ApiOperation("查询出差申请") public TableDataInfo list(BusinessTripApproval businessTripApproval) { startPage(); QueryWrapper queryWrapper = new QueryWrapper<>(businessTripApproval); queryWrapper.orderByDesc("create_time"); List list = businessTripApprovalService.list(queryWrapper); long size = businessTripApprovalService.count(queryWrapper); return getDataTableEnhance(list, size); } @PostMapping("/add") @ApiOperation("新增出差申请") public AjaxResult add(@RequestBody BusinessTripApproval dto) { SysUser user = getLoginUser().getUser(); if (dto.getHasOffcialCar() == null) { throw new RuntimeException("请选择是否自带公务车辆"); } if (dto.getHasOffcialCar() != null && dto.getHasOffcialCar() == 1) { if (dto.getCarModel() == null) { throw new RuntimeException("请选择使用车型"); } if (dto.getUseCarType() == null) { throw new RuntimeException("请选择用车方式"); } if (dto.getSeats() == null) { throw new RuntimeException("请输入座位数"); } } if (dto.getStartDate() == null || dto.getEndDate() == null) { throw new RuntimeException("请选择出差时间范围"); } if (dto.getDestinationId() == null) { throw new RuntimeException("请选择出差目的地"); } dto.setUserName(user.getNickName()); dto.setUserId(user.getUserId()); dto.setDepartment(user.getDept().getDeptName()); List togUserIdList = dto.getTogUserIdList(); int isLeader = 0; if (user.getIsLeader() != null) { isLeader = 1; } StringBuilder ids = new StringBuilder(); if (togUserIdList != null && !togUserIdList.isEmpty()) { for (Long aLong : togUserIdList) { if (ids.length() > 0) { ids.append(","); } ids.append(aLong); SysUser user1 = sysUserService.selectUserById(aLong); if (user1.getIsLeader() != null) { isLeader = 1; } } dto.setTogUserIds(ids.toString()); } String s = OrderNumberGenerator(); dto.setApplyNo(s); UUID uuid = UUID.randomUUID(); String uuids = uuid.toString(); dto.setUuid(uuids); BaseAddressInfo byId = baseAddressInfoService.getById(dto.getDestinationId()); ApprovalProcess approvalProcess = new ApprovalProcess(); approvalProcess.setApprovalNo(s); approvalProcess.setProcessTitle("出差呈请"); approvalProcess.setSubmitterName(user.getNickName()); approvalProcess.setSubmitterId(user.getUserId()); approvalProcess.setSubmitTime(new Date().getTime()); approvalProcess.setMatterType(2); approvalProcess.setReadStatus(0); approvalProcess.setBusinessTripDestination(byId.getAddName()); approvalProcess.setStartTime(dto.getStartDate()); approvalProcess.setEndTime(dto.getEndDate()); approvalProcess.setCreateTime(new Date().getTime()); approvalProcess.setUuid(uuids); List longs = new ArrayList<>(); if (isLeader == 1) { if (dto.getHasOffcialCar() == 1) { approvalProcess.setApprovalStatus(100); longs.add(2165L); } else { approvalProcess.setApprovalStatus(400); longs.add(1660L); longs.add(1661L); } } else { approvalProcess.setApprovalStatus(1); longs.add(user.getDept().getLeaderId()); } addLog(approvalProcess, longs); return toAjax(businessTripApprovalService.save(dto) && approvalsProcessService.save(approvalProcess) ); } @GetMapping("/userList") @ApiOperation("查询用户人员列表") public AjaxResult applyList() { List sysUsers = sysUserService.selectList(); List sysDepts = sysDeptService.selectDeptList(new SysDept()); sysDepts = sysDepts.stream() .filter(dept -> dept.getDeptId() != 131000) // 过滤掉id为131000的部门 .collect(Collectors.toList()); List depts = sysDeptService.buildDeptTree(sysDepts); Map> deptUsersMap = new HashMap<>(); for (SysUser user : sysUsers) { deptUsersMap.computeIfAbsent(user.getDeptId(), k -> new ArrayList<>()).add(user); } return AjaxResult.success(buildTree(depts, deptUsersMap)); } public List buildTree(List depts, Map> deptUsersMap) { List treeList = new ArrayList<>(); for (SysDept dept : depts) { TreeSelect treeSelect = new TreeSelect(); treeSelect.setId(dept.getDeptId()); treeSelect.setLabel(dept.getDeptName()); // 分配用户到部门 List users = deptUsersMap.getOrDefault(dept.getDeptId(), Collections.emptyList()); // 这里可以根据需要处理用户信息,比如只存储用户名或创建包含用户详细信息的子对象 // 这里简单示例,只存储用户名列表 List children = new ArrayList<>(); if (users != null) { for (SysUser sysUser : users) { TreeSelect treeSelect1 = new TreeSelect(); treeSelect1.setLabel(sysUser.getNickName()); treeSelect1.setId(sysUser.getUserId()); treeSelect1.setDictId(dept.getDeptId()); treeSelect1.setDictName(dept.getDeptName()); children.add(treeSelect1); } } // 递归处理子部门 children.addAll(buildTree(dept.getChildren(), deptUsersMap)); if (!children.isEmpty()) { treeSelect.setChildren(children); // 注意:这里我们重置了children,因为前面我们错误地将它设置为了用户名字符串列表 } treeList.add(treeSelect); } return treeList; } @GetMapping("/addressList") @ApiOperation("查询地址信息列表") public AjaxResult addressList() { List allAddresses = baseAddressInfoService.list(); Map> addressMap = allAddresses.stream() .collect(Collectors.groupingBy(BaseAddressInfo::getTopId)); List tree = buildTree(addressMap, 0L); return AjaxResult.success(tree); } private List buildTree( Map> addressMap, Long parentId ) { List children = addressMap.get(parentId); if (children == null) return Collections.emptyList(); return children.stream() .map(info -> { AddTreeSelect node = new AddTreeSelect(); node.setId(info.getId()); node.setLabel(info.getAddName()); node.setValue(info.getId().toString()); node.setChildren(buildTree(addressMap, info.getId())); return node; }) .collect(Collectors.toList()); } private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); private static final String DATA_KEY = "DDATAKEY:"; public static String OrderNumberGenerator() { String format = sdf.format(new Date()); RedisCache bean = SpringUtils.getBean(RedisCache.class); bean.setCacheObject(DATA_KEY + format, bean.getCacheObject(DATA_KEY + format) == null ? 1 : (Integer) (bean.getCacheObject(DATA_KEY + format)) + 1); return format + String.format("%04d", (Integer) (bean.getCacheObject(DATA_KEY + format))); } @ApiOperation("审批出差申请") @PostMapping("/approval") public AjaxResult approval(ApprovaltBusinessDto dto) { SysUser user = getLoginUser().getUser(); String uuid = dto.getUuid(); if (uuid == null) { throw new RuntimeException("uuid不可为空"); } if (dto.getState() == null) { throw new RuntimeException("请选择通过或者驳回"); } gwglLog one = gwglLogService.lambdaQuery() .eq(gwglLog::getUuid, uuid) .orderByDesc(gwglLog::getCreateTime) .last("LIMIT 1") .one(); Integer checkState = one.getCheckState(); if (checkState != 0) { throw new RuntimeException("记录已被处理"); } String checkUserId = one.getCheckUserId(); List longs = new ArrayList<>(); if (checkUserId != null) { if (checkUserId.contains(",")) { String[] split = checkUserId.split(","); for (String s : split) { longs.add(Long.parseLong(s)); } } else { longs.add(Long.parseLong(checkUserId)); } } if (longs.isEmpty() || !longs.contains(user.getUserId())) { throw new RuntimeException("当前用户无法操作"); } ApprovalProcess approvalProcess = approvalsProcessService.lambdaQuery() .eq(ApprovalProcess::getUuid, uuid) .last("LIMIT 1") .one(); BusinessTripApproval businessTripApproval = businessTripApprovalService.lambdaQuery() .eq(BusinessTripApproval::getUuid,uuid) .last("LIMIT 1") .one(); Integer approvalStatus = approvalProcess.getApprovalStatus(); if (dto.getState() == 1) { if (approvalStatus == 1) { List userIds = new ArrayList<>(); if (businessTripApproval.getHasOffcialCar() == 1) { approvalProcess.setApprovalStatus(100); userIds.add(2165L); } else { approvalProcess.setApprovalStatus(400); userIds.add(1660L); userIds.add(1661L); } addLog(approvalProcess, userIds); } if (approvalStatus == 100) { if (dto.getDriverName() == null) { throw new RuntimeException("驾驶员姓名不可为空"); } if (dto.getPlateNo() == null) { throw new RuntimeException("车牌号不可为空"); } approvalProcess.setApprovalStatus(200); List userIds = new ArrayList<>(); userIds.add(2157L); addLog(approvalProcess, userIds); } if (approvalStatus == 200) { if (dto.getLeaderId() == null) { throw new RuntimeException("请选择分管局领导"); } approvalProcess.setApprovalStatus(300); List userIds = new ArrayList<>(); userIds.add(dto.getLeaderId()); addLog(approvalProcess, userIds); } if (approvalStatus == 300) { approvalProcess.setApprovalStatus(400); List userIds = new ArrayList<>(); userIds.add(1660L); userIds.add(1661L); addLog(approvalProcess, userIds); } if (approvalStatus == 400) { approvalProcess.setApprovalStatus(500); } one.setCheckState(dto.getState()); one.setCheckTime(System.currentTimeMillis() / 1000L); one.setReason(dto.getReason()); boolean updateById = gwglLogService.updateById(one); boolean updateById1 = approvalsProcessService.updateById(approvalProcess); return toAjax(updateById1 && updateById); } else if (dto.getState() == 2) { if (dto.getReason() == null) { throw new RuntimeException("驳回意见不可为空"); } approvalProcess.setApprovalStatus(0); one.setCheckState(dto.getState()); one.setCheckTime(System.currentTimeMillis() / 1000L); one.setReason(dto.getReason()); List userIds = new ArrayList<>(); userIds.add(approvalProcess.getSubmitterId()); addLog(approvalProcess, userIds); boolean updateById = gwglLogService.updateById(one); boolean updateById1 = approvalsProcessService.updateById(approvalProcess); return toAjax(updateById1 && updateById); } else { throw new RuntimeException("处理结果参数不正确"); } } public Boolean addLog(ApprovalProcess approvalProcess, List userIds) { gwglLog gwglLog = new gwglLog(); gwglLog.setUserId(approvalProcess.getSubmitterId()); gwglLog.setUserName(approvalProcess.getSubmitterName()); gwglLog.setCheckState(0); gwglLog.setUuid(approvalProcess.getUuid()); gwglLog.setApprovalStatus(approvalProcess.getApprovalStatus()); gwglLog.setCreateTime(System.currentTimeMillis() / 1000L); SysUser user = new SysUser(); StringBuilder name = new StringBuilder(); StringBuilder ids = new StringBuilder(); for (Long userId : userIds) { SysUser user1 = sysUserService.selectUserById(userId); if (name.length() > 0) { name.append(","); } name.append(user1.getNickName()); if (ids.length() > 0) { ids.append(","); } ids.append(user1.getUserId()); } gwglLog.setCheckUserId(ids.toString()); gwglLog.setChecker(name.toString()); if (user.getDept() != null) { gwglLog.setCheckDeptName(user.getDept().getDeptName()); } gwglLog.setCheckDeptId(user.getDeptId()); return gwglLogService.save(gwglLog); } }