PrivateAccount.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: user
  5. * Date: 2019/11/17
  6. * Time: 14:38
  7. */
  8. namespace backend\models;
  9. use Yii;
  10. /**
  11. * This is the model class for table "crm_private_account".
  12. *
  13. * @property integer $id
  14. * @property integer $add_time
  15. * @property integer $is_close
  16. * @property integer $is_used
  17. * @property string $account
  18. */
  19. class PrivateAccount extends \yii\db\ActiveRecord
  20. {
  21. /**
  22. * @inheritdoc
  23. */
  24. public static function tableName()
  25. {
  26. return 'crm_private_account';
  27. }
  28. /**
  29. * @return \yii\db\Connection the database connection used by this AR class.
  30. */
  31. public static function getDb()
  32. {
  33. return Yii::$app->get('dbXcrm');
  34. }
  35. /**
  36. * @inheritdoc
  37. */
  38. public function rules()
  39. {
  40. return [
  41. [['id', 'add_time','is_close','is_used'], 'number'],
  42. [['account'], 'string'],
  43. ];
  44. }
  45. /**
  46. * @inheritdoc
  47. */
  48. public function attributeLabels()
  49. {
  50. return [
  51. 'id' => 'id',
  52. 'add_time' => 'add_time',
  53. 'is_close' => 'is_close',
  54. 'is_used' => 'is_used',
  55. 'account' => 'account',
  56. ];
  57. }
  58. /**
  59. * @return array|bool|\yii\db\ActiveRecord[]
  60. */
  61. public static function getAccountConfig()
  62. {
  63. $sql = "select id from crm_private_account where is_close = 0";
  64. $res = static::findBySql($sql)->asArray()->all();
  65. if(empty($res)){
  66. return false;//没有开放的对私帐户
  67. }
  68. $sql = "select id,account from crm_private_account where is_close = 0 and is_used = 0 order by id desc limit 1";
  69. $account_arr = static::findBySql($sql)->asArray()->all();
  70. if($account_arr[0]['id']){
  71. return $account_arr[0];
  72. }else{
  73. static::updateAll(['is_used' => 0],['is_used' => 1,'is_close' => 0]);
  74. $sql = "select id,account from crm_private_account where is_close = 0 and is_used = 0 order by id desc limit 1";
  75. $account_arr = static::findBySql($sql)->asArray()->all();
  76. if($account_arr[0]['id']){
  77. file_put_contents('test.txt',json_encode($account_arr[0]));
  78. return $account_arr[0];
  79. }
  80. }
  81. }
  82. }