123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- package user_merchant
- import (
- "context"
- "gd_management/apis"
- "gd_management/common.in/storage"
- "gd_management/common.in/utils"
- "gd_management/errors"
- "github.com/astaxie/beego/orm"
- "go.uber.org/zap"
- "time"
- )
- func ManagementAddMerchantH5Service(ctx context.Context, req *apis.ManagementAddMerchantH5ServiceReq, reply *apis.ManagementAddMerchantH5ServiceReply) (err error) {
- task := func(o orm.Ormer) error {
- now := time.Now()
- nowString := now.Format("2006-01-02 15:04:05")
- nowSecond := now.Unix()
- //1 获取套餐详情,判断是否是有效套餐
- comTab := apis.TGdH5ServiceCombo{}
- err := o.QueryTable("t_gd_h5_service_combo").Filter("id", req.ComboId).One(&comTab)
- if err != nil {
- if err == orm.ErrNoRows {
- return errors.DataComboNotExist
- }
- return errors.DataBaseError
- }
- if comTab.Type == 0 {
- exist := o.QueryTable("t_gd_h5_service_order").Filter("merchant_id", req.MerchantId).Filter("h5_service_combo_id", req.ComboId).Exist()
- if exist {
- return errors.OrderFreeExist
- }
- }
- orderTab := apis.TGdH5ServiceOrder{
- MerchantId: req.MerchantId,
- H5ServiceComboId: req.ComboId,
- ThemeColor: req.ThemeColor,
- Status: 2,
- CreateTime: nowString,
- }
- _, err = o.Insert(&orderTab)
- if err != nil {
- return errors.DataBaseError
- }
- mh5Tab := apis.TGdH5ServiceMerchant{}
- err = o.QueryTable("t_gd_h5_service_merchant").Filter("merchant_id", req.MerchantId).Filter("h5_service_id", comTab.H5ServiceId).One(&mh5Tab)
- if err != nil {
- if err != orm.ErrNoRows {
- return errors.DataBaseError
- }
- }
- comboType := 0
- if err == nil {
- if mh5Tab.EndTime == 0 {
- comboType = 1
- } else {
- comboType = 2
- }
- if comboType != comTab.Type && mh5Tab.State == 0 {
- return errors.CanNotAddOrder
- }
- //存在相同api 只更新count, day_per_count, start_time, end_time, combo type, state字段
- mh5Tab.State = 0
- if comTab.Type == 1 {
- //套餐已过期 或 以前套餐为按次数,目标套餐为按次数,累加count即可
- mh5Tab.Count += int(comTab.Count)
- mh5Tab.StartTime = 0
- mh5Tab.EndTime = 0
- } else if comboType == 1 {
- //套餐已过期且之前是按次数,目标套餐为按天,根据当前时间更新时间
- mh5Tab.Count = int(comTab.Count)
- mh5Tab.StartTime = nowSecond
- mh5Tab.EndTime = nowSecond + int64(comTab.TotalDay*(24*3600))
- } else {
- //套餐未过期且之前是按天,目标套餐为按天,但单天次数不一样,不允许添加
- if mh5Tab.State == 0 && int(comTab.Count) != mh5Tab.Count {
- return errors.DayComboCountMisMatch
- }
- mh5Tab.Count = int(comTab.Count)
- if mh5Tab.State == 1 {
- //套餐已过期且之前是按天,目标套餐为按天,且前后单天次数一样, 根据当前时间更新时间
- mh5Tab.EndTime = nowSecond + int64(comTab.TotalDay*(24*3600))
- } else {
- //套餐未过期且之前是按天,目标套餐为按天,且前后单天次数一样, 根据以前end时间更新时间
- mh5Tab.EndTime += int64(comTab.TotalDay * (24 * 3600))
- }
- }
- _, err := o.Update(&mh5Tab)
- if err != nil {
- return errors.DataBaseError
- }
- return nil
- }
- //插入商户h5 service
- mh5Tab = apis.TGdH5ServiceMerchant{}
- mh5Tab.MerchantId = req.MerchantId
- mh5Tab.H5ServiceId = comTab.H5ServiceId
- if comTab.Type == 1 || comTab.Type == 0 {
- //按次数
- mh5Tab.Count = int(comTab.Count)
- mh5Tab.StartTime = 0
- mh5Tab.EndTime = 0
- }
- if comTab.Type == 2 {
- mh5Tab.Count = int(comTab.Count)
- mh5Tab.StartTime = nowSecond
- mh5Tab.EndTime = nowSecond + int64(comTab.TotalDay*(24*3600))
- }
- mh5Tab.CreateTime = nowString
- mh5Id, err := o.Insert(&mh5Tab)
- if err != nil {
- l.Error("mysql",
- zap.String("sql", "insert t_gd_h5_service_merchant"),
- zap.String("fields", utils.MarshalJsonString(mh5Tab)),
- zap.String("error", err.Error()))
- return errors.DataBaseError
- }
- reply.MerchantH5ServiceId = mh5Id
- return nil
- }
- tasks := []storage.DbaTasker{}
- tasks = append(tasks, storage.GenerateDbaTask(task))
- storage.ExecTrans(tasks...)
- l.Debug(utils.MarshalJsonString(req, reply))
- return nil
- }
|