Files
2026-09-01 22:15:50 +08:00

177 lines
5.4 KiB
PHP

<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace app\store\model;
use app\common\model\Account as AccountModel;
/**
* 公司用户池模型
* Class Account
* @package app\store\model
*/
class Account extends AccountModel
{
/**
* 获取当前用户总数
* @param array $param 查询参数
* @return int|string
*/
public function getUserTotal(array $param = [])
{
// 检索查询条件
$filter = $this->getUserTotalFilter($param);
// 查询结果
return $this->where($filter)
->where('is_delete', '=', '0')
->count();
}
/**
* 获取当前用户总数的查询条件
* @param array $param
* @return array
*/
private function getUserTotalFilter(array $param = [])
{
// 默认查询参数
$params = $this->setQueryDefaultValue($param, [
'search' => null, // 模糊搜索
'last_called_at' => null, // 最后使用时间
'company_id' => null, // 公司ID
]);
// 检索查询条件
$filter = [];
if (!is_null($params['last_called_at'])) {
$startTime = strtotime($params['last_called_at']);
$filter[] = ['last_called_at', '>=', $startTime];
$filter[] = ['last_called_at', '<', $startTime + 86400];
}
if ($params['company_id']) {
$filter[] = ['company_id', '=', $params['company_id']];
}
!empty($params['search']) && $filter[] = ['userNo|userName|mobile', 'like', "%{$params['search']}%"];
return $filter;
}
/**
* 获取用户列表
* @param array $param
* @return \think\Paginator
* @throws \think\db\exception\DbException
*/
public function getList(array $param = [])
{
// 检索查询条件
$filter = $this->getFilter($param);
// 获取用户列表
return $this->with(['company'])
->where($filter)
->where('is_delete', '=', '0')
->order(['create_time' => 'desc'])
->paginate(15);
}
/**
* 获取查询条件
* @param array $param
* @return array
*/
private function getFilter(array $param = [])
{
// 默认查询条件
$params = $this->setQueryDefaultValue($param, [
'search' => '', // 用户名称|手机号
'company_id' => '', // 公司ID
'last_called_at' => null, // 最后时间时间
'store_user_id' => null, // 所属员工
]);
// 检索查询条件
$filter = [];
// 微信昵称
!empty($params['search']) && $filter[] = ['userNo|userName|mobile', 'like', "%{$params['search']}%"];
// 所属公司
$params['company_id'] && $filter[] = ['company_id', '=', (int)$params['company_id']];
// 起止时间
if (!empty($params['last_called_at'])) {
$times = between_time($params['last_called_at']);
$filter[] = ['last_called_at', '>=', $times['start_time']];
$filter[] = ['last_called_at', '<', $times['end_time'] + 86400];
}
// 所属员工
$params['store_user_id'] && $filter[] = ['store_user_id', 'in', (array)$params['store_user_id']];
return $filter;
}
/**
* 删除用户
* @return bool|mixed
*/
public function setDelete()
{
return $this->transaction(function () {
// 标记为已删除
return $this->save(['is_delete' => 1]);
});
}
/**
* 新增记录
* @param $data
* @param array $user
* @return bool|false|int
* @throws \think\exception\DbException
*/
public function add($data, $user = [])
{
$this->startTrans();
try {
$data['store_user_id'] = $user['store_user_id'];
$data['store_id'] = self::$storeId;
$this->save($data);
$this->commit();
return true;
} catch (\Exception $e) {
$this->error = $e->getMessage();
$this->rollback();
return false;
}
}
/**
* 更新记录
* @param array $data
* @return bool
* @throws \think\exception\DbException
*/
public function edit($data)
{
if (empty($data['userNo'])) {
$this->error = '用户编号为空';
return false;
}
$this->startTrans();
try {
// 更新管理员记录
$this->save($data);
$this->commit();
return true;
} catch (\Exception $e) {
$this->error = $e->getMessage();
$this->rollback();
return false;
}
}
}