package user_merchant import ( "context" "gd_management/apis" "gd_management/common.in/storage" "gd_management/common.in/utils" "gd_management/errors" "gd_management/impl/base_api" "fmt" "github.com/astaxie/beego/orm" "go.uber.org/zap" "time" ) func ManagementAddMerchantDataApi(ctx context.Context, req *apis.ManagementAddMerchantDataApiReq, reply *apis.ManagementAddMerchantDataApiReply) (err error) { task := func(o orm.Ormer) error { now := time.Now() nowString := now.Format("2006-01-02 15:04:05") nowSecond := now.Unix() var queryIsFree bool sql := "select is_free from t_gd_data_api_query_type where id=?" err = o.Raw(sql, req.QueryTypeId).QueryRow(&queryIsFree) if err != nil { l.Error("mysql", zap.String("sql", sql), zap.String("fields", fmt.Sprintf("id:%d", req.QueryTypeId)), zap.String("error", err.Error())) if err == orm.ErrNoRows { return errors.QueryTypeNotExist } return errors.DataBaseError } comTab := apis.DataCombo{} if queryIsFree == false { err := o.QueryTable("t_gd_data_api_combo").Filter("id", req.ComboId).One(&comTab) if err != nil { l.Error("mysql", zap.String("sql", "select from t_gd_data_api_combo"), zap.String("fields", fmt.Sprintf("id:%d", req.ComboId)), zap.String("error", err.Error())) if err == orm.ErrNoRows { return errors.DataComboNotExist } return errors.DataBaseError } } if queryIsFree == true || comTab.Type == 0 { exist := o.QueryTable("t_gd_data_api_order").Filter("merchant_id", req.MerchantId).Filter("query_type_id", req.QueryTypeId).Filter("combo_id", req.ComboId).Exist() if exist { return errors.OrderFreeExist } } orderTab := apis.TGdDataApiOrder{ MerchantId: req.MerchantId, ComboId: req.ComboId, QueryTypeId: req.QueryTypeId, GoodsPrice: comTab.Price, Status: 2, CreateTime: nowString, } _, err = o.Insert(&orderTab) if err != nil { l.Error("mysql", zap.String("sql", sql), zap.String("fields", fmt.Sprintf("%d %d %f %d %s", req.MerchantId, req.ComboId, comTab.Price, 2, nowString)), zap.String("error", err.Error())) return errors.DataBaseError } mdaTab := apis.TGdMerchantDataApi{} err = o.QueryTable("t_gd_merchant_data_api").Filter("merchant_id", req.MerchantId).Filter("query_type_id", req.QueryTypeId).One(&mdaTab) if err != nil { if err != orm.ErrNoRows { return errors.DataBaseError } } if err == nil { if mdaTab.ComboType == 0 { if mdaTab.EndTime == 0 { mdaTab.ComboType = 1 } else { mdaTab.ComboType = 2 } } if mdaTab.ComboType != comTab.Type && mdaTab.State == 0 { return errors.CanNotAddOrder } //存在相同api 只更新count, day_per_count, start_time, end_time, combo type, state字段 mdaTab.State = 0 mdaTab.UpdateTime = nowString if comTab.Type == 1 { //套餐已过期 或 以前套餐为按次数,目标套餐为按次数,累加count即可 mdaTab.Count += int(comTab.Count) mdaTab.StartTime = 0 mdaTab.EndTime = 0 mdaTab.CountPerDay = 0 } else if mdaTab.ComboType == 1 { //套餐已过期且之前是按次数,目标套餐为按天,根据当前时间更新时间 mdaTab.Count = 0 mdaTab.CountPerDay = int(comTab.Count) mdaTab.StartTime = nowSecond mdaTab.EndTime = nowSecond + int64(comTab.TotalDay*(24*3600)) } else { //套餐未过期且之前是按天,目标套餐为按天,但单天次数不一样,不允许添加 if mdaTab.State == 0 && int(comTab.Count) != mdaTab.CountPerDay { return errors.DayComboCountMisMatch } mdaTab.Count = 0 mdaTab.CountPerDay = int(comTab.Count) if mdaTab.State == 1 { //套餐已过期且之前是按天,目标套餐为按天,且前后单天次数一样, 根据当前时间更新时间 mdaTab.EndTime = nowSecond + int64(comTab.TotalDay*(24*3600)) } else { //套餐未过期且之前是按天,目标套餐为按天,且前后单天次数一样, 根据以前end时间更新时间 mdaTab.EndTime += int64(comTab.TotalDay * (24 * 3600)) } } mdaTab.ComboType = comTab.Type _, err := o.Update(&mdaTab) if err != nil { return errors.DataBaseError } return nil } //插入商户数据api mdaTab = apis.TGdMerchantDataApi{} mdaTab.MerchantId = req.MerchantId mdaTab.QueryTypeId = req.QueryTypeId if comTab.Type == 1 || comTab.Type == 0 { //按次数 mdaTab.Count = int(comTab.Count) mdaTab.StartTime = 0 mdaTab.EndTime = 0 } if comTab.Type == 2 { mdaTab.CountPerDay = int(comTab.Count) mdaTab.StartTime = nowSecond mdaTab.EndTime = nowSecond + int64(comTab.TotalDay*(24*3600)) } mdaTab.CreateTime = nowString mdaTab.UpdateTime = nowString sql = "select unit_price from t_gd_data_api_query_type where id=?" err = o.Raw(sql, req.QueryTypeId).QueryRow(&mdaTab.UnitPrice) if err != nil { l.Error("mysql", zap.String("sql", sql), zap.String("fields", fmt.Sprintf("id:%d", req.QueryTypeId)), zap.String("error", err.Error())) return errors.DataBaseError } mdaId, err := o.Insert(&mdaTab) if err != nil { l.Error("mysql", zap.String("sql", "insert t_gd_merchant_data_api"), zap.String("fields", utils.MarshalJsonString(mdaTab)), zap.String("error", err.Error())) return errors.DataBaseError } //插入商户基础api //mcdaTabs := []apis.TGdMerchantChildDataApi{} cdaTabs := []apis.ManagementDataApiBaseApi{} _, err = o.QueryTable("t_gd_child_data_api").Filter("query_type_id", req.QueryTypeId).All(&cdaTabs) if err != nil { l.Error("mysql", zap.String("sql", "select t_gd_child_data_api"), zap.String("fields", fmt.Sprintf("data_api_query_type_id:%d", req.QueryTypeId)), zap.String("error", err.Error())) return errors.DataBaseError } for _, v := range cdaTabs { mcdaTab := apis.TGdMerchantChildDataApi{} mcdaTab.MerchantDataApiId = int64(mdaId) mcdaTab.ApiId = v.ApiId mcdaTab.SaleName = v.SaleName mcdaTab.CountType = v.CountType mcdaTab.CountCode = v.CountCode mcdaTab.RequestParam = v.RequestParam mcdaTab.ResponseParam = v.ResponseParam mcdaTab.ForceUpdate = v.ForceUpdate mcdaTab.IsCrypto = v.IsCrypto mcdaTab.CreateTime = nowString mcdaTab.UpdateTime = nowString mcdaTab.ReuseTime = v.ReuseTime //mcdaTab.IsEnable = true err := o.Raw("select enable from t_gd_api where id = ?", v.ApiId).QueryRow(&mcdaTab.IsEnable) if err != nil { return errors.DataBaseError } id, err := o.Insert(&mcdaTab) if err != nil { return errors.DataBaseError } mReq := apis.ManagementGetBaseApiProviderListReq{ApiId: v.ApiId} mReply := apis.ManagementGetBaseApiProviderListReply{} base_api.ManagementGetBaseApiProviderList(ctx, &mReq, &mReply) merchantProviderRelatons := []apis.MerchantApiProviderRelation{} for _, group := range mReply.ProviderGroupList { for _, providerApi := range group.ProviderApiList { merchantProviderRelaton := apis.MerchantApiProviderRelation{} merchantProviderRelaton.ProviderApiId = providerApi.ProviderApiId merchantProviderRelaton.MerchantChildApiId = id merchantProviderRelaton.Enable = true merchantProviderRelaton.GroupNo = group.GroupNo merchantProviderRelatons = append(merchantProviderRelatons, merchantProviderRelaton) } } if len(merchantProviderRelatons) > 0 { _, err = o.InsertMulti(len(merchantProviderRelatons), &merchantProviderRelatons) if err != nil { return errors.DataBaseError } } } /* _, err = o.InsertMulti(len(mcdaTabs), &mcdaTabs) if err != nil { l.Error("mysql", zap.String("sql", "insert t_gd_merchant_child_data_api"), zap.String("fields", utils.MarshalJsonString(mcdaTabs)), zap.String("error", err.Error())) return errors.DataBaseError } */ return nil } tasks := []storage.DbaTasker{} tasks = append(tasks, storage.GenerateDbaTask(task)) storage.ExecTrans(tasks...) l.Debug(utils.MarshalJsonString(req, reply)) return nil }