G2AProduct.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. package model
  2. import (
  3. "crawler/config"
  4. "crawler/service"
  5. "database/sql"
  6. "encoding/json"
  7. "fmt"
  8. "reflect"
  9. )
  10. type G2AProductModel struct {
  11. G2AProductParams
  12. G2AProductResult
  13. G2AProduct
  14. }
  15. // 검색 변수들
  16. type G2AProductParams struct {
  17. Page int `json:"page"`
  18. ID string `json:"id"`
  19. MinQty int `json:"minQty"`
  20. MinPriceFrom int `json:"minPriceFrom"`
  21. MinPriceTo int `json:"minPriceTo"`
  22. IncludeOutOfStock string `json:"includeOutOfStock"`
  23. UpdatedAtFrom string `json:"updatedAtFrom"`
  24. UpdatedAtTo string `json:"updatedAtTo"`
  25. }
  26. // G2A 상품 조회 결과
  27. type G2AProductResult struct {
  28. Total int `json:"total"`
  29. Page int `json:"page"`
  30. Docs []G2AProduct `json:"docs"`
  31. }
  32. // G2A 상품 정보
  33. type G2AProduct struct {
  34. ID string `json:"id"`
  35. Name string `json:"name"`
  36. IsTest int
  37. Type string `json:"type"`
  38. Slug string `json:"slug"`
  39. Qty int `json:"qty"`
  40. MinPrice float64 `json:"minPrice"`
  41. RetailMinPrice float64 `json:"retail_min_price"`
  42. RetailMinBasePrice float64 `json:"retailMinBasePrice"`
  43. Thumbnail string `json:"thumbnail"`
  44. SmallImage string `json:"smallImage"`
  45. CoverImage string `json:"coverImage"`
  46. Images []string `json:"images"`
  47. UpdatedAt string `json:"updated_at"`
  48. ReleaseDate string `json:"release_date"`
  49. Region string `json:"region"`
  50. Developer string `json:"developer"`
  51. Publisher string `json:"publisher"`
  52. Platform string `json:"platform"`
  53. PriceLimit PriceLimit `json:"priceLimit"`
  54. Restrictions Restrictions `json:"restrictions"`
  55. Requirements Requirements `json:"requirements"`
  56. Videos []Videos `json:"videos"`
  57. Categories []Categories `json:"categories"`
  58. }
  59. type PriceLimit struct {
  60. Min float64 `json:"min"`
  61. Max float64 `json:"max"`
  62. }
  63. type Restrictions struct {
  64. PegiViolence bool `json:"pegi_violence"`
  65. PegiProfanity bool `json:"pegi_profanity"`
  66. PegiDiscrimination bool `json:"pegi_discrimination"`
  67. PegiDrugs bool `json:"pegi_drugs"`
  68. PegiFear bool `json:"pegi_fear"`
  69. PegiGambling bool `json:"pegi_gambling"`
  70. PegiOnline bool `json:"pegi_online"`
  71. PegiSex bool `json:"pegi_sex"`
  72. }
  73. type Requirements struct {
  74. Minimal Spec `json:"minimal"`
  75. Recommended Spec `json:"recommended"`
  76. }
  77. type Spec struct {
  78. ReqProcessor string `json:"reqprocessor"`
  79. ReqGraphics string `json:"reqgraphics"`
  80. ReqMemory string `json:"reqmemory"`
  81. ReqDiskSpace string `json:"reqdiskspace"`
  82. ReqSystem string `json:"reqsystem"`
  83. ReqOther string `json:"reqother"`
  84. }
  85. type Videos struct {
  86. Type string `json:"type"`
  87. Url string `json:"url"`
  88. }
  89. type Categories struct {
  90. ID int `json:"id"`
  91. Name string `json:"name"`
  92. }
  93. func (this *G2AProductModel) IsExists(id string) bool {
  94. var (
  95. db = service.DB_PLAYR
  96. conn = db.SQLDB
  97. query = "SELECT IF(COUNT(*) <= 0, 0, 1) AS `exists` FROM tb_g2a_product WHERE id = ?;"
  98. exists = false
  99. )
  100. err := conn.QueryRow(query, id).Scan(&exists)
  101. if err != nil {
  102. db.SetErrorLog(err, query)
  103. return exists
  104. }
  105. db.SetGeneralLog(config.GL_ACTION_SELECT, query, "select exists g2a")
  106. return exists
  107. }
  108. func (this *G2AProductModel) Insert(list []G2AProduct) error {
  109. if len(list) == 0 {
  110. return fmt.Errorf("Product 상품이 존재하지 않습니다.")
  111. }
  112. var (
  113. db = service.DB_PLAYR
  114. conn = db.SQLDB
  115. query = `
  116. INSERT INTO tb_g2a_product (
  117. id, name, is_test, type, slug, qty, min_price, retail_min_price, retail_min_base_price,
  118. thumbnail, small_image, cover_image, images, updated_at, release_date, region, developer,
  119. publisher, platform, price_limit, restrictions, requirements, videos, categories, created_at
  120. )
  121. VALUES
  122. `
  123. vals = []interface{}{}
  124. dup string
  125. )
  126. for _, row := range list {
  127. query += `(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW()),`
  128. data := this.DataFilter(row)
  129. vals = append(vals,
  130. data["id"], data["name"], data["IsTest"], data["type"], data["slug"], data["qty"],
  131. data["minPrice"], data["retail_min_price"], data["retailMinBasePrice"], data["thumbnail"],
  132. data["smallImage"], data["coverImage"], data["images"], data["updated_at"], data["release_date"],
  133. data["region"], data["developer"], data["publisher"], data["platform"], data["priceLimit"],
  134. data["restrictions"], data["requirements"], data["videos"], data["categories"],
  135. )
  136. dup += `name = VALUES(name), is_test = VALUES(is_test), type = VALUES(type),
  137. slug = VALUES(slug), qty = VALUES(qty), min_price = VALUES(min_price), retail_min_price = VALUES(retail_min_price),
  138. retail_min_base_price = VALUES(retail_min_base_price), thumbnail = VALUES(thumbnail), small_image = VALUES(small_image),
  139. cover_image = VALUES(cover_image), images = VALUES(images), updated_at = VALUES(updated_at), release_date = VALUES(release_date),
  140. region = VALUES(region), developer = VALUES(developer), publisher = VALUES(publisher),
  141. platform = VALUES(platform), price_limit = VALUES(price_limit), restrictions = VALUES(restrictions),
  142. requirements = VALUES(requirements), videos = VALUES(videos), categories = VALUES(categories),`
  143. }
  144. query = query[0 : len(query)-1]
  145. dup = dup[0 : len(dup)-1]
  146. query += `ON DUPLICATE KEY UPDATE ` + dup
  147. stmt, err := conn.Prepare(query)
  148. if err != nil {
  149. db.SetErrorLog(err, query)
  150. return err
  151. }
  152. defer stmt.Close()
  153. _, err = stmt.Exec(vals...)
  154. if err != nil {
  155. db.SetErrorLog(err, query)
  156. return err
  157. }
  158. db.SetGeneralLog(config.GL_ACTION_WRITE, query, "insert g2a products")
  159. return nil
  160. }
  161. func (this *G2AProductModel) Update(row G2AProduct) error {
  162. var (
  163. db = service.DB_PLAYR
  164. conn = db.SQLDB
  165. query = `
  166. UPDATE tb_g2a_product SET
  167. name = ?, is_test = ?, type = ?, slug = ?, qty = ?, min_price = ?, retail_min_price = ?, retail_min_base_price = ?,
  168. thumbnail = ?, small_image = ?, cover_image = ?, images = ?, updated_at = ?, release_date = ?, region = ?, developer = ?,
  169. publisher = ?, platform = ?, price_limit = ?, restrictions = ?, requirements = ?, videos = ?, categories = ?, created_at = NOW()
  170. WHERE id = ?;
  171. `
  172. data = this.DataFilter(row)
  173. )
  174. _, err := conn.Exec(query,
  175. data["name"], data["IsTest"], data["type"], data["slug"], data["qty"],
  176. data["minPrice"], data["retail_min_price"], data["retailMinBasePrice"], data["thumbnail"],
  177. data["smallImage"], data["coverImage"], data["images"], data["updated_at"], data["release_date"],
  178. data["region"], data["developer"], data["publisher"], data["platform"], data["priceLimit"],
  179. data["restrictions"], data["requirements"], data["videos"], data["categories"], data["id"])
  180. if err != nil {
  181. db.SetErrorLog(err, query)
  182. return err
  183. }
  184. db.SetGeneralLog(config.GL_ACTION_MODIFY, query, "update g2a product info")
  185. return nil
  186. }
  187. func (this *G2AProductModel) Info(id int) (G2AProduct, error) {
  188. var (
  189. db = service.DB_PLAYR
  190. conn = db.SQLDB
  191. query = `SELECT id, name, qty, min_price, retail_min_price, retail_min_base_price FROM tb_g2a_product WHERE id = ?;`
  192. info G2AProduct
  193. )
  194. err := conn.QueryRow(query, id).Scan(
  195. &info.ID, &info.Name, &info.Qty, &info.MinPrice, &info.RetailMinPrice, &info.RetailMinBasePrice,
  196. )
  197. if err != nil && err != sql.ErrNoRows {
  198. db.SetErrorLog(err, query)
  199. return info, err
  200. }
  201. db.SetGeneralLog(config.GL_ACTION_SELECT, query, "select g2a product info")
  202. return info, nil
  203. }
  204. func (this *G2AProductModel) DataFilter(product G2AProduct) map[string]interface{} {
  205. var row = map[string]any{}
  206. data, _ := json.Marshal(product)
  207. _ = json.Unmarshal(data, &row)
  208. for k, v := range row {
  209. switch c := v.(type) {
  210. case string:
  211. if c == "" {
  212. row[k] = nil
  213. }
  214. case int, int32, int64:
  215. if c == 0 {
  216. row[k] = 0
  217. }
  218. case float32, float64:
  219. if c == 0 || c == nil || c == "" {
  220. row[k] = 0.0
  221. }
  222. case []string:
  223. if len(c) == 0 {
  224. row[k] = nil
  225. } else {
  226. s, _ := json.Marshal(c)
  227. row[k] = string(s)
  228. }
  229. case nil:
  230. row[k] = nil
  231. case []interface{}:
  232. if c == nil || len(c) == 0 {
  233. row[k] = nil
  234. } else {
  235. s, _ := json.Marshal(c)
  236. row[k] = string(s)
  237. }
  238. case map[string]interface{}:
  239. if c == nil || len(c) == 0 {
  240. row[k] = nil
  241. } else {
  242. s, _ := json.Marshal(c)
  243. row[k] = string(s)
  244. }
  245. default:
  246. fmt.Println(k)
  247. fmt.Println(reflect.TypeOf(c))
  248. }
  249. }
  250. return row
  251. }