<?php namespace app\common\model; use think\Model; use think\Db; use fast\Random; /** * 模型 */ class Guild extends Model { // 开启自动写入时间戳字段 protected $autoWriteTimestamp = 'int'; // 定义时间戳字段名 protected $createTime = 'createtime'; /** * 创建公会 */ public static function createGuild($party_id,$party_name,$user_id) { Db::startTrans(); try{ // 获取g_id $ids = self::column("g_id"); $data = [ "g_id" => self::getUinqueId(4,$ids), "user_id" => $user_id, "party_id" => $party_id, "name" => $party_name."的公会", "image" => "/assets/img/guild_image.jpeg", "desc" => "请编辑公会简介内容!", "notice" => "请编辑公会公告内容!", "createtime" => time() ]; $guild_id = self::insertGetId($data); // 添加工会长 $data = [ "guild_id" => $guild_id, "user_id" => $user_id, "role" => 2, "sign_type" => 3, "status" => 1, "sign_time" => 2147483647, "createtime" => time() ]; $res2 = \app\common\model\GuildMember::insert($data); // 更新用户公会ID $res3 = \app\common\model\User::update(["guild_id"=>$guild_id],["id"=>$user_id]); if($guild_id && $res2 && $res3) { Db::commit(); } }catch (ValidateException $e) { Db::rollback(); } catch (PDOException $e) { Db::rollback(); } catch (Exception $e) { Db::rollback(); } } /** * 生成不重复的随机数字 */ public static function getUinqueId($length = 4,$ids = []) { $newid = Random::build("nozero",$length); if(in_array($newid,$ids)) { self::getUinqueId(4,$ids); } return $newid; } }