Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.18 KB | None | 0 0
  1. <?php
  2.  
  3. namespace app\models\api;
  4.  
  5. use Yii;
  6. use app\models\db\User;
  7. use yii\db\Exception;
  8. use SoapClient;
  9. use DateTime;
  10.  
  11. /**
  12. * This is the model class for table "profile".
  13. *
  14. * @property integer $id
  15. * @property string $image_url
  16. * @property integer $height
  17. * @property double $weight
  18. * @property double $MDG
  19. * @property double $IMT
  20. * @property double $water_mass
  21. * @property string $pressure
  22. * @property integer $user_id
  23. * @property double $fat_mass
  24. * @property double $weight_without_fat
  25. * @property double $FIB
  26. * @property integer $pulse
  27. * @property integer $respiratory
  28. * @property integer $heart_movements
  29. * @property double $sugar
  30. * @property double $PH
  31. * @property double $leucocytes
  32. * @property double $cholesterol
  33. * @property integer $activity_level_id
  34. * @property string $age
  35. * @property integer $gender
  36. * @property integer $change_date_of_age
  37. * @property string $onesignal_user_id
  38. * @property integer $keito_user_id
  39. * @property integer $keito_barcode
  40. *
  41. * @property ActivityLevel $activityLevel
  42. * @property User $user
  43. */
  44. class Profile extends \yii\db\ActiveRecord
  45. {
  46. public $characteristicsHistory = '21';
  47. static $_soapClient = null;
  48. private static function getSoapClient () {
  49. if (!self::$_soapClient) {
  50. self::$_soapClient = new SoapClient('http://www.keitovs.net/keitows/fingerprint.asmx?wsdl');
  51. }
  52. return self::$_soapClient;
  53. }
  54. static $ERROR_NONE = 'resErrNoError';
  55.  
  56. /**
  57. * @inheritdoc
  58. */
  59. public static function tableName()
  60. {
  61. return 'profile';
  62. }
  63.  
  64. public static function findForCurrentUser()
  65. {
  66. return self::findForUserId(Yii::$app->user->id);
  67. }
  68.  
  69. public static function findForUserId($userId)
  70. {
  71. $profile = parent::find()->where(['user_id' => $userId])->one();
  72. if ($profile['change_date_of_age'] < strtotime('-1 year')) {
  73. $profile['change_date_of_age'] = time();
  74. $profile['age'] = $profile['age'] + 1;
  75. $profile->save(false);
  76. }
  77. $newProfile = [];
  78. $newProfile["activityLevel"] = $profile->hasOne(ActivityLevel::className(), ['id' => 'activity_level_id'])->one();
  79. $user = User::findOne($userId);
  80. $newProfile['email'] = $user['email'];
  81. foreach ($profile as $key => $value)
  82. $newProfile[$key] = $value;
  83. return $newProfile;
  84. }
  85.  
  86. public static function updateProfile($params)
  87. {
  88. $profile = isset($params["id"]) ? self::findOne($params["id"]) : new Profile();
  89.  
  90. /* if (isset($params["firstname"]))
  91. $profile->firstname = $params["firstname"];
  92. if (isset($params["lastname"]))
  93. $profile->lastname = $params["lastname"]; */
  94. /* if (isset($params["email"])) { // todo тут нужен мех-м изменения имейла
  95.  
  96. } */
  97. if (isset($params["age"])) {
  98. if ($profile->age != $params["age"] || !$profile->age) {
  99. $profile->change_date_of_age = time();
  100. }
  101. $profile->age = $params["age"];
  102. }
  103. if (isset($params["gender"]))
  104. $profile->gender = $params["gender"];
  105. if (isset($params["height"]))
  106. $profile->height = $params["height"];
  107. if (isset($params["pressure"]))
  108. $profile->pressure = $params["pressure"];
  109. if (isset($params["pulse"]))
  110. $profile->pulse = $params["pulse"];
  111. if (isset($params["respiratory"]))
  112. $profile->respiratory = $params["respiratory"];
  113. if (isset($params["heart_movements"]))
  114. $profile->heart_movements = $params["heart_movements"];
  115. if (isset($params["activity_level_id"]))
  116. $profile->activity_level_id = $params["activity_level_id"];
  117. if (isset($params["weight"]))
  118. $profile->weight = $params["weight"];
  119. if (isset($params["MDG"]))
  120. $profile->MDG = $params["MDG"];
  121. if (isset($params["IMT"]))
  122. $profile->IMT = $params["IMT"];
  123. if (isset($params["water_mass"]))
  124. $profile->water_mass = $params["water_mass"];
  125. if (isset($params["fat_mass"]))
  126. $profile->fat_mass = $params["fat_mass"];
  127. if (isset($params["weight_without_fat"]))
  128. $profile->weight_without_fat = $params["weight_without_fat"];
  129. if (isset($params["FIB"]))
  130. $profile->FIB = $params["FIB"];
  131. if (isset($params["sugar"]))
  132. $profile->sugar = $params["sugar"];
  133. if (isset($params["PH"]))
  134. $profile->PH = $params["PH"];
  135. if (isset($params["leucocytes"]))
  136. $profile->leucocytes = $params["leucocytes"];
  137. if (isset($params["cholesterol"]))
  138. $profile->cholesterol = $params["cholesterol"];
  139. if (isset($params["image_url"]))
  140. $profile->image_url = $params["image_url"];
  141. if (isset($params["keito_user_id"]))
  142. $profile->keito_user_id = $params["keito_user_id"];
  143. if (isset($params["onesignal_user_id"]))
  144. $profile->onesignal_user_id = $params["onesignal_user_id"];
  145. if (isset($params["keito_barcode"])) {
  146. $profile->keito_barcode = $params["keito_barcode"];
  147.  
  148. $settings = GlobalSettings::find()->all();
  149. if ($settings && count($settings) && $settings[0]['keito_login'] && $settings[0]['keito_password']) {
  150. $login = $settings[0]['keito_login'];
  151. $pass = $settings[0]['keito_password'];
  152. $organizationCode = self::getOrganizationCode($login, $pass);
  153. $tempId = self::getUserIDFromCardCode($params["keito_barcode"], $organizationCode, $login, $pass);
  154. if ($tempId) {
  155. $profile->keito_user_id = $tempId;
  156. }
  157. }
  158. }
  159.  
  160. $profile->user_id = Yii::$app->user->id;
  161. $achievedTargets = [];
  162. $id = isset($params["id"]) ? isset($params["id"]) : '';
  163. $isSuccess = $profile->save(false);
  164. if (!$isSuccess) {
  165. throw new Exception('Profile ' . $id . ' was not updated.', ['$profile' => $profile], 1);
  166. } else if (isset($params["weight"])) {
  167. $achievedTargets = Competition::checkForAchieveTarget($params["weight"]);
  168. }
  169.  
  170. $newProf = self::convertToArray($profile, [
  171. "achievedTargets" =>$achievedTargets
  172. ]);
  173.  
  174. return $newProf;
  175. }
  176.  
  177. public static function convertToArray($obj, $arrNewProps)
  178. {
  179. $result = [];
  180. foreach ($arrNewProps as $key => $value)
  181. $result[$key] = $value;
  182. foreach ($obj as $key => $value)
  183. $result[$key] = $value;
  184. return $result;
  185. }
  186.  
  187. public static function updateCharacteristicsFromKeito($cardCode)
  188. {
  189. $measure = self::getLastUserMeasure($cardCode);
  190. $organizationCode = isset($measure['organizationCode']) ? $measure['organizationCode'] : null;
  191. $userId = isset($measure['userId']) ? $measure['userId'] : null;
  192. $login = isset($measure['login']) ? $measure['login'] : null;
  193. $password = isset($measure['password']) ? $measure['password'] : null;
  194.  
  195. $keitoUser = self::getUserInfoFromKeito($cardCode, $organizationCode, $userId, $login, $password);
  196.  
  197. $maxPressure = $measure['nMaxBloodPressure'];
  198. $minPressure = $measure['nMinBloodPressure'];
  199. $pressure = "$maxPressure/$minPressure";
  200.  
  201. $fatMass = $measure['sngWeight'] * $measure['sngIGP'] / 100;
  202. // $MDG = $fatMass * 100 / $measure['sngWeight'];
  203. $IMT = $measure['sngWeight'] / ($measure['nHeight'] * $measure['nHeight'] / 10000);
  204. $FIB = $fatMass / ($measure['nHeight'] * $measure['nHeight'] / 10000);
  205. $weightWithoutFat = $measure['sngWeight'] - $fatMass;
  206.  
  207. $datetime = new DateTime($keitoUser->dBirthDate);
  208. $now = new DateTime();
  209. $age = $now->format('Y') - $datetime->format('Y');
  210. $gender = $keitoUser->Sex === 'sexMale' ? 1 : 0;
  211.  
  212. return self::updateProfile([
  213. 'id' => $measure['profileId'],
  214. 'height' => $measure['nHeight'],
  215. 'weight' => $measure['sngWeight'],
  216. 'pulse' => $measure['nPulse'],
  217. 'FIB' => $FIB,
  218. 'pressure' => $pressure,
  219. 'keito_user_id' => $measure['userId'],
  220. 'fat_mass' => $fatMass,
  221. 'MDG' => $measure['sngIGP'],
  222. 'IMT' => $IMT,
  223. 'weight_without_fat' => $weightWithoutFat,
  224. 'keito_barcode' => $cardCode,
  225. 'gender' => $gender,
  226. 'age' => $age
  227. ]);
  228. }
  229.  
  230. public static function getLastUserMeasure($cardCode)
  231. {
  232. $result = null;
  233. $newResult = [];
  234. $settings = GlobalSettings::find()->all();
  235. if ($settings && count($settings) && $settings[0]['keito_login'] && $settings[0]['keito_password']) {
  236. $login = $settings[0]['keito_login'];
  237. $pass = $settings[0]['keito_password'];
  238. $organizationCode = self::getOrganizationCode($login, $pass);
  239.  
  240. $currentProfile = self::findForCurrentUser();
  241. $barcode = !!$cardCode ? $cardCode : $currentProfile['keito_barcode'];
  242. $userId = self::getUserIDFromCardCode($barcode, $organizationCode, $login, $pass);
  243.  
  244. if ($userId) {
  245. $response = self::getSoapClient()->AppGetUserMeasurements([
  246. 'sLoginName' => $login,
  247. 'sPassWord' => $pass,
  248. 'nOrganizationID' => $organizationCode,
  249. 'nUserID' => $userId,
  250. 'nNumberOfMeasures' => 1 // последнее измерение
  251. ]);
  252.  
  253. $result = (
  254. $response &&
  255. $response->AppGetUserMeasurementsResult &&
  256. $response->AppGetUserMeasurementsResult->Result &&
  257. $response->AppGetUserMeasurementsResult->Result->nErrorCode == self::$ERROR_NONE &&
  258. $response->AppGetUserMeasurementsResult->Measures &&
  259. $response->AppGetUserMeasurementsResult->Measures->CMeasure &&
  260. $response->AppGetUserMeasurementsResult->Measures->CMeasure
  261. ) ? $response->AppGetUserMeasurementsResult->Measures->CMeasure : null;
  262.  
  263. $newResult['userId'] = $userId;
  264. $newResult['profileId'] = $currentProfile['id'];
  265. $newResult['login'] = $login;
  266. $newResult['password'] = $pass;
  267. $newResult['organizationCode'] = $organizationCode;
  268. $newResult['userId'] = $userId;
  269. foreach ($result as $key => $value)
  270. $newResult[$key] = $value;
  271. } else {
  272. throw new Exception('User with this barcode not exist', null, 1);
  273. }
  274. }
  275. return $newResult;
  276. }
  277.  
  278. public static function getUserInfoFromKeito($cardCode, $organizationCode = null, $userId = null, $login = null, $password = null)
  279. {
  280. $result = null;
  281. $settings = !($login && $password) ? GlobalSettings::find()->all() : [['keito_login' => $login, 'keito_password' => $password]];
  282. if ($settings && count($settings) && $settings[0]['keito_login'] && $settings[0]['keito_password']) {
  283. $login = $settings[0]['keito_login'];
  284. $pass = $settings[0]['keito_password'];
  285. $organizationCode = $organizationCode ? $organizationCode : self::getOrganizationCode($login, $pass);
  286.  
  287. // $currentProfile = self::findForCurrentUser();
  288. $userId = $userId ? $userId : self::getUserIDFromCardCode($cardCode, $organizationCode, $login, $pass);
  289.  
  290. if ($userId) {
  291. $result = self::getUserInfo($userId, $organizationCode, $login, $pass);
  292. }
  293. }
  294. return $result;
  295. }
  296.  
  297. private static function getOrganizationCode($login, $password)
  298. {
  299. $response = self::getSoapClient()->AppGetAdminOrganizations([
  300. 'sLoginName' => $login,
  301. 'sPassWord' => $password
  302. ]);
  303. $result = (
  304. $response &&
  305. $response->AppGetAdminOrganizationsResult &&
  306. $response->AppGetAdminOrganizationsResult->Result &&
  307. $response->AppGetAdminOrganizationsResult->Result->nErrorCode == self::$ERROR_NONE &&
  308. $response->AppGetAdminOrganizationsResult->Organizations &&
  309. $response->AppGetAdminOrganizationsResult->Organizations->COrganization
  310. ) ? $response->AppGetAdminOrganizationsResult->Organizations->COrganization->nID : null;
  311. return $result;
  312. }
  313.  
  314. private static function getUserIDFromCardCode($cardCode, $organizationCode, $login, $password)
  315. {
  316. $result = null;
  317. /*$result = self::getSoapClient()->AppGetUserIDFromCardCode([ // todo не работает, возможно причина в неверном типе кода
  318. 'sLoginName' => $login,
  319. 'sPassWord' => $password,
  320. 'nOrganizationID' => $organizationCode,
  321. 'eCardType' => 'ctBarcodeEAN13',
  322. 'sCardCode' => $cardCode
  323. ]);*/
  324. $response = self::getSoapClient()->AppGetOrgUsersEx([
  325. 'sLoginName' => $login,
  326. 'sPassWord' => $password,
  327. 'nOrganizationID' => $organizationCode
  328. ]);
  329.  
  330. $userList = (
  331. $response &&
  332. $response->AppGetOrgUsersExResult &&
  333. $response->AppGetOrgUsersExResult->Result &&
  334. $response->AppGetOrgUsersExResult->Result->nErrorCode == self::$ERROR_NONE &&
  335. $response->AppGetOrgUsersExResult->Users &&
  336. $response->AppGetOrgUsersExResult->Users->CUserEx
  337. ) ? $response->AppGetOrgUsersExResult->Users->CUserEx : null;
  338.  
  339. if ($userList) {
  340. foreach ($userList as $user) {
  341. if (isset($user->asCardCodes) && isset($user->asCardCodes->string) && $user->asCardCodes->string == $cardCode) {
  342. $result = $user->nUserID;
  343. break;
  344. }
  345. }
  346. }
  347.  
  348. return $result;
  349. }
  350.  
  351. private static function getUserInfo($userId, $organizationCode, $login, $password)
  352. {
  353. $response = self::getSoapClient()->AppGetUserInfo([
  354. 'sLoginName' => $login,
  355. 'sPassWord' => $password,
  356. 'nOrganizationID' => $organizationCode,
  357. 'nUserID' => $userId
  358. ]);
  359. $result = (
  360. $response &&
  361. $response->AppGetUserInfoResult &&
  362. $response->AppGetUserInfoResult->Result &&
  363. $response->AppGetUserInfoResult->Result->nErrorCode == self::$ERROR_NONE &&
  364. $response->AppGetUserInfoResult->Users &&
  365. $response->AppGetUserInfoResult->Users->CUser
  366. ) ? $response->AppGetUserInfoResult->Users->CUser : null;
  367. return $result;
  368. }
  369.  
  370. /**
  371. * @inheritdoc
  372. */
  373. public function rules()
  374. {
  375. return [
  376. [['gender'], 'required'],
  377. [['height', 'user_id', 'pulse', 'respiratory', 'heart_movements', 'activity_level_id', 'gender', 'age', 'keito_user_id'], 'integer'],
  378. [['weight', 'MDG', 'IMT', 'water_mass', 'fat_mass', 'weight_without_fat', 'FIB', 'sugar', 'PH', 'leucocytes', 'cholesterol'], 'number'],
  379. [['image_url'], 'string'],
  380. [['pressure'], 'string', 'max' => 128],
  381. [['keito_barcode'], 'string', 'max' => 32],
  382. [['activity_level_id'], 'exist', 'skipOnError' => true, 'targetClass' => ActivityLevel::className(), 'targetAttribute' => ['activity_level_id' => 'id']],
  383. [['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['user_id' => 'id']],
  384. ];
  385. }
  386.  
  387. /**
  388. * @inheritdoc
  389. */
  390. public function attributeLabels()
  391. {
  392. return [
  393. 'id' => 'ID',
  394. 'image_url' => 'Image Url',
  395. 'height' => 'Height',
  396. 'weight' => 'Weight',
  397. 'MDG' => 'Mdg',
  398. 'IMT' => 'Imt',
  399. 'water_mass' => 'Water Mass',
  400. 'pressure' => 'Pressure',
  401. 'user_id' => 'User ID',
  402. 'fat_mass' => 'Fat Mass',
  403. 'weight_without_fat' => 'Weight Without Fat',
  404. 'FIB' => 'Fib',
  405. 'pulse' => 'Pulse',
  406. 'respiratory' => 'Respiratory',
  407. 'heart_movements' => 'Heart Movements',
  408. 'sugar' => 'Sugar',
  409. 'PH' => 'Ph',
  410. 'leucocytes' => 'Leucocytes',
  411. 'cholesterol' => 'Cholesterol',
  412. 'activity_level_id' => 'Activity Level ID',
  413. 'age' => 'Age',
  414. 'gender' => 'Gender',
  415. ];
  416. }
  417.  
  418. /**
  419. * @return \yii\db\ActiveQuery
  420. */
  421. public function getActivityLevel()
  422. {
  423. return $this->hasOne(ActivityLevel::className(), ['id' => 'activity_level_id']);
  424. }
  425.  
  426. /**
  427. * @return \yii\db\ActiveQuery
  428. */
  429. public function getUser()
  430. {
  431. return $this->hasOne(User::className(), ['id' => 'user_id']);
  432. }
  433. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement