import com.groupstp.rtneo.entity.*
import com.haulmont.cuba.core.global.*;
import com.haulmont.cuba.core.app.*;
import com.haulmont.cuba.core.entity.EntitySnapshot;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
DataManager dataManager = AppBeans.get(DataManager.NAME);
Contract contract = dataManager.load(Contract.class).id(UUID.fromString('73834655-6998-77dd-791e-62ebd9ebed66')).view("contract-print").optional().orElse(null);
List<ContractPositionContainerYard> cpcyHistory = []
List<HashMap<String, Object>> itemsCY = []
int startCount = 1
for(ContractPosition cp : contract.getPositions()){
log.debug("${cp.getContragentRealEstate().getRealEstate().getCadastralNumber()}")
cpcyHistory = getHistoryCY(cp.getContract().getContragent(), cp)
itemsCY.add(getHistoryPrintItemsCY(cpcyHistory, startCount++))
// log.debug(itemsCY)
// break
}
//Вывод
for(def items : itemsCY){
log.debug("**********************************")
for(def item : items){
log.debug(item)
}
}
//Метод, например выбирающий, пропускать сущность из истории или нет
//**************************************************************
//Получить КП для печати
//Нужно разделить(на 3. логика выбор! создание)
//**************************************************************
List<HashMap<String, Object>> getHistoryPrintItemsCY(List<ContractPositionContainerYard> cpcyHistory, Integer startCount){
SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy")
HashMap<String, Object> cyHash = [:]
List<HashMap<String, Object>> cyItemList = []
char nChar = 'А'
def nInt = Double.valueOf(startCount)
def n = null
def prevCode = null
def prevFrom = null
def prevBefore = null
def prevCre = null
def prevCP = null
def prevN = null
char prevNChar = nChar
def prevNInt = nInt
log.debug(cpcyHistory.size())
for(ContractPositionContainerYard cpcy : cpcyHistory){
def changeCY = false
def changeCre = false
//Логика выбора
if(!prevCode.equals(cpcy.getContainerYard().getCode())){
changeCY = true
}
log.debug("${prevCre}")
if(prevCre != null){log.debug("--${prevCre.getCalculationAmount()} ${cpcy.getContractPosition().getContragentRealEstate().getCalculationAmount()}")}
if(prevCre != null && (prevCre.getCalculationAmount() != cpcy.getContractPosition().getContragentRealEstate().getCalculationAmount())){
changeCre = true
}
if(!prevCre.equals(cpcy.getContractPosition().getContragentRealEstate())){
changeCre = true
}
log.debug("1111${changeCre}")
if(!changeCre & !changeCY)continue
if(prevFrom.equals(cpcy.getContractPosition().getContract().getFrom())){
cyItemList.remove(cyItemList.size()-1)
n = prevN
}else{
//Получение номера вынести в отдельный метод
if(changeCY & !changeCre){
n = nChar++
}
if(changeCre){
nChar = 'А'
if(nInt%1 == 0.0){
n = nInt
nInt +=0.1
}else{
n = nInt
nInt +=0.1
}
}
//
if(cyItemList.size()>0)cyItemList.get(cyItemList.size()-1)["endBefore"] = df.format(cpcy.getContractPosition().getContract().getFrom())
}
def pos = createContainerItem(cpcy, n, df.format(cpcy.getContractPosition().getContract().getFrom()), df.format(cpcy.getContractPosition().getContract().getBefore()))
cyItemList.push(pos)
//Устанавливаем предыдущие значения
prevCode = cpcy.getContainerYard().getCode()
prevFrom = cpcy.getContractPosition().getContract().getFrom()
prevBefore = cpcy.getContractPosition().getContract().getBefore()
prevCre = cpcy.getContractPosition().getContragentRealEstate()
// if(!isPrevN)prevN = n
prevN = n
prevNChar = nChar
prevNInt = nInt
//Конец выбора
}
//log.debug(cyItemList)
return cyItemList
}
//**************************************************************
//Получить историю КП для объекта
//**************************************************************
List<ContractPositionContainerYard> getHistoryCY(Contragent contragent, ContractPosition contractPosition){
List<ContractPosition> cpList = getHistoryCP(contragent, contractPosition)
List<ContractPositionContainerYard> cpcyList = []
for(ContractPosition cp : cpList){
for(ContractPositionContainerYard cpcy : cp.getContainerYards()){
cpcyList.push(cpcy)
}
}
return cpcyList
}
//**************************************************************
//Получить историю позиции договора
//**************************************************************
List<ContractPosition> getHistoryCP(Contragent contragent, ContractPosition cp){
DataManager dataManager = AppBeans.get(DataManager.NAME);
EntitySnapshotService entitySnapshotService = AppBeans.get(EntitySnapshotService.NAME);
List<SnapshotHistory> listSnapshotHistory = dataManager.load(SnapshotHistory.class)
.query('select s from rtneo$SnapshotHistory s where s.contragent.id = :contragent and s.contract.accepted = true order by s.contract.createTs asc')
.parameter("contragent", contragent.getId())
.view("_local")
.list();
List<ContractPosition> cpList = []
for(def snapshotHistory : listSnapshotHistory){
def entitySnapshot = dataManager.load(EntitySnapshot.class).id(snapshotHistory.getSnapshot()).optional().orElse(null)
Contract entity = entitySnapshotService.extractEntity(entitySnapshot)
for(ContractPosition cpItem : entity.getPositions()){
if(cp.getContragentRealEstate().getId() == cpItem.getContragentRealEstate().getId())
cpList.push(cpItem)
}
}
return cpList;
}
def createContainerItem(ContractPositionContainerYard cpcy, Object n, String from, String before){
def pos = [
realEstateName:cpcy.getContractPosition().getContragentRealEstate().getRealEstate().getName(),
cadastralNumber:cpcy.getContractPosition().getContragentRealEstate().getRealEstate().getCadastralNumber(),
address:cpcy.getContractPosition().getContragentRealEstate().getRealEstate().getAddress(),
startFrom:from,
endBefore:before,
containerYardNumber:cpcy.getContainerYard().getCode(),
n:n
]
// log.debug(pos)
return pos
}