You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

347 lines
12 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package operation
import (
"errors"
"fmt"
"git.oa00.com/go/mysql"
"github.com/druidcaesa/gotool"
"github.com/shopspring/decimal"
"recook/internal/v2/model/actives"
"recook/internal/v2/model/operationstaties"
"recook/internal/v2/model/recook/user"
"strconv"
"time"
)
type operationLogic struct {
}
var Logic = &operationLogic{}
/*逻辑层代码*/
type yunYinResp struct {
GysNum uint `json:"gys_num"`
GysProportion float64 `json:"gys_proportion"`
GoodsNum uint `json:"goods_num"`
GoodsProportion float64 `json:"goods_proportion"`
OrderNum uint `json:"order_num"`
OrderProportion float64 `json:"order_proportion"`
SaleNum decimal.Decimal `json:"sale_num"`
SaleProportion decimal.Decimal `json:"sale_proportion"`
}
//ViewDetail 运营报显示数据
func (l operationLogic) ViewDetail(start string, end string, num uint) (rest yunYinResp, err error) {
//显示时间段内的报表
//获取4个时间2个现在时间显示的数据2个过去的时间比较的数据
startTime := time.Time{}
endTime := time.Time{}
olderStart := time.Time{}
olderEnd := time.Time{}
t1 := time.Now().Format("2006-01-02")
fmt.Println(startTime, endTime, olderStart, olderEnd)
if num == 1 {
//查询今日的数据
startTime, _ = time.ParseInLocation("2006-01-02", t1, time.Local)
endTime, _ = time.ParseInLocation("2006-01-02", t1, time.Local)
//比较的时间为昨日
t2 := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
olderStart, _ = time.ParseInLocation("2006-01-02", t2, time.Local)
olderEnd, _ = time.ParseInLocation("2006-01-02", t2, time.Local)
}
if num == 2 { //本周
//现在周几
d := gotool.DateUtil.GetWeekDay(time.Now())
//startTime本周 周一
st := time.Now().AddDate(0, 0, -d+1)
et := st.AddDate(0, 0, 6) //周末的时间
os := st.AddDate(0, 0, -7)
oe := st.AddDate(0, 0, -1)
startTime, _ = time.ParseInLocation("2006-01-02", st.Format("2006-01-02"), time.Local)
endTime, _ = time.ParseInLocation("2006-01-02", et.Format("2006-01-02"), time.Local)
olderStart, _ = time.ParseInLocation("2006-01-02", os.Format("2006-01-02"), time.Local)
olderEnd, _ = time.ParseInLocation("2006-01-02", oe.Format("2006-01-02"), time.Local)
}
if num == 3 { //本月
//m := int(time.Now().Month())
y := time.Now().Year()
//获取该月月初时间
st := time.Date(y, time.Now().Month(), 1, 0, 0, 0, 0, time.Local)
//月末时间
et := st.AddDate(0, 1, 0).AddDate(0, 0, -1)
//获取上月时间
os := st.AddDate(0, -1, 0)
od := st.AddDate(0, 0, -1)
startTime, _ = time.ParseInLocation("2006-01-02", st.Format("2006-01-02"), time.Local)
endTime, _ = time.ParseInLocation("2006-01-02", et.Format("2006-01-02"), time.Local)
olderStart, _ = time.ParseInLocation("2006-01-02", os.Format("2006-01-02"), time.Local)
olderEnd, _ = time.ParseInLocation("2006-01-02", od.Format("2006-01-02"), time.Local)
}
if num == 4 {
//获取年度时间
st := time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Local)
//年底时间
et := st.AddDate(1, 0, 0).AddDate(0, 0, -1)
//获取比对的去年时间段
os := st.AddDate(-1, 0, 0)
od := st.AddDate(0, 0, -1)
startTime, _ = time.ParseInLocation("2006-01-02", st.Format("2006-01-02"), time.Local)
endTime, _ = time.ParseInLocation("2006-01-02", et.Format("2006-01-02"), time.Local)
olderStart, _ = time.ParseInLocation("2006-01-02", os.Format("2006-01-02"), time.Local)
olderEnd, _ = time.ParseInLocation("2006-01-02", od.Format("2006-01-02"), time.Local)
}
if num > 0 && start != "" && end != "" {
return yunYinResp{}, errors.New("时间段与状态不能同时传!")
}
if start != "" && end != "" {
//传入的比对时间段
startTime, _ = time.ParseInLocation("2006-01-02", start, time.Local)
endTime, _ = time.ParseInLocation("2006-01-02", end, time.Local)
//获取比对的时间段
d := int(endTime.Sub(startTime).Hours() / 24)
os := startTime.AddDate(0, 0, -d-1)
od := startTime.AddDate(0, 0, -1)
olderStart, _ = time.ParseInLocation("2006-01-02", os.Format("2006-01-02"), time.Local)
olderEnd, _ = time.ParseInLocation("2006-01-02", od.Format("2006-01-02"), time.Local)
}
fmt.Println(startTime, endTime, olderStart, olderEnd)
//获取展示部分的数据
var op operationstaties.OperationStatisticsModel
var opList []operationstaties.OperationStatisticsModel
mysql.Db.Table(op.TableName()).Where("day_time between ? and ? ", startTime, endTime).Find(&opList)
var gysNow uint
var goodsNow uint
var orderNow uint
var saleNow decimal.Decimal
for _, model := range opList {
gysNow += model.GysNewAdd
goodsNow += model.GoodsNewAdd
orderNow += model.OrderNewAdd
saleNow = saleNow.Add(model.SalesVolume)
}
fmt.Println("当前数据:", gysNow, goodsNow, orderNow, saleNow)
//获取需要比对的数据
var opListOld []operationstaties.OperationStatisticsModel
mysql.Db.Table(op.TableName()).Where("day_time between ? and ? ", olderStart, olderEnd).Find(&opListOld)
var gysOld uint
var goodsOld uint
var orderOld uint
var saleOld decimal.Decimal
for _, model := range opListOld {
gysOld += model.GysNewAdd
goodsOld += model.GoodsNewAdd
orderOld += model.OrderNewAdd
saleOld = saleOld.Add(model.SalesVolume)
}
fmt.Println("比对数据:", gysOld, goodsOld, orderOld, saleOld)
//gys
rest.GysNum = gysNow
if gysOld == 0 {
rest.GysProportion = float64(1)
} else {
gys := (float64(gysNow) - float64(gysOld)) / float64(gysOld)
value, _ := strconv.ParseFloat(fmt.Sprintf("%.3f", gys), 64)
rest.GysProportion = value
}
//goods
rest.GoodsNum = goodsNow
if goodsOld == 0 {
rest.GoodsProportion = float64(1)
} else {
gsp := (float64(goodsNow) - float64(goodsOld)) / float64(goodsOld)
value, _ := strconv.ParseFloat(fmt.Sprintf("%.3f", gsp), 64)
rest.GoodsProportion = value
}
//order
rest.OrderNum = orderNow
if orderOld == 0 {
rest.OrderProportion = float64(1)
} else {
or := (float64(orderNow) - float64(orderOld)) / float64(orderOld)
value, _ := strconv.ParseFloat(fmt.Sprintf("%.3f", or), 64)
rest.OrderProportion = value
}
//sale
rest.SaleNum = saleNow
if saleOld.IsZero() {
rest.SaleProportion = decimal.NewFromFloat(1.00)
} else {
rest.SaleProportion = (saleNow.Sub(saleOld)).Div(saleOld).Truncate(4)
}
//if num == 1 {
// //查询今日的数据
// day := time.Now().Format("2006-01-02")
// var op1 operationstaties.OperationStatisticsModel
// mysql.Db.Table(op1.TableName()).Where("day_time=?", day).First(&op1)
// //获取昨日时间
// old := time.Now().AddDate(0, 0, -1)
// var op2 operationstaties.OperationStatisticsModel
// mysql.Db.Table(op1.TableName()).Where("day_time=?", old).First(&op2)
// //gys
// rest.GysNum = op1.GysNewAdd
// rest.GysProportion = decimal.NewFromInt(int64(op1.GysNewAdd)).Sub(decimal.NewFromInt(int64(op2.GysNewAdd))).Div(decimal.NewFromInt(int64(op2.GysNewAdd)))
// //goods
// rest.GoodsNum = op1.GoodsNewAdd
// rest.GoodsProportion = decimal.NewFromInt(int64(op1.GoodsNewAdd)).Sub(decimal.NewFromInt(int64(op2.GoodsNewAdd))).Div(decimal.NewFromInt(int64(op2.GoodsNewAdd)))
// //order
// rest.OrderNum = op1.OrderNewAdd
// rest.OrderProportion = decimal.NewFromInt(int64(op1.OrderNewAdd)).Sub(decimal.NewFromInt(int64(op2.OrderNewAdd))).Div(decimal.NewFromInt(int64(op2.OrderNewAdd)))
// //sale
// rest.SaleNum = op1.SalesVolume
// rest.SaleProportion = op1.SalesVolume.Sub(op2.SalesVolume).Div(op2.SalesVolume)
//}
//if num == 2 { //本周数据
// //获取今天是周几 例如周三。
// day := time.Now().Format("2006-01-02")
// day2 := gotool.DateUtil.GetWeekDay(time.Now())
// //获取周一的日期
// old := time.Now().AddDate(0, 0, -day2+1)
// //获取上周的时间
// var op []operationstaties.OperationStatisticsModel
//}
return rest, nil
}
type viewActiveRes struct {
NewUser int64
NewProportion float64
OlderUser int64
OlderProportion float64
ActiveAll int64
OneProportion float64
WeekProportion float64
MonthProportion float64
}
//ViewActive 统计活跃人数
func (l operationLogic) ViewActive(startTime string, endTime string, num uint) (rest viewActiveRes, err error) {
if startTime != "" && endTime != "" && num != 0 {
return viewActiveRes{}, errors.New("时间段和状态不可同时传")
}
var start time.Time
var end time.Time
if num == 1 {
start, _ = time.ParseInLocation("2006-01-02", time.Now().Format("2006-01-02"), time.Local)
end, _ = time.ParseInLocation("2006-01-02", time.Now().Format("2006-01-02"), time.Local)
}
if num == 2 {
//现在周几
d := gotool.DateUtil.GetWeekDay(time.Now())
//startTime本周 周一
st := time.Now().AddDate(0, 0, -d+1)
et := st.AddDate(0, 0, 6) //周末的时间
start, _ = time.ParseInLocation("2006-01-02", st.Format("2006-01-02"), time.Local)
end, _ = time.ParseInLocation("2006-01-02", et.Format("2006-01-02"), time.Local)
}
if num == 3 {
//m := int(time.Now().Month())
y := time.Now().Year()
//获取该月月初时间
st := time.Date(y, time.Now().Month(), 1, 0, 0, 0, 0, time.Local)
//月末时间
et := st.AddDate(0, 1, 0).AddDate(0, 0, -1)
start, _ = time.ParseInLocation("2006-01-02", st.Format("2006-01-02"), time.Local)
end, _ = time.ParseInLocation("2006-01-02", et.Format("2006-01-02"), time.Local)
}
if num == 4 {
//获取年度时间
st := time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Local)
//年底时间
et := st.AddDate(1, 0, 0).AddDate(0, 0, -1)
start, _ = time.ParseInLocation("2006-01-02", st.Format("2006-01-02"), time.Local)
end, _ = time.ParseInLocation("2006-01-02", et.Format("2006-01-02"), time.Local)
}
if startTime != "" && endTime != "" {
start, _ = time.ParseInLocation("2006-01-02", startTime, time.Local)
end, _ = time.ParseInLocation("2006-01-02", endTime, time.Local)
}
fmt.Println(start, end)
//获取时间段内注册新老用户数
var userinfo user.RecookUserInfoModel
var newNum int64
var olderNum int64
mysql.Db.Table(userinfo.TableName()).Where("created_at between ? and ?", start, end).Count(&newNum)
mysql.Db.Table(userinfo.TableName()).Where("created_at <?", start).Count(&olderNum)
var newPro float64
allP := newNum + olderNum
newPro = float64(newNum) / float64(allP)
var olderPro float64
olderPro = 1 - newPro
rest.NewUser = newNum
rest.NewProportion = newPro
rest.OlderUser = olderNum
rest.OlderProportion = olderPro
//获取在线活跃用户
var idList []uint
mysql.Db.Table(userinfo.TableName()).Where("created_at between ? and ?", start, end).Select("id").Pluck("id", &idList)
var acp []actives.ActiveUserModel
//sm := map[string]string{}
mysql.Db.Table((&actives.ActiveUserModel{}).TableName()).Where("user_id in (?)", idList).Find(&acp)
//for _, v := range acp {
//
// sl := strings.Split(v.ActiveList, ",")
// for _, k := range sl {
// //if _, ok := sm[k]; ok {
// // //存在
// // continue
// //} else {
// // sm[k] = v.Date
// //}
// if k == "" {
// continue
// } else {
// sm[k] = v.Date.Format("2006-01-02")
// }
// }
//}
activePeo := len(acp)
rest.ActiveAll = int64(activePeo)
//新户留存率模块
var onep int64
var week int64
var momth int64
for _, v := range acp {
ut, _ := time.ParseInLocation("2006-01-02", v.UpdateTime.Format("2006-01-02"), time.Local)
ct, _ := time.ParseInLocation("2006-01-02", v.CreatedTime.Format("2006-01-02"), time.Local)
n := ut.Sub(ct) / 24
if n > 1 {
onep++
}
if n > 7 {
week++
}
if n > 30 {
momth++
}
}
//日留存率
if newNum == 0 {
rest.OneProportion = 0
rest.WeekProportion = 0
rest.MonthProportion = 0
} else {
onePro := float64(onep) / float64(newNum)
value1, _ := strconv.ParseFloat(fmt.Sprintf("%.3f", onePro), 64)
rest.OneProportion = value1
weekPro := float64(week) / float64(newNum)
value2, _ := strconv.ParseFloat(fmt.Sprintf("%.3f", weekPro), 64)
rest.WeekProportion = value2
momthPro := float64(momth) / float64(newNum)
value3, _ := strconv.ParseFloat(fmt.Sprintf("%.3f", momthPro), 64)
rest.MonthProportion = value3
}
return
}