dlh_test_server/app/api/logic/LocalStoreGoodsLogic.php

384 lines
13 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\api\logic;
use app\common\basics\Logic;
use app\common\model\localstore\LocalStoreGoods;
use app\common\model\localstore\LocalStoreGoodsPic;
use app\common\model\localstore\LocalStoreGoodsItem;
use app\common\model\localstore\LocalStoreGoodsSpec;
use app\common\model\localstore\LocalStoreGoodsSpecValue;
use app\common\enum\LocalStoreGoodsEnum;
use app\common\server\ConfigServer;
use think\facade\Config;
use think\facade\Db;
class LocalStoreGoodsLogic extends Logic
{
// 88折
public static $sale = 88 / 100;
// 88折商家ID
public static $lstore_id = 138;
// 获取商品列表
public static function lists($get)
{
// 1 已上架 2 已下架
if ($get['type'] == 1 || $get['type'] == 2) {
$where[] = ['state', '=', $get['type']];
// 审核成功
$where[] = ['audit_status', '=', LocalStoreGoodsEnum::AUDIT_STATUS_OK];
}
// 待审核
if ($get['type'] == 3) {
$where[] = ['audit_status', '=', LocalStoreGoodsEnum::AUDIT_STATUS_STAY];
}
// 审核失败
if ($get['type'] == 4) {
$where[] = ['audit_status', '=', LocalStoreGoodsEnum::AUDIT_STATUS_REFUSE];
}
// 搜索商品名称
if ($get['keyword']) {
$where[] = ['title', 'like', '%' . $get['keyword'] . '%'];
}
$order = ['id' => 'desc'];
if (!empty($get['money_sort'])) {
$order = ['price' => $get['money_sort']];
}
$m_where[] = ['lstore_id', '=', $get['lstore_id']];
$m_where[] = ['delete_time', '=', 0];
$list = LocalStoreGoods::with('pic')
->field('id, lstore_id, title, price')
->where($where)
->where($m_where)
->order($order)
->page($get['page_no'], $get['page_size'])
->select()
->toArray();
if ($get['lstore_id'] == Config::get('localstore.dlh_lstore_id')) {
foreach ($list as $k => &$v) {
$list[$k]['original_price'] = $v['price'];
$v['price'] = number_format($v['price'] * self::$sale, 2, '.', '');
}
}
$count = LocalStoreGoods::where($where)->where($m_where)->count();
// 统计待审核的商品数量
$stay_goods_count = LocalStoreGoods::where($m_where)->where(['audit_status' => LocalStoreGoodsEnum::AUDIT_STATUS_STAY])->count();
// 统计审核失败的商品数量
$fail_goods_count = LocalStoreGoods::where($m_where)->where(['audit_status' => LocalStoreGoodsEnum::AUDIT_STATUS_REFUSE])->count();
$result = [
'stay_goods_count' => $stay_goods_count,
'fail_goods_count' => $fail_goods_count,
'count' => $count,
'lists' => $list,
'more' => is_more($count, $get['page_no'], $get['page_size']),
'page_no' => $get['page_no'],
'page_size' => $get['page_size']
];
return $result;
}
// 添加商品
public static function saveGoods($form, $user_id)
{
Db::startTrans();
try {
$len = strlen(Config::get('aliyunoss.domain'));
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'] ?? '',
'is_multiple_spec' => !empty($form['sku']) && !empty($form['spec']) ? 1 : 0,
'create_time' => time(),
]);
// 规格添加
self::addSpec($id, $user_id, $form);
} else if ($form['type'] === 'edit') {
$id = $form['id'];
LocalStoreGoods::where(['id' => $id])->update([
'title' => $form['title'],
'price' => $form['price'],
'desc' => $form['desc'] ?? '',
'details' => $form['details'] ?? '',
'audit_status' => LocalStoreGoodsEnum::AUDIT_STATUS_STAY,
'update_time' => time(),
]);
LocalStoreGoodsPic::where(['lsg_id' => $id])->delete();
$state = LocalStoreGoods::where(['id' => $id])->field('is_multiple_spec')->find();
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);
}
}
// 处理图片
$data = [];
foreach ($form['url'] as $k => &$v) {
$data[$k]['lsg_id'] = $id;
$data[$k]['url'] = substr($v, $len, strlen($v));
}
LocalStoreGoodsPic::insertAll($data);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::$error = $e->getMessage();
return false;
}
}
public static function editPrice($form, $user_id)
{
LocalStoreGoods::where(['id' => $form['id'], 'user_id' => $user_id])->update([
'price' => $form['price'],
'update_time' => time(),
]);
if (!empty($form['sku'])) {
foreach ($form['sku'] as &$v) {
LocalStoreGoodsItem::where(['id' => $v['id']])->update([
'price' => $v['price'],
'stock' => $v['stock'],
'note' => $v['note'],
]);
}
}
return true;
}
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) {
$data[$k]['lsg_id'] = $id;
$data[$k]['url'] = basename($v);
}
LocalStoreGoodsPic::insertAll($data);
return true;
} catch (\Exception $e) {
self::$error = $e->getMessage();
return false;
}
}
// 获取商家自身的商品详情
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();
// 查找多规格
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();
foreach ($data['item'] as &$v) {
$v['image'] = Config::get('aliyunoss.domain') . $v['image'];
}
$data['spec'] = LocalStoreGoodsSpec::with('item')->where([
'goods_id' => $get['id']
])->field('id, name')->select();
foreach ($data['spec'] as &$v1) {
foreach ($v1['item'] as $key => &$v2) {
$v1['item'][$key] = $v2['value'];
}
}
}
if ($data) {
foreach ($data['pic'] as &$v) {
$v['url'] = Config::get('aliyunoss.domain') . $v['url'];
}
return $data;
}
return false;
}
// 获取商品详情
public static function details($get)
{
$data = LocalStoreGoods::with('pic')
->where([
'id' => $get['id'],
'lstore_id' => $get['lstore_id'],
'delete_time' => 0,
'state' => LocalStoreGoodsEnum::SHELVES,
'audit_status' => LocalStoreGoodsEnum::AUDIT_STATUS_OK,
])->find()->toArray();
// 东龙汇超时价格 *88折
if ($get['lstore_id'] == Config::get('localstore.dlh_lstore_id')) {
$data['original_price'] = $data['price'];
$data['price'] = number_format($data['price'] * self::$sale, 2, '.', '');
}
if ($get['lstore_id'] != Config::get('localstore.dlh_lstore_id')) {
$data['close'] = array(
'status' => ConfigServer::get('website', 'show_local_notice'),
'text' => ConfigServer::get('website', 'local_notice')
);
}
// 查找多规格
if ($data['is_multiple_spec'] == 1) {
$data['item'] = LocalStoreGoodsItem::where([
'goods_id' => $get['id']
])->field('id, image, spec_value_str, price, stock, note')->select();
foreach ($data['item'] as &$v) {
$v['image'] = Config::get('aliyunoss.domain') . $v['image'];
// 东龙汇超时价格 *88折
if ($get['lstore_id'] == Config::get('localstore.dlh_lstore_id')) {
$v['original_price'] = $v['price'];
$v['price'] = number_format($v['price'] * self::$sale, 2, '.', '');
}
}
}
// 如果参数中含有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();
}
return $data;
}
// 设置状态
public static function setState($form)
{
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'],
'update_time' => time()
]);
if ($result) {
return $result;
}
return false;
}
return false;
}
public static function delGoods($form)
{
$result = LocalStoreGoods::where([
'id' => $form['id']
])->update(['delete_time' => time()]);
return $result;
}
// 添加规格
private static function addSpec($id, $user_id, $form)
{
$len = strlen(Config::get('aliyunoss.domain'));
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;
$data[$key]['image'] = substr($value['image'], $len, strlen($value['image']));
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);
}
}
}