Files
jx-callback/business/jxstore/cms/job.go
苏尹岚 f78f323db2 job limit
2020-10-15 14:16:48 +08:00

125 lines
3.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package cms
import (
"fmt"
"time"
"git.rosy.net.cn/jx-callback/business/jxstore/financial"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/jxutils"
"git.rosy.net.cn/jx-callback/globals/api"
"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
"git.rosy.net.cn/jx-callback/business/model"
"git.rosy.net.cn/jx-callback/business/model/dao"
)
const (
AcceptMaxCount = 2
CancelMaxCount = 5
)
func PublishJob(ctx *jxcontext.Context, job *model.Job) (err error) {
var (
db = dao.GetDB()
timeNow = utils.Time2Date(time.Now())
fromTime = utils.Time2Str(timeNow) + "00:00:00"
toTime = utils.Time2Str(timeNow) + "23:59:59"
)
//需根据任务类型做一些参数判断,比如门店商品链接,地址
// switch job.JobCategoryID {
// case 1:
// }
if ctx.GetUserID() != job.UserID {
return fmt.Errorf("用户信息已过期,请重新登录!")
}
//发布任务要扣除任务总额的保证金,不够扣就要进行充值
userBill, err := dao.GetUserBill(db, job.UserID, "")
if userBill == nil {
return fmt.Errorf("未查询到该用户的账单!")
}
job.TotalPrice = job.Count * job.AvgPrice
if userBill.DepositBalance < job.TotalPrice {
job.Status = model.JobStatusFailed
} else {
job.Status = model.JobStatusDoing
}
if job.Count <= 0 {
return fmt.Errorf("任务数量不能为0")
}
if job.UserID == "" {
return fmt.Errorf("任务发起人不能为空!")
}
jobs, err := dao.GetJobsNoPage(db, []string{job.UserID}, nil, utils.Str2Time(fromTime), utils.Str2Time(toTime), false)
if len(jobs) > 0 {
members, err := dao.GetUserMember(db, job.UserID, 0, 0, true)
if err != nil {
return err
}
if len(members) <= 0 {
return fmt.Errorf("非会员一天只能发布一起任务,请确认!")
}
}
if job.Address != "" && (job.Lng == 0 || job.Lat == 0) {
lng, lat, err := api.AutonaviAPI.GetCoordinateFromAddressByPage(job.Address)
if err != nil {
return err
}
job.Lng = jxutils.StandardCoordinate2Int(lng)
job.Lat = jxutils.StandardCoordinate2Int(lat)
}
dao.WrapAddIDCULEntity(job, ctx.GetUserName())
dao.Begin(db)
defer func() {
if r := recover(); r != nil {
dao.Rollback(db)
panic(r)
}
}()
err = dao.CreateEntity(db, job)
for _, v := range job.JobSteps {
dao.WrapAddIDCULEntity(v, ctx.GetUserName())
v.JobID = job.ID
err = dao.CreateEntity(db, v)
}
if err != nil {
dao.Rollback(db)
}
//发布任务要扣除任务总额的保证金,不够扣就要进行充值
if err == nil && job.Status != model.JobStatusFailed {
//1、账户支出增加一条记录
if err = financial.AddBillExpend(db, userBill.BillID, model.OrderTypeDeposit, job.TotalPrice); err != nil {
dao.Rollback(db)
}
//2、账户表保证金总额增加相应值
userBill.DepositBalance -= job.TotalPrice
if _, err = dao.UpdateEntity(db, userBill, "DepositBalance"); err != nil {
dao.Rollback(db)
}
}
dao.Commit(db)
return err
}
func GetJobs(ctx *jxcontext.Context, userIDs []string, categoryIDs []int, includeStep bool, fromTime, toTime string, pageSize, offset int) (pagedInfo *model.PagedInfo, err error) {
return dao.GetJobs(dao.GetDB(), userIDs, categoryIDs, includeStep, utils.Str2Time(fromTime), utils.Str2Time(toTime), pageSize, offset)
}
func AcceptJob(ctx *jxcontext.Context, jobID int) (err error) {
var (
db = dao.GetDB()
// userID = ctx.GetUserID()
)
job := &model.Job{}
job.ID = jobID
err = dao.GetEntity(db, &job)
if job.UserID == "" || job.Status == model.JobStatusFailed || job.FinishedAt.Sub(time.Now()) <= 0 {
return fmt.Errorf("未找到该任务或该任务状态不正常,无法接单!")
}
return err
}