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.common.enums.QATestItemReportStatusEnum;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.dto.TTestMethodConfirmSheetDTO;
import com.ruoyi.system.dto.TestMethodConfirmSheetSignDTO;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.model.*;
import com.ruoyi.system.query.TTestMethodConfirmSheetQuery;
import com.ruoyi.system.service.*;
import com.ruoyi.system.vo.TTestMethodConfirmSheetVO;
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.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
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 TTestMethodConfirmSheetController {
private final TTestMethodConfirmSheetService testMethodConfirmSheetService;
private final TokenService tokenService;
private final ISysUserService sysUserService;
private final SysUserMapper sysUserMapper;
private final TTestMethodConfirmSheetTermService testMethodConfirmSheetTermService;
private final TTestMethodConfirmSheetOriginalService testMethodConfirmSheetOriginalService;
private final TProjectTeamService projectTeamService;
private final TProjectTeamStaffService projectTeamStaffService;
private final TExperimentDispatchService experimentDispatchService;
private final TProjectProposalService projectProposalService;
private final TExperimentDispatchParticipantsService experimentDispatchParticipantsService;
private final TNoticeService noticeService;
@Autowired
public TTestMethodConfirmSheetController(TTestMethodConfirmSheetService testMethodConfirmSheetService, TokenService tokenService, ISysUserService sysUserService, SysUserMapper sysUserMapper, TTestMethodConfirmSheetTermService testMethodConfirmSheetTermService, TTestMethodConfirmSheetOriginalService testMethodConfirmSheetOriginalService, TProjectTeamService projectTeamService, TProjectTeamStaffService projectTeamStaffService, TExperimentDispatchService experimentDispatchService, TProjectProposalService projectProposalService, TExperimentDispatchParticipantsService experimentDispatchParticipantsService, TNoticeService noticeService) {
this.testMethodConfirmSheetService = testMethodConfirmSheetService;
this.tokenService = tokenService;
this.sysUserService = sysUserService;
this.sysUserMapper = sysUserMapper;
this.testMethodConfirmSheetTermService = testMethodConfirmSheetTermService;
this.testMethodConfirmSheetOriginalService = testMethodConfirmSheetOriginalService;
this.projectTeamService = projectTeamService;
this.projectTeamStaffService = projectTeamStaffService;
this.experimentDispatchService = experimentDispatchService;
this.projectProposalService = projectProposalService;
this.experimentDispatchParticipantsService = experimentDispatchParticipantsService;
this.noticeService = noticeService;
}
/**
* 获取检验方法确认单管理列表
*/
//@PreAuthorize("@ss.hasPermi('system:testMethodConfirmSheet:list')")
@ApiOperation(value = "获取检验方法确认单分页列表",response = TTestMethodConfirmSheetQuery.class)
@PostMapping(value = "/api/t-test-method-confirm-sheet/pageList")
public R> pageList(@RequestBody String param) {
TTestMethodConfirmSheetQuery query = JSON.parseObject(param, TTestMethodConfirmSheetQuery.class);
// 获取当前用户
Long userId = tokenService.getLoginUser().getUserId();
Integer roleType = tokenService.getLoginUser().getUser().getRoleType();
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(testMethodConfirmSheetService.pageList(query));
}
/**
* 添加检验方法确认单管理
*/
//@PreAuthorize("@ss.hasPermi('system:testMethodConfirmSheet:add')")
@Log(title = "检验方法确认单信息-新增检验方法确认单", businessType = BusinessType.INSERT)
@ApiOperation(value = "添加检验方法确认单",response = TTestMethodConfirmSheetDTO.class)
@PostMapping(value = "/api/t-test-method-confirm-sheet/add")
public R add(@RequestBody String param) {
TTestMethodConfirmSheetDTO dto = JSON.parseObject(param,TTestMethodConfirmSheetDTO.class);
Long userId = tokenService.getLoginUser().getUserId();
dto.setCommitUserId(userId);
testMethodConfirmSheetService.save(dto);
List testMethodConfirmSheetTerms = dto.getTestMethodConfirmSheetTerms();
testMethodConfirmSheetTerms.forEach(testMethodConfirmSheetTerm -> {
testMethodConfirmSheetTerm.setTestId(dto.getId());
testMethodConfirmSheetTerm.setStatus(1);
});
testMethodConfirmSheetTermService.saveBatch(testMethodConfirmSheetTerms);
// 通过当前用户查询项目组
TProjectTeamStaff projectTeamStaff = projectTeamStaffService.getOne(Wrappers.lambdaQuery(TProjectTeamStaff.class)
.eq(TProjectTeamStaff::getUserId, userId)
.last("LIMIT 1"));
if(Objects.isNull(projectTeamStaff)){
return R.fail("当前用户未分配项目组,无法创建项目课题方案");
}
// 查询项目组
TProjectTeam projectTeam = projectTeamService.getById(projectTeamStaff.getTeamId());
if(Objects.isNull(projectTeam)){
return R.fail("项目组不存在");
}
if(projectTeam.getStatus() == 2){
return R.fail("项目组已封存,无法创建项目课题方案");
}
// 生成原始记录编号
String format = new SimpleDateFormat("yyyyMMdd").format(new Date());
String originalCode = projectTeam.getTeamName() + format.substring(2) + "-JL";
for (TTestMethodConfirmSheetTerm testMethodConfirmSheetTerm : testMethodConfirmSheetTerms) {
// 添加原始记录
TTestMethodConfirmSheetOriginal testMethodConfirmSheetOriginal = new TTestMethodConfirmSheetOriginal();
testMethodConfirmSheetOriginal.setTermId(testMethodConfirmSheetTerm.getId());
// 查询上个项目课题方案的序号
long count = testMethodConfirmSheetOriginalService.count(Wrappers.lambdaQuery(TTestMethodConfirmSheetOriginal.class)
.like(TTestMethodConfirmSheetOriginal::getOriginalCode, originalCode));
originalCode = originalCode + String.format("%02d", count+1);
testMethodConfirmSheetOriginal.setOriginalCode(originalCode);
testMethodConfirmSheetOriginal.setStatus(1);
testMethodConfirmSheetOriginalService.save(testMethodConfirmSheetOriginal);
}
// 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(), "您有【1】条【检验方法确认单】等待审批", tokenService.getLoginUser().getUser().getNickName(),6);
}
return R.ok();
}
/**
* 修改检验方法确认单
*/
//@PreAuthorize("@ss.hasPermi('system:testMethodConfirmSheet:edit')")
@Log(title = "检验方法确认单信息-修改检验方法确认单", businessType = BusinessType.UPDATE)
@ApiOperation(value = "修改检验方法确认单")
@PostMapping(value = "/api/t-test-method-confirm-sheet/update")
public R update(@RequestBody String param) {
TTestMethodConfirmSheetDTO dto = JSON.parseObject(param,TTestMethodConfirmSheetDTO.class);
testMethodConfirmSheetService.updateById(dto);
testMethodConfirmSheetTermService.remove(Wrappers.lambdaQuery(TTestMethodConfirmSheetTerm.class).eq(TTestMethodConfirmSheetTerm::getTestId,dto.getId()));
List testMethodConfirmSheetTerms = dto.getTestMethodConfirmSheetTerms();
testMethodConfirmSheetTerms.forEach(testMethodConfirmSheetTerm -> {
testMethodConfirmSheetTerm.setTestId(dto.getId());
testMethodConfirmSheetTerm.setStatus(1);
});
testMethodConfirmSheetTermService.saveBatch(testMethodConfirmSheetTerms);
return R.ok();
}
/**
* 查看检验方法确认单详情
*/
//@PreAuthorize("@ss.hasPermi('system:testMethodConfirmSheet:detail')")
@ApiOperation(value = "查看检验方法确认单详情")
@GetMapping(value = "/open/t-test-method-confirm-sheet/getDetailById")
public R getDetailById(@RequestParam String id) {
TTestMethodConfirmSheet testMethodConfirmSheet = testMethodConfirmSheetService.getById(id);
TTestMethodConfirmSheetVO testMethodConfirmSheetVO = new TTestMethodConfirmSheetVO();
BeanUtils.copyProperties(testMethodConfirmSheet, testMethodConfirmSheetVO);
// 获取检测项
List testMethodConfirmSheetTerms = testMethodConfirmSheetTermService.list(Wrappers.lambdaQuery(TTestMethodConfirmSheetTerm.class)
.eq(TTestMethodConfirmSheetTerm::getTestId, id));
testMethodConfirmSheetVO.setTestMethodConfirmSheetTerms(testMethodConfirmSheetTerms);
// 查询实验调度信息
TExperimentDispatch experimentDispatch = experimentDispatchService.getById(testMethodConfirmSheetVO.getDispatchId());
if(Objects.nonNull(experimentDispatch)){
testMethodConfirmSheetVO.setExperimentName(experimentDispatch.getExperimentName());
testMethodConfirmSheetVO.setExperimentCode(experimentDispatch.getExperimentCode());
// 查询项目课题方案
TProjectProposal projectProposal = projectProposalService.getById(experimentDispatch.getProposalId());
if(Objects.nonNull(projectProposal)){
testMethodConfirmSheetVO.setProjectName(projectProposal.getProjectName());
}
}
// 查询审核人姓名
SysUser sysUser = sysUserService.selectUserById(testMethodConfirmSheet.getAuditPersonId());
if(Objects.nonNull(sysUser)){
testMethodConfirmSheetVO.setAuditPersonName(sysUser.getNickName());
}
return R.ok(testMethodConfirmSheetVO);
}
/**
* 删除检验方法确认单
*/
//@PreAuthorize("@ss.hasPermi('system:testMethodConfirmSheet:delete')")
@Log(title = "检验方法确认单信息-删除检验方法确认单", businessType = BusinessType.DELETE)
@ApiOperation(value = "删除检验方法确认单")
@DeleteMapping(value = "/open/t-test-method-confirm-sheet/deleteById")
public R deleteById(@RequestParam String id) {
// 删除检测项
testMethodConfirmSheetTermService.remove(Wrappers.lambdaQuery(TTestMethodConfirmSheetTerm.class).eq(TTestMethodConfirmSheetTerm::getTestId, id));
return R.ok(testMethodConfirmSheetService.removeById(id));
}
/**
* 批量删除检验方法确认单
*/
//@PreAuthorize("@ss.hasPermi('system:testMethodConfirmSheet:delete')")
@Log(title = "检验方法确认单信息-删除检验方法确认单", businessType = BusinessType.DELETE)
@ApiOperation(value = "批量删除检验方法确认单")
@DeleteMapping(value = "/open/t-test-method-confirm-sheet/deleteByIds")
public R deleteByIds(@RequestBody List ids) {
// 删除检测项
testMethodConfirmSheetTermService.remove(Wrappers.lambdaQuery(TTestMethodConfirmSheetTerm.class).in(TTestMethodConfirmSheetTerm::getTestId, ids));
return R.ok(testMethodConfirmSheetService.removeByIds(ids));
}
/**
* 批量删除检验方法确认单
*/
//@PreAuthorize("@ss.hasPermi('system:testMethodConfirmSheet:sign')")
@Log(title = "检验方法确认单信息-检验方法确认单签字", businessType = BusinessType.UPDATE)
@ApiOperation(value = "检验方法确认单签字",response = TestMethodConfirmSheetSignDTO.class)
@PostMapping(value = "/api/t-test-method-confirm-sheet/sign")
public R sign(@RequestBody String param) {
TestMethodConfirmSheetSignDTO testMethodConfirmSheetSign = JSON.parseObject(param, TestMethodConfirmSheetSignDTO.class);
Long userId = tokenService.getLoginUser().getUserId();
testMethodConfirmSheetService.update(Wrappers.lambdaUpdate(TTestMethodConfirmSheet.class)
.eq(TTestMethodConfirmSheet::getId, testMethodConfirmSheetSign.getTestMethodConfirmSheetId())
.set(TTestMethodConfirmSheet::getAuditStatus, 2)
.set(TTestMethodConfirmSheet::getAuditTime, LocalDateTime.now())
.set(TTestMethodConfirmSheet::getAuditPersonId, userId)
.set(TTestMethodConfirmSheet::getAuditSign, testMethodConfirmSheetSign.getConfirmSign()));
// MESSAGE 添加消息
TTestMethodConfirmSheet testMethodConfirmSheet = testMethodConfirmSheetService.getById(testMethodConfirmSheetSign.getTestMethodConfirmSheetId());
noticeService.saveNotice(testMethodConfirmSheet.getCommitUserId(), "您的【检验方案确认单】已被确认", tokenService.getLoginUser().getUser().getNickName(),12);
return R.ok();
}
/**
* 撤销QA检测项报告管理
*/
//@PreAuthorize("@ss.hasPermi('system:qaTestItemReport:revokedReport')")
@Log(title = "检验方法确认单信息-撤销检验方法确认单信息状态", businessType = BusinessType.UPDATE)
@ApiOperation(value = "撤销检验方法确认单信息状态")
@PutMapping(value = "/open/t-test-method-confirm-sheet/revokedSheet")
public R revokedSheet(@RequestParam String id) {
TTestMethodConfirmSheet testMethodConfirmSheet = testMethodConfirmSheetService.getById(id);
testMethodConfirmSheet.setAuditStatus(QATestItemReportStatusEnum.REVOKED.getCode());
testMethodConfirmSheetService.updateById(testMethodConfirmSheet);
return R.ok();
}
}