Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 5237 | Rev 6849 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
5237 efrain 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Command;
6
 
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputInterface;
9
//use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Laminas\Db\Adapter\AdapterInterface;
6749 efrain 12
use LeadersLinked\Cache\CacheInterface;
5237 efrain 13
use Laminas\Log\LoggerInterface;
14
use LeadersLinked\Mapper\CompanyMicrolearningUserProgressMapper;
15
use LeadersLinked\Mapper\CompanyMicrolearningSlideMapper;
16
use LeadersLinked\Mapper\QueryMapper;
17
use LeadersLinked\Model\CompanyMicrolearningUserProgress;
18
use LeadersLinked\Mapper\EngagementMapper;
19
use LeadersLinked\Mapper\EngagementRecordMapper;
20
use LeadersLinked\Mapper\EngagementUserMapper;
21
use LeadersLinked\Mapper\DailyPulseEmojiMapper;
22
use LeadersLinked\Mapper\DailyPulseRecordMapper;
23
use LeadersLinked\Mapper\UserMapper;
24
use LeadersLinked\Model\DailyPulseRecord;
25
use LeadersLinked\Model\EngagementUser;
26
use LeadersLinked\Model\EngagementRecord;
27
 
28
 
29
class DailyPulseSimulateCommand extends Command
30
{
31
    /**
32
     *
33
     * @var AdapterInterface
34
     */
35
    private $adapter;
36
 
37
 
38
    /**
39
     *
6749 efrain 40
     * @var CacheInterface
5237 efrain 41
     */
42
    private $cache;
43
 
44
    /**
45
     *
46
     * @var  LoggerInterface
47
     */
48
    private $logger;
49
 
50
    /**
51
     *
52
     * @var array
53
     */
54
    private $config;
55
 
56
 
57
    /**
58
     *
59
     * @param AdapterInterface $adapter
6749 efrain 60
     * @param CacheInterface $cache
5237 efrain 61
     * @param LoggerInterface $logger
62
     * @param array $config
63
     */
64
     public function __construct($adapter, $cache, $logger, $config)
65
    {
66
        $this->adapter      = $adapter;
67
        $this->cache        = $cache;
68
        $this->logger       = $logger;
69
        $this->config       = $config;
70
 
71
        parent::__construct();
72
    }
73
 
74
 
75
    protected function execute(InputInterface $input, OutputInterface $output) : int
76
    {
77
 
78
        $output->writeln('Comenzamos a generar Daily Pulse de los últimos 30 días');
79
 
80
 
81
        $company_id = 1;
82
        $engagementMapper = EngagementMapper::getInstance($this->adapter);
83
        $engagement = $engagementMapper->fetchOneByCompanyId($company_id);
84
 
85
        $engagementRecordMapper = EngagementRecordMapper::getInstance($this->adapter);
86
        $engagementUserMapper = EngagementUserMapper::getInstance($this->adapter);
87
 
88
 
89
 
90
 
91
        $dailyPulseEmojiMapper = DailyPulseEmojiMapper::getInstance($this->adapter);
92
        $emojis_climate = $dailyPulseEmojiMapper->fetchAllActiveTypeClimateOnYourOrganizationByCompanyId($company_id);
93
        $max_emojis_climate = count($emojis_climate) - 1;
94
 
95
        $emojis_feel = $dailyPulseEmojiMapper->fetchAllActiveTypeHowAreYouFeelByCompanyId($company_id);
96
        $max_emojis_feel = count($emojis_feel) - 1;
97
 
98
 
99
        $dailyPulseRecordMapper = DailyPulseRecordMapper::getInstance($this->adapter);
100
 
101
 
102
        $endDate = date('Y-m-d');
103
        $startDate = date('Y-m-d', strtotime('-1 MONTH'));
104
 
105
        $seed = (int) (floor(time()/86400));
106
        srand($seed);
107
        $min_users = 100;
108
        $max_users = 300;
109
 
110
        $queryMapper = QueryMapper::getInstance($this->adapter);
111
 
112
 
113
 
114
        $output->writeln('$startDate = ' .  $startDate);
115
        $output->writeln('$endDate = ' .  $endDate);
116
 
117
        $dt = \DateTime::createFromFormat('Y-m-d', $startDate);
118
        do {
119
 
120
            $date = $dt->format('Y-m-d');
121
 
122
            $limit = rand($min_users, $max_users);
123
 
124
            $sql  = " SELECT id FROM " . UserMapper::_TABLE;
125
            $sql .= " ORDER BY RAND() LIMIT $limit";
126
 
127
            $output->writeln('$sql = ' . $sql);
128
 
129
            $user_ids = [];
130
 
131
            $records = $queryMapper->fetchAllUsingParameters($sql);
132
            foreach($records as $record)
133
            {
134
                array_push($user_ids, $record['id']);
135
            }
136
 
137
            $max = count($user_ids);
138
            for ($i = 0; $i < $max; $i++)
139
            {
140
                $user_id = $user_ids[$i];
141
 
142
 
143
                $idx = rand(0, $max_emojis_feel);
144
                $emoji = $emojis_feel[$idx];
145
 
146
 
147
 
148
                $dailyPulse = new DailyPulseRecord();
149
                $dailyPulse->company_id = $company_id;
150
                $dailyPulse->user_id = $user_id;
151
                $dailyPulse->emoji_id = $emoji->id;
152
                $dailyPulse->date = $date;
153
                $dailyPulse->type = DailyPulseRecord::TYPE_HOW_ARE_YOU_FEEL;
154
                $dailyPulse->engagement = $engagement ? $engagement->daily_pulse_how_are_you_feel : 0;
155
                $dailyPulse->points = $emoji->points;
156
 
157
                $result = $dailyPulseRecordMapper->insert($dailyPulse);
158
                if($result) {
159
 
160
 
161
 
162
 
163
 
164
                    $engagementUserMapper = EngagementUserMapper::getInstance($this->adapter);
165
                    $engagementUser = $engagementUserMapper->fetchOneByCompanyIdAndUserId($company_id, $user_id);
166
 
167
                    if($engagementUser) {
168
                        $engagementUser->points = $engagementUser->points + ($engagement ? $engagement->daily_pulse_how_are_you_feel : 0);
169
                        $engagementUserMapper->update( $engagementUser );
170
                    } else {
171
                        $engagementUser = new EngagementUser();
172
                        $engagementUser->company_id = $company_id;
173
                        $engagementUser->user_id = $user_id;
174
                        $engagementUser->points = $engagement ? $engagement->daily_pulse_how_are_you_feel : 0;
175
                        $engagementUserMapper->insert( $engagementUser );
176
                    }
177
 
178
                    $engagementRecord = new EngagementRecord();
179
                    $engagementRecord->company_id = $company_id;
180
                    $engagementRecord->user_id = $user_id;
181
                    $engagementRecord->date = $date;
182
                    $engagementRecord->points = $engagement->daily_pulse_how_are_you_feel;
183
                    $engagementRecord->type = EngagementRecord::TYPE_DAILY_PULSE_HOW_ARE_YOU_FEEL;
184
 
185
                    $engagementRecordMapper = EngagementRecordMapper::getInstance($this->adapter);
186
                    $engagementRecordMapper->insert($engagementRecord);
187
 
188
 
189
                }
190
 
191
            }
192
 
193
 
194
            $percentaje = rand(20, 80);
195
 
196
            $max = (int) (($percentaje * 100) / $max);
197
            for ($i = 0; $i < $max; $i++)
198
            {
199
                $user_id = $user_ids[$i];
200
 
201
 
202
                $idx = rand(0, $max_emojis_climate);
203
                $emoji = $emojis_climate[$idx];
204
 
205
                $dailyPulse = new DailyPulseRecord();
206
                $dailyPulse->company_id = $company_id;
207
                $dailyPulse->user_id = $user_id;
208
                $dailyPulse->emoji_id = $emoji->id;
209
                $dailyPulse->type = DailyPulseRecord::TYPE_CLIMATE_ON_YOUR_ORGANIZATION;
210
                $dailyPulse->date = $date;
211
                $dailyPulse->engagement = $engagement ? $engagement->daily_pulse_climate_on_your_organization : 0;
212
                $dailyPulse->points = $emoji->points;
213
 
214
                $result = $dailyPulseRecordMapper->insert($dailyPulse);
215
                if($result) {
216
 
217
                    $engagementUserMapper = EngagementUserMapper::getInstance($this->adapter);
218
                    $engagementUser = $engagementUserMapper->fetchOneByCompanyIdAndUserId($company_id, $user_id);
219
 
220
                    if($engagementUser) {
221
                        $engagementUser->points = $engagementUser->points + ($engagement ? $engagement->daily_pulse_climate_on_your_organization : 0);
222
                        $engagementUserMapper->update( $engagementUser );
223
 
224
                    } else {
225
                        $engagementUser = new EngagementUser();
226
                        $engagementUser->company_id = $company_id;
227
                        $engagementUser->user_id = $user_id;
228
                        $engagementUser->points = $engagement ? $engagement->daily_pulse_climate_on_your_organization : 0;
229
                        $engagementUserMapper->insert( $engagementUser );
230
                    }
231
 
232
                    $engagementRecord = new EngagementRecord();
233
                    $engagementRecord->company_id = $company_id;
234
                    $engagementRecord->user_id = $user_id;
235
                    $engagementRecord->date = $date;
236
                    $engagementRecord->points = $engagement->daily_pulse_climate_on_your_organization;
237
                    $engagementRecord->type = EngagementRecord::TYPE_DAILY_PULSE_CLIMATE_ON_YOUR_ORGANIZATION;
238
 
239
                    $engagementRecordMapper = EngagementRecordMapper::getInstance($this->adapter);
240
                    $engagementRecordMapper->insert($engagementRecord);
241
 
242
 
243
                }
244
 
245
            }
246
 
247
 
248
 
249
 
250
            $output->writeln('$percentaje = ' . $percentaje);
251
 
252
 
253
 
254
 
255
 
256
            $dt->add(new \DateInterval('P1D'));
257
 
258
 
259
        } while($date < $endDate);
260
 
261
 
262
        $output->writeln('Terminamos a generar Daily Pulse de los últimos 30 días');
263
 
264
 
265
        return 0;
266
    }
267
 
268
 
269
 
270
}