2023-12-22 15:42:07 +08:00

313 lines
12 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package service
import (
"errors"
"fmt"
"hospital-admin-api/api/dao"
"hospital-admin-api/api/model"
"hospital-admin-api/extend/prescription"
"hospital-admin-api/global"
"hospital-admin-api/utils"
"strings"
"time"
)
type OrderService struct {
}
// ReportPreProduct 商品订单上报处方平台
func (r *OrderService) ReportPreProduct(orderProductId, adminUserId int64) (bool, error) {
// 获取药品订单详情
orderProductDao := dao.OrderProductDao{}
orderProduct, err := orderProductDao.GetOrderProductPreloadById(orderProductId)
if err != nil || orderProduct == nil {
return false, errors.New("订单数据错误")
}
// 已上报
if orderProduct.ReportPreStatus == 1 {
return false, errors.New("订单已上报")
}
// 检测订单状态
if orderProduct.OrderProductStatus == 1 {
return false, errors.New("订单处于待支付状态,无法取消")
}
if orderProduct.OrderProductStatus == 3 {
return false, errors.New("订单已发货,无法取消")
}
if orderProduct.OrderProductStatus == 4 {
return false, errors.New("订单已签收,无法取消")
}
if orderProduct.OrderProductStatus == 5 {
return false, errors.New("订单已取消,无法取消")
}
// 检测支付状态
if orderProduct.PayStatus != 2 {
return false, errors.New("订单支付状态错误")
}
// 检测订单退款状态
if orderProduct.RefundStatus == 1 {
return false, errors.New("订单申请退款中,无法取消")
}
if orderProduct.RefundStatus == 2 {
return false, errors.New("订单正在退款中,无法取消")
}
if orderProduct.RefundStatus == 3 {
return false, errors.New("订单已退款成功,无法取消")
}
if orderProduct.RefundStatus == 6 {
return false, errors.New("订单退款异常,请联系技术人员")
}
// 获取处方数据
orderPrescriptionDao := dao.OrderPrescriptionDao{}
orderPrescription, _ := orderPrescriptionDao.GetById(orderProduct.OrderPrescriptionId)
if orderPrescription == nil {
return false, errors.New("数据错误,无处方数据")
}
// 获取问诊订单数据
orderInquiryDao := dao.OrderInquiryDao{}
orderInquiry, _ := orderInquiryDao.GetOrderInquiryById(orderProduct.OrderInquiryId)
if orderInquiry == nil {
return false, errors.New("数据错误,无问诊订单数据")
}
// 获取问诊病例数据
orderInquiryCaseDao := dao.OrderInquiryCaseDao{}
orderInquiryCase, _ := orderInquiryCaseDao.GetOrderInquiryCaseByOrderInquiryId(orderProduct.OrderInquiryId)
if orderInquiryCase == nil {
return false, errors.New("数据错误,无问诊病例")
}
// 获取商品订单列表数据
orderProductItemDao := dao.OrderProductItemDao{}
orderProductItems, err := orderProductItemDao.GetOrderProductItemByOrderProductId(orderProductId)
if err != nil || len(orderProductItems) == 0 {
return false, errors.New("数据错误,无药品数据")
}
// 获取就诊患者用户数据
userDao := dao.UserDao{}
user, err := userDao.GetUserById(orderInquiry.UserId)
if err != nil && user == nil {
return false, errors.New("就诊患者数据错误")
}
// 获取家庭成员-基本信息
patientFamilyDao := dao.PatientFamilyDao{}
patientFamily, _ := patientFamilyDao.GetPatientFamilyById(orderInquiry.FamilyId)
if patientFamily == nil {
return false, errors.New("数据错误,无家庭成员数据")
}
// 获取处方关联疾病数据
orderPrescriptionIcdDao := dao.OrderPrescriptionIcdDao{}
orderPrescriptionIcds, _ := orderPrescriptionIcdDao.GetOrderPrescriptionIcdListByOrderPrescriptionId(orderPrescription.OrderPrescriptionId)
if err != nil || len(orderPrescriptionIcds) == 0 {
return false, errors.New("数据错误,无处方关联疾病数据")
}
// 获取医生数据
userDoctorDao := dao.UserDoctorDao{}
userDoctor, _ := userDoctorDao.GetUserDoctorById(orderPrescription.DoctorId)
if userDoctor == nil {
return false, errors.New("数据错误,无医生数据")
}
// 获取医生详情数据
userDoctorInfoDao := dao.UserDoctorInfoDao{}
userDoctorInfo, _ := userDoctorInfoDao.GetUserDoctorInfoByDoctorId(orderPrescription.DoctorId)
if userDoctorInfo == nil {
return false, errors.New("数据错误,无医生详情数据")
}
// 获取药师数据
userPharmacistDao := dao.UserPharmacistDao{}
userPharmacist, _ := userPharmacistDao.GetUserPharmacistById(orderPrescription.PharmacistId)
if userPharmacist == nil {
return false, errors.New("数据错误,无药师数据")
}
// 获取药师详情数据
userPharmacistInfoDao := dao.UserPharmacistInfoDao{}
userPharmacistInfo, _ := userPharmacistInfoDao.GetUserPharmacistInfoByPharmacistId(orderPrescription.PharmacistId)
if userPharmacistInfo == nil {
return false, errors.New("数据错误,无药师详情数据")
}
// 获取医生科室数据
hospitalDepartmentCustomDao := dao.HospitalDepartmentCustomDao{}
hospitalDepartmentCustom, _ := hospitalDepartmentCustomDao.GetHospitalDepartmentCustomById(userDoctor.DepartmentCustomId)
if hospitalDepartmentCustom == nil {
return false, errors.New("数据错误,无医生科室数据")
}
// 处理疾病数据
icdNameSlice := make([]string, 0)
for _, orderPrescriptionIcd := range orderPrescriptionIcds {
if orderPrescriptionIcd.IcdName != "" {
icdNameSlice = append(icdNameSlice, orderPrescriptionIcd.IcdName)
}
}
icdNameStr := strings.Join(icdNameSlice, ";")
// 处理上传数据-药品数据
drugListRequests := make([]prescription.DrugListRequest, len(orderProductItems))
orderDrugListRequests := make([]prescription.OrderDrugListRequest, len(orderProductItems))
for i, o := range orderProductItems {
// 获取商品数据
productDao := dao.ProductDao{}
product, _ := productDao.GetProductById(o.ProductId)
if product == nil {
return false, errors.New("数据错误,无药品数据")
}
useDays := o.Amount * 7
if product.AvailableDays != 0 {
useDays = int(product.AvailableDays)
}
// 药品数据1
drugRequest := prescription.DrugListRequest{
DrugCode: product.ProductPlatformCode, // 药品编码
ApprovalNumber: product.LicenseNumber, // 批准文号
DrugName: product.ProductName, // 药品名称
Specifications: product.ProductSpec, // 药品规格
Price: product.ProductPrice, // 药品单价
PackingCount: o.Amount, // 药品数量
SurplusPackingCount: 0, // 处方药品剩余使用数量
PackingUnit: product.PackagingUnit, // 药品单位
SingleDosage: 1, // 单次用量
SingleDosageUnit: "片", // 单次用量单位
UseName: product.SingleUse, // 用法名称
FrequencyName: product.FrequencyUse, // 频次名称
UseDays: useDays, // 使用天数
}
drugListRequests[i] = drugRequest
// 药品数据2
orderDrugRequest := prescription.OrderDrugListRequest{
DrugCode: product.ProductPlatformCode, // 药品编码
ApprovalNumber: product.LicenseNumber, // 批准文号
DrugName: product.ProductName, // 药品名称
Specifications: product.ProductSpec, // 药品规格
Price: product.ProductPrice, // 药品单价
DrugCount: o.Amount, // 药品数量
PackingUnit: product.PackagingUnit, // 药品单位
}
orderDrugListRequests[i] = orderDrugRequest
}
// 处理上传数据-处方数据
presRequests := make([]prescription.PresRequest, 1)
presRequest := prescription.PresRequest{
PrescriptionNo: orderPrescription.PrescriptionCode, // // 处方编号
PrescriptionSubType: 1, // 处方类型 0无类型 1普 通处方 2儿科处 方
PatientName: orderPrescription.PatientName, // 就诊人姓名
PatientPhone: user.Mobile, // 就诊人联系方式
IdCard: patientFamily.IdNumber, // 身份证号
Advice: orderPrescription.DoctorAdvice, // 医嘱
DiagnosisName: icdNameStr, // 诊断
ThirdDoctorName: userDoctor.UserName, // 开方医生姓名
ThirdDeptName: hospitalDepartmentCustom.DepartmentName, // 开方科室名称
ThirdDoctorNameImg: utils.AddOssDomain(userDoctorInfo.SignImage),
PrescriptionTime: time.Time(orderPrescription.DoctorCreatedTime).Format("2006-01-02 15:04:05"),
ThirdFirstPharmacist: userPharmacist.UserName,
ThirdFirstPharmacistImg: utils.AddOssDomain(userPharmacistInfo.SignImage),
ThirdFirstTime: time.Time(orderPrescription.PharmacistVerifyTime).Format("2006-01-02 15:04:05"),
ThirdLastPharmacist: userPharmacist.UserName,
ThirdLastPharmacistImg: utils.AddOssDomain(userPharmacistInfo.SignImage),
ThirdLastTime: time.Time(orderPrescription.PharmacistVerifyTime).Format("2006-01-02 15:04:05"),
ThirdSignImg: utils.AddOssDomain("/basic/file/hospital_signature.png"),
ReferenceCharge: orderProduct.AmountTotal,
ChiefComplaint: orderInquiryCase.DiseaseDesc,
HistoryPresent: orderInquiryCase.DiseaseClassName,
PastHistory: orderInquiryCase.FamilyHistory,
PhysicalExamination: "无",
SupplementaryExamination: "无",
AllergicHistory: orderInquiryCase.AllergyHistory,
DrugList: drugListRequests,
OrderDrugList: orderDrugListRequests,
}
presRequests[0] = presRequest
// 处理上传数据-基础数据
ReportPreRequest := prescription.ReportPreRequest{
TerminalCode: "ZD-10003",
OrderNo: orderProduct.OrderProductNo, // 订单编号
TransactNo: orderProduct.EscrowTradeNo, // 流水单号
PayDate: time.Time(orderProduct.PayTime).Format("2006-01-02 15:04:05"), // 支付时间
Money: orderProduct.PaymentAmountTotal, // 订单金额
Freight: orderProduct.LogisticsFee, // 运费(单位:元)
TakeTypeCode: 2, // 取货方式 1 自提 2 快递,目前只支持快递,传固定值 2
BuyerName: orderProduct.ConsigneeName, // 收货人姓名
BuyerPhone: orderProduct.ConsigneeTel, // 收货人联系方式
BuyerAddress: orderProduct.Address, // 收货人地址
ProvinceCode: fmt.Sprintf("%d", orderProduct.ProvinceId), // 收货地址(省) 编码
ProvinceName: orderProduct.Province, // 收货地址(省) 名称
CityCode: fmt.Sprintf("%d", orderProduct.CityId), // 收货地址(市) 编码
CityName: orderProduct.City, // 收货地址(市) 名称
DistrictCode: fmt.Sprintf("%d", orderProduct.CountyId), // 收货地址(区 县)编码
DistrictName: orderProduct.County, // 收货地址(区 县)名称
PresList: presRequests,
}
// 开始事务
tx := global.Db.Begin()
defer func() {
if r := recover(); r != nil {
tx.Rollback()
}
}()
// 上报处方
_, err = ReportPreRequest.ReportPre()
if err != nil {
tx.Rollback()
return false, errors.New("上报处方失败1")
}
// 修改商品表
orderProductData := make(map[string]interface{})
orderProductData["report_pre_status"] = 1
orderProductData["report_pre_time"] = time.Now().Format("2006-01-02 15:04:05")
err = orderProductDao.EditOrderProductById(tx, orderProductId, orderProductData)
if err != nil {
tx.Rollback()
return false, errors.New("上报处方失败")
}
// 记录日志
orderOperationLog := &model.OrderOperationLog{
OrderId: orderProduct.OrderProductId,
OrderNo: orderProduct.OrderProductNo,
OrderType: 2,
OperatorId: adminUserId,
OperationContent: "上报处方平台",
}
orderOperationLogDao := dao.OrderOperationLogDao{}
orderOperationLog, err = orderOperationLogDao.AddOrderOperationLog(tx, orderOperationLog)
if err != nil || orderOperationLog == nil {
tx.Rollback()
return false, errors.New(err.Error())
}
tx.Commit()
return true, nil
}