INSERT INTO #{dynamic_securityFlow}.tlk_certificate (LASTMODIFIED, CREATED, FORMID, APPLICATIONID, DOMAINID, ITEM_domain_id, ITEM_companyName, ITEM_securityName, ITEM_documentType, ITEM_documentID, ITEM_certificateType, ITEM_certificateNO, ITEM_level, ITEM_certificateFrom, ITEM_status, ITEM_certificateDate, ID, ITEM_certificateFromNO, ITEM_MARK,  ITEM_TRAININGCOMNAME, ITEM_PEIXUNID, ITEM_confirmDate, item_isInMyCompany, ITEM_areaCode,ITEM_REGIONALORIGIN) VALUES ('#{LASTMODIFIED}', '#{CREATED}', '__TTeElnX8IULbf7iTxsM', '__wGomeHJBz6CpLFCCl4y', '#{dynamic_domain_id}', '#{ITEM_COMPANYNAMETOGETCODE}', '#{ITEM_COMPANYNAMETOGET}', '#{ITEM_NAME}', '#{ITEM_IDCARDTYPE}', '#{ITEM_IDCARDNO}', '#{ITEM_CERTIFICATETYPE}', '#{ITEM_CERTIFICATENO}', '#{ITEM_LEVEL}', '#{ITEM_CERTIFICATEFROM}', '#{ITEM_STATUS}', '#{ITEM_CERTIFICATEDATE}', replace('#{id}','__JPHvq5gsbT0nuFotaqq','__TTeElnX8IULbf7iTxsM'), '#{ITEM_CERTIFICATEFROMNO}', '#{ITEM_MARK}', '#{ITEM_CULTIVATECOMPANY}', '#{ITEM_CULTIVATECOMPANYCODE}', '#{ITEM_CONFIRMDATE}', ifnull((select ITEM_isInMyCompany from #{dynamic_securityFlow}.tlk_securityman where ITEM_documentID ='#{ITEM_IDCARDNO}' ORDER BY ITEM_entryDate desc limit 1),0), '#{ITEM_AREACODE}','#{ITEM_AREACODE}')ON DUPLICATE KEY UPDATE FORMID='__TTeElnX8IULbf7iTxsM',ITEM_REGIONALORIGIN='#{ITEM_AREACODE}';
UPDATE
  #{dynamic_securityFlow}.tlk_securityman SET ITEM_CERTIFICATENO=(SELECT tla.ITEM_CERTIFICATENO FROM #{dynamic_securityFlow}.tlk_certificate tla where tla.item_documentid='#{ITEM_IDCARDNO}' and ITEM_CERTIFICATETYPE = '1' limit 1)
where
   ITEM_documentID = '#{ITEM_IDCARDNO}';