package svc import ( "applet/app/admin/md" "applet/app/db" "applet/app/db/model" enum2 "applet/app/enum" "applet/app/utils" "errors" "fmt" "strings" "time" ) func NursingHomeUserUpdate(req md.NursingHomeUserUpdateReq) (err error) { //1、查找当前用户&&当前单位下的所有身份 var userIdentities []model.UserIdentity err = db.Db.Where("uid =? AND enterprise_id =?", req.Uid, req.EnterpriseId).Find(&userIdentities) if err != nil { return } var userIdentityIds []int for _, v := range userIdentities { userIdentityIds = append(userIdentityIds, v.Id) } _, err = db.Db.In("user_identity_id", userIdentityIds).Delete(model.ClassWithUser{}) if err != nil { return } session := db.Db.NewSession() defer session.Close() session.Begin() //2、新增 / 编辑 数据 now := time.Now() userIdentityDb := db.UserIdentityDb{} userIdentityDb.Set(req.Uid) var newUserIdentityIds []string for _, v := range req.BindUserIdentities { if v.UserIdentityId == 0 { // 新增 insertId, err1 := userIdentityDb.UserIdentityInsertBySession(session, &model.UserIdentity{ Uid: req.Uid, Name: v.Name, IdNo: v.IdNo, Kind: enum2.UserIdentityKindForCommon, Identity: enum2.UserIdentityForNursingHome, EnterpriseId: req.EnterpriseId, State: enum2.UserIdentityStateForNormal, Memo: "", CreateAt: now.Format("2006-01-02 15:04:05"), UpdateAt: now.Format("2006-01-02 15:04:05"), }) if err1 != nil { _ = session.Rollback() return err1 } newUserIdentityIds = append(newUserIdentityIds, utils.IntToStr(insertId)) } else { //编辑 identity, err2 := userIdentityDb.GetUserIdentity(v.UserIdentityId) if err != nil { _ = session.Rollback() return err2 } if identity == nil { _ = session.Rollback() return errors.New("未查询到对应的身份信息记录") } identity.IdNo = v.IdNo identity.Name = v.Name _, err3 := userIdentityDb.UserIdentityUpdateBySession(session, v.UserIdentityId, identity, "id_no", "name") if err3 != nil { _ = session.Rollback() return err3 } newUserIdentityIds = append(newUserIdentityIds, utils.IntToStr(v.UserIdentityId)) } } //3、删除需要删除的身份信息 for _, v := range userIdentityIds { if !utils.InArr(utils.IntToStr(v), newUserIdentityIds) { _, err = session.Where("id = ?", v).Delete(model.UserIdentity{}) if err != nil { _ = session.Rollback() return } } } return session.Commit() } func NursingHomeUserDelete(req md.NursingHomeUserDeleteReq) (err error) { //1、删除 user_identity _, err = db.Db.Where("enterprise_id =?", req.EnterpriseId).In("uid", req.Uids).Delete(model.UserIdentity{}) if err != nil { return } return } func NursingHomeOrdList(req md.NursingHomeOrdListReq) (resp []md.NursingHomeOrdListResp, count int64, err error) { var classWithUserIdentityIdsOne []int var classWithUserIdentityIdsTwo []int classWithUserDb := db.ClassWithUserDb{} classWithUserDb.Set() sess := db.Db.Desc("nursing_home_package_ord.id") if req.EnterpriseId != 0 { sess.And("nursing_home_package_ord.enterprise_id =?", req.EnterpriseId) } if req.StartDate != "" { sess.And("nursing_home_package_ord.create_at >= ?", req.StartDate) } if req.EndDate != "" { sess.And("nursing_home_package_ord.create_at <= ?", req.EndDate) } if req.Kind != 0 { sess.And("nursing_home_package_ord.kind = ", req.Kind) } if req.Sate != 0 { sess.And("nursing_home_package_ord.state = ", req.Sate) } if req.OrdSate != 0 { sess.And("nursing_home_package_ord.ord_state = ", req.OrdSate) } if req.OutTradeNo != "" { sess.And("nursing_home_package_ord.out_trade_no like ?", "%"+req.OutTradeNo+"%") } if req.Name != "" { sess.And("user_identity.name like ?", "%"+req.Name+"%") } if req.Phone != "" { sess.And("user.phone like ?", "%"+req.Phone+"%") } if len(classWithUserIdentityIdsOne) > 0 { sess.In("user_identity.id", classWithUserIdentityIdsOne) } if len(classWithUserIdentityIdsTwo) > 0 { sess.In("user_identity.id", classWithUserIdentityIdsTwo) } var m []*db.NursingHomePackageOrdWithUserIdentity count, err = sess. Join("LEFT", "user_identity", "nursing_home_package_ord.user_identity_id = user_identity.id"). Join("LEFT", "enterprise", "enterprise.id = nursing_home_package_ord.enterprise_id"). Join("LEFT", "user", "user.id = nursing_home_package_ord.uid"). Limit(req.Limit, (req.Page-1)*req.Limit).FindAndCount(&m) if err != nil { return nil, 0, err } for _, v := range m { resp = append(resp, md.NursingHomeOrdListResp{ EnterpriseId: v.UserIdentity.EnterpriseId, EnterpriseName: v.Enterprise.Name, Uid: v.UserIdentity.Uid, UserIdentityId: v.UserIdentity.Id, UserIdentityKind: v.UserIdentity.Kind, UserIdentityName: v.NursingHomePackageOrd.UserIdentityName, TotalPrice: v.TotalPrice, Kind: v.NursingHomePackageOrd.Kind, OutTradeNo: v.NursingHomePackageOrd.OutTradeNo, TradeNo: v.NursingHomePackageOrd.TradeNo, State: v.NursingHomePackageOrd.State, OrdState: v.NursingHomePackageOrd.OrdState, CreateAt: v.NursingHomePackageOrd.CreateAt, Phone: v.User.Phone, Name: v.UserIdentity.Name, }) } return } func NursingHomeReserveList(req md.NursingHomeReserveListReq) (resp []md.NursingHomeReserveListResp, count int64, err error) { var classWithUserIdentityIdsOne []int var classWithUserIdentityIdsTwo []int classWithUserDb := db.ClassWithUserDb{} classWithUserDb.Set() sess := db.Db.Where("1=1").Desc("nursing_home_package_ord_for_reserve.id") if req.EnterpriseId != 0 { sess.And("nursing_home_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId) } if req.StartDate != "" { sess.And("nursing_home_package_ord_for_reserve.meal_time_start >= ?", req.StartDate) } if req.EndDate != "" { sess.And("nursing_home_package_ord_for_reserve.meal_time_start <= ?", req.EndDate) } if req.Kind != 0 { sess.And("nursing_home_package_ord_for_reserve.kind = ?", req.Kind) } if req.OutTradeNo != "" { sess.And("nursing_home_package_ord_for_reserve.out_trade_no like ?", "%"+req.OutTradeNo+"%") } if req.Name != "" { sess.And("user_identity.name like ?", "%"+req.Name+"%") } if req.Phone != "" { sess.And("user.phone like ?", "%"+req.Phone+"%") } if len(classWithUserIdentityIdsOne) > 0 { sess.In("user_identity.id", classWithUserIdentityIdsOne) } if len(classWithUserIdentityIdsTwo) > 0 { sess.In("user_identity.id", classWithUserIdentityIdsTwo) } var m []*db.NursingHomePackageOrdForReserveWithUserIdentity count, err = sess. Join("LEFT", "nursing_home_package_ord", "nursing_home_package_ord_for_reserve.out_trade_no = nursing_home_package_ord.out_trade_no"). Join("LEFT", "user_identity", "nursing_home_package_ord.user_identity_id = user_identity.id"). Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id"). Join("LEFT", "user", "user.id = user_identity.uid"). Limit(req.Limit, (req.Page-1)*req.Limit).FindAndCount(&m) if err != nil { return nil, 0, err } for _, v := range m { resp = append(resp, md.NursingHomeReserveListResp{ Id: v.NursingHomePackageOrdForReserve.Id, EnterpriseId: v.UserIdentity.EnterpriseId, EnterpriseName: v.Enterprise.Name, Uid: v.UserIdentity.Uid, UserIdentityId: v.UserIdentity.Id, UserIdentityName: v.NursingHomePackageOrd.UserIdentityName, Kind: v.NursingHomePackageOrdForReserve.Kind, ReserveMonth: v.NursingHomePackageOrdForReserve.ReserveMonth, CreateAt: v.NursingHomePackageOrdForReserve.CreateAt, OutTradeNo: v.NursingHomePackageOrdForReserve.OutTradeNo, Phone: v.User.Phone, }) } return } func NursingHomeOrdRefund(req md.NursingHomeOrdRefundReq) (err error) { //1、查询出所有 `nursing_home_user_with_day` 记录 var m []model.NursingHomeUserWithDay nursingHomeUserWithDayDb := db.NursingHomeUserWithDayDb{} nursingHomeUserWithDayDb.Set(0) err = nursingHomeUserWithDayDb.Db.In("id", req.Ids).Find(&m) if err != nil { return err } if len(m) > 0 { //2、更改 `nursing_home_user_with_day` 的 state 为 退款中 sql := "update nursing_home_user_with_day set state = %d where id In (%s)" idsStr := strings.Join(req.Ids, ",") sql = fmt.Sprintf(sql, enum2.NursingHomeUserWithDayStateForCanceling, idsStr) fmt.Println(sql) _, err = db.ExecuteOriginalSql(db.Db, sql) if err != nil { return err } //3、循环处理数据 var dealOutTradeNo = map[string]string{} var nursingHomeUserRefundDays []*model.NursingHomeUserRefundDay now := time.Now() for _, v := range m { dealOutTradeNo[v.OrdNo] = v.OrdNo outRequestNo := utils.OrderUUID(v.Uid) nursingHomeUserRefundDays = append(nursingHomeUserRefundDays, &model.NursingHomeUserRefundDay{ OutTradeNo: v.OrdNo, OutRequestNo: outRequestNo, Uid: v.Uid, IdentityId: v.IdentityId, RecordsId: v.Id, State: enum2.NursingHomeUserRefundDayStateForAuditing, Amount: v.Amount, Memo: "", CreateAt: now.Format("2006-01-02 15:04:05"), UpdateAt: now.Format("2006-01-02 15:04:05"), }) } //4、处理 `nursing_home_package_ord` 的 订单状态(ord_state) for _, v := range dealOutTradeNo { err1 := JudgeNursingHomePackageOrdOrdState(v) if err1 != nil { return err1 } } //5、新增 `nursing_home_user_refund_day` 数据 nursingHomeUserRefundDayDb := db.NursingHomeUserRefundDayDb{} nursingHomeUserRefundDayDb.Set(0) _, err = nursingHomeUserRefundDayDb.BatchAddNursingHomeUserRefundDays(nursingHomeUserRefundDays) } return } // JudgeNursingHomePackageOrdOrdState 判断订单状态 func JudgeNursingHomePackageOrdOrdState(outTradeNo string) (err error) { nursingHomePackageOrd := db.NursingHomePackageOrd{} nursingHomePackageOrd.Set(outTradeNo) ord, err := nursingHomePackageOrd.GetNursingHomePackageOrd() if err != nil { return } var ordState, oldOrdState int oldOrdState = ord.OrdState ordState = oldOrdState var m model.NursingHomeUserWithDay total, err := db.Db.Where("ord_no =?", outTradeNo).Count(&m) if err != nil { return } //1、判断是否有 `待就餐` count1, err := db.Db.Where("ord_no =?", outTradeNo).And("state =?", enum2.NursingHomeUserWithDayStateForWait).Count(&m) if err != nil { return } if count1 == 0 { ordState = enum2.NursingHomePackageOrdOrdStateForComplete } else { ordState = enum2.NursingHomePackageOrdOrdStateForSuccess } //2、判断是否有 `已退款` / `部分退款` count2, err := db.Db.Where("ord_no =?", outTradeNo).And("state =?", enum2.NursingHomeUserWithDayStateForCancel).Count(&m) if err != nil { return } //if count2 > 0 && count2 < total && count1 > 0 { if count2 > 0 && count2 < total { ordState = enum2.NursingHomePackageOrdOrdStateForPartRefunded } if count2 > 0 && count2 == total { ordState = enum2.NursingHomePackageOrdOrdStateForRefunded } //3、判断是否有 `退款中` count3, err := db.Db.Where("ord_no =?", outTradeNo).And("state =?", enum2.NursingHomeUserWithDayStateForCanceling).Count(&m) if err != nil { return } if count3 > 0 { ordState = enum2.NursingHomePackageOrdOrdStateForRefunding } if ordState != oldOrdState { ord.OrdState = ordState _, err2 := nursingHomePackageOrd.NursingHomePackageOrdUpdate(ord, "ord_state") if err2 != nil { return err2 } } return }