dlh_test_server/app/admin/logic/groupbuy/GroupbuyLogic.php
2024-11-28 16:59:50 +08:00

256 lines
8.2 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\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 app\common\model\groupbuy\GroupbuyConfig;
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();
if($lists){
foreach ($lists['data'] as &$item){
$item['goods_image'] = UrlServer::getFileUrl($item['goods_image']);
}
}
return ['count'=>$lists['total'], 'lists'=>$lists['data']];
}
public static function add($post){
try{
$now = time();
$activity_sn = $now.rand(1000, 9999);
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,
'activity_sn' =>$activity_sn,
'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], // 审核通过
['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();
}
public static function getConfig()
{
$data = GroupbuyConfig::select();
$config = array();
if (!empty($data)) {
$result = $data->toArray();
foreach ($result as &$v) {
$config[$v['name']] = $v['value'];
}
}
return $config;
}
// 设置配置
public static function setConfig($post)
{
try {
Db::startTrans();
$time = time();
$data = [
['name' => 'status', 'value' => (isset($post['status']) && $post['status'] == 'on') ? 1 : 0, 'update_time' => $time],
['name' => 'end_time', 'value' => $post['end_time'], 'update_time' => $time],
['name' => 'rule', 'value' => $post['rule'], 'update_time' => $time],
['name' => 'service', 'value' => str_replace(',', ',', $post['service']), 'update_time' => $time],
];
foreach ($data as $item) {
Db::name('groupbuy_config')->replace()->save($item);
}
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::$error = $e->getMessage();
return false;
}
}
}