joborders

This commit is contained in:
苏尹岚
2020-10-16 09:59:25 +08:00
parent 61252f4527
commit 2dbd9d542b
5 changed files with 167 additions and 17 deletions

View File

@@ -22,6 +22,29 @@ const (
CancelMaxCount = 5
)
var (
DayTimeBegin time.Time
DayTimeEnd time.Time
WeekTimeBegin time.Time
WeekTimeEnd time.Time
)
func init() {
DayTimeBegin = utils.Str2Time(utils.Time2Str(utils.Time2Date(time.Now())) + " 00:00:00")
DayTimeEnd = utils.Str2Time(utils.Time2Str(utils.Time2Date(time.Now())) + " 23:59:59")
WeekTimeBegin, WeekTimeEnd = getWeekTime()
}
func getWeekTime() (weekTimeBegin, weekTimeEnd time.Time) {
offset := int(time.Now().Weekday() - 1)
if offset == -1 {
offset = -6
}
weekTimeBegin = time.Now().AddDate(0, 0, offset)
weekTimeEnd = weekTimeBegin.AddDate(0, 0, 7)
return weekTimeBegin, weekTimeEnd
}
func PublishJob(ctx *jxcontext.Context, job *model.Job) (err error) {
var (
db = dao.GetDB()
@@ -94,7 +117,7 @@ func PublishJob(ctx *jxcontext.Context, job *model.Job) (err error) {
if err = financial.AddBillExpend(db, userBill.BillID, model.OrderTypeDeposit, job.TotalPrice); err != nil {
dao.Rollback(db)
}
//2、账户表保证金总额增加相应值
//2、账户表保证金总额减少相应值
userBill.DepositBalance -= job.TotalPrice
if _, err = dao.UpdateEntity(db, userBill, "DepositBalance"); err != nil {
dao.Rollback(db)
@@ -110,15 +133,49 @@ func GetJobs(ctx *jxcontext.Context, userIDs []string, categoryIDs []int, includ
func AcceptJob(ctx *jxcontext.Context, jobID int) (err error) {
var (
db = dao.GetDB()
// userID = ctx.GetUserID()
db = dao.GetDB()
userID = ctx.GetUserID()
num int
)
job := &model.Job{}
job.ID = jobID
err = dao.GetEntity(db, &job)
if job.UserID == "" || job.Status == model.JobStatusFailed || job.FinishedAt.Sub(time.Now()) <= 0 {
if job.UserID == "" || job.Status == model.JobStatusFailed || job.FinishedAt.Sub(time.Now()) <= 0 || job.SurplusCount <= 0 || job.LimitCountType <= 0 {
return fmt.Errorf("未找到该任务或该任务状态不正常,无法接单!")
}
num, err = checkJobOrders(db, "<= "+utils.Int2Str(model.JobOrderStatusWaitAudit), userID, utils.ZeroTimeValue, utils.ZeroTimeValue)
if num > 0 {
return fmt.Errorf("您还有此任务未完成,请完成后再接取!")
}
num, err = checkJobOrders(db, "= "+utils.Int2Str(model.JobOrderStatusAuditUnPass), userID, utils.ZeroTimeValue, utils.ZeroTimeValue)
if num > 0 {
return fmt.Errorf("您还有此任务未审核通过记录,可直接在未审核中重新提交!")
}
switch job.LimitCountType {
case model.JobLimitCountTypePO:
num, err = checkJobOrders(db, "<> "+utils.Int2Str(model.JobOrderStatusCancel), userID, utils.ZeroTimeValue, utils.ZeroTimeValue)
if num > 0 {
return fmt.Errorf("此任务只支持每人做一次!")
}
case model.JobLimitCountTypePDO:
num, err = checkJobOrders(db, "<> "+utils.Int2Str(model.JobOrderStatusCancel), userID, DayTimeBegin, DayTimeEnd)
if num > 0 {
return fmt.Errorf("此任务只支持每人每天做一次!")
}
case model.JobLimitCountTypePWO:
num, err = checkJobOrders(db, "<> "+utils.Int2Str(model.JobOrderStatusCancel), userID, WeekTimeBegin, WeekTimeEnd)
if num > 0 {
return fmt.Errorf("此任务只支持每人每周做一次!")
}
case model.JobLimitCountTypeNoLimit:
default:
return fmt.Errorf("不支持的任务限次类型!%v", job.LimitCountType)
}
return err
}
func checkJobOrders(db *dao.DaoDB, statusCompareStr, userID string, fromTime, toTime time.Time) (num int, err error) {
jobOrders, err := dao.GetJobOrdersNoPage(db, 0, 0, userID, statusCompareStr, fromTime, toTime, nil)
return len(jobOrders), err
}