You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
nt_backend/application/common/model/UserCompany.php

139 lines
4.5 KiB

<?php
namespace app\common\model;
use addons\shopro\exception\Exception;
use app\admin\model\AuthGroup;
use app\admin\model\AuthGroupAccess;
use app\common\library\Auth;
use fast\Random;
use think\Db;
use think\Model;
class UserCompany extends Model
{
// 表名
protected $name = 'user_company';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
public function companyUser() {
return $this->hasOne(User::class,'company_id','id');
}
public static function getInfoByIds($ids) {
return self::field('id,company_name,contact_phone')->whereIn('id', $ids)->select();
}
public function getAuditStatusList()
{
return ['0' => __('Audit_status 0'), '1' => __('Audit_status 1'), '2' => __('Audit_status 2')];
}
/**
* 添加企业管理员
* @return boolean
*/
public static function saveCompanyAdmin($data, $type = 'add') {
$salt = Random::alnum();
$user_data = [
'username' => $data['account'],
'nickname' => $data['contact_name'],
'email' => $data['contact_email'],
'mobile' => $data['contact_phone'],
'password' => Auth::instance()->getEncryptPassword($data['pwd'],$salt),
'company_id' => $data['company_id'],
'avatar' => '/assets/img/avatar.png', //设置新管理员默认头像。
'salt' => $salt
];
if ($data['company_id'] && $type == 'update') {
User::where('company_id',$data['company_id'])->update($user_data);
} else {
//添加用户账号信息
$user = User::create($user_data);
//添加企业管理员权限
$group_id = AuthGroup::where('type', AuthGroup::$group_type[0]['id'])->value('id');
AuthGroupAccess::create(['uid' => $user->id, 'group_id' => $group_id]);
//记录企业用户关系
ProjectCompanyUser::create(['company_id' => $data['company_id'], 'user_id' => $user->id, 'role_id' => $group_id, 'role_type' => AuthGroup::$group_type[0]['id']]);
}
return true;
}
/**
* 变更账户余额
* @param int $money 余额
* @param int $user_id 会员ID
* @param int $company_id 企业ID
* @param string $memo 备注
*/
public static function money($money, $user_id, $company_id,$memo, $ext = [])
{
// 判断用户
if (is_numeric($user_id)) {
$user = User::get($user_id);
}
if (!$user) {
new Exception('未找到用户');
}
// 判断金额
if ($money == 0) {
new Exception('请输入正确的金额');
}
$company = self::lock(true)->find($company_id);
if (!$company) {
new Exception('您还不是企业支付账号');
}
$before = $company->money ?? 0;
$after = $company->money + $money ?? 0;
if ($after < 0) {
new Exception('可用余额不足');
}
Db::startTrans();
try {
//更新账户余额
$company->save(['money' => $after]);
//写入日志
CompanyMoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => $memo, 'ext' => ($ext ? json_encode($ext) : '')]);
Db::commit();
} catch (\Exception $e) {
Db::rollback();
new Exception('您提交的数据不正确');
}
}
/**
* 获取用户可以支付的企业信息
* @return array
*/
public static function getPayByCompanyIds($user_id,$companyIds) {
$money = 0;//企业余额
$is_pay = 0;//是否可以企业支付
$company_id = 0;//可以支付的企业ID
if ($companyIds) {
foreach ($companyIds as $company_id) {
$company = UserCompany::get($company_id);
$money = $company->money ?? 0;
//判断账号是否可以进行支付
if(Auth::instance()->isCompanyAdminNew($user_id, $company_id)) {
$is_pay = 1;
break;
}
if (!empty($company->pay_account)) {
$pay_account = explode(',', $company->pay_account);
if (in_array($user_id,$pay_account)) {
$is_pay = 1;
break;
}
}
}
}
return ['money'=>$money, 'is_pay'=>$is_pay, 'company_id'=>$company_id];
}
}