更新商品逻辑,添加商品分类更新功能,优化代码格式。
This commit is contained in:
parent
8c137736cd
commit
6ea09b4cb3
@ -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;
|
||||
|
||||
@ -30,10 +31,11 @@ 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).',';
|
||||
}
|
||||
// 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'] ?? '',
|
||||
@ -79,10 +81,7 @@ 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).',';
|
||||
}
|
||||
self::updateGoodsclass($params['class']);
|
||||
Goods::where('id', $params['id'])->update([
|
||||
'name' => $params['name'],
|
||||
'py' => $params['py'] ?? '',
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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()
|
||||
|
@ -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 [];
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user