Files
oil/server-tp6/app/store/model/Company.php
T
2026-09-01 22:15:50 +08:00

221 lines
6.6 KiB
PHP

<?php
declare (strict_types=1);
namespace app\store\model;
use app\store\model\store\UserRole;
use think\Session;
use app\common\model\Company as CompanyModel;
use app\store\model\Order as OrderModel;
use app\store\model\store\User as UserModel;
use app\store\model\company\BalanceLog as BalanceLogModel;
use app\store\service\store\User as StoreUserService;
use app\common\enum\company\balanceLog\Scene as SceneEnum;
use app\common\library\helper;
/**
* 公司模型
* Class Company
* @package app\store\model
*/
class Company extends CompanyModel
{
// 充值类型枚举: 余额
const RECHARGE_TYPE_BALANCE = 'balance';
/**
* 获取商户列表
* @param array $data
* @return \think\Paginator
* @throws \think\exception\DbException
*/
public function getList($data = [])
{
return $this->order(['create_time' => 'desc'])
->where($data)
->paginate(15);
}
/**
* 新增记录
* @param $data
* @param array $user
* @return bool|false|int
* @throws \think\exception\DbException
*/
public function add($data, $user = [])
{
if (empty($data['name'])) {
$this->error = '请填写公司名称';
return false;
}
if (empty($data['address'])) {
$this->error = '请填写公司地址';
return false;
}
if (empty($data['orgNo'])) {
$this->error = '请填写公司编号';
return false;
}
$this->startTrans();
try {
$data['privateKey'] = str_replace(["\r\n", "\n"], '', $data['privateKey']);
$data['publicKey'] = str_replace(["\r\n", "\n"], '', $data['publicKey']);
$data['platformPublicKey'] = str_replace(["\r\n", "\n"], '', $data['platformPublicKey']);
$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;
}
}
/*返回随机数*/
public function suiji($digit=12){
$a = "abcdefghijklmnopqrstuvwxyz";
$x = strlen($a)-1;
$html = "";
for($i=1;$i<=$digit;$i++) {
$rand = rand(1,$x);
$html .= substr($a,$rand,1);
if($i == 1){//某些语言变量首字母不能是数字
$a .= "0123456789";
$x = strlen($a)-1;
}
}
return $html;
}
/**
* 更新记录
* @param array $data
* @return bool
* @throws \think\exception\DbException
*/
public function edit($data)
{
if (empty($data['name'])) {
$this->error = '请填写公司名称';
return false;
}
if (empty($data['address'])) {
$this->error = '请填写公司地址';
return false;
}
if (empty($data['orgNo'])) {
$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;
}
}
/**
* 软删除
* @return false|int
*/
public function setDelete()
{
return $this->delete();
}
/**
* 公司数据统计
* @param $user
* @param $get
* @return \think\Paginator
* @throws \think\exception\DbException
*/
public function getCompanyStatistics($user,$get = [])
{
$filter = [];
if($user['is_super'] == 0){
$company_idArr = $user['company_id']?json_decode($user['company_id'],true):[];
$filter[] = ['a.company_id', 'in', $company_idArr];
}
if(isset($get['name'])){
$filter[] = ['a.name', 'like', '%'.$get['name'].'%'];
}
$times = [];
if(isset($get['betweenTime'])) {
$times = between_time($get['betweenTime']);
}
$and = "";
if(isset($times['start_time'])){
$and .= " and create_time >= '".$times['start_time']."' ";
}
if(isset($times['end_time'])){
$and .= " and create_time < '".($times['end_time'] + 86400)."' ";
}
$and2 = " and create_time >= '".strtotime(date('Y-m-d'))."' and create_time < '".strtotime(date('Y-m-d').' 23:59:59')."' ";
return $this->alias('a')
->field('a.*, (select IFNULL(sum(amount),0) from yoshop_order where company_id = a.company_id and order_status = 20 '.$and.') as amount, (select IFNULL(sum(amount),0) from yoshop_order where company_id = a.company_id and order_status = 20 '.$and2.') as today')
->order(['a.create_time' => 'desc'])
->where($filter)
->paginate(15);
}
/**
* 商户充值
* @param string $target 充值类型
* @param array $data 表单数据
* @return bool
*/
public function recharge(string $target, array $data)
{
// 当前操作人用户名
$storeUserName = StoreUserService::getLoginInfo()['user']['user_name'];
if ($target === self::RECHARGE_TYPE_BALANCE) {
return $this->rechargeToBalance($storeUserName, $data['balance']);
}
return false;
}
/**
* 商户充值:余额
* @param string $storeUserName
* @param array $data
* @return bool
*/
private function rechargeToBalance(string $storeUserName, array $data)
{
if (!isset($data['money']) || $data['money'] === '' || $data['money'] < 0) {
$this->error = '请输入正确的金额';
return false;
}
// 判断充值方式,计算最终金额
if ($data['mode'] === 'inc') {
$diffMoney = $data['money'];
} elseif ($data['mode'] === 'dec') {
$diffMoney = -$data['money'];
} else {
$diffMoney = helper::bcsub($data['money'], $this['balance']);
}
// 更新记录
$this->transaction(function () use ($storeUserName, $data, $diffMoney) {
// 更新账户余额
static::setIncBalance((int)$this['company_id'], (float)$diffMoney);
// 新增余额变动记录
BalanceLogModel::add(SceneEnum::ADMIN, [
'company_id' => $this['company_id'],
'money' => (float)$diffMoney,
'remark' => $data['remark'],
], [$storeUserName]);
});
return true;
}
}