| 
									
										
										
										
											2023-08-10 14:59:52 +08:00
										 |  |  |  | <?php | 
					
						
							|  |  |  |  | // +----------------------------------------------------------------------
 | 
					
						
							|  |  |  |  | // | likeshop开源商城系统
 | 
					
						
							|  |  |  |  | // +----------------------------------------------------------------------
 | 
					
						
							|  |  |  |  | // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
 | 
					
						
							|  |  |  |  | // | gitee下载:https://gitee.com/likeshop_gitee
 | 
					
						
							|  |  |  |  | // | github下载:https://github.com/likeshop-github
 | 
					
						
							|  |  |  |  | // | 访问官网:https://www.likeshop.cn
 | 
					
						
							|  |  |  |  | // | 访问社区:https://home.likeshop.cn
 | 
					
						
							|  |  |  |  | // | 访问手册:http://doc.likeshop.cn
 | 
					
						
							|  |  |  |  | // | 微信公众号:likeshop技术社区
 | 
					
						
							|  |  |  |  | // | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
 | 
					
						
							|  |  |  |  | // |  likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
 | 
					
						
							|  |  |  |  | // | 禁止对系统程序代码以任何目的,任何形式的再发布
 | 
					
						
							|  |  |  |  | // | likeshop团队版权所有并拥有最终解释权
 | 
					
						
							|  |  |  |  | // +----------------------------------------------------------------------
 | 
					
						
							|  |  |  |  | // | author: likeshop.cn.team
 | 
					
						
							|  |  |  |  | // +----------------------------------------------------------------------
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | namespace app\common\model\goods; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | use app\common\basics\Models; | 
					
						
							|  |  |  |  | use app\common\model\distribution\DistributionGoods; | 
					
						
							| 
									
										
										
										
											2023-10-12 16:37:02 +08:00
										 |  |  |  | use Overtrue\Pinyin\Pinyin; | 
					
						
							| 
									
										
										
										
											2023-10-12 15:45:14 +08:00
										 |  |  |  | use think\facade\Db; | 
					
						
							| 
									
										
										
										
											2023-08-10 14:59:52 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | /** | 
					
						
							|  |  |  |  |  * 商品-模型 | 
					
						
							|  |  |  |  |  * Class Goods | 
					
						
							|  |  |  |  |  * @package app\common\model\goods | 
					
						
							|  |  |  |  |  */ | 
					
						
							|  |  |  |  | class Goods extends Models | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     /** | 
					
						
							|  |  |  |  |      * 商品轮播图 关联模型 | 
					
						
							|  |  |  |  |      */ | 
					
						
							|  |  |  |  |     public function GoodsImage() | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         return $this->hasMany('GoodsImage', 'goods_id', 'id')->field('goods_id, uri'); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  |     /** | 
					
						
							|  |  |  |  |      * 商品SKU 关联模型 | 
					
						
							|  |  |  |  |      */ | 
					
						
							|  |  |  |  |     public function GoodsItem() | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         return $this->hasMany('GoodsItem', 'goods_id', 'id') | 
					
						
							|  |  |  |  |             ->field('id, goods_id, image, spec_value_ids, spec_value_str, market_price, price, stock, chengben_price'); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /** | 
					
						
							|  |  |  |  |      * 店铺 关联模型 | 
					
						
							|  |  |  |  |      */ | 
					
						
							|  |  |  |  |     public function Shop() | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         return $this->hasOne(\app\common\model\shop\Shop::class, 'id', 'shop_id') | 
					
						
							|  |  |  |  |             ->field('id, name, logo, type, star, score, intro,is_pay, mobile'); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     public function getIsDistributionDescAttr($value, $data) | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         return $data['is_distribution'] ? '是': '否'; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /** | 
					
						
							|  |  |  |  |      * 根据商品id获取商品名称 | 
					
						
							|  |  |  |  |      */ | 
					
						
							|  |  |  |  |     public function getGoodsNameById($goods_id) | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         return $this->where('id',$goods_id)->value('name'); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /** | 
					
						
							|  |  |  |  |      * 根据商品id查询商品是否上架 | 
					
						
							|  |  |  |  |      */ | 
					
						
							|  |  |  |  |     public function checkStatusById($goods_id) | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         $status = $this | 
					
						
							|  |  |  |  |             ->where([ | 
					
						
							|  |  |  |  |                 ['id','=',$goods_id], | 
					
						
							|  |  |  |  |                 ['del','=',0], | 
					
						
							|  |  |  |  |             ]) | 
					
						
							|  |  |  |  |             ->value('status'); | 
					
						
							|  |  |  |  |         if ($status){ | 
					
						
							|  |  |  |  |             if ($status == 1){ | 
					
						
							|  |  |  |  |                 return true; | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |             if (empty($status) || $status ===0){ | 
					
						
							|  |  |  |  |                 return  false; | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |         return false; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /** | 
					
						
							|  |  |  |  |      * 根据goods_id查询商品配送方式及所需信息 | 
					
						
							|  |  |  |  |      */ | 
					
						
							|  |  |  |  |     public function getExpressType($goods_id) | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         return $this->where('id',$goods_id)->column('express_type,express_money,express_template_id')[0]; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /** | 
					
						
							|  |  |  |  |      * 最小值与最大值范围 | 
					
						
							|  |  |  |  |      */ | 
					
						
							|  |  |  |  |     public function getMinMaxPriceAttr($value, $data) | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         return '¥ ' . $data['min_price'] . '~ ¥ '. $data['max_price']; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /** | 
					
						
							|  |  |  |  |      * @notes 商品是否参与分销 | 
					
						
							|  |  |  |  |      * @param $value | 
					
						
							|  |  |  |  |      * @return string | 
					
						
							|  |  |  |  |      * @author Tab | 
					
						
							|  |  |  |  |      * @date 2021/9/1 17:29 | 
					
						
							|  |  |  |  |      */ | 
					
						
							|  |  |  |  |     public function getDistributionFlagAttr($value) | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         $data = DistributionGoods::where('goods_id', $value)->findOrEmpty()->toArray(); | 
					
						
							|  |  |  |  |         if (!empty($data) && $data['is_distribution'] == 1) { | 
					
						
							|  |  |  |  |             return true; | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |         return false; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /** | 
					
						
							|  |  |  |  |      * @notes 分销状态搜索器 | 
					
						
							|  |  |  |  |      * @param $query | 
					
						
							|  |  |  |  |      * @param $value | 
					
						
							|  |  |  |  |      * @param $data | 
					
						
							|  |  |  |  |      * @author Tab | 
					
						
							|  |  |  |  |      * @date 2021/9/2 9:55 | 
					
						
							|  |  |  |  |      */ | 
					
						
							|  |  |  |  |     public function searchIsDistributionAttr($query, $value, $data) | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         // 不参与分销
 | 
					
						
							|  |  |  |  |         if (isset($data['is_distribution']) && $data['is_distribution'] == '0') { | 
					
						
							|  |  |  |  |             // 先找出参与分销的商品id
 | 
					
						
							|  |  |  |  |             $ids = DistributionGoods::where('is_distribution', 1)->column('goods_id'); | 
					
						
							|  |  |  |  |             // 在搜索条件中将它们排除掉
 | 
					
						
							|  |  |  |  |             $query->where('id', 'not in', $ids); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |         // 参与分销
 | 
					
						
							|  |  |  |  |         if (isset($data['is_distribution']) && $data['is_distribution'] == '1') { | 
					
						
							|  |  |  |  |             // 先找出参与分销的商品id
 | 
					
						
							|  |  |  |  |             $ids = DistributionGoods::where('is_distribution', 1)->column('goods_id'); | 
					
						
							|  |  |  |  |             // 在搜索条件中使用它们来进行过滤
 | 
					
						
							|  |  |  |  |             $query->where('id', 'in', $ids); | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-10-09 11:13:50 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-12 11:27:45 +08:00
										 |  |  |  |     // 批量增加商品SKU相关  包括 规格名、规格值和sku
 | 
					
						
							|  |  |  |  |     public static function batchHandleSku(): bool | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         // 查询import_status=0的商品记录 如果量大的话使用游标查询
 | 
					
						
							|  |  |  |  |         $needHandleList = self::where('import_status', 0)->column('id,min_price,stock'); | 
					
						
							|  |  |  |  |         $insertSpec = [];//商品默认规格
 | 
					
						
							|  |  |  |  |         $insertItem = [];//商品默认sku
 | 
					
						
							|  |  |  |  |         $goodsIds = []; | 
					
						
							|  |  |  |  |         foreach ($needHandleList as $item) { | 
					
						
							|  |  |  |  |             $goodsIds[] = $item['id']; | 
					
						
							|  |  |  |  |             $insertSpec[] = ['goods_id' => $item['id'], 'name' => '默认']; | 
					
						
							|  |  |  |  |             $insertItem[] = [ | 
					
						
							|  |  |  |  |                 'goods_id' => $item['id'], | 
					
						
							|  |  |  |  |                 'spec_value_ids' => '0',//使用0占位
 | 
					
						
							|  |  |  |  |                 'spec_value_str' => '默认', | 
					
						
							|  |  |  |  |                 'price' => $item['min_price'], | 
					
						
							|  |  |  |  |                 'stock' => $item['stock'], | 
					
						
							| 
									
										
										
										
											2023-10-12 15:45:14 +08:00
										 |  |  |  |                 'volume' => 0, | 
					
						
							|  |  |  |  |                 'weight' => 0, | 
					
						
							|  |  |  |  |                 'bar_code' => 0, | 
					
						
							| 
									
										
										
										
											2023-10-12 11:27:45 +08:00
										 |  |  |  |             ]; | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         if (!empty($insertSpec)) { | 
					
						
							|  |  |  |  |             GoodsSpec::limit(2000)->insertAll($insertSpec); | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         if (!empty($insertItem)) { | 
					
						
							|  |  |  |  |             // 查询上述商品的默认规格名
 | 
					
						
							|  |  |  |  |             $defaultSpecList = GoodsSpec::whereIn('goods_id', $goodsIds)->where('name', '默认')->column('id', 'goods_id');//只需要拿默认的
 | 
					
						
							|  |  |  |  |             foreach ($insertItem as &$i) { | 
					
						
							|  |  |  |  |                 $i['spec_value_ids'] = $defaultSpecList[$i['goods_id']] ?? '0'; | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |             // 商品规格值
 | 
					
						
							|  |  |  |  |             $insertSpecValue = []; | 
					
						
							|  |  |  |  |             foreach ($defaultSpecList as $goodsId => $specId) { | 
					
						
							|  |  |  |  |                 $insertSpecValue[] = [ | 
					
						
							|  |  |  |  |                     'goods_id' => $goodsId, | 
					
						
							|  |  |  |  |                     'spec_id' => $specId, | 
					
						
							|  |  |  |  |                     'value' => '默认', | 
					
						
							|  |  |  |  |                 ]; | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |             GoodsSpecValue::limit(2000)->insertAll($insertSpecValue); | 
					
						
							|  |  |  |  |             GoodsItem::limit(2000)->insertAll($insertItem); | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         // 上述完成后 就默认商品的导入完成
 | 
					
						
							|  |  |  |  |         Goods::whereIn('id', $goodsIds)->save(['import_status' => 1]); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         return true; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 处理组图
 | 
					
						
							|  |  |  |  |     public static function handleImages() | 
					
						
							|  |  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-10-12 16:37:02 +08:00
										 |  |  |  |         // 查询商品表images不为空的记录,批量插入到goods_image表
 | 
					
						
							| 
									
										
										
										
											2023-10-12 11:27:45 +08:00
										 |  |  |  |         $needHandleList = self::whereNotNull('images')->column('id,images'); | 
					
						
							|  |  |  |  |         $insertImage = []; | 
					
						
							|  |  |  |  |         foreach ($needHandleList as $item) { | 
					
						
							|  |  |  |  |             $imageArr = explode(',', $item['images']); | 
					
						
							|  |  |  |  |             foreach ($imageArr as $img) { | 
					
						
							| 
									
										
										
										
											2023-10-12 16:37:02 +08:00
										 |  |  |  |                 if (empty($img)) { | 
					
						
							|  |  |  |  |                     continue; | 
					
						
							|  |  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2023-10-12 11:27:45 +08:00
										 |  |  |  |                 $insertImage[] = [ | 
					
						
							|  |  |  |  |                     'goods_id' => $item['id'], | 
					
						
							|  |  |  |  |                     'uri' => $img, | 
					
						
							|  |  |  |  |                 ]; | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |         GoodsImage::limit(2000)->insertAll($insertImage); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 批量插入没有的品牌
 | 
					
						
							|  |  |  |  |     public static function handleBrand() | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         // 查询商品表brand_name不为空brand_id=0的记录,批量插入到goods_brand表
 | 
					
						
							|  |  |  |  |         $needHandleList = self::whereNotNull('brand_name')->where('brand_id', 0)->group('brand_name')->column('brand_name'); | 
					
						
							|  |  |  |  |         $allBrand = GoodsBrand::getAll(); | 
					
						
							|  |  |  |  |         $allBrandName = array_keys($allBrand); | 
					
						
							|  |  |  |  |         $insert = []; | 
					
						
							|  |  |  |  |         $now = time(); | 
					
						
							|  |  |  |  |         foreach ($needHandleList as $item) { | 
					
						
							| 
									
										
										
										
											2023-10-12 15:45:14 +08:00
										 |  |  |  |             if (empty($item)) { | 
					
						
							|  |  |  |  |                 continue; | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |             if (!in_array($item, $allBrandName)) { | 
					
						
							| 
									
										
										
										
											2023-10-12 16:37:02 +08:00
										 |  |  |  |                 $word = Pinyin::abbr($item)->join(); | 
					
						
							|  |  |  |  |                 $word = substr($word, 0, 1); | 
					
						
							|  |  |  |  |                 $insert[] = ['name' =>$item, 'create_time'=> $now, 'image' => '', 'initial' => strtoupper($word)]; | 
					
						
							| 
									
										
										
										
											2023-10-12 11:27:45 +08:00
										 |  |  |  |             } | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |         GoodsBrand::limit(2000)->insertAll($insert); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 批量插入没有的单位
 | 
					
						
							|  |  |  |  |     public static function handleUnit() | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         // 查询商品表unit_name不为空unit_id=0的记录,批量插入到goods_unit表
 | 
					
						
							|  |  |  |  |         $needHandleList = self::whereNotNull('unit_name')->where('unit_id', 0)->group('unit_name')->column('unit_name'); | 
					
						
							|  |  |  |  |         $all = GoodsUnit::getAll(); | 
					
						
							|  |  |  |  |         $allName = array_keys($all); | 
					
						
							|  |  |  |  |         $insert = []; | 
					
						
							|  |  |  |  |         $now = time(); | 
					
						
							|  |  |  |  |         foreach ($needHandleList as $item) { | 
					
						
							| 
									
										
										
										
											2023-10-12 15:45:14 +08:00
										 |  |  |  |             if (empty($item)) { | 
					
						
							|  |  |  |  |                 continue; | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |             if (!in_array($item, $allName)) { | 
					
						
							|  |  |  |  |                 $insert[] = ['name' =>$item, 'create_time'=> $now]; | 
					
						
							| 
									
										
										
										
											2023-10-12 11:27:45 +08:00
										 |  |  |  |             } | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |         GoodsUnit::limit(2000)->insertAll($insert); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 批量插入没有的商品分类
 | 
					
						
							|  |  |  |  |     public static function handleCategory() | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         // 查询商品表first_cate不为空first_cate_id=0的记录
 | 
					
						
							|  |  |  |  |         $needHandleList1 = self::whereNotNull('first_cate')->where('first_cate_id', 0)->group('first_cate')->column('first_cate'); | 
					
						
							|  |  |  |  |         $needHandleList2 = self::whereNotNull('second_cate')->where('second_cate_id', 0)->group('second_cate')->column('second_cate,first_cate'); | 
					
						
							|  |  |  |  |         $needHandleList3 = self::whereNotNull('third_cate')->where('third_cate_id', 0)->group('third_cate')->column('third_cate,second_cate'); | 
					
						
							|  |  |  |  |         $all = GoodsCategory::getAll(); | 
					
						
							|  |  |  |  |         $allName = array_keys($all); | 
					
						
							|  |  |  |  |         $insert = []; | 
					
						
							|  |  |  |  |         $now = time(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         foreach ($needHandleList1 as $item) { | 
					
						
							| 
									
										
										
										
											2023-10-12 15:45:14 +08:00
										 |  |  |  |             if (empty($item)) { | 
					
						
							|  |  |  |  |                 continue; | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |             if (!in_array($item, $allName)) { | 
					
						
							|  |  |  |  |                 $insert[] = ['name' =>$item, 'pid' => 0, 'level' => 1,'create_time'=> $now, 'parent_name' => null]; | 
					
						
							| 
									
										
										
										
											2023-10-12 11:27:45 +08:00
										 |  |  |  |             } | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         foreach ($needHandleList2 as $item) { | 
					
						
							| 
									
										
										
										
											2023-10-12 15:45:14 +08:00
										 |  |  |  |             if (empty($item)) { | 
					
						
							|  |  |  |  |                 continue; | 
					
						
							|  |  |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-10-12 11:27:45 +08:00
										 |  |  |  |             if (!in_array($item['second_cate'], $allName)) { | 
					
						
							| 
									
										
										
										
											2023-10-12 16:37:02 +08:00
										 |  |  |  |                 $insert[] = ['name' =>$item['second_cate'], 'pid' => 0, 'level' => 2,'create_time'=> $now, 'parent_name' => $item['first_cate']]; | 
					
						
							| 
									
										
										
										
											2023-10-12 11:27:45 +08:00
										 |  |  |  |             } | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         foreach ($needHandleList3 as $item) { | 
					
						
							| 
									
										
										
										
											2023-10-12 15:45:14 +08:00
										 |  |  |  |             if (empty($item)) { | 
					
						
							|  |  |  |  |                 continue; | 
					
						
							|  |  |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-10-12 11:27:45 +08:00
										 |  |  |  |             if (!in_array($item['third_cate'], $allName)) { | 
					
						
							|  |  |  |  |                 $insert[] = ['name' =>$item['third_cate'], 'pid' => 0, 'level' => 3,'create_time'=> $now, 'parent_name' => $item['second_cate']]; | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-12 16:37:02 +08:00
										 |  |  |  |         if (!empty($insert)) { | 
					
						
							|  |  |  |  |             GoodsCategory::limit(2000)->insertAll($insert); | 
					
						
							|  |  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-10-12 11:27:45 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |         // 将level > 1但是pid=0且parent_name不为空的记录批量更新
 | 
					
						
							|  |  |  |  |         GoodsCategory::alias('main') | 
					
						
							|  |  |  |  |             ->join('goods_category parent', 'main.parent_name = parent.name') | 
					
						
							|  |  |  |  |             ->where('main.level', '>', 1) | 
					
						
							|  |  |  |  |             ->where('main.pid', 0) | 
					
						
							|  |  |  |  |             ->whereNotNull('main.parent_name') | 
					
						
							| 
									
										
										
										
											2023-10-12 16:37:02 +08:00
										 |  |  |  |             ->update(['main.pid' => Db::raw('`parent`.id')]); | 
					
						
							| 
									
										
										
										
											2023-10-12 11:27:45 +08:00
										 |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     public static function handleSelfInfo() | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         // 更新一级分类ID 一级分类ID=0但分类名不为空的
 | 
					
						
							|  |  |  |  |         self::alias('main') | 
					
						
							|  |  |  |  |             ->join('goods_category c', 'main.first_cate = c.name') | 
					
						
							|  |  |  |  |             ->where('main.first_cate_id', 0) | 
					
						
							|  |  |  |  |             ->whereNotNull('main.first_cate') | 
					
						
							| 
									
										
										
										
											2023-10-12 15:45:14 +08:00
										 |  |  |  |             ->update(['main.first_cate_id' => Db::raw('`c`.id')]); | 
					
						
							| 
									
										
										
										
											2023-10-12 11:27:45 +08:00
										 |  |  |  |         // 更新二级分类ID 二级分类ID=0但分类名不为空的
 | 
					
						
							|  |  |  |  |         self::alias('main') | 
					
						
							| 
									
										
										
										
											2023-10-12 15:45:14 +08:00
										 |  |  |  |             ->join('goods_category c', 'main.second_cate = c.name') | 
					
						
							| 
									
										
										
										
											2023-10-12 11:27:45 +08:00
										 |  |  |  |             ->where('main.second_cate_id', 0) | 
					
						
							|  |  |  |  |             ->whereNotNull('main.second_cate') | 
					
						
							| 
									
										
										
										
											2023-10-12 15:45:14 +08:00
										 |  |  |  |             ->update(['main.second_cate_id' => Db::raw('`c`.id')]); | 
					
						
							| 
									
										
										
										
											2023-10-12 11:27:45 +08:00
										 |  |  |  |         // 更新三级分类ID 三级分类ID=0但分类名不为空的
 | 
					
						
							|  |  |  |  |         self::alias('main') | 
					
						
							| 
									
										
										
										
											2023-10-12 15:45:14 +08:00
										 |  |  |  |             ->join('goods_category c', 'main.third_cate = c.name') | 
					
						
							| 
									
										
										
										
											2023-10-12 11:27:45 +08:00
										 |  |  |  |             ->where('main.third_cate_id', 0) | 
					
						
							|  |  |  |  |             ->whereNotNull('main.third_cate') | 
					
						
							| 
									
										
										
										
											2023-10-12 15:45:14 +08:00
										 |  |  |  |             ->update(['main.third_cate_id' => Db::raw('`c`.id')]); | 
					
						
							| 
									
										
										
										
											2023-10-12 11:27:45 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |         // 更新品牌ID 品牌ID=0但品牌名不为空的
 | 
					
						
							|  |  |  |  |         self::alias('main') | 
					
						
							| 
									
										
										
										
											2023-10-12 15:45:14 +08:00
										 |  |  |  |             ->join('goods_brand c', 'main.brand_name = c.name') | 
					
						
							| 
									
										
										
										
											2023-10-12 11:27:45 +08:00
										 |  |  |  |             ->where('main.brand_id', 0) | 
					
						
							|  |  |  |  |             ->whereNotNull('main.brand_name') | 
					
						
							| 
									
										
										
										
											2023-10-12 15:45:14 +08:00
										 |  |  |  |             ->update(['main.brand_id' => Db::raw('`c`.id')]); | 
					
						
							| 
									
										
										
										
											2023-10-12 11:27:45 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |         // 更新单位ID 单位ID=0但单位名不为空的
 | 
					
						
							|  |  |  |  |         self::alias('main') | 
					
						
							| 
									
										
										
										
											2023-10-12 15:45:14 +08:00
										 |  |  |  |             ->join('goods_unit c', 'main.unit_name = c.name') | 
					
						
							| 
									
										
										
										
											2023-10-12 11:27:45 +08:00
										 |  |  |  |             ->where('main.unit_id', 0) | 
					
						
							|  |  |  |  |             ->whereNotNull('main.unit_name') | 
					
						
							| 
									
										
										
										
											2023-10-12 15:45:14 +08:00
										 |  |  |  |             ->update(['main.unit_id' => Db::raw('`c`.id')]); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     public static function batchInsert(int $shopId, array $dataAll, array $batchCode) | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         // 已存在的商品编码数组
 | 
					
						
							|  |  |  |  |         $exists = self::whereIn('code', $batchCode)->column('code'); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         $now = time(); | 
					
						
							|  |  |  |  |         $needInsertGoods = []; | 
					
						
							|  |  |  |  |         foreach ($dataAll as $code => $item) { | 
					
						
							|  |  |  |  |             if (!in_array($code, $exists)) { | 
					
						
							|  |  |  |  |                 $needInsertGoods[$code] = [ | 
					
						
							|  |  |  |  |                     'code' => $item['code'], | 
					
						
							|  |  |  |  |                     'name' => $item['name'], | 
					
						
							|  |  |  |  |                     'shop_id' => $shopId, | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-12 16:37:02 +08:00
										 |  |  |  |                     'first_cate' => $item['first_cate'] ?: null, | 
					
						
							|  |  |  |  |                     'second_cate' => $item['second_cate'] ?: null, | 
					
						
							|  |  |  |  |                     'third_cate' => $item['third_cate'] ?: null, | 
					
						
							| 
									
										
										
										
											2023-10-12 15:45:14 +08:00
										 |  |  |  |                     'brand_name' => $item['brand'], | 
					
						
							|  |  |  |  |                     'unit_name' => $item['unit'], | 
					
						
							|  |  |  |  |                     'images' => $item['images'], | 
					
						
							|  |  |  |  |                     'import_status' => 0,//导入状态 0未完成
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |                     'express_money' =>0, | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-12 16:37:02 +08:00
										 |  |  |  |                     'image' => $item['image'] ?: '', | 
					
						
							|  |  |  |  |                     'content' => $item['content'] ?: '', | 
					
						
							| 
									
										
										
										
											2023-10-12 15:45:14 +08:00
										 |  |  |  |                     'spec_type' => 1,//1 统一规格
 | 
					
						
							| 
									
										
										
										
											2023-10-12 16:37:02 +08:00
										 |  |  |  |                     'max_price' => $item['price'] ?: 0, | 
					
						
							|  |  |  |  |                     'min_price' => $item['price'] ?: 0, | 
					
						
							|  |  |  |  |                     'stock' => $item['stock'] ?: 1, | 
					
						
							| 
									
										
										
										
											2023-10-12 15:45:14 +08:00
										 |  |  |  |                     'create_time' => $now, | 
					
						
							|  |  |  |  |                     'update_time' => $now, | 
					
						
							|  |  |  |  |                     'custom_params' => $item['custom_params'], | 
					
						
							|  |  |  |  |                 ]; | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |         // 批量插入商品
 | 
					
						
							|  |  |  |  |         self::insertAll($needInsertGoods); | 
					
						
							| 
									
										
										
										
											2023-10-09 11:13:50 +08:00
										 |  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-08-10 14:59:52 +08:00
										 |  |  |  | } |