UserPerformanceLogs.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace common\models;
  3. use common\helpers\Date;
  4. use Exception;
  5. /**
  6. * This is the model class for table "{{%USER_PERFORMANCE_LOGS}}".
  7. *
  8. * @property string ID
  9. * @property string USER_PERFORMANCE_ID
  10. * @property string ORDER_ID
  11. * @property double AMOUNTS
  12. * @property integer PERIOD_NUM
  13. * @property integer CREATED_AT
  14. * @property string REMARK
  15. */
  16. class UserPerformanceLogs extends \common\components\ActiveRecord
  17. {
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public static function tableName()
  22. {
  23. return '{{%USER_PERFORMANCE_LOGS}}';
  24. }
  25. /**
  26. * @inheritdoc
  27. */
  28. public function rules()
  29. {
  30. return [
  31. [['USER_PERFORMANCE_ID'], 'required'],
  32. [['AMOUNTS'], 'number'],
  33. [['UPDATED_AT', 'PERIOD_NUM'], 'integer'],
  34. [['ID','USER_ID'], 'string', 'max' => 32],
  35. ];
  36. }
  37. /**
  38. * @inheritdoc
  39. */
  40. public function attributeLabels()
  41. {
  42. return [
  43. 'ID' => 'ID',
  44. 'USER_ID' => '用户id',
  45. 'AMOUNTS' => '消耗金额',
  46. 'CREATED_AT' => '修改时间',
  47. ];
  48. }
  49. /**
  50. * @throws Exception
  51. */
  52. public static function changeAmountLogs($prpId, $amount, $periodNum, $orderId = '')
  53. {
  54. $id = Date::today('Ymd') . self::_random(10, 1);
  55. $model = new self();
  56. $model->ID = $id;
  57. $model->USER_PERFORMANCE_ID = $prpId;
  58. $model->ORDER_ID = $orderId;
  59. $model->AMOUNTS = $amount;
  60. $model->PERIOD_NUM = $periodNum;
  61. $model->CREATED_AT = time();
  62. if(!$model->save()){
  63. throw new Exception($model->getErrors());
  64. }
  65. }
  66. private static function _random($length, $numeric = 0) {
  67. $seed = base_convert(md5(microtime() . $_SERVER['DOCUMENT_ROOT']), 16, $numeric ? 10 : 35);
  68. $seed = $numeric ? (str_replace('0', '', $seed) . '012340567890') : ($seed . 'zZ' . strtoupper($seed));
  69. $hash = '';
  70. $max = strlen($seed) - 1;
  71. for ($i = 0; $i < $length; $i++) {
  72. $hash .= $seed[mt_rand(0, $max)];
  73. }
  74. return $hash;
  75. }
  76. }