package task import ( "encoding/base64" "errors" "fmt" mysql2 "git.oa00.com/go/mysql" "github.com/360EntSecGroup-Skylar/excelize/v2" "github.com/gin-gonic/gin" "github.com/golangkit/formatime" "github.com/jinzhu/gorm" "github.com/shopspring/decimal" "gopkg.in/gomail.v2" "log" "path/filepath" "recook/configs" "recook/internal/static_path" "recook/internal/v2/lib/back" "recook/internal/v2/lib/common" "recook/internal/v2/lib/excel" "recook/internal/v2/lib/jingtong" "recook/internal/v2/model/gys/enterprise" "recook/internal/v2/model/recook/goods" manage2 "recook/internal/v2/model/recook/order" "recook/internal/v2/model/recook/user" manage "recook/internal/v2/model/third" "recook/tools" "regexp" "strconv" "strings" "time" ) type PushNewGoods struct { } type argsAddGoods struct { Step uint `json:"step" form:"step"` } // @Style 添加新商品 //func (p *PushNewGoods) AddGoods(c *gin.Context) { // stepStr, _ := c.GetQuery("step") // step, _ := strconv.Atoi(stepStr) // //if step <= 0 { // // http.Fail(c, "间隔时长不能为空") // // return // //} // if dbc.Rds.Get(configs.Config_Jingtong_Task_AddGoods_redis_key).Val() != "" { // back.Fail(c, "任务还在进行中,请勿重复运行") // return // } // dbc.Rds.Set(configs.Config_Jingtong_Task_AddGoods_redis_key, "1", 0) // go p.addGoods(step) // // back.Suc(c, "任务运行成功", "") // return //} //邮件提醒 type newResponse struct { GysName string `json:"gys_name"` GoodsName string `json:"goods_name"` Coke string `json:"coke"` SkuID uint `json:"sku_id"` } func (p *PushNewGoods) UpdateTask(c *gin.Context) { go func() { index := 0 for true { recookGoodsInfoModel := &goods.RecookGoodsInfoModel{} lists := recookGoodsInfoModel.List(index, 100, "id asc", "publish_status = ?", goods.RecookGoodsInfoPublishStatusTrue) index += 100 for _, list := range lists { if err := p.goods(list.ThirdPartyId, 0); err != nil { log.Println("taskError: err:", err) } } if len(lists) != 100 { break } } p.SendNewGoods() //更新完商品数据,发邮件提醒售价为0的商品 if configs.IsProductionEnv() { //是否为正式环境 p.SendNewGoods() } }() back.Suc(c, "任务运行成功", "") return } //func (p *PushNewGoods) addGoods(step int) { // defer func() { // dbc.Rds.Del(configs.Config_Jingtong_Task_AddGoods_redis_key) // }() // recookThirdPartyModel := &manage.RecookThirdPartyModel{} // log.Println("推送新商品任务开始") // for { // lists := recookThirdPartyModel.GetLimit(100) // if len(lists) == 0 { // log.Println("推送新商品任务结束") // return // } // idsSuc := []uint{} // idsFail := []uint{} // for _, list := range lists { // if err := p.goods(list.GoodsId, step); err != nil { // log.Println("推送新商品接口错误") // idsFail = append(idsFail, list.Id) // } else { // idsSuc = append(idsSuc, list.Id) // } // if step > 0 { // time.Sleep(time.Duration(step) * time.Second) // } // } // if len(idsSuc) > 0 { // recookThirdPartyModel.UpdateSucByIds(idsSuc) // } // if len(idsFail) > 0 { // recookThirdPartyModel.UpdateFailByIds(idsFail) // } // } //} func (p *PushNewGoods) goods(goodsId uint, step int) error { defer func() { if r := recover(); r != nil { log.Println(r) } }() info, err := jingtong.Jingtong.Info(goodsId) if err != nil { return err } if info.Name == "" { return errors.New("该商品正在补货中") } // 已下架 //if info.Marketable == 0 { // //return errors.New("已下架") //} if step > 0 { time.Sleep(time.Duration(step) * time.Second) } price, err := jingtong.Jingtong.Price(goodsId) if err != nil { return err } brandName := strings.TrimSpace(info.BrandName) recookGoodsBrandModel := &goods.RecookGoodsBrandModel{} brandInfo := recookGoodsBrandModel.FindByName(brandName) if brandInfo.Id <= 0 { // 创建品牌信息 brandInfo.Name = brandName recookGoodsBrandModel.Create(&brandInfo) if brandInfo.Id <= 0 { return errors.New("网络异常1") } } recookGoodsInfoModel := &goods.RecookGoodsInfoModel{} // 处理仓库 //if strings.Index(info.Warehouse, "国内") != -1 || strings.Index(info.Warehouse, "上海") != -1 { // info.Type = "国内仓库" //} else if strings.Index(info.Warehouse, "保税") != -1 { // info.Type = "自贸区" //} else { // info.Type = "海外直邮" //} recookThirdPartyDetailModel := &manage.RecookThirdPartyDetailModel{} goodsDetail := recookGoodsInfoModel.FindByThirdIdType(info.GoodsId, goods.RecookGoodsInfoJCook) if err := recookGoodsInfoModel.Transaction(func(tx *gorm.DB) error { recookThirdPartyDetailModel.SetDb(tx) fmt.Println("商品:", info) data := manage.RecookThirdPartyDetailModel{ LastModify: formatime.NewSecond(info.LastModify), CatId: info.CatId, CatName: info.CategoryName, BrandId: info.BrandId, BrandName: info.BrandName, Price: decimal.NewFromFloat32(info.Price), Bn: info.Bn, Name: info.Name, Weight: decimal.NewFromFloat32(info.Weight), Store: info.Store, Score: info.Score, Uptime: formatime.NewSecond(info.Uptime), GoodsLink: info.GoodsLink, Unit: info.Unit, GoodsId: info.GoodsId, Type: info.Type, IsShipping: info.IsShipping, DutyFree: info.DutyFree, WarehouseId: info.WarehouseId, CountryId: info.CountryId, CountryAbbreviation: info.CountryAbbreviation, Warehouse: info.Warehouse, DetailPrice: decimal.NewFromFloat32(price.Price), DetailStore: price.Store, DetailIsShipping: price.IsShipping, DetailMarketPrice: decimal.NewFromFloat32(price.MarketPrice), } if goodsDetail.Id > 0 { row := recookThirdPartyDetailModel.UpdateByGoodsId(info.GoodsId, &data) if row <= 0 { } } else { fmt.Println("data:", data) recookThirdPartyDetailModel.Create(&data) if data.Id <= 0 { return errors.New("网络异常2-新商品") } } recookGoodsInfoModel.SetDb(tx) haxAuth := 0 if info.Type == "自贸区" || info.Type == "海外直邮" { haxAuth = 1 } storehouse := goods.RecookGoodsInfoStorehouseNone isFerme := goods.RecookGoodsInfoIsFermeFalse //switch info.Type { //case "国内仓库": // storehouse = goods.RecookGoodsInfoStorehouseDomestic //case "海外直邮": // storehouse = goods.RecookGoodsInfoStorehouseDirectMail // isFerme = goods.RecookGoodsInfoIsFermeTrue //case "自贸区": // storehouse = goods.RecookGoodsInfoStorehouseBonded // isFerme = goods.RecookGoodsInfoIsFermeTrue //} // 插入商品信息 goodsInfo := goods.RecookGoodsInfoModel{ BrandID: brandInfo.Id, VendorID: configs.Config_Third_Party_Jingtong_Vender_Id, GoodsName: strings.TrimSpace(info.Name), Description: "", Material: "", // 材质 FirstCategoryID: configs.Config_Third_Party_Jingtong_Main_Category, SecondCategoryID: configs.Config_Third_Party_Jingtong_Sub_Category, PublishStatus: 0, FreightID: 1, // 包邮 Hash: tools.GenerateGoodsHashSign(), IsJoinTeamPerformance: 1, //是否有分成,默认有 ThirdPartyId: info.GoodsId, ThirdPartyType: goods.RecookGoodsInfoJCook, HasAuth: haxAuth, IsImport: goods.RecookGoodsInfoIsImportTrue, Storehouse: storehouse, IsFerme: isFerme, } if goodsDetail.Id > 0 { // 插入商品信息 goodsData := map[string]interface{}{ //"hash": tools.GenerateGoodsHashSign(), "goods_name": strings.TrimSpace(info.Name), "has_auth": haxAuth, "is_import": goods.RecookGoodsInfoIsImportTrue, "storehouse": storehouse, "is_ferme": isFerme, } //goods.RecookGoodsInfoModel{ // GoodsName: strings.TrimSpace(info.name), // HasAuth: haxAuth, // IsImport: goods.RecookGoodsInfoIsImportTrue, // Storehouse: storehouse, // IsFerme: isFerme, //} // 已下架 if info.Marketable == 0 { goodsData["publish_status"] = 0 } row := recookGoodsInfoModel.UpdateByThirdIdType(info.GoodsId, goods.RecookGoodsInfoJCook, goodsData) if row <= 0 { } // 已下架 if info.Marketable == 0 { return nil } goodsInfo.Id = goodsDetail.Id } else { // 已下架 if info.Marketable == 0 { return errors.New("已下架") } recookGoodsInfoModel.Create(&goodsInfo) if goodsInfo.Id <= 0 { return errors.New("网络错误") } } // ---------------------------更新商品品牌属性--------------------------------- if goodsDetail.Id <= 0 { recookGoodsBrandModel.SetDb(tx) row := recookGoodsBrandModel.AddGoodsCount(brandInfo.Id) if row <= 0 { return errors.New("网络错误") } } tempUrl := "" if goodsDetail.Id == 0 { // 不需要更新图片 // -----------------------------存储主图------------------------------- //这边要将主图保存在本地 mainImgs := []goods.RecookGoodsMainPhotoModel{} tempUrls, width, height := common.UrlToImg(info.ImageDefault, "jingtong") tempUrl = tempUrls if tempUrl == "" { //return errors.New("网络错误") } mainImgs = append(mainImgs, goods.RecookGoodsMainPhotoModel{ GoodsId: goodsInfo.Id, Url: tempUrl, Name: "api", IsMaster: 1, Width: width, Height: height, }) for _, img := range info.ImgUrl { tempUrl1, width1, height1 := common.UrlToImg("https://www.jtgloble.com/"+img, "jingtong") if tempUrl1 == "" { continue } mainImgs = append(mainImgs, goods.RecookGoodsMainPhotoModel{ GoodsId: goodsInfo.Id, Url: tempUrl1, Name: "api", Width: width1, Height: height1, }) } recookGoodsMainPhotoModel := &goods.RecookGoodsMainPhotoModel{} recookGoodsMainPhotoModel.SetDb(tx) if goodsDetail.Id > 0 { recookGoodsMainPhotoModel.DeleteByGoodsId(goodsInfo.Id) } row := recookGoodsMainPhotoModel.CreateAll(&mainImgs) if row <= 0 { return errors.New("网络异常3") } } // ---------------------------存储属性--------------------------------- recookGoodsAttributeModel := &goods.RecookGoodsAttributeModel{} recookGoodsAttributeModel.SetDb(tx) attr := goods.RecookGoodsAttributeModel{ GoodsId: goodsInfo.Id, Name: "规格", Value: "标准", } if goodsDetail.Id == 0 { recookGoodsAttributeModel.Create(&attr) if attr.Id <= 0 { return errors.New("网络异常5") } } // ------------------------------存储sku------------------------------ purchasePrice := decimal.NewFromFloat32(price.Price) discountPrice := decimal.NewFromFloat32(price.MarketPrice) if info.DutyFree == 0 { tmp := decimal.NewFromFloat32(1.091) purchasePrice = purchasePrice.Mul(tmp) discountPrice = discountPrice.Mul(tmp) } hundred := decimal.NewFromInt(100) sku := goods.RecookGoodsSkuModel{ GoodsId: goodsInfo.Id, Name: "标准", CombineId: strconv.FormatUint(uint64(attr.Id), 10), PicURL: tempUrl, Code: info.Bn, PurchasePrice: purchasePrice.Mul(hundred).Ceil().Div(hundred), OriginalPrice: purchasePrice.Mul(hundred).Ceil().Div(hundred), DiscountPrice: discountPrice.Mul(hundred).Ceil().Div(hundred), Inventory: price.Store, ThirdPartySkuId: strconv.FormatUint(uint64(info.GoodsId), 10), ThirdPartyType: goods.RecookGoodsInfoJCook, } recookGoodsSkuModel := &goods.RecookGoodsSkuModel{} recookGoodsSkuModel.SetDb(tx) if goodsDetail.Id > 0 { sku := goods.RecookGoodsSkuModel{ //PicURL: tempUrl,// 不需要更新图片 Inventory: price.Store, Code: info.Bn, PurchasePrice: purchasePrice.Mul(hundred).Ceil().Div(hundred), } row := recookGoodsSkuModel.UpdateByGoodsId(goodsInfo.Id, &sku) if row <= 0 { } } else { recookGoodsSkuModel.Create(&sku) if sku.Id <= 0 { return errors.New("网络异常6") } } // -----------------------------存储详情图------------------------------- if goodsDetail.Id > 0 { // 不需要更新图片 return nil } detailPhotos := []goods.RecookGoodsDetailPhotoModel{} imgExp := regexp.MustCompile(`(?U) 1 { url := s[1] if !strings.HasPrefix(url, "http") { url = "https://www.jtgloble.com" + url } tempUrl1, width1, height1 := common.UrlToImg(url, "jingtong") if tempUrl1 == "" { continue } detailPhotos = append(detailPhotos, goods.RecookGoodsDetailPhotoModel{ GoodsID: goodsInfo.Id, Url: tempUrl1, OrderNo: index, Name: "api", Width: width1, Height: height1, }) index++ } } if len(detailPhotos) > 0 { recookGoodsDetailPhotoModel := &goods.RecookGoodsDetailPhotoModel{} if goodsDetail.Id > 0 { recookGoodsDetailPhotoModel.DeleteByGoodsId(goodsInfo.Id) } row := recookGoodsDetailPhotoModel.CreateAll(&detailPhotos) if row <= 0 { return errors.New("网络异常7") } } // -----------------------------存储详情图end------------------------------- return nil }); err != nil { return err } return nil } func (p *PushNewGoods) SendEmail(email string, one []newResponse) error { m := gomail.NewMessage() m.SetHeader("From", configs.Config_Gys_Email_Setting_Mail) m.SetHeader("To", email, "austin@akuhome.com") //m.SetHeader("To", "792209833@qq.com") m.SetHeader("Subject", "未设置售价商品通知") if (len(one)) <= 10 { str := "

\n您好!\n

" if len(one) > 0 { for i, v := range one { fmt.Printf("%v:%v,%v,%v,%v\n", i, v.GysName, v.GoodsName, v.Coke, v.SkuID) str += v.GysName + "," + v.GoodsName + "," + v.Coke + "," + fmt.Sprintf("%v", v.SkuID) + "

" } } m.SetBody("text/html", str) } else { str := "具体数据请看表格" m.SetBody("text/html", str) // 把资料整合成Excel f := excelize.NewFile() // Create a new sheet. //index2 := f.NewSheet("Sheet2") f.SetColWidth("Sheet1", "A", "A", 35) f.SetColWidth("Sheet1", "B", "B", 50) f.SetColWidth("Sheet1", "C", "C", 20) f.SetCellValue("Sheet1", "A1", "供应商") f.SetCellValue("Sheet1", "B1", "商品名称") f.SetCellValue("Sheet1", "C1", "条形码") f.SetCellValue("Sheet1", "D1", "skuId") for i, v := range one { fmt.Printf("%v:%v,%v,%v,%v\n", i, v.GysName, v.GoodsName, v.Coke, v.SkuID) f.SetCellValue("Sheet1", "A"+strconv.Itoa(i+2), v.GysName) f.SetCellValue("Sheet1", "B"+strconv.Itoa(i+2), v.GoodsName) f.SetCellValue("Sheet1", "C"+strconv.Itoa(i+2), v.Coke) f.SetCellValue("Sheet1", "D"+strconv.Itoa(i+2), v.SkuID) } //f.SetActiveSheet(index2) name := time.Now().Format("20060102-150405") + tools.GenerateGoodsHashSign() + ".xlsx" f.Path = filepath.Join(static_path.Dir.Root, static_path.Dir.Temp, name) if err := f.SaveAs(f.Path); err != nil { fmt.Println(err) } fmt.Println(f.Path) m.Attach(f.Path, gomail.Rename("=?utf-8?B?"+base64.StdEncoding.EncodeToString([]byte("未设置售价商品通知.xlsx"))+"?=")) } d := gomail.NewDialer(configs.Config_Gys_Email_Setting_Smtp, configs.Config_Gys_Email_Setting_Port, configs.Config_Gys_Email_Setting_User, configs.Config_Gys_Email_Setting_Pass) if err := d.DialAndSend(m); err != nil { return err } return nil } func (p *PushNewGoods) SendNewGoods() { var goodsList []newResponse var all []goods.RecookGoodsInfoModel //推送新商品上架中的全部商品 gys := enterprise.GysEnterpriseStateModel{} sku := goods.RecookGoodsSkuModel{} mysql2.Db.Preload("GoodsSku", "discount_price = 0").Where("publish_status = ?", goods.RecookGoodsInfoPublishStatusTrue).Find(&all) for _, v := range all { code := sku.FindByGoods(v.Id) for _, k := range code { name := gys.FindByUserId(v.VendorID) goodsList = append(goodsList, newResponse{ GysName: name.EnterpriseName, GoodsName: v.GoodsName, Coke: k.Code, SkuID: k.Id, }) } } err := p.SendEmail(configs.ConfigJTEmail, goodsList) if err != nil { fmt.Println(err.Error()) } } type order struct { manage2.RecookOrderGoodsDetailModel Sku goods.RecookGoodsSkuModel `gorm:"foreignKey:sku_id"` Addr manage2.RecookOrderAddrModel `gorm:"foreignKey:order_id;references:order_id"` User user.RecookUserInfoModel `gorm:"foreignKey:user_id"` Order manage2.RecookOrderInfoModel `gorm:"foreignKey:order_id"` } func genStyle(s *excel.Sheet) int { styleStr := `{ "fill":{ "type":"pattern", "pattern":2, "color":["#325000"] }, "font":{ "bold":true, "italic":false, "family":"微软雅黑", "size":9, "color":"#000000" }, "alignment":{ "horizontal":"center", "vertical":"center", "wrap_text":true } }` style, _ := s.File.NewStyle(styleStr) return style }