SyncUser.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: xiaofeng
  5. * Date: 2018/3/12
  6. * Time: 上午9:57
  7. * 同步资讯
  8. */
  9. namespace backend\server;
  10. use common\models\UserRole;
  11. use common\models\UserUr;
  12. use Yii;
  13. use common\models\User;
  14. class SyncUser
  15. {
  16. public function userOpe($input)
  17. {
  18. try {
  19. $auditor_name = $input['auditor_name'];
  20. $oss_content = $input['data'];
  21. $this_content = [];
  22. $content = json_decode($oss_content, true);
  23. $userNameList = json_encode(array_column($content, 'name'), JSON_UNESCAPED_UNICODE);
  24. $Transaction = Yii::$app->db->beginTransaction();
  25. foreach ($content as $val) {
  26. $row = User::find()->where(['unid' => $val['unid']])->one();
  27. if(empty($row)){
  28. $row = User::find()->where(['user' => $val['user']])->one();
  29. }
  30. if ($row) {
  31. $this_content[] = [
  32. 'user' => $val['user'],
  33. 'password' => $val['password'],
  34. 'name' => $val['name'],
  35. 'status' => $val['status'],
  36. 'unid' => $val['unid'],
  37. ];
  38. $row->user = $val['user'];
  39. $row->password = $val['password'];
  40. $row->name = $val['name'];
  41. $row->status = $val['status'];
  42. $row->unid = $val['unid'];
  43. $row->update(false);
  44. $code = 'edit';
  45. } else {
  46. $row = new user();
  47. $row->user = $val['user'];
  48. $row->password = $val['password'];
  49. $row->name = $val['name'];
  50. $row->status = $val['status'];
  51. $row->unid = $val['unid'];
  52. if (!($row->save())) {
  53. $Transaction->rollBack();
  54. return '插入失败-2';
  55. }
  56. $code = 'add';
  57. }
  58. $UserInfo = [
  59. 'user_list' => $userNameList,
  60. 'auditor_name' => $auditor_name,
  61. 'this_content' => json_encode($this_content),
  62. 'oss_content' => $oss_content,
  63. 'code' => $code
  64. ];
  65. if (!empty($val['role']) && !$this->addUserRole($row->uid, $val['role'])) {
  66. $Transaction->rollBack();
  67. return '用户角色分配失败!';
  68. }
  69. if (!($this->addUserRecord($UserInfo))) {
  70. $Transaction->rollBack();
  71. return '纪录表记录失败';
  72. }
  73. $Transaction->commit();
  74. return true;
  75. }
  76. return $userNameList;
  77. } catch (\Exception $e) {
  78. return $e->getMessage();
  79. }
  80. }
  81. public function addUserRecord($UserInfo)
  82. {
  83. $row = new \common\models\Syncuserrecord();
  84. $row->user_list = $UserInfo['user_list'];
  85. $row->auditor_name = $UserInfo['auditor_name'];
  86. $row->this_content = $UserInfo['this_content'];
  87. $row->oss_content = $UserInfo['oss_content'];
  88. $row->code = $UserInfo['code'];
  89. if ($row->insert()) {
  90. return true;
  91. } else {
  92. return false;
  93. }
  94. }
  95. //用户角色分配
  96. public function addUserRole($uid, $role)
  97. {
  98. $roleModel = UserRole::find()->where(['name' => $role])->select('id')->column();
  99. UserUr::deleteAll(['uid' => $uid]);
  100. $urList = [];
  101. $time = time();
  102. foreach ($roleModel as $val) {
  103. $t['uid'] = $uid;
  104. $t['rid'] = $val;
  105. $t['create_at'] = $time;
  106. $urList[] = $t;
  107. }
  108. $state = Yii::$app->db->createCommand()->batchInsert('pfg_user_ur', ['uid', 'rid', 'create_at'], $urList)->execute();
  109. if ($state) {
  110. return true;
  111. }
  112. return false;
  113. }
  114. }