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

272 lines
8.9 KiB
PHP

<?php
declare (strict_types=1);
namespace app\store\controller;
use app\common\service\SybOil;
use app\common\service\IdingjuOil;
use app\store\controller\Controller;
use app\store\model\Company as CompanyModel;
use app\store\model\Account as AccountModel;
use app\store\model\Goods as GoodsModel;
use app\store\model\store\User;
use dashing\generator\Generator;
use think\facade\Db;
use think\response\Json;
use app\common\service\Random;
use app\common\service\QueueService;
/**
* 公司控制器
* Class Company
* @package app\store\controller
*/
class Company extends Controller
{
/**
* 公司列表
* @return mixed
* @throws \think\exception\DbException
*/
public function index()
{
$model = new CompanyModel;
$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']['company_id']) && $this->store['user']['is_super'] == 0){
$filter[] = ['company_id', 'in', json_decode($this->store['user']['company_id'],true)];
}*/
$list = $model->getList($data);
return $this->renderSuccess(['list'=>$list,'user'=>$this->store['user']]);
}
/**
* 获取所有公司
* @return mixed
* @throws \think\exception\DbException
*/
public function all(): Json
{
$user = $this->store['user'];
$list = CompanyModel::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 CompanyModel;
$data = $this->postForm();
// 新增记录
if ($model->add($data,$this->store['user'])) {
return $this->renderSuccess('添加成功');
}
return $this->renderError($model->getError() ?: '添加失败');
}
/**
* 公司详情
* @param int $companyId
* @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 $companyId): Json
{
$companyInfo = CompanyModel::detail($companyId);
return $this->renderSuccess(compact('companyInfo'));
}
/**
* 编辑公司
* @param int $companyId
* @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 $companyId): Json
{
// 商品详情
$model = CompanyModel::detail($companyId);
$data = $this->postForm();
// 更新记录
if ($model->edit($data)) {
return $this->renderSuccess('更新成功');
}
return $this->renderError($model->getError() ?: '更新失败');
}
/**
* 用户充值
* @param int $companyId
* @param string $target
* @return array
*/
public function recharge(int $companyId, string $target)
{
// 用户详情
$model = CompanyModel::detail($companyId);
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();
foreach ((array)$arr as $val){
$model = CompanyModel::detail($val);
$model->setDelete();
}
return $this->renderSuccess('删除成功');
}
/**
* 添加公司用户
* @return array|mixed
* @throws \think\Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function addAccount()
{
$queueService = new QueueService();
$data = $this->postForm();
if($data['users'] > 10000){
return $this->renderSuccess('单次最多生成10000条用户');
}
$users = (int)$data['users'];
$success = 0;
for ($i = 1;$i <= $users;$i++){
$model = new AccountModel;
$info = [
'userName' => Random::generateName(),
'mobile' => Random::getMobile(),
'userIdCard' => Random::getIdcard(),
'company_id' => $data['company_id'],
];
// 新增记录
if ($model->add($info,$this->store['user'])) {
$account_id = $model->account_id;
// $queueService->push('account_queue', $account_id);
$success++;
}
}
return $this->renderSuccess('成功添加'.$success.'用户');
}
/**
* 修改店铺列表状态
* @return Json
*/
public function changeState(): Json
{
$post = $this->postData();
$status = $post['state']?10:20;
// 商品详情
foreach ((array)$post['companyIds'] as $company_id) {
$model = CompanyModel::detail($company_id);
$warn = $model->changeStatus($status);
if ($warn !== true) {
return $this->renderError($warn);
}
}
return $this->renderSuccess($post);
}
/**
* @param $orgNo
* @return array
*/
public function getGoodsNo($orgNo): array
{
switch ($orgNo) {
case 'umQZhIHKofyA':
return ['djgdface100','djgdface200','djgdface300'];
case 'HpWqXIgYvTBg':
return ['jmgxyyface100','jmgxyyface200','jmgxyyface300','jmgzface100','jmgzface200','jmgzface300','jmfjface100','jmfjface200','jmfjface300'];
case 'NKJBgaaRNdiL':
return ['face003'];
default:
return [];
}
}
/**
* 更新公司商品库存
* @return Json
*/
public function stock(): Json
{
$arr = $this->postData();
// 商品详情
foreach ((array)$arr as $company_id) {
$company = CompanyModel::detail($company_id);
$IdingjuOil = new IdingjuOil();
$goodsNo = $this->getGoodsNo($company['orgNo']);
$goodsList = GoodsModel::getAll([['goods_no','in',$goodsNo]]);
$canOrderNum = $unUseNum = $useNum = 0;
foreach ($goodsList as $array){
// 对接平台接口
$res = $IdingjuOil->stockList(['goodsNo' => $array['goods_no']], $company);
// 更新记录
if ($res['code'] == '1') {
/*$res['data']['canOrderNum'] = $res['data']['canOrderNum']?:0;
$res['data']['unUseNum'] = $res['data']['unUseNum']?:0;
$res['data']['useNum'] = $res['data']['useNum']?:0;
$canOrderNum += $res['data']['canOrderNum'];
$unUseNum += $res['data']['unUseNum'];
$useNum += $res['data']['useNum'];
Db::name('goods')
->where('goods_id', $array['goods_id'])
->update(['stock_total' => $res['data']['canOrderNum'], 'unUseNum' => $res['data']['unUseNum'], 'useNum' => $res['data']['useNum']]);
Db::name('goods_sku')
->where('goods_id', $array['goods_id'])
->update(['stock_num' => $res['data']['canOrderNum'], 'unUseNum' => $res['data']['unUseNum'], 'useNum' => $res['data']['useNum']]);
*/
$res['data']['quantity'] = $res['data']['quantity']?:0;
$canOrderNum += $res['data']['quantity'];
Db::name('goods')
->where('goods_id', $array['goods_id'])
->update(['stock_total' => $res['data']['quantity']]);
Db::name('goods_sku')
->where('goods_id', $array['goods_id'])
->update(['stock_num' => $res['data']['quantity']]);
}
}
$company->save(['canOrderNum'=>$canOrderNum,'unUseNum'=>$unUseNum,'useNum'=>$useNum]);
}
return $this->renderSuccess($arr);
}
}