package com.ruoyi.web.controller.api;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.basic.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.BaseModel;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.applet.dto.*;
import com.ruoyi.system.applet.query.*;
import com.ruoyi.system.applet.vo.*;
import com.ruoyi.system.dto.KnowledgeDTO;
import com.ruoyi.system.dto.TTaskAuditBatchDTO;
import com.ruoyi.system.dto.TTaskAuditDTO;
import com.ruoyi.system.dto.TTaskDTO;
import com.ruoyi.system.model.*;
import com.ruoyi.system.query.InsepectorListQuery;
import com.ruoyi.system.query.KnowledgeListQuery;
import com.ruoyi.system.query.TaskListQuery;
import com.ruoyi.system.service.*;
import com.ruoyi.system.utils.wx.body.resp.Code2SessionRespBody;
import com.ruoyi.system.utils.wx.body.resq.Code2SessionResqBody;
import com.ruoyi.system.utils.wx.model.WeixinProperties;
import com.ruoyi.system.utils.wx.pojo.AppletUserDecodeData;
import com.ruoyi.system.utils.wx.pojo.AppletUserEncrypteData;
import com.ruoyi.system.utils.wx.tools.WxAppletTools;
import com.ruoyi.system.utils.wx.tools.WxUtils;
import com.ruoyi.system.vo.system.*;
import com.ruoyi.web.controller.tool.EmailUtils;
import com.ruoyi.web.controller.tool.MsgUtils;
import com.sun.jna.platform.win32.LMAccess;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
/**
*
* 任务记录 前端控制器
*
*
* @author xiaochen
* @since 2025-05-28
*/
@Slf4j
@Api(tags = "个人中心")
@RestController
@RequestMapping("/t-user")
public class TUserController {
@Resource
private TTaskCleanService taskCleanerService;
@Resource
private TTaskDetailService taskDetailService;
@Resource
private TLocationTypeService locationTypeService;
@Resource
private TLocationService locationService;
@Resource
private TProjectDeptService projectDeptService;
@Resource
private TTaskDetailService tTaskDetailService;
@Resource
private TDeptService deptService;
@Resource
private ISysUserService sysUserService;
@Resource
private ISysRoleService roleService;
@Resource
private TDictDataService dictDataService;
@Resource
private TNoticeSetService noticeSetService;
@Resource
private TLeaveService leaveService;
@Resource
private TLeaveAuditService leaveAuditService;
@Resource
private TokenService tokenService;
@Resource
private TAppealService appealService;
@Resource
private TInspectorService inspectorService;
@Resource
private TFeedbackService feedbackService;
@Resource
private TProblemEscalationService problemEscalationService;
@Resource
private TNoticeService noticeService;
@Autowired
private RestTemplate wxRestTemplate;
@Autowired
private WeixinProperties weixinProperties;
@Resource
private RedisService redisService;
@Resource
private MsgUtils msgUtils;
@ApiOperation(value = "通过code获得openid,获取用户信息",tags = {"微信小程序登录"})
@GetMapping("/openIdByJsCode")
public R openIdByJsCode(@RequestParam String code) {
Long userId = tokenService.getLoginUser().getUserId();
SysUser sysUser = sysUserService.selectUserById(userId);
if(Objects.isNull(sysUser)){
return R.fail("未查询到当前登录用户信息");
}
if(StringUtils.hasLength(sysUser.getOpenId())){
return R.ok();
}
log.info("<<<<<<<<换取openid开始<<<<<<<<:{}", code);
WxAppletTools appletTools = new WxAppletTools(wxRestTemplate, weixinProperties, redisService);
Code2SessionRespBody body = appletTools.getOpenIdByJscode2session(new Code2SessionResqBody().build(code));
String openid = body.getOpenid();
sysUser.setOpenId(openid);
sysUserService.updateUser(sysUser);
return R.ok();
}
@ApiOperation(value = "消息通知列表")
@PostMapping(value = "/noticeList")
public R> noticeList(@RequestBody NoticeListQuery query) {
Long userId = tokenService.getLoginUserApplet().getUserId();
query.setUserId(userId + "");
PageInfo noticeListVOPageInfo = noticeService.noticeList(query);
List records = noticeListVOPageInfo.getRecords();
List noticeListVOS = new ArrayList<>(records);
for (NoticeListVO noticeListVO : noticeListVOS) {
noticeListVO.setStatus(2);
noticeService.updateById(noticeListVO);
}
return R.ok(noticeListVOPageInfo);
}
/**
* 生成数据
*
* @param id
* @return
*/
@ApiOperation(value = "测试接口不要调")
@GetMapping(value = "/testData")
public R testData(@RequestParam String id) {
// List tAppeals = new ArrayList<>();
// for (int i = 0; i < 25; i++) {
// TAppeal tAppeal = new TAppeal();
// tAppeal.setTaskId("1938536771253641217");
// tAppeal.setAppealPerson("152");
// tAppeal.setStatus(1);
// tAppeal.setAppealContent("申诉");
// tAppeal.setPictures("https://bjxj.oss-cn-guangzhou.aliyuncs.com/bjxj/88d2d5614fe6492bba5666a7ab01e76b.png");
// tAppeals.add(tAppeal);
// }
// appealService.saveBatch(tAppeals);
// List tAppeals1 = new ArrayList<>();
// for (int i = 0; i < 25; i++) {
// TAppeal tAppeal = new TAppeal();
// tAppeal.setTaskId("1938536771253641217");
// tAppeal.setAppealPerson("152");
// tAppeal.setStatus(2);
// tAppeal.setAppealContent("申诉");
// tAppeal.setPictures("https://bjxj.oss-cn-guangzhou.aliyuncs.com/bjxj/88d2d5614fe6492bba5666a7ab01e76b.png");
// tAppeal.setAuditPerson("162");
// tAppeal.setAuditRemark("备注");
// tAppeal.setAuditTime(LocalDateTime.now());
// tAppeals1.add(tAppeal);
// }
// appealService.saveBatch(tAppeals1);
// List tAppeals2 = new ArrayList<>();
// for (int i = 0; i < 25; i++) {
// TAppeal tAppeal = new TAppeal();
// tAppeal.setTaskId("1938536771253641217");
// tAppeal.setAppealPerson("152");
// tAppeal.setStatus(3);
// tAppeal.setAppealContent("申诉");
// tAppeal.setPictures("https://bjxj.oss-cn-guangzhou.aliyuncs.com/bjxj/88d2d5614fe6492bba5666a7ab01e76b.png");
// tAppeal.setAuditPerson("162");
// tAppeal.setAuditRemark("备注");
// tAppeal.setAuditTime(LocalDateTime.now());
// tAppeals2.add(tAppeal);
// }
// appealService.saveBatch(tAppeals2);
// List tAppeals3 = new ArrayList<>();
// for (int i = 0; i < 25; i++) {
// TAppeal tAppeal = new TAppeal();
// tAppeal.setTaskId("1938536771253641217");
// tAppeal.setAppealPerson("152");
// tAppeal.setStatus(4);
// tAppeal.setAppealContent("申诉");
// tAppeal.setPictures("https://bjxj.oss-cn-guangzhou.aliyuncs.com/bjxj/88d2d5614fe6492bba5666a7ab01e76b.png");
//
// tAppeals3.add(tAppeal);
//
// }
// appealService.saveBatch(tAppeals3);
return R.ok();
}
@ApiOperation(value = "个人中心-首页")
@PostMapping(value = "/userIndex")
public R userIndex() {
UserIndexVO userIndexVO = new UserIndexVO();
List userAll = sysUserService.selectAllList();
List users = userAll.stream().filter(e -> e.getUserId() != 1).collect(Collectors.toList());
Long userId = tokenService.getLoginUserApplet().getUserId();
SysUser sysUser = sysUserService.selectUserById(userId);
userIndexVO.setAvatar(sysUser.getAvatar());
userIndexVO.setNickName(sysUser.getNickName());
userIndexVO.setPhonenumber(sysUser.getPhonenumber());
if (sysUser.getDeptType() == 1) {
TProjectDept tProjectDept = projectDeptService.getById(sysUser.getDeptId());
if (!tProjectDept.getParentId().equals("0")) {
TProjectDept tProjectDept1 = projectDeptService.getById(tProjectDept.getParentId());
userIndexVO.setDeptName(tProjectDept1.getProjectName() + ">" + tProjectDept.getProjectName());
} else {
userIndexVO.setDeptName(tProjectDept.getProjectName());
}
} else {
TDept tDept = deptService.getById(sysUser.getDeptId());
if (tDept != null) {
userIndexVO.setDeptName(tDept.getDeptName());
}
}
// 查询请假状态
List list = leaveService.lambdaQuery().eq(TLeave::getLeavePerson, userId)
.eq(TLeave::getAuditStatus, 2)
.list();
userIndexVO.setIsLeave(0);
for (TLeave tLeave : list) {
LocalDate now = LocalDate.now();
LocalDate startDate = tLeave.getStartTime().toLocalDate();
LocalDate endDate = tLeave.getStartTime().toLocalDate();
if (now.isAfter(startDate) && now.isBefore(endDate)) {
userIndexVO.setIsLeave(1);
} else if (now.isEqual(startDate) || now.isEqual(endDate)) {
userIndexVO.setIsLeave(1);
}
}
// 获取本月第一天凌晨00:00:00 和本月最后一天23:59:59 类型为LocalDateTime
// 获取当前日期
LocalDate now = LocalDate.now();
// 本月第一天
LocalDate firstDayOfMonth = now.withDayOfMonth(1);
// 本月最后一天
LocalDate lastDayOfMonth = now.withDayOfMonth(now.lengthOfMonth());
// 转换为 LocalDateTime:第一天的 00:00:00
LocalDateTime startOfMonth = LocalDateTime.of(firstDayOfMonth, LocalTime.MIN);
// 转换为 LocalDateTime:最后一天的 23:59:59
LocalDateTime endOfMonth = LocalDateTime.of(lastDayOfMonth, LocalTime.MAX);
List finishTask = taskCleanerService.lambdaQuery().eq(TTask::getPatrolInspector, userId)
.ge(TTask::getImplementTime, startOfMonth)
.le(TTask::getImplementTime, endOfMonth).list();
List taskIds = finishTask.stream().map(TTask::getId).collect(Collectors.toList());
if (taskIds.isEmpty()){
taskIds.add("-1");
}
List taskDetails = taskDetailService.lambdaQuery()
.in(TTaskDetail::getTaskId, taskIds)
.eq(TTaskDetail::getAuditStatus, 1)
.eq(TTaskDetail::getHandleType,1)
.isNotNull(TTaskDetail::getClearStatus)
.orderByDesc(TTaskDetail::getCreateTime).list();
// 使用 stream 去重,保留每个 taskId 最新的一条
taskDetails = new ArrayList<>(taskDetails.stream()
.collect(Collectors.groupingBy(
TTaskDetail::getTaskId,
Collectors.collectingAndThen(
Collectors.toList(),
listAll -> listAll.get(0)
)
))
.values());
int size = (int) finishTask.stream().filter(e -> e.getStatus() == 5 || e.getStatus() == 6).count();
List taskIdsStatus = finishTask.stream().map(TTask::getId).collect(Collectors.toList());
if (taskIdsStatus.isEmpty()){
taskIdsStatus.add("-1");
}
userIndexVO.setFinishCount(size);
List status1 = taskDetails.stream().filter(e -> e.getClearStatus() == 1&&taskIdsStatus.contains(e.getTaskId())).collect(Collectors.toList());
List status2 = taskDetails.stream().filter(e -> e.getClearStatus() == 2&&taskIdsStatus.contains(e.getTaskId())).collect(Collectors.toList());
if (!status1.isEmpty()) {
BigDecimal divide = new BigDecimal(status1.size())
.divide(new BigDecimal(status1.size()+status2.size()), 2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(100));
userIndexVO.setRate(divide);
} else {
userIndexVO.setRate(new BigDecimal(0));
}
// 判断时间
String startTime = "";
String endTime = "";
Map monthDate = DateUtils.getMonthDate(new Date());
startTime = new SimpleDateFormat("yyyy-MM-dd").format((monthDate.get("first"))) + " 00:00:00";
endTime = new SimpleDateFormat("yyyy-MM-dd").format((monthDate.get("last"))) + " 23:59:59";
List taskRank = taskCleanerService.lambdaQuery().between(TTask::getImplementTime, startTime, endTime).list();
List taskIdsRank = taskRank.stream().map(TTask::getId).collect(Collectors.toList());
if (taskIdsRank.isEmpty()){
userIndexVO.setRanking(0);
}else{
for (SysUser user : users) {
int finishCount = (int) taskRank.stream().filter(e->e.getPatrolInspector().equals(user.getUserId()+"")&&e.getStatus() == 5 || e.getStatus() == 6).count();
int totalCount = (int) taskRank.stream().filter(e->e.getPatrolInspector().equals(user.getUserId()+"")).count();
if (totalCount!=0){
user.setFinish(new BigDecimal(finishCount).divide(new BigDecimal(totalCount),2, RoundingMode.HALF_DOWN));
}else{
user.setFinish(BigDecimal.ZERO);
}
List status1User = taskDetails.stream().filter(e -> e.getClearStatus() == 1).collect(Collectors.toList());
List status2User = taskDetails.stream().filter(e -> e.getClearStatus() == 2).collect(Collectors.toList());
if (!status1User.isEmpty()) {
BigDecimal divide = new BigDecimal(status1User.size() + status2User.size())
.divide(new BigDecimal(status1User.size()), 2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(100));
user.setRate(divide);
} else {
user.setRate(new BigDecimal(0));
}
}
// 将users进行排序,优先根据rate倒叙排序,如果rate相同 再finish倒叙排序
users.sort((o1, o2) -> {
if (o1.getRate().compareTo(o2.getRate()) == 0) {
return o2.getFinish().compareTo(o1.getFinish());
} else {
return o2.getRate().compareTo(o1.getRate());
}
});
// 查询当前登陆人处于哪一个排名
for (int i = 0; i < users.size(); i++) {
if (users.get(i).getUserId().equals(userId)) {
userIndexVO.setRanking(i + 1);
break;
}
}
}
return R.ok(userIndexVO);
}
@ApiOperation(value = "任务记录分页列表")
@PostMapping(value = "/taskList")
public R> pageList(@RequestBody TaskUserListQuery query) {
Long userId = tokenService.getLoginUserApplet().getUserId();
query.setUserId(userId + "");
return R.ok(taskCleanerService.pageListUser(query));
}
@ApiOperation(value = "详情任务")
@GetMapping(value = "/detail")
public R detail(@RequestParam String id) {
List list = taskDetailService.lambdaQuery().eq(TTaskDetail::getTaskId, id)
.orderByDesc(BaseModel::getCreateTime).list();
TaskDetailVO taskDetailVO = new TaskDetailVO();
TTask byId = taskCleanerService.getById(id);
BeanUtils.copyProperties(byId, taskDetailVO);
TLocation byId1 = locationService.getById(byId.getLocationId());
TLocationType byId2 = locationTypeService.getById(byId1.getLocationType());
taskDetailVO.setLocationName(byId1.getLocationName());
taskDetailVO.setLocationAddress(byId1.getLocationAddress());
taskDetailVO.setLocationAddressEnd(byId1.getLocationAddressEnd());
taskDetailVO.setLocationIcon(byId2.getLocationIcon());
taskDetailVO.setLocationTypeName(byId2.getLocationName());
taskDetailVO.setLocationLon(byId1.getLocationLon());
taskDetailVO.setLocationLat(byId1.getLocationLat());
taskDetailVO.setLocationLatEnd(byId1.getLocationLatEnd());
taskDetailVO.setLocationLonEnd(byId1.getLocationLonEnd());
TTaskDetail tTaskDetail = list.stream().filter(e -> e.getHandleType() == 1).findFirst().orElse(null);
if (tTaskDetail != null && tTaskDetail.getUnqualified() != null) {
TDictData byId3 = dictDataService.getById(tTaskDetail.getUnqualified());
if (byId3 != null) {
tTaskDetail.setUnqualifiedName(byId3.getDataContent());
}
}
if (tTaskDetail != null) {
tTaskDetail.setFinishTime(tTaskDetail.getCreateTime());
}
taskDetailVO.setTaskDetail(tTaskDetail);
taskDetailVO.setRecords(list);
if (byId.getStatus()==4){
TTaskDetail reject = taskDetailService.lambdaQuery().eq(TTaskDetail::getTaskId, id)
.eq(TTaskDetail::getHandleType,3)
.orderByDesc(BaseModel::getCreateTime).last("limit 1").one();
if (reject!=null){
taskDetailVO.setRejectRemark(reject.getAuditRemark());
}
}
return R.ok(taskDetailVO);
}
@ApiOperation(value = "详情任务-操作记录-详情")
@GetMapping(value = "/detailRecord")
public R detailRecord(@RequestParam String id) {
TaskRecordDetailVO taskDetailVO = new TaskRecordDetailVO();
TTaskDetail byId4 = taskDetailService.getById(id);
TTask byId = taskCleanerService.getById(byId4.getTaskId());
BeanUtils.copyProperties(byId, taskDetailVO);
TLocation byId1 = locationService.getById(byId.getLocationId());
TLocationType byId2 = locationTypeService.getById(byId1.getLocationType());
taskDetailVO.setLocationAddress(byId1.getLocationAddress());
taskDetailVO.setLocationIcon(byId2.getLocationIcon());
taskDetailVO.setLocationName(byId2.getLocationName());
if (byId4.getUnqualified() != null) {
TDictData byId3 = dictDataService.getById(byId4.getUnqualified());
if (byId3 != null) {
byId4.setUnqualifiedName(byId3.getDataContent());
}
}
byId4.setFinishTime(byId4.getCreateTime());
SysUser sysUser = sysUserService.selectUserById(Long.valueOf(byId4.getAuditPerson()));
if (sysUser != null) {
byId4.setAuditPersonName(sysUser.getUserName());
}
taskDetailVO.setTaskDetail(byId4);
return R.ok(taskDetailVO);
}
@ApiOperation(value = "申诉记录分页列表")
@PostMapping(value = "/appealList")
public R> pageListUser(@RequestBody AppealListQuery query) {
query.setUserId(tokenService.getLoginUserApplet().getUserId() + "");
return R.ok(appealService.pageListUser(query));
}
@ApiOperation(value = "详情申诉记录")
@GetMapping(value = "/appealDetail")
public R appealDetail(@RequestParam String id) {
AppealDetailVO appealDetailVO = new AppealDetailVO();
TAppeal appeal = appealService.getById(id);
BeanUtils.copyProperties(appeal, appealDetailVO);
TTask byId = taskCleanerService.getById(appeal.getTaskId());
TLocation byId1 = locationService.getById(byId.getLocationId());
TLocationType byId2 = locationTypeService.getById(byId1.getLocationType());
appealDetailVO.setLocationAddress(byId1.getLocationAddress());
appealDetailVO.setLocationIcon(byId2.getLocationIcon());
appealDetailVO.setLocationName(byId1.getLocationName());
appealDetailVO.setTaskId(byId.getId());
appealDetailVO.setLocationTypeName(byId2.getLocationName());
if (StringUtils.hasLength(appeal.getAuditPerson())) {
SysUser sysUser = sysUserService.selectUserById(Long.valueOf(appeal.getAuditPerson()));
if (sysUser != null) {
appealDetailVO.setAuditPersonName(sysUser.getUserName());
}
}
appealDetailVO.setTaskName(byId.getTaskName());
appealDetailVO.setTaskCode(byId.getTaskCode());
return R.ok(appealDetailVO);
}
@Log(title = "取消申诉记录", businessType = BusinessType.OTHER)
@ApiOperation(value = "取消申诉记录")
@GetMapping(value = "/cancelAppeal")
public R cancelAppeal(@RequestParam String id) {
TAppeal byId = appealService.getById(id);
byId.setStatus(4);
appealService.updateById(byId);
return R.ok();
}
@ApiOperation(value = "获取距离最近的任务列表")
@PostMapping(value = "/listAllTask")
public R> listAllTask(@RequestBody AppealListDTO appealListDTO) {
appealListDTO.setUserId(tokenService.getLoginUserApplet().getUserId() + "");
SysUser sysUser = sysUserService.selectUserById(tokenService.getLoginUserApplet().getUserId());
// if (sysUser.getDeptType()==2){
// // 公司部门查询所有任务
// appealListDTO.setUserId(null);
// }
List res = appealService.listAllTask(appealListDTO);
return R.ok(res);
}
@ApiOperation(value = "不合格原因列表")
@PostMapping(value = "/unqualifiedList")
public R> unqualifiedList() {
List list = dictDataService.list(
new LambdaQueryWrapper()
.eq(TDictData::getDataType, 2)
);
return R.ok(list);
}
@ApiOperation(value = "上传督察任务")
@PostMapping(value = "/addInspector")
public R addInspector(@RequestBody InspectorAddDTO dto) throws Exception {
TTask task = taskCleanerService.getById(dto.getTaskId());
TLocation location = locationService.getById(task.getLocationId());
SysUser sysUser = sysUserService.selectUserById(Long.valueOf(task.getPatrolInspector()));
dto.setCommitPerson(tokenService.getLoginUserApplet().getUserId() + "");
if (dto.getClearStatus() == 1) {
dto.setStatus(4);
} else {
dto.setStatus(1);
}
inspectorService.save(dto);
return R.ok();
}
@ApiOperation(value = "上传意见反馈")
@PostMapping(value = "/addFeedback")
public R addFeedback(@RequestBody FeedbackDTO dto) {
dto.setFeedbackPerson(tokenService.getLoginUserApplet().getUserId() + "");
feedbackService.save(dto);
return R.ok();
}
@ApiOperation(value = "上传问题上报")
@PostMapping(value = "/addProblem")
public R addProblem(@RequestBody ProblemDTO dto) {
dto.setEscalationId(tokenService.getLoginUserApplet().getUserId() + "");
problemEscalationService.save(dto);
return R.ok();
}
@ApiOperation(value = "问题类型列表")
@PostMapping(value = "/problemList")
public R> problemList() {
List list = dictDataService.list(
new LambdaQueryWrapper()
.eq(TDictData::getDataType, 3)
);
return R.ok(list);
}
@Resource
private TKnowledgeService knowledgeService;
@ApiOperation(value = "环卫知识分页列表")
@PostMapping(value = "/pageList")
public R> pageList(@RequestBody KnowledgeListUserQuery query) {
return R.ok(knowledgeService.pageListUser(query));
}
@ApiOperation(value = "详情环卫知识")
@GetMapping(value = "/detailKnowledge")
public R detailKnowledge(@RequestParam String id) {
TKnowledge byId = knowledgeService.getById(id);
int i = byId.getViews() + 1;
byId.setViews(i);
knowledgeService.updateById(byId);
return R.ok(byId);
}
@ApiOperation(value = "发起请假-获取审批流程")
@GetMapping(value = "/leaveAuditList")
public R leaveAuditList() {
LeaveAuditListVO res = new LeaveAuditListVO();
Long userId = tokenService.getLoginUserApplet().getUserId();
SysUser user = sysUserService.selectUserById(userId);
Long userRole = sysUserService.getUserRole(userId);
SysRole sysRole = roleService.selectRoleById(userRole);
List sysUsers = sysUserService.selectAllList();
SysUser auditPerson = sysUserService.selectUserById(999L);
List paojectList = projectDeptService.lambdaQuery().eq(TProjectDept::getParentId, "0").list();
//项目部负责人、部门负责人、公司成员提交由最终审批人审批;
if (user.getDeptType() == 2 || sysRole.getRoleName().contains("项目部负责人") || sysRole.getRoleName().contains("部门负责人")) {
LeaveAuditVO tLeaveAudit = new LeaveAuditVO();
tLeaveAudit.setAvatar(auditPerson.getAvatar());
tLeaveAudit.setNickName(auditPerson.getNickName());
res.setAuditPerson(tLeaveAudit);
res.setAuditPersonId("999");
} else if (!sysRole.getRoleName().equals("部门助理")) {
// 审批人根据当前角色类型来
if (sysRole.getRoleName().contains("片区负责人") || sysRole.getRoleName().contains("组长")|| sysRole.getRoleName().contains("队长")
||sysRole.getRoleName().contains("项目部助理")) {
TProjectDept userDept = projectDeptService.getById(user.getDeptId());
List collect = sysUsers.stream().filter(e -> e.getDeptType() == 1).collect(Collectors.toList());
for (SysUser sysUser : collect) {
Long userRole1 = sysUserService.getUserRole(sysUser.getUserId());
SysRole sysRole1 = roleService.selectRoleById(userRole1);
// if (sysRole1 != null && sysRole1.getRoleName().equals("项目部负责人")) {
// if (userDept.getId().equals(sysUser.getDeptId())){
// LeaveAuditVO tLeaveAudit = new LeaveAuditVO();
// tLeaveAudit.setAvatar(sysUser.getAvatar());
// tLeaveAudit.setNickName(sysUser.getNickName());
// res.setAuditPerson(tLeaveAudit);
// res.setAuditPersonId(sysUser.getUserId() + "");
// break;
// }
// }
if (sysRole1 != null && sysRole1.getRoleName().equals("项目部负责人")) {
if (!sysRole.getRoleName().contains("项目部助理")){
if (userDept.getParentId().equals(sysUser.getDeptId())){
LeaveAuditVO tLeaveAudit = new LeaveAuditVO();
tLeaveAudit.setAvatar(sysUser.getAvatar());
tLeaveAudit.setNickName(sysUser.getNickName());
res.setAuditPerson(tLeaveAudit);
res.setAuditPersonId(sysUser.getUserId() + "");
break;
}
}else{
if (userDept.getId().equals(sysUser.getDeptId())){
LeaveAuditVO tLeaveAudit = new LeaveAuditVO();
tLeaveAudit.setAvatar(sysUser.getAvatar());
tLeaveAudit.setNickName(sysUser.getNickName());
res.setAuditPerson(tLeaveAudit);
res.setAuditPersonId(sysUser.getUserId() + "");
break;
}
}
}
}
}
} else {
List collect = sysUsers.stream().filter(e -> e.getDeptType() == 1 &&
e.getDeptId().equals(user.getDeptId())).collect(Collectors.toList());
for (SysUser sysUser : collect) {
Long userRole1 = sysUserService.getUserRole(sysUser.getUserId());
SysRole sysRole1 = roleService.selectRoleById(userRole1);
if (sysRole1 != null && sysRole1.getRoleName().contains("部门负责人")) {
LeaveAuditVO tLeaveAudit = new LeaveAuditVO();
tLeaveAudit.setAvatar(sysUser.getAvatar());
tLeaveAudit.setNickName(sysUser.getNickName());
res.setAuditPerson(tLeaveAudit);
res.setAuditPersonId(sysUser.getUserId() + "");
break;
}
}
}
List leaveAuditVOS = new ArrayList<>();
// 当前登陆人-抄送人
LeaveAuditVO leaveAuditVO1 = new LeaveAuditVO();
leaveAuditVO1.setAvatar(user.getAvatar());
leaveAuditVO1.setNickName(user.getNickName());
// 最终审核人-抄送人
LeaveAuditVO leaveAuditVO = new LeaveAuditVO();
leaveAuditVO.setAvatar(auditPerson.getAvatar());
leaveAuditVO.setNickName(auditPerson.getNickName());
leaveAuditVOS.add(leaveAuditVO);
leaveAuditVOS.add(leaveAuditVO1);
res.setCopyPerson(leaveAuditVOS);
return R.ok(res);
}
public static void main(String[] args) {
String temp = "2025-06-30 00:00:00";
// 轉化爲localdateTime
LocalDateTime parse = LocalDateTime.parse(temp, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
System.out.println(parse);
}
@ApiOperation(value = "发起请假")
@PostMapping(value = "/addLeave")
public R addLeave(@RequestBody LeaveDTO leaveDTO) throws Exception {
// String startTime1 = leaveDTO.getStartTime1();
// // 轉化爲LocalDateTime類型
// leaveDTO.setStartTime(LocalDateTime.parse(startTime1, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
// String endTime1 = leaveDTO.getEndTime1();
// // 轉化爲LocalDateTime類型
// leaveDTO.setStartTime(LocalDateTime.parse(endTime1, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
Long userId = tokenService.getLoginUserApplet().getUserId();
SysUser user = sysUserService.selectUserById(userId);
Long userRole = sysUserService.getUserRole(userId);
SysRole sysRole = roleService.selectRoleById(userRole);
List sysUsers = sysUserService.selectAllList();
SysUser auditPerson = sysUserService.selectUserById(999L);
leaveDTO.setAuditStatus(1);
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
// 生成随机数字6位
String format = simpleDateFormat.format(date);
leaveDTO.setCode(format + RandomUtil.randomNumbers(6));
leaveDTO.setTemplateId(user.getTemplateId());
leaveDTO.setLeavePerson(userId + "");
leaveService.save(leaveDTO);
TNoticeSet noticeSet = noticeSetService.lambdaQuery().eq(TNoticeSet::getType, 2).last("limit 1")
.one();
// 邮箱
TDictData email = dictDataService.lambdaQuery().eq(TDictData::getDataType,4).one();
// 授权码
TDictData code = dictDataService.lambdaQuery().eq(TDictData::getDataType,5).one();
//项目部负责人、部门负责人、公司成员提交由最终审批人审批;
if (user.getDeptType() == 2 || sysRole.getRoleName().contains("项目部负责人") || sysRole.getRoleName().contains("部门负责人")) {
TLeaveAudit tLeaveAudit1 = new TLeaveAudit();
tLeaveAudit1.setLeaveId(leaveDTO.getId());
tLeaveAudit1.setAuditStatus(1);
tLeaveAudit1.setAuditId(auditPerson.getUserId() + "");
tLeaveAudit1.setAuditType(1);
leaveAuditService.save(tLeaveAudit1);
TLeaveAudit tLeaveAudit2 = new TLeaveAudit();
tLeaveAudit2.setLeaveId(leaveDTO.getId());
tLeaveAudit2.setAuditStatus(2);
tLeaveAudit2.setAuditId(user.getUserId() + "," + auditPerson.getUserId());
tLeaveAudit2.setAuditType(2);
tLeaveAudit2.setAuditTime(LocalDateTime.now());
leaveAuditService.save(tLeaveAudit2);
} else if (!sysRole.getRoleName().equals("部门助理")) {
// 审批人根据当前角色类型来
if (sysRole.getRoleName().contains("片区负责人") || sysRole.getRoleName().contains("组长")|| sysRole.getRoleName().contains("队长")
||sysRole.getRoleName().contains("项目部助理")) {
TProjectDept userDept = projectDeptService.getById(user.getDeptId());
List collect = sysUsers.stream().filter(e -> e.getDeptType() == 1).collect(Collectors.toList());
for (SysUser sysUser : collect) {
Long userRole1 = sysUserService.getUserRole(sysUser.getUserId());
SysRole sysRole1 = roleService.selectRoleById(userRole1);
if (sysRole1 != null && sysRole1.getRoleName().equals("项目部负责人")) {
if (!sysRole.getRoleName().contains("项目部助理")){
if (userDept.getParentId().equals(sysUser.getDeptId())){
LeaveAuditVO tLeaveAudit = new LeaveAuditVO();
tLeaveAudit.setAvatar(sysUser.getAvatar());
tLeaveAudit.setNickName(sysUser.getNickName());
TLeaveAudit tLeaveAudit1 = new TLeaveAudit();
tLeaveAudit1.setLeaveId(leaveDTO.getId());
tLeaveAudit1.setAuditStatus(1);
tLeaveAudit1.setAuditId(sysUser.getUserId() + "");
tLeaveAudit1.setAuditType(1);
leaveAuditService.save(tLeaveAudit1);
// 新增消息
TNotice tNotice = new TNotice();
tNotice.setUserId(sysUser.getUserId()+"");
tNotice.setNoticeContent(user.getNickName() + "提交了请假申请,请尽快审核");
tNotice.setStatus(1);
tNotice.setDataId(leaveDTO.getId());
tNotice.setNoticeType(6);
switch (noticeSet.getNoticeType()) {
case 1:
msgUtils.sendMsg4(sysUser.getPhonenumber());
break;
case 2:
if (StringUtils.hasLength(email.getDataContent()) && StringUtils.hasLength(code.getDataContent())) {
EmailUtils.sendEmail(sysUser.getEmail(), email.getDataContent(), code.getDataContent(), "你有一条请假申请待处理,请登录沿海市政质管小程序处理。");
}
break;
case 3:
break;
}
noticeService.save(tNotice);
TLeaveAudit tLeaveAudit2 = new TLeaveAudit();
tLeaveAudit2.setLeaveId(leaveDTO.getId());
tLeaveAudit2.setAuditStatus(2);
tLeaveAudit2.setAuditId(user.getUserId() + "," + sysUser.getUserId());
tLeaveAudit2.setAuditType(2);
tLeaveAudit2.setAuditTime(LocalDateTime.now());
leaveAuditService.save(tLeaveAudit2);
break;
}
}else{
if (userDept.getId().equals(sysUser.getDeptId())){
LeaveAuditVO tLeaveAudit = new LeaveAuditVO();
tLeaveAudit.setAvatar(sysUser.getAvatar());
tLeaveAudit.setNickName(sysUser.getNickName());
TLeaveAudit tLeaveAudit1 = new TLeaveAudit();
tLeaveAudit1.setLeaveId(leaveDTO.getId());
tLeaveAudit1.setAuditStatus(1);
tLeaveAudit1.setAuditId(sysUser.getUserId() + "");
tLeaveAudit1.setAuditType(1);
leaveAuditService.save(tLeaveAudit1);
// 新增消息
TNotice tNotice = new TNotice();
tNotice.setUserId(sysUser.getUserId()+"");
tNotice.setNoticeContent(user.getNickName() + "提交了请假申请,请尽快审核");
tNotice.setStatus(1);
tNotice.setDataId(leaveDTO.getId());
tNotice.setNoticeType(6);
noticeService.save(tNotice);
switch (noticeSet.getNoticeType()) {
case 1:
msgUtils.sendMsg4(sysUser.getPhonenumber());
break;
case 2:
if (StringUtils.hasLength(email.getDataContent()) && StringUtils.hasLength(code.getDataContent())) {
EmailUtils.sendEmail(sysUser.getEmail(), email.getDataContent(), code.getDataContent(), "你有一条请假申请待处理,请登录沿海市政质管小程序处理。");
}
break;
case 3:
break;
}
TLeaveAudit tLeaveAudit2 = new TLeaveAudit();
tLeaveAudit2.setLeaveId(leaveDTO.getId());
tLeaveAudit2.setAuditStatus(2);
tLeaveAudit2.setAuditId(user.getUserId() + "," + sysUser.getUserId());
tLeaveAudit2.setAuditType(2);
tLeaveAudit2.setAuditTime(LocalDateTime.now());
leaveAuditService.save(tLeaveAudit2);
break;
}
}
}
}
}
} else {
List collect = sysUsers.stream().filter(e -> e.getDeptType() == 1 &&
e.getDeptId().equals(user.getDeptId())).collect(Collectors.toList());
for (SysUser sysUser : collect) {
Long userRole1 = sysUserService.getUserRole(sysUser.getUserId());
SysRole sysRole1 = roleService.selectRoleById(userRole1);
if (sysRole1 != null && sysRole1.getRoleName().contains("部门负责人")) {
TLeaveAudit tLeaveAudit1 = new TLeaveAudit();
tLeaveAudit1.setLeaveId(leaveDTO.getId());
tLeaveAudit1.setAuditStatus(1);
tLeaveAudit1.setAuditId(sysUser.getUserId() + "");
tLeaveAudit1.setAuditType(1);
leaveAuditService.save(tLeaveAudit1);
// 新增消息
TNotice tNotice = new TNotice();
tNotice.setUserId(sysUser.getUserId()+"");
tNotice.setNoticeContent(user.getNickName() + "提交了请假申请,请尽快审核");
tNotice.setStatus(1);
tNotice.setDataId(leaveDTO.getId());
tNotice.setNoticeType(6);
noticeService.save(tNotice);
switch (noticeSet.getNoticeType()) {
case 1:
msgUtils.sendMsg4(sysUser.getPhonenumber());
break;
case 2:
if (StringUtils.hasLength(email.getDataContent()) && StringUtils.hasLength(code.getDataContent())) {
EmailUtils.sendEmail(sysUser.getEmail(), email.getDataContent(), code.getDataContent(), "你有一条请假申请待处理,请登录沿海市政质管小程序处理。");
}
break;
case 3:
break;
}
TLeaveAudit tLeaveAudit2 = new TLeaveAudit();
tLeaveAudit2.setLeaveId(leaveDTO.getId());
tLeaveAudit2.setAuditStatus(2);
tLeaveAudit2.setAuditId(user.getUserId() + "," + auditPerson.getUserId());
tLeaveAudit2.setAuditType(2);
tLeaveAudit2.setAuditTime(LocalDateTime.now());
leaveAuditService.save(tLeaveAudit2);
}
}
}
return R.ok();
}
@ApiOperation(value = "我的审批-我的申请")
@PostMapping(value = "/leaveList")
public R> leaveList(@RequestBody LeaveListUserQuery query) {
query.setUserId(tokenService.getLoginUserApplet().getUserId() + "");
return R.ok(leaveService.leaveList(query));
}
@ApiOperation(value = "我的审批-审批")
@PostMapping(value = "/auditLeave")
public R auditLeave(@RequestBody LeaveAuditDTO dto) throws Exception {
dto.setAuditId(tokenService.getLoginUserApplet().getUserId() + "");
dto.setAuditTime(LocalDateTime.now());
leaveService.updateById(dto);
List sysUsers = sysUserService.selectAllList();
SysUser sysUser = sysUserService.selectUserById(Long.valueOf(dto.getLeavePerson()));
TLeaveAudit leaveAudit = leaveAuditService.lambdaQuery().eq(TLeaveAudit::getLeaveId, dto.getId())
.eq(TLeaveAudit::getAuditType, 2).one();
TNoticeSet noticeSet = noticeSetService.lambdaQuery().eq(TNoticeSet::getType, 2).last("limit 1")
.one();
// 邮箱
TDictData email = dictDataService.lambdaQuery().eq(TDictData::getDataType,4).one();
// 授权码
TDictData code = dictDataService.lambdaQuery().eq(TDictData::getDataType,5).one();
TNotice tNotice = new TNotice();
tNotice.setUserId(leaveAudit.getAuditId().split(",")[0]);
tNotice.setNoticeContent(sysUser.getNickName()+"的请假申请已审核,请查收");
tNotice.setStatus(1);
tNotice.setDataId(dto.getId());
tNotice.setNoticeType(7);
noticeService.save(tNotice);
LocalDateTime startTime = dto.getStartTime();
LocalDateTime endTime = dto.getEndTime();
SysUser sysUser1 = sysUsers.stream().filter(e -> e.getUserId().equals(Long.valueOf(leaveAudit.getAuditId().split(",")[0])))
.findFirst().orElse(null);
SysUser sysUser2 = sysUsers.stream().filter(e -> e.getUserId().equals(Long.valueOf(leaveAudit.getAuditId().split(",")[1])))
.findFirst().orElse(null);
// 转化为yyyy-MM-dd 格式字符串
String leaveTime = startTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))+"至"+endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
if (sysUser1!=null){
switch (noticeSet.getNoticeType()) {
case 1:
msgUtils.sendMsg5(sysUser1.getPhonenumber(),sysUser1.getNickName(),leaveTime);
break;
case 2:
if (StringUtils.hasLength(email.getDataContent())&&StringUtils.hasLength(code.getDataContent())){
EmailUtils.sendEmail(sysUser1.getEmail(),email.getDataContent(),code.getDataContent(),sysUser1.getNickName()+"的请假申请已审核,请查收");
}
break;
case 3:
break;
}
}
TNotice tNotice1 = new TNotice();
tNotice1.setUserId(leaveAudit.getAuditId().split(",")[1]);
tNotice1.setNoticeContent(sysUser.getNickName()+"的请假申请已审核,请查收");
tNotice1.setStatus(1);
tNotice1.setDataId(dto.getId());
tNotice1.setNoticeType(7);
noticeService.save(tNotice1);
if (sysUser2!=null) {
switch (noticeSet.getNoticeType()) {
case 1:
msgUtils.sendMsg5(sysUser2.getPhonenumber(), sysUser2.getNickName(), leaveTime);
break;
case 2:
if (StringUtils.hasLength(email.getDataContent()) && StringUtils.hasLength(code.getDataContent())) {
EmailUtils.sendEmail(sysUser2.getEmail(), email.getDataContent(), code.getDataContent(), sysUser2.getNickName() + "的请假申请已审核,请查收");
}
break;
case 3:
break;
}
}
return R.ok();
}
@ApiOperation(value = "我的审批-详情")
@GetMapping(value = "/leaveDetail")
public R leaveDetail(@RequestParam String id) {
TLeave leave = leaveService.getById(id);
LeaveUserDetailVO leaveUserDetailVO = new LeaveUserDetailVO();
SysUser sysUser = sysUserService.selectUserById(tokenService.getLoginUserApplet().getUserId());
Long userRole = sysUserService.getUserRole(sysUser.getUserId());
SysRole sysRole = roleService.selectRoleById(userRole);
if (sysRole.getRoleName().contains("片区负责人") || sysRole.getRoleName().equals("组长")|| sysRole.getRoleName().equals("队长")||sysRole.getRoleName().contains("项目部助理")) {
if (sysUser.getDeptType() == 1) {
TProjectDept tProjectDept = projectDeptService.getById(sysUser.getDeptId());
if (!tProjectDept.getParentId().equals("0")) {
TProjectDept tProjectDept1 = projectDeptService.getById(tProjectDept.getParentId());
leaveUserDetailVO.setDeptName(tProjectDept1.getProjectName() + ">" + tProjectDept.getProjectName());
} else {
leaveUserDetailVO.setDeptName(tProjectDept.getProjectName());
}
} else {
TDept tDept = deptService.getById(sysUser.getDeptId());
leaveUserDetailVO.setDeptName(tDept.getDeptName());
}
} else if (sysRole.getRoleName().contains("项目部负责人")) {
TProjectDept tProjectDept = projectDeptService.getById(sysUser.getDeptId());
leaveUserDetailVO.setDeptName(tProjectDept.getProjectName());
}
if (sysUser.getDeptType() == 2) {
TDept byId = deptService.getById(sysUser.getDeptId());
leaveUserDetailVO.setDeptName(byId.getDeptName());
}
if (sysUser.getDeptType()==1){
TProjectDept projectDept = projectDeptService.getById(sysUser.getDeptId());
if (projectDept.getParentId().equals("0")){
leaveUserDetailVO.setDeptName(projectDept.getProjectName());
}else{
TProjectDept projectDept1 = projectDeptService.getById(projectDept.getParentId());
leaveUserDetailVO.setDeptName(projectDept1.getProjectName()+">"+projectDept.getProjectName());
}
}
leaveUserDetailVO.setLeavePersonName(sysUser.getNickName());
LeaveAuditListVO leaveAuditListVO = new LeaveAuditListVO();
List list = leaveAuditService.list(new LambdaQueryWrapper()
.eq(TLeaveAudit::getLeaveId, leave.getId()));
for (TLeaveAudit tLeaveAudit : list) {
if (tLeaveAudit.getAuditType() == 1) {
LeaveAuditVO leaveAuditVO = new LeaveAuditVO();
SysUser sysUser1 = sysUserService.selectUserById(Long.valueOf(tLeaveAudit.getAuditId()));
if (sysUser1 != null) {
leaveAuditVO.setAvatar(sysUser1.getAvatar());
leaveAuditVO.setNickName(sysUser1.getNickName());
}
leaveAuditVO.setCreateTime(tLeaveAudit.getAuditTime());
leaveAuditVO.setAuditStatus(tLeaveAudit.getAuditStatus());
leaveAuditListVO.setAuditPerson(leaveAuditVO);
} else {
List leaveAuditVOS = new ArrayList<>();
// 抄送人
LeaveAuditVO leaveAuditVO = new LeaveAuditVO();
SysUser sysUser1 = sysUserService.selectUserById(Long.valueOf(tLeaveAudit.getAuditId().split(",")[0]));
leaveAuditVO.setAvatar(sysUser1.getAvatar());
leaveAuditVO.setNickName(sysUser1.getNickName());
leaveAuditVO.setCreateTime(tLeaveAudit.getCreateTime());
leaveAuditVO.setAuditStatus(tLeaveAudit.getAuditStatus());
leaveAuditVOS.add(leaveAuditVO);
LeaveAuditVO leaveAuditVO1 = new LeaveAuditVO();
SysUser sysUser2 = sysUserService.selectUserById(Long.valueOf(tLeaveAudit.getAuditId().split(",")[1]));
leaveAuditVO1.setAvatar(sysUser2.getAvatar());
leaveAuditVO1.setNickName(sysUser2.getNickName());
leaveAuditVO1.setCreateTime(tLeaveAudit.getCreateTime());
leaveAuditVO1.setAuditStatus(tLeaveAudit.getAuditStatus());
leaveAuditVOS.add(leaveAuditVO1);
leaveAuditListVO.setCopyPerson(leaveAuditVOS);
}
}
leaveUserDetailVO.setAudit(leaveAuditListVO);
leaveUserDetailVO.setId(leave.getId());
leaveUserDetailVO.setLeavePerson(leave.getLeavePerson());
leaveUserDetailVO.setStartTime(leave.getStartTime());
leaveUserDetailVO.setEndTime(leave.getEndTime());
leaveUserDetailVO.setLeaveType(leave.getLeaveType());
leaveUserDetailVO.setLeaveDay(leave.getLeaveDay());
leaveUserDetailVO.setLeaveCause(leave.getLeaveCause());
leaveUserDetailVO.setPictures(leave.getPictures());
leaveUserDetailVO.setAuditStatus(leave.getAuditStatus());
leaveUserDetailVO.setAuditId(leave.getAuditId());
leaveUserDetailVO.setAuditTime(leave.getAuditTime());
leaveUserDetailVO.setAuditRemark(leave.getAuditRemark());
leaveUserDetailVO.setCode(leave.getCode());
return R.ok(leaveUserDetailVO);
}
}