<?php
/**
 * Created by PhpStorm.
 * User: xiaofeng
 * Date: 2018/3/12
 * Time: 上午9:57
 * 同步资讯
 */

namespace backend\server;

use common\models\UserRole;
use common\models\UserUr;
use Yii;
use common\models\User;

class SyncUser
{
    public function userOpe($input)
    {
        try {
            $auditor_name = $input['auditor_name'];
            $oss_content = $input['data'];
            $this_content = [];
            $content = json_decode($oss_content, true);
            $userNameList = json_encode(array_column($content, 'name'), JSON_UNESCAPED_UNICODE);
            $Transaction = Yii::$app->db->beginTransaction();
            foreach ($content as $val) {
                $row = User::find()->where(['unid' => $val['unid']])->one();
                if(empty($row)){
                    $row = User::find()->where(['user' => $val['user']])->one();
                }
                if ($row) {
                    $this_content[] = [
                        'user' => $val['user'],
                        'password' => $val['password'],
                        'name' => $val['name'],
                        'status' => $val['status'],
                        'unid' => $val['unid'],
                    ];
                    $row->user = $val['user'];
                    $row->password = $val['password'];
                    $row->name = $val['name'];
                    $row->status = $val['status'];
                    $row->unid = $val['unid'];
                    $row->update(false);
                    $code = 'edit';
                } else {
                    $row = new user();
                    $row->user = $val['user'];
                    $row->password = $val['password'];
                    $row->name = $val['name'];
                    $row->status = $val['status'];
                    $row->unid = $val['unid'];
                    if (!($row->save())) {
                        $Transaction->rollBack();
                        return '插入失败-2';
                    }
                    $code = 'add';
                }
                $UserInfo = [
                    'user_list' => $userNameList,
                    'auditor_name' => $auditor_name,
                    'this_content' => json_encode($this_content),
                    'oss_content' => $oss_content,
                    'code' => $code
                ];
                if (!empty($val['role']) && !$this->addUserRole($row->uid, $val['role'])) {
                    $Transaction->rollBack();
                    return '用户角色分配失败!';
                }

                if (!($this->addUserRecord($UserInfo))) {
                    $Transaction->rollBack();
                    return '纪录表记录失败';
                }
                $Transaction->commit();
                return true;
            }
            return $userNameList;
        } catch (\Exception $e) {
            return $e->getMessage();
        }

    }

    public function addUserRecord($UserInfo)
    {
        $row = new \common\models\Syncuserrecord();
        $row->user_list = $UserInfo['user_list'];
        $row->auditor_name = $UserInfo['auditor_name'];
        $row->this_content = $UserInfo['this_content'];
        $row->oss_content = $UserInfo['oss_content'];
        $row->code = $UserInfo['code'];
        if ($row->insert()) {
            return true;
        } else {
            return false;
        }
    }

    //用户角色分配
    public function addUserRole($uid, $role)
    {
        $roleModel = UserRole::find()->where(['name' => $role])->select('id')->column();
        UserUr::deleteAll(['uid' => $uid]);
        $urList = [];
        $time = time();
        foreach ($roleModel as $val) {
            $t['uid'] = $uid;
            $t['rid'] = $val;
            $t['create_at'] = $time;
            $urList[] = $t;
        }
        $state = Yii::$app->db->createCommand()->batchInsert('pfg_user_ur', ['uid', 'rid', 'create_at'], $urList)->execute();
        if ($state) {
            return true;
        }
        return false;
    }
}