249 lines
10 KiB
PHP
Executable File
249 lines
10 KiB
PHP
Executable File
<?php
|
||
|
||
namespace app\admin\controller;
|
||
|
||
use app\common\controller\BaseController as Base;
|
||
use \think\Validate;
|
||
|
||
class Operate_Of_ReceiveSync
|
||
{
|
||
const Add = 'add';
|
||
const Update = 'update';
|
||
const Enable = 'enable';
|
||
const Disable = 'disable';
|
||
}
|
||
|
||
// 接收来自产品目录系统的同步
|
||
class ReceiveSync extends Base
|
||
{
|
||
// 接收分类同步
|
||
public function category()
|
||
{
|
||
$data = request()->post();
|
||
if (empty($data)) {
|
||
return json(['code' => 0, 'msg' => '请确认同步数据']);
|
||
}
|
||
|
||
$query = new \think\db\Query();
|
||
|
||
$language = $data['lang'] == 'zh-cn' ? 'ZH' : 'US';
|
||
try {
|
||
$record = [
|
||
'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'],
|
||
'country_code' => $language,
|
||
'sync_time' => strtotime($data['created_at'])
|
||
];
|
||
|
||
if (Operate_Of_ReceiveSync::Disable == $data['operate']) {
|
||
$record['disabled'] = 1;
|
||
}
|
||
|
||
$validate = new Validate([
|
||
'name|分类名称' => 'require',
|
||
'erp_code|分类ERP编码' => 'require',
|
||
]);
|
||
if (!$validate->check($record)) {
|
||
throw new \Exception((string)$validate->getError());
|
||
}
|
||
|
||
$mp = $query->name('product_tco_category')
|
||
->field(['id', 'category_id', 'sync_time'])
|
||
->where('erp_code', '=', $record['erp_code'])
|
||
->where('country_code', '=', $record['country_code'])
|
||
->find();
|
||
if (empty($mp)) {
|
||
$record['created_at'] = date('Y-m-d H:i:s');
|
||
$query->transaction(function () use ($query, $language, $record) {
|
||
$pid = 0;
|
||
if (!empty($record['tco_pid'])) {
|
||
$prev = $query->name('product_tco_category')
|
||
->where('tco_id', '=', $record['tco_pid'])
|
||
->where('country_code', '=', $language)
|
||
->find();
|
||
if (!empty($prev) && !empty($prev['category_id'])) {
|
||
$pid = $prev['category_id'];
|
||
|
||
// 更新上级分类
|
||
$query->name('product_category')->where('id', '=', $pid)->update(['haschild' => 1]);
|
||
}
|
||
}
|
||
// 同步新增官网分类
|
||
$record['category_id'] = $query->name('product_category')->insertGetId([
|
||
'pid' => $pid,
|
||
'name' => $record['name'],
|
||
'sort' => 9999,
|
||
'isshow' => 1,
|
||
'content' => '',
|
||
'classtype' => 2,
|
||
'siteid' => 32267,
|
||
'country_code' => $record['country_code'],
|
||
]);
|
||
if (empty($record['category_id'])) {
|
||
throw new \Exception('同步新增失败');
|
||
}
|
||
|
||
// 新增官网与产品目录关联分类
|
||
$ok = $query->name('product_tco_category')->insert($record);
|
||
if (!$ok) {
|
||
throw new \Exception('同步新增失败');
|
||
}
|
||
});
|
||
} else {
|
||
// 同步时间检查,防止脏数据
|
||
if ($mp['sync_time'] < $record['sync_time']) {
|
||
$record['updated_at'] = date('Y-m-d H:i:s');
|
||
$ok = $query->name('product_tco_category')->where('id', '=', $mp['id'])->update($record);
|
||
if (!$ok) {
|
||
throw new \Exception('同步更新失败');
|
||
}
|
||
$children = $query->name('product_tco_category')->where('tco_pid', '=', $mp['tco_id'])->find();
|
||
if (empty($children)) {
|
||
$query->name('product_category')->where('id', '=', $mp['category_id'])->update(['haschild' => 0]);
|
||
}
|
||
}
|
||
}
|
||
} catch (\Throwable $th) {
|
||
return json([
|
||
'code' => 0,
|
||
'msg' => sprintf("Exception %s %s:%d", $th->getMessage(), $th->getFile(), $th->getLine()),
|
||
]);
|
||
}
|
||
|
||
return json([
|
||
'code' => 1,
|
||
'msg' => '成功',
|
||
]);
|
||
}
|
||
|
||
// 接收产品同步
|
||
public function product()
|
||
{
|
||
$data = request()->post();
|
||
if (empty($data)) {
|
||
return json(['code' => 0, 'msg' => '请确认同步数据']);
|
||
}
|
||
|
||
$query = new \think\db\Query();
|
||
|
||
try {
|
||
$validate = new Validate([
|
||
'spu' => 'require',
|
||
'name' => 'require',
|
||
'category_erp_code' => 'require',
|
||
'lang' => 'require',
|
||
'created_at' => 'require',
|
||
]);
|
||
if (!$validate->check($data)) {
|
||
throw new \Exception((string)$validate->getError());
|
||
}
|
||
|
||
$language = $data['lang'] == 'zh-cn' ? 'ZH' : 'US';
|
||
$record = [
|
||
'name' => $data['name'],
|
||
'brand_id' => $data['spu'],
|
||
'country_code' => $language,
|
||
'synctime' => strtotime($data['created_at'])
|
||
];
|
||
|
||
// 执行下架
|
||
if (Operate_Of_ReceiveSync::Disable == $data['operate']) {
|
||
$record['is_show'] = -1;
|
||
}
|
||
|
||
// 如果 spu_before_modification 存在则根据 spu_before_modification 更新型号
|
||
$spu = !empty($data['spu_before_modification']) ? $data['spu_before_modification'] : $data['spu'];
|
||
$mp = $query->name('product')
|
||
->field(['id', 'cid', 'synctime'])
|
||
->where('brand_id', '=', $spu)
|
||
->where('country_code', '=', $language)
|
||
->find();
|
||
if (empty($mp)) {
|
||
// 避免 spu_before_modification 更新型号时,人为删除了旧型号导致的新增,从而出现重复型号问题,而进行再次验证
|
||
$exists = $query->name('product')->where('brand_id', '=', $data['spu'])->where('country_code', '=', $language)->value('id');
|
||
if ($exists) {
|
||
throw new \Exception(sprintf('【%s】该型号已存在', $data['spu']));
|
||
}
|
||
|
||
$record['stock_quantity'] = 0; // 库存
|
||
$record['sort'] = 9999; // 排序值
|
||
$record['isnew'] = 1; // 是否新品
|
||
$record['ishot'] = 0; // 是否热门
|
||
$record['recommend'] = 0; // 是否爆款
|
||
$record['isfeatured'] = 0; // 是否特色
|
||
$record['stat'] = 0; // 状态
|
||
$record['is_show'] = -1; // 是否上架,0为上架,-1为不上架
|
||
$record['createtime'] = time();
|
||
$record['product_desc'] = serialize([]);
|
||
$record['siteid'] = 32267;
|
||
|
||
// 关联分类
|
||
$category_id = $query->name('product_tco_category')
|
||
->where('erp_code', '=', $data['category_erp_code'])
|
||
->where('country_code', '=', $language)
|
||
->value('category_id');
|
||
if (empty($category_id)) {
|
||
throw new \Exception('官网未找到关联的分类');
|
||
}
|
||
$record['cid'] = $category_id;
|
||
|
||
$ok = $query->name('product')->insert($record);
|
||
if (!$ok) {
|
||
throw new \Exception('同步新增失败');
|
||
}
|
||
} else {
|
||
// 同步时间检查,防止脏数据
|
||
if ($mp['synctime'] < $record['synctime']) {
|
||
$category_id = $query->name('product_tco_category')
|
||
->where('erp_code', '=', $data['category_erp_code'])
|
||
->where('country_code', '=', $language)
|
||
->value('category_id');
|
||
// 关联分类
|
||
if (empty($mp['cid'])) {
|
||
// 如果产品未设置所属分类,则根据分类关联关系设置
|
||
if (empty($category_id)) {
|
||
throw new \Exception('官网未找到关联的分类');
|
||
}
|
||
$record['cid'] = $category_id;
|
||
} else {
|
||
// 如果产品设置了所属分类,分类映射关系未配置,则根据产品分类配置分类关系,否则更新产品分类
|
||
if (empty($category_id)) {
|
||
$query->name('product_tco_category')
|
||
->where('erp_code', '=', $data['category_erp_code'])
|
||
->where('country_code', '=', $language)
|
||
->update(['category_id' => $mp['cid']]);
|
||
} else {
|
||
$record['cid'] = $category_id;
|
||
}
|
||
}
|
||
|
||
// 不更新产品名称
|
||
unset($record['name']);
|
||
|
||
$record['updatetime'] = time();
|
||
$ok = $query->name('product')->where('id', '=', $mp['id'])->update($record);
|
||
if (!$ok) {
|
||
throw new \Exception('同步更新失败');
|
||
}
|
||
}
|
||
}
|
||
} catch (\Throwable $th) {
|
||
return json([
|
||
'code' => 0,
|
||
'msg' => sprintf("Exception %s %s:%d", $th->getMessage(), $th->getFile(), $th->getLine()),
|
||
]);
|
||
}
|
||
|
||
return json([
|
||
'code' => 1,
|
||
'msg' => '成功',
|
||
]);
|
||
}
|
||
}
|