execute_detail_notice.py 11 KB

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