db.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. import mysql.connector
  2. import hashlib
  3. db = mysql.connector.connect(
  4. host="10.8.230.16",
  5. user="root",
  6. passwd="kfzs123",
  7. port=3306,
  8. database="aws_dev"
  9. )
  10. cursor = db.cursor()
  11. def update_spider_data(goods):
  12. sql = "update spider_data set attr_leading=%s,attr_sku=%s,img_header=%s,img_desc=%s,status=1 where id=%s"
  13. val = (goods['attr_leading'], goods['attr_sku'], goods['img_header'], goods['img_desc'], goods['id'])
  14. cursor.execute(sql, val)
  15. db.commit()
  16. print(cursor.rowcount, " 条记录被修改")
  17. return
  18. def update_spider_data_other(goods):
  19. sql = "update spider_data_other set img_desc=%s,status=4 where id=%s"
  20. val = (goods['img_desc'], goods['id'])
  21. cursor.execute(sql, val)
  22. db.commit()
  23. print(cursor.rowcount, " 条记录被修改")
  24. return
  25. def delete_spider_data(spider_id):
  26. sql = "delete from spider_data where id=%s"
  27. val = (spider_id,)
  28. cursor.execute(sql, val)
  29. db.commit()
  30. print("spider_data_delete")
  31. def get_spider_task():
  32. sql = "select id,supplier,sku,title,url,category_first,category_second from spider_data where status<>1"
  33. cursor.execute(sql)
  34. result = cursor.fetchall()
  35. return result
  36. def get_update_spider_task():
  37. sql = "select id,supplier,sku,title,url from spider_data_other where status=5"
  38. cursor.execute(sql)
  39. result = cursor.fetchall()
  40. return result
  41. # todo:这里跟类型有关系 ,注意
  42. def add_shop_goods(goods, desc_list):
  43. goods_desc = ""
  44. for img in desc_list:
  45. goods_desc += "<img src='" + img + "' /><br/>"
  46. sql = "INSERT INTO shop_goods (goods_no, goods_name,supplier,goods_desc,spider_id) VALUES (%s, %s,%s,%s,%s)"
  47. val = (goods['goods_no'], goods['goods_name'], goods['supplier'], goods_desc, goods['spider_id'])
  48. cursor.execute(sql, val)
  49. db.commit()
  50. print(goods['goods_name'], "记录插入成功", cursor.lastrowid)
  51. return cursor.lastrowid
  52. def add_goods_gallery(goods_id, thumb_list):
  53. val = []
  54. if len(thumb_list) == 0:
  55. print("thumb_list_is_null")
  56. return
  57. for img_url in thumb_list:
  58. v = (goods_id, img_url)
  59. val.append(v)
  60. sql = "insert into shop_goods_gallery(goods_id,thumb_url)values (%s,%s)"
  61. cursor.executemany(sql, val)
  62. db.commit()
  63. print(goods_id, "记录插入成功", cursor.lastrowid)
  64. return cursor.lastrowid
  65. # todo:这里跟类别有关系,注意
  66. def add_shop_attribute(attr_name):
  67. sql = "select id from shop_attribute where attr_name=%s limit 1"
  68. val = (attr_name,)
  69. cursor.execute(sql, val)
  70. r = cursor.fetchone()
  71. attr_id = 0
  72. if r is None:
  73. sql = "insert into shop_attribute(attr_name)values ('" + attr_name + "')"
  74. cursor.execute(sql)
  75. db.commit()
  76. print(attr_name, "记录插入成功", cursor.lastrowid)
  77. attr_id = cursor.lastrowid
  78. else:
  79. attr_id = r[0]
  80. return attr_id
  81. def add_shop_goods_attr(goods_id, attr_id, attr_value_list):
  82. val = []
  83. if len(attr_value_list) == 0:
  84. return
  85. for value in attr_value_list:
  86. v = (goods_id, attr_id, value)
  87. val.append(v)
  88. sql = "insert into shop_goods_attr(goods_id,attr_id,attr_value)values (%s,%s,%s)"
  89. cursor.executemany(sql, val)
  90. db.commit()
  91. print("记录插入成功", cursor.lastrowid)
  92. return cursor.lastrowid
  93. def add_shop_goods_sku(goods_id, supplier, cat_name):
  94. sql = "select attr_id,attr_value,id from shop_goods_attr where goods_id=" + str(goods_id)
  95. cursor.execute(sql)
  96. result = cursor.fetchall()
  97. attr_ids_dic = {}
  98. attr_leading = []
  99. attr_sku = []
  100. attr_dic = {}
  101. for v in result:
  102. attr_ids_dic[v[0]] = v[1]
  103. k = str(v[0]) + v[1]
  104. attr_dic[k] = v[2]
  105. # goods_attr += str(v[0]) + "|"
  106. # names += v[1] + "|"
  107. attr_ids = list(attr_ids_dic.keys())
  108. supplier_code = get_supplier_code(supplier)
  109. cat_code = get_cat_code(cat_name)
  110. parent_sku = str(supplier_code) + str(cat_code) + "00"
  111. max_sku = get_max_sku(parent_sku)
  112. if max_sku == 0:
  113. max_sku = parent_sku + "00000"
  114. val = []
  115. sku_no = int(max_sku) + 1
  116. if len(attr_ids) == 1: # 只有一种属性
  117. for v in result:
  118. d = (goods_id, v[2], v[1], sku_no)
  119. val.append(d)
  120. sku_no += 1
  121. elif len(attr_ids) == 2: # 两种属性
  122. attr_leading_id = attr_ids[0]
  123. attr_sku_id = attr_ids[1]
  124. for v in result:
  125. attr_id = v[0]
  126. attr_name = v[1]
  127. if attr_id == attr_leading_id:
  128. attr_leading.append(attr_name)
  129. if attr_id == attr_sku_id:
  130. attr_sku.append(attr_name)
  131. for lead in attr_leading:
  132. k = str(attr_leading_id) + lead
  133. id_att_leading = attr_dic[k]
  134. for j in attr_sku:
  135. k = str(attr_sku_id) + j
  136. id_att_sku = attr_dic[k]
  137. goods_attr = str(id_att_leading) + "|" + str(id_att_sku)
  138. names = lead + "|" + j
  139. d = (goods_id, goods_attr, names, sku_no)
  140. val.append(d)
  141. sku_no += 1
  142. elif len(attr_ids) == 0: # 没有属性
  143. d = (goods_id, "", "", sku_no)
  144. val.append(d)
  145. sql = "insert into shop_goods_sku(goods_id,goods_attr,names,sku_no)values (%s,%s,%s,%s)"
  146. # val = (goods_id, goods_attr, names, sku_no)
  147. # print(val)
  148. cursor.executemany(sql, val)
  149. db.commit()
  150. print("记录插入成功", cursor.lastrowid)
  151. return cursor.lastrowid
  152. def get_supplier_code(supplier_name):
  153. sql = "select supplier_code from shop_supplier where supplier_name=%s limit 1"
  154. val = (supplier_name,)
  155. cursor.execute(sql, val)
  156. result = cursor.fetchone()
  157. code = ""
  158. if result is None:
  159. sql = "select max(supplier_code) from shop_supplier"
  160. cursor.execute(sql)
  161. result = cursor.fetchone()
  162. max_value = result[0]
  163. sql = "insert into shop_supplier (supplier_name,supplier_code)value(%s,%s)"
  164. code = int(max_value) + 1
  165. val = (supplier_name, code)
  166. cursor.execute(sql, val)
  167. else:
  168. code = result[0]
  169. return code
  170. def get_cat_code(cat_name):
  171. if cat_name == "":
  172. return 0
  173. cat_id = 0
  174. sql = "select cat_code from shop_category where cat_name=%s limit 1"
  175. val = (cat_name,)
  176. cursor.execute(sql, val)
  177. result = cursor.fetchone()
  178. if result is None:
  179. sql = "select max(cat_code) from shop_category"
  180. cursor.execute(sql)
  181. result = cursor.fetchone()
  182. max_value = result[0]
  183. cat_id = int(max_value) + 1
  184. sql = "insert into shop_category(cat_code,cat_name)value(%s,%s)"
  185. val = (cat_id, cat_name)
  186. cursor.execute(sql, val)
  187. else:
  188. cat_id = result[0]
  189. return cat_id
  190. def get_max_sku(parent_sku):
  191. sql = "select sku_no from shop_goods_sku where sku_no like '" + parent_sku + "%' order by sku_no desc limit 1"
  192. cursor.execute(sql)
  193. r = cursor.fetchone()
  194. sku = 0
  195. if r is not None:
  196. sku = r[0]
  197. return sku
  198. def get_max_spu(supplier, cat_name):
  199. supplier_code = get_supplier_code(supplier)
  200. cat_code = get_cat_code(cat_name)
  201. # parent_sku = str(supplier_code) + str(cat_code) + "0000"
  202. parent_sku = str(supplier_code) + str(cat_code)
  203. sql = "select goods_no from shop_goods where goods_no like '" + parent_sku + "%' order by goods_no desc limit 1"
  204. cursor.execute(sql)
  205. r = cursor.fetchone()
  206. sku = 0
  207. if r is not None:
  208. sku = int(r[0])
  209. sku += 1
  210. else:
  211. sku = str(supplier_code) + str(cat_code) + "0000"
  212. sku = int(sku) + 1
  213. return sku
  214. def add_spider_other():
  215. sql = "select sku,url,title,supplier from spider_data_other where sku not in(select goods_no from shop_goods) "
  216. cursor.execute(sql)
  217. result = cursor.fetchall()
  218. # result = cursor.fetchone()
  219. for r in result:
  220. print(r)
  221. sku_no = str(r[0])
  222. url = r[1]
  223. goods_name = r[2]
  224. try:
  225. s2 = "select sku from spider_data where url='" + url + "'"
  226. print(s2)
  227. # return
  228. cursor.execute(s2)
  229. r2 = cursor.fetchone()
  230. sku_old = r2[0]
  231. s3 = "select id from shop_goods where goods_no='" + sku_old + "'"
  232. cursor.execute(s3)
  233. r3 = cursor.fetchone()
  234. print(r3[0])
  235. goods_id_old = str(r3[0])
  236. sql_insert = "insert into shop_goods(goods_no,goods_name,supplier,goods_desc)select " + sku_no + ",'" + goods_name + "',supplier,goods_desc from shop_goods where goods_no=" + sku_old
  237. print(sql_insert)
  238. cursor.execute(sql_insert)
  239. db.commit()
  240. goods_id = cursor.lastrowid
  241. # goods_id = 1
  242. sql_insert = "insert into shop_goods_gallery(goods_id,thumb_url) select " + str(
  243. goods_id) + ", thumb_url from shop_goods_gallery where goods_id=" + goods_id_old
  244. cursor.execute(sql_insert)
  245. db.commit()
  246. print(sql_insert)
  247. sql_insert = "insert into shop_goods_sku(goods_id,goods_attr,names) select " + str(
  248. goods_id) + ",goods_attr,names from shop_goods_sku where goods_id=" + goods_id_old
  249. cursor.execute(sql_insert)
  250. db.commit()
  251. print(sql_insert)
  252. except:
  253. print("error")
  254. continue
  255. def update_shop_goods(goods, desc_list):
  256. goods_desc = ""
  257. for img in desc_list:
  258. goods_desc += "<img src='" + img + "' /><br/>"
  259. sql = "update shop_goods set goods_desc=%s where goods_no=%s limit 1"
  260. val = (goods_desc, goods['goods_no'])
  261. cursor.execute(sql, val)
  262. db.commit()
  263. print("更新成功", goods['goods_no'])
  264. return
  265. def insert_tongji_data(array, type):
  266. column = "ID,createdate, `XH`,`FWSJ`,`DY`,`LY`,`RKYM`,`SSC`,`FWIP`,`FKBSM`,`FWSC`,`FWYS`,`ZHTL`,`SYCFWSJ`,`FWLX`,`FWPC`,"
  267. column += "`CZXT`,`WLFWS`,`LLQ`,`YYHJ`,`PMFBL`,`PMYS`,`FBB`,`SJZCC`,`SFZCJA`,`DKSJ`,`TLSC`,`YMDZ`,`type`"
  268. main_key = str(array[1]) + str(array[2]) + str(array[6]) + str(array[7]) + str(array[23]) + str(array[25])
  269. id = hashlib.md5(main_key.encode('utf-8')).hexdigest()
  270. sql = "insert into BO_EU_PTSYQ_TONGJI(" + column + ")values"
  271. val = "("
  272. val += "'" + id + "',now(),"
  273. val_b = ""
  274. num = range(0, 26)
  275. for n in num:
  276. val_b += "'" + str(array[n]) + "',"
  277. if len(val_b) == 0:
  278. return
  279. # val += val_b.strip(",")
  280. val += val_b
  281. val += "'" + type + "'"
  282. val += ")"
  283. sql += val
  284. print(sql)
  285. try:
  286. cursor.execute(sql)
  287. db.commit()
  288. except Exception as e:
  289. print(e)
  290. print("出错")