package com.ruoyi.web.controller.api;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.basic.PageInfo;
import com.ruoyi.common.core.domain.BaseModel;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.dto.TAppealAuditDTO;
import com.ruoyi.system.model.*;
import com.ruoyi.system.query.InsepectorListQuery;
import com.ruoyi.system.service.*;
import com.ruoyi.system.vo.system.AppealDetailVO;
import com.ruoyi.system.vo.system.AppealListVO;
import com.ruoyi.web.controller.tool.EmailUtils;
import com.ruoyi.web.controller.tool.MsgUtils;
import com.tencentcloudapi.es.v20180416.models.TaskDetail;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
*
* 任务申诉 前端控制器
*
*
* @author xiaochen
* @since 2025-05-28
*/
@Api(tags = "任务申诉")
@RestController
@RequestMapping("/t-appeal")
public class TAppealController {
@Resource
private TTaskCleanService taskCleanService;
@Resource
private TLocationTypeService locationTypeService;
@Resource
private TLocationService locationService;
@Resource
private TTaskDetailService tTaskDetailService;
@Resource
private TProjectDeptService projectDeptService;
@Resource
private TDeptService deptService;
@Resource
private ISysUserService sysUserService;
@Resource
private TokenService tokenService;
@Resource
private TAppealService appealService;
@ApiOperation(value = "申诉记录分页列表")
@PostMapping(value = "/pageList")
public R> pageList(@RequestBody InsepectorListQuery query) {
List sysUsers = sysUserService.selectAllList();
List locationList = locationService.list();
if (StringUtils.hasLength(query.getDeptName())){
List list = projectDeptService.lambdaQuery().like(TProjectDept::getProjectName, query.getDeptName()).list();
List listIds = projectDeptService.lambdaQuery().like(TProjectDept::getProjectName, query.getDeptName()).list()
.stream().map(TProjectDept::getId).collect(Collectors.toList());
for (TProjectDept tProjectDept : list) {
if (tProjectDept.getParentId().equals("0")){
List collect = projectDeptService.lambdaQuery().eq(TProjectDept::getParentId, tProjectDept.getId()).list()
.stream().map(TProjectDept::getId).collect(Collectors.toList());
listIds.addAll(collect);
}
}
List deptIds = deptService.lambdaQuery().like(TDept::getDeptName, query.getDeptName()).list()
.stream().map(TDept::getId).collect(Collectors.toList());
listIds.addAll(deptIds);
if (listIds.isEmpty()){
listIds.add("0");
}
query.setPatrolInspectorIds(sysUsers.stream().filter(e->listIds.contains(e.getDeptId())).map(SysUser::getUserId)
.collect(Collectors.toList()));
if (StringUtils.hasLength(query.getPhonenumber())){
List patrolInspectorIds = sysUsers.stream().filter(sysUser ->
sysUser.getPhonenumber().equals(query.getPhonenumber())
&& listIds.contains(sysUser.getDeptId())
).map(SysUser::getUserId).collect(Collectors.toList());
if (patrolInspectorIds.isEmpty()){
return R.ok(new PageInfo<>());
}
query.setPatrolInspectorIds(patrolInspectorIds);
}
}
if (StringUtils.hasLength(query.getPhonenumber())){
List patrolInspectorIds = sysUsers.stream().filter(sysUser ->
sysUser.getPhonenumber().equals(query.getPhonenumber())
).map(SysUser::getUserId).collect(Collectors.toList());
if (!CollectionUtils.isEmpty(query.getPatrolInspectorIds())){
// 取交集
patrolInspectorIds = patrolInspectorIds.stream().filter(query.getPatrolInspectorIds()::contains).collect(Collectors.toList());
}
query.setPatrolInspectorIds(patrolInspectorIds);
if (patrolInspectorIds.isEmpty()){
return R.ok(new PageInfo<>());
}
}
if (query.getClearStatus()!=null){
List collect = tTaskDetailService.lambdaQuery().eq(TTaskDetail::getClearStatus, query.getClearStatus())
.eq(TTaskDetail::getHandleType,1)
.groupBy(TTaskDetail::getTaskId)
.orderByDesc(BaseModel::getCreateTime)
.list().stream().map(TTaskDetail::getTaskId).collect(Collectors.toList());
query.setTaskIds(collect);
if (collect.isEmpty()){
return R.ok(new PageInfo<>());
}
}
if (query.getLocationType()!=null){
List collect = locationList.stream().filter(e -> e.getLocationType().equals(query.getLocationType())).map(TLocation::getId)
.collect(Collectors.toList());
if (collect.isEmpty()){
return R.ok(new PageInfo<>());
}
query.setLocationIds(collect);
}
if (StringUtils.hasLength(query.getLocationName())){
List collect = locationList.stream().filter(e -> e.getLocationName().contains(query.getLocationName())).map(TLocation::getId)
.collect(Collectors.toList());
if (collect.isEmpty()){
query.setLocationIds(collect);
return R.ok(new PageInfo<>());
}else{
if (query.getLocationIds()!=null){
collect = collect.stream().filter(query.getLocationIds()::contains).collect(Collectors.toList());
if (collect.isEmpty()){
return R.ok(new PageInfo<>());
}
}
query.setLocationIds(collect);
}
}
return R.ok(appealService.pageList(query));
}
@Log(title = "批量删除申诉记录", businessType = BusinessType.DELETE)
@ApiOperation(value = "批量删除申诉记录")
@DeleteMapping(value = "/deleteByIds")
public R deleteByIds(@RequestParam String ids) {
String[] split = ids.split(",");
appealService.removeBatchByIds(Arrays.asList(split));
return R.ok();
}
@Log(title = "删除申诉记录", businessType = BusinessType.DELETE)
@ApiOperation(value = "删除申诉记录")
@DeleteMapping(value = "/deleteById")
public R deleteById(@RequestParam String id) {
appealService.removeById(id);
return R.ok();
}
@ApiOperation(value = "详情申诉记录")
@GetMapping(value = "/detail")
public R detail(@RequestParam String id) {
AppealDetailVO appealDetailVO = new AppealDetailVO();
TAppeal appeal = appealService.getById(id);
BeanUtils.copyProperties(appeal,appealDetailVO);
TTask byId = taskCleanService.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(byId2.getLocationName());
appealDetailVO.setTaskId(byId.getId());
if (appeal.getAuditPerson()!=null){
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);
}
@Resource
private TNoticeService noticeService;
@Resource
private MsgUtils msgUtils;
@Resource
private TNoticeSetService noticeSetService;
@Resource
private TDictDataService dictDataService;
@Log(title = "审核申诉记录", businessType = BusinessType.UPDATE)
@ApiOperation(value = "审核申诉记录")
@PostMapping(value = "/audit")
public R audit(@RequestBody @Valid TAppealAuditDTO dto) throws Exception {
List sysUsers = sysUserService.selectAllList();
TAppeal appeal = appealService.getById(dto.getId());
SysUser sysUser = sysUsers.stream().filter(e -> e.getUserId().equals(Long.valueOf(dto.getAppealPerson())))
.findFirst().orElse(null);
dto.setStatus(dto.getStatus());
dto.setAuditPerson(tokenService.getLoginUser().getUserId()+"");
dto.setAuditTime(LocalDateTime.now());
TTask task = taskCleanService.getById(appeal.getTaskId());
TLocation location = locationService.getById(task.getLocationId());
TNotice tNotice = new TNotice();
tNotice.setUserId(appeal.getAppealPerson());
tNotice.setStatus(1);
tNotice.setDataId(dto.getId());
dto.setAuditTime(LocalDateTime.now());
TTaskDetail taskDetail = new TTaskDetail();
taskDetail.setTaskId(appeal.getTaskId());
Long count = tTaskDetailService.lambdaQuery().eq(TTaskDetail::getTaskId, appeal.getTaskId())
.eq(TTaskDetail::getHandleType, 1).count();
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 (dto.getStatus()==2){
taskDetail.setClearStatus(1);
taskDetail.setAuditTime(LocalDateTime.now());
taskDetail.setAuditPerson(tokenService.getLoginUser().getUserId()+"");
taskDetail.setAuditStatus(1);
taskDetail.setAuditRemark(dto.getAuditRemark());
taskDetail.setHandleType(5);
taskDetail.setAppealId(dto.getId());
// 通过
TTask byId = taskCleanService.getById(dto.getTaskId());
if (count>1){
byId.setStatus(5);
}else{
byId.setStatus(6);
}
taskCleanService.updateById(byId);
tNotice.setNoticeType(5);
tNotice.setNoticeContent("【"+location.getLocationName()+"】任务申诉通过");
if (sysUser!=null){
switch (noticeSet.getNoticeType()) {
case 1:
msgUtils.sendMsg3(sysUser.getPhonenumber(),location.getLocationName(),"通过");
break;
case 2:
if (StringUtils.hasLength(email.getDataContent())&&StringUtils.hasLength(code.getDataContent())){
EmailUtils.sendEmail(sysUser.getEmail(),email.getDataContent(),code.getDataContent(),"【"+location.getLocationName()+"】任务申诉通过");
}
break;
case 3:
break;
}
}
}else{
taskDetail.setClearStatus(2);
taskDetail.setAuditTime(LocalDateTime.now());
taskDetail.setAuditPerson(tokenService.getLoginUser().getUserId()+"");
taskDetail.setAuditStatus(2);
taskDetail.setAuditRemark(dto.getAuditRemark());
taskDetail.setHandleType(6);
taskDetail.setAppealId(dto.getId());
tNotice.setNoticeType(4);
tNotice.setNoticeContent("【"+location.getLocationName()+"】任务申诉驳回");
if (sysUser!=null){
switch (noticeSet.getNoticeType()) {
case 1:
msgUtils.sendMsg3(sysUser.getPhonenumber(),location.getLocationName(),"通过");
break;
case 2:
if (StringUtils.hasLength(email.getDataContent())&&StringUtils.hasLength(code.getDataContent())){
EmailUtils.sendEmail(sysUser.getEmail(),email.getDataContent(),code.getDataContent(),"【"+location.getLocationName()+"】任务申诉驳回");
}
break;
case 3:
break;
}
}
}
noticeService.save(tNotice);
dto.setAuditPerson(tokenService.getLoginUser().getUserId()+"");
appealService.updateById(dto);
tTaskDetailService.save(taskDetail);
return R.ok();
}
}