get_data.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import dataset
  2. import codecs
  3. import sys
  4. import codecs
  5. import traceback
  6. import requests
  7. import re
  8. import pandas as pd
  9. import random
  10. import urllib
  11. import json
  12. import gspread
  13. import datetime
  14. from gspread_pandas import Spread, Client
  15. from oauth2client.service_account import ServiceAccountCredentials
  16. import os
  17. def save_sheet(df,filename,tabname,startpos='A1'):
  18. scope = ['https://spreadsheets.google.com/feeds',
  19. 'https://www.googleapis.com/auth/drive']
  20. # credentials = ServiceAccountCredentials.from_json_keyfile_name('c:\\keys\\service\\gspread.json', scope)
  21. credentials = ServiceAccountCredentials.from_json_keyfile_name('c:\\keys\\spread2.json', scope)
  22. gc = gspread.authorize(credentials)
  23. spread = Spread(filename,creds=credentials)
  24. spread.df_to_sheet(df, index=False, sheet=tabname, start=startpos, replace=False)
  25. db = dataset.connect('mysql://hhh7796hhh:lYmWsu^ujcA1@127.0.0.1:13306/crm?charset=utf8mb4')
  26. contracts=[]
  27. def any_contract(comp):
  28. global contracts
  29. for c in contracts:
  30. if comp in c:
  31. return True
  32. return False
  33. #cursor=db.query("SELECT company,contract_time FROM xoops.execute_form where contract_time>'2020-01-01' and is_delete <> 'Y'")
  34. cursor=db.query("SELECT distinct f.company,f.contract_time FROM xoops.execute_form f, xoops.execute_detail i where f.exf_id=i.exf_id and f.contract_time>'2020-01-01' and f.is_delete <> 'Y'")
  35. for c in cursor:
  36. contracts.append(c['company'])
  37. df = pd.DataFrame(columns=('company','contact','title','telephone','worktype','sales','dt'))
  38. idx=0
  39. #cursor=db.query("SELECT company_s_temp, company_f_temp,sales,create_time FROM crm.customer_contact where create_time >='2020-01-01' and create_time <='2020-21-31'")
  40. cursor=db.query("SELECT c.company_s_temp,c.company_f_temp,c.name,c.title,c.telete,m.work_type, m.sales, m.set_date FROM crm.customer_maintenance m, customer_contact c where ( (m.work_type='M') or (m.work_type='MTWR') ) and c.cus_id=m.cus_id and m.set_date >='2020-01-01' and m.set_date <='2020-21-31'")
  41. for c in cursor:
  42. if c['company_s_temp'] is None:
  43. continue
  44. if not any_contract(c['company_s_temp']):
  45. df.loc[idx]=[c['company_f_temp'],c['name'],c['title'],c['telete'],c['work_type'],c['sales'],c['set_date']]
  46. idx+=1
  47. save_sheet(df,'sales_report','report')