goods.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. package conv
  2. import (
  3. _ "embed"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "gadmin/utility"
  8. "io"
  9. "os"
  10. "os/exec"
  11. "sort"
  12. "github.com/sirupsen/logrus"
  13. )
  14. var Goods = new(cGoods)
  15. type cGoods struct {
  16. version string
  17. lst []*GoodItem
  18. }
  19. type GoodsSlice []*GoodItem
  20. func (p GoodsSlice) Len() int {
  21. return len(p)
  22. }
  23. func (p GoodsSlice) Swap(i, j int) {
  24. p[i], p[j] = p[j], p[i]
  25. }
  26. func (p GoodsSlice) Less(i, j int) bool {
  27. return p[j].Value > p[i].Value
  28. }
  29. type GoodItem struct {
  30. Value int64 `json:"value"`
  31. Label string `json:"label"`
  32. }
  33. type GoodsItemer interface {
  34. GetId() int64
  35. GetName() string
  36. }
  37. type ShopGoodsItem struct {
  38. ID int64 `json:"goods_id"`
  39. Name string `json:"name"`
  40. }
  41. func (d ShopGoodsItem) GetId() int64 {
  42. return d.ID
  43. }
  44. func (d ShopGoodsItem) GetName() string {
  45. return d.Name
  46. }
  47. type GiftItem struct {
  48. ID int64 `json:"gift_id"`
  49. Name string `json:"gift_name"`
  50. }
  51. func (d GiftItem) GetId() int64 {
  52. return d.ID
  53. }
  54. func (d GiftItem) GetName() string {
  55. return d.Name
  56. }
  57. type LimitGiftItem struct {
  58. ID int64 `json:"id"`
  59. Name string `json:"name"`
  60. }
  61. func (d LimitGiftItem) GetId() int64 {
  62. return d.ID
  63. }
  64. func (d LimitGiftItem) GetName() string {
  65. return d.Name
  66. }
  67. type EquipBoxBaseItem struct {
  68. ID int64 `json:"goods_id"`
  69. Name int32 `json:"BoxType"`
  70. }
  71. func (d EquipBoxBaseItem) GetId() int64 {
  72. return d.ID
  73. }
  74. func (d EquipBoxBaseItem) GetName() string {
  75. return fmt.Sprintf("%v", d.Name)
  76. }
  77. type EquipBoxGiftItem struct {
  78. ID int64 `json:"GiftPackID"`
  79. Name string `json:"PackName"`
  80. }
  81. func (d EquipBoxGiftItem) GetId() int64 {
  82. return d.ID
  83. }
  84. func (d EquipBoxGiftItem) GetName() string {
  85. return d.Name
  86. }
  87. type FirtstChargeItem struct {
  88. ID int64 `json:"giftId"`
  89. Name string `json:"giftName"`
  90. }
  91. func (d FirtstChargeItem) GetId() int64 {
  92. return d.ID
  93. }
  94. func (d FirtstChargeItem) GetName() string {
  95. return d.Name
  96. }
  97. type PropStoreItem struct {
  98. ID int64 `json:"Shop_ID"`
  99. Name int32 `json:"Item_Type"`
  100. }
  101. func (d PropStoreItem) GetId() int64 {
  102. return d.ID
  103. }
  104. func (d PropStoreItem) GetName() string {
  105. return fmt.Sprintf("%v", d.Name)
  106. }
  107. type WeekendGiftItem struct {
  108. ID int64 `json:"gift_id"`
  109. Name string `json:"gift_name"`
  110. }
  111. func (d WeekendGiftItem) GetId() int64 {
  112. return d.ID
  113. }
  114. func (d WeekendGiftItem) GetName() string {
  115. return d.Name
  116. }
  117. type MonarchGiftRewardItem struct {
  118. ID int64 `json:"shopId"`
  119. Name string `json:"shopName"`
  120. }
  121. func (d MonarchGiftRewardItem) GetId() int64 {
  122. return d.ID
  123. }
  124. func (d MonarchGiftRewardItem) GetName() string {
  125. return d.Name
  126. }
  127. type MonarchGift2Item struct {
  128. ID int64 `json:"shopId"`
  129. Name string `json:"shopName"`
  130. }
  131. func (d MonarchGift2Item) GetId() int64 {
  132. return d.ID
  133. }
  134. func (d MonarchGift2Item) GetName() string {
  135. return d.Name
  136. }
  137. type ELPassportItem struct {
  138. ID int64 `json:"GiftPackID"`
  139. Name string `json:"Name"`
  140. }
  141. func (d ELPassportItem) GetId() int64 {
  142. return d.ID
  143. }
  144. func (d ELPassportItem) GetName() string {
  145. return d.Name
  146. }
  147. type Holiday12GiftItem struct {
  148. ID int64 `json:"gift_id"`
  149. Name string `json:"gift_name"`
  150. }
  151. func (d Holiday12GiftItem) GetId() int64 {
  152. return d.ID
  153. }
  154. func (d Holiday12GiftItem) GetName() string {
  155. return d.Name
  156. }
  157. type Cost struct {
  158. Id int64 `json:"id"`
  159. Count int64 `json:"count"`
  160. }
  161. type ShopGoods struct {
  162. Id int64 `json:"ID"`
  163. Name string `json:"Name"`
  164. Cost []Cost `json:"Cost"`
  165. GoodsType int `json:"GoodsType"`
  166. }
  167. //go:embed resource/jsonConvert
  168. var jsonConvert []byte
  169. func (j *cGoods) webProduct() *cGoods {
  170. /*utility.CreateDir("./tmp")
  171. if _, err := os.Stat("./tmp/jsonConvert"); err != nil {
  172. tmpFile, err := os.Create("./tmp/jsonConvert")
  173. if err != nil {
  174. logrus.Errorln("创建jsonConvert失败", err)
  175. return j
  176. }
  177. if _, err := tmpFile.Write(jsonConvert); err != nil {
  178. logrus.Errorln("写入jsonConvert失败", err)
  179. return j
  180. }
  181. _ = tmpFile.Close()
  182. if err := os.Chmod("./tmp/jsonConvert", 0755); err != nil {
  183. logrus.Errorln("修改jsonConvert权限失败", err)
  184. return j
  185. }
  186. }*/
  187. // 执行jsonConvert,生成webproduct.json文件并放到resource/public/json/下
  188. /*cmd := exec.Command("./jsonConvert", fmt.Sprintf("-v=%s", j.version), fmt.Sprintf("-m=%s", os.Getenv("JSONCONVERTiMODE")), "-j=./")
  189. if err := os.Chdir("./tmp"); err != nil {
  190. logrus.Errorf("os.Chdir err:%+v", err)
  191. }
  192. if err := cmd.Run(); err != nil {
  193. logrus.Errorf("cmd.Run err:%+v", err)
  194. }
  195. if err := os.Chdir("../"); err != nil {
  196. logrus.Errorf("os.Chdir2 err:%+v", err)
  197. }*/
  198. utility.CreateDir(fmt.Sprintf("./resource/public/json/%s/", j.version))
  199. cmd2 := exec.Command("cp", "./tmp/serverjson/webproduct.json", fmt.Sprintf("./resource/public/json/%s/", j.version))
  200. if err := cmd2.Run(); err != nil {
  201. logrus.Errorf("cmd2.Run err:%+v", err)
  202. }
  203. return j
  204. }
  205. func (j *cGoods) Extract(version string) {
  206. logrus.Info("Goods Extract...")
  207. // shopMaps := map[int32]string{
  208. // 1: "低品质广告宝箱",
  209. // 2: "高品质广告宝箱",
  210. // 3: "十连抽宝箱",
  211. // 4: "钻石购买少量金币",
  212. // 5: "钻石购买中等金币",
  213. // 6: "钻石购买大量金币",
  214. // 7: "6元充值",
  215. // 8: "30元充值",
  216. // 9: "98元充值",
  217. // 12: "198元充值",
  218. // 13: "328元充值",
  219. // 14: "648元充值",
  220. // 10: "6元首充",
  221. // 11: "25元首充",
  222. // 15: "每日礼包",
  223. // 16: "金币不足 钻石换购",
  224. // 17: "新年礼包",
  225. // 18: "新年礼包",
  226. // 19: "月卡(25元)",
  227. // 20: "季卡",
  228. // 21: "角色抽奖-单抽",
  229. // 22: "角色抽奖-十连抽",
  230. // 23: "每日金币礼包",
  231. // 24: "月卡(128元)",
  232. // 25: "成长基金(30元)",
  233. // 26: "成长基金(128元)",
  234. // 27: "通行证",
  235. // 28: "关卡内购买广告券",
  236. // }
  237. // for k, v := range shopMaps {
  238. // j.lst = append(j.lst, &GoodItem{
  239. // Value: k,
  240. // Label: v,
  241. // })
  242. // }
  243. j.setVersion(version).convert().webProduct()
  244. tmpShopGoods := make(map[int64]*ShopGoods)
  245. filePath := utility.LocalJsonPath(j.version, "webproduct.json")
  246. err := LoadItemData(filePath, &tmpShopGoods)
  247. if err == nil {
  248. for _, v := range j.lst {
  249. if v2, ok := tmpShopGoods[v.Value]; ok {
  250. v2.Name = v.Label
  251. }
  252. }
  253. j.save("product.json", tmpShopGoods)
  254. } else {
  255. logrus.Errorf("LocalJsonPath err:%+v", err)
  256. }
  257. for _, v := range j.lst {
  258. v.Label = fmt.Sprintf("%v(%v)", v.Label, v.Value)
  259. }
  260. j.save("goods.json", j.lst)
  261. }
  262. func (j *cGoods) setVersion(version string) *cGoods {
  263. j.version = version
  264. return j
  265. }
  266. func (j *cGoods) downloadFile(fileName string, lst any) error {
  267. b, err := GetUrlBody(j.version, fileName)
  268. if err != nil {
  269. logrus.Errorf("GetUrlBody err:%+v", err)
  270. return err
  271. }
  272. if err := json.Unmarshal(b, lst); err != nil {
  273. return err
  274. }
  275. return nil
  276. }
  277. func downloadGift[T GoodsItemer](filePath string, j *cGoods) error {
  278. lst1 := make([]T, 0)
  279. err := j.downloadFile(filePath, &lst1)
  280. if err != nil {
  281. logrus.Errorf("downloadGift %v err: %+v", filePath, err)
  282. return err
  283. }
  284. for _, v := range lst1 {
  285. if v.GetId() == 0 || len(v.GetName()) == 0 {
  286. logrus.Errorf("downloadGift %v filed error", filePath)
  287. return errors.New("field error")
  288. }
  289. j.lst = append(j.lst, &GoodItem{
  290. Value: v.GetId(),
  291. Label: v.GetName(),
  292. })
  293. }
  294. return nil
  295. }
  296. func (j *cGoods) convert() *cGoods {
  297. downloadGift[ShopGoodsItem]("shop.json", j)
  298. downloadGift[GiftItem]("DailyGift.json", j)
  299. downloadGift[GiftItem]("RoleGift.json", j)
  300. downloadGift[GiftItem]("NewerGift.json", j)
  301. downloadGift[GiftItem]("Holiday9Gift.json", j)
  302. downloadGift[GiftItem]("LanternFestivalGift.json", j)
  303. downloadGift[GiftItem]("Holiday3_Gift.json", j)
  304. downloadGift[GiftItem]("BuyOnceGetThreeDays.json", j)
  305. downloadGift[GiftItem]("Holiday10Gift.json", j)
  306. downloadGift[GiftItem]("LaborDayGift.json", j)
  307. downloadGift[GiftItem]("DragonBoatFestivalGift.json", j)
  308. downloadGift[GiftItem]("WeekendGift.json", j)
  309. downloadGift[GiftItem]("QixiFestivalGift.json", j)
  310. downloadGift[GiftItem]("MoonFestivalGift.json", j)
  311. downloadGift[EquipBoxGiftItem]("EquipBoxGiftPack.json", j)
  312. downloadGift[EquipBoxBaseItem]("EquipBoxBase.json", j)
  313. downloadGift[ELPassportItem]("EndlessPassportBase.json", j)
  314. downloadGift[GiftItem]("ReturnGift.json", j)
  315. downloadGift[GiftItem]("NewServerGift.json", j)
  316. downloadGift[FirtstChargeItem]("FirstChargeGift.json", j)
  317. downloadGift[PropStoreItem]("PropsStore.json", j)
  318. downloadGift[LimitGiftItem]("LimitGift.json", j)
  319. downloadGift[WeekendGiftItem]("WeekendGift.json", j)
  320. downloadGift[MonarchGiftRewardItem]("MonarchGiftReward.json", j)
  321. downloadGift[MonarchGift2Item]("MonarchGift2.json", j)
  322. downloadGift[Holiday12GiftItem]("Holiday12Gift.json", j)
  323. sort.Sort(GoodsSlice(j.lst))
  324. return j
  325. }
  326. // // ActivityGiftItem 活动礼包
  327. // type ActivityGiftItem struct {
  328. // GiftId int32 `json:"gift_id"`
  329. // GiftName string `json:"gift_name"`
  330. // Icon string `json:"icon"`
  331. // GiftType int `json:"gift_type"`
  332. // OriginalPrice int `json:"original_price"`
  333. // GiftPrice int `json:"gift_price"`
  334. // GiftAward [][]int `json:"gift_award"`
  335. // GiftStartTime string `json:"gift_start_time"`
  336. // GiftEndTime string `json:"gift_end_time"`
  337. // GiftLimit int `json:"gift_limit"`
  338. // ShowOrNot int `json:"show_or_not"`
  339. // }
  340. // func (j *cGoods) loadActivityGift() {
  341. // var jsons = []string{
  342. // "DragonBoatFestivalGift.json",
  343. // "LaborDayGift.json",
  344. // "TombSweepingDayGift.json",
  345. // "Holiday3_Gift.json",
  346. // "LanternFestivalGift.json",
  347. // "NewYearGift.json",
  348. // "QixiFestivalGift.json",
  349. // }
  350. // for _, file := range jsons {
  351. // var lst []*ActivityGiftItem
  352. // b, err := GetUrlBody(j.version, file)
  353. // if err != nil {
  354. // logrus.Errorf("loadActivityGift GetUrlBody err:%+v", err)
  355. // continue
  356. // }
  357. // if err = json.Unmarshal(b, &lst); err != nil {
  358. // logrus.Fatalf("loadActivityGift err=%v", err)
  359. // }
  360. // for _, v := range lst {
  361. // j.lst = append(j.lst, &GoodItem{
  362. // Value: v.GiftId,
  363. // Label: v.GiftName,
  364. // })
  365. // }
  366. // }
  367. // }
  368. // // LimitGiftItem 礼包详情
  369. // type LimitGiftItem2 struct {
  370. // Id int32 `json:"id"` // 礼包ID
  371. // Type int64 `json:"type"` // 礼包类型
  372. // //Value []int `json:"value"` // 类型值
  373. // }
  374. // func (j *cGoods) loadLimitGift() {
  375. // var limitGift []*LimitGiftItem
  376. // b, err := GetUrlBody(j.version, "LimitGift.json")
  377. // if err != nil {
  378. // logrus.Fatalf("loadLimitGift GetUrlBody err:%+v", err)
  379. // }
  380. // if err = json.Unmarshal(b, &limitGift); err != nil {
  381. // logrus.Fatalf("loadLimitGift err=%v", err)
  382. // }
  383. // var typeMap = make(map[int64]string)
  384. // typeMap = map[int64]string{
  385. // 1001: "登录",
  386. // 1002: "冒险等级达到N级",
  387. // 1003: "角色激活成功",
  388. // 1004: "角色突破成功",
  389. // 1005: "角色升星成功",
  390. // 1006: "章节通关",
  391. // 1007: "闯关失败(未通关)",
  392. // 1008: "已佩戴N件X品质装备",
  393. // 1009: "广告特惠礼包",
  394. // }
  395. // for _, v := range limitGift {
  396. // j.lst = append(j.lst, &GoodItem{
  397. // Value: v.Id,
  398. // Label: "限时礼包-" + typeMap[v.Type],
  399. // })
  400. // }
  401. // }
  402. // func (j *cGoods) append(v interface{}) {
  403. // getValue := reflect.ValueOf(v)
  404. // if getValue.Kind() != reflect.Slice {
  405. // logrus.Fatalf("need slice kind")
  406. // }
  407. // l := getValue.Len()
  408. // for i := 0; i < l; i++ {
  409. // value := getValue.Index(i)
  410. // typel := value.Type()
  411. // if typel.Kind() != reflect.Struct {
  412. // logrus.Fatalf("need struct kind:%+v", typel.Kind())
  413. // }
  414. // var goodItem GoodItem
  415. // num := value.NumField()
  416. // for j2 := 0; j2 < num; j2++ {
  417. // if typel.Field(j2).Name == "ID" {
  418. // goodItem.Value = value.Field(j2).Interface().(int32)
  419. // } else if typel.Field(j2).Name == "Name" {
  420. // goodItem.Label = value.Field(j2).Interface().(string)
  421. // } else {
  422. // fmt.Printf("跳过数据 type-kind: %s, name: %s, type: %s, value: %v\n", typel.Kind(), typel.Field(j2).Name, value.Field(j2).Type(), value.Field(j2).Interface())
  423. // }
  424. // }
  425. // j.lst = append(j.lst, &goodItem)
  426. // }
  427. // }
  428. func (j *cGoods) save(fileName string, v interface{}) {
  429. //保存到文件
  430. filePath := utility.LocalJsonPath(j.version, fileName)
  431. SaveJsonPath(filePath, v)
  432. }
  433. func LoadItemData(filePath string, value interface{}) (err error) {
  434. if filePath == "" {
  435. return errors.New("file cannot be blank")
  436. }
  437. _, err = os.Stat(filePath)
  438. if err != nil {
  439. if os.IsNotExist(err) {
  440. return err
  441. }
  442. return err
  443. }
  444. fileObj, err := os.Open(filePath)
  445. if err != nil {
  446. return err
  447. }
  448. fileData, err := io.ReadAll(fileObj)
  449. if err != nil {
  450. return err
  451. }
  452. err = json.Unmarshal(fileData, value)
  453. if err != nil {
  454. return err
  455. }
  456. return nil
  457. }