472 lines
17 KiB
Go
472 lines
17 KiB
Go
package service
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"gorm.io/gorm"
|
|
"hospital-admin-api/config"
|
|
"hospital-admin-api/api/dao"
|
|
"hospital-admin-api/api/model"
|
|
"hospital-admin-api/extend/aliyun"
|
|
"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, // 药品单位
|
|
ActualSellingPrice: o.ActualProductPrice, // 实际商品价格
|
|
}
|
|
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
|
|
|
|
// 终端代码:优先使用订单关联药房的代码,未配置则使用全局配置
|
|
terminalCode := config.C.Pre.PrePlatTerminalCode
|
|
if orderProduct.PharmacyCode != "" {
|
|
terminalCode = orderProduct.PharmacyCode
|
|
}
|
|
|
|
// 处理上传数据-基础数据
|
|
ReportPreRequest := prescription.ReportPreRequest{
|
|
TerminalCode: terminalCode,
|
|
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, err
|
|
}
|
|
|
|
// 修改商品表
|
|
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
|
|
}
|
|
|
|
// 发起订单退款-未完成
|
|
func (r *OrderService) orderRefund(orderNo string, refundReason string) (bool, error) {
|
|
orderDao := dao.OrderDao{}
|
|
|
|
// 获取订单数据
|
|
maps := make(map[string]interface{})
|
|
maps["order_no"] = orderNo
|
|
order, err := orderDao.GetOrder(maps)
|
|
if err != nil || order == nil {
|
|
return false, errors.New("订单数据错误")
|
|
}
|
|
|
|
// 检测订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭 6:退款异常)
|
|
if order.RefundStatus == 2 {
|
|
return false, errors.New("订单退款中")
|
|
}
|
|
|
|
if order.RefundStatus == 3 {
|
|
return false, errors.New("订单已退款成功")
|
|
}
|
|
|
|
if order.RefundStatus == 5 {
|
|
return false, errors.New("订单已退款关闭")
|
|
}
|
|
|
|
// 检测支付状态 (1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款)
|
|
if order.PayStatus != 2 {
|
|
return false, errors.New("订单支付状态错误")
|
|
}
|
|
|
|
return false, nil
|
|
}
|
|
|
|
// PdfToImg 处理pdf转图片
|
|
func (r *OrderService) PdfToImg() (bool, error) {
|
|
//orderPrescription := dao.OrderPrescriptionDao{}
|
|
orderPrescriptionFileDao := dao.OrderPrescriptionFileDao{}
|
|
|
|
// 获取订单数据
|
|
maps := make(map[string]interface{})
|
|
orderPrescriptionFiles, err := orderPrescriptionFileDao.GetOrderPrescriptionFileList(maps)
|
|
if err != nil {
|
|
return false, errors.New("订单数据错误")
|
|
}
|
|
|
|
for _, orderPrescriptionFile := range orderPrescriptionFiles {
|
|
if orderPrescriptionFile.PrescriptionPdfOssPath == "" {
|
|
continue
|
|
}
|
|
|
|
if orderPrescriptionFile.IsConvertedPdf == 1 {
|
|
continue
|
|
}
|
|
|
|
OrderPrescriptionId := fmt.Sprintf("%d", orderPrescriptionFile.OrderPrescriptionId)
|
|
|
|
PrescriptionImgOssPath := strings.TrimLeft(orderPrescriptionFile.PrescriptionImgOssPath, "/")
|
|
|
|
// 下载处方图片到本地
|
|
local, err := aliyun.GetObjectToLocal(PrescriptionImgOssPath,
|
|
"/Users/wucongxing/Desktop/work/go/hospital-admin-api/data/bak/img/"+OrderPrescriptionId+".jpg")
|
|
if err != nil || !local {
|
|
return false, err
|
|
}
|
|
|
|
// 下载处方pdf到本地
|
|
local, err = aliyun.GetObjectToLocal(orderPrescriptionFile.PrescriptionPdfOssPath,
|
|
"/Users/wucongxing/Desktop/work/go/hospital-admin-api/data/bak/pdf/"+OrderPrescriptionId+".pdf")
|
|
if err != nil || !local {
|
|
return false, err
|
|
}
|
|
|
|
pdfPath := "/Users/wucongxing/Desktop/work/go/hospital-admin-api/data/bak/pdf/" + OrderPrescriptionId + ".pdf"
|
|
outputDir := "/Users/wucongxing/Desktop/work/go/hospital-admin-api/data/img"
|
|
|
|
if err := utils.ConvertPDFToImages(pdfPath, outputDir, OrderPrescriptionId+".jpg"); err != nil {
|
|
return false, err
|
|
}
|
|
|
|
// 修改为已转换
|
|
// 开始事务
|
|
tx := global.Db.Begin()
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
tx.Rollback()
|
|
}
|
|
}()
|
|
|
|
data := make(map[string]interface{})
|
|
data["is_converted_pdf"] = 1
|
|
err = orderPrescriptionFileDao.EditOrderPrescriptionFileById(tx, orderPrescriptionFile.PrescriptionFileId, data)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
return false, err
|
|
}
|
|
|
|
tx.Commit()
|
|
}
|
|
|
|
return true, nil
|
|
}
|
|
|
|
// ReturnOrderCoupon 退还用户优惠卷
|
|
func (r *OrderService) ReturnOrderCoupon(orderId int64, tx *gorm.DB) (bool, error) {
|
|
orderCouponDao := dao.OrderCouponDao{}
|
|
userCouponDao := dao.UserCouponDao{}
|
|
|
|
// 获取该订单全部优惠卷数据
|
|
maps := make(map[string]interface{})
|
|
maps["order_id"] = orderId
|
|
orderCoupons, err := orderCouponDao.GetOrderCouponList(maps)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
// 无优惠卷数据
|
|
if len(orderCoupons) == 0 {
|
|
return true, nil
|
|
}
|
|
|
|
for _, coupon := range orderCoupons {
|
|
// 获取用户优惠卷数据
|
|
userCoupon, err := userCouponDao.GetUserCouponById(coupon.UserCouponId)
|
|
if err != nil || userCoupon == nil {
|
|
// 无该优惠卷数据,无需处理
|
|
continue
|
|
}
|
|
|
|
// 恢复优惠卷
|
|
userCouponData := make(map[string]interface{})
|
|
|
|
// 检测优惠卷过期时间。判断是否需要退还
|
|
if userCoupon.ValidEndTime.Before(time.Now()) {
|
|
userCouponData["user_coupon_status"] = 3
|
|
} else {
|
|
userCouponData["user_coupon_status"] = 0
|
|
userCouponData["coupon_use_date"] = nil
|
|
}
|
|
|
|
err = userCouponDao.EditUserCouponById(tx, userCoupon.UserCouponId, userCouponData)
|
|
if err != nil {
|
|
// 恢复优惠卷失败
|
|
return false, errors.New("恢复优惠卷失败")
|
|
}
|
|
}
|
|
|
|
return true, nil
|
|
}
|