dlh_test_server/app/admin/logic/groupbuy/GroupbuyLogic.php
2024-09-24 15:52:33 +08:00

211 lines
6.8 KiB
PHP

<?php
namespace app\admin\logic\groupbuy;
use app\common\basics\Logic;
use app\common\enum\GoodsEnum;
use app\common\model\goods\Goods;
use app\common\model\goods\GoodsItem;
use app\common\model\groupbuy\GroupbuyGoods;
use app\common\model\goods\GoodsCategory as GoodsCategory;
use app\common\server\UrlServer;
use think\facade\Db;
class GroupbuyLogic extends Logic
{
public static function goodsList($get){
$where = [];
// 商品名称
if(isset($get['shop_name']) && !($get['shop_name'] == '')) {
$where[] = ['s.name', 'like', '%'.trim($get['shop_name']).'%'];
}
// 商家名称
if(isset($get['goods_name']) && !($get['goods_name'] == '')) {
$where[] = ['g.name', 'like', '%'.trim($get['goods_name']).'%'];
}
$lists = GroupbuyGoods::alias('gu')
->leftJoin('goods g','g.id = gu.goods_id')
->leftJoin('shop s','s.id = g.shop_id')
->field('gu.id,gu.goods_id,g.name as goods_name,g.image as goods_image,s.name as shop_name,s.logo,gu.stock,g.max_price,gu.activity_price,gu.status')
->where($where)
->where('gu.del','1')
->group('gu.goods_id')
->order(['gu.id' => 'desc'])
->paginate([
'page' => $get['page'],
'list_rows' => $get['limit'],
'var_page' => 'page'
])
->toArray();
return ['count'=>$lists['total'], 'lists'=>$lists['data']];
}
public static function add($post){
try{
$now = time();
foreach ($post['item'] as $item){
$add_data[] =[
'goods_id' => $item['goods_id'],
'item_id' => $item['item_id'],
'activity_price' => $item['price'],
'stock'=>$item['stock'],
'del' => 1,
'create_time' => $now,
'update_time' => $now,
];
}
$GroupbuyGoods = new GroupbuyGoods();
$GroupbuyGoods->saveAll($add_data);
return true;
}catch(\Exception $e) {
self::$error = $e->getMessage();
return false;
}
}
public static function edit($post){
Db::startTrans();
try{
foreach ($post['item'] as $goods){
$review_status = GroupbuyGoods::where('id', $goods['id'])->value('status');
$update_data = [
'activity_price' => $goods['activity_price'],
'stock' => $goods['stock'],
];
GroupbuyGoods::where(['id'=>$goods['id']])->update($update_data);
}
Db::commit();
return true;
}catch(\Exception $e) {
self::$error = $e->getMessage();
Db::rollback();
return false;
}
}
public static function getEditGoods($id){
$groupGoods = GroupbuyGoods::alias('gu')
->join('goods_item gi','gu.item_id = gi.id')
->where(['del'=>1,'gu.goods_id'=>$id])
->field('gu.*,gi.image,gi.spec_value_str,gi.price as goods_price')
->select()
->toArray();
$goods_id = $groupGoods[0]['goods_id'];
$goods = Goods::where(['del'=>0,'id'=>$goods_id])->field('image,name')->find();
foreach ($groupGoods as &$item){
$item['name'] = $goods['name'];
if(!$item['image']){
$item['image'] = $goods['image'];
}
}
return $groupGoods;
}
public static function del($id){
try{
$update_data = [
'update_time' => time(),
'del' => 0,
];
GroupbuyGoods::where([
'del'=>1,
'goods_id'=>$id,
])->update($update_data);
return true;
}catch(\Exception $e) {
self::$error = $e->getMessage();
return false;
}
}
public static function switchStatus($post){
try{
$update_data = [
'update_time' => time(),
'status' => $post['is_show'],
];
GroupbuyGoods::where([
'goods_id'=>$post['id'],
])->update($update_data);
return true;
}catch(\Exception $e) {
self::$error = $e->getMessage();
return false;
}
}
//获取商品列表
public static function getGoodsList($get,$is_item = false){
$where = [
['del', '=', GoodsEnum::DEL_NORMAL], // 未删除
['audit_status', '=', GoodsEnum::AUDIT_STATUS_OK], // 审核通过
['status', '=', GoodsEnum::STATUS_SHELVES], // 上架中
['type', '=', GoodsEnum::TYPE_ACTUAL] // 实物商品才参与
];
if (isset($get['keyword']) && $get['keyword']) {
$where[] = ['name', 'like', '%' . $get['keyword'] . '%'];
}
if(!empty($get['cid'])) {
$where[] = ['shop_cate_id', '=', $get['cid']];
}
$goods_count = Goods::where($where)->count();
$goods_list = Goods::where($where)
->page($get['page'], $get['limit'])
->column('*','id');
foreach ($goods_list as &$item) {
$item['goods_item'] = [];
$item['price'] = '¥'.$item['min_price'];
if($item['max_price'] != $item['min_price']){
$item['price'] = '¥'.$item['min_price'].'~'.'¥'.$item['max_price'];
}
$item['create_time_desc'] = date('Y-m-d H:i:s',$item['create_time']);
$item['image'] = UrlServer::getFileUrl($item['image']);
}
if($is_item){
$goods_ids = array_keys($goods_list);
$goods_item = GoodsItem::where(['goods_id'=>$goods_ids])->select()->toArray();
foreach ($goods_item as $items){
if(isset($goods_list[$items['goods_id']])){
if($items['image']){
$items['image'] = UrlServer::getFileUrl($items['image']);
}else{
$items['image'] = $goods_list[$items['goods_id']]['image'];
}
$goods_list[$items['goods_id']]['goods_item'][] = $items;
}
}
}
return ['count' => $goods_count, 'list' =>array_values($goods_list)];
}
/**
* 店铺商品分类
*/
public static function listAll()
{
$lists = GoodsCategory::field('id,name')->where([
'del'=>0,
])->order('id', 'asc')->select();
return empty($lists) ? [] : $lists->toArray();
}
}