| 1 |
efrain |
1 |
<?php
|
|
|
2 |
declare(strict_types=1);
|
|
|
3 |
|
|
|
4 |
namespace LeadersLinked\Mapper;
|
|
|
5 |
|
|
|
6 |
|
|
|
7 |
use Laminas\Db\Adapter\AdapterInterface;
|
|
|
8 |
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
|
|
|
9 |
use Laminas\Log\LoggerInterface;
|
|
|
10 |
|
|
|
11 |
|
|
|
12 |
use LeadersLinked\Model\DailyPulseRecord;
|
|
|
13 |
use LeadersLinked\Mapper\Common\MapperCommon;
|
|
|
14 |
use Laminas\Db\Sql\Expression;
|
|
|
15 |
|
|
|
16 |
class DailyPulseRecordMapper extends MapperCommon
|
|
|
17 |
{
|
|
|
18 |
const _TABLE = 'tbl_daily_pulse_records';
|
|
|
19 |
|
|
|
20 |
/**
|
|
|
21 |
*
|
|
|
22 |
* @var DailyPulseRecordMapper
|
|
|
23 |
*/
|
|
|
24 |
private static $_instance;
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
*
|
|
|
28 |
* @param AdapterInterface $adapter
|
|
|
29 |
*/
|
|
|
30 |
private function __construct($adapter)
|
|
|
31 |
{
|
|
|
32 |
parent::__construct($adapter);
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
*
|
|
|
37 |
* @param AdapterInterface $adapter
|
|
|
38 |
* @return DailyPulseRecordMapper
|
|
|
39 |
*/
|
|
|
40 |
public static function getInstance($adapter)
|
|
|
41 |
{
|
|
|
42 |
if(self::$_instance == null) {
|
|
|
43 |
self::$_instance = new DailyPulseRecordMapper($adapter);
|
|
|
44 |
}
|
|
|
45 |
return self::$_instance;
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
*
|
|
|
51 |
* @param int $user_id
|
|
|
52 |
* @param string $date
|
|
|
53 |
* @param string $type
|
|
|
54 |
* @return DailyPulseRecord
|
|
|
55 |
*/
|
|
|
56 |
public function fetchOneByUserIdAndDateAndType($user_id, $date, $type)
|
|
|
57 |
{
|
|
|
58 |
$select = $this->sql->select(self::_TABLE);
|
|
|
59 |
$select->where->equalTo('user_id', $user_id);
|
|
|
60 |
$select->where->equalTo('date', $date);
|
|
|
61 |
$select->where->equalTo('type', $type);
|
|
|
62 |
$select->limit(1);
|
|
|
63 |
|
|
|
64 |
//echo $select->getSqlString($this->adapter->platform); exit;
|
|
|
65 |
|
|
|
66 |
$prototype = new DailyPulseRecord();
|
|
|
67 |
return $this->executeFetchOneObject($select, $prototype);
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
*
|
|
|
74 |
* @param int $user_id
|
|
|
75 |
* @param string $type
|
|
|
76 |
* @return DailyPulseRecord
|
|
|
77 |
*/
|
|
|
78 |
public function fetchOneCurrentByUserIdAndType($user_id, $type)
|
|
|
79 |
{
|
|
|
80 |
$select = $this->sql->select(self::_TABLE);
|
|
|
81 |
$select->where->equalTo('user_id', $user_id);
|
|
|
82 |
$select->where->equalTo('date', new Expression('DATE(NOW())'));
|
|
|
83 |
$select->where->equalTo('type', $type);
|
|
|
84 |
$select->limit(1);
|
|
|
85 |
|
|
|
86 |
//echo $select->getSqlString($this->adapter->platform); exit;
|
|
|
87 |
|
|
|
88 |
$prototype = new DailyPulseRecord();
|
|
|
89 |
return $this->executeFetchOneObject($select, $prototype);
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
/**
|
|
|
93 |
*
|
|
|
94 |
* @param int $company_id
|
|
|
95 |
* @param string $type
|
|
|
96 |
* @param string $start_date
|
|
|
97 |
* @param string $end_date
|
|
|
98 |
* @return array
|
|
|
99 |
*/
|
|
|
100 |
public function fetchAllDataChartOrExcelByCompanyIdAndTypeAndDateRange($company_id, $type, $start_date, $end_date)
|
|
|
101 |
{
|
|
|
102 |
$select = $this->sql->select(self::_TABLE);
|
|
|
103 |
$select->columns([
|
|
|
104 |
'date',
|
|
|
105 |
'points' => new Expression('SUM(points)'),
|
|
|
106 |
'users' => new Expression('COUNT(user_id)')
|
|
|
107 |
]);
|
|
|
108 |
$select->where->equalTo('company_id', $company_id);
|
|
|
109 |
$select->where->between('date', $start_date, $end_date);
|
|
|
110 |
$select->where->equalTo('type', $type);
|
|
|
111 |
$select->group('date');
|
|
|
112 |
$select->order('date ASC');
|
|
|
113 |
|
|
|
114 |
//echo $select->getSqlString($this->adapter->platform); exit;
|
|
|
115 |
|
|
|
116 |
|
|
|
117 |
return $this->executeFetchAllArray($select);
|
|
|
118 |
}
|
| 152 |
efrain |
119 |
|
|
|
120 |
/**
|
|
|
121 |
*
|
|
|
122 |
* @param int $company_id
|
|
|
123 |
* @return string
|
|
|
124 |
*/
|
|
|
125 |
public function fetchOneMaxDateActivityFromCompanyId($company_id)
|
|
|
126 |
{
|
|
|
127 |
|
|
|
128 |
|
|
|
129 |
$select = $this->sql->select();
|
|
|
130 |
$select->columns(['date' => new Expression('MAX(date)') ] );
|
|
|
131 |
$select->from(self::_TABLE);
|
|
|
132 |
$select->where->equalTo('company_id', $company_id);
|
|
|
133 |
|
|
|
134 |
|
|
|
135 |
$record = $this->executeFetchOneArray($select);
|
|
|
136 |
return empty($record['date']) ? '' : $record['date'];
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
/**
|
|
|
140 |
*
|
|
|
141 |
* @param int $company_id
|
|
|
142 |
* @param string $start_date
|
|
|
143 |
* @param string $end_date
|
|
|
144 |
* @return array
|
|
|
145 |
*/
|
|
|
146 |
public function fetchAllCountUsersWithClosedCapsulesDailyByCompanyIdAndStartDateAndEndDate($company_id, $start_date, $end_date)
|
|
|
147 |
{
|
|
|
148 |
$select = $this->sql->select();
|
|
|
149 |
$select->columns(['total' => new Expression('COUNT(DISTINCT(user_id))'), 'date' ] );
|
|
|
150 |
$select->from(self::_TABLE);
|
|
|
151 |
$select->where->equalTo('company_id', $company_id);
|
|
|
152 |
$select->where->between('date', $start_date, $end_date);
|
|
|
153 |
$select->group('date');
|
|
|
154 |
$select->order('date desc');
|
|
|
155 |
|
|
|
156 |
// echo $select->getSqlString( $this->adapter->platform ); exit;
|
|
|
157 |
|
|
|
158 |
return $this->executeFetchAllArray($select);
|
|
|
159 |
}
|
|
|
160 |
|
| 1 |
efrain |
161 |
|
|
|
162 |
|
|
|
163 |
|
|
|
164 |
|
|
|
165 |
/**
|
|
|
166 |
*
|
|
|
167 |
* @param DailyPulseRecord $record
|
|
|
168 |
* @return boolean
|
|
|
169 |
*/
|
|
|
170 |
public function insert($record)
|
|
|
171 |
{
|
|
|
172 |
$hydrator = new ObjectPropertyHydrator();
|
|
|
173 |
$values = $hydrator->extract($record);
|
|
|
174 |
$values = $this->removeEmpty($values);
|
|
|
175 |
|
|
|
176 |
if(empty($values['date'])) {
|
|
|
177 |
|
|
|
178 |
$values['date'] = new Expression('DATE(NOW())');
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
$insert = $this->sql->insert(self::_TABLE);
|
|
|
182 |
$insert->values($values);
|
|
|
183 |
|
|
|
184 |
|
|
|
185 |
return $this->executeInsert($insert);
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
|
|
|
189 |
/**
|
|
|
190 |
*
|
|
|
191 |
* @param DailyPulseRecord $record
|
|
|
192 |
* @return boolean
|
|
|
193 |
*/
|
|
|
194 |
public function update($record)
|
|
|
195 |
{
|
|
|
196 |
$hydrator = new ObjectPropertyHydrator();
|
|
|
197 |
$values = $hydrator->extract($record);
|
|
|
198 |
$values = $this->removeEmpty($values);
|
|
|
199 |
|
|
|
200 |
$update = $this->sql->update(self::_TABLE);
|
|
|
201 |
$update->set($values);
|
|
|
202 |
$update->where->equalTo('company_id', $record->company_id);
|
|
|
203 |
$update->where->equalTo('user_id', $record->user_id);
|
|
|
204 |
$update->where->equalTo('type', $record->type);
|
|
|
205 |
|
|
|
206 |
return $this->executeUpdate($update);
|
|
|
207 |
}
|
|
|
208 |
|
|
|
209 |
}
|
|
|
210 |
|