package user_merchant import ( "context" "gd_management/apis" "gd_management/common.in/utils" "gd_management/errors" "fmt" "github.com/astaxie/beego/orm" "go.uber.org/zap" "time" ) //var PAGESIZE = 10 func getMerchantH5List(req *apis.ManagementGetMerchantH5ServiceReq, reply *apis.ManagementGetMerchantH5ServiceReply) error { var maps []orm.Params o := orm.NewOrm() err := o.Raw("select count(1) from t_gd_h5_service_merchant where merchant_id=?", req.MerchantId).QueryRow(&reply.Total) if err != nil { l.Error("mysql", zap.String("sql", "select count(1) from t_gd_merchant_data_api"), zap.String("args", utils.MarshalJsonString(req)), zap.String("error", err.Error())) if err == orm.ErrNoRows { return nil } return errors.DataBaseError } sql := "SELECT a.count, a.id, a.state," + "( SELECT service_name FROM t_gd_h5_service WHERE id = a.h5_service_id) AS h5_service_name," + "( SELECT b.count FROM t_gd_h5_service_access_count AS b WHERE b.h5_service_merchant_id = a.id AND create_time =? ) AS total_used_count," + "( SELECT b.count FROM t_gd_h5_service_access_count AS b WHERE b.h5_service_merchant_id = a.id AND create_time = ? ) AS day_used_count," + "a.end_time, a.start_time FROM t_gd_h5_service_merchant as a" + " where a.merchant_id=?" zeroDate := "0000-00-00" nowDate := time.Now().Format("2006-01-02") if req.IsAll == true { _, err = o.Raw(sql, zeroDate, nowDate, req.MerchantId).Values(&maps) } else { sql = fmt.Sprintf("%s limit ? offset ?", sql) _, err = o.Raw(sql, zeroDate, nowDate, req.MerchantId, PAGESIZE, req.PageNumber*PAGESIZE).Values(&maps) } if err != nil { l.Error("mysql", zap.String("sql", sql), zap.String("args", utils.MarshalJsonString(req)), zap.String("error", err.Error())) if err == orm.ErrNoRows { return nil } return errors.DataBaseError } var start_time, end_time int64 for _, v := range maps { item := apis.MerchantH5ServiceItem{} item.H5ServiceName, _ = v["h5_service_name"].(string) item.MerchantH5ServiceId = int64(ormStringToInt(v, "id")) if int64(ormStringToInt(v, "state")) == 0 { item.State = "正常" } else { item.State = "过期" } start_time = ormStringToInt(v, "start_time") end_time = ormStringToInt(v, "end_time") if end_time == 0 && int(ormStringToInt(v, "count")) == 0 { item.ComboType = 0 }else if end_time == 0 { item.ComboType = 1 } else { item.ComboType = 2 } if item.ComboType == 1 { //按总次数 item.TotalCount = int(ormStringToInt(v, "count")) item.RemainCount = item.TotalCount - int(ormStringToInt(v, "total_used_count")) if item.RemainCount <= 0 { item.State = "过期" } } else if item.ComboType == 2 { //按天 item.DayCount = int(ormStringToInt(v, "count")) item.DayRemainCount = item.DayCount - int(ormStringToInt(v, "day_used_count")) item.DayNumberCount = getDayNumber(start_time, end_time) item.DayNumberRemain = getRemainDayNumber(end_time) if item.DayNumberCount < 0 || item.DayRemainCount <= 0 { item.State = "过期" } } reply.MerchantH5ServiceList = append(reply.MerchantH5ServiceList, item) } reply.PageNumber = req.PageNumber + 1 reply.PageSize = PAGESIZE return nil } func ManagementGetMerchantH5Service(ctx context.Context, req *apis.ManagementGetMerchantH5ServiceReq, reply *apis.ManagementGetMerchantH5ServiceReply) (err error) { if req.PageNumber > 0 { req.PageNumber -= 1 } else { req.PageNumber = 0 } err = getMerchantH5List(req, reply) if err != nil { l.Error("func", zap.String("call", "ManagementGetMerchantH5Service"), zap.String("args", utils.MarshalJsonString(req)), zap.String("error", err.Error())) } l.Debug(utils.MarshalJsonString(req, reply)) return }