glhcp/server/app/common/model/goods/Goods.php

398 lines
14 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?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;
use think\facade\Db;
/**
* 商品-模型
* 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);
}
}
// 批量增加商品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'],
'volume' => 0,
'weight' => 0,
'bar_code' => 0,
];
}
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()
{
// 查询商品表images不为空的记录批量插入到goods_image表再将这些images字段置空
$needHandleList = self::whereNotNull('images')->column('id,images');
$goodsIds = [];
$insertImage = [];
foreach ($needHandleList as $item) {
$goodsIds[] = $item['id'];
$imageArr = explode(',', $item['images']);
foreach ($imageArr as $img) {
$insertImage[] = [
'goods_id' => $item['id'],
'uri' => $img,
];
}
}
GoodsImage::limit(2000)->insertAll($insertImage);
self::whereIn('id', $goodsIds)->save([
'images' => null
]);
}
// 批量插入没有的品牌
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) {
if (empty($item)) {
continue;
}
if (!in_array($item, $allBrandName)) {
$insert[] = ['name' =>$item, 'create_time'=> $now, 'image' => '', 'initial' => ''];
}
}
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) {
if (empty($item)) {
continue;
}
if (!in_array($item, $allName)) {
$insert[] = ['name' =>$item, 'create_time'=> $now];
}
}
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) {
if (empty($item)) {
continue;
}
if (!in_array($item, $allName)) {
$insert[] = ['name' =>$item, 'pid' => 0, 'level' => 1,'create_time'=> $now, 'parent_name' => null];
}
}
foreach ($needHandleList2 as $item) {
if (empty($item)) {
continue;
}
if (!in_array($item['second_cate'], $allName)) {
$insert[] = ['name' =>$item['first_cate'], 'pid' => 0, 'level' => 2,'create_time'=> $now, 'parent_name' => $item['first_cate']];
}
}
foreach ($needHandleList3 as $item) {
if (empty($item)) {
continue;
}
if (!in_array($item['third_cate'], $allName)) {
$insert[] = ['name' =>$item['third_cate'], 'pid' => 0, 'level' => 3,'create_time'=> $now, 'parent_name' => $item['second_cate']];
}
}
GoodsCategory::limit(2000)->insertAll($insert);
// 将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')
->update(['main.pid' => 'parent.id']);
}
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')
->update(['main.first_cate_id' => Db::raw('`c`.id')]);
// 更新二级分类ID 二级分类ID=0但分类名不为空的
self::alias('main')
->join('goods_category c', 'main.second_cate = c.name')
->where('main.second_cate_id', 0)
->whereNotNull('main.second_cate')
->update(['main.second_cate_id' => Db::raw('`c`.id')]);
// 更新三级分类ID 三级分类ID=0但分类名不为空的
self::alias('main')
->join('goods_category c', 'main.third_cate = c.name')
->where('main.third_cate_id', 0)
->whereNotNull('main.third_cate')
->update(['main.third_cate_id' => Db::raw('`c`.id')]);
// 更新品牌ID 品牌ID=0但品牌名不为空的
self::alias('main')
->join('goods_brand c', 'main.brand_name = c.name')
->where('main.brand_id', 0)
->whereNotNull('main.brand_name')
->update(['main.brand_id' => Db::raw('`c`.id')]);
// 更新单位ID 单位ID=0但单位名不为空的
self::alias('main')
->join('goods_unit c', 'main.unit_name = c.name')
->where('main.unit_id', 0)
->whereNotNull('main.unit_name')
->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,
'first_cate' => $item['first_cate'],
'second_cate' => $item['second_cate'],
'third_cate' => $item['third_cate'],
'brand_name' => $item['brand'],
'unit_name' => $item['unit'],
'images' => $item['images'],
'import_status' => 0,//导入状态 0未完成
'express_money' =>0,
'image' => $item['image'],
'content' => $item['content'],
'spec_type' => 1,//1 统一规格
'max_price' => $item['price'],
'min_price' => $item['price'],
'stock' => $item['stock'],
'create_time' => $now,
'update_time' => $now,
'custom_params' => $item['custom_params'],
];
}
}
// 批量插入商品
self::insertAll($needInsertGoods);
}
}