package com.glxp.api.controller.trace; import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.StrUtil; 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.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.TraceOrderUploadRequest; import com.glxp.api.req.trace.TraceRecordRequest; import com.glxp.api.res.basic.BasicUdiRelResponse; import com.glxp.api.service.basic.BasicUdiRelService; import com.glxp.api.service.trace.*; import com.glxp.api.util.GennerOrderUtils; import com.glxp.api.util.udi.FilterUdiUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; 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.List; @ApiIgnore @RestController @Slf4j public class UdiTraceController extends BaseController { @Resource TraceProductDetailService traceProductDetailService; @Resource BasicUdiRelService basicUdiRelService; @Resource GennerOrderUtils gennerOrderUtils; @Resource TraceTaskNoticeService traceTaskNoticeService; @Resource RabbitPushService rabbitPushService; @Resource TraceOrderService traceOrderService; @Resource TraceRecordService traceRecordService; @Resource UdiTraceService udiTraceService; /** * 源头追溯查询 * * @param traceRecordRequest * @return */ @AuthRuleAnnotation("udi/trace/source/search") @GetMapping("udi/trace/source/search") public BaseResponse createTrace(TraceRecordRequest traceRecordRequest) { TraceRecordLogEntity traceRecordLogEntity = null; TraceProductDetailEntity traceProductDetailEntity = null; BasicUdiRelResponse basicUdiRelResponse = null; UdiEntity udiEntity = null; UserCompanyEntity userCompanyEntity = null; if (traceRecordRequest.getTraceType() == Constant.UDI_TRACE) { //1.校验UDI码格式 udiEntity = FilterUdiUtils.getUdi(traceRecordRequest.getUdiCode()); if (udiEntity == null) { return ResultVOUtils.error(500, "UDI码格式错误"); } //2.查询源头企业 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 = traceRecordService.getById(traceRecordRequest.getRecordCode()); if (traceRecordLogEntity == null) traceRecordLogEntity = udiTraceService.createRecordLog(userId, traceRecordRequest.getFromType(), traceRecordRequest.getTraceType()); //5.创建查询详情记录,若存在相同查询记录则直接获取 if (traceRecordLogEntity.getTraceProductIdFk() != null) { traceProductDetailEntity = traceProductDetailService.getById(traceRecordLogEntity.getTraceProductIdFk()); if (traceProductDetailEntity == null) { traceProductDetailEntity = udiTraceService.createTracerProductByUdiCode(traceRecordRequest.getUdiCode(), 1, 1, basicUdiRelResponse, udiEntity); traceRecordLogEntity.setTraceProductIdFk(traceProductDetailEntity.getId()); traceRecordService.updateById(traceRecordLogEntity); } } else { traceProductDetailEntity = udiTraceService.createTracerProductByUdiCode(traceRecordRequest.getUdiCode(), 1, 1, basicUdiRelResponse, udiEntity); } } 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 = 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); } //5.创建查询任务表 TraceTaskNoticeEntity traceTaskNoticeEntity = udiTraceService.createTraceNotice(1, traceProductDetailEntity, basicUdiRelResponse, udiEntity); //6.构建MQ推送消息 MqTraceMessage mqTraceMessage = new MqTraceMessage(); mqTraceMessage.setCreateTime(new Date()); mqTraceMessage.setTaskType(1); mqTraceMessage.setCompanyId(basicUdiRelResponse.getCompanyIdFk()); mqTraceMessage.setCerditNo(userCompanyEntity.getCreditNum()); mqTraceMessage.setTaskId(traceTaskNoticeEntity.getId()); rabbitPushService.send(mqTraceMessage); return ResultVOUtils.success(traceRecordLogEntity); } /** * 现场检查查询 * * @param traceRecordRequest * @return */ @AuthRuleAnnotation("udi/trace/scene/search") @GetMapping("udi/trace/scene/search") public BaseResponse sceneTrace(TraceRecordRequest traceRecordRequest) { if (traceRecordRequest.getTraceType() == Constant.UDI_TRACE) { } else if (traceRecordRequest.getTraceType() == Constant.UDI_BATCHNO) { } else { return ResultVOUtils.error(500, "请选择查询方式!"); } return ResultVOUtils.error(500, "查询失败"); } /** * 客户端根据任务上传出入库单据 * * @param traceOrderUploadRequest * @param bindingResult * @return */ @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()); } TraceOrderEntity traceOrderEntity = new TraceOrderEntity(); BeanUtils.copyProperties(traceOrderUploadRequest, traceOrderEntity); traceOrderEntity.setId(IdUtil.getSnowflakeNextId()); traceOrderEntity.setUpdateTime(new Date()); traceOrderService.save(traceOrderEntity); return ResultVOUtils.success("上传成功"); } /** * 客户端根据任务上传生产关联关系信息 * * @param traceOrderUploadRequest * @param bindingResult * @return */ @AuthRuleAnnotation("udi/trace/manuOrder/upload") @PostMapping("udi/trace/manuOrder/upload") public BaseResponse uploadManuOrder(@RequestBody TraceOrderUploadRequest traceOrderUploadRequest, BindingResult bindingResult) { if (bindingResult.hasErrors()) { return ResultVOUtils.error(ResultEnum.PARAM_VERIFY_FALL, bindingResult.getFieldError().getDefaultMessage()); } TraceOrderEntity traceOrderEntity = new TraceOrderEntity(); BeanUtils.copyProperties(traceOrderUploadRequest, traceOrderEntity); traceOrderEntity.setId(IdUtil.getSnowflakeNextId()); traceOrderEntity.setUpdateTime(new Date()); traceOrderService.save(traceOrderEntity); return ResultVOUtils.success("上传成功"); } }