|
@@ -0,0 +1,122 @@
|
|
|
+package db
|
|
|
+
|
|
|
+import (
|
|
|
+ "context"
|
|
|
+ "fmt"
|
|
|
+ "github.com/caixw/lib.go/conv"
|
|
|
+ "github.com/sirupsen/logrus"
|
|
|
+ "reflect"
|
|
|
+ "time"
|
|
|
+ "youngee_m_api/model/common_model"
|
|
|
+ "youngee_m_api/model/gorm_model"
|
|
|
+ "youngee_m_api/model/http_model"
|
|
|
+ "youngee_m_api/util"
|
|
|
+)
|
|
|
+
|
|
|
+func CreatePricingStrategy(ctx context.Context, req *http_model.AddPricingRequest) (string, string, error) {
|
|
|
+ db := GetReadDB(ctx)
|
|
|
+ project_type, fee_form, platform, fans_low, fans_up, service_charge, base_offer := req.ProjectType, req.ManuscriptForm, req.Platform, req.FansLow, req.FansHigh, req.PlatformFee, req.BaseOffer
|
|
|
+ var IsExclusive gorm_model.InfoPricingStrategy
|
|
|
+ err := db.Debug().Where(" fee_form = ? && platform = ? && fans_low <= ? && fans_up >= ?", fee_form, platform, fans_low, fans_up).First(&IsExclusive).Error
|
|
|
+ if err == nil {
|
|
|
+ return "该策略与已经运行的策略互斥,请修改相关字段后重新创建", "", nil
|
|
|
+ }
|
|
|
+ var newStrategy gorm_model.InfoPricingStrategy
|
|
|
+ var strategyInfo gorm_model.InfoPricingStrategy
|
|
|
+ getMonth := time.Now().Format("01") //获取月
|
|
|
+ getDay := time.Now().Day() //获取日
|
|
|
+ dayString := ""
|
|
|
+ if getDay < 10 {
|
|
|
+ dayString = conv.MustString(getDay, "")
|
|
|
+ dayString = "0" + dayString
|
|
|
+ } else {
|
|
|
+ dayString = conv.MustString(getDay, "")
|
|
|
+ }
|
|
|
+ monthAndDay := conv.MustString(getMonth, "") + dayString
|
|
|
+ err = db.Debug().Where(fmt.Sprintf(" strategyId like '%s%%'", monthAndDay)).Last(&strategyInfo).Error
|
|
|
+
|
|
|
+ num := 1
|
|
|
+ if strategyInfo.StrategyId != "" {
|
|
|
+ num = conv.MustInt(strategyInfo.StrategyId[4:6], 0)
|
|
|
+ num = num + 1 //获取日
|
|
|
+ } else {
|
|
|
+ num = 1
|
|
|
+ }
|
|
|
+ numString := ""
|
|
|
+ if num < 10 {
|
|
|
+ numString = conv.MustString(num, "")
|
|
|
+ numString = "0" + numString
|
|
|
+ } else {
|
|
|
+ numString = conv.MustString(num, "")
|
|
|
+ }
|
|
|
+ newStrategy.StrategyId = monthAndDay + numString
|
|
|
+ newStrategy.Status = 0
|
|
|
+ newStrategy.FansLow = fans_low
|
|
|
+ newStrategy.FansUp = fans_up
|
|
|
+ newStrategy.ProjectType = conv.MustInt64(project_type, 0)
|
|
|
+ newStrategy.BaseOffer = conv.MustInt64(base_offer, 0)
|
|
|
+ newStrategy.Platform = conv.MustInt64(platform, 0)
|
|
|
+ newStrategy.FeeForm = conv.MustInt64(fee_form, 0)
|
|
|
+ newStrategy.ServiceCharge = conv.MustInt64(service_charge, 0)
|
|
|
+ newStrategy.CreateAt = time.Now()
|
|
|
+ newStrategy.UpdateAt = time.Now()
|
|
|
+ err = db.Create(&newStrategy).Error
|
|
|
+ if err != nil {
|
|
|
+ return "创建失败", "", err
|
|
|
+ }
|
|
|
+ return "创建成功", monthAndDay + numString, nil
|
|
|
+}
|
|
|
+
|
|
|
+func SearchPricing(ctx context.Context, pageSize, pageNum int32, conditions *common_model.PricingConditions) ([]*gorm_model.InfoPricingStrategy, int64, error) {
|
|
|
+ db := GetReadDB(ctx)
|
|
|
+ db = db.Debug().Model(gorm_model.InfoPricingStrategy{})
|
|
|
+ // 根据 查询条件过滤
|
|
|
+ conditionType := reflect.TypeOf(conditions).Elem()
|
|
|
+ conditionValue := reflect.ValueOf(conditions).Elem()
|
|
|
+ for i := 0; i < conditionType.NumField(); i++ {
|
|
|
+ field := conditionType.Field(i)
|
|
|
+ tag := field.Tag.Get("condition")
|
|
|
+ value := conditionValue.FieldByName(field.Name)
|
|
|
+ if !util.IsBlank(value) && tag != "update_at" {
|
|
|
+ db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
|
|
|
+ } else if tag == "created_at" && value.Interface() != nil {
|
|
|
+ db = db.Where(fmt.Sprintf("update_at like '%s%%'", value.Interface()))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 查询总数
|
|
|
+ var total int64
|
|
|
+ var PricingDatas []*gorm_model.InfoPricingStrategy
|
|
|
+ if err := db.Count(&total).Error; err != nil {
|
|
|
+ logrus.WithContext(ctx).Errorf("[SearchPricing] error query mysql total, err:%+v", err)
|
|
|
+ return nil, 0, err
|
|
|
+ }
|
|
|
+ // 查询该页数据
|
|
|
+ limit := pageSize
|
|
|
+ offset := pageSize * pageNum // assert pageNum start with 0
|
|
|
+ err := db.Order("update_at desc").Limit(int(limit)).Offset(int(offset)).Find(&PricingDatas).Error
|
|
|
+ if err != nil {
|
|
|
+ logrus.WithContext(ctx).Errorf("[SearchPricing] error query mysql find, err:%+v", err)
|
|
|
+ return nil, 0, err
|
|
|
+ }
|
|
|
+ return PricingDatas, total, nil
|
|
|
+}
|
|
|
+
|
|
|
+func ModifyPricing(ctx context.Context, req *http_model.ModifyPricingRequest) (string, error) {
|
|
|
+ db := GetReadDB(ctx)
|
|
|
+ strategyId := req.StrategyId
|
|
|
+ db = db.Debug().Model(gorm_model.InfoPricingStrategy{}).Where("strategyId = ?", strategyId)
|
|
|
+ project_type, fee_form, platform, fans_low, fans_up, service_charge, base_offer := req.ProjectType, req.FeeForm, req.Platform, req.FansLow, req.FansHigh, req.PlatformFee, req.BaseOffer
|
|
|
+ var IsExclusive gorm_model.InfoPricingStrategy
|
|
|
+ err := db.Debug().Where(" fee_form = ? && platform = ? && fans_low <= ? && fans_up >= ?", fee_form, platform, fans_low, fans_up).First(&IsExclusive).Error
|
|
|
+ if err == nil {
|
|
|
+ return "要修改策略与已经运行的策略互斥,请修改相关字段", nil
|
|
|
+ }
|
|
|
+ err = db.Updates(map[string]interface{}{
|
|
|
+ "project_type": project_type, "base_offer": base_offer, "fee_form": fee_form,
|
|
|
+ "platform": platform, "fans_low": fans_low, "fans_up": fans_up, "service_charge": service_charge,
|
|
|
+ }).Error
|
|
|
+ if err != nil {
|
|
|
+ return "", err
|
|
|
+ }
|
|
|
+ return "修改成功", nil
|
|
|
+}
|