123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- package order
- import (
- "context"
- "errors"
- "fmt"
- "github.com/gogf/gf/v2/database/gdb"
- "github.com/gogf/gf/v2/errors/gerror"
- "github.com/gogf/gf/v2/frame/g"
- "github.com/gogf/gf/v2/util/gconv"
- "jyOrderManager/internal/consts"
- "jyOrderManager/internal/jyutil"
- "jyOrderManager/internal/logic/product"
- "jyOrderManager/internal/model"
- "log"
- "time"
- )
- func ServiceOpen(ctx context.Context, param model.OrderOpenServiceParams) error {
- //查询订单
- if !consts.PhoneRegex.MatchString(param.Phone) {
- return errors.New("手机号格式异常")
- }
- orderData, _ := g.DB().GetOne(ctx, fmt.Sprintf("select * from dataexport_order where order_code ='%s' and audit_status =3", param.OrderCode))
- if orderData.IsEmpty() {
- return errors.New("为查询到订单信息")
- }
- if gconv.String(orderData.Map()["user_phone"]) != param.Phone {
- g.DB().Update(ctx, "dataexport_order", map[string]interface{}{
- "user_phone": param.Phone,
- }, map[string]interface{}{
- "order_code": param.OrderCode,
- })
- }
- productDetail, err := g.DB().Ctx(ctx).Query(ctx, fmt.Sprintf(`SELECT * FROM jy_order_detail WHERE order_code ='%s' and is_service_open = 0 and status =1`, param.OrderCode))
- if err != nil || productDetail.IsEmpty() {
- return err
- }
- uData, entId, userPositionId, err := jyutil.GetCreateUserData(param.Phone, gconv.String(orderData.Map()["company_name"]), gconv.Int(orderData.Map()["buy_subject"]) == 2)
- if err != nil {
- return errors.New("用户创建失败")
- }
- if err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
- // 产品服务开通
- for _, m := range productDetail.List() {
- if !jyutil.IsServiceOpen(m) {
- continue
- }
- var openTime time.Time
- if gconv.Time(param.StartTime).Unix() > time.Now().Unix() {
- openTime = gconv.Time(param.StartTime)
- } else {
- openTime = time.Now()
- }
- //参数注入
- m["userMap"] = map[string]interface{}{
- "userData": uData, "entId": entId, "userPositionId": userPositionId,
- }
- m["phone"] = param.Phone
- m["order_code"] = param.OrderCode
- m["reqCompanyName"] = orderData.Map()["company_name"]
- m["amount"] = m["final_price"]
- m["reqSubject"] = orderData.Map()["buy_subject"]
- m["linked_orderId"] = m["linked_detail_id"]
- productCode := gconv.String(m["product_code"])
- pFunc, err := product.JyProFunc.GetProductInitFuncByCode(productCode)
- if err != nil {
- return err
- }
- pObj, err := pFunc(m)
- if err != nil {
- return gerror.Wrap(err, fmt.Sprintf("获取%s商品异常", productCode))
- }
- if err := pObj.OpenService(ctx, openTime); err != nil {
- return err
- }
- }
- if orderUserId := gconv.String(orderData.Map()["user_id"]); orderUserId == "" || orderUserId != gconv.String(uData["userId"]) {
- log.Printf("同步更新订单用户身份:orderUserId:%s,userId:%s,entId:%d\n", orderUserId, uData["userId"], entId)
- upData := g.Map{
- "user_id": uData["userId"],
- }
- if entId > 0 { //企业服务
- upData["ent_id"] = entId
- if personPhone := gconv.String(orderData.Map()["personPhone"]); personPhone != "" {
- jyutil.EndAddUser(ctx, entId, gconv.String(orderData.Map()["user_phone"]), personPhone, gconv.String(orderData.Map()["personName"]))
- }
- }
- //更新订单
- _, err = g.DB().Update(ctx, consts.OrderListTableName, upData, "order_code=?", param.OrderCode)
- if err != nil {
- return err
- }
- }
- return nil
- }); err != nil {
- log.Println(err)
- return err
- }
- return nil
- }
|