      
      {} ks)))
 
-
 (defn do-parse-rows [parser h-rows]
   "Разбор всех строк"
   (map #(get-cell-and-parse parser %) h-rows))
 
+;; tender.sk.kz ------------------------------------------------------------------
 
-(defn ->->> [x f & xx]
-  "Функция - модификатор вызова"
-  (apply f (conj (vec xx) x)))
+(defn tender-sk-kz-lots []
+  "Получение данных лотов по адресу http://tender.sk.kz/index.php/ru/lots"
+  (let [res  "tender.sk.kz"
+        url  "http://tender.sk.kz/index.php/ru/lots"]
+    (-> (h/html-resource (java.net.URL. url))
+        (h/select [:.showtab :tr])
+        rest ;; Убираем шапку таблицы, берем только тело
+        (->->> map #(h/select % [:td]))
+        (->->> map (fn [x] (map #(first (% :content)) x)))
+        (->->> do-parse-rows {:keyname [0,str]
+                              :caption [2,#(first (% :content))]
+                              :description [3,str]
+                              :sum_all [6, #(bigdec (-> %
+                                                        (clojure.string/replace #" " "")
+                                                        (clojure.string/replace #"," ".")))]
+                               
+                              :bdate [8,#(tc/to-sql-date (tf/parse (tf/formatter "dd-MM-yyyy") %)) ]
+                              :edate [9,#(tc/to-sql-date (tf/parse (tf/formatter "dd-MM-yyyy") %)) ]
 
- 
-(defn tender-sk-kz-lots []
-  (-> (h/html-resource (java.net.URL. "http://tender.sk.kz/index.php/ru/lots"))
-      (h/select [:.showtab :tr])
-      rest ;; Убираем шапку таблицы
-      (->->> map #(h/select % [:td]))
-      (->->> map (fn [x] (map #(first (% :content)) x)))
-      (->->> do-parse-rows {:keyname [0,str]
-                            :caption [2,#(first (% :content))]
-                            :description [3,str]
-                            :sum_all [6, #(bigdec (-> %
-                                                    (clojure.string/replace #" " "")
-                                                    (clojure.string/replace #"," ".")))]
-                            
-                            :bdate [8,#(tc/to-sql-date (tf/parse (tf/formatter "dd-MM-yyyy") %)) ]
-                            :edate [9,#(tc/to-sql-date (tf/parse (tf/formatter "dd-MM-yyyy") %)) ]
+                              :place [10,str]
 
-                            :place [10,str]
+                              } )
 
-                            } )
-      ;;first
-      ))
+        ;; Добавление служебной информации
+        (->->> map #(assoc % :url url :res res :updated (tc/to-sql-time (tco/now)) ))
+        
+        ;;first
+        )))
+
+;;(insert lots (values (tender-sk-kz-lots)))
 
 
+;; tender.sk.kz ...
 
 
+(defn insert-update-rows [rows]
+  "Дополнить/обновить записи"
+  (->> rows
+       (map (fn [{res :res keyname :keyname  :as row}]
+              (let [r (update lots (set-fields row) (where (and (= :res res) (= :keyname keyname))))]
+                (if (nil? r) row nil))))
+       
+       (filter #(not (nil? %)))
+       
+       (#(if (empty? %) :only-updated
+            (do (insert lots (values %)) :updated-and-inserted)))))
+              
+
+         
+    
 
 
 

  (table :lots)
  (pk :id))


;; PARSER ------------------------------------------------------------------------------------------


;; (defn comic-titles
;;   [n]
;;   (let [dom (h/html-resource
;;              (java.net.URL. "http://xkcd.com/archive"))
;;         title-nodes (h/select dom [:#middleContainer :a])
;;         titles (map h/text title-nodes)]
;;     (take n titles)))

;; (defn parse1 [] (h/html-resource
;;              (java.net.URL. "http://linux.org.ru")))



;; (defn tender-sk-kz-lots []
;;   "Распарсить сайт"
  
;;   (let [dom  (h/html-resource  (java.net.URL. "http://tender.sk.kz/index.php/ru/lots"))
;;         rows (h/select dom [:.showtab :tr])
        
;;         ]

;;     (first (map
;;             #(h/select % [:td])
           
;;             (rest rows)))

;;     ))

;; (defn tender-sk-kz-lots []
;;   "Распарсить сайт"
;;   ((comp 

;;     first
;;     (partial map
;;              (fn [x] (map #(first (% :content)) x)))
    
;;     (partial map #(h/select % [:td]))
;;     rest
;;     #(h/select % [:.showtab :tr])    
;;     ) (h/html-resource (java.net.URL. "http://tender.sk.kz/index.php/ru/lots"))))


;; (def column-keys
;;   "Колонки метаданных"
;;   [:stat 
;;    :keyname
;;    :caption
;;    :description
;;    :sum_all
;;    :place
;;    :bdate
;;    :edate])


(defn get-two-row [i1 i2 rows]
  "Тестовая функция"
  [(get (vec rows) i1) (get (vec rows) i2)])


(defn get-cell-and-parse [parser h-cells]
  "Функция разбора значений строки"

            [net.cgrand.enlive-html :as h]

            [overtone.at-at :as at]

            [clj-time.core :as tco]
            [clj-time.format :as tf]
            [clj-time.coerce :as tc]
            )
