Files
oil/server-tp6/app/store/controller/Partners.php
T
2026-09-18 20:55:21 +08:00

191 lines
6.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
declare (strict_types=1);
namespace app\store\controller;
use app\common\model\qyt\Enterprise as EnterpriseModel;
use app\store\controller\Controller;
use app\store\model\Partners as PartnersModel;
use app\store\model\partners\Order as OrderModel;
use app\store\model\store\User;
use think\response\Json;
/**
* 商户控制器
* Class StoreUser
* @package app\store\controller
*/
class Partners extends Controller
{
/**
* 商户列表
* @return mixed
* @throws \think\exception\DbException
*/
public function index()
{
$model = new PartnersModel;
$getData = $this->request->param();
$data = [];
if (isset($getData['name']) && !empty($getData['name'])) {
$data[] = ['name', 'like', "%{$getData['name']}%"];
}
$user = $this->store['user'];
if($user['is_super'] == 0) {
$data[] = ['store_user_id', 'in', User::getScope($user)];
}
/*if(!empty($this->store['user']['partners_id']) && $this->store['user']['is_super'] == 0){
$filter[] = ['partners_id', 'in', json_decode($this->store['user']['partners_id'],true)];
}*/
$list = $model->getList($data)->toArray();
$newList = [];
$OrderModel = new OrderModel();
$date = date("Y-m-d");
// 企油通企业:口径与下单时 Enterprise::resolve 一致(绑定且启用 -> 用绑定的;否则 -> 默认企业)
$bindIds = array_filter(array_map('intval', array_column($list['data'], 'qyt_enterprise_id')));
$enterprises = $bindIds
? array_column(EnterpriseModel::whereIn('enterprise_id', $bindIds)
->field('enterprise_id,company_name,passageway,is_enable')->select()->toArray(), null, 'enterprise_id')
: [];
$default = EnterpriseModel::getDefault();
foreach ($list['data'] as $val){
$val['day_money'] =$OrderModel->getOrderTotalPrice($date, $date, $val);
$val['total_money'] =$OrderModel->getOrderTotalPrice(null, null, $val);
$val = array_merge($val, $this->enterpriseInfo((int)$val['qyt_enterprise_id'], $enterprises, $default));
$newList[] = $val;
}
$list['data'] = $newList;
return $this->renderSuccess(['list'=>$list,'user'=>$this->store['user']]);
}
/**
* 合作商实际使用的企油通企业
* qyt_enterprise_source:bind 绑定 / default 未绑定走默认 / invalid 绑定的企业已停用或删除(实际走默认)/ none 无可用企业
*/
private function enterpriseInfo(int $bindId, array $enterprises, $default): array
{
$bound = $bindId > 0 ? ($enterprises[$bindId] ?? null) : null;
if ($bound && (int)$bound['is_enable'] === 1) {
$source = 'bind';
$use = $bound;
} else {
$source = $bindId > 0 ? 'invalid' : 'default';
$use = $default ? $default->toArray() : null;
!$use && $source = $bindId > 0 ? 'invalid' : 'none';
}
return [
'qyt_enterprise_source' => $source,
// 绑定失效时也把原绑定企业名带出来,便于排查
'qyt_bind_name' => $bound['company_name'] ?? '',
'qyt_enterprise_name' => $use['company_name'] ?? '',
'qyt_enterprise_channel' => isset($use['passageway']) ? ($use['passageway'] === 'pingan' ? '平安银行' : '支付宝企业码') : '',
];
}
/**
* 获取所有商户
* @return mixed
* @throws \think\exception\DbException
*/
public function all(): Json
{
$user = $this->store['user'];
$list = PartnersModel::getAll($user);
return $this->renderSuccess(compact('list','user'));
}
/**
* 添加商户
* @return array|mixed
* @throws \think\Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function add()
{
$model = new PartnersModel;
$data = $this->postForm();
// 新增记录
if ($model->add($data,$this->store['user'])) {
return $this->renderSuccess('添加成功');
}
return $this->renderError($model->getError() ?: '添加失败');
}
/**
* 商户详情
* @param int $partnersId
* @return Json
* @throws BaseException
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @throws \cores\exception\BaseException
*/
public function detail(int $partnersId): Json
{
$partnersInfo = PartnersModel::detail($partnersId);
return $this->renderSuccess(compact('partnersInfo'));
}
/**
* 编辑商户
* @param int $partnersId
* @return Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @throws \cores\exception\BaseException
*/
public function edit(int $partnersId): Json
{
// 商品详情
$model = PartnersModel::detail($partnersId);
$data = $this->postForm();
// 更新记录
if ($model->edit($data)) {
return $this->renderSuccess('更新成功');
}
return $this->renderError($model->getError() ?: '更新失败');
}
/**
* 用户充值
* @param int $partnersId
* @param string $target
* @return array
*/
public function recharge(int $partnersId, string $target)
{
// 用户详情
$model = PartnersModel::detail($partnersId);
if ($model->recharge($target, $this->postForm())) {
return $this->renderSuccess('操作成功');
}
return $this->renderError($model->getError() ?: '操作失败');
}
/**
* 删除商户
* @return Json
* @throws \think\exception\DbException
*/
public function delete(): Json
{
$arr = $this->postData();
$userModel = new User();
foreach ((array)$arr as $val){
$model = PartnersModel::detail($val);
if ($model->setDelete()) {
$userModel->where([
['level','=',1],
['partners_id','like','%'.$val.'%'],
])->delete();
}
}
return $this->renderSuccess('删除成功');
}
}