new files
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: CRMEB Team <admin@crmeb.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace app\services\product\shipping;
|
||||
|
||||
|
||||
use app\dao\product\shipping\ShippingTemplatesFreeCityDao;
|
||||
use app\services\BaseServices;
|
||||
use think\annotation\Inject;
|
||||
|
||||
/**
|
||||
* 包邮和城市数据连表业务处理层
|
||||
* Class ShippingTemplatesFreeCityServices
|
||||
* @package app\services\product\shipping
|
||||
* @mixin ShippingTemplatesFreeCityDao
|
||||
*/
|
||||
class ShippingTemplatesFreeCityServices extends BaseServices
|
||||
{
|
||||
/**
|
||||
* @var ShippingTemplatesFreeCityDao
|
||||
*/
|
||||
#[Inject]
|
||||
protected ShippingTemplatesFreeCityDao $dao;
|
||||
}
|
||||
@@ -0,0 +1,217 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: CRMEB Team <admin@crmeb.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace app\services\product\shipping;
|
||||
|
||||
|
||||
use app\dao\product\shipping\ShippingTemplatesFreeDao;
|
||||
use app\services\BaseServices;
|
||||
use crmeb\exceptions\AdminException;
|
||||
use crmeb\services\CacheService;
|
||||
use crmeb\utils\Arr;
|
||||
use think\annotation\Inject;
|
||||
|
||||
/**
|
||||
* 包邮
|
||||
* Class ShippingTemplatesFreeServices
|
||||
* @package app\services\product\shipping
|
||||
* @mixin ShippingTemplatesFreeDao
|
||||
*/
|
||||
class ShippingTemplatesFreeServices extends BaseServices
|
||||
{
|
||||
/**
|
||||
* @var ShippingTemplatesFreeDao
|
||||
*/
|
||||
#[Inject]
|
||||
protected ShippingTemplatesFreeDao $dao;
|
||||
|
||||
/**
|
||||
* @param array $tempIds
|
||||
* @param array $cityIds
|
||||
* @param int $expire
|
||||
* @return bool|mixed|null
|
||||
* @throws \Throwable
|
||||
*/
|
||||
public function isFreeListCache(array $tempIds, array $cityIds, int $expire = 60)
|
||||
{
|
||||
return CacheService::redisHandler('apiShipping')->remember(md5('isFreeList' . json_encode([$tempIds, $cityIds])), function () use ($tempIds, $cityIds) {
|
||||
return $this->dao->isFreeList($tempIds, $cityIds, 0, 'temp_id,number,price', 'temp_id');
|
||||
}, $expire);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加包邮信息
|
||||
* @param array $appointInfo
|
||||
* @param int $group
|
||||
* @param int $tempId
|
||||
* @return bool
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function saveFreeV1(array $appointInfo, int $group = 0, int $tempId = 0)
|
||||
{
|
||||
$res = true;
|
||||
if ($tempId) {
|
||||
if ($this->dao->count(['temp_id' => $tempId])) {
|
||||
$res = $this->dao->delete($tempId, 'temp_id');
|
||||
}
|
||||
}
|
||||
$placeList = [];
|
||||
mt_srand();
|
||||
foreach ($appointInfo as $item) {
|
||||
$uniqid = uniqid('adminapi') . rand(1000, 9999);
|
||||
foreach ($item['city_ids'] as $cityId) {
|
||||
$placeList [] = [
|
||||
'temp_id' => $tempId,
|
||||
'city_id' => $cityId[count($cityId) - 1],
|
||||
'value' => json_encode($cityId),
|
||||
'number' => $item['number'] ?? 0,
|
||||
'price' => $item['price'] ?? 0,
|
||||
'group' => $group,
|
||||
'uniqid' => $uniqid,
|
||||
];
|
||||
}
|
||||
}
|
||||
if (count($placeList)) {
|
||||
return $res && $this->dao->saveAll($placeList);
|
||||
} else {
|
||||
return $res;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加包邮信息
|
||||
* @param array $appointInfo
|
||||
* @param int $group
|
||||
* @param int $tempId
|
||||
* @return bool
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function saveFree(array $appointInfo, int $group = 0, int $tempId = 0)
|
||||
{
|
||||
$res = true;
|
||||
if ($tempId) {
|
||||
if ($this->dao->count(['temp_id' => $tempId])) {
|
||||
$res = $this->dao->delete($tempId, 'temp_id');
|
||||
}
|
||||
}
|
||||
$placeList = [];
|
||||
mt_srand();
|
||||
foreach ($appointInfo as $item) {
|
||||
if (isset($item['place']) && is_array($item['place'])) {
|
||||
$uniqid = uniqid('adminapi') . rand(1000, 9999);
|
||||
foreach ($item['place'] as $value) {
|
||||
if (isset($value['children']) && is_array($value['children'])) {
|
||||
foreach ($value['children'] as $vv) {
|
||||
if (!isset($vv['city_id'])) {
|
||||
throw new AdminException('缺少城市id无法保存');
|
||||
}
|
||||
$placeList [] = [
|
||||
'temp_id' => $tempId,
|
||||
'province_id' => $value['city_id'] ?? 0,
|
||||
'city_id' => $vv['city_id'] ?? 0,
|
||||
'number' => $item['a_num'] ?? 0,
|
||||
'price' => $item['a_price'] ?? 0,
|
||||
'group' => $group,
|
||||
'uniqid' => $uniqid,
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (count($placeList)) {
|
||||
return $res && $this->dao->saveAll($placeList);
|
||||
} else {
|
||||
return $res;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $tempId
|
||||
* @return array
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function getFreeListV1(int $tempId)
|
||||
{
|
||||
$freeList = $this->dao->getShippingList(['temp_id' => $tempId]);
|
||||
return Arr::formatShipping($freeList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得指定包邮城市地址
|
||||
* @param int $tempId
|
||||
* @return array
|
||||
*/
|
||||
public function getFreeList(int $tempId)
|
||||
{
|
||||
$freeIdList = $this->dao->getShippingGroupArray(['temp_id' => $tempId], 'uniqid', 'uniqid', '');
|
||||
$freeData = [];
|
||||
$infos = $this->dao->getShippingArray(['uniqid' => $freeIdList, 'temp_id' => $tempId], '*', 'uniqid');
|
||||
foreach ($freeIdList as $uniqid) {
|
||||
$info = $infos[$uniqid];
|
||||
$freeData[] = [
|
||||
'place' => $this->getFreeTemp($uniqid, $info['province_id']),
|
||||
'a_num' => $info['number'] ? floatval($info['number']) : 0,
|
||||
'a_price' => $info['price'] ? floatval($info['price']) : 0,
|
||||
];
|
||||
}
|
||||
foreach ($freeData as &$item) {
|
||||
$item['placeName'] = implode(';', array_column($item['place'], 'name'));
|
||||
}
|
||||
return $freeData;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取包邮的省份
|
||||
* @param string $uniqid
|
||||
* @param int $provinceId
|
||||
* @return array
|
||||
*/
|
||||
public function getFreeTemp(string $uniqid, int $provinceId)
|
||||
{
|
||||
/** @var ShippingTemplatesFreeCityServices $service */
|
||||
$service = app()->make(ShippingTemplatesFreeCityServices::class);
|
||||
$infoList = $service->getUniqidList(['uniqid' => $uniqid]);
|
||||
$childrenData = [];
|
||||
foreach ($infoList as $item) {
|
||||
$childrenData[] = [
|
||||
'city_id' => $item['province_id'],
|
||||
'name' => $item['name'] ?? '全国',
|
||||
'children' => $this->getCityTemp($uniqid, $provinceId)
|
||||
];
|
||||
}
|
||||
return $childrenData;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取市区数据
|
||||
* @param string $uniqid
|
||||
* @param int $provinceId
|
||||
* @return array
|
||||
*/
|
||||
public function getCityTemp(string $uniqid, int $provinceId)
|
||||
{
|
||||
/** @var ShippingTemplatesFreeCityServices $service */
|
||||
$service = app()->make(ShippingTemplatesFreeCityServices::class);
|
||||
$infoList = $service->getUniqidList(['uniqid' => $uniqid, 'province_id' => $provinceId], false);
|
||||
$childrenData = [];
|
||||
foreach ($infoList as $item) {
|
||||
$childrenData[] = [
|
||||
'city_id' => $item['city_id'],
|
||||
'name' => $item['name'] ?? '全国',
|
||||
];
|
||||
}
|
||||
return $childrenData;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: CRMEB Team <admin@crmeb.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace app\services\product\shipping;
|
||||
|
||||
|
||||
use app\dao\product\shipping\ShippingTemplatesNoDeliveryCityDao;
|
||||
use app\services\BaseServices;
|
||||
use think\annotation\Inject;
|
||||
|
||||
/**
|
||||
* 不送达和城市数据连表业务处理层
|
||||
* Class ShippingTemplatesNoDeliveryCityServices
|
||||
* @package app\services\product\shipping
|
||||
* @mixin ShippingTemplatesNoDeliveryCityDao
|
||||
*/
|
||||
class ShippingTemplatesNoDeliveryCityServices extends BaseServices
|
||||
{
|
||||
/**
|
||||
* @var ShippingTemplatesNoDeliveryCityDao
|
||||
*/
|
||||
#[Inject]
|
||||
protected ShippingTemplatesNoDeliveryCityDao $dao;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,191 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: CRMEB Team <admin@crmeb.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace app\services\product\shipping;
|
||||
|
||||
|
||||
use app\dao\product\shipping\ShippingTemplatesNoDeliveryDao;
|
||||
use app\services\BaseServices;
|
||||
use crmeb\exceptions\AdminException;
|
||||
use crmeb\utils\Arr;
|
||||
use think\annotation\Inject;
|
||||
|
||||
/**
|
||||
* 不送达
|
||||
* Class ShippingTemplatesNoDeliveryServices
|
||||
* @package app\services\product\shipping
|
||||
* @mixin ShippingTemplatesNoDeliveryDao
|
||||
*/
|
||||
class ShippingTemplatesNoDeliveryServices extends BaseServices
|
||||
{
|
||||
|
||||
/**
|
||||
* @var ShippingTemplatesNoDeliveryDao
|
||||
*/
|
||||
#[Inject]
|
||||
protected ShippingTemplatesNoDeliveryDao $dao;
|
||||
|
||||
/**
|
||||
* 添加不送达信息
|
||||
* @param array $noDeliveryInfo
|
||||
* @param int $tempId
|
||||
* @return bool|mixed
|
||||
*/
|
||||
public function saveNoDeliveryV1(array $noDeliveryInfo, int $tempId = 0)
|
||||
{
|
||||
$res = true;
|
||||
if ($tempId) {
|
||||
if ($this->dao->count(['temp_id' => $tempId])) {
|
||||
$res = $this->dao->delete($tempId, 'temp_id');
|
||||
}
|
||||
}
|
||||
$placeList = [];
|
||||
mt_srand();
|
||||
foreach ($noDeliveryInfo as $item) {
|
||||
$uniqid = uniqid('adminapi') . rand(1000, 9999);
|
||||
foreach ($item['city_ids'] as $cityId) {
|
||||
$placeList [] = [
|
||||
'temp_id' => $tempId,
|
||||
'city_id' => $cityId[count($cityId) - 1],
|
||||
'value' => json_encode($cityId),
|
||||
'uniqid' => $uniqid,
|
||||
];
|
||||
}
|
||||
}
|
||||
if (count($placeList)) {
|
||||
return $res && $this->dao->saveAll($placeList);
|
||||
} else {
|
||||
return $res;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加不送达信息
|
||||
* @param array $noDeliveryInfo
|
||||
* @param int $tempId
|
||||
* @return bool|mixed
|
||||
*/
|
||||
public function saveNoDelivery(array $noDeliveryInfo, int $tempId = 0)
|
||||
{
|
||||
$res = true;
|
||||
if ($tempId) {
|
||||
if ($this->dao->count(['temp_id' => $tempId])) {
|
||||
$res = $this->dao->delete($tempId, 'temp_id');
|
||||
}
|
||||
}
|
||||
$placeList = [];
|
||||
mt_srand();
|
||||
foreach ($noDeliveryInfo as $item) {
|
||||
if (isset($item['place']) && is_array($item['place'])) {
|
||||
$uniqid = uniqid('adminapi') . rand(1000, 9999);
|
||||
foreach ($item['place'] as $value) {
|
||||
if (isset($value['children']) && is_array($value['children'])) {
|
||||
foreach ($value['children'] as $vv) {
|
||||
if (!isset($vv['city_id'])) {
|
||||
throw new AdminException('缺少城市id无法保存');
|
||||
}
|
||||
$placeList [] = [
|
||||
'temp_id' => $tempId,
|
||||
'province_id' => $value['city_id'] ?? 0,
|
||||
'city_id' => $vv['city_id'] ?? 0,
|
||||
'uniqid' => $uniqid,
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (count($placeList)) {
|
||||
return $res && $this->dao->saveAll($placeList);
|
||||
} else {
|
||||
return $res;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $tempId
|
||||
* @return array
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function getNoDeliveryListV1(int $tempId)
|
||||
{
|
||||
$freeList = $this->dao->getShippingList(['temp_id' => $tempId]);
|
||||
return Arr::formatShipping($freeList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得指定包邮城市地址
|
||||
* @param int $tempId
|
||||
* @return array
|
||||
*/
|
||||
public function getNoDeliveryList(int $tempId)
|
||||
{
|
||||
$freeIdList = $this->dao->getShippingGroupArray(['temp_id' => $tempId], 'uniqid', 'uniqid', '');
|
||||
$freeData = [];
|
||||
$infos = $this->dao->getShippingArray(['uniqid' => $freeIdList, 'temp_id' => $tempId], '*', 'uniqid');
|
||||
foreach ($freeIdList as $uniqid) {
|
||||
$info = $infos[$uniqid];
|
||||
$freeData[] = [
|
||||
'place' => $this->getNoDeliveryTemp($uniqid, $info['province_id']),
|
||||
];
|
||||
}
|
||||
foreach ($freeData as &$item) {
|
||||
$item['placeName'] = implode(';', array_column($item['place'], 'name'));
|
||||
}
|
||||
return $freeData;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取不送达的省份
|
||||
* @param string $uniqid
|
||||
* @param int $provinceId
|
||||
* @return array
|
||||
*/
|
||||
public function getNoDeliveryTemp(string $uniqid, int $provinceId)
|
||||
{
|
||||
/** @var ShippingTemplatesNoDeliveryCityServices $service */
|
||||
$service = app()->make(ShippingTemplatesNoDeliveryCityServices::class);
|
||||
$infoList = $service->getUniqidList(['uniqid' => $uniqid]);
|
||||
$childrenData = [];
|
||||
foreach ($infoList as $item) {
|
||||
$childrenData[] = [
|
||||
'city_id' => $item['province_id'],
|
||||
'name' => $item['name'] ?? '全国',
|
||||
'children' => $this->getCityTemp($uniqid, $provinceId)
|
||||
];
|
||||
}
|
||||
return $childrenData;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取市区数据
|
||||
* @param string $uniqid
|
||||
* @param int $provinceId
|
||||
* @return array
|
||||
*/
|
||||
public function getCityTemp(string $uniqid, int $provinceId)
|
||||
{
|
||||
/** @var ShippingTemplatesNoDeliveryCityServices $service */
|
||||
$service = app()->make(ShippingTemplatesNoDeliveryCityServices::class);
|
||||
$infoList = $service->getUniqidList(['uniqid' => $uniqid, 'province_id' => $provinceId], false);
|
||||
$childrenData = [];
|
||||
foreach ($infoList as $item) {
|
||||
$childrenData[] = [
|
||||
'city_id' => $item['city_id'],
|
||||
'name' => $item['name'] ?? '全国',
|
||||
];
|
||||
}
|
||||
return $childrenData;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: CRMEB Team <admin@crmeb.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace app\services\product\shipping;
|
||||
|
||||
|
||||
use app\dao\product\shipping\ShippingTemplatesRegionCityDao;
|
||||
use app\services\BaseServices;
|
||||
use think\annotation\Inject;
|
||||
|
||||
/**
|
||||
* 根据地区设置邮费
|
||||
* Class ShippingTemplatesRegionCityServices
|
||||
* @package app\services\product\shipping
|
||||
* @mixin ShippingTemplatesRegionCityDao
|
||||
*/
|
||||
class ShippingTemplatesRegionCityServices extends BaseServices
|
||||
{
|
||||
|
||||
/**
|
||||
* @var ShippingTemplatesRegionCityDao
|
||||
*/
|
||||
#[Inject]
|
||||
protected ShippingTemplatesRegionCityDao $dao;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,255 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: CRMEB Team <admin@crmeb.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace app\services\product\shipping;
|
||||
|
||||
|
||||
use app\dao\product\shipping\ShippingTemplatesRegionDao;
|
||||
use app\services\BaseServices;
|
||||
use crmeb\exceptions\AdminException;
|
||||
use crmeb\services\CacheService;
|
||||
use crmeb\utils\Arr;
|
||||
use think\annotation\Inject;
|
||||
|
||||
/**
|
||||
* 指定邮费
|
||||
* Class ShippingTemplatesRegionServices
|
||||
* @package app\services\product\shipping
|
||||
* @mixin ShippingTemplatesRegionDao
|
||||
*/
|
||||
class ShippingTemplatesRegionServices extends BaseServices
|
||||
{
|
||||
|
||||
/**
|
||||
* @var ShippingTemplatesRegionDao
|
||||
*/
|
||||
#[Inject]
|
||||
protected ShippingTemplatesRegionDao $dao;
|
||||
|
||||
/**
|
||||
* @param array $tempIds
|
||||
* @param array $cityIds
|
||||
* @param int $expire
|
||||
* @return bool|mixed|null
|
||||
* @throws \Throwable
|
||||
*/
|
||||
public function getTempRegionListCache(array $tempIds, array $cityIds, int $expire = 60)
|
||||
{
|
||||
return CacheService::redisHandler('apiShipping')->remember(md5('RegionList' . json_encode([$tempIds, $cityIds])), function () use ($tempIds, $cityIds) {
|
||||
return $this->dao->getTempRegionList($tempIds, $cityIds, 'temp_id,first,first_price,continue,continue_price', 'temp_id');
|
||||
}, $expire);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加运费信息
|
||||
* @param array $regionInfo
|
||||
* @param int $group
|
||||
* @param int $tempId
|
||||
* @return bool
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function saveRegionV1(array $regionInfo, int $group = 0, $tempId = 0)
|
||||
{
|
||||
$res = true;
|
||||
if ($tempId) {
|
||||
if ($this->dao->count(['temp_id' => $tempId])) {
|
||||
$res = $this->dao->delete($tempId, 'temp_id');
|
||||
}
|
||||
}
|
||||
$regionList = [];
|
||||
mt_srand();
|
||||
foreach ($regionInfo as $item) {
|
||||
$uniqid = uniqid('adminapi') . rand(1000, 9999);
|
||||
if (isset($item['city_ids']) && $item['city_ids']) {
|
||||
foreach ($item['city_ids'] as $value) {
|
||||
$regionList[] = [
|
||||
'temp_id' => $tempId,
|
||||
'city_id' => $value ? $value[count($value) - 1] : 0,
|
||||
'value' => json_encode($value),
|
||||
'first' => $item['first'] ?? 0,
|
||||
'first_price' => $item['first_price'] ?? 0,
|
||||
'continue' => $item['continue'] ?? 0,
|
||||
'continue_price' => $item['continue_price'] ?? 0,
|
||||
'group' => $group,
|
||||
'uniqid' => $uniqid,
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
return $res && $this->dao->saveAll($regionList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加运费信息
|
||||
* @param array $regionInfo
|
||||
* @param int $group
|
||||
* @param int $tempId
|
||||
* @return bool
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function saveRegion(array $regionInfo, int $group = 0, $tempId = 0)
|
||||
{
|
||||
$res = true;
|
||||
if ($tempId) {
|
||||
if ($this->dao->count(['temp_id' => $tempId])) {
|
||||
$res = $this->dao->delete($tempId, 'temp_id');
|
||||
}
|
||||
}
|
||||
$regionList = [];
|
||||
mt_srand();
|
||||
foreach ($regionInfo as $item) {
|
||||
if (isset($item['region']) && is_array($item['region'])) {
|
||||
$uniqid = uniqid('adminapi') . rand(1000, 9999);
|
||||
foreach ($item['region'] as $value) {
|
||||
if (isset($value['children']) && is_array($value['children'])) {
|
||||
foreach ($value['children'] as $vv) {
|
||||
if (!isset($vv['city_id'])) {
|
||||
throw new AdminException('缺少城市id无法保存');
|
||||
}
|
||||
$regionList[] = [
|
||||
'temp_id' => $tempId,
|
||||
'province_id' => $value['city_id'] ?? 0,
|
||||
'city_id' => $vv['city_id'] ?? 0,
|
||||
'first' => $item['first'] ?? 0,
|
||||
'first_price' => $item['price'] ?? 0,
|
||||
'continue' => $item['continue'] ?? 0,
|
||||
'continue_price' => $item['continue_price'] ?? 0,
|
||||
'group' => $group,
|
||||
'uniqid' => $uniqid,
|
||||
];
|
||||
}
|
||||
} else {
|
||||
$regionList[0] = [
|
||||
'temp_id' => $tempId,
|
||||
'province_id' => 0,
|
||||
'city_id' => 0,
|
||||
'first' => $item['first'] ?? 0,
|
||||
'first_price' => $item['price'] ?? 0,
|
||||
'continue' => $item['continue'] ?? 0,
|
||||
'continue_price' => $item['continue_price'] ?? 0,
|
||||
'group' => $group,
|
||||
'uniqid' => $uniqid,
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $res && $this->dao->saveAll($regionList);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $tempId
|
||||
* @return array
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function getRegionListV1(int $tempId)
|
||||
{
|
||||
$freeList = $this->dao->getShippingList(['temp_id' => $tempId]);
|
||||
return Arr::formatShipping($freeList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取某个运费模板下的城市数据
|
||||
* @param int $tempId
|
||||
* @return array
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function getRegionList(int $tempId)
|
||||
{
|
||||
$regionList = $this->dao->getShippingGroupArray(['temp_id' => $tempId], 'uniqid', 'uniqid', '');
|
||||
$regionData = [];
|
||||
$infos = $this->dao->getShippingArray(['uniqid' => $regionList, 'temp_id' => $tempId], '*', 'uniqid');
|
||||
foreach ($regionList as $uniqid) {
|
||||
$info = $infos[$uniqid];
|
||||
if ($info['province_id'] == 0) {
|
||||
$regionData[] = [
|
||||
'region' => [
|
||||
'city_id' => 0,
|
||||
'name' => '默认全国',
|
||||
],
|
||||
'regionName' => '默认全国',
|
||||
'first' => $info['first'] ? floatval($info['first']) : 0,
|
||||
'price' => $info['first_price'] ? floatval($info['first_price']) : 0,
|
||||
'continue' => $info['continue'] ? floatval($info['continue']) : 0,
|
||||
'continue_price' => $info['continue_price'] ? floatval($info['continue_price']) : 0,
|
||||
'uniqid' => $info['uniqid'],
|
||||
];
|
||||
} else {
|
||||
$regionData[] = [
|
||||
'region' => $this->getRegionTemp($uniqid, $info['province_id']),
|
||||
'regionName' => '',
|
||||
'first' => $info['first'] ? floatval($info['first']) : 0,
|
||||
'price' => $info['first_price'] ? floatval($info['first_price']) : 0,
|
||||
'continue' => $info['continue'] ? floatval($info['continue']) : 0,
|
||||
'continue_price' => $info['continue_price'] ? floatval($info['continue_price']) : 0,
|
||||
'uniqid' => $info['uniqid'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($regionData as &$item) {
|
||||
if (!$item['regionName']) {
|
||||
$item['regionName'] = implode(';', array_map(function ($val) {
|
||||
return $val['name'];
|
||||
}, $item['region']));
|
||||
}
|
||||
}
|
||||
|
||||
return $regionData;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取省份下运费模板
|
||||
* @param string $uniqid
|
||||
* @param int $provinceId
|
||||
* @return array
|
||||
*/
|
||||
public function getRegionTemp(string $uniqid, int $provinceId)
|
||||
{
|
||||
/** @var ShippingTemplatesRegionCityServices $services */
|
||||
$services = app()->make(ShippingTemplatesRegionCityServices::class);
|
||||
$infoList = $services->getUniqidList(['uniqid' => $uniqid]);
|
||||
$childrenData = [];
|
||||
foreach ($infoList as $item) {
|
||||
$childrenData[] = [
|
||||
'city_id' => $item['province_id'],
|
||||
'name' => $item['name'] ?? '全国',
|
||||
'children' => $this->getCityTemp($uniqid, $item['province_id'])
|
||||
];
|
||||
}
|
||||
return $childrenData;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取市区下的数据
|
||||
* @param string $uniqid
|
||||
* @param int $provinceId
|
||||
* @return array
|
||||
*/
|
||||
public function getCityTemp(string $uniqid, int $provinceId)
|
||||
{
|
||||
/** @var ShippingTemplatesRegionCityServices $services */
|
||||
$services = app()->make(ShippingTemplatesRegionCityServices::class);
|
||||
$infoList = $services->getUniqidList(['uniqid' => $uniqid, 'province_id' => $provinceId], false);
|
||||
$childrenData = [];
|
||||
foreach ($infoList as $item) {
|
||||
$childrenData[] = [
|
||||
'city_id' => $item['city_id'],
|
||||
'name' => $item['name'] ?? '全国',
|
||||
];
|
||||
}
|
||||
return $childrenData;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,189 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: CRMEB Team <admin@crmeb.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types=1);
|
||||
|
||||
namespace app\services\product\shipping;
|
||||
|
||||
use app\services\BaseServices;
|
||||
use app\dao\product\shipping\ShippingTemplatesDao;
|
||||
use app\services\product\product\StoreProductServices;
|
||||
use crmeb\exceptions\AdminException;
|
||||
use crmeb\services\CacheService;
|
||||
use think\annotation\Inject;
|
||||
|
||||
/**
|
||||
* 运费模板
|
||||
* Class ShippingTemplatesServices
|
||||
* @package app\services\product\shipping
|
||||
* @mixin ShippingTemplatesDao
|
||||
*/
|
||||
class ShippingTemplatesServices extends BaseServices
|
||||
{
|
||||
|
||||
/**
|
||||
* 计费类型
|
||||
* @var string[]
|
||||
*/
|
||||
protected array $group = [
|
||||
1 => '按件数',
|
||||
2 => '按重量',
|
||||
3 => '按体积'
|
||||
];
|
||||
|
||||
/**
|
||||
* @var ShippingTemplatesDao
|
||||
*/
|
||||
#[Inject]
|
||||
protected ShippingTemplatesDao $dao;
|
||||
|
||||
/**
|
||||
* 获取运费模板列表
|
||||
* @param array $where
|
||||
* @return array
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function getShippingList(array $where)
|
||||
{
|
||||
[$page, $limit] = $this->getPageValue();
|
||||
$data = $this->dao->getShippingList($where, $page, $limit);
|
||||
if ($data) {
|
||||
$groupName = $this->group;
|
||||
foreach ($data as &$item) {
|
||||
$item['type'] = $groupName[$item['group']] ?? '';
|
||||
}
|
||||
}
|
||||
$count = $this->dao->count($where);
|
||||
return compact('data', 'count');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $where
|
||||
* @param $field
|
||||
* @param string|null $key
|
||||
* @param int $exprie
|
||||
* @return bool|mixed|null
|
||||
*/
|
||||
public function getShippingColumnCache(array $where, $field, string $key = null, int $exprie = 60)
|
||||
{
|
||||
return CacheService::redisHandler('apiShipping')->remember(md5('Shipping_column' . json_encode($where)), function () use ($where, $field, $key) {
|
||||
return $this->dao->getShippingColumn($where, $field, $key);
|
||||
}, $exprie);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取需要修改的运费模板
|
||||
* @param int $id
|
||||
* @return mixed
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function getShipping(int $id)
|
||||
{
|
||||
$templates = $this->dao->get($id);
|
||||
if (!$templates) {
|
||||
throw new AdminException('修改的模板不存在');
|
||||
}
|
||||
/** @var ShippingTemplatesFreeServices $freeServices */
|
||||
$freeServices = app()->make(ShippingTemplatesFreeServices::class);
|
||||
/** @var ShippingTemplatesRegionServices $regionServices */
|
||||
$regionServices = app()->make(ShippingTemplatesRegionServices::class);
|
||||
/** @var ShippingTemplatesNoDeliveryServices $noDeliveryServices */
|
||||
$noDeliveryServices = app()->make(ShippingTemplatesNoDeliveryServices::class);
|
||||
$data['appointList'] = $freeServices->getFreeListV1($id);
|
||||
$data['templateList'] = $regionServices->getRegionListV1($id);
|
||||
$data['noDeliveryList'] = $noDeliveryServices->getNoDeliveryListV1($id);
|
||||
if (!isset($data['templateList'][0]['city_ids'])) {
|
||||
$data['templateList'][0] = ['city_ids' => [[0]], 'regionName' => '默认全国'];
|
||||
}
|
||||
$data['formData'] = [
|
||||
'name' => $templates->name,
|
||||
'type' => $templates->getData('group'),
|
||||
'appoint_check' => intval($templates->getData('appoint')),
|
||||
'no_delivery_check' => intval($templates->getData('no_delivery')),
|
||||
'sort' => intval($templates->getData('sort')),
|
||||
];
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存或者修改运费模板
|
||||
* @param int $id
|
||||
* @param array $temp
|
||||
* @param array $data
|
||||
* @return mixed
|
||||
*/
|
||||
public function save(int $id, array $temp, array $data)
|
||||
{
|
||||
|
||||
/** @var ShippingTemplatesRegionServices $regionServices */
|
||||
$regionServices = app()->make(ShippingTemplatesRegionServices::class);
|
||||
$this->transaction(function () use ($regionServices, $data, $id, $temp) {
|
||||
|
||||
if ($id) {
|
||||
$res = $this->dao->update($id, $temp);
|
||||
} else {
|
||||
$res = $this->dao->save($temp);
|
||||
}
|
||||
if (!$res) {
|
||||
throw new AdminException('保存失败');
|
||||
}
|
||||
if (!$id) $id = $res->id;
|
||||
|
||||
//设置区域配送
|
||||
if (!$regionServices->saveRegionV1($data['region_info'], (int)$data['group'], (int)$id)) {
|
||||
throw new AdminException('指定区域邮费添加失败!');
|
||||
}
|
||||
//设置指定包邮
|
||||
if ($data['appoint']) {
|
||||
/** @var ShippingTemplatesFreeServices $freeServices */
|
||||
$freeServices = app()->make(ShippingTemplatesFreeServices::class);
|
||||
if (!$freeServices->saveFreeV1($data['appoint_info'], (int)$data['group'], (int)$id)) {
|
||||
throw new AdminException('指定包邮添加失败!');
|
||||
}
|
||||
}
|
||||
//设置不送达
|
||||
if ($data['no_delivery']) {
|
||||
/** @var ShippingTemplatesNoDeliveryServices $noDeliveryServices */
|
||||
$noDeliveryServices = app()->make(ShippingTemplatesNoDeliveryServices::class);
|
||||
if (!$noDeliveryServices->saveNoDeliveryV1($data['no_delivery_info'], (int)$id)) {
|
||||
throw new AdminException('指定不送达添加失败!');
|
||||
}
|
||||
}
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除运费模板
|
||||
* @param int $id
|
||||
*/
|
||||
public function detete(int $id)
|
||||
{
|
||||
$this->dao->delete($id);
|
||||
/** @var ShippingTemplatesFreeServices $freeServices */
|
||||
$freeServices = app()->make(ShippingTemplatesFreeServices::class);
|
||||
/** @var ShippingTemplatesRegionServices $regionServices */
|
||||
$regionServices = app()->make(ShippingTemplatesRegionServices::class);
|
||||
/** @var ShippingTemplatesNoDeliveryServices $noDeliveryServices */
|
||||
$noDeliveryServices = app()->make(ShippingTemplatesNoDeliveryServices::class);
|
||||
/** @var StoreProductServices $storeProductServices */
|
||||
$storeProductServices = app()->make(StoreProductServices::class);
|
||||
$this->transaction(function () use ($id, $freeServices, $regionServices, $noDeliveryServices, $storeProductServices) {
|
||||
$freeServices->delete($id, 'temp_id');
|
||||
$regionServices->delete($id, 'temp_id');
|
||||
$noDeliveryServices->delete($id, 'temp_id');
|
||||
$storeProductServices->update(['temp_id' => $id], ['temp_id' => 1]);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user