dlh_test_server/app/api/logic/LocalStoreGoodsLogic.php

177 lines
5.3 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-08-22 17:20:33 +08:00
use app\common\enum\LocalStoreFileEnum;
use think\facade\Config;
2024-08-22 13:59:35 +08:00
class LocalStoreGoodsLogic extends Logic
{
2024-08-23 14:13:52 +08:00
private static $STATUS = [
2024-08-26 11:02:08 +08:00
'stay' => 0, // 默认
'shelves' => 1, // 已上架
'status_sold_out' => 2 // 已下架
];
private static $AUDIT_STATUS = [
'audit_status_stay' => 0, // 待审核
'audit_status_ok' => 1, // 审核通过
'audit_status_refuse' => 2 // 审核失败
2024-08-23 14:13:52 +08:00
];
2024-08-23 10:33:31 +08:00
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-26 11:25:46 +08:00
$where[] = ['audit_status', '=', self::$AUDIT_STATUS['audit_status_ok']];
2024-08-26 11:11:17 +08:00
}
// 待审核
if ($get['type'] == 3) {
$where[] = ['audit_status', '=', self::$AUDIT_STATUS['audit_status_stay']];
}
$where[] = ['lstore_id', '=', $get['lstore_id']];
$where[] = ['lstore_id', '=', 0];
$list = LocalStoreGoods::with('pic')
->field('id, lstore_id, title, price')
2024-08-26 11:11:17 +08:00
->where($where)
->order('id', 'desc')
2024-08-22 14:11:04 +08:00
->page($get['page_no'], $get['page_size'])
->select()
->toArray();
2024-08-26 11:11:17 +08:00
$count = LocalStoreGoods::where($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-22 14:11:04 +08:00
$result = [
'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
{
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-08-23 14:13:52 +08:00
'state' => self::$STATUS['shelves'],
2024-08-23 10:33:31 +08:00
'create_time' => time(),
]);
} else if ($form['type'] === 'edit') {
LocalStoreGoods::where(['id' => $form['id']])->update([
2024-08-23 10:33:31 +08:00
'title' => $form['title'],
'price' => $form['price'],
'desc' => $form['desc'] ?? '',
'details' => $form['details'] ?? '',
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-08-23 11:28:36 +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 10:10:57 +08:00
LocalStoreGoodsPic::insertAll($data);
return true;
} catch (\Exception $e) {
self::$error = $e->getMessage();
return false;
}
}
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
// 获取商品详情
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,
2024-08-26 11:02:08 +08:00
'audit_status' => self::$STATUS['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
{
2024-08-23 14:23:28 +08:00
if (isset(self::$STATUS[$form['type']])) {
$state = self::$STATUS[$form['type']];
$result = LocalStoreGoods::where(['id' => $form['id'], 'user_id' => $user_id, 'lstore_id' => $form['lstore_id']])->update([
'state' => $state,
]);
return $result;
}
return false;
2024-08-23 14:13:52 +08:00
}
2024-08-22 13:59:35 +08:00
}