no_set_date_execute_detail_notice.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. '''
  2. E2:預定完成日期為0000-00-00者,每個月的第1個工作日上午 9:00 發送逾期通知給CRM
  3. '''
  4. import dataset
  5. from datetime import datetime, timedelta
  6. from email.mime.text import MIMEText
  7. from email.mime.image import MIMEImage
  8. from email.mime.multipart import MIMEMultipart
  9. from email.mime.base import MIMEBase
  10. from email.mime.application import MIMEApplication
  11. import os
  12. import boto3
  13. from botocore.exceptions import ClientError
  14. # 寄送逾期執行表單Email通知
  15. def hhh_send_mail(email, html):
  16. SENDER = "Gorgeous Space <noreply@hhh.com.tw>"
  17. RECIPIENT = email
  18. AWS_REGION = "us-east-1"
  19. CHARSET = "UTF-8"
  20. client = boto3.client('ses',region_name=AWS_REGION)
  21. try:
  22. msg = MIMEMultipart()
  23. # msg["Subject"] = "執行項目通知(無預定完成日期)_" + str(datetime.date.today())
  24. msg["Subject"] = "執行項目通知(無預定完成日期)_" + datetime.now().strftime("%Y-%m-%d %H:%M")
  25. msg["From"] = "noreply@hhh.com.tw"
  26. msg["To"] = email
  27. # Set message body
  28. body = MIMEText(html, "html")
  29. msg.attach(body)
  30. response = client.send_raw_email(
  31. Source=msg["From"],
  32. Destinations=[msg["To"]],
  33. RawMessage={"Data": msg.as_string()}
  34. )
  35. print(response)
  36. # Display an error if something goes wrong.
  37. except ClientError as e:
  38. print(e.response['Error']['Message'])
  39. else:
  40. print("Email sent! Message ID:"),
  41. print(response['MessageId'])
  42. # Choozmo DB
  43. # db = dataset.connect("mysql://choozmo:pAssw0rd@db.ptt.cx:3306/hhh?charset=utf8mb4")
  44. # 測試機 DB
  45. db = dataset.connect('mysql://hhh7796hhh:lYmWsu^ujcA1@hhh-v57-cluster.cluster-cmab1ctkglka.ap-northeast-2.rds.amazonaws.com:3306/stage?charset=utf8mb4')
  46. # 正式機 DB
  47. # db = dataset.connect('mysql://hhh7796hhh:lYmWsu^ujcA1@hhh-v57-cluster.cluster-cmab1ctkglka.ap-northeast-2.rds.amazonaws.com:3306/xoops?charset=utf8mb4')
  48. print ('發送執行項目通知(無預定完成日期) start...')
  49. # 所有合約(無預定完成日期) (CRM)
  50. q1 = "SELECT DISTINCT(ef.exf_id) \
  51. FROM execute_detail ed \
  52. left JOIN execute_form ef ON ed.exf_id=ef.exf_id \
  53. WHERE ef.is_close='N' AND ef.is_delete='N' \
  54. AND ed.is_delete = 'N' AND ed.is_complete='N' \
  55. AND ed.set_date = '0000-00-00' \
  56. ORDER BY ed.execute_man"
  57. # 所有執行項(無預定完成日期) (CRM)
  58. q2 = "SELECT * \
  59. FROM execute_detail ed \
  60. left JOIN execute_form ef ON ed.exf_id=ef.exf_id \
  61. WHERE ef.is_close='N' AND ef.is_delete='N' \
  62. AND ed.is_delete = 'N' AND ed.is_complete='N' \
  63. AND ed.set_date = '0000-00-00' \
  64. ORDER BY ed.execute_man"
  65. html = """
  66. <!DOCTYPE html>
  67. <head>
  68. <meta charset="UTF-8">
  69. </head>
  70. <body>
  71. """
  72. # q1_result_count = len(list(db.query(q1)))
  73. q2_result_count = len(list(db.query(q2)))
  74. # if q1_result_count > 0: # 有合約(無預定完成日期)
  75. # html += "<div style='color:blue;'><b>逾期合約: 共" + str(q1_result_count) + "筆</b></div>"
  76. if q2_result_count > 0: # 有執行項目(無預定完成日期)
  77. html += "<div style='color:blue;'><b>無預定完成日期的執行項目: 共" + str(q2_result_count) + "筆</b><br><br></div>"
  78. html += "<table style='width:100%;border-collapse: collapse;' border='1'> \
  79. <tr style='text-align:left;'> \
  80. <th style='width:5%;'>#</th> \
  81. <th>合約</th> \
  82. <th style='width:20%;'>設計公司</th> \
  83. <th style='width:15%;'>合約到期日</th> \
  84. <th style='width:5%;'>負責業務</th> \
  85. <th style='width:20%;'>執行項</th> \
  86. </tr>";
  87. count = 1
  88. for r1 in db.query(q1):
  89. # 特定合約的所有執行項(無預定完成日期)
  90. q3 = "SELECT * \
  91. FROM execute_detail ed \
  92. left JOIN execute_form ef ON ed.exf_id=ef.exf_id \
  93. WHERE ef.is_close='N' AND ef.is_delete='N' \
  94. AND ed.is_delete = 'N' AND ed.is_complete='N' \
  95. AND ed.set_date = '0000-00-00' \
  96. AND ef.exf_id = '" + str(r1['exf_id']) + "' \
  97. ORDER BY ed.execute_man"
  98. q3_result_count = str(len(list(db.query(q3)))+1)
  99. idx = 0
  100. for r3 in db.query(q3):
  101. url = 'https://stage-backstage.hhh.com.tw/admin/home/execute?exf_id=' + str(r3['exf_id'])
  102. if idx < 1:
  103. html += "<tr><td rowspan='" + q3_result_count + "'>" + str(count) + "</td><td rowspan='" + q3_result_count + "'><a href='" + url + "' target='_blank'>" + r3['num'] + "</a></td><td rowspan='" + q3_result_count + "'>" + r3['company'] + "</td><td rowspan='" + q3_result_count + "'>" + str(r3['contract_time']) + "</td><td rowspan='" + q3_result_count + "'>" + r3['sales_man'] + "</td><td>" + r3['lv1'] + "_" + r3['lv2'] + "</td><tr>"
  104. idx += 1
  105. else:
  106. html += "<tr><td>" + r3['lv1'] + "_" + r3['lv2'] + "</td></tr>"
  107. count += 1
  108. html += """
  109. </table>
  110. </body>
  111. </html>
  112. """
  113. # Email清單
  114. crm_list = ['mike@choozmo.com', 'stacy@choozmo.com', 'oluckyo.mike@gmail.com']
  115. # crm_list = ['miko@hhh.com.tw', 'mollie@hhh.com.tw', 'lynn@hhh.com.tw', 'zoe_lo@hhh.com.tw']
  116. # 寄送逾期執行表單Email通知
  117. for email in crm_list:
  118. print ("發送Email: " + email)
  119. hhh_send_mail(email, html)
  120. else: # 沒有未預定完成日期的執行表單資料
  121. print ("所有執行項目皆有【預定完成日期】,不須處理")
  122. # html += "所有執行項目皆有【預定完成日期】,不須處理"
  123. print ('發送執行項目通知(無預定完成日期) end...')
  124. print ('\n發送執行項目通知(無預定完成日期) (執行單位) start...')
  125. # 執行單位Email
  126. # hhh_edit@hhh.com.tw: 編輯採訪部
  127. # hhh_mk@hhh.com.tw: 行銷企劃部
  128. # hhh_web@hhh.com.tw: 網站工程部
  129. # hhh_video@hhh.com.tw: 影音企劃部
  130. # agent@hhh.com.tw: 幸福經紀人
  131. # hhh_admin@hhh.com.tw: 行政部
  132. # hhh_sales@hhh.com.tw: 業務部
  133. execute_man_list = ['hhh_edit@hhh.com.tw','hhh_mk@hhh.com.tw','hhh_web@hhh.com.tw','hhh_video@hhh.com.tw','agent@hhh.com.tw','hhh_admin@hhh.com.tw','hhh_sales@hhh.com.tw']
  134. for execute_man in execute_man_list:
  135. print ("execute_man: " + execute_man)
  136. # 所有合約(無預定完成日期) (CRM)
  137. q4 = "SELECT DISTINCT(ef.exf_id) \
  138. FROM execute_detail ed \
  139. left JOIN execute_form ef ON ed.exf_id=ef.exf_id \
  140. WHERE ef.is_close='N' AND ef.is_delete='N' \
  141. AND ed.is_delete = 'N' AND ed.is_complete='N' \
  142. AND ed.set_date = '0000-00-00' \
  143. AND ed.execute_man = '" + execute_man + "'"
  144. # 所有執行項(無預定完成日期) (CRM)
  145. q5 = "SELECT * \
  146. FROM execute_detail ed \
  147. left JOIN execute_form ef ON ed.exf_id=ef.exf_id \
  148. WHERE ef.is_close='N' AND ef.is_delete='N' \
  149. AND ed.is_delete = 'N' AND ed.is_complete='N' \
  150. AND ed.set_date = '0000-00-00' \
  151. AND ed.execute_man = '" + execute_man + "'"
  152. html2 = """
  153. <!DOCTYPE html>
  154. <head>
  155. <meta charset="UTF-8">
  156. </head>
  157. <body>
  158. """
  159. q4_result_count = len(list(db.query(q4)))
  160. q5_result_count = len(list(db.query(q5)))
  161. print ("q4_result_count: " + str(q4_result_count))
  162. print ("q5_result_count: " + str(q5_result_count))
  163. if q5_result_count > 0: # 有無預定完成日期執行項目
  164. # html += "<div style='color:blue;'><b>逾期執行項目: 共" + str(q2_result_count) + "筆</b><br><br></div>"
  165. html2 += "<div style='color:blue;'><b>" + execute_man + " 無預定完成日期: 共" + str(q5_result_count) + "筆</b><br><br></div>"
  166. html2 += "<table style='width:100%;border-collapse: collapse;' border='1'> \
  167. <tr style='text-align:left;'> \
  168. <th style='width:5%;'>#</th> \
  169. <th>合約</th> \
  170. <th style='width:20%;'>設計公司</th> \
  171. <th style='width:15%;'>合約到期日</th> \
  172. <th style='width:5%;'>負責業務</th> \
  173. <th style='width:20%;'>執行項</th> \
  174. </tr>";
  175. count = 1
  176. for r4 in db.query(q4):
  177. # 特定合約的所有執行項(無預定完成日期)
  178. q6 = "SELECT * \
  179. FROM execute_detail ed \
  180. left JOIN execute_form ef ON ed.exf_id=ef.exf_id \
  181. WHERE ef.is_close='N' AND ef.is_delete='N' \
  182. AND ed.is_delete = 'N' AND ed.is_complete='N' \
  183. AND ed.set_date = '0000-00-00' \
  184. AND ef.exf_id = '" + str(r4['exf_id']) + "' \
  185. AND ed.execute_man = '" + execute_man + "'"
  186. q6_result_count = str(len(list(db.query(q6)))+1)
  187. idx = 0
  188. for r6 in db.query(q6):
  189. url = 'https://stage-backstage.hhh.com.tw/admin/home/execute?exf_id=' + str(r6['exf_id'])
  190. if idx < 1:
  191. html2 += "<tr><td rowspan='" + q6_result_count + "'>" + str(count) + "</td><td rowspan='" + q6_result_count + "'><a href='" + url + "' target='_blank'>" + r6['num'] + "</a></td><td rowspan='" + q6_result_count + "'>" + r6['company'] + "</td><td rowspan='" + q6_result_count + "'>" + str(r6['contract_time']) + "</td><td rowspan='" + q6_result_count + "'>" + r6['sales_man'] + "</td><td>" + r6['lv1'] + "_" + r6['lv2'] + "</td><tr>"
  192. idx += 1
  193. else:
  194. html2 += "<tr><td>" + r6['lv1'] + "_" + r6['lv2'] + "</td></tr>"
  195. count += 1
  196. html2 += "</table>";
  197. else:
  198. print (execute_man + " 皆有預定完成日期,不須處理")
  199. #html2 += "<div>" + execute_man + " 皆有預定完成日期,不須處理</div>"
  200. continue
  201. html2 += """
  202. </body>
  203. </html>
  204. """
  205. # Email清單
  206. email_list = ['mike@choozmo.com', 'stacy@choozmo.com']
  207. # 寄送無預定完成日期Email通知
  208. for email in email_list:
  209. print ("發送Email: " + email)
  210. hhh_send_mail(email, html2)
  211. print ('發送執行項目通知(無預定完成日期) (執行單位) end...')