0

Оптимизация

This commit is contained in:
2016-05-23 09:24:21 +03:00
parent 5017f5f332
commit 0cad49fdfa
5 changed files with 86 additions and 72 deletions

View File

@@ -5,15 +5,23 @@
package ru.dmitriymx.astralcheckreport; package ru.dmitriymx.astralcheckreport;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject; 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.Browser;
import ru.dmitriymx.tools.browser.SimpleBrowser; import ru.dmitriymx.tools.browser.SimpleBrowser;
public class AstralSession { class AstralSession {
private Browser browser = new SimpleBrowser(); 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 = String postData =
"ScriptManager1=UpdatePanel1%7CbtnLogin&" + "ScriptManager1=UpdatePanel1%7CbtnLogin&" +
"__EVENTTARGET=&" + "__EVENTTARGET=&" +
@@ -27,7 +35,7 @@ public class AstralSession {
browser.post("http://reg.astralnalog.ru/Authorisation.aspx", postData); 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," + String postData = "{\"context\":{\"CurrentPageIndex\":1,\"RecordsPerPage\":30," +
"\"AbonentName\":\"\",\"CompanyGroupName\":\"\"," + "\"AbonentName\":\"\",\"CompanyGroupName\":\"\"," +
"\"Inn\":\"" + inn + "\",\"Kpp\":\"" + kpp + "\"," + "\"Inn\":\"" + inn + "\",\"Kpp\":\"" + kpp + "\"," +
@@ -44,12 +52,11 @@ public class AstralSession {
postData = "{\"abonentId\":\""+ id +"\"}"; postData = "{\"abonentId\":\""+ id +"\"}";
jsonStr = browser.post("http://reg.astralnalog.ru/AbonentModule.aspx/GetClientAbonentById", postData); jsonStr = browser.post("http://reg.astralnalog.ru/AbonentModule.aspx/GetClientAbonentById", postData);
jsonObj = str2jsonObj(jsonStr); 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 postData = "{\"productId\":" + productId + "}";
String jsonStr = browser.post("http://reg.astralnalog.ru/AbonentModule.aspx/GetCertificatesHistory", postData); String jsonStr = browser.post("http://reg.astralnalog.ru/AbonentModule.aspx/GetCertificatesHistory", postData);
@@ -60,9 +67,4 @@ public class AstralSession {
private JsonObject str2jsonObj(String jsonStr) { private JsonObject str2jsonObj(String jsonStr) {
return gson.fromJson(jsonStr, JsonObject.class); return gson.fromJson(jsonStr, JsonObject.class);
} }
@Override
public String toString() {
return super.toString();
}
} }

View File

@@ -4,17 +4,15 @@
*/ */
package ru.dmitriymx.astralcheckreport; package ru.dmitriymx.astralcheckreport;
public class ExcelDataRow { class ExcelDataRow {
public String inn, kpp, tarif; String inn, kpp, tarif;
public int addonCert; int addonCert;
public int row;
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.inn = inn;
this.kpp = kpp; this.kpp = kpp;
this.tarif = tarif; this.tarif = tarif;
this.addonCert = addonCert; this.addonCert = addonCert;
this.row = row;
} }
@Override @Override

View File

@@ -10,18 +10,17 @@ import org.apache.poi.ss.usermodel.*;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
public class ExcelReader { class ExcelReader {
private Workbook xls;
private Sheet sheet; private Sheet sheet;
private int startRow = 10, private int startRow = 10,
currentRow = startRow; currentRow = startRow;
public ExcelReader(File file, String sheetName) throws IOException, InvalidFormatException { ExcelReader(File file, String sheetName) throws IOException, InvalidFormatException {
xls = WorkbookFactory.create(file); Workbook xls = WorkbookFactory.create(file);
sheet = xls.getSheet(sheetName); sheet = xls.getSheet(sheetName);
} }
public ExcelDataRow getNextData() { ExcelDataRow getNextData() {
Row row = sheet.getRow(currentRow++); Row row = sheet.getRow(currentRow++);
Cell cellInn = row.getCell(2); Cell cellInn = row.getCell(2);
cellInn.setCellType(Cell.CELL_TYPE_STRING); cellInn.setCellType(Cell.CELL_TYPE_STRING);
@@ -36,7 +35,7 @@ public class ExcelReader {
cellInn.getStringCellValue(), cellInn.getStringCellValue(),
cellKpp.getStringCellValue(), cellKpp.getStringCellValue(),
cellTarif.getStringCellValue(), cellTarif.getStringCellValue(),
(int)cellAddonCert.getNumericCellValue(), (int)cellAddonCert.getNumericCellValue()
currentRow); );
} }
} }

View File

@@ -4,16 +4,13 @@
*/ */
package ru.dmitriymx.astralcheckreport; package ru.dmitriymx.astralcheckreport;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import ru.dmitriymx.astralcheckreport.objects.Organization;
import ru.dmitriymx.astralcheckreport.objects.Product; import ru.dmitriymx.astralcheckreport.objects.Product;
import ru.dmitriymx.astralcheckreport.objects.Tarif; import ru.dmitriymx.astralcheckreport.objects.Tarif;
import ru.dmitriymx.astralcheckreport.objects.TarifDeserialize;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@@ -24,19 +21,12 @@ public class Main implements Runnable {
private Logger logger = LoggerFactory.getLogger(Main.class); private Logger logger = LoggerFactory.getLogger(Main.class);
private ExcelReader excel; private ExcelReader excel;
private Properties properties = new Properties(); private Properties properties = new Properties();
private Gson gson;
public static void main(String[] args) { public static void main(String[] args) {
new Main().start(); new Main().start();
} }
public Main() { private void start() {
gson = new GsonBuilder()
.registerTypeAdapter(Tarif.class, new TarifDeserialize())
.create();
}
public void start() {
logger.info("Astral check report v1.0"); logger.info("Astral check report v1.0");
logger.trace("Init config"); logger.trace("Init config");
@@ -49,8 +39,7 @@ public class Main implements Runnable {
logger.trace("Load excel file"); logger.trace("Load excel file");
try { try {
excel = new ExcelReader(new File(properties.getProperty("file")), excel = new ExcelReader(new File(properties.getProperty("file")), "Операции");
properties.getProperty("sheet"));
} catch (IOException | InvalidFormatException e) { } catch (IOException | InvalidFormatException e) {
logger.error("Load excel file", e); logger.error("Load excel file", e);
return; return;
@@ -70,11 +59,11 @@ public class Main implements Runnable {
} }
public synchronized ExcelDataRow nextData() { private synchronized ExcelDataRow nextData() {
return excel.getNextData(); return excel.getNextData();
} }
public static void safeSleep(long ms) { private static void safeSleep(long ms) {
try { try {
Thread.sleep(ms); Thread.sleep(ms);
} catch (InterruptedException ignore) { } catch (InterruptedException ignore) {
@@ -92,43 +81,51 @@ public class Main implements Runnable {
ExcelDataRow data; ExcelDataRow data;
while( !Thread.currentThread().isInterrupted() && ((data = nextData()) != null) ) { while( !Thread.currentThread().isInterrupted() && ((data = nextData()) != null) ) {
if (!data.tarif.equalsIgnoreCase("Акция 3 месяца")) continue; //FIXME if (!data.tarif.equalsIgnoreCase("Акция 3 месяца")) continue; //FIXME
logger.info("Check {} / {} ...", data.inn, data.kpp); logger.info("Проверяю {} / {} ...", data.inn, data.kpp);
logger.debug("data = {}", data); logger.debug("data = {}", data);
JsonObject jsonObj = astral.findOrg(data.inn, data.kpp); Organization organization = astral.getOrganization(data.inn, data.kpp);
logger.debug("result = {}", jsonObj.toString()); logger.debug("result = {}", organization);
if (data.tarif.equalsIgnoreCase("Акция 3 месяца")) { if (data.tarif.equalsIgnoreCase("Акция 3 месяца")) {
Product product = gson.fromJson(jsonObj.get("products").getAsJsonArray().get(0), Product.class); tarif_Akcia3Mount(astral, organization, data);
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("андартный (годовое обслуживание)")) {
logger.info("No logic tarif: \"{}\"", tarif.name);
} else {
logger.trace("Ignore tarif: \"{}\"", tarif.name);
}
} }
safeSleep(200); 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("андартный (годовое обслуживание)")) {
logger.warn("Логика тарифа не настроена: \"{}\"", tarif.name);
}
else {
logger.trace("Тариф игнорируется: \"{}\"", tarif.name);
}
}
} }

View File

@@ -0,0 +1,18 @@
/*
* DmitriyMX <mail@dmitriymx.ru>
* 2016-05-23
*/
package ru.dmitriymx.astralcheckreport.objects;
import java.util.List;
public class Organization {
private int id;
public String inn, kpp;
public List<Product> products;
@Override
public String toString() {
return "Organization@{id:" + id + ", inn:\"" + inn + "\", kpp:\"" + kpp + "\"}";
}
}