dlh_test_server/app/api/logic/LocalStoreGoodsLogic.php

299 lines
10 KiB
PHP
Raw Normal View History

2024-08-22 13:59:35 +08:00
<?php
namespace app\api\logic;
use app\common\basics\Logic;
use app\common\model\localstore\LocalStoreGoods;
use app\common\model\localstore\LocalStoreGoodsPic;
2024-09-11 17:19:51 +08:00
use app\common\model\localstore\LocalStoreGoodsItem;
use app\common\model\localstore\LocalStoreGoodsSpec;
use app\common\model\localstore\LocalStoreGoodsSpecValue;
use app\common\enum\LocalStoreGoodsEnum;
2024-08-22 17:20:33 +08:00
use think\facade\Config;
2024-09-12 11:45:46 +08:00
use think\facade\Db;
2024-08-22 13:59:35 +08:00
class LocalStoreGoodsLogic extends Logic
{
2024-08-23 09:31:23 +08:00
// 获取商品列表
2024-08-22 14:11:04 +08:00
public static function lists($get)
{
2024-08-26 11:11:17 +08:00
// 1 已上架 2 已下架
if ($get['type'] == 1 || $get['type'] == 2) {
$where[] = ['state', '=', $get['type']];
2024-08-27 17:20:07 +08:00
// 审核成功
$where[] = ['audit_status', '=', LocalStoreGoodsEnum::AUDIT_STATUS_OK];
2024-08-26 11:11:17 +08:00
}
// 待审核
if ($get['type'] == 3) {
$where[] = ['audit_status', '=', LocalStoreGoodsEnum::AUDIT_STATUS_STAY];
2024-08-26 11:11:17 +08:00
}
2024-08-27 10:56:00 +08:00
// 审核失败
if ($get['type'] == 4) {
$where[] = ['audit_status', '=', LocalStoreGoodsEnum::AUDIT_STATUS_REFUSE];
2024-08-27 10:56:00 +08:00
}
$m_where[] = ['lstore_id', '=', $get['lstore_id']];
$m_where[] = ['delete_time', '=', 0];
2024-08-26 11:11:17 +08:00
$list = LocalStoreGoods::with('pic')
->field('id, lstore_id, title, price')
2024-08-26 11:11:17 +08:00
->where($where)
2024-08-27 10:56:00 +08:00
->where($m_where)
->order('id', 'desc')
2024-08-22 14:11:04 +08:00
->page($get['page_no'], $get['page_size'])
->select()
->toArray();
2024-08-27 10:56:00 +08:00
$count = LocalStoreGoods::where($where)->where($m_where)->count();
2024-08-22 14:11:04 +08:00
foreach ($list as &$v) {
foreach ($v['pic'] as &$v1) {
2024-08-23 10:44:34 +08:00
$v1['url'] = Config::get('aliyunoss.domain') . $v1['url'];
}
2024-08-22 17:20:33 +08:00
}
2024-08-27 10:57:39 +08:00
// 统计待审核的商品数量
$stay_goods_count = LocalStoreGoods::where($m_where)->where(['audit_status' => LocalStoreGoodsEnum::AUDIT_STATUS_STAY])->count();
2024-08-27 10:57:39 +08:00
2024-08-27 10:56:00 +08:00
// 统计审核失败的商品数量
$fail_goods_count = LocalStoreGoods::where($m_where)->where(['audit_status' => LocalStoreGoodsEnum::AUDIT_STATUS_REFUSE])->count();
2024-08-27 10:56:00 +08:00
2024-08-22 14:11:04 +08:00
$result = [
2024-08-27 10:57:39 +08:00
'stay_goods_count' => $stay_goods_count,
2024-08-27 10:56:00 +08:00
'fail_goods_count' => $fail_goods_count,
2024-08-22 14:11:04 +08:00
'count' => $count,
'lists' => $list,
'more' => is_more($count, $get['page_no'], $get['page_size']),
'page_no' => $get['page_no'],
'page_size' => $get['page_size']
];
2024-08-22 14:11:04 +08:00
return $result;
}
2024-08-23 09:31:23 +08:00
// 添加商品
2024-08-23 10:10:57 +08:00
public static function saveGoods($form, $user_id)
2024-08-22 13:59:35 +08:00
{
2024-09-12 11:45:46 +08:00
Db::startTrans();
2024-08-22 13:59:35 +08:00
try {
2024-08-23 10:33:31 +08:00
$len = strlen(Config::get('aliyunoss.domain'));
2024-08-22 13:59:35 +08:00
2024-08-23 10:33:31 +08:00
if ($form['type'] === 'add') {
$id = LocalStoreGoods::insertGetId([
'user_id' => $user_id,
'lstore_id' => $form['lstore_id'],
'title' => $form['title'],
'price' => $form['price'],
'desc' => $form['desc'] ?? '',
'details' => $form['details'] ?? '',
2024-09-12 11:48:31 +08:00
'is_multiple_spec' => !empty($form['sku']) && !empty($form['spec']) ? 1 : 0,
2024-08-23 10:33:31 +08:00
'create_time' => time(),
]);
2024-09-11 17:19:51 +08:00
2024-09-12 10:06:24 +08:00
if (!empty($form['sku']) && !empty($form['spec'])) {
// 写入规格
2024-09-12 09:49:45 +08:00
$spec = [];
2024-09-12 09:42:33 +08:00
foreach ($form['spec'] as &$v) {
$spec[] = [
'user_id' => $user_id,
'lstore_id' => $form['lstore_id'],
2024-09-11 17:19:51 +08:00
'goods_id' => $id,
'name' => $v['name']
2024-09-12 09:42:33 +08:00
];
2024-09-11 17:19:51 +08:00
}
2024-09-12 09:42:33 +08:00
LocalStoreGoodsSpec::insertAll($spec);
$spec_name_id = LocalStoreGoodsSpec::where(['goods_id' => $id])->column('id', 'name');
2024-09-12 09:49:45 +08:00
2024-09-12 10:06:24 +08:00
// 写入规格值
2024-09-12 09:49:45 +08:00
$spec_value = [];
2024-09-12 10:06:24 +08:00
foreach ($form['spec'] as &$item) {
foreach ($item['item'] as &$v2) {
2024-09-12 09:49:45 +08:00
$spec_value[] = [
'goods_id' => $id,
2024-09-12 10:27:35 +08:00
'spec_id' => $spec_name_id[$item['name']],
2024-09-12 09:49:45 +08:00
'value' => $v2
];
}
}
2024-09-12 10:27:35 +08:00
2024-09-12 09:52:13 +08:00
LocalStoreGoodsSpecValue::insertAll($spec_value);
$spec_value_id = LocalStoreGoodsSpecValue::where(['goods_id' => $id])->column('id', 'value');
2024-09-12 10:06:24 +08:00
// 写入item
$data = [];
foreach ($form['sku'] as $key => &$value) {
2024-09-12 10:17:15 +08:00
$data[$key]['spec_value_ids'] = '';
2024-09-12 10:06:24 +08:00
$data[$key]['user_id'] = $user_id;
$data[$key]['lstore_id'] = $form['lstore_id'];
$data[$key]['goods_id'] = $id;
$data[$key]['image'] = $value['pic'];
foreach ($value['attributes'] as &$value2) {
2024-09-12 10:17:15 +08:00
$data[$key]['spec_value_ids'] .= $spec_value_id[$value2] . ',';
2024-09-12 10:06:24 +08:00
}
2024-09-12 10:17:15 +08:00
$data[$key]['spec_value_ids'] = trim($data[$key]['spec_value_ids'], ',');
$data[$key]['spec_value_str'] = implode(',', $value['attributes']);
$data[$key]['price'] = $value['price'];
$data[$key]['stock'] = $value['stock'];
2024-09-12 10:28:55 +08:00
$data[$key]['note'] = $value['note'];
2024-09-12 10:17:15 +08:00
$data[$key]['create_time'] = time();
2024-09-12 10:06:24 +08:00
}
2024-09-12 10:17:15 +08:00
LocalStoreGoodsItem::insertAll($data);
2024-09-11 17:19:51 +08:00
}
2024-08-23 10:33:31 +08:00
} else if ($form['type'] === 'edit') {
2024-09-05 17:29:06 +08:00
LocalStoreGoods::where(['id' => $form['id'], 'user_id' => $user_id])->update([
2024-08-23 10:33:31 +08:00
'title' => $form['title'],
'price' => $form['price'],
'desc' => $form['desc'] ?? '',
'details' => $form['details'] ?? '',
'audit_status' => LocalStoreGoodsEnum::AUDIT_STATUS_STAY,
2024-08-23 10:33:31 +08:00
'update_time' => time(),
]);
2024-08-23 11:00:04 +08:00
LocalStoreGoodsPic::where(['lsg_id' => $form['id']])->delete();
2024-08-23 11:31:28 +08:00
$id = $form['id'];
2024-08-23 10:10:57 +08:00
}
2024-08-23 10:52:55 +08:00
2024-09-05 17:29:06 +08:00
// 处理图片
$data = [];
foreach ($form['url'] as $k => &$v) {
$data[$k]['lsg_id'] = $id;
$data[$k]['url'] = substr($v, $len, strlen($v));
2024-08-23 11:28:36 +08:00
}
2024-09-05 17:29:06 +08:00
LocalStoreGoodsPic::insertAll($data);
2024-09-05 17:22:44 +08:00
2024-09-12 11:45:46 +08:00
Db::commit();
2024-08-23 10:10:57 +08:00
return true;
} catch (\Exception $e) {
2024-09-12 11:45:46 +08:00
Db::rollback();
2024-08-23 10:10:57 +08:00
self::$error = $e->getMessage();
return false;
}
}
2024-09-05 17:29:06 +08:00
public static function editPrice($form, $user_id)
{
LocalStoreGoods::where(['id' => $form['id'], 'user_id' => $user_id])->update([
'price' => $form['price'],
'update_time' => time(),
]);
return true;
}
2024-08-23 10:10:57 +08:00
public static function editGoods($form, $user_id)
{
try {
$form['user_id'] = $user_id;
$form['update'] = time();
$form['state'] = 1;
$pic = $form['url'];
unset($form['url']);
$id = LocalStoreGoods::update([
'title' => $form['title'],
'price' => $form['price'],
'desc' => $form['desc'],
'details' => $form['details'],
]);
$data = [];
foreach ($pic as $k => &$v) {
2024-08-22 13:59:35 +08:00
$data[$k]['lsg_id'] = $id;
$data[$k]['url'] = basename($v);
}
LocalStoreGoodsPic::insertAll($data);
return true;
} catch (\Exception $e) {
self::$error = $e->getMessage();
return false;
}
}
2024-08-23 09:31:23 +08:00
2024-08-26 11:54:48 +08:00
// 获取商家自身的商品详情
public static function shop_details($get)
{
$data = LocalStoreGoods::with('pic')
->where([
'id' => $get['id'],
'lstore_id' => $get['lstore_id'],
'delete_time' => 0,
'audit_status' => $get['audit_status']
])->find()->toArray();
2024-09-12 14:28:26 +08:00
// 查找多规格
if ($data['is_multiple_spec'] == 1) {
$data['item'] = LocalStoreGoodsItem::where([
'goods_id' => $get['id']
])->field('id, image, spec_value_ids, spec_value_str, price, stock, note')->select();
2024-09-12 17:01:03 +08:00
$data['spec'] = LocalStoreGoodsSpec::with('item')->where([
2024-09-12 14:28:26 +08:00
'goods_id' => $get['id']
])->field('id, name')->select();
2024-09-12 17:01:03 +08:00
// $data['spec_value'] = LocalStoreGoodsSpecValue::where([
// 'goods_id' => $get['id']
// ])->field('id, spec_id, value')->select();
//
2024-09-12 17:19:39 +08:00
foreach ($data['spec'] as &$v1) {
foreach ($v1['item'] as $key => &$v2) {
2024-09-12 17:21:07 +08:00
$v1['attr'][] = $v2;
2024-09-12 17:19:39 +08:00
$v1['item'][$key] = $v2['value'];
}
}
2024-09-12 14:28:26 +08:00
}
2024-08-26 11:54:48 +08:00
if ($data) {
foreach ($data['pic'] as &$v) {
$v['url'] = Config::get('aliyunoss.domain') . $v['url'];
}
return $data;
}
return false;
}
2024-08-23 09:31:23 +08:00
// 获取商品详情
public static function details($get)
{
2024-08-23 14:13:52 +08:00
$data = LocalStoreGoods::with('pic')
2024-08-23 09:31:23 +08:00
->where([
'id' => $get['id'],
'lstore_id' => $get['lstore_id'],
'delete_time' => 0,
'state' => LocalStoreGoodsEnum::SHELVES,
'audit_status' => LocalStoreGoodsEnum::AUDIT_STATUS_OK,
2024-08-23 09:36:21 +08:00
])->find()->toArray();
2024-08-23 10:02:53 +08:00
if ($data) {
foreach ($data['pic'] as &$v) {
2024-08-23 10:43:36 +08:00
$v['url'] = Config::get('aliyunoss.domain') . $v['url'];
2024-08-23 10:02:53 +08:00
}
return $data;
}
return false;
2024-08-23 09:31:23 +08:00
}
2024-08-23 14:13:52 +08:00
// 设置状态
2024-08-23 14:23:28 +08:00
public static function setState($form, $user_id)
2024-08-23 14:13:52 +08:00
{
if ($form['type'] == LocalStoreGoodsEnum::SHELVES || $form['type'] == LocalStoreGoodsEnum::STATUS_SOLD_OUT) {
2024-08-23 14:23:28 +08:00
$result = LocalStoreGoods::where(['id' => $form['id'], 'user_id' => $user_id, 'lstore_id' => $form['lstore_id']])->update([
'state' => $form['type'],
2024-08-28 09:32:01 +08:00
'update_time' => time()
2024-08-23 14:23:28 +08:00
]);
if ($result) {
return $result;
}
return false;
2024-08-23 14:23:28 +08:00
}
return false;
2024-08-23 14:13:52 +08:00
}
2024-08-22 13:59:35 +08:00
}