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

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\model\goods\Goods;
use app\common\logic\BaseLogic; use app\common\logic\BaseLogic;
use app\common\model\goods\Goodsclass;
use app\common\model\goods\GoodsLabel; use app\common\model\goods\GoodsLabel;
use think\facade\Db; use think\facade\Db;
@ -29,11 +30,12 @@ class GoodsLogic extends BaseLogic
{ {
Db::startTrans(); Db::startTrans();
try { try {
$sys_labels=''; $sys_labels = '';
if(isset($params['sys_labels'])){ // if(isset($params['sys_labels'])){
$sys_labels=explode(',',$params['sys_labels']); // $sys_labels=explode(',',$params['sys_labels']);
$sys_labels= ','.implode(',',$sys_labels).','; // $sys_labels= ','.implode(',',$sys_labels).',';
} // }
self::updateGoodsclass($params['class']);
Goods::create([ Goods::create([
'name' => $params['name'], 'name' => $params['name'],
'py' => $params['py'] ?? '', 'py' => $params['py'] ?? '',
@ -54,7 +56,7 @@ class GoodsLogic extends BaseLogic
'data' => $params['data'] ?? '', 'data' => $params['data'] ?? '',
'more' => $params['more'] ?? '', 'more' => $params['more'] ?? '',
'sort' => $params['sort'] ?? '', 'sort' => $params['sort'] ?? '',
'sys_labels' => $sys_labels, 'sys_labels' => $sys_labels,
]); ]);
Db::commit(); Db::commit();
@ -78,32 +80,29 @@ class GoodsLogic extends BaseLogic
{ {
Db::startTrans(); Db::startTrans();
try { try {
$sys_labels=''; $sys_labels = '';
if(isset($params['sys_labels'])){ self::updateGoodsclass($params['class']);
$sys_labels=explode(',',$params['sys_labels']);
$sys_labels= ','.implode(',',$sys_labels).',';
}
Goods::where('id', $params['id'])->update([ Goods::where('id', $params['id'])->update([
'name' => $params['name'], 'name' => $params['name'],
'py' => $params['py'] ?? '', 'py' => $params['py'] ?? '',
'number' => $params['number'], 'number' => $params['number'],
'spec' => $params['spec'], 'spec' => $params['spec'],
'class' => $params['class'], 'class' => $params['class'],
'brand' => $params['brand'] ?? '', 'brand' => $params['brand'] ?? '',
'unit' => $params['unit'], 'unit' => $params['unit'],
'buy' => $params['buy'], 'buy' => $params['buy'],
'sell' => $params['sell'] ?? '', 'sell' => $params['sell'] ?? '',
'retail' => $params['retail'] ?? '', 'retail' => $params['retail'] ?? '',
'code' => $params['code'] ?? '', 'code' => $params['code'] ?? '',
'warehouse' => $params['warehouse'], 'warehouse' => $params['warehouse'],
'location' => $params['location'] ?? '', 'location' => $params['location'] ?? '',
'stocktip' => $params['stocktip'], 'stocktip' => $params['stocktip'],
'imgs' => $params['imgs'], 'imgs' => $params['imgs'],
'details' => $params['details'] ? implode(',', $params['details']) : '', 'details' => $params['details'] ? implode(',', $params['details']) : '',
'data' => $params['data'] ?? '', 'data' => $params['data'] ?? '',
'more' => $params['more'] ?? '', 'more' => $params['more'] ?? '',
'sort' => $params['sort'] ?? '', 'sort' => $params['sort'] ?? '',
'sys_labels' => $sys_labels, 'sys_labels' => $sys_labels,
]); ]);
Db::commit(); Db::commit();
@ -138,13 +137,13 @@ class GoodsLogic extends BaseLogic
*/ */
public static function detail($params): array public static function detail($params): array
{ {
$data = Goods::findOrEmpty($params['id']); $data = Goods::findOrEmpty($params['id']);
if(!empty($data['sys_labels'])){ if (!empty($data['sys_labels'])) {
$goodslabel = GoodsLabel::where('id','in',trim($data['sys_labels'],','))->column('name'); $goodslabel = GoodsLabel::where('id', 'in', trim($data['sys_labels'], ','))->column('name');
$data['sys_labels_text'] = implode(',',$goodslabel); $data['sys_labels_text'] = implode(',', $goodslabel);
}else{ } else {
$data['sys_labels_text'] = ''; $data['sys_labels_text'] = '';
} }
return $data->toArray(); return $data->toArray();
} }
/** /**
@ -158,9 +157,9 @@ class GoodsLogic extends BaseLogic
try { try {
$value = $params['sys_labels']; $value = $params['sys_labels'];
if (!empty($value)) { if (!empty($value)) {
if (!is_array($value)){ if (!is_array($value)) {
$value= ',' . $value . ','; $value = ',' . $value . ',';
}else{ } else {
$value = ',' . implode(',', $value) . ','; $value = ',' . implode(',', $value) . ',';
} }
} }
@ -171,4 +170,27 @@ class GoodsLogic extends BaseLogic
return false; 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\logic\operation\OpurchaseclassLogic;
use app\admin\validate\tools\GenerateTableValidate; use app\admin\validate\tools\GenerateTableValidate;
use app\admin\logic\tools\GeneratorLogic; 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\pay\PayService;
use app\common\service\PushService; use app\common\service\PushService;
use app\common\service\wechat\WeChatMnpService; use app\common\service\wechat\WeChatMnpService;
@ -26,6 +28,28 @@ class IndexController extends BaseApiController
public function index() 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 = new JPush($app_key, $master_secret);
// $client->push() // $client->push()

View File

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