package com.glxp.api.controller.trace; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.lang.UUID; import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.glxp.api.annotation.AuthRuleAnnotation; import com.glxp.api.common.enums.ResultEnum; import com.glxp.api.common.res.BaseResponse; import com.glxp.api.common.util.ResultVOUtils; import com.glxp.api.config.rabbit.TopicRabbitConfig; import com.glxp.api.constant.Constant; import com.glxp.api.controller.BaseController; import com.glxp.api.entity.basic.UdiEntity; import com.glxp.api.entity.sup.UserCompanyEntity; import com.glxp.api.entity.trace.*; import com.glxp.api.req.basic.FilterUdiRelRequest; import com.glxp.api.req.trace.TraceManuDetailRequest; import com.glxp.api.req.trace.TraceManuOrderUploadRequest; import com.glxp.api.req.trace.TraceOrderUploadRequest; import com.glxp.api.req.trace.TraceRecordRequest; import com.glxp.api.res.basic.BasicUdiRelResponse; import com.glxp.api.res.trace.TraceSearchReponse; import com.glxp.api.service.basic.BasicUdiRelService; import com.glxp.api.service.sup.UserCompanyService; import com.glxp.api.service.system.SystemParamConfigService; import com.glxp.api.service.trace.*; import com.glxp.api.util.DateUtil; import com.glxp.api.util.GennerOrderUtils; import com.glxp.api.util.udi.FilterUdiUtils; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.BeanUtils; import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import springfox.documentation.annotations.ApiIgnore; import javax.annotation.Resource; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; @Api(tags = "UDI追溯相关接口") @ApiIgnore @RestController @Slf4j @RequiredArgsConstructor public class UdiTraceController extends BaseController { @Resource TraceProductDetailService traceProductDetailService; @Resource BasicUdiRelService basicUdiRelService; @Resource GennerOrderUtils gennerOrderUtils; @Resource TraceTaskNoticeService traceTaskNoticeService; @Resource TraceOrderService traceOrderService; @Resource TraceManuOrderService traceManuOrderService; @Resource TraceManuCodeRelService traceManuCodeRelService; @Resource TraceRecordService traceRecordService; @Resource UdiTraceService udiTraceService; @Resource UserCompanyService userCompanyService; @Resource SystemParamConfigService systemParamConfigService; @Resource RabbitPushService rabbitPushService; @Resource private final RabbitTemplate rabbitTemplate; @ApiIgnore @GetMapping("/udi/trace/source/sendUDI") public BaseResponse sendUdi(@RequestBody TraceRecordRequest traceRecordRequest) { String messageJson = JSON.toJSONString(traceRecordRequest); System.out.println(messageJson); rabbitTemplate.convertAndSend("trace.topic.exchange", TopicRabbitConfig.common, messageJson); return ResultVOUtils.success("发送成功"); } @PostMapping("/udi/trace/source/task") public BaseResponse sendTask(@RequestBody MqTaskDelayMessage mqTaskDelayMessage) { String messageJson = JSON.toJSONString(mqTaskDelayMessage); System.out.println(messageJson); rabbitTemplate.convertAndSend(TopicRabbitConfig.TRACE_DELAY_EXCHANGE, TopicRabbitConfig.delaytrace, mqTaskDelayMessage); return ResultVOUtils.success("发送成功"); } /** * 源头追溯查询 * * @param traceRecordRequest * @return */ @ApiOperation(value = "源头追溯查询",response = TraceOrderEntity.class) @AuthRuleAnnotation("udi/trace/source/search") @GetMapping("udi/trace/source/search") public BaseResponse createTrace(TraceRecordRequest traceRecordRequest) { TraceRecordLogEntity traceRecordLogEntity; TraceProductDetailEntity traceProductDetailEntity = null; BasicUdiRelResponse basicUdiRelResponse = null; UdiEntity udiEntity = null; UserCompanyEntity userCompanyEntity = null; if (StrUtil.isNotEmpty(traceRecordRequest.getUdiCode())) { traceRecordRequest.setTraceType(Constant.UDI_TRACE); } if (traceRecordRequest.getTraceType() == Constant.UDI_TRACE) { //1.校验UDI码格式 udiEntity = FilterUdiUtils.getUdi(traceRecordRequest.getUdiCode()); if (udiEntity == null) { return ResultVOUtils.error(500, "UDI码格式错误"); } //2.查询源头企业 System.out.println(udiEntity.getUdi()); userCompanyEntity = basicUdiRelService.findByNameCode(udiEntity.getUdi()); if (userCompanyEntity == null) { return ResultVOUtils.error(500, "生产企业未关联该产品信息!"); } //3.查询产品信息 basicUdiRelResponse = basicUdiRelService.findByProductNameCode(udiEntity.getUdi()); if (basicUdiRelResponse == null) { return ResultVOUtils.error(500, "未查询到产品信息"); } //4.生成查询记录 String userId = getUserId() + ""; traceRecordLogEntity = udiTraceService.createRecordLog(userId, traceRecordRequest.getFromType(), traceRecordRequest.getTraceType()); //5.创建查询详情记录,若存在相同查询记录则直接获取 traceProductDetailEntity = udiTraceService.createTracerProductByUdiCode(traceRecordRequest.getUdiCode(), 1, 1, basicUdiRelResponse, udiEntity); traceRecordLogEntity.setTraceProductIdFk(traceProductDetailEntity.getId()); traceRecordService.updateById(traceRecordLogEntity); udiTraceService.createSourceTraceTask(traceProductDetailEntity, userCompanyEntity.getId()); } else { //1:校验参数是否正确 if (StrUtil.isEmpty(traceRecordRequest.getYlqxzcrbarmc())) { return ResultVOUtils.error(500, "医疗器械注册人不能为空!"); } if (StrUtil.isEmpty(traceRecordRequest.getCpmctymc())) { return ResultVOUtils.error(500, "产品通用名称不能为空!"); } if (StrUtil.isEmpty(traceRecordRequest.getGgxh())) { return ResultVOUtils.error(500, "规格型号不能为空!"); } if (StrUtil.isEmpty(traceRecordRequest.getNameCode())) { return ResultVOUtils.error(500, "DI标识不能为空!"); } if (StrUtil.isEmpty(traceRecordRequest.getBatchNo())) { return ResultVOUtils.error(500, "请手动输入批次号!"); } //2.查询源头企业 FilterUdiRelRequest filterUdiRelRequest = new FilterUdiRelRequest(); filterUdiRelRequest.setYlqxzcrbarmc(traceRecordRequest.getYlqxzcrbarmc()); filterUdiRelRequest.setCpmctymc(traceRecordRequest.getCpmctymc()); filterUdiRelRequest.setGgxh(traceRecordRequest.getGgxh()); userCompanyEntity = basicUdiRelService.findByProduct(filterUdiRelRequest); if (userCompanyEntity == null) { return ResultVOUtils.error(500, "生产企业未关联该产品信息!"); } //3.查询产品信息 basicUdiRelResponse = basicUdiRelService.findByProductName(filterUdiRelRequest); if (basicUdiRelResponse == null) { return ResultVOUtils.error(500, "未查询到产品信息"); } //4.生成查询记录 String userId = getUserId() + ""; traceRecordLogEntity = udiTraceService.createRecordLog(userId, traceRecordRequest.getFromType(), traceRecordRequest.getTraceType()); //5.创建查询详情记录,若存在相同查询记录则直接获取 traceProductDetailEntity = udiTraceService.createTracerProductByBatchNo(traceRecordRequest, 1, 2, basicUdiRelResponse); traceRecordLogEntity.setTraceProductIdFk(traceProductDetailEntity.getId()); traceRecordService.updateById(traceRecordLogEntity); udiTraceService.createSourceTraceTask(traceProductDetailEntity, userCompanyEntity.getId()); } List traceOrderEntities = traceOrderService.findByProductIdFk(traceRecordLogEntity.getTraceProductIdFk()); return ResultVOUtils.success(traceOrderEntities); } /** * 现场检查查询 * * @param traceRecordRequest * @return */ @ApiOperation(value = "现场检查查询",response = TraceOrderEntity.class) @AuthRuleAnnotation("udi/trace/scene/search") @GetMapping("udi/trace/scene/search") public BaseResponse sceneTrace(TraceRecordRequest traceRecordRequest) { TraceSearchReponse traceSearchReponse = new TraceSearchReponse(); TraceRecordLogEntity traceRecordLogEntity = null; TraceProductDetailEntity traceProductDetailEntity = null; BasicUdiRelResponse basicUdiRelResponse = null; UdiEntity udiEntity = null; UserCompanyEntity userCompanyEntity = null; if (StrUtil.isNotEmpty(traceRecordRequest.getUdiCode())) { traceRecordRequest.setTraceType(Constant.UDI_TRACE); } if (traceRecordRequest.getTraceType() == Constant.UDI_TRACE) { //1.校验UDI码格式 udiEntity = FilterUdiUtils.getUdi(traceRecordRequest.getUdiCode()); if (udiEntity == null) { return ResultVOUtils.error(500, "UDI码格式错误"); } //2.查询报备企业 userCompanyEntity = userCompanyService.getById(traceRecordRequest.getCompanyId()); if (userCompanyEntity == null) { return ResultVOUtils.error(500, "未查询到该报备企业信息!"); } //3.查询产品信息 basicUdiRelResponse = basicUdiRelService.findByProductNameCode(udiEntity.getUdi()); if (basicUdiRelResponse == null) { return ResultVOUtils.error(500, "未查询到产品信息"); } //4.生成查询记录 String userId = getUserId() + ""; traceRecordLogEntity = udiTraceService.createRecordLog(userId, traceRecordRequest.getFromType(), traceRecordRequest.getTraceType()); //5.创建查询详情记录,若存在相同查询记录则直接获取 traceProductDetailEntity = udiTraceService.createTracerProductByUdiCode(traceRecordRequest.getUdiCode(), 2, 1, basicUdiRelResponse, udiEntity); traceRecordLogEntity.setTraceProductIdFk(traceProductDetailEntity.getId()); traceRecordService.updateById(traceRecordLogEntity); udiTraceService.createCheckTraceTask(traceProductDetailEntity, userCompanyEntity.getId()); } else { //1:校验参数是否正确 if (StrUtil.isEmpty(traceRecordRequest.getYlqxzcrbarmc())) { return ResultVOUtils.error(500, "医疗器械注册人不能为空!"); } if (StrUtil.isEmpty(traceRecordRequest.getCpmctymc())) { return ResultVOUtils.error(500, "产品通用名称不能为空!"); } if (StrUtil.isEmpty(traceRecordRequest.getGgxh())) { return ResultVOUtils.error(500, "规格型号不能为空!"); } //2.查询源头企业 FilterUdiRelRequest filterUdiRelRequest = new FilterUdiRelRequest(); filterUdiRelRequest.setYlqxzcrbarmc(traceRecordRequest.getYlqxzcrbarmc()); filterUdiRelRequest.setCpmctymc(traceRecordRequest.getCpmctymc()); filterUdiRelRequest.setGgxh(traceRecordRequest.getGgxh()); userCompanyEntity = userCompanyService.getById(traceRecordRequest.getCompanyId()); if (userCompanyEntity == null) { return ResultVOUtils.error(500, "未查询到该报备企业信息!"); } //3.查询产品信息 basicUdiRelResponse = basicUdiRelService.findByProductName(filterUdiRelRequest); if (basicUdiRelResponse == null) { return ResultVOUtils.error(500, "未查询到产品信息"); } //4.生成查询记录 String userId = getUserId() + ""; traceRecordLogEntity = udiTraceService.createRecordLog(userId, traceRecordRequest.getFromType(), traceRecordRequest.getTraceType()); //5.创建查询详情记录,若存在相同查询记录则直接获取 traceProductDetailEntity = udiTraceService.createTracerProductByBatchNo(traceRecordRequest, 2, 2, basicUdiRelResponse); udiTraceService.createCheckTraceTask(traceProductDetailEntity, userCompanyEntity.getId()); } List traceOrderEntities = traceOrderService.findByProductIdFk(traceRecordLogEntity.getTraceProductIdFk()); return ResultVOUtils.success(traceOrderEntities); } /** * 客户端根据任务上传出入库单据 * * @param traceOrderUploadRequest * @param bindingResult * @return */ @Transactional @ApiOperation(value = "客户端根据任务上传出入库单据",response = BaseResponse.class) @AuthRuleAnnotation("udi/trace/order/upload") @PostMapping("udi/trace/order/upload") public BaseResponse uploadOrder(@RequestBody TraceOrderUploadRequest traceOrderUploadRequest, BindingResult bindingResult) { if (bindingResult.hasErrors()) { return ResultVOUtils.error(ResultEnum.PARAM_VERIFY_FALL, bindingResult.getFieldError().getDefaultMessage()); } TraceTaskNoticeEntity taskNoticeEntity = traceTaskNoticeService.getById(traceOrderUploadRequest.getTaskId()); if (taskNoticeEntity == null) { return ResultVOUtils.error(500, "任务已过期,或任务不存在!"); } //清空历史数据 traceOrderService.deleteByCompanyId(taskNoticeEntity.getProdudctRecordIdFk(), taskNoticeEntity.getCompanyIdFk()); TraceOrderEntity traceOrderEntity = new TraceOrderEntity(); BeanUtils.copyProperties(traceOrderUploadRequest, traceOrderEntity); traceOrderEntity.setId(IdUtil.getSnowflakeNextId()); traceOrderEntity.setUpdateTime(new Date()); traceOrderService.save(traceOrderEntity); //设置任务已完成 taskNoticeEntity.setStatus(3); traceTaskNoticeService.updateById(taskNoticeEntity); udiTraceService.pushDelayRemoveTask(taskNoticeEntity); return ResultVOUtils.success("上传成功"); } /** * 客户端根据任务上传生产关联关系信息 * * @param traceManuOrderUploadRequest * @param bindingResult * @return */ @Transactional @ApiOperation(value = "客户端根据任务上传生产关联关系信息",response = BaseResponse.class) @AuthRuleAnnotation("udi/trace/manuOrder/upload") @PostMapping("udi/trace/manuOrder/upload") public BaseResponse uploadManuOrder(@RequestBody TraceManuOrderUploadRequest traceManuOrderUploadRequest, BindingResult bindingResult) { if (bindingResult.hasErrors()) { return ResultVOUtils.error(ResultEnum.PARAM_VERIFY_FALL, bindingResult.getFieldError().getDefaultMessage()); } TraceTaskNoticeEntity taskNoticeEntity = traceTaskNoticeService.getById(traceManuOrderUploadRequest.getTaskId()); if (taskNoticeEntity == null) { return ResultVOUtils.error(500, "任务已过期,或任务不存在!"); } //清除历史数据 TraceManuOrderEntity originOrder = traceManuOrderService.findByBillNo(traceManuOrderUploadRequest.getBillNo(), taskNoticeEntity.getProdudctRecordIdFk(), taskNoticeEntity.getCompanyIdFk()); if (originOrder != null) { traceManuOrderService.removeById(originOrder.getId()); traceManuCodeRelService.remove(new QueryWrapper().eq("manuOrderIdFk", originOrder.getId())); } //新增生产订单 TraceManuOrderEntity traceManuOrderEntity = new TraceManuOrderEntity(); BeanUtils.copyProperties(traceManuOrderUploadRequest, traceManuOrderEntity); traceManuOrderEntity.setId(IdUtil.getSnowflakeNextId()); traceManuOrderService.save(traceManuOrderEntity); //新增订单关联明细 List traceManuDetailRequests = traceManuOrderUploadRequest.getDetails(); if (CollUtil.isNotEmpty(traceManuDetailRequests)) { for (TraceManuDetailRequest traceManuDetailRequest : traceManuDetailRequests) { TraceManuCodeRelEntity traceManuCodeRelEntity = new TraceManuCodeRelEntity(); BeanUtils.copyProperties(traceManuDetailRequest, traceManuCodeRelEntity); traceManuCodeRelEntity.setId(IdUtil.getSnowflakeNextId()); traceManuCodeRelEntity.setManuOrderIdFk(traceManuOrderEntity.getId()); traceManuOrderService.save(traceManuOrderEntity); } } //设置任务已完成 taskNoticeEntity.setStatus(3); traceTaskNoticeService.updateById(taskNoticeEntity); udiTraceService.pushDelayRemoveTask(taskNoticeEntity); return ResultVOUtils.success("上传成功"); } }