feat: 同步数据接收
This commit is contained in:
71
app/admin/controller/ReceiveSync.php
Normal file
71
app/admin/controller/ReceiveSync.php
Normal file
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\admin\controller;
|
||||
|
||||
use app\admin\model\v1\LanguageModel;
|
||||
use app\admin\model\v1\ProductCategoryModel;
|
||||
use app\admin\model\v1\ProductTcoCategoryModel;
|
||||
use think\facade\Db;
|
||||
|
||||
class Operate_Of_ReceiveSync
|
||||
{
|
||||
const Add = 'add';
|
||||
const Update = 'update';
|
||||
const Enable = 'enable';
|
||||
const Disable = 'disable';
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收同步数据
|
||||
*/
|
||||
class ReceiveSync
|
||||
{
|
||||
public function product_category()
|
||||
{
|
||||
$data = request()->post();
|
||||
if (empty($data)) return error('请确认同步数据');
|
||||
|
||||
$lang_id = LanguageModel::where('code', $data['lang'])->value('id');
|
||||
$record = [
|
||||
'language_id' => $lang_id,
|
||||
'name' => $data['name'],
|
||||
'tco_id' => $data['tco_id'],
|
||||
'tco_pid' => $data['tco_pid'],
|
||||
'tco_path' => $data['tco_path'],
|
||||
'erp_id' => $data['erp_id'],
|
||||
'erp_pid' => $data['erp_pid'],
|
||||
'erp_code' => $data['erp_code'],
|
||||
'erp_path' => $data['erp_path'],
|
||||
'disabled' => 0,
|
||||
'sync_time' => strtotime($data['created_at'])
|
||||
];
|
||||
|
||||
if (Operate_Of_ReceiveSync::Disable == $data['operate']) {
|
||||
$record['disabled'] = 1;
|
||||
}
|
||||
|
||||
$validate = validate([
|
||||
'name|分类名称' => 'require',
|
||||
'erp_code|分类ERP编码' => 'require',
|
||||
]);
|
||||
if (!$validate->check($record)) {
|
||||
throw new \Exception((string)$validate->getError());
|
||||
}
|
||||
|
||||
$category = ProductTcoCategoryModel::language($lang_id)->erpCode($record['erp_code'])->find();
|
||||
Db::startTrans();
|
||||
try {
|
||||
if (empty($category)) {
|
||||
ProductTcoCategoryModel::create($record);
|
||||
ProductCategoryModel::where
|
||||
} else {
|
||||
$category->save($record);
|
||||
}
|
||||
} catch (\Throwable $th) {
|
||||
Db::rollback();
|
||||
throw $th;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -23,6 +23,12 @@ class ProductTcoCategoryModel extends ProductTcoCategoryBaseModel
|
||||
$query->where('language_id', '=', $value);
|
||||
}
|
||||
|
||||
// 根据ERP Code查询
|
||||
public function scopeErpCode($query, $value)
|
||||
{
|
||||
$query->where('erp_code', '=', $value);
|
||||
}
|
||||
|
||||
// 按分类名称搜索
|
||||
public function searchNameAttr($query, $value, $data)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user