255 lines
8.5 KiB
Go
255 lines
8.5 KiB
Go
package service
|
|
|
|
import (
|
|
"errors"
|
|
"hospital-admin-api/api/dao"
|
|
"hospital-admin-api/api/dto"
|
|
"hospital-admin-api/api/model"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
type OrderServicePackageService struct {
|
|
}
|
|
|
|
// GetOrderServicePackage 服务包订单详情
|
|
func (r *OrderServicePackageService) GetOrderServicePackage(orderServiceId int64) (g *dto.OrderServicePackageDto, err error) {
|
|
// 获取订单数据
|
|
orderServicePackageDao := dao.OrderServicePackageDao{}
|
|
orderServicePackage, err := orderServicePackageDao.GetOrderServicePackageById(orderServiceId)
|
|
if err != nil || orderServicePackage == nil {
|
|
return nil, errors.New(err.Error())
|
|
}
|
|
|
|
// 获取病例
|
|
orderServicePackageCaseDao := dao.OrderServicePackageCaseDao{}
|
|
orderServicePackageCase, err := orderServicePackageCaseDao.GetOrderServicePackageCaseByOrderServicePackageId(orderServiceId)
|
|
if orderServicePackageCase == nil {
|
|
return nil, errors.New("数据错误,无问诊病例")
|
|
}
|
|
|
|
// 获取退款数据
|
|
orderServicePackageRefundDao := dao.OrderServicePackageRefundDao{}
|
|
orderServicePackageRefund, err := orderServicePackageRefundDao.GetOrderServicePackageRefundByOrderServicePackageId(orderServiceId)
|
|
|
|
// 医生数据
|
|
userDoctorService := UserDoctorService{}
|
|
userDoctor, err := userDoctorService.GetUserDoctorById(orderServicePackage.DoctorId)
|
|
if err != nil {
|
|
return nil, errors.New(err.Error())
|
|
}
|
|
|
|
// 处理返回值
|
|
g = dto.GetOrderServicePackageDto(orderServicePackage)
|
|
|
|
// 加载医生数据
|
|
g.UserDoctor = userDoctor
|
|
|
|
// 加载订单退款数据
|
|
g.LoadOrderServicePackageRefund(orderServicePackageRefund)
|
|
|
|
// 加载问诊病例
|
|
g.LoadMaskOrderServicePackageCase(orderServicePackageCase)
|
|
|
|
return g, nil
|
|
}
|
|
|
|
// GetOrderServicePackageDetailInfo 获取服务包订单服务权益详情
|
|
func (r *OrderServicePackageService) GetOrderServicePackageDetailInfo(orderServiceId int64) (g *dto.OrderServicePackageDetailInfoDto, err error) {
|
|
// 获取订单数据
|
|
orderServicePackageDao := dao.OrderServicePackageDao{}
|
|
orderServicePackage, err := orderServicePackageDao.GetOrderServicePackageById(orderServiceId)
|
|
if err != nil || orderServicePackage == nil {
|
|
return nil, errors.New(err.Error())
|
|
}
|
|
|
|
// 获取关联问诊订单
|
|
orderServicePackageInquiryDao := dao.OrderServicePackageInquiryDao{}
|
|
orderServicePackageInquiry, err := orderServicePackageInquiryDao.GetOrderServicePackageInquiryPreloadByOrderNo(orderServicePackage.OrderServiceNo)
|
|
|
|
// 获取订单详情数据
|
|
orderServicePackageDDao := dao.OrderServicePackageDetailDao{}
|
|
orderServicePackageDetail, err := orderServicePackageDDao.GetOrderServicePackageDetailByOrderServicePackageId(orderServiceId)
|
|
if err != nil || orderServicePackageDetail == nil {
|
|
return nil, errors.New(err.Error())
|
|
}
|
|
|
|
// 处理返回值
|
|
g = dto.GetOrderServicePackageDetailInfoDto(orderServicePackage)
|
|
|
|
// 加载关联问诊订单数据
|
|
if len(orderServicePackageInquiry) > 0 {
|
|
g.LoadOrderServicePackageInquiry(orderServicePackageInquiry)
|
|
}
|
|
|
|
// 加载服务包订单详情数据
|
|
g.LoadOrderServicePackageDetail(orderServicePackageDetail)
|
|
|
|
// 获取服务包当前月时间区间
|
|
CurrentMonthStartDate, CurrentMonthFinishDate, err := r.getOrderServicePackageCurrentMonthDate(g.StartTime)
|
|
g.CurrentMonthStartDate = CurrentMonthStartDate.Format("2006年01月02日 15时04分")
|
|
g.CurrentMonthFinishDate = CurrentMonthFinishDate.Format("2006年01月02日 15时04分")
|
|
|
|
// 获取服务包当月已问诊次数
|
|
g.MonthInquiryCount, err = r.GetCurrentMonthInquiryCount(g.OrderServiceType, orderServicePackage.UserId, orderServicePackage.DoctorId, CurrentMonthStartDate, CurrentMonthFinishDate)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// 获取服务包当月剩余问诊次数
|
|
g.RemainingInquiryCount = r.GetRemainingInquiryCount(orderServicePackageDetail.MonthlyFrequency, g.MonthInquiryCount)
|
|
|
|
// 健康包数据
|
|
if orderServicePackage.OrderServiceType == 1 {
|
|
if g.OrderServiceStatus == 3 || g.OrderServiceStatus == 4 || g.OrderServiceStatus == 5 {
|
|
// 获取用户某一类型优惠卷
|
|
userCouponDao := dao.UserCouponDao{}
|
|
userCoupon, _ := userCouponDao.GetUserAllObjectTypeCoupon(orderServicePackage.UserId, 7)
|
|
|
|
// 加载优惠卷数据
|
|
g.LoadUserCoupon(userCoupon)
|
|
|
|
// 获取健康包药品订单数据-周期内所有药品订单
|
|
orderServicePackageProductDao := dao.OrderServicePackageProductDao{}
|
|
orderServicePackageProduct, _ := orderServicePackageProductDao.GetOrderServicePackageProductPreloadByOrderServiceId(
|
|
orderServicePackage.OrderServiceId)
|
|
|
|
// 加载关联药品订单数据
|
|
g.LoadOrderServicePackageProduct(orderServicePackageProduct)
|
|
|
|
// 获取服务包内所有药品
|
|
healthPackageProductDao := dao.HealthPackageProductDao{}
|
|
healthPackageProducts, err := healthPackageProductDao.GetHealthPackageProductByPackageId(orderServicePackageDetail.PackageId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for _, healthPackageProduct := range healthPackageProducts {
|
|
// 获取服务包内某一药品的剩余数量
|
|
remainingQuantity, err := r.GetOrderServiceProductCanUseQuantity(orderServicePackage.OrderServiceId, healthPackageProduct.ProductId, healthPackageProduct.Quantity)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
g.OrderServicePackageDetail.LoadRemainingQuantity(remainingQuantity)
|
|
}
|
|
}
|
|
}
|
|
|
|
return g, nil
|
|
}
|
|
|
|
// 获取服务包当前月时间区间
|
|
func (r *OrderServicePackageService) getOrderServicePackageCurrentMonthDate(startTime model.LocalTime) (startDate, finishDate time.Time, err error) {
|
|
if startTime.IsEmpty() {
|
|
return time.Time{}, time.Time{}, err
|
|
}
|
|
|
|
// 获取当前时间
|
|
now := time.Now()
|
|
|
|
// 获取开启服务日期和今日的相差天数
|
|
diff := now.Sub(time.Time(startTime)).Hours() / 24
|
|
diffDays := int(diff)
|
|
|
|
// 获取当前月次
|
|
monthTime := (diffDays + 29) / 30 // 向上取整
|
|
|
|
days := monthTime * 30
|
|
|
|
// 当前所属月结束时间
|
|
currentMonthFinishDate := time.Time(startTime).AddDate(0, 0, days)
|
|
|
|
// 当前所属月开始时间
|
|
currentMonthStartDate := currentMonthFinishDate.AddDate(0, 0, -30)
|
|
|
|
return currentMonthStartDate, currentMonthFinishDate, nil
|
|
}
|
|
|
|
// 检测问诊是否服务包首次问诊
|
|
func (r *OrderServicePackageService) isFirstInquiryServicePackage(orderNo string) (bool, error) {
|
|
orderServicePackageDao := dao.OrderServicePackageDao{}
|
|
|
|
maps := make(map[string]interface{})
|
|
maps["order_service_no"] = orderNo
|
|
orderServicePackage, err := orderServicePackageDao.GetOrderServicePackageList(maps)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
if len(orderServicePackage) == 1 {
|
|
return true, nil
|
|
}
|
|
|
|
return false, err
|
|
}
|
|
|
|
// GetCurrentMonthInquiryCount 获取服务包当月已问诊次数
|
|
func (r *OrderServicePackageService) GetCurrentMonthInquiryCount(orderServiceType int, userId, doctorId int64, startDate, endDate time.Time) (int, error) {
|
|
orderInquiryDao := dao.OrderInquiryDao{}
|
|
|
|
maps := make(map[string]interface{})
|
|
maps["inquiry_type"] = 1
|
|
|
|
if orderServiceType == 1 {
|
|
maps["inquiry_mode"] = 8
|
|
} else {
|
|
maps["inquiry_mode"] = 9
|
|
}
|
|
|
|
maps["user_id"] = userId
|
|
maps["doctor_id"] = doctorId
|
|
maps["inquiry_refund_status"] = 0
|
|
maps["inquiry_pay_status"] = 2
|
|
maps["inquiry_status"] = []int{1, 2, 3, 4, 5, 6}
|
|
orderInquirys, err := orderInquiryDao.GetOrderInquiryTimeList(maps, startDate.Format("2006-01-02 15:04"), endDate.Format("2006-01-02 15:04:05"))
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
return len(orderInquirys), err
|
|
}
|
|
|
|
// GetRemainingInquiryCount 获取服务包当月剩余问诊次数
|
|
func (r *OrderServicePackageService) GetRemainingInquiryCount(monthlyFrequency, monthInquiryCount int) string {
|
|
result := ""
|
|
if monthlyFrequency != 0 {
|
|
monthInquiryCount = monthlyFrequency - monthInquiryCount
|
|
if monthInquiryCount < 0 {
|
|
result = "0"
|
|
} else {
|
|
result = strconv.Itoa(monthInquiryCount)
|
|
}
|
|
} else {
|
|
result = "不限"
|
|
}
|
|
return result
|
|
}
|
|
|
|
// GetOrderServiceProductCanUseQuantity 获取服务包内某一药品的可使用数量
|
|
func (r *OrderServicePackageService) GetOrderServiceProductCanUseQuantity(orderServiceId, productId int64, totalQuantity int) (int, error) {
|
|
orderServicePackageProductDao := dao.OrderServicePackageProductDao{}
|
|
|
|
maps := make(map[string]interface{})
|
|
maps["order_service_id"] = orderServiceId
|
|
maps["product_id"] = productId
|
|
orderServicePackageProducts, err := orderServicePackageProductDao.GetOrderServicePackageProductList(maps)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
// 订单使用数量
|
|
usedQuantity := 0
|
|
for _, product := range orderServicePackageProducts {
|
|
usedQuantity = usedQuantity + product.UsedQuantity
|
|
}
|
|
|
|
// 剩余数量 = 总数量-使用数量
|
|
remainingQuantity := totalQuantity - usedQuantity
|
|
if remainingQuantity < 0 {
|
|
remainingQuantity = 0
|
|
}
|
|
|
|
return remainingQuantity, err
|
|
}
|