diff --git a/src/main/java/ru/dmitriymx/astralcheckreport/AstralSession.java b/src/main/java/ru/dmitriymx/astralcheckreport/AstralSession.java index e4b95d1..316881b 100644 --- a/src/main/java/ru/dmitriymx/astralcheckreport/AstralSession.java +++ b/src/main/java/ru/dmitriymx/astralcheckreport/AstralSession.java @@ -5,15 +5,23 @@ package ru.dmitriymx.astralcheckreport; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import com.google.gson.JsonObject; +import ru.dmitriymx.astralcheckreport.objects.Organization; +import ru.dmitriymx.astralcheckreport.objects.Tarif; +import ru.dmitriymx.astralcheckreport.objects.TarifDeserialize; import ru.dmitriymx.tools.browser.Browser; import ru.dmitriymx.tools.browser.SimpleBrowser; -public class AstralSession { +class AstralSession { private Browser browser = new SimpleBrowser(); - private Gson gson = new Gson(); + private Gson gson; + + AstralSession(String login, String password) { + gson = new GsonBuilder() + .registerTypeAdapter(Tarif.class, new TarifDeserialize()) + .create(); - public AstralSession(String login, String password) { String postData = "ScriptManager1=UpdatePanel1%7CbtnLogin&" + "__EVENTTARGET=&" + @@ -27,7 +35,7 @@ public class AstralSession { browser.post("http://reg.astralnalog.ru/Authorisation.aspx", postData); } - public JsonObject findOrg(String inn, String kpp) { + Organization getOrganization(String inn, String kpp) { String postData = "{\"context\":{\"CurrentPageIndex\":1,\"RecordsPerPage\":30," + "\"AbonentName\":\"\",\"CompanyGroupName\":\"\"," + "\"Inn\":\"" + inn + "\",\"Kpp\":\"" + kpp + "\"," + @@ -44,12 +52,11 @@ public class AstralSession { postData = "{\"abonentId\":\""+ id +"\"}"; jsonStr = browser.post("http://reg.astralnalog.ru/AbonentModule.aspx/GetClientAbonentById", postData); jsonObj = str2jsonObj(jsonStr); - jsonObj = str2jsonObj(jsonObj.get("d").getAsString()); - return jsonObj; + return gson.fromJson(jsonObj.get("d").getAsString(), Organization.class); } - public JsonObject historyCert(int productId) { + JsonObject historyCert(int productId) { String postData = "{\"productId\":" + productId + "}"; String jsonStr = browser.post("http://reg.astralnalog.ru/AbonentModule.aspx/GetCertificatesHistory", postData); @@ -60,9 +67,4 @@ public class AstralSession { private JsonObject str2jsonObj(String jsonStr) { return gson.fromJson(jsonStr, JsonObject.class); } - - @Override - public String toString() { - return super.toString(); - } } diff --git a/src/main/java/ru/dmitriymx/astralcheckreport/ExcelDataRow.java b/src/main/java/ru/dmitriymx/astralcheckreport/ExcelDataRow.java index 2a6e532..dc04d8a 100644 --- a/src/main/java/ru/dmitriymx/astralcheckreport/ExcelDataRow.java +++ b/src/main/java/ru/dmitriymx/astralcheckreport/ExcelDataRow.java @@ -4,17 +4,15 @@ */ package ru.dmitriymx.astralcheckreport; -public class ExcelDataRow { - public String inn, kpp, tarif; - public int addonCert; - public int row; +class ExcelDataRow { + String inn, kpp, tarif; + int addonCert; - public ExcelDataRow(String inn, String kpp, String tarif, int addonCert, int row) { + ExcelDataRow(String inn, String kpp, String tarif, int addonCert) { this.inn = inn; this.kpp = kpp; this.tarif = tarif; this.addonCert = addonCert; - this.row = row; } @Override diff --git a/src/main/java/ru/dmitriymx/astralcheckreport/ExcelReader.java b/src/main/java/ru/dmitriymx/astralcheckreport/ExcelReader.java index 6f5faca..d1bb971 100644 --- a/src/main/java/ru/dmitriymx/astralcheckreport/ExcelReader.java +++ b/src/main/java/ru/dmitriymx/astralcheckreport/ExcelReader.java @@ -10,18 +10,17 @@ import org.apache.poi.ss.usermodel.*; import java.io.File; import java.io.IOException; -public class ExcelReader { - private Workbook xls; +class ExcelReader { private Sheet sheet; private int startRow = 10, currentRow = startRow; - public ExcelReader(File file, String sheetName) throws IOException, InvalidFormatException { - xls = WorkbookFactory.create(file); + ExcelReader(File file, String sheetName) throws IOException, InvalidFormatException { + Workbook xls = WorkbookFactory.create(file); sheet = xls.getSheet(sheetName); } - public ExcelDataRow getNextData() { + ExcelDataRow getNextData() { Row row = sheet.getRow(currentRow++); Cell cellInn = row.getCell(2); cellInn.setCellType(Cell.CELL_TYPE_STRING); @@ -36,7 +35,7 @@ public class ExcelReader { cellInn.getStringCellValue(), cellKpp.getStringCellValue(), cellTarif.getStringCellValue(), - (int)cellAddonCert.getNumericCellValue(), - currentRow); + (int)cellAddonCert.getNumericCellValue() + ); } } diff --git a/src/main/java/ru/dmitriymx/astralcheckreport/Main.java b/src/main/java/ru/dmitriymx/astralcheckreport/Main.java index 48c4d1a..17fd91d 100644 --- a/src/main/java/ru/dmitriymx/astralcheckreport/Main.java +++ b/src/main/java/ru/dmitriymx/astralcheckreport/Main.java @@ -4,16 +4,13 @@ */ package ru.dmitriymx.astralcheckreport; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; import com.google.gson.JsonObject; -import com.google.gson.reflect.TypeToken; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import ru.dmitriymx.astralcheckreport.objects.Organization; import ru.dmitriymx.astralcheckreport.objects.Product; import ru.dmitriymx.astralcheckreport.objects.Tarif; -import ru.dmitriymx.astralcheckreport.objects.TarifDeserialize; import java.io.File; import java.io.IOException; @@ -24,19 +21,12 @@ public class Main implements Runnable { private Logger logger = LoggerFactory.getLogger(Main.class); private ExcelReader excel; private Properties properties = new Properties(); - private Gson gson; public static void main(String[] args) { new Main().start(); } - public Main() { - gson = new GsonBuilder() - .registerTypeAdapter(Tarif.class, new TarifDeserialize()) - .create(); - } - - public void start() { + private void start() { logger.info("Astral check report v1.0"); logger.trace("Init config"); @@ -49,8 +39,7 @@ public class Main implements Runnable { logger.trace("Load excel file"); try { - excel = new ExcelReader(new File(properties.getProperty("file")), - properties.getProperty("sheet")); + excel = new ExcelReader(new File(properties.getProperty("file")), "Операции"); } catch (IOException | InvalidFormatException e) { logger.error("Load excel file", e); return; @@ -70,11 +59,11 @@ public class Main implements Runnable { } - public synchronized ExcelDataRow nextData() { + private synchronized ExcelDataRow nextData() { return excel.getNextData(); } - public static void safeSleep(long ms) { + private static void safeSleep(long ms) { try { Thread.sleep(ms); } catch (InterruptedException ignore) { @@ -92,43 +81,51 @@ public class Main implements Runnable { ExcelDataRow data; while( !Thread.currentThread().isInterrupted() && ((data = nextData()) != null) ) { if (!data.tarif.equalsIgnoreCase("Акция 3 месяца")) continue; //FIXME - logger.info("Check {} / {} ...", data.inn, data.kpp); + logger.info("Проверяю {} / {} ...", data.inn, data.kpp); logger.debug("data = {}", data); - JsonObject jsonObj = astral.findOrg(data.inn, data.kpp); - logger.debug("result = {}", jsonObj.toString()); + Organization organization = astral.getOrganization(data.inn, data.kpp); + logger.debug("result = {}", organization); if (data.tarif.equalsIgnoreCase("Акция 3 месяца")) { - Product product = gson.fromJson(jsonObj.get("products").getAsJsonArray().get(0), Product.class); - Tarif tarif = product.contractTariffs.get(0); - - if (tarif.name.equalsIgnoreCase("Промо (3 месяца бесплатно)")) { - // 2016-03-25 - int mountInit = Integer.parseInt(tarif.initialDate.substring(5, 7)); - int mountEnd = Integer.parseInt(tarif.endDate.substring(5, 7)); - int delta = mountEnd - mountInit; - - if (delta != 3) { - logger.warn("({}/{}): tarif date error!", data.inn, data.kpp); - continue; - } - - JsonObject historyCert = astral.historyCert(product.id); - - int totalCert = historyCert.get("total").getAsInt(); - if (totalCert > 1) { - if (totalCert != (1 + data.addonCert)) { - logger.warn("({}/{}): error history cert", data.inn, data.kpp); - continue; - } - } - } else if (tarif.name.equalsIgnoreCase("Cтандартный (годовое обслуживание)")) { - logger.info("No logic tarif: \"{}\"", tarif.name); - } else { - logger.trace("Ignore tarif: \"{}\"", tarif.name); - } + tarif_Akcia3Mount(astral, organization, data); } safeSleep(200); } } + + // Тариф: Акция 3 месяца + private void tarif_Akcia3Mount(AstralSession astral, Organization organization, ExcelDataRow data) { + Product product = organization.products.get(0); + Tarif tarif = product.contractTariffs.get(0); + + if (tarif.name.equalsIgnoreCase("Промо (3 месяца бесплатно)")) { + // Step I - Проверяем разницу дат. + // Разница должна быть 3 месяца + int mountInit = Integer.parseInt(tarif.initialDate.substring(5, 7)); + int mountEnd = Integer.parseInt(tarif.endDate.substring(5, 7)); + if ((mountEnd - mountInit) != 3) { + logger.error("({}/{}): Ошибка в подсчете дат!", organization.inn, organization.kpp); + return; + } + + // Step II - Проверка истории сертификатов + // Сертификатов должно быть: + // - либо один + // - либо соответствовать формуле "1 + доп.сертификаты" + JsonObject historyCert = astral.historyCert(product.id); + int totalCert = historyCert.get("total").getAsInt(); + if (totalCert > 1) { + if (totalCert != (1 + data.addonCert)) { + logger.error("({}/{}): Ошибка количества сертификатов!", organization.inn, organization.kpp); + } + } + } + else if (tarif.name.equalsIgnoreCase("Cтандартный (годовое обслуживание)")) { + logger.warn("Логика тарифа не настроена: \"{}\"", tarif.name); + } + else { + logger.trace("Тариф игнорируется: \"{}\"", tarif.name); + } + } } diff --git a/src/main/java/ru/dmitriymx/astralcheckreport/objects/Organization.java b/src/main/java/ru/dmitriymx/astralcheckreport/objects/Organization.java new file mode 100644 index 0000000..416d903 --- /dev/null +++ b/src/main/java/ru/dmitriymx/astralcheckreport/objects/Organization.java @@ -0,0 +1,18 @@ +/* + * DmitriyMX + * 2016-05-23 + */ +package ru.dmitriymx.astralcheckreport.objects; + +import java.util.List; + +public class Organization { + private int id; + public String inn, kpp; + public List products; + + @Override + public String toString() { + return "Organization@{id:" + id + ", inn:\"" + inn + "\", kpp:\"" + kpp + "\"}"; + } +}