<?php
namespace App\Controller;
use App\CropWise\Load\CropWiseReportDownload;
use App\CropWise\Load\CropWiseReportLoad;
use App\Entity\Agro\Card;
use App\Entity\Agro\Operation;
use App\Entity\Agro\Parameter;
use App\Entity\Field;
use App\Entity\Report\Report;
use App\Entity\Report\ReportValue;
use App\Entity\Report\Task;
use App\Entity\User;
use App\Method\Api\FireStore\FireBaseReportUpdater;
use App\Method\Api\FireStore\FireBaseUserUpdater;
use App\Method\Api\NeuralNetwork;
use App\Method\Api\Report\ReportConvertToWeb;
use App\Method\Api\Storage\ParamSaveImageStorage;
use App\Method\Api\Storage\ReportSaveImageStorage;
use App\Method\Api\Storage\SelectelStorage;
use App\Method\Api\Telegram\TelegramSender;
use App\Method\CopyImageToYandex;
use App\Model\ApiJsonResponseModel;
use App\Repository\Report\ReportRepository;
use App\Repository\Report\ReportValueRepository;
use Arhitector\Yandex\Disk;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Cache\CacheItem;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Routing\Annotation\Route;
class HomeController extends AbstractController
{
/**
* @Route("/", name="app_home")
*/
public function index()
{
return $this->redirectToRoute('app_login');
}
/**
* @Route("/telegram", name="app_home_telegram")
*/
public function test(FireBaseUserUpdater $baseUserUpdater, NeuralNetwork $neuralNetwork)
{
// $baseUserUpdater->updateAllUser();
// $report = $this->getDoctrine()->getRepository(Report::class)->find(30406);
// $baseReportUpdater->updateAllReport();
// $cropWiseReportLoad->run(30308,1190);
// $cropWiseReportLoad->run(30309,1188);
// $cropWiseReportLoad->run(30310,1188);
// $cropWiseReportLoad->run(3308,1190);
// $report = $this->getDoctrine()->getRepository(Report::class)->find(30363);
// $response = $telegramSender->send($report);
// dump($response);die;
$neuralNetwork->run();
return $this->json([
'message' => 'Welcome to your new controller!',
'path' => 'src/Controller/LoadFireBaseController.php',
]);
}
/**
* @Route("/telegram2", name="app_home_telegram2")
*/
public function telegram(TelegramSender $telegramSender,ReportSaveImageStorage $storage)
{
$reports = [];
// $reports = $this->getDoctrine()->getRepository(Report::class)->findBy([
// 'user' =>357,
// ]);
$telegramSender->setTest(true);
// $telegramSender->setOneImage(true);
$telegramSender->setDump(true);
$reports[] = $this->getDoctrine()->getRepository(Report::class)->find(39465);
// $reports[] = $this->getDoctrine()->getRepository(Report::class)->find(39466);
// $reports[] = $this->getDoctrine()->getRepository(Report::class)->find(39467);
// $reports[] = $this->getDoctrine()->getRepository(Report::class)->find(39468);
// $reports[] = $this->getDoctrine()->getRepository(Report::class)->find(39469);
// $reports[] = $this->getDoctrine()->getRepository(Report::class)->find(39470);
$count = 0;
foreach ($reports as $report) {
// $storage->save($report);
$response = $telegramSender->send($report);
// $count++;
// dump($count);
}
die;
// dump($response);die;
return $this->json([
'message' => 'Welcome to your new controller!',
'path' => 'src/Controller/LoadFireBaseController.php',
]);
}
/**
* @Route("/load/fire/base", name="app_load_fire_base")
*/
public function index2(FireBaseUserUpdater $fireBaseUserUpdater): JsonResponse
{
$fireBaseUserUpdater->updateAllUser();
return $this->json([
'message' => 'Welcome to your new controller!',
'path' => 'src/Controller/LoadFireBaseController.php',
]);
}
/**
* @Route("/log-save", name="app_recommendation_log_save")
*/
public function recommendationLogSave(Request $request, KernelInterface $kernel): JsonResponse
{
$dir = $kernel->getProjectDir()."/var/recommendation";
if(!is_dir($dir)){
mkdir($dir,0777,true);
}
$content = $request->request->get('content');
if($content){
file_put_contents($dir."/".date("Y-m-d_H-i-s-u"), json_encode($content));
return $this->json([
'status' => true,
]);
}
return $this->json([
'message' => 'Parameter "content" not found',
'status' => false,
]);
}
/**
* @Route("/log-view", name="app_recommendation_log_view")
*/
public function recommendationLogView(Request $request, KernelInterface $kernel): JsonResponse
{
$limit = $request->query->get('limit')?$request->query->get('limit'):20;
$page = $request->query->get('page')?$request->query->get('page'):1;
$offset = ($page-1)*$limit;
$dir = $kernel->getProjectDir()."/var/recommendation";
$files = glob($dir."/*");
$files = array_reverse($files);
$dumpFiles = array_splice($files, $offset,$limit);
foreach ($dumpFiles as $file) {
dump(json_decode(file_get_contents($file)));
}
if(count($files) > 20){
$dumpFiles = array_splice($files, 0,$limit);
foreach ($files as $file) {
unlink($file);
}
}
die;
}
/**
* @Route("/images", name="app_home_images_storage")
*/
public function images(SelectelStorage $storage, KernelInterface $kernel)
{
$dir = $kernel->getProjectDir()."/public/reports";
$dirThumb = $kernel->getProjectDir()."/public/thump";
$reports = $this->getDoctrine()->getRepository(ReportValue::class)->findAllImages(10);
foreach($reports as $value){
$path = $dir."/".$value->getImage();
$pathThump = $dirThumb."/".$value->getImage();
$key = $storage->createKey($value);
$object = $storage->save($path,$key);
$url = $this->getParameter('selectel_storage_public_domain')."/".$key;
if(!empty($object['ObjectURL']) && !empty($object['ObjectURL'])){
$value->setStorageImage($url);
$this->getDoctrine()->getManager()->persist($value);
$this->getDoctrine()->getManager()->flush();
}
// if(file_exists($pathThump)){
$command = 'convert -resize 100x100 -quality 50% '. $path." ".$pathThump;
exec($command);
$thumpKey = str_replace('params','thumb',$key);
$object = $storage->save($pathThump,$thumpKey);
// unlink($path);
// unlink($pathThump);
// }
}
}
/**
* @Route("/images-parameter", name="app_home_images_storage")
*/
public function imagesParameter(ParamSaveImageStorage $storage, KernelInterface $kernel)
{
$dir = $kernel->getProjectDir()."/public/params";
$start = microtime(true);
$end = (new \DateTime("+ 55 minutes"))->getTimestamp();
$reports = $this->getDoctrine()->getRepository(Parameter::class)->findAllImagesByOperation(9445);
foreach($reports as $value){
if($end < $start){
return 1;
}
$object = $storage->save($value);
}
dd($reports);
}
/**
* @Route("/images-report", name="app_home_images_storage_report")
*/
public function imagesReport(ReportSaveImageStorage $storage, KernelInterface $kernel)
{
$dir = $kernel->getProjectDir()."/public/reports";
$start = microtime(true);
$end = (new \DateTime("+ 55 minutes"))->getTimestamp();
$reports = $this->getDoctrine()->getRepository(Report::class)->findAllImages(100);
foreach($reports as $value){
if($end < $start){
return 1;
}
$object = $storage->save($value);
}
}
/**
* @Route("/field-change-to-field", name="app_home_field_change_to_field")
*/
public function changeField()
{
die;
$seasons = [
// 2023,
2024
];
$fileldIdDonor = 3140;
$fileldIdTarget = 22054;
/** @var Field $donor */
$donor = $this->getDoctrine()->getRepository(Field::class)->find($fileldIdDonor);
$target = $this->getDoctrine()->getRepository(Field::class)->find($fileldIdTarget);
// if($donor->getTechcard()->getId() == $target->getTechcard()->getId()) {
$donor->setHash();
$target->setHash();
$reports = $donor->getReports();
foreach ($reports as $report) {
if(in_array($report->getTechcard()->getSeason()->getName(),$seasons)){
$report->setField($target);
$this->getDoctrine()->getManager()->persist($report);
}
}
$this->getDoctrine()->getManager()->flush();
$objectives = $donor->getObjectives();
foreach ($objectives as $objective) {
if(in_array($objective->getSeason()->getName(),$seasons)) {
$objective->setField($target);
$objective->setHash();
$this->getDoctrine()->getManager()->persist($objective);
}
}
$this->getDoctrine()->getManager()->flush();
$tasks = $donor->getTaskLists();
foreach ($tasks as $task) {
if(in_array($task->getSeason()->getName(),$seasons)) {
$task->setField($target);
$this->getDoctrine()->getManager()->persist($task);
}
}
$this->getDoctrine()->getManager()->persist($donor);
$this->getDoctrine()->getManager()->persist($target);
$this->getDoctrine()->getManager()->flush();
die('complete');
// }
// die('techcard -');
}
/**
* @Route("/send-report-cropio", name="app_home_send_report_cropio")
*/
public function sendReportCropiowise( ReportRepository $reportRepository,
CropWiseReportLoad $cropWiseReportLoad)
{
$reports = $reportRepository->findAllForCropWise();
/** @var Report $report */
$all = count($reports);
// $reports = [];
// $reports[] = $this->getDoctrine()->getRepository(Report::class)->find(49392);
$load = 0;
foreach ($reports as $report) {
$cropWiseReportLoad->run($report->getId(),$report->getField()->getId());
$load++;
$now = microtime(true);
}
die('end');
}
/**
* @Route("/download-report-cropwise", name="app_home_download_report_cropwise")
*/
public function downloadReport( CropWiseReportDownload $download, ReportConvertToWeb $reportConvertToWeb)
{
if(isset($_REQUEST['dev']) && $_REQUEST['dev'] == '1') {
$this->removedublicateTemplate();
$this->removeUser();die;
}
$fields = $this->getDoctrine()->getRepository(Field::class)->findAllDownloadCropWiseReport();
if(!empty($fields)) {
foreach ($fields as $key => $field) {
$download->run($field->getId());
}
}die;
$download->run(22815);
}
public function removedublicateTemplate()
{
$em = $this->getDoctrine()->getManager();
$em->flush();
$count = 0;
$ids = ['64','54',10,52,54,55,42,13,9,11,12,13,14,15,16,0,45,48];
foreach ($ids as $id) {
$operations = $this->getDoctrine()->getRepository(Operation::class)->findBy(["cropWiseTemplateId" => $id]);
$problems = $this->getDoctrine()->getRepository(Operation::class)->findBy(["cropWiseGrowthStageId" => 0]);
$operations = array_merge($operations,$problems);
foreach ($operations as $operation) {
// $operation = $this->getDoctrine()->getRepository(Operation::class)->find(21688);
$tasks = $this->getDoctrine()->getRepository(Task::class)->findBy(["operation" => $operation->getId()]);
foreach ($tasks as $task) {
$em->remove($task);
}
foreach ($operation->getObjectives() as $objective) {
/** @var Report $report */
foreach ($objective->getReports() as $report) {
$task = $report->getTask();
$task->setOperation(null);
$em->persist($task);
$getObjective = $report->getObjective();
$em->persist($task->getTaskList());
$em->persist($task);
$em->persist($getObjective);
$report->setObjective(null);
$report->setTask(null);
foreach ($report->getReportValues() as $value) {
$report->removeReportValue($value);
$em->remove($value);
}
$objective->removeReport($report);
$em->persist($objective);
// $em->remove($objective);
$em->remove($report);
}
$em->remove($objective);
$em->flush();
}
/** @var Card $card */
foreach ($operation->getCards() as $card) {
foreach ($card->getAllParams(true) as $param) {
foreach ($param->getChildren(true) as $child) {
foreach ($child->getParametersValues() as $value) {
$em->remove($value);
$em->flush();
}
$card->removeParam($child);
$em->remove($child);
$em->flush();
}
foreach ($param->getAnswers(true) as $answer) {
foreach ($answer->getParametersValues() as $value) {
$em->remove($value);
$em->flush();
}
$card->removeParam($answer);
$em->remove($answer);
$em->flush();
}
foreach ($param->getParametersValues() as $value) {
$em->remove($value);
$em->flush();
}
$card->removeParam($param);
$em->remove($param);
$em->flush();
}
$em->persist($card);
$em->flush();
$em->remove($card);
$em->flush();
}
dump($count++);
dump($operation->getId());
$em->remove($operation);
$em->flush();
}
}
$reports = $this->getDoctrine()->getRepository(Report::class)->findBy(["cropWiseStatusDownload" => 'complete']);
foreach ($reports as $report) {
foreach ($report->getReportValues() as $value) {
$em->remove($value);
}
dump($report->getId());
$em->remove($report);
}
$em->flush();
die;
}
public function removeUser()
{
$em = $this->getDoctrine()->getManager();
for($id = 376;$id <= 437; $id++) {
$user = $this->getDoctrine()->getRepository(User::class)->find($id);
if(!empty($user) && strpos($user->getEmail(),'cerera2025@gap-rs.ru') !== false){
foreach ($user->getCreatorObjectives() as $creatorObjective) {
$tasks = $this->getDoctrine()->getRepository(Task::class)->findBy(["operation" => $creatorObjective->getOperation()->getId()]);
$em = $this->getDoctrine()->getManager();
foreach ($tasks as $task) {
$em->remove($task);
}
foreach ($creatorObjective->getOperation()->getObjectives() as $objective) {
foreach ($objective->getReports() as $report) {
foreach ($report->getReportValues() as $value) {
$em->remove($value);
}
$em->remove($objective);
}
$em->remove($report);
}
}
$user->setCompany(null);
$em->persist($user);
$em->remove($user);
}
$em->flush();
}
die;
}
}