123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- 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 ManagementPayOrder(ctx context.Context, req *apis.ManagementPayOrderReq, reply *apis.ManagementPayOrderReply) (err error) {
- task := func(o orm.Ormer) error {
- var status int
- now := time.Now()
- nowString := now.Format("2006-01-02 15:04:05")
- nowSecond := now.Unix()
- err := o.Raw("select status from t_gd_data_api_order where id=?", req.OrderId).QueryRow(&status)
- if err != nil {
- return errors.DataBaseError
- }
- if status == 2 {
- return errors.OrderPayed
- }
- //获取套餐信息
- var merchantId, comboId int64
- err = o.Raw("select merchant_id, combo_id from t_gd_data_api_order where id=?", req.OrderId).QueryRow(&merchantId, &comboId)
- if err != nil {
- l.Error("mysql",
- zap.String("sql", "select merchant_id, combo_id from t_data_api_order where id=?"),
- zap.String("fields", fmt.Sprintf("id:%d", req.OrderId)),
- zap.String("error", err.Error()))
- if err == orm.ErrNoRows {
- return errors.DataBaseNoRecord
- }
- return errors.DataBaseError
- }
- comTab := apis.DataCombo{}
- err = o.QueryTable("t_gd_data_api_combo").Filter("id", 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", comboId)),
- zap.String("error", err.Error()))
- if err == orm.ErrNoRows {
- return errors.DataBaseNoRecord
- }
- return errors.DataBaseError
- }
- //插入商户数据api
- mdaTab := apis.TGdMerchantDataApi{}
- err = o.QueryTable("t_gd_merchant_data_api").Filter("merchant_id", merchantId).Filter("query_type_id", comTab.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
- }
- _, err = o.Raw("update t_gd_data_api_order set status=? where id=?", 2, req.OrderId).Exec()
- if err != nil {
- return errors.DataBaseError
- }
- return nil
- }
- mdaTab = apis.TGdMerchantDataApi{}
- mdaTab.ComboType = comTab.Type
- mdaTab.MerchantId = merchantId
- mdaTab.QueryTypeId = comTab.QueryTypeId
- if comTab.Type == 1 {
- //按次数
- 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, comTab.QueryTypeId).QueryRow(&mdaTab.UnitPrice)
- if err != nil {
- l.Error("mysql",
- zap.String("sql", sql),
- zap.String("fields", fmt.Sprintf("id:%d", comTab.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", comTab.QueryTypeId).All(&cdaTabs)
- if err != nil {
- l.Error("mysql",
- zap.String("sql", "select t_gd_child_data_api"),
- zap.String("fields", fmt.Sprintf("query_type_id:%d", comTab.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.ProviderApis = v.ProviderApis
- mcdaTab.ForceUpdate = v.ForceUpdate
- mcdaTab.IsCrypto = v.IsCrypto
- mcdaTab.CreateTime = nowString
- mcdaTab.UpdateTime = nowString
- mcdaTab.IsEnable = true //TODO 从内存中读取
- mcdaTab.ReuseTime = v.ReuseTime
- //mcdaTabs = append(mcdaTabs, mcdaTab)
- 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
- }*/
- _, err = o.Raw("update t_gd_data_api_order set status=? where id=?", 2, req.OrderId).Exec()
- if err != nil {
- 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
- }
|