123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- package accounting
- import (
- "context"
- "gd_management/apis"
- "gd_management/common.in/utils"
- "gd_management/consts"
- "gd_management/errors"
- "fmt"
- "sort"
- "time"
- "github.com/astaxie/beego/orm"
- "go.uber.org/zap"
- )
- func ConsumeTrendList(ctx context.Context, req *apis.ConsumeTrendListReq, reply *apis.ConsumeTrendListReply) error {
- o := orm.NewOrm()
- sql := "select t1.day,SUM(t1.amount) as amount from t_gd_consume as t1 left join t_gd_merchant_data_api t2 on t1.merchant_data_api_id=t2.id left join t_gd_merchants as t3 on t2.merchant_id=t3.id left join t_gd_data_api_query_type as t4 on t4.id=t2.query_type_id left join t_gd_data_api as t5 on t5.id=t4.data_api_id"
- where := ""
- if req.MerchantId != 0 {
- if where == "" {
- where = fmt.Sprintf(" where t2.merchant_id=%d", req.MerchantId)
- } else {
- where = fmt.Sprintf("%s and t2.merchant_id=%d", where, req.MerchantId)
- }
- }
- if req.DataApiId != 0 {
- if where == "" {
- where = fmt.Sprintf(" where t4.data_api_id=%d", req.DataApiId)
- } else {
- where = fmt.Sprintf("%s and t4.data_api_id=%d", where, req.DataApiId)
- }
- }
- if req.StartTime != "" {
- if len(req.StartTime) > consts.DayLen {
- req.StartTime = req.StartTime[0:consts.DayLen]
- }
- if where == "" {
- where = fmt.Sprintf(" where t1.day>='%s'", req.StartTime)
- } else {
- where = fmt.Sprintf("%s and t1.day>='%s'", where, req.StartTime)
- }
- }
- if req.EndTime != "" {
- if len(req.EndTime) > consts.DayLen {
- req.EndTime = req.EndTime[0:consts.DayLen]
- }
- if where == "" {
- where = fmt.Sprintf(" where t1.day<='%s'", req.EndTime)
- } else {
- where = fmt.Sprintf("%s and t1.day<='%s'", where, req.EndTime)
- }
- }
- sql = sql + where + " group by t1.day order by t1.day asc"
- _, err := o.Raw(sql).QueryRows(&reply.List)
- if err != nil {
- l.Error("func",
- zap.String("call", "ConsumeList"),
- zap.String("args", utils.MarshalJsonString(req)),
- zap.String("error", err.Error()))
- return errors.DataBaseError
- }
- endTime, _ := time.ParseInLocation("2006-01-02", req.EndTime, time.Local)
- startTime, _ := time.ParseInLocation("2006-01-02", req.StartTime, time.Local)
- dateArray := make([]string, 0)
- for endTime.After(startTime.AddDate(0, 0, -1)) {
- dateArray = append(dateArray, startTime.Format("2006-01-02"))
- startTime = startTime.AddDate(0, 0, 1)
- }
- for _, v := range dateArray {
- if !InSlice(reply.List, v) {
- reply.List = append(reply.List, apis.ConsumeTrend{
- Day: v,
- Amount: 0.00,
- })
- }
- }
- sort.SliceStable(reply.List, func(i, j int) bool {
- return reply.List[i].Day < reply.List[j].Day
- })
- return nil
- }
- func InSlice(items []apis.ConsumeTrend, item string) bool {
- for _, v := range items {
- if v.Day == item {
- return true
- }
- }
- return false
- }
|