Files
jx-callback/business/jxstore/cms/job.go
苏尹岚 2dbd9d542b joborders
2020-10-16 09:59:25 +08:00

182 lines
5.8 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
)
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()
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()
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 || 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
}