From d69c31b77340b53c5f90b82b74aa663ed5660c7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E5=B0=B9=E5=B2=9A?= <770236076@qq.com> Date: Thu, 10 Dec 2020 14:02:46 +0800 Subject: [PATCH] bilifanxian --- business/jxstore/cms/job.go | 89 ++++++++++++++++++++---------- business/jxstore/cms/user2.go | 2 +- business/jxstore/event/event.go | 4 +- business/jxstore/financial/bill.go | 14 +++-- business/jxstore/financial/pay.go | 4 +- business/model/dao/dao_bill.go | 16 ++++++ business/model/job.go | 19 ++++--- 7 files changed, 100 insertions(+), 48 deletions(-) diff --git a/business/jxstore/cms/job.go b/business/jxstore/cms/job.go index c68a5d4c1..7d9dd7e93 100644 --- a/business/jxstore/cms/job.go +++ b/business/jxstore/cms/job.go @@ -34,6 +34,8 @@ const ( CancelMaxCount = 5 waybillKgPrice = 200 + + mtwmMemberPrice = 1100 ) var ( @@ -70,8 +72,8 @@ func PublishJob(ctx *jxcontext.Context, jobExt *model.JobExt) (errCode string, e // 需根据返现类型做一些参数判断 switch job.CashbackType { case model.JobCashbackPercentage: - if job.Percentage <= 0 || job.Percentage > 10 { - return errCode, fmt.Errorf("返现比例请输入1-10之间的比例!") + if job.Percentage <= 0 || job.Percentage > 100 { + return errCode, fmt.Errorf("返现比例请输入1-100之间的比例!") } case model.JobCashbackPrice: default: @@ -152,7 +154,7 @@ func PublishJob(ctx *jxcontext.Context, jobExt *model.JobExt) (errCode string, e } //发布任务要扣除任务总额的保证金,不够扣就要进行充值 if err == nil && job.Status != model.JobStatusFailed { - if err = financial.AddExpendUpdateAccount(db, userBill, model.BillTypeDeposit, job.TotalPrice); err != nil { + if err = financial.AddExpendUpdateAccount(db, userBill, model.BillTypeDeposit, job.TotalPrice, job.ID); err != nil { dao.Rollback(db) return } @@ -183,13 +185,27 @@ func CancelPublishJob(ctx *jxcontext.Context, jobID int) (err error) { panic(r) } }() - if err = financial.AddIncomeUpdateAccount(db, userBill, model.BillTypeJobCancelOverdue, job.SurplusCount*job.AvgPrice); err != nil { + //如果是固定返现,退给任务发起人剩余数量*单价 + //如果是比例返现,推给任务发起人任务总价-接取这个任务的用户实际返现的价格之和 + var price int + if job.CashbackType == model.JobCashbackPrice { + price = job.SurplusCount * job.AvgPrice + } else { + if billIncomes, err := dao.GetBillIncome(db, jobID); err == nil { + for _, v := range billIncomes { + price += v.IncomePrice + } + } + price = job.TotalPrice - price + } + if err = financial.AddIncomeUpdateAccount(db, userBill, model.BillTypeJobCancelOverdue, price, jobID); err != nil { dao.Rollback(db) return } //3、任务状态被取消 job.Status = model.JobStatusFailed - if _, err = dao.UpdateEntity(db, job, "Status"); err != nil { + job.DeletedAt = time.Now() + if _, err = dao.UpdateEntity(db, job, "Status", "DeletedAt"); err != nil { dao.Rollback(db) return } @@ -349,9 +365,12 @@ func CancelAcceptJob(ctx *jxcontext.Context, jobID int, jobOrderID int64) (err e } //如果状态不正常(取消或者过期)就要把这一笔退回去 //2、账户收入 - if err = financial.AddIncomeUpdateAccount(db, userBill, model.BillTypeJobCancelOverdue, job.AvgPrice); err != nil { - dao.Rollback(db) - return err + //是固定返现才会退一笔任务单价 + if job.CashbackType == model.JobCashbackPrice { + if err = financial.AddIncomeUpdateAccount(db, userBill, model.BillTypeJobCancelOverdue, job.AvgPrice, jobID); err != nil { + dao.Rollback(db) + return err + } } } //3、任务订单状态被取消 @@ -441,6 +460,12 @@ func SubmitJob(ctx *jxcontext.Context, jobOrder *model.JobOrder) (err error) { jobOrder2 := &model.JobOrder{} jobOrder2.JobOrderID = jobOrder.JobOrderID err = dao.GetEntity(db, jobOrder2, "JobOrderID") + //如果是比例返现 + if job.CashbackType == model.JobCashbackPercentage { + if jobOrder.UserActualPrice == 0 { + return fmt.Errorf("比例返现任务请输入订单实际实付金额,用于任务发起人审核!") + } + } if jobOrder2.JobID == 0 { return fmt.Errorf("未查询到相应的任务!") } @@ -487,8 +512,9 @@ func AuditJob(ctx *jxcontext.Context, jobOrderID, status int, comment string) (e if ctx.GetUserID() != job.UserID { return fmt.Errorf("任务发起人才能审核!") } - //审核时,若此任务已过期或者已取消,不通过则应将此任务保证金退还给发起人,通过则应将单次任务保证金给接受人 - //若此任务未过期,不通过则此任务剩余数量将+1,通过则应将单次任务保证金给接受人 + //固定返现 + //1、审核时,若此任务已过期或者已取消,不通过则应将此任务保证金退还给发起人,通过则应将单次任务保证金给接受人 + //2、若此任务未过期,不通过则此任务剩余数量将+1,通过则应将单次任务保证金给接受人 jobOrder.Status = status jobOrder.Comment = comment jobOrder.AuditAt = time.Now() @@ -506,6 +532,15 @@ func AuditJob(ctx *jxcontext.Context, jobOrderID, status int, comment string) (e return } if status == model.JobOrderStatusAuditPass { + var price int + if job.CashbackType == model.JobCashbackPrice { + price = job.AvgPrice + } else { + price = jobOrder.UserActualPrice * job.Percentage / 100 + if price > job.AvgPrice { + price = job.AvgPrice + } + } //若完成任务的人在某个群组中,则要向群主分成 if messageGroupMembers, err := dao.GetMessageGroupMembers(db, 0, model.GroupTypeMulit, jobOrder.UserID); err == nil { if len(messageGroupMembers) > 1 { @@ -516,19 +551,19 @@ func AuditJob(ctx *jxcontext.Context, jobOrderID, status int, comment string) (e //不分成 if messageGroupsResult[0].DividePercentage != 0 { if userBillGroupMaster, err := dao.GetUserBill(db, messageGroupsResult[0].UserID, ""); err == nil { - if err = financial.AddIncomeUpdateAccount(db, userBillGroupMaster, model.BillTypeDivide, job.AvgPrice*messageGroupsResult[0].DividePercentage/100); err != nil { + if err = financial.AddIncomeUpdateAccount(db, userBillGroupMaster, model.BillTypeDivide, price*messageGroupsResult[0].DividePercentage/100, job.ID); err != nil { dao.Rollback(db) return err } } //接收人账户收入 - if err = financial.AddIncomeUpdateAccount(db, userBillJobOrder, model.BillTypeJobDivide, job.AvgPrice*(100-messageGroupsResult[0].DividePercentage)/100); err != nil { + if err = financial.AddIncomeUpdateAccount(db, userBillJobOrder, model.BillTypeJobDivide, price*(100-messageGroupsResult[0].DividePercentage)/100, job.ID); err != nil { dao.Rollback(db) return err } } else { //接收人账户收入 - if err = financial.AddIncomeUpdateAccount(db, userBillJobOrder, model.BillTypeJob, job.AvgPrice); err != nil { + if err = financial.AddIncomeUpdateAccount(db, userBillJobOrder, model.BillTypeJob, price, job.ID); err != nil { dao.Rollback(db) return err } @@ -537,7 +572,7 @@ func AuditJob(ctx *jxcontext.Context, jobOrderID, status int, comment string) (e } } else if len(messageGroupMembers) == 0 { //若没有在某个群组,则得到全部 //接收人账户收入 - if err = financial.AddIncomeUpdateAccount(db, userBillJobOrder, model.BillTypeJob, job.AvgPrice); err != nil { + if err = financial.AddIncomeUpdateAccount(db, userBillJobOrder, model.BillTypeJob, price, job.ID); err != nil { dao.Rollback(db) return err } @@ -550,10 +585,12 @@ func AuditJob(ctx *jxcontext.Context, jobOrderID, status int, comment string) (e } } else { if job.Status < 0 { - userBill, err := dao.GetUserBill(db, job.UserID, "") - if err = financial.AddIncomeUpdateAccount(db, userBill, model.BillTypeJobAuditUnPassWithCancelOverdue, job.AvgPrice); err != nil { - dao.Rollback(db) - return err + if job.CashbackType == model.JobCashbackPrice { + userBill, err := dao.GetUserBill(db, job.UserID, "") + if err = financial.AddIncomeUpdateAccount(db, userBill, model.BillTypeJobAuditUnPassWithCancelOverdue, job.AvgPrice, job.ID); err != nil { + dao.Rollback(db) + return err + } } } else { job.SurplusCount += 1 @@ -593,11 +630,7 @@ func RefreshJobStatus(ctx *jxcontext.Context) (err error) { } for _, job := range jobs { if time.Now().Sub(*job.FinishedAt) >= 0 { - job2 := &model.Job{} - job2.ID = job.ID - dao.GetEntity(db, job2) - job2.Status = model.JobStatusOverdue - dao.UpdateEntity(db, job2, "Status") + CancelPublishJob(ctx, job.ID) } } globals.SugarLogger.Debugf("RefreshJobStatus end...") @@ -654,11 +687,11 @@ func RechargeMtMembers(ctx *jxcontext.Context, phone int) (errCode string, err e if err != nil { return errCode, err } - if userBill.AccountBalance < 1100 { + if userBill.AccountBalance < mtwmMemberPrice { return model.ErrCodeAccountBalanceNotEnough, fmt.Errorf("用户余额不足,请充值!") } //账户支出 - if err = financial.AddExpendUpdateAccount(db, userBill, model.BillTypeSpJob, 1100); err != nil { + if err = financial.AddExpendUpdateAccount(db, userBill, model.BillTypeSpJob, mtwmMemberPrice, 1); err != nil { dao.Rollback(db) return errCode, err } @@ -779,7 +812,7 @@ func SendJdDelivery(ctx *jxcontext.Context, dOrder *model.DeliveryOrder) (errCod } }() //账户支出明细 - if err = financial.AddExpendUpdateAccount(db, userBill, model.BillTypeSpJob, dOrder.PayPrice); err != nil { + if err = financial.AddExpendUpdateAccount(db, userBill, model.BillTypeSpJob, dOrder.PayPrice, 2); err != nil { dao.Rollback(db) return } @@ -843,7 +876,7 @@ func CancelJdDelivery(ctx *jxcontext.Context, vendorWaybillID, reason string) (e dao.Rollback(db) return } - if err = financial.AddIncomeUpdateAccount(db, userBill, model.BillTypeSpJob, dOrder.PayPrice); err != nil { + if err = financial.AddIncomeUpdateAccount(db, userBill, model.BillTypeSpJob, dOrder.PayPrice, 2); err != nil { dao.Rollback(db) return } @@ -925,7 +958,7 @@ func CheckJdDeliveryWeight(ctx *jxcontext.Context) (err error) { if err != nil { return retVal, err } - if err = financial.AddExpendUpdateAccount(db, userBill, model.BillTypeJdWaybillOverWeight, utils.Float64TwoInt(diffPrice)); err != nil { + if err = financial.AddExpendUpdateAccount(db, userBill, model.BillTypeJdWaybillOverWeight, utils.Float64TwoInt(diffPrice), 2); err != nil { return retVal, err } deliveryOrder.IsWeight = 2 diff --git a/business/jxstore/cms/user2.go b/business/jxstore/cms/user2.go index 9ea86cb6b..d10cf5f40 100644 --- a/business/jxstore/cms/user2.go +++ b/business/jxstore/cms/user2.go @@ -605,7 +605,7 @@ func InvestMember(ctx *jxcontext.Context, memberID int, userID string, isFree bo } if !isFree { //支出明细 - if err = financial.AddExpendUpdateAccount(db, userBill, model.BillTypeMember, memberCard.Price); err != nil { + if err = financial.AddExpendUpdateAccount(db, userBill, model.BillTypeMember, memberCard.Price, 0); err != nil { dao.Rollback(db) return } diff --git a/business/jxstore/event/event.go b/business/jxstore/event/event.go index fb1e25c52..21122f705 100644 --- a/business/jxstore/event/event.go +++ b/business/jxstore/event/event.go @@ -420,12 +420,12 @@ func DeleteMessageGroup(ctx *jxcontext.Context, groupID int, userID string) (err } }() //账户支出 - if err = financial.AddExpendUpdateAccount(db, userBill, model.BillTypeQuitGroup, quitPrice); err != nil { + if err = financial.AddExpendUpdateAccount(db, userBill, model.BillTypeQuitGroup, quitPrice, 0); err != nil { dao.Rollback(db) return errCode, err } //群主收到退团金额 - if err = financial.AddIncomeUpdateAccount(db, userBillGroupMaster, model.BillTypeQuitGroup, quitPrice); err != nil { + if err = financial.AddIncomeUpdateAccount(db, userBillGroupMaster, model.BillTypeQuitGroup, quitPrice, 0); err != nil { dao.Rollback(db) return errCode, err } diff --git a/business/jxstore/financial/bill.go b/business/jxstore/financial/bill.go index 70ce3d060..0e75764d3 100644 --- a/business/jxstore/financial/bill.go +++ b/business/jxstore/financial/bill.go @@ -7,21 +7,23 @@ import ( "git.rosy.net.cn/jx-callback/business/model/dao" ) -func AddBillIncome(db *dao.DaoDB, billID int64, billType, incomePrice int) (err error) { +func AddBillIncome(db *dao.DaoDB, billID int64, billType, incomePrice, jobID int) (err error) { billIncome := &model.BillIncome{ BillID: billID, Type: billType, IncomePrice: incomePrice, + JobID: jobID, } dao.WrapAddIDCULEntity(billIncome, jxcontext.AdminCtx.GetUserName()) return dao.CreateEntity(db, billIncome) } -func AddBillExpend(db *dao.DaoDB, billID int64, billType, expendPrice int) (err error) { +func AddBillExpend(db *dao.DaoDB, billID int64, billType, expendPrice, jobID int) (err error) { billExpend := &model.BillExpend{ BillID: billID, Type: billType, ExpendPrice: expendPrice, + JobID: jobID, } dao.WrapAddIDCULEntity(billExpend, jxcontext.AdminCtx.GetUserName()) return dao.CreateEntity(db, billExpend) @@ -39,9 +41,9 @@ func GetUserBillDetail(ctx *jxcontext.Context, userID, fromTime, toTime string, return dao.GetUserBillDetail(dao.GetDB(), userID, utils.Str2Time(fromTime), utils.Str2Time(toTime), pageSize, offset) } -func AddExpendUpdateAccount(db *dao.DaoDB, userBill *model.UserBill, billType, price int) (err error) { +func AddExpendUpdateAccount(db *dao.DaoDB, userBill *model.UserBill, billType, price, jobID int) (err error) { //1、账户支出增加一条记录 - err = AddBillExpend(db, userBill.BillID, billType, price) + err = AddBillExpend(db, userBill.BillID, billType, price, jobID) if err != nil { return err } @@ -51,9 +53,9 @@ func AddExpendUpdateAccount(db *dao.DaoDB, userBill *model.UserBill, billType, p return err } -func AddIncomeUpdateAccount(db *dao.DaoDB, userBill *model.UserBill, billType, price int) (err error) { +func AddIncomeUpdateAccount(db *dao.DaoDB, userBill *model.UserBill, billType, price, jobID int) (err error) { //2、账户收入增加一条记录 - err = AddBillIncome(db, userBill.BillID, billType, price) + err = AddBillIncome(db, userBill.BillID, billType, price, jobID) if err != nil { return err } diff --git a/business/jxstore/financial/pay.go b/business/jxstore/financial/pay.go index 4fb27d99a..6ec846a96 100644 --- a/business/jxstore/financial/pay.go +++ b/business/jxstore/financial/pay.go @@ -58,7 +58,7 @@ func OnPayFinished(order *model.Order) (err error) { case model.OrderTypePay: //如果是账户充值(发布任务等) //账户收入 - if err = AddIncomeUpdateAccount(db, userBill, model.BillTypeInvest, order.PayPrice); err != nil { + if err = AddIncomeUpdateAccount(db, userBill, model.BillTypeInvest, order.PayPrice, 0); err != nil { dao.Rollback(db) } default: @@ -93,7 +93,7 @@ func OnCashFinished(order *model.Order) (err error) { case model.OrderTypeCash: //如果是账户提现 //账户支出 - if err = AddExpendUpdateAccount(db, userBill, model.BillTypeCash, order.PayPrice); err != nil { + if err = AddExpendUpdateAccount(db, userBill, model.BillTypeCash, order.PayPrice, 0); err != nil { dao.Rollback(db) } default: diff --git a/business/model/dao/dao_bill.go b/business/model/dao/dao_bill.go index 4c81d99de..56dcbd2a1 100644 --- a/business/model/dao/dao_bill.go +++ b/business/model/dao/dao_bill.go @@ -53,6 +53,22 @@ func GetBillExpend(db *DaoDB, userID string, billType int, fromTime, toTime time return billExpends, err } +func GetBillIncome(db *DaoDB, jobID int) (billIncomes []*model.BillIncome, err error) { + sql := ` + SELECT b.* + FROM user_bill a + LEFT JOIN bill_income b ON b.bill_id = a.bill_id + WHERE a.deleted_at = ? + ` + sqlParams := []interface{}{utils.DefaultTimeValue} + if jobID != 0 { + sql += ` AND b.job_id = ?` + sqlParams = append(sqlParams, jobID) + } + err = GetRows(db, &billIncomes, sql, sqlParams) + return billIncomes, err +} + type UserBillDetail struct { CreatedAt time.Time `json:"created_at"` LastOperator string `json:"lastOperator"` diff --git a/business/model/job.go b/business/model/job.go index 2ed67ea05..31b3a9b7d 100644 --- a/business/model/job.go +++ b/business/model/job.go @@ -138,15 +138,16 @@ func (v *JobStep) TableIndex() [][]string { type JobOrder struct { ModelIDCUL - JobID int `orm:"column(job_id)" json:"jobID"` //任务ID - JobOrderID int64 `orm:"column(job_order_id)" json:"jobOrderID"` //任务订单号 - UserID string `orm:"column(user_id)" json:"userID"` //接任务人ID - Status int `json:"status"` //任务订单状态,接单,待审核,已审核,已结算等 - SubmitAuditAt time.Time `json:"submitAuditTime"` //提交审核日期 - AuditAt time.Time `json:"auditAt"` //审核日期 - Content string `josn:"content"` //任务审核内容 - Imgs string `json:"imgs"` //任务审核图片 - Comment string `json:"comment"` //审核理由 + JobID int `orm:"column(job_id)" json:"jobID"` //任务ID + JobOrderID int64 `orm:"column(job_order_id)" json:"jobOrderID"` //任务订单号 + UserID string `orm:"column(user_id)" json:"userID"` //接任务人ID + Status int `json:"status"` //任务订单状态,接单,待审核,已审核,已结算等 + SubmitAuditAt time.Time `json:"submitAuditTime"` //提交审核日期 + AuditAt time.Time `json:"auditAt"` //审核日期 + Content string `josn:"content"` //任务审核内容 + Imgs string `json:"imgs"` //任务审核图片 + Comment string `json:"comment"` //审核理由 + UserActualPrice int `json:"userActualPrice"` //用户订单实际支付(用户自填) } func (v *JobOrder) TableIndex() [][]string {