package com.ruoyi.web.controller.api;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.basic.PageInfo;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.dto.TInspectionReportDTO;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.model.*;
import com.ruoyi.system.query.TInspectionReportQuery;
import com.ruoyi.system.service.*;
import com.ruoyi.system.vo.TInspectionReportVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
/**
*
* 检验报告 前端控制器
*
*
* @author xiaochen
* @since 2025-04-08
*/
@Api(tags = "检验报告管理")
@RestController
@RequestMapping("")
public class TInspectionReportController {
private final TInspectionReportService inspectionReportService;
private final TExperimentDispatchService experimentDispatchService;
private final TProjectProposalService projectProposalService;
private final TExperimentDispatchParticipantsService experimentDispatchParticipantsService;
private final TokenService tokenService;
private final ISysUserService sysUserService;
private final SysUserMapper sysUserMapper;
private final TNoticeService noticeService;
private final TProjectTeamService projectTeamService;
private final TProjectTeamStaffService projectTeamStaffService;
@Autowired
public TInspectionReportController(TInspectionReportService inspectionReportService, TExperimentDispatchService experimentDispatchService, TProjectProposalService projectProposalService, TExperimentDispatchParticipantsService experimentDispatchParticipantsService, TokenService tokenService, ISysUserService sysUserService, SysUserMapper sysUserMapper, TNoticeService noticeService, TProjectTeamService projectTeamService, TProjectTeamStaffService projectTeamStaffService) {
this.inspectionReportService = inspectionReportService;
this.experimentDispatchService = experimentDispatchService;
this.projectProposalService = projectProposalService;
this.experimentDispatchParticipantsService = experimentDispatchParticipantsService;
this.tokenService = tokenService;
this.sysUserService = sysUserService;
this.sysUserMapper = sysUserMapper;
this.noticeService = noticeService;
this.projectTeamService = projectTeamService;
this.projectTeamStaffService = projectTeamStaffService;
}
/**
* 获取检验报告管理列表
*/
//@PreAuthorize("@ss.hasPermi('system:inspectionReport:list')")
@ApiOperation(value = "获取检验报告分页列表", response = TInspectionReportQuery.class)
@PostMapping(value = "/api/t-inspection-report/pageList")
public R> pageList(@RequestBody String param) {
TInspectionReportQuery query = JSON.parseObject(param, TInspectionReportQuery.class);
// 获取当前用户
Long userId = tokenService.getLoginUser().getUserId();
Integer roleType = tokenService.getLoginUser().getUser().getRoleType();
query.setRoleType(roleType);
if (roleType != 1){
query.setUserId(userId);
if(roleType ==2){
// 查询项目组
TProjectTeamStaff projectTeamStaff = projectTeamStaffService.getOne(Wrappers.lambdaQuery(TProjectTeamStaff.class)
.eq(TProjectTeamStaff::getUserId, userId));
// 查询项目的工艺工程师id
TProjectTeamStaff teamStaff = projectTeamStaffService.getOne(Wrappers.lambdaQuery(TProjectTeamStaff.class)
.eq(TProjectTeamStaff::getTeamId, projectTeamStaff.getTeamId())
.eq(TProjectTeamStaff::getRoleType, 3)
.last("LIMIT 1"));
// 查询实验参与人员
List experimentDispatchParticipants = experimentDispatchParticipantsService.list(Wrappers.lambdaQuery(TExperimentDispatchParticipants.class)
.eq(TExperimentDispatchParticipants::getUserId, teamStaff.getUserId()));
if(!CollectionUtils.isEmpty(experimentDispatchParticipants)){
List dispatchIds = experimentDispatchParticipants.stream().map(TExperimentDispatchParticipants::getDispatchId).distinct().collect(Collectors.toList());
query.setDispatchIds(dispatchIds);
}
}else {
// 查询实验参与人员
List experimentDispatchParticipants = experimentDispatchParticipantsService.list(Wrappers.lambdaQuery(TExperimentDispatchParticipants.class)
.eq(TExperimentDispatchParticipants::getUserId, userId));
if (!CollectionUtils.isEmpty(experimentDispatchParticipants)) {
List dispatchIds = experimentDispatchParticipants.stream().map(TExperimentDispatchParticipants::getDispatchId).distinct().collect(Collectors.toList());
query.setDispatchIds(dispatchIds);
}
}
}
return R.ok(inspectionReportService.pageList(query));
}
/**
* 添加检验报告管理
*/
//@PreAuthorize("@ss.hasPermi('system:inspectionReport:add')")
@Log(title = "检验报告信息-新增检验报告", businessType = BusinessType.INSERT)
@ApiOperation(value = "添加检验报告",response = TInspectionReportDTO.class)
@PostMapping(value = "/api/t-inspection-report/add")
public R add(@RequestBody String param) {
TInspectionReportDTO dto = JSON.parseObject(param,TInspectionReportDTO.class);
inspectionReportService.save(dto);
// 通过当前用户查询项目组
Long userId = tokenService.getLoginUser().getUserId();
TProjectTeamStaff projectTeamStaff = projectTeamStaffService.getOne(Wrappers.lambdaQuery(TProjectTeamStaff.class)
.eq(TProjectTeamStaff::getUserId, userId)
.last("LIMIT 1"));
// 查询项目组
TProjectTeam projectTeam = projectTeamService.getById(projectTeamStaff.getTeamId());
// MESSAGE 添加消息
TProjectTeamStaff teamStaff = projectTeamStaffService.getOne(Wrappers.lambdaQuery(TProjectTeamStaff.class)
.eq(TProjectTeamStaff::getTeamId, projectTeam.getId())
.eq(TProjectTeamStaff::getRoleType, 3)
.last("LIMIT 1"));
if(Objects.nonNull(teamStaff)){
noticeService.saveNotice(teamStaff.getUserId(), "化验师已提交检验报告,请查收!", tokenService.getLoginUser().getUser().getNickName(),7);
}
return R.ok();
}
/**
* 修改检验报告
*/
//@PreAuthorize("@ss.hasPermi('system:inspectionReport:edit')")
@Log(title = "检验报告信息-修改检验报告", businessType = BusinessType.UPDATE)
@ApiOperation(value = "修改检验报告")
@PostMapping(value = "/api/t-inspection-report/update")
public R update(@RequestBody String param) {
TInspectionReportDTO dto = JSON.parseObject(param,TInspectionReportDTO.class);
inspectionReportService.updateById(dto);
return R.ok();
}
/**
* 查看检验报告详情
*/
//@PreAuthorize("@ss.hasPermi('system:inspectionReport:detail')")
@ApiOperation(value = "查看检验报告详情")
@GetMapping(value = "/open/t-inspection-report/getDetailById")
public R getDetailById(@RequestParam String id) {
TInspectionReport inspectionReport = inspectionReportService.getById(id);
TInspectionReportVO inspectionReportVO = new TInspectionReportVO();
BeanUtils.copyProperties(inspectionReport, inspectionReportVO);
// 查询实验调度
TExperimentDispatch experimentDispatch = experimentDispatchService.getById(inspectionReportVO.getDispatchId());
// 查询项目课题
TProjectProposal projectProposal = projectProposalService.getById(experimentDispatch.getProposalId());
experimentDispatch.setProjectName(projectProposal.getProjectName());
// 查询实验调度人员
List experimentDispatchParticipants = experimentDispatchParticipantsService.list(Wrappers.lambdaQuery(TExperimentDispatchParticipants.class)
.eq(TExperimentDispatchParticipants::getDispatchId, inspectionReportVO.getDispatchId()));
// 将参与人员名字拼接
List userIds = experimentDispatchParticipants.stream().map(TExperimentDispatchParticipants::getUserId).distinct().collect(Collectors.toList());
List sysUsers = sysUserMapper.selectUserByIds(userIds);
if(!CollectionUtils.isEmpty(sysUsers)){
String nickNames = sysUsers.stream().map(SysUser::getNickName).collect(Collectors.joining(","));
experimentDispatch.setParticipantsName(nickNames);
}
inspectionReportVO.setExperimentDispatch(experimentDispatch);
return R.ok(inspectionReportVO);
}
/**
* 删除检验报告
*/
//@PreAuthorize("@ss.hasPermi('system:inspectionReport:delete')")
@Log(title = "检验报告信息-删除检验报告", businessType = BusinessType.DELETE)
@ApiOperation(value = "删除检验报告")
@DeleteMapping(value = "/open/t-inspection-report/deleteById")
public R deleteById(@RequestParam String id) {
return R.ok(inspectionReportService.removeById(id));
}
/**
* 批量删除检验报告
*/
//@PreAuthorize("@ss.hasPermi('system:inspectionReport:delete')")
@Log(title = "检验报告信息-删除检验报告", businessType = BusinessType.DELETE)
@ApiOperation(value = "批量删除检验报告")
@DeleteMapping(value = "/open/t-inspection-report/deleteByIds")
public R deleteByIds(@RequestBody List ids) {
return R.ok(inspectionReportService.removeByIds(ids));
}
/**
* 提交检验报告
*/
//@PreAuthorize("@ss.hasPermi('system:inspectionReport:commit')")
@Log(title = "检验报告信息-提交检验报告", businessType = BusinessType.UPDATE)
@ApiOperation(value = "提交检验报告")
@PutMapping(value = "/open/t-inspection-report/commit")
public R commit(@RequestParam(value = "id") String id) {
// 修改状态为已提交
TInspectionReport inspectionReport = inspectionReportService.getById(id);
inspectionReport.setStatus(2);
inspectionReportService.updateById(inspectionReport);
return R.ok();
}
}