diff --git a/app/admin/logic/goods/GoodsLogic.php b/app/admin/logic/goods/GoodsLogic.php index 688b40b..2cc88af 100644 --- a/app/admin/logic/goods/GoodsLogic.php +++ b/app/admin/logic/goods/GoodsLogic.php @@ -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); + } + } + } + } + } + } } diff --git a/app/api/controller/IndexController.php b/app/api/controller/IndexController.php index e6ad97e..3f74119 100644 --- a/app/api/controller/IndexController.php +++ b/app/api/controller/IndexController.php @@ -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() diff --git a/app/common/model/goods/Goodsclass.php b/app/common/model/goods/Goodsclass.php index 1cecc7e..70407d7 100644 --- a/app/common/model/goods/Goodsclass.php +++ b/app/common/model/goods/Goodsclass.php @@ -18,5 +18,12 @@ class Goodsclass extends BaseModel protected $name = 'goodsclass'; protected $deleteTime = 'delete_time'; - -} \ No newline at end of file + public function getChildrenAttr($value) + { + if($value){ + return json_decode($value, true); + }else{ + return []; + } + } +}