更新商品逻辑,添加商品分类更新功能,优化代码格式。

This commit is contained in:
mkm 2024-05-24 16:06:27 +08:00
parent 8c137736cd
commit 6ea09b4cb3
3 changed files with 96 additions and 43 deletions

View File

@ -5,6 +5,7 @@ namespace app\admin\logic\goods;
use app\common\model\goods\Goods;
use app\common\logic\BaseLogic;
use app\common\model\goods\Goodsclass;
use app\common\model\goods\GoodsLabel;
use think\facade\Db;
@ -29,11 +30,12 @@ class GoodsLogic extends BaseLogic
{
Db::startTrans();
try {
$sys_labels='';
if(isset($params['sys_labels'])){
$sys_labels=explode(',',$params['sys_labels']);
$sys_labels= ','.implode(',',$sys_labels).',';
}
$sys_labels = '';
// if(isset($params['sys_labels'])){
// $sys_labels=explode(',',$params['sys_labels']);
// $sys_labels= ','.implode(',',$sys_labels).',';
// }
self::updateGoodsclass($params['class']);
Goods::create([
'name' => $params['name'],
'py' => $params['py'] ?? '',
@ -54,7 +56,7 @@ class GoodsLogic extends BaseLogic
'data' => $params['data'] ?? '',
'more' => $params['more'] ?? '',
'sort' => $params['sort'] ?? '',
'sys_labels' => $sys_labels,
'sys_labels' => $sys_labels,
]);
Db::commit();
@ -78,32 +80,29 @@ class GoodsLogic extends BaseLogic
{
Db::startTrans();
try {
$sys_labels='';
if(isset($params['sys_labels'])){
$sys_labels=explode(',',$params['sys_labels']);
$sys_labels= ','.implode(',',$sys_labels).',';
}
$sys_labels = '';
self::updateGoodsclass($params['class']);
Goods::where('id', $params['id'])->update([
'name' => $params['name'],
'py' => $params['py'] ?? '',
'number' => $params['number'],
'spec' => $params['spec'],
'class' => $params['class'],
'brand' => $params['brand'] ?? '',
'unit' => $params['unit'],
'buy' => $params['buy'],
'sell' => $params['sell'] ?? '',
'retail' => $params['retail'] ?? '',
'code' => $params['code'] ?? '',
'warehouse' => $params['warehouse'],
'location' => $params['location'] ?? '',
'stocktip' => $params['stocktip'],
'imgs' => $params['imgs'],
'details' => $params['details'] ? implode(',', $params['details']) : '',
'data' => $params['data'] ?? '',
'more' => $params['more'] ?? '',
'sort' => $params['sort'] ?? '',
'sys_labels' => $sys_labels,
'name' => $params['name'],
'py' => $params['py'] ?? '',
'number' => $params['number'],
'spec' => $params['spec'],
'class' => $params['class'],
'brand' => $params['brand'] ?? '',
'unit' => $params['unit'],
'buy' => $params['buy'],
'sell' => $params['sell'] ?? '',
'retail' => $params['retail'] ?? '',
'code' => $params['code'] ?? '',
'warehouse' => $params['warehouse'],
'location' => $params['location'] ?? '',
'stocktip' => $params['stocktip'],
'imgs' => $params['imgs'],
'details' => $params['details'] ? implode(',', $params['details']) : '',
'data' => $params['data'] ?? '',
'more' => $params['more'] ?? '',
'sort' => $params['sort'] ?? '',
'sys_labels' => $sys_labels,
]);
Db::commit();
@ -138,13 +137,13 @@ class GoodsLogic extends BaseLogic
*/
public static function detail($params): array
{
$data = Goods::findOrEmpty($params['id']);
if(!empty($data['sys_labels'])){
$goodslabel = GoodsLabel::where('id','in',trim($data['sys_labels'],','))->column('name');
$data['sys_labels_text'] = implode(',',$goodslabel);
}else{
$data['sys_labels_text'] = '';
}
$data = Goods::findOrEmpty($params['id']);
if (!empty($data['sys_labels'])) {
$goodslabel = GoodsLabel::where('id', 'in', trim($data['sys_labels'], ','))->column('name');
$data['sys_labels_text'] = implode(',', $goodslabel);
} else {
$data['sys_labels_text'] = '';
}
return $data->toArray();
}
/**
@ -158,9 +157,9 @@ class GoodsLogic extends BaseLogic
try {
$value = $params['sys_labels'];
if (!empty($value)) {
if (!is_array($value)){
$value= ',' . $value . ',';
}else{
if (!is_array($value)) {
$value = ',' . $value . ',';
} else {
$value = ',' . implode(',', $value) . ',';
}
}
@ -171,4 +170,27 @@ class GoodsLogic extends BaseLogic
return false;
}
}
/**
* 更新商品分类
*/
public static function updateGoodsclass($id,$type=0)
{
$pid = Goodsclass::where('id', $id)->value('pid');
if ($pid) {
$goodsclass = Goodsclass::where('id', $pid)->field('pid,children')->find();
if ($goodsclass) {
if (count($goodsclass['children']) >= 1) {
if (!in_array($id, $goodsclass['children'])) {
$arr = $goodsclass['children'];
array_push($arr, $id);
Goodsclass::where('id', $pid)->update(['children' => $arr]);
if ($goodsclass['pid'] != 0 &&$type==0) {
self::updateGoodsclass($pid,1);
}
}
}
}
}
}
}

View File

@ -5,6 +5,8 @@ namespace app\api\controller;
use app\admin\logic\operation\OpurchaseclassLogic;
use app\admin\validate\tools\GenerateTableValidate;
use app\admin\logic\tools\GeneratorLogic;
use app\common\model\goods\Goods;
use app\common\model\goods\Goodsclass;
use app\common\service\pay\PayService;
use app\common\service\PushService;
use app\common\service\wechat\WeChatMnpService;
@ -26,6 +28,28 @@ class IndexController extends BaseApiController
public function index()
{
// $select=(new Goods())->column('class');
// $a=array_unique($select);
// $arr=[];
// foreach($a as $k=>$v){
// $pid=Goodsclass::where('id',$v)->value('pid');
// $arr[$pid][]=$v;
// }
// foreach($arr as $k=>$v){
// Goodsclass::where('id',$k)->update(['children'=>json_encode($v,true)]);
// }
// $a=Goodsclass::where('pid','>',0)->where('children','<>',null)->column('id');
// $arr = [];
// foreach ($a as $k => $v) {
// $pid = Goodsclass::where('id', $v)->value('pid');
// $arr[$pid][] = $v;
// }
// foreach ($arr as $k => $v) {
// Goodsclass::where('id', $k)->update(['children' => json_encode($v, true)]);
// }
d(1);
// $a=array_values($a);
// $client = new JPush($app_key, $master_secret);
// $client->push()

View File

@ -18,5 +18,12 @@ class Goodsclass extends BaseModel
protected $name = 'goodsclass';
protected $deleteTime = 'delete_time';
}
public function getChildrenAttr($value)
{
if($value){
return json_decode($value, true);
}else{
return [];
}
}
}