You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
udi-wms-third-java/src/main/java/com/glxp/mipsdl/client/ptxhyy/PtxhyyClient.java

380 lines
19 KiB
Java

package com.glxp.mipsdl.client.ptxhyy;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.glxp.mipsdl.client.CommonHttpClient;
import com.glxp.mipsdl.client.ptxhyy.entity.OrderUploadResponse;
import com.glxp.mipsdl.config.ThirdSysConfig;
import com.glxp.mipsdl.constant.ConstantType;
import com.glxp.mipsdl.constant.Constants;
import com.glxp.mipsdl.dao.auth.AuthUserDao;
import com.glxp.mipsdl.dao.auth.AuthWarehouseDao;
import com.glxp.mipsdl.dao.basic.BasicBussinessTypeDao;
import com.glxp.mipsdl.dao.basic.BasicCorpDao;
import com.glxp.mipsdl.dao.inout.IoCodeDao;
import com.glxp.mipsdl.dao.inout.IoOrderDao;
import com.glxp.mipsdl.dao.inout.IoOrderDetailBizDao;
import com.glxp.mipsdl.dao.inout.IoOrderUploadLogDao;
import com.glxp.mipsdl.dao.thrsys.ThrInvWarehouseDao;
import com.glxp.mipsdl.dao.thrsys.ThrProductsDao;
import com.glxp.mipsdl.dao.thrsys.ThrSystemBusApiDao;
import com.glxp.mipsdl.entity.auth.AuthUserEntity;
import com.glxp.mipsdl.entity.auth.AuthWarehouseEntity;
import com.glxp.mipsdl.entity.auth.AuthWarehouseUser;
import com.glxp.mipsdl.dao.auth.AuthWarehouseUserMapper;
import com.glxp.mipsdl.entity.basic.BasicBussinessTypeEntity;
import com.glxp.mipsdl.entity.basic.BasicCorpEntity;
import com.glxp.mipsdl.entity.inout.*;
import com.glxp.mipsdl.entity.thrsys.ThrInvWarehouseEntity;
import com.glxp.mipsdl.entity.thrsys.ThrSystemBusApiEntity;
import com.glxp.mipsdl.http.HttpClient;
import com.glxp.mipsdl.req.base.UdiwmsOrderDetailRequest;
import com.glxp.mipsdl.req.base.UdiwmsOrderRequest;
import com.glxp.mipsdl.req.ptxhyy.PtxhyyOrderDetailRequest;
import com.glxp.mipsdl.req.ptxhyy.PtxhyyOrderRequest;
import com.glxp.mipsdl.res.BaseResponse;
import com.glxp.mipsdl.service.auth.AuthWarehouseService;
import com.glxp.mipsdl.service.thrsys.ThrCorpService;
import com.glxp.mipsdl.service.thrsys.ThrSystemDetailService;
import com.glxp.mipsdl.util.DateUtil;
import com.glxp.mipsdl.util.ResultVOUtils;
import com.glxp.mipsdl.util.SubmitOrderUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
/**
*
*/
@Slf4j
@Service(value = "100008HttpClient")
@RequiredArgsConstructor
public class PtxhyyClient extends CommonHttpClient {
private final ThirdSysConfig thirdSysConfig;
private final IoOrderDao orderDao;
private final ThrSystemBusApiDao thrSystemBusApiDao;
private final BasicBussinessTypeDao basicBussinessTypeDao;
private final BasicCorpDao basicCorpDao;
private final AuthWarehouseDao authWarehouseDao;
private final ThrInvWarehouseDao thrInvWarehouseDao;
private final AuthWarehouseService authWarehouseService;
private final ThrCorpService thrCorpService;
private final IoCodeDao codeDao;
private final IoOrderDetailBizDao orderDetailBizDao;
private final SubmitOrderUtil submitOrderUtil;
private final ThrProductsDao thrProductsDao;
@Override
public BaseResponse submitOrders(UdiwmsOrderRequest udiwmsOrderRequest) {
if (StrUtil.isBlank(udiwmsOrderRequest.getBillNo())) {
return ResultVOUtils.error(500, "单据号不能为空");
}
//查询单据和详情信息,组装第三方系统接口参数
IoOrderEntity orderEntity = orderDao.selectOne(new LambdaQueryWrapper<IoOrderEntity>().eq(IoOrderEntity::getBillNo, udiwmsOrderRequest.getBillNo()));
udiwmsOrderRequest.setBillDate(DateUtil.formatDateTime(orderEntity.getCreateTime()));
//查询对应的第三方单据类型
ThrSystemBusApiEntity thrSystemBusApiEntity = thrSystemBusApiDao.selectOne(new LambdaQueryWrapper<ThrSystemBusApiEntity>().eq(ThrSystemBusApiEntity::getCode, udiwmsOrderRequest.getBillType()));
if (StrUtil.isBlank(thrSystemBusApiEntity.getUrl())) {
return ResultVOUtils.error(500, "接口未配置,无法提交!");
}
udiwmsOrderRequest.setBillType(thrSystemBusApiEntity.getThirdBuyCode());
//查询单据类型,设置往来单位信息
BasicBussinessTypeEntity basicBussinessTypeEntity = basicBussinessTypeDao.selectByAction(orderEntity.getAction());
if (basicBussinessTypeEntity.getCorpType().equals(Constants.CORP_HOSPTIAL)) {
//医院客户
udiwmsOrderRequest.setCorpId(orderEntity.getFromCorp());
//查询客户名称
String corpName = basicCorpDao.selectNameByErpId(orderEntity.getFromCorp());
udiwmsOrderRequest.setCorpName(corpName);
} else if (basicBussinessTypeEntity.getCorpType().equals(Constants.CORP_INTTERNAL)) {
if (StrUtil.isNotEmpty(orderEntity.getFromCorp())) {
BasicCorpEntity basicCorpEntity = basicCorpDao.selectByErpId(orderEntity.getFromCorp());
if (null != basicCorpEntity) {
udiwmsOrderRequest.setCorpId(basicCorpEntity.getThirdId());
udiwmsOrderRequest.setCorpName(basicCorpEntity.getName());
} else {
String msg = "查询单据对应的第三方往来单位信息异常";
log.error(msg);
return ResultVOUtils.error(500, msg);
}
} else {
//内部科室
AuthWarehouseEntity warehouseEntity = authWarehouseDao.selectOne(new LambdaQueryWrapper<AuthWarehouseEntity>().eq(AuthWarehouseEntity::getCode, udiwmsOrderRequest.getCorpId()));
try {
//根据第三方系统标识,获取对应字段的值
ThrInvWarehouseEntity thrInvWarehouseEntity = thrInvWarehouseDao.selectOne(new LambdaQueryWrapper<ThrInvWarehouseEntity>()
.eq(ThrInvWarehouseEntity::getCode, authWarehouseService.getThirdInvCode(warehouseEntity))
.eq(ThrInvWarehouseEntity::getThirdSysFk, thirdSysConfig.getThirdId()));
udiwmsOrderRequest.setCorpId(thrInvWarehouseEntity.getCode());
udiwmsOrderRequest.setCorpName(thrInvWarehouseEntity.getName());
} catch (Exception e) {
String msg = "查询单据对应往来单位的第三方仓库信息异常";
log.error(msg, e);
return ResultVOUtils.error(500, msg);
}
}
} else if (basicBussinessTypeEntity.getCorpType().equals(Constants.CORP_SP)) {
//供应商
BasicCorpEntity basicCorpEntity = basicCorpDao.selectByErpId(orderEntity.getFromCorp());
if (null != basicCorpEntity) {
udiwmsOrderRequest.setCorpId(basicCorpEntity.getThirdId());
udiwmsOrderRequest.setCorpName(basicCorpEntity.getName());
} else {
String msg = "查询单据对应的第三方往来单位信息异常";
log.error(msg);
return ResultVOUtils.error(500, msg);
}
}
//查询仓库信息
AuthWarehouseEntity warehouseEntity = authWarehouseDao.selectOne(new LambdaQueryWrapper<AuthWarehouseEntity>().eq(AuthWarehouseEntity::getCode, orderEntity.getInvCode()));
try {
List<ThrInvWarehouseEntity> thrInvWarehouseEntitys = thrInvWarehouseDao.selectList(new LambdaQueryWrapper<ThrInvWarehouseEntity>()
.eq(ThrInvWarehouseEntity::getCode, authWarehouseService.getThirdInvCode(warehouseEntity))
.eq(ThrInvWarehouseEntity::getThirdSysFk, thirdSysConfig.getThirdId()));
if (CollUtil.isNotEmpty(thrInvWarehouseEntitys)) {
udiwmsOrderRequest.setLocInvCode(thrInvWarehouseEntitys.get(0).getCode());
udiwmsOrderRequest.setLocInvName(thrInvWarehouseEntitys.get(0).getName());
}
} catch (Exception e) {
log.info("查询第三方系统仓库信息异常", ExceptionUtil.stacktraceToString(e));
return ResultVOUtils.error(500, "查询第三方系统仓库信息异常");
}
udiwmsOrderRequest.setBillFlag("1");
//设置单据出入库类型
if (basicBussinessTypeEntity.getMainAction().equals(ConstantType.TYPE_PUT)) {
udiwmsOrderRequest.setInoutType(1); //入库
} else if (basicBussinessTypeEntity.getMainAction().equals(ConstantType.TYPE_OUT)) {
udiwmsOrderRequest.setInoutType(2); //出库
}
udiwmsOrderRequest.setRemark("udi管理系统提交单据:" + orderEntity.getBillNo());
//组装单据明细数据
List<IoCodeEntity> ioCodeEntities = codeDao.selectList(new LambdaQueryWrapper<IoCodeEntity>()
.eq(IoCodeEntity::getOrderId, orderEntity.getBillNo())
.ne(IoCodeEntity::getAction, "StockCheck")
);
List<UdiwmsOrderDetailRequest> items = new ArrayList<>();
//查询业务单据详情
List<IoOrderDetailBizEntity> detailBizEntities = orderDetailBizDao.selectList(new LambdaQueryWrapper<IoOrderDetailBizEntity>().eq(IoOrderDetailBizEntity::getOrderIdFk, orderEntity.getBillNo()));
//单据金额
BigDecimal djje = detailBizEntities.stream().map(IoOrderDetailBizEntity::getPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
List<IoOrderInvoiceEntity> ioOrderInvoiceEntities = IoOrderInvoiceEntity.builder().build().selectList(Wrappers.lambdaQuery(IoOrderInvoiceEntity.class).eq(IoOrderInvoiceEntity::getOrderIdFk, orderEntity.getBillNo()));
int fbbz = 0;
if (CollectionUtil.isNotEmpty(ioOrderInvoiceEntities) && ioOrderInvoiceEntities.size() > 0) {
fbbz = 1;
}
String userName;
try {
userName = getUserName(orderEntity);
if (StrUtil.isEmpty(userName)) {
return ResultVOUtils.error(500, "该单据无创建人!");
}
} catch (Exception e) {
log.info("查询操作人异常", ExceptionUtil.stacktraceToString(e));
userName = getUserName(orderEntity);
}
PtxhyyOrderRequest ptxhyyOrderRequest = PtxhyyOrderRequest
.builder()
.kfxh(udiwmsOrderRequest.getLocInvCode())
.lzdh(udiwmsOrderRequest.getBillNo())
.czgh(userName)
.lzfs(udiwmsOrderRequest.getBillType())
.dwxh(udiwmsOrderRequest.getCorpId())
.rkrq(udiwmsOrderRequest.getBillDate())
.djje(djje)
.fpbz(fbbz)
.zdrq(udiwmsOrderRequest.getBillDate())
.lsje(djje)
.fpzs(CollectionUtil.isNotEmpty(ioOrderInvoiceEntities) ? ioOrderInvoiceEntities.size() : 0)
.udixh(udiwmsOrderRequest.getBillNo())
.build();
ptxhyyOrderRequest.setDetailList(new ArrayList<>());
String errMs = setOrderDetailList(ioCodeEntities, items, detailBizEntities);
if (errMs != null) {
return ResultVOUtils.error(500, errMs);
}
Collections.reverse(items);
for (UdiwmsOrderDetailRequest i : items) {
String udim = "";
if (CollectionUtil.isNotEmpty(i.getCodeList())) {
udim = i.getCodeList().stream().map(UdiwmsOrderDetailRequest.CodeDetailEntity::getCode).collect(Collectors.joining(","));
}
String fphm = "";
String fprq = null;
if (CollectionUtil.isNotEmpty(ioOrderInvoiceEntities)) {
List<IoOrderInvoiceEntity> invoiceList = ioOrderInvoiceEntities.stream().filter(s -> i.getBindRlFk().equals(s.getBindRlFk()) && i.getBatchNo().equals(s.getBatchNo()))
.sorted(Comparator.comparing(IoOrderInvoiceEntity::getInvoiceDate, Comparator.reverseOrder())).collect(Collectors.toList());
if (CollectionUtil.isNotEmpty(invoiceList)) {
fphm = invoiceList.stream().map(IoOrderInvoiceEntity::getInvoiceCode).collect(Collectors.joining(","));
if (invoiceList.get(0).getInvoiceDate() != null)
fprq = DateUtil.formatDateTime(invoiceList.get(0).getInvoiceDate());
ioOrderInvoiceEntities.removeAll(invoiceList);
}
}
BigDecimal wzje = i.getPrice().multiply(BigDecimal.valueOf(i.getCount().intValue()));
PtxhyyOrderDetailRequest detailRequest = PtxhyyOrderDetailRequest.builder()
.wzxh(i.getProductId())
.cjxh(i.getManufactoryCode())
.bzdw(i.getMeasname())
.bzsl(i.getCount())
.wzjg(i.getPrice())
.lsjg(i.getPrice() + "")
.lsje(wzje)
.wzje(wzje + "")
.udim(udim)
.hsfl(i.getCplb())
.wzph(i.getBatchNo())
.fphm(fphm)
.fprq(fprq)
.djrq(DateUtil.formatDate(orderEntity.getCreateTime(), "yyyy-MM-dd"))
.producedDate(i.getProductDate())
.expireDate(i.getExpireDate())
.registerNo(i.getRegisterNo())
.build();
ptxhyyOrderRequest.getDetailList().add(detailRequest);
}
return submitOrder(orderEntity, ptxhyyOrderRequest);
}
/**
*
*
* @param ioCodeEntities
* @param orderDetailRequests
* @param detailBizEntities
*/
private String setOrderDetailList(List<IoCodeEntity> ioCodeEntities, List<UdiwmsOrderDetailRequest> orderDetailRequests, List<IoOrderDetailBizEntity> detailBizEntities) {
for (IoOrderDetailBizEntity detailBizEntity : detailBizEntities) {
UdiwmsOrderDetailRequest item = submitOrderUtil.buildProductInfo(detailBizEntity);
if (item == null) {
return detailBizEntity.getNameCode() + "第三方产品编码未关联";
}
//封装条码信息
submitOrderUtil.setCodeList(ioCodeEntities, detailBizEntity, item);
orderDetailRequests.add(item);
}
return null;
}
@Resource
AuthUserDao authUserDao;
@Resource
AuthWarehouseUserMapper authWarehouseUserMapper;
@Resource
private HttpClient httpClient;
@Resource
private ThrSystemDetailService thrSystemDetailService;
@Resource
private IoOrderUploadLogDao orderUploadLogDao;
public String getUserName(IoOrderEntity orderEntity) {
//创建人非供应商
if (StrUtil.isNotEmpty(orderEntity.getCheckUser())) {
AuthUserEntity authUserEntity = authUserDao.selectById(orderEntity.getReviewUser());
return authUserEntity.getUserName();
} else if (StrUtil.isNotEmpty(orderEntity.getReviewUser())) {
AuthUserEntity authUserEntity = authUserDao.selectById(orderEntity.getReviewUser());
return authUserEntity.getUserName();
} else if (StrUtil.isNotEmpty(orderEntity.getCreateUser()) && orderEntity.getCreateUser().length() != 14) {
AuthUserEntity authUserEntity = authUserDao.selectById(orderEntity.getCreateUser());
return authUserEntity.getUserName();
} else if (StrUtil.isNotEmpty(orderEntity.getUpdateUser()) && orderEntity.getUpdateUser().length() != 14) {
AuthUserEntity authUserEntity = authUserDao.selectById(orderEntity.getUpdateUser());
return authUserEntity.getUserName();
} else {
List<AuthWarehouseUser> authWarehouseUsers = authWarehouseUserMapper.selectList(new QueryWrapper<AuthWarehouseUser>().eq("code", orderEntity.getInvCode()));
if (CollUtil.isNotEmpty(authWarehouseUsers)) {
return authWarehouseUsers.get(0).getUserName();
}
}
return null;
}
public String getDefaultUserName(IoOrderEntity orderEntity) {
List<AuthWarehouseUser> authWarehouseUsers = authWarehouseUserMapper.selectList(new QueryWrapper<AuthWarehouseUser>().eq("code", orderEntity.getInvCode()));
if (CollUtil.isNotEmpty(authWarehouseUsers)) {
return authWarehouseUsers.get(0).getUserName();
}
return null;
}
public BaseResponse submitOrder(IoOrderEntity orderEntity, Object udiwmsOrderRequest) {
//记录日志
IoOrderUploadLogEntity uploadLog = new IoOrderUploadLogEntity();
uploadLog.setBillDate(orderEntity.getCreateTime());
uploadLog.setBillType(orderEntity.getAction());
uploadLog.setSubmitTime(new Date());
uploadLog.setBillNo(orderEntity.getBillNo());
uploadLog.setFromType(Constants.THIRD_ORDER_UPLOAD);
String response = httpClient.postJson(thrSystemDetailService.getUrl(Constants.URL_NAME_ORDER_SUBMIT_URL), udiwmsOrderRequest);
if (StrUtil.isNotBlank(response)) {
try {
BaseResponse<OrderUploadResponse> baseResponse =
JSONObject.parseObject(response, new TypeReference<BaseResponse<OrderUploadResponse>>() {
});
if (baseResponse.getCode() == 20000) {
String thirdBillNo = baseResponse.getData().getDjxh();
orderEntity.setThirdBillNo(thirdBillNo);
orderEntity.setExportStatus(1); //上传成功
uploadLog.setStatus(2);
uploadLog.setThrBillNo(thirdBillNo);
uploadLog.setResult("提交成功");
orderDao.updateById(orderEntity);
orderUploadLogDao.insert(uploadLog);
return ResultVOUtils.success("提交成功");
}
orderEntity.setExportStatus(2); //上传失败
uploadLog.setStatus(3);
uploadLog.setResult("提交失败,错误信息:" + baseResponse.getMessage());
orderUploadLogDao.insert(uploadLog);
return baseResponse;
} catch (Exception e) {
log.error("解析提交单据返回结果异常,结果信息:{}", response);
log.error("异常信息:{}", e);
uploadLog.setStatus(3);
uploadLog.setResult("提交失败,错误信息:" + response);
orderEntity.setExportStatus(2); //上传失败
orderDao.updateById(orderEntity);
orderUploadLogDao.insert(uploadLog);
return ResultVOUtils.error(500, "提交失败");
}
} else {
uploadLog.setStatus(3);
uploadLog.setResult("提交失败,调用接口返回为空");
orderEntity.setExportStatus(2); //上传失败
orderDao.updateById(orderEntity);
orderUploadLogDao.insert(uploadLog);
return ResultVOUtils.error(500, "提交失败");
}
}
}