<?php
namespace App\Command;
use App\Entity\Product;
use App\Helper\EnumsHelper;
use App\Services\B2BGoldAndSilver;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
class EfsFillRewardsPricesCommand extends Command
{
protected static $defaultName = 'efs:rewards:fill';
protected static $defaultDescription = 'Add a short description for your command';
public function __construct(EntityManagerInterface $entityManager, B2BGoldAndSilver $b2BGoldAndSilver)
{
$this->entityManager = $entityManager;
$this->b2BGoldAndSilver = $b2BGoldAndSilver;
$this->connection = $this->entityManager->getConnection();
parent::__construct(self::$defaultName);
}
protected function configure(): void
{
// $this
// ->addArgument('arg1', InputArgument::OPTIONAL, 'Argument description')
// ->addOption('option1', null, InputOption::VALUE_NONE, 'Option description')
// ;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$query = $this->connection->executeQuery('SELECT id FROM product WHERE (product_type = "bond" or product_type = "loan" or product_type = "stock")');
$resultProducts = $query->fetchAllAssociative();
// this up() migration is auto-generated, please modify it to your needs
$query = $this->connection->executeQuery('SELECT * FROM affboss_affjuniors WHERE relation_type = "structure"');
$result = $query->fetchAllAssociative();
// direct odmeny
$directAlreadyCreated = [];
foreach ($result as $row){
if(false == in_array($row['user_target'], $directAlreadyCreated)){
$directAlreadyCreated[] = $row['user_target'];
foreach ($resultProducts as $product) {
//$this->connection->executeQuery("INSERT INTO affboss_affjuniors set user_source = null, user_target = '" . $row['user_target'] . "', reward = 6, relation_type = 'reward_direct', product_id = '".$product["id"]."'");
$this->insertIfNotExists(null, $row['user_target'], $product['id'], 6, 'reward_direct');
}
}
if(false == in_array($row['user_source'], $directAlreadyCreated)){
$directAlreadyCreated[] = $row['user_source'];
foreach ($resultProducts as $product) {
//$this->connection->executeQuery("INSERT INTO affboss_affjuniors set user_source = null, user_target = '" . $row['user_source'] . "', reward = 6, relation_type = 'reward_direct', product_id = '".$product["id"]."'");
$this->insertIfNotExists(null, $row['user_source'], $product['id'], 6, 'reward_direct');
}
}
}
$alreadyIns = [];
foreach ($result as $row){
foreach ($resultProducts as $product) {
if(false == in_array($row['user_source']."_".$row['user_target']. "_".$product["id"], $alreadyIns)) {
$alreadyIns[] = $row['user_source'] . "_" . $row['user_target']. "_".$product["id"];
//echo("INSERT INTO affboss_affjuniors set user_source = " . $row['user_source'] . ", user_target = '" . $row['user_target'] . "', reward = 1, relation_type = 'reward', product_id = '" . $product["id"] . "'\r\n");
//$this->connection->executeQuery("INSERT INTO affboss_affjuniors set user_source = " . $row['user_source'] . ", user_target = '" . $row['user_target'] . "', reward = 1, relation_type = 'reward', product_id = '" . $product["id"] . "'");
$this->insertIfNotExists($row['user_source'], $row['user_target'], $product['id'], 1);
}
}
$this->createRecursivly($alreadyIns, $row['user_target'], $row['user_source'], $resultProducts);
}
$io->success('The generation of rewards has been finished');
return Command::SUCCESS;
}
protected function insertIfNotExists($userSource, $userTarget, $productId, $reward, $rewardType = 'reward'){
if(is_null($userSource)){
$userSourceQuery = "user_source IS null ";
}else{
$userSourceQuery = "user_source = '" . $userSource . "'";
}
$query = $this->connection->executeQuery("SELECT * FROM affboss_affjuniors WHERE ".$userSourceQuery." AND user_target = '".$userTarget."' AND product_id = '".$productId."' AND relation_type = '".$rewardType."'");
$result = $query->fetchAllAssociative();
if(is_array($result) && count($result) == 0) {
if(is_null($userSource)){
$userSourceQuery = "user_source = null, ";
}else{
$userSourceQuery = "user_source = '" . $userSource . "',";
}
$this->connection->executeQuery("INSERT INTO affboss_affjuniors set ".$userSourceQuery." user_target = '" . $userTarget . "', relation_type = '".$rewardType."', reward = '".$reward."', product_id = '" . $productId . "'");
}
}
protected function createRecursivly(&$alreadyIns = [], $currentBossId, $juniorId, $products){
$query = $this->connection->executeQuery('SELECT * FROM affboss_affjuniors WHERE relation_type = "structure" and user_source = "'.$currentBossId.'"');
$result = $query->fetchAllAssociative();
foreach ($result as $row){
foreach ($products as $product) {
if(false == in_array($row['user_source']."_".$juniorId."_".$product["id"], $alreadyIns)) {
$alreadyIns[] = $row['user_source'] . "_" . $juniorId."_".$product["id"];
//echo ("2 - INSERT INTO affboss_affjuniors set user_source = " . $juniorId . ", user_target = '" . $row['user_target'] . "', reward = 1, relation_type = 'reward', product_id = '" . $product["id"] . "' \r\n");
//$this->connection->executeQuery("INSERT INTO affboss_affjuniors set user_source = " . $juniorId . ", user_target = '" . $row['user_target'] . "', reward = 1, relation_type = 'reward', product_id = '" . $product["id"] . "'");
$this->insertIfNotExists($juniorId, $row['user_target'], $product['id'], 1);
}
}
$this->createRecursivly($alreadyIns, $row['user_target'], $juniorId, $products);
}
}
}