dlh_test_server/app/api/logic/LocalStoreGoodsLogic.php

386 lines
13 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;
2024-08-22 13:59:35 +08:00
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-10-19 11:35:40 +08:00
use app\common\server\ConfigServer;
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
{
// 88折
public static $sale = 88 / 100;
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
}
2024-10-05 16:05:03 +08:00
// 搜索商品名称
if ($get['keyword']) {
$where[] = ['title', 'like', '%' . $get['keyword'] . '%'];
}
2024-10-17 16:07:44 +08:00
$order = ['id' => 'desc'];
if (!empty($get['money_sort'])) {
$order = ['price' => $get['money_sort']];
}
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)
2024-10-17 16:07:44 +08:00
->order($order)
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-13 11:58:13 +08:00
// 规格添加
self::addSpec($id, $user_id, $form);
2024-08-23 10:33:31 +08:00
} else if ($form['type'] === 'edit') {
2024-09-13 11:58:13 +08:00
$id = $form['id'];
2024-10-07 10:07:12 +08:00
LocalStoreGoods::where(['id' => $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-09-13 11:58:13 +08:00
LocalStoreGoodsPic::where(['lsg_id' => $id])->delete();
2024-10-07 10:07:12 +08:00
$state = LocalStoreGoods::where(['id' => $id])->field('is_multiple_spec')->find();
2024-09-13 11:58:13 +08:00
if ($state['is_multiple_spec'] == 1) {
// 删除多规格
LocalStoreGoodsSpec::where(['goods_id' => $id])->delete();
LocalStoreGoodsSpecValue::where(['goods_id' => $id])->delete();
LocalStoreGoodsItem::where(['goods_id' => $id])->delete();
// 规格添加
self::addSpec($id, $user_id, $form);
}
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(),
]);
2024-09-13 14:44:57 +08:00
if (!empty($form['sku'])) {
foreach ($form['sku'] as &$v) {
LocalStoreGoodsItem::where(['id' => $v['id']])->update([
'price' => $v['price'],
'stock' => $v['stock'],
'note' => $v['note'],
2024-09-13 14:44:57 +08:00
]);
}
}
2024-09-05 17:29:06 +08:00
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-13 14:06:06 +08:00
foreach ($data['item'] as &$v) {
$v['image'] = Config::get('aliyunoss.domain') . $v['image'];
}
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-13 11:58:13 +08:00
2024-09-12 17:19:39 +08:00
foreach ($data['spec'] as &$v1) {
foreach ($v1['item'] as $key => &$v2) {
$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
// 东龙汇超时价格 *88折
if ($get['lstore_id'] == 138) {
$data['price'] = round($data['price'] * self::$sale, 2);
}
if ($get['lstore_id'] != 138) {
2024-10-19 11:35:40 +08:00
$data['close'] = array(
2024-10-19 11:37:03 +08:00
'status' => ConfigServer::get('website', 'show_local_notice'),
'text' => ConfigServer::get('website', 'local_notice')
2024-10-19 11:35:40 +08:00
);
}
2024-09-13 16:34:41 +08:00
// 查找多规格
if ($data['is_multiple_spec'] == 1) {
$data['item'] = LocalStoreGoodsItem::where([
'goods_id' => $get['id']
])->field('id, image, spec_value_str, price, stock, note')->select();
2024-09-13 16:34:41 +08:00
foreach ($data['item'] as &$v) {
$v['image'] = Config::get('aliyunoss.domain') . $v['image'];
// 东龙汇超时价格 *88折
if ($get['lstore_id'] == 138) {
$v['price'] = round($v['price'] * self::$sale, 2);
}
2024-09-13 16:34:41 +08:00
}
}
// 如果参数中含有spec_item_id参数,就把规格中的金额拿到
if (!empty($get['spec_item_id']) && $get['spec_item_id'] > 0) {
$data['spec'] = LocalStoreGoodsItem::where(['id' => $get['spec_item_id']])->field('price, spec_value_str')->find();
}
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
// 设置状态
public static function setState($form)
2024-08-23 14:13:52 +08:00
{
if ($form['type'] == LocalStoreGoodsEnum::SHELVES || $form['type'] == LocalStoreGoodsEnum::STATUS_SOLD_OUT) {
$result = LocalStoreGoods::where(['id' => $form['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-09-13 11:58:13 +08:00
public static function delGoods($form)
2024-09-15 09:33:53 +08:00
{
$result = LocalStoreGoods::where([
'id' => $form['id']
2024-09-15 09:33:53 +08:00
])->update(['delete_time' => time()]);
return $result;
}
// 添加规格
2024-09-13 11:58:13 +08:00
private static function addSpec($id, $user_id, $form)
{
2024-09-13 14:03:31 +08:00
$len = strlen(Config::get('aliyunoss.domain'));
2024-09-13 11:58:13 +08:00
if (!empty($form['sku']) && !empty($form['spec'])) {
// 写入规格
$spec = [];
foreach ($form['spec'] as &$v) {
$spec[] = [
'user_id' => $user_id,
'lstore_id' => $form['lstore_id'],
'goods_id' => $id,
'name' => $v['name']
];
}
LocalStoreGoodsSpec::insertAll($spec);
$spec_name_id = LocalStoreGoodsSpec::where(['goods_id' => $id])->column('id', 'name');
// 写入规格值
$spec_value = [];
foreach ($form['spec'] as &$item) {
foreach ($item['item'] as &$v2) {
$spec_value[] = [
'goods_id' => $id,
'spec_id' => $spec_name_id[$item['name']],
'value' => $v2
];
}
}
LocalStoreGoodsSpecValue::insertAll($spec_value);
$spec_value_id = LocalStoreGoodsSpecValue::where(['goods_id' => $id])->column('id', 'value');
// 写入item
$data = [];
foreach ($form['sku'] as $key => &$value) {
$data[$key]['spec_value_ids'] = '';
$data[$key]['user_id'] = $user_id;
$data[$key]['lstore_id'] = $form['lstore_id'];
$data[$key]['goods_id'] = $id;
2024-09-13 14:03:31 +08:00
$data[$key]['image'] = substr($value['image'], $len, strlen($value['image']));
2024-09-13 11:58:13 +08:00
foreach ($value['attributes'] as &$value2) {
$data[$key]['spec_value_ids'] .= $spec_value_id[$value2] . ',';
}
$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'];
$data[$key]['note'] = $value['note'];
$data[$key]['create_time'] = time();
}
LocalStoreGoodsItem::insertAll($data);
}
}
2024-08-22 13:59:35 +08:00
}