Kaynağa Gözat

专项发货管理

yuliang1112 2 yıl önce
ebeveyn
işleme
1320fb238b

+ 1 - 0
db/init.go

@@ -24,6 +24,7 @@ func Init(config *system_model.Mysql) {
 func GetReadDB(ctx context.Context) *gorm.DB {
 	return client.WithContext(ctx)
 }
+
 func GetWriteDB(ctx context.Context) *gorm.DB {
 	return client.WithContext(ctx)
 }

+ 72 - 0
handler/createSpecialLogistics.go

@@ -0,0 +1,72 @@
+package handler
+
+import (
+	"github.com/gin-gonic/gin"
+	"github.com/sirupsen/logrus"
+	"youngee_m_api/consts"
+	"youngee_m_api/model/http_model"
+	"youngee_m_api/service"
+	"youngee_m_api/util"
+)
+
+func WrapCreateSpecialLogisticsHandler(ctx *gin.Context) {
+	handler := newCreateSpecialLogisticsHandler(ctx)
+	BaseRun(handler)
+}
+
+type CreateSpecialLogistics struct {
+	ctx  *gin.Context
+	req  *http_model.CreateSpecialLogisticsRequest
+	resp *http_model.CommonResponse
+}
+
+func (c CreateSpecialLogistics) getContext() *gin.Context {
+	return c.ctx
+}
+
+func (c CreateSpecialLogistics) getResponse() interface{} {
+	return c.resp
+}
+
+func (c CreateSpecialLogistics) getRequest() interface{} {
+	return c.req
+}
+
+func (c CreateSpecialLogistics) run() {
+	data := http_model.CreateSpecialLogisticsRequest{}
+	data = *c.req
+	isUpdate := data.IsUpdate
+	if isUpdate == 0 {
+		res, err := service.Logistics.CreateSpecialLogistics(c.ctx, data)
+		if err != nil {
+			logrus.Errorf("[CreateSpecialLogistics] call Create err:%+v\n", err)
+			util.HandlerPackErrorResp(c.resp, consts.ErrorInternal, "")
+			logrus.Info("CreateSpecialLogistics fail,req:%+v", c.req)
+			return
+		}
+		c.resp.Message = "成功添加物流信息"
+		c.resp.Data = res
+	} else {
+		res, err := service.Logistics.UpdateSpecialLogistics(c.ctx, data)
+		if err != nil {
+			logrus.Errorf("[CreateSpecialLogistics] call Create err:%+v\n", err)
+			util.HandlerPackErrorResp(c.resp, consts.ErrorInternal, "")
+			logrus.Info("CreateSpecialLogistics fail,req:%+v", c.req)
+			return
+		}
+		c.resp.Message = "成功修改物流信息"
+		c.resp.Data = res
+	}
+}
+
+func (c CreateSpecialLogistics) checkParam() error {
+	return nil
+}
+
+func newCreateSpecialLogisticsHandler(ctx *gin.Context) *CreateSpecialLogistics {
+	return &CreateSpecialLogistics{
+		ctx:  ctx,
+		req:  http_model.NewCreateSpecialLogisticsRequest(),
+		resp: http_model.NewCreateSpecialLogisticsResponse(),
+	}
+}

+ 4 - 1
handler/logistics_create.go

@@ -33,12 +33,15 @@ type CreateLogisticsHandler struct {
 func (h *CreateLogisticsHandler) getRequest() interface{} {
 	return h.req
 }
+
 func (h *CreateLogisticsHandler) getContext() *gin.Context {
 	return h.ctx
 }
+
 func (h *CreateLogisticsHandler) getResponse() interface{} {
 	return h.resp
 }
+
 func (h *CreateLogisticsHandler) run() {
 	data := http_model.CreateLogisticsRequest{}
 	data = *h.req
@@ -64,8 +67,8 @@ func (h *CreateLogisticsHandler) run() {
 		h.resp.Message = "成功修改物流信息"
 		h.resp.Data = res
 	}
-
 }
+
 func (h *CreateLogisticsHandler) checkParam() error {
 	return nil
 }

+ 58 - 0
handler/signForSpecialLogistic.go

@@ -0,0 +1,58 @@
+package handler
+
+import (
+	"github.com/gin-gonic/gin"
+	"github.com/sirupsen/logrus"
+	"youngee_m_api/consts"
+	"youngee_m_api/model/http_model"
+	"youngee_m_api/service"
+	"youngee_m_api/util"
+)
+
+func WrapSignForSpecialLogisticHandler(ctx *gin.Context) {
+	handler := newSignForSpecialLogisticHandler(ctx)
+	BaseRun(handler)
+}
+
+type SignForSpecialLogisticHandler struct {
+	ctx  *gin.Context
+	req  *http_model.SignForSpecialLogisticRequest
+	resp *http_model.CommonResponse
+}
+
+func (s SignForSpecialLogisticHandler) getContext() *gin.Context {
+	return s.ctx
+}
+
+func (s SignForSpecialLogisticHandler) getResponse() interface{} {
+	return s.resp
+}
+
+func (s SignForSpecialLogisticHandler) getRequest() interface{} {
+	return s.req
+}
+
+func (s SignForSpecialLogisticHandler) run() {
+	data := http_model.SignForSpecialLogisticRequest{}
+	data = *s.req
+	err := service.Logistics.SignForSpecialLogistic(s.ctx, data)
+	if err != nil {
+		logrus.Errorf("[SignForSpecialLogisticHandler] call Create err:%+v\n", err)
+		util.HandlerPackErrorResp(s.resp, consts.ErrorInternal, "")
+		logrus.Info("SignForSpecialLogisticHandler fail,req:%+v", s.req)
+		return
+	}
+	s.resp.Message = "物流状态更换成功"
+}
+
+func (s SignForSpecialLogisticHandler) checkParam() error {
+	return nil
+}
+
+func newSignForSpecialLogisticHandler(ctx *gin.Context) *SignForSpecialLogisticHandler {
+	return &SignForSpecialLogisticHandler{
+		ctx:  ctx,
+		req:  http_model.NewSignForSpecialLogisticRequest(),
+		resp: http_model.NewSignForSpecialLogisticResponse(),
+	}
+}

+ 29 - 0
model/http_model/CreateSpecialLogisticsRequest.go

@@ -0,0 +1,29 @@
+package http_model
+
+type CreateSpecialLogisticsRequest struct {
+	LogisticsID           int64  `json:"logistics_id"`            // 货物-id
+	CompanyName           string `json:"company_name"`            // 实物商品-物流公司名称
+	LogisticsNumber       string `json:"logistics_number"`        // 实物商品-物流单号
+	ExplorestoreStarttime string `json:"explorestore_starttime"`  // 线下探店-探店开始时间
+	ExplorestoreEndtime   string `json:"explorestore_endtime"`    // 线下探店-探店结束时间
+	ExplorestorePeriod    string `json:"explorestore_period"`     // 线下探店-探店持续时间
+	CouponCodeInformation string `json:"coupon_code_information"` // 虚拟产品-券码信息
+	TaskID                string `json:"task_id"`                 // 任务id
+	DeliveryTime          string `json:"delivery_time"`           // 发货时间
+	ThingsType            int    `json:"things_type"`             // 产品类型 1:实物, 2:虚拟产品,3:线下探店
+	IsUpdate              int    `json:"is_update"`               // 更新标志位 0:不更新 1:更新
+}
+
+type SpecialLogisticsData struct {
+	LogisticsID int64 `json:"logistics_id"` // 货物-id
+}
+
+func NewCreateSpecialLogisticsRequest() *CreateSpecialLogisticsRequest {
+	return new(CreateSpecialLogisticsRequest)
+}
+
+func NewCreateSpecialLogisticsResponse() *CommonResponse {
+	resp := new(CommonResponse)
+	resp.Data = new(SpecialLogisticsData)
+	return resp
+}

+ 14 - 0
model/http_model/SignForSpecialLogisticRequest.go

@@ -0,0 +1,14 @@
+package http_model
+
+type SignForSpecialLogisticRequest struct {
+	TaskId string `json:"task_id"`
+}
+
+func NewSignForSpecialLogisticRequest() *SignForSpecialLogisticRequest {
+	return new(SignForSpecialLogisticRequest)
+}
+
+func NewSignForSpecialLogisticResponse() *CommonResponse {
+	resp := new(CommonResponse)
+	return resp
+}

+ 1 - 0
model/http_model/logistic_number_info.go

@@ -20,6 +20,7 @@ type GetLogisticsNumberInfoData struct {
 func NewGetLogisticsNumberInfoRequest() *GetLogisticsNumberInfoRequest {
 	return new(GetLogisticsNumberInfoRequest)
 }
+
 func NewGetLogisticsNumberInfoResponse() *CommonResponse {
 	resp := new(CommonResponse)
 	resp.Data = new(GetLogisticsNumberInfoData)

+ 5 - 0
redis/init.go

@@ -1,6 +1,7 @@
 package redis
 
 import (
+	"context"
 	"fmt"
 	"youngee_m_api/model/system_model"
 
@@ -17,3 +18,7 @@ func Init(config *system_model.Redis) {
 	})
 	client = rdb
 }
+
+func GetRedisClient(ctx context.Context) *redis.Client {
+	return client.WithContext(ctx)
+}

+ 2 - 0
route/init.go

@@ -89,6 +89,8 @@ func InitRoute(r *gin.Engine) {
 		m.POST("/project/getSpecialInviteNumber", handler.WrapGetSpecialInviteNumberHandler)       // 查询专项任务邀请管理任务数量
 		m.POST("/project/getSpecialLogisticNumber", handler.WrapGetSpecialLogisticNumberHandler)   // 查询专项任务发货管理任务数量
 		m.POST("/project/getSpecialLogisticList", handler.WrapGetSpecialLogisticListHandler)       // 查询专项任务发货管理任务列表
+		m.POST("/project/createSpecialLogistics", handler.WrapCreateSpecialLogisticsHandler)       // 创建专项创建物流信息
+		m.POST("/project/signForSpecialLogistic", handler.WrapSignForSpecialLogisticHandler)       // 签收专项创建物流订单
 		m.POST("/project/getSpecialReviewNumber", handler.WrapGetSpecialReviewNumberHandler)       // 查询专项任务审稿管理任务数量
 		m.POST("/project/getSpecialLinkNumber", handler.WrapGetSpecialLinkNumberHandler)           // 查询专项任务发布管理任务数量
 		m.POST("/project/getSpecialDataNumber", handler.WrapGetSpecialDataNumberHandler)           // 查询专项任务数据管理任务数量

+ 143 - 3
service/logistics.go

@@ -16,10 +16,9 @@ import (
 
 var Logistics *logistics
 
-type logistics struct {
-}
+type logistics struct{}
 
-// Create 物流信息表插入记录
+// Create 全流程物流信息表插入记录
 func (*logistics) Create(ctx context.Context, newLogistics http_model.CreateLogisticsRequest) (*http_model.CreateLogisticsData, error) {
 	ThingsType := newLogistics.ThingsType
 	StrategyID := newLogistics.StrategyID
@@ -115,6 +114,63 @@ func (*logistics) Create(ctx context.Context, newLogistics http_model.CreateLogi
 	return res, nil
 }
 
+func (l *logistics) CreateSpecialLogistics(ctx context.Context, newLogistics http_model.CreateSpecialLogisticsRequest) (*http_model.SpecialLogisticsData, error) {
+	ThingsType := newLogistics.ThingsType
+	Logistics := gorm_model.YoungeeTaskLogistics{
+		LogisticsID:           newLogistics.LogisticsID,
+		TaskID:                newLogistics.TaskID,
+		ThingsType:            int64(ThingsType),
+		ExplorestoreStarttime: time.Now(),
+		ExplorestoreEndtime:   time.Now(),
+		DeliveryTime:          time.Now(),
+	}
+	//实物
+	if ThingsType == 1 {
+		Logistics.CompanyName = newLogistics.CompanyName
+		Logistics.LogisticsNumber = newLogistics.LogisticsNumber
+		Logistics.DeliveryTime = time.Now()
+	} else if ThingsType == 3 {
+		ExplorestoreStarttime, _ := time.ParseInLocation("2006-01-02 15:04:05", newLogistics.ExplorestoreStarttime, time.Local)
+		ExplorestoreEndtime, _ := time.ParseInLocation("2006-01-02 15:04:05", newLogistics.ExplorestoreEndtime, time.Local)
+		Logistics.ExplorestoreStarttime = ExplorestoreStarttime
+		Logistics.ExplorestoreEndtime = ExplorestoreEndtime
+	} else {
+		Logistics.CouponCodeInformation = newLogistics.CouponCodeInformation
+	}
+	logisticsID, err := db.CreateLogistics(ctx, Logistics, 0)
+	if err != nil {
+		logrus.WithContext(ctx).Errorf("[logistics service] call CreateLogistics error,err:%+v", err)
+		return nil, err
+	}
+	// 修改task_info中发货时间
+	err = db.UpdateLogisticsDate(ctx, Logistics.TaskID)
+	if err != nil {
+		logrus.WithContext(ctx).Errorf("[logistics service] call UpdateLogisticsDate error,err:%+v", err)
+		return nil, err
+	}
+	// 修改task_info中任务阶段
+	err = db.UpdateTaskStageByTaskId(ctx, Logistics.TaskID, 2, 5) //修改为待传初稿
+	if err != nil {
+		logrus.WithContext(ctx).Errorf("[logistics service] call UpdateLogisticsDate error,err:%+v", err)
+		return nil, err
+	}
+	// 记录任务日志-发货
+	err = db.CreateTaskLog(ctx, Logistics.TaskID, "发货时间")
+	if err != nil {
+		logrus.WithContext(ctx).Errorf("[logistics service] call CreateTaskLog error,err:%+v", err)
+		return nil, err
+	}
+	err = db.CreateMessageByTaskId(ctx, 8, 2, Logistics.TaskID)
+	if err != nil {
+		logrus.WithContext(ctx).Errorf("[logistics service] call CreateMessageByTaskId error,err:%+v", err)
+		return nil, err
+	}
+	res := &http_model.SpecialLogisticsData{
+		LogisticsID: *logisticsID,
+	}
+	return res, nil
+}
+
 // 修改物流信息表
 func (*logistics) Update(ctx context.Context, newLogistics http_model.CreateLogisticsRequest) (*http_model.CreateLogisticsData, error) {
 	ThingsType := newLogistics.ThingsType
@@ -149,6 +205,37 @@ func (*logistics) Update(ctx context.Context, newLogistics http_model.CreateLogi
 	return res, nil
 }
 
+func (*logistics) UpdateSpecialLogistics(ctx context.Context, newLogistics http_model.CreateSpecialLogisticsRequest) (*http_model.SpecialLogisticsData, error) {
+	ThingsType := newLogistics.ThingsType
+	Logistics := gorm_model.YoungeeTaskLogistics{
+		LogisticsID:  newLogistics.LogisticsID,
+		TaskID:       newLogistics.TaskID,
+		ThingsType:   int64(ThingsType),
+		DeliveryTime: time.Now(),
+	}
+	//实物
+	if ThingsType == 1 {
+		Logistics.CompanyName = newLogistics.CompanyName
+		Logistics.LogisticsNumber = newLogistics.LogisticsNumber
+	} else if ThingsType == 3 {
+		ExplorestoreStarttime, _ := time.ParseInLocation("2006-01-02 15:04:05", newLogistics.ExplorestoreStarttime, time.Local)
+		ExplorestoreEndtime, _ := time.ParseInLocation("2006-01-02 15:04:05", newLogistics.ExplorestoreEndtime, time.Local)
+		Logistics.ExplorestoreStarttime = ExplorestoreStarttime
+		Logistics.ExplorestoreEndtime = ExplorestoreEndtime
+	} else {
+		Logistics.CouponCodeInformation = newLogistics.CouponCodeInformation
+	}
+	logisticsID, err := db.UpdateLogistics(ctx, Logistics)
+	if err != nil {
+		logrus.WithContext(ctx).Errorf("[logistics service] call UpdateLogistics error,err:%+v", err)
+		return nil, err
+	}
+	res := &http_model.SpecialLogisticsData{
+		LogisticsID: *logisticsID,
+	}
+	return res, nil
+}
+
 // 签收
 func (*logistics) SignForReceipt(ctx *gin.Context, data http_model.SignForReceiptRequest) interface{} {
 	projectId, err1 := db.GetProjectIdByTaskId(ctx, data.TaskStrategyIds[0].TaskId)
@@ -217,6 +304,59 @@ func (*logistics) SignForReceipt(ctx *gin.Context, data http_model.SignForReceip
 			return err
 		}
 	}
+	return nil
+}
 
+func (l *logistics) SignForSpecialLogistic(ctx *gin.Context, logisticRequest http_model.SignForSpecialLogisticRequest) error {
+	projectId, err1 := db.GetProjectIdByTaskId(ctx, logisticRequest.TaskId)
+	if err1 != nil {
+		logrus.WithContext(ctx).Errorf("[project service] call GetProjectIdByTaskId error,err:%+v", err1)
+		return err1
+	}
+	// 签收时更新任务阶段
+	project, err3 := db.GetProjectDetail(ctx, *projectId)
+	if err3 != nil {
+		logrus.WithContext(ctx).Errorf("[project service] call GetPorjectDetail error,err:%+v", err3)
+		return err3
+	}
+	if project.ContentType == 1 {
+		err := db.UpdateTaskStageByProjectId(ctx, *projectId, 2, 9)
+		if err != nil {
+			logrus.WithContext(ctx).Errorf("[projectPay service] call UpdateTaskStatusPaying error,err:%+v", err)
+			return err
+		}
+	} else {
+		err := db.UpdateTaskStageByProjectId(ctx, *projectId, 2, 7)
+		if err != nil {
+			logrus.WithContext(ctx).Errorf("[projectPay service] call UpdateTaskStatusPaying error,err:%+v", err)
+			return err
+		}
+	}
+
+	taskId := logisticRequest.TaskId
+	err := db.UpdateLogisticsStatus(ctx, taskId, 3)
+	if err != nil {
+		logrus.WithContext(ctx).Errorf("[project service] call UpdateLogisticsStatus error,err:%+v", err)
+		return err
+	}
+
+	// 签收时间
+	err = db.SignForReceipt(ctx, taskId)
+	if err != nil {
+		logrus.WithContext(ctx).Errorf("[project service] call SignForReceipt error,err:%+v", err)
+		return err
+	}
+
+	// 记录任务日志
+	err = db.CreateTaskLog(ctx, taskId, "签收时间")
+	if err != nil {
+		logrus.WithContext(ctx).Errorf("[logistics service] call CreateTaskLog error,err:%+v", err)
+		return err
+	}
+	err = db.CreateMessageByTaskId(ctx, 9, 2, taskId)
+	if err != nil {
+		logrus.WithContext(ctx).Errorf("[logistics service] call CreateMessageByTaskId error,err:%+v", err)
+		return err
+	}
 	return nil
 }

+ 0 - 1
service/number_info.go

@@ -122,7 +122,6 @@ func (*number) GetSpecialLogisticNumber(ctx context.Context, request http_model.
 		logrus.WithContext(ctx).Errorf("[Data service] call GetSpecialLogisticNumber error,err:%+v", err)
 		return nil, err
 	}
-
 	return NumberData, nil
 }
 

+ 33 - 0
service/wechatPay.go

@@ -2,12 +2,15 @@ package service
 
 import (
 	"context"
+	"fmt"
+	//"github.com/go-redis/redis/v8"
 	"github.com/wechatpay-apiv3/wechatpay-go/core"
 	"github.com/wechatpay-apiv3/wechatpay-go/core/option"
 	"github.com/wechatpay-apiv3/wechatpay-go/services/payments/native"
 	"github.com/wechatpay-apiv3/wechatpay-go/utils"
 	"log"
 	"time"
+	"youngee_m_api/redis"
 )
 
 func NativeApiServicePrepay(tradeId string, amount int64) (codeUrl string, err error) {
@@ -64,6 +67,36 @@ func NativeApiServicePrepay(tradeId string, amount int64) (codeUrl string, err e
 	return *resp.CodeUrl, nil
 }
 
+func handleCallbackOnce(orderNo string, lockTimeout time.Duration) (status string, err error) {
+	redisClient := redis.GetRedisClient(context.Background())
+	// 定义锁的名称
+	lockName := fmt.Sprintf("lock_%s", orderNo)
+
+	// 获取分布式锁
+	lock, err := redisClient.SetNX(context.Background(), lockName, "locked", lockTimeout).Result()
+	if err != nil {
+		panic(err)
+	}
+
+	if lock {
+		// 成功获取锁,执行回调函数
+		status, err = QueryOrderByOutTradeNo(orderNo)
+		if err != nil {
+			return
+		}
+
+		// 释放锁
+		_, err = redisClient.Del(context.Background(), lockName).Result()
+		if err != nil {
+			panic(err)
+		}
+	} else {
+		// 获取锁失败,说明该笔订单正在被其他进程处理
+		fmt.Printf("Order %s is being processed, skip\n", orderNo)
+	}
+	return status, nil
+}
+
 func QueryOrderByOutTradeNo(tradeId string) (status string, err error) {
 
 	var (