Commit 3d06d502 by huangchengyi

1.0

parent cd90f610
package main
import (
"fmt"
"github.com/tidwall/gjson"
)
func main() {
var json = `{"mykey":[{"a":10},{"a":20},{"a":30},{"a":40}]}`
dd := gjson.Get(json,"mykey").Array()
for _,v := range dd {
fmt.Print(v.Get("a").String())
}
//json := `{"bar": {"id": 99, "xyz": "my xyz"}, "foo": {"myfoo": [605]}}`
//paths := []string{"foo.myfoo", "bar.id", "bar.xyz", "bar.abc"}
//expected := []string{"[605]", "99", "my xyz", ""}
//results := gjson.GetMany(json, paths...)
//if len(expected) != len(results) {
// fmt.Print(len(expected),len(results))
//}
//for i, path := range paths {
// if results[i].String() != expected[i] {
// fmt.Println( expected[i],results[i].String(),path)
// }
// fmt.Println(results[i].String())
//}
print("nin")
}
package logger package logger
import ( import (
"fmt"
"io" "io"
"os" "os"
"time" "time"
...@@ -27,7 +28,7 @@ func checkFileIsExist(filename string) bool { ...@@ -27,7 +28,7 @@ func checkFileIsExist(filename string) bool {
@param file_pre 附加文件前缀 @param file_pre 附加文件前缀
eg: logic.Loginfo("逾期统计开始","_yuqilv_"); eg: logic.Loginfo("逾期统计开始","_yuqilv_");
*/ */
func Log(writeString string,log_file_pre string) { func Log(writeString string,log_file_pre string,types int) {
log_file_pre = "_"+log_file_pre log_file_pre = "_"+log_file_pre
date := time.Now().Format("2006-01-02") date := time.Now().Format("2006-01-02")
date2 := time.Now().Format("2006-01-02 15:04:05") date2 := time.Now().Format("2006-01-02 15:04:05")
...@@ -43,6 +44,9 @@ func Log(writeString string,log_file_pre string) { ...@@ -43,6 +44,9 @@ func Log(writeString string,log_file_pre string) {
check(err1) check(err1)
ss := StrReplace("\r\n","",writeString,1) //替换多余换行 ss := StrReplace("\r\n","",writeString,1) //替换多余换行
io.WriteString(f,"\r\n"+date2+"----"+ss) //写入文件(字符串) io.WriteString(f,"\r\n"+date2+"----"+ss) //写入文件(字符串)
if types == 1 {
fmt.Print(writeString)
}
//fmt.Print(err) //fmt.Print(err)
} }
// StrReplace str_replace() // StrReplace str_replace()
......
package service package service
import ( import (
"encoding/json"
"fmt" "fmt"
"github.com/gomodule/redigo/redis"
"github.com/syyongx/php2go"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
"goods_machining/pkg/common" "goods_machining/pkg/common"
"goods_machining/pkg/gredis" "goods_machining/pkg/gredis"
"goods_machining/pkg/logger"
"goods_machining/pkg/mysql" "goods_machining/pkg/mysql"
"sort"
) )
// 自营定时任务服务 // 自营定时任务服务
...@@ -14,8 +19,9 @@ type ZyCronService struct { ...@@ -14,8 +19,9 @@ type ZyCronService struct {
/* /*
计算自营折扣信息 计算自营折扣信息
@param checkGoodsId 单独一个sku运行
*/ */
func (a *ZyCronService) ZyDiscount(goodsId string) { func (a *ZyCronService) ZyDiscount(checkGoodsId string) {
var ( var (
i int i int
...@@ -25,55 +31,192 @@ func (a *ZyCronService) ZyDiscount(goodsId string) { ...@@ -25,55 +31,192 @@ func (a *ZyCronService) ZyDiscount(goodsId string) {
) )
defer redisConn.Close() defer redisConn.Close()
page_size := 1000 redisConn.Do("del","zy_ratio_sku") //删除阶梯价缓存
for i=1;i<=1000;i++ {
start := 0;
if i== 1 {
start = 0;
}else{
start = (i-1)*page_size
}
sql := "select goods_id from lie_goods where self_supplier_type = 1 and stock >0 limit "+common.MyIntToStr(page_size)+","+common.MyIntToStr(start); twoYearsAgotime := php2go.Time()-60 * 60 * 24 * 365 * 2 //两年前时间戳
if goodsId != "" {
sql = "select goods_id from lie_goods where self_supplier_type = 1 and stock >0 limit "+goodsId; page_size := 1000 //查数据库每页大小
} OuterLoop:
goodsInfos, err := dbSpu.QueryString(sql) for i=1;i<=1000;i++ {
if err != nil || len(goodsInfos) == 0{ start := 0;
fmt.Print("查询没有数据") if i== 1 {
break; start = 0;
} }else{
for _,info := range goodsInfos{ start = (i-1)*page_size
now_goods_id := info["goods_id"] }
/********开始计算系数******/
fmt.Println("开始计算系数"+goodsIdTemp) sql := "select goods_id,class_id,brand_id,cost,ladder_price from lie_goods where self_supplier_type = 1 and stock >0 and status=1 limit "+common.MyIntToStr(page_size)+","+common.MyIntToStr(start);
if checkGoodsId != "" {
goodsBatch,_ := gredis.String(redisConn.Do("HGET","Self_goods_batch",now_goods_id)) //获取商品批次信息 sql = "select goods_id,class_id,brand_id,cost,ladder_price from lie_goods where self_supplier_type = 1 and stock >0 and status=1 limit "+checkGoodsId;
apiGoodsList := gjson.Get(goodsBatch, "dc_time").Array() }
goodsInfos, err := dbSpu.QueryString(sql)
var EarlyAddTime int64 = 0; //最早的批次 if err != nil || len(goodsInfos) == 0{
for _, goods := range apiGoodsList { fmt.Print("查询没有数据")
dc_time := goods.Get("dc_time").Int() break;
if EarlyAddTime == 0 { }
EarlyAddTime = dc_time for _,a := range goodsInfos{
now_goods_id := a["goods_id"]
now_class_id := a["class_id"]
now_brand_id := a["brand_id"]
/********开始计算系数******/
fmt.Println("开始计算系数"+goodsIdTemp)
goodsBatch,_ := gredis.String(redisConn.Do("HGET","Self_goods_batch",now_goods_id)) //获取商品批次信息
apiGoodsList := gjson.Get(goodsBatch, "dc_time").Array()
var EarlyBatchTime int64 = 0; //最早的批次
for _, goods := range apiGoodsList {
dc_time := goods.Get("dc_time").Int()
if EarlyBatchTime == 0 {
EarlyBatchTime = dc_time
continue;
}
if dc_time < EarlyBatchTime {
EarlyBatchTime = dc_time
}
}
if EarlyBatchTime == 0 {
fmt.Println("没有批次信息,goods_id :"+now_goods_id)
continue; continue;
} }
if dc_time < EarlyAddTime { //查询所有的系数列表
EarlyAddTime = dc_time ratioAll, _ := redis.Values(redisConn.Do("hgetall", "Self_goods_ratio"))
var retioArr []string
err := redis.ScanSlice(ratioAll, &retioArr)
if err != nil || len(retioArr) == 0 {
fmt.Println("查询系数失败");
return
} }
}
if EarlyAddTime == 0 { //系数排序,sort最高的优先排序
fmt.Println("没有批次信息") keys := make([]int,0)
continue; rationList := make(map[int][]map[string]string,0)
} for _,v:=range retioArr{
keys = append(keys,int(gjson.Get(v,"sort").Int()))
rationTemp := make([]map[string]string,0)
rationTemp = append(rationTemp,map[string]string{
"goods_id":gjson.Get(v,"goods_id").String(),
"name":gjson.Get(v,"name").String(), //系数名称
"ratio":gjson.Get(v,"ratio").String(), //折扣价
"min_ratio":gjson.Get(v,"min_ratio").String(), //保本折扣价
"class_id":gjson.Get(v,"class_id").String(),
"brand_id":gjson.Get(v,"brand_id").String(),
"expire_time":gjson.Get(v,"expire_time").String(),
"allow_coupon":gjson.Get(v,"allow_coupon").String(),
"allow_presale":gjson.Get(v,"allow_presale").String(),
})
rationList[int(gjson.Get(v,"sort").Int())] =rationTemp
}
sort.Sort(sort.Reverse(sort.IntSlice(keys))) //倒叙排
} //计算系数
for _,sort :=range keys{
rationInfo := rationList[sort]
for _,info := range rationInfo {
//不参与系数的商品
if info["goods_id"] != "" {
if php2go.Stripos(info["goods_id"],","+now_goods_id+",",0) > -1 {
fmt.Println("1001 本商品不参与系数"+now_goods_id)
continue;
}
}
//不参与分类的商品
if info["class_id"] != "" {
if php2go.Stripos(info["class_id"],","+now_class_id+",",0) > -1 {
continue;
}
}
//不参与分类的商品
if info["brand_id"] != "" {
if php2go.Stripos(info["class_id"],","+now_brand_id+",",0) > -1 {
continue;
}
}
//处理过期
isExpire := false
expireTimeArr := php2go.Explode(",",info["expire_time"])
for _,v1 := range expireTimeArr {
//已过期
if v1 == "-1" && EarlyBatchTime <= twoYearsAgotime {
isExpire = true;
break
}
//三个月后过期
if v1 == "-2" && EarlyBatchTime >= (twoYearsAgotime+3600* 24 * 30 * 3) {
isExpire = true;
break
}
//三个内过期
if twoYearsAgotime <= EarlyBatchTime && EarlyBatchTime <= (twoYearsAgotime+common.MyInt64(v1)*3600*24*30) {
isExpire = true;
break
}
}
if isExpire == false {
fmt.Println("1001 本商品不存在过期"+now_goods_id)
continue;
}
//处理清库存系数,几个系数符合,往下面算
var ratioPrice float64;
ratioPrice = common.MyFloat64(a["cost"]) * common.MyFloat64(info["ratio"])
//查询立创数据
szlcPriceStr,_ := gredis.String(redisConn.Do("HGET","Self_szlc_price",now_goods_id)) //获取商品批次信息
if szlcPriceStr != "" { //有立创的价格
nowLadder := gjson.Get(a["ladder_price"], "purchases").Array()
endnowLadder := nowLadder[len(nowLadder)-1].Get("purchases").Int() //最后一个梯度
//获取立创对应的梯度价格
szlcPriceStr = `{"mykey":`+szlcPriceStr+`}`
szlcPriceArr := gjson.Get(szlcPriceStr, "mykey").Array()
var szlcPrice float64 =0 //找到立创对应的梯度价格
for _,b := range szlcPriceArr{
if b.Get("purchases").Int() <= endnowLadder {
szlcPrice = b.Get("price").Float() //自营立创
}
}
szlcPrice = szlcPrice*0.95 //立创95折
if ratioPrice > szlcPrice { //比立创的95折大
minPrice := common.MyFloat64(a["cost"]) * common.MyFloat64(info["min_ratio"])
if szlcPrice < minPrice {
ratioPrice = minPrice
}else{
ratioPrice = szlcPrice
}
}
}
//立创价格
allow_coupon := info["allow_coupon"]
if allow_coupon == "" {
allow_coupon = "2"
}
allow_presale := info["allow_presale"]
if allow_presale == "" {
allow_presale = "2"
}
//redisWrite.Do("HSET",config.Get("redis_all.BRAND_NAME_ALL").String(),brandkey,brandId) ratioRes,_ := json.Marshal(map[string]string{
"goods_id":now_goods_id,
"allow_coupon":allow_coupon,
"allow_presale":allow_presale,
"szlc_price":szlcPriceStr,
"price_ac":common.MyFloat64ToStr(ratioPrice),
})
redisConn.Do("HSET","zy_ratio_sku",now_goods_id,string(ratioRes)) //写入缓存
} //最后写日志
logger.Log("计算阶梯价成功 goods_id: "+now_goods_id+" 原始梯度价:"+a["ladder_price"]+" 立创梯度价: "+ szlcPriceStr +" 结果:"+string(ratioRes),"ratio_sku_",1)
}
}
if checkGoodsId != "" {
break OuterLoop; //只跑一个跳出所有循环
}
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment