glhcp/server/app/shop/logic/free_shipping/FreeShippingLogic.php

133 lines
4.7 KiB
PHP
Raw Normal View History

2023-08-10 06:59:52 +00:00
<?php
// +----------------------------------------------------------------------
// | likeshop开源商城系统
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | gitee下载https://gitee.com/likeshop_gitee
// | github下载https://github.com/likeshop-github
// | 访问官网https://www.likeshop.cn
// | 访问社区https://home.likeshop.cn
// | 访问手册http://doc.likeshop.cn
// | 微信公众号likeshop技术社区
// | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用未经许可不能去除前后端官方版权标识
// | likeshop系列产品收费版本务必购买商业授权购买去版权授权后方可去除前后端官方版权标识
// | 禁止对系统程序代码以任何目的,任何形式的再发布
// | likeshop团队版权所有并拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeshop.cn.team
// +----------------------------------------------------------------------
namespace app\shop\logic\free_shipping;
use app\common\basics\Logic;
use app\common\server\ConfigServer;
use think\facade\Db;
class FreeShippingLogic extends Logic
{
public static function index($params)
{
Db::startTrans();
try {
// 保存设置
$config = Db::name('free_shipping_config')->where([
'shop_id' => $params['shop_id'],
'del' => 0
])->findOrEmpty();
if (empty($config)) {
// 无则添加
Db::name('free_shipping_config')->save([
'shop_id' => $params['shop_id'],
'status' => $params['status'],
'goods_type' => $params['goods_type'],
'free_rule' => $params['free_rule'],
'create_time' => time(),
'del' => 0,
]);
} else {
// 有则更新
Db::name('free_shipping_config')->where([
'shop_id' => $params['shop_id'],
'del' => 0
])->update([
'status' => $params['status'],
'goods_type' => $params['goods_type'],
'free_rule' => $params['free_rule'],
'update_time' => time()
]);
}
// 先清除旧数据
Db::name('free_shipping_region')->where([
'shop_id' => $params['shop_id'],
'del' => 0
])->update(['del' => 1]);
// 保存活动区域
$data = [];
$time = time();
$del = 0;
foreach($params['region'] as $key => $value) {
if($params['order_amount'][$key] < 0) {
throw new \Exception('订单金额不能小于0');
}
$data[] = ['shop_id' => $params['shop_id'], 'region' => $value, 'order_amount' => $params['order_amount'][$key], 'create_time' => $time, 'del' => $del];
}
Db::name('free_shipping_region')->insertAll($data);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::$error = $e->getMessage();
return false;
}
}
public static function getData($shopId)
{
$config = Db::name('free_shipping_config')
->field('status,goods_type,free_rule')
->where([
'shop_id' => $shopId,
'del' => 0,
])->findOrEmpty();
if(empty($config)) {
// 默认值
$config = [
'status' => 0,
'goods_type' => 1,
'free_rule' => 1,
];
}
$regionArr = Db::name('free_shipping_region')->field('region,order_amount')
->where([
'shop_id' => $shopId,
'del' => 0,
])->select()->toArray();
$regions = Db::name('dev_region')->column('name', 'id');
foreach ($regionArr as &$item) {
$item['region_name'] = '';
if ($item['region'] == 'all'){
$item['region_name'] = '全国地区默认规则';
continue;
}
$region = explode(',', $item['region']);
foreach ($region as $v) {
if (isset($regions[$v])) {
$item['region_name'] .= $regions[$v] . ',';
}
}
$item['region_name'] = rtrim($item['region_name'], ',');
}
return [
'config' => $config,
'region' => $regionArr,
];
}
}