872 行
35 KiB
PHP
可执行文件
872 行
35 KiB
PHP
可执行文件
<?php
|
|
|
|
namespace Retail\Controller;
|
|
|
|
use Common\Model\CitysModel;
|
|
use Common\Model\DeliveryTemplateModel;
|
|
use Common\Model\RetailGoodsModel;
|
|
use Common\Model\StoreConfigModel;
|
|
use Common\Model\TemplateAreaModel;
|
|
use Think\Controller;
|
|
use Addons\Api\GlobalFunction;
|
|
use Addons\Api\ResultCodeManager;
|
|
|
|
//[1.8.80]
|
|
|
|
/**
|
|
* 门店区域配送模板设置
|
|
* Class SystemDeliveryController
|
|
* @package Retail\Controller
|
|
*/
|
|
class SystemDeliveryController extends BaseController
|
|
{
|
|
//增加模板
|
|
public function addFormWork()
|
|
{
|
|
$this->display();
|
|
|
|
}
|
|
|
|
//数组封装(目前最可行)
|
|
public function map()
|
|
{
|
|
/* ini_set('memory_limit', '2048M');*/
|
|
$map['id'] = array('between', '50473,54127');
|
|
$arrMap['map'] = [];
|
|
//获取省、市、区所有数据
|
|
$CitysModel = new CitysModel();
|
|
$data['province'] = $CitysModel->field(' cityid as value, city_name as label, parentid ')->where(array($map))->select();
|
|
//封装省信息
|
|
foreach ($data['province'] as $k => $v) {
|
|
if ($v['parentid'] == 0) {
|
|
array_push($arrMap['map'], [
|
|
'value' => $v['value'],
|
|
'label' => $v['label'],
|
|
'children' => []
|
|
]);
|
|
}
|
|
}
|
|
//封装市信息
|
|
foreach ($arrMap['map'] as $k1 => &$v1) {
|
|
foreach ($data['province'] as $k => $v) {
|
|
if ($v1['value'] === $v['parentid']) {
|
|
$v1['children'][] = [
|
|
'value' => $v['value'],
|
|
'label' => $v['label'],
|
|
'children' => []
|
|
];
|
|
}
|
|
}
|
|
}
|
|
//封装区信息
|
|
foreach ($arrMap['map'] as $ke => &$va) {
|
|
foreach ($va['children'] as $k1 => &$v1) {
|
|
foreach ($data['province'] as $k => $v) {
|
|
|
|
if ($v1['value'] === $v['parentid']) {
|
|
$v1['children'][] = [
|
|
'value' => $v['value'],
|
|
'label' => $v['label'],
|
|
];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
$this->ajaxReturn(GlobalFunction::buildResult(ResultCodeManager::SUCCESS, ResultCodeManager::$messages[ResultCodeManager::SUCCESS], $arrMap));
|
|
}
|
|
|
|
/* jsy配送模板*/
|
|
public function distributionTemplate()
|
|
{
|
|
$this->display();
|
|
}
|
|
|
|
/* jsy批量使用*/
|
|
public function batchUse()
|
|
{
|
|
$a = I('get.uid');//接收前端传参
|
|
$type = I('get.type');//接收前端传参
|
|
$this->assign('id', $a);
|
|
$this->assign('type', $type);
|
|
$this->display();
|
|
}
|
|
|
|
/* jsy修改模板*/
|
|
public function modifyTemplate()
|
|
{
|
|
$a = I('get.id');//接收前端传参
|
|
$this->assign('id', $a);
|
|
$this->display();
|
|
}
|
|
|
|
/* jsy配送区域查看*/
|
|
public function regionalView()
|
|
{
|
|
$a = I('get.id');//接收前端传参
|
|
$this->assign('id', $a);
|
|
$this->display();
|
|
}
|
|
|
|
/**
|
|
* 门店配送模板信息
|
|
*/
|
|
public function displayDistributionTemInfo()
|
|
{
|
|
$post = I('post.');//接收前端传参
|
|
$paramArr = array(
|
|
'startTime',//开始时间
|
|
'endTime',//结束时间
|
|
'templateName',//模板名称
|
|
'currentPage',//当前页面
|
|
'pageSize',//当前页面显示数量
|
|
);
|
|
$checkRet = GlobalFunction::checkParams($paramArr, $post);//验证参数
|
|
if ($checkRet['status'] == 1) {//值为1 过滤出错 前端字段传参不全
|
|
$this->ajaxReturn($checkRet['result']);//返回缺失字段
|
|
}
|
|
$startTime = strtotime($post['startTime']);
|
|
$endTime = strtotime($post['endTime']);
|
|
$DeliveryTemplateModel = new DeliveryTemplateModel();
|
|
$temBaseData = $DeliveryTemplateModel->displayDistributionTemInfo($this->store_id, $startTime, $endTime, $post['templateName'], $post['currentPage'], $post['pageSize']);
|
|
if (empty($temBaseData)) {
|
|
$this->ajaxReturn(GlobalFunction::buildResult(ResultCodeManager::NO_DATA, ResultCodeManager::$messages[ResultCodeManager::NO_DATA]));
|
|
}
|
|
$map = self::getObtainJudgmentCondAndRetReq($post);//根据判断条件获取所需的查询条件
|
|
$totalQuantity = $DeliveryTemplateModel->getCount($map);//获取当前门店模板总数量
|
|
$temData = array(
|
|
'temBaseData' => $temBaseData,
|
|
'totalQuantity' => $totalQuantity
|
|
);
|
|
$this->ajaxReturn(GlobalFunction::buildResult(ResultCodeManager::SUCCESS, ResultCodeManager::$messages[ResultCodeManager::SUCCESS], $temData));
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
* 添加运费模板
|
|
*/
|
|
public function addDistributionTem()
|
|
{
|
|
$post = I('post.');//接收前端传参
|
|
$paramArr = array(
|
|
'temTitle',//模板标题
|
|
'temBillingModel',//模板计费方式 1免运费 2按件数 3按重量 4统一运费
|
|
'areaData',//区域数组集合
|
|
);
|
|
$checkRet = GlobalFunction::checkParams($paramArr, $post);//验证参数
|
|
if ($checkRet['status'] == 1) {//值为1 过滤出错 前端字段传参不全
|
|
$this->ajaxReturn($checkRet['result']);//返回缺失字段
|
|
}
|
|
$DeliveryTemplateModel = new DeliveryTemplateModel();
|
|
$checkRet = $DeliveryTemplateModel->getFind(array('title' => $post['temTitle'], 'status' => array('in', '1,2'), 'store_id' => $this->store_id));//验证模板标题是否重复
|
|
if ($checkRet) {
|
|
$this->ajaxReturn(GlobalFunction::buildResult(ResultCodeManager::TEM_TITLE_REPEAT, ResultCodeManager::$messages[ResultCodeManager::TEM_TITLE_REPEAT]));
|
|
}
|
|
//组装模板表添加数据
|
|
$temData = array(
|
|
'title' => $post['temTitle'],
|
|
'type' => $post['temBillingModel'],
|
|
'add_time' => time(),
|
|
'store_id' => $this->store_id,
|
|
'merchant_id' => $this->merchant_id,
|
|
);
|
|
$addTemRet = $DeliveryTemplateModel->addDistributionTem($temData);//添加进模板表
|
|
if (!$addTemRet) {
|
|
$this->ajaxReturn(GlobalFunction::buildResult(ResultCodeManager::E_SAVE_ERROR, ResultCodeManager::$messages[ResultCodeManager::E_SAVE_ERROR]));
|
|
}
|
|
//获得模板区域的数据
|
|
$temAreaData = json_decode($post['areaData'], true);
|
|
//判断数组是否为一维数组
|
|
if (count($temAreaData) == count($temAreaData, 1)) {
|
|
$temAreaData['template_id'] = $addTemRet;
|
|
$temAreaData['add_time'] = time();
|
|
} else {
|
|
foreach ($temAreaData as &$item) {
|
|
foreach ($item as &$v) {
|
|
if (empty($v)) {
|
|
$v = 0;
|
|
}
|
|
}
|
|
$item['template_id'] = $addTemRet;
|
|
$item['add_time'] = time();
|
|
}
|
|
}
|
|
$TemplateAreaModel = new TemplateAreaModel();
|
|
$addTemAreaRet = $TemplateAreaModel->addTemplateArea($temAreaData);
|
|
if (!$addTemAreaRet) {
|
|
$this->ajaxReturn(GlobalFunction::buildResult(ResultCodeManager::E_SAVE_ERROR, ResultCodeManager::$messages[ResultCodeManager::E_SAVE_ERROR]));
|
|
}
|
|
$this->ajaxReturn(GlobalFunction::buildResult(ResultCodeManager::SUCCESS, ResultCodeManager::$messages[ResultCodeManager::SUCCESS]));
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
* 编辑 -> 获取当前模板的信息
|
|
*/
|
|
public function getTemInfo()
|
|
{
|
|
$post = I('post.');//接收前端传参
|
|
$paramArr = array(
|
|
'temId',//模板ID
|
|
);
|
|
$checkRet = GlobalFunction::checkParams($paramArr, $post);//验证参数
|
|
if ($checkRet['status'] == 1) {//值为1 过滤出错 前端字段传参不全
|
|
$this->ajaxReturn($checkRet['result']);//返回缺失字段
|
|
}
|
|
$DeliveryTemplateModel = new DeliveryTemplateModel();
|
|
$temBaseInfo = $DeliveryTemplateModel->getTemInfoByTemId($post['temId']);//根据模板ID获取模板基本信息
|
|
$TemplateAreaModel = new TemplateAreaModel();
|
|
$temAreaInfo = $TemplateAreaModel->getTemAreas($post['temId']);//根据模板ID获取区域信息
|
|
foreach ($temAreaInfo as &$item) {
|
|
$areasId = str_replace('#', '', $item['area_id']);//取得所需数据
|
|
$CitysModel = new CitysModel();
|
|
$cityInfo = $CitysModel->distributionAreaCityInfo($areasId);//查询对应ID的城市信息
|
|
$item['areaData'] = self::getAreaDetails($cityInfo);//组装城市信息
|
|
}
|
|
$temBaseInfo['areaInfo'] = $temAreaInfo;
|
|
if (!$temBaseInfo) {
|
|
$this->ajaxReturn(GlobalFunction::buildResult(ResultCodeManager::NO_DATA, ResultCodeManager::$messages[ResultCodeManager::NO_DATA]));
|
|
}
|
|
$this->ajaxReturn(GlobalFunction::buildResult(ResultCodeManager::SUCCESS, ResultCodeManager::$messages[ResultCodeManager::SUCCESS], $temBaseInfo));
|
|
|
|
}
|
|
|
|
/**
|
|
* 删除当前模板区域
|
|
*/
|
|
public function deleteTemArea()
|
|
{
|
|
$post = I('post.');//接收前端传参
|
|
$paramArr = array(
|
|
'areaId',//区域表ID
|
|
);
|
|
$checkRet = GlobalFunction::checkParams($paramArr, $post);//验证参数
|
|
if ($checkRet['status'] == 1) {//值为1 过滤出错 前端字段传参不全
|
|
$this->ajaxReturn($checkRet['result']);//返回缺失字段
|
|
}
|
|
$TemplateAreaModel = new TemplateAreaModel();
|
|
$deleteRet = $TemplateAreaModel->deleteTemArea($post['areaId']);
|
|
if (!$deleteRet) {
|
|
$this->ajaxReturn(GlobalFunction::buildResult(ResultCodeManager::E_SAVE_ERROR, ResultCodeManager::$messages[ResultCodeManager::E_SAVE_ERROR]));
|
|
}
|
|
$this->ajaxReturn(GlobalFunction::buildResult(ResultCodeManager::SUCCESS, ResultCodeManager::$messages[ResultCodeManager::SUCCESS]));
|
|
}
|
|
|
|
|
|
/**
|
|
* 更新区域数据
|
|
*/
|
|
public function updateTemArea()
|
|
{
|
|
$post = I('post.');//接收前端传参
|
|
$paramArr = array(
|
|
'areaId',//区域表ID
|
|
'areaCom',//区域ID拼接
|
|
);
|
|
$checkRet = GlobalFunction::checkParams($paramArr, $post);//验证参数
|
|
if ($checkRet['status'] == 1) {//值为1 过滤出错 前端字段传参不全
|
|
$this->ajaxReturn($checkRet['result']);//返回缺失字段
|
|
}
|
|
$data = array(
|
|
'id' => $post['areaId'],
|
|
'area_id' => $post['areaCom']
|
|
);
|
|
$TemplateAreaModel = new TemplateAreaModel();
|
|
$updateRet = $TemplateAreaModel->updateTemArea($data);
|
|
if (!$updateRet) {
|
|
$this->ajaxReturn(GlobalFunction::buildResult(ResultCodeManager::E_SAVE_ERROR, ResultCodeManager::$messages[ResultCodeManager::E_SAVE_ERROR]));
|
|
}
|
|
$this->ajaxReturn(GlobalFunction::buildResult(ResultCodeManager::SUCCESS, ResultCodeManager::$messages[ResultCodeManager::SUCCESS]));
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
* 编辑当前模板
|
|
*/
|
|
public function editDistributionTem()
|
|
{
|
|
$post = I('post.');//接收前端传参
|
|
$paramArr = array(
|
|
'temId',//模板ID
|
|
'temTitle',//模板标题
|
|
'temBillingModel',//模板计费方式 1免运费 2按件数 3按重量 4统一运费
|
|
'areaData',//区域数组集合
|
|
);
|
|
$checkRet = GlobalFunction::checkParams($paramArr, $post);//验证参数
|
|
if ($checkRet['status'] == 1) {//值为1 过滤出错 前端字段传参不全
|
|
$this->ajaxReturn($checkRet['result']);//返回缺失字段
|
|
}
|
|
$DeliveryTemplateModel = new DeliveryTemplateModel();
|
|
$checkRet = $DeliveryTemplateModel->getFind(array('title' => $post['temTitle'], 'status' => array('in', '1,2'), 'store_id' => $this->store_id));//验证模板标题是否重复
|
|
if ($checkRet) {
|
|
$this->ajaxReturn(GlobalFunction::buildResult(ResultCodeManager::TEM_TITLE_REPEAT, ResultCodeManager::$messages[ResultCodeManager::TEM_TITLE_REPEAT]));
|
|
}
|
|
//获取模板信息
|
|
$baseData = array(
|
|
'title' => $post['temTitle'],
|
|
'type' => $post['temBillingModel'],
|
|
'store_id' => session('store_userinfo')['id']
|
|
);
|
|
$addRet = $DeliveryTemplateModel->addDistributionTem($baseData, $post['temId']);//修改模板信息
|
|
if (!$addRet) {
|
|
$this->ajaxReturn(GlobalFunction::buildResult(ResultCodeManager::E_SAVE_ERROR, ResultCodeManager::$messages[ResultCodeManager::E_SAVE_ERROR]));
|
|
}
|
|
//获取模板下区域信息
|
|
$editData = json_decode($post['areaData'], true);
|
|
$TemplateAreaModel = new TemplateAreaModel();
|
|
foreach ($editData as $item) {
|
|
if ($item['id'] == null) {
|
|
unset($item['id']);
|
|
$addAreaRet = $TemplateAreaModel->addTemplateArea($item);//新增区域
|
|
if (!$addAreaRet) {
|
|
$this->ajaxReturn(GlobalFunction::buildResult(ResultCodeManager::E_SAVE_ERROR, ResultCodeManager::$messages[ResultCodeManager::E_SAVE_ERROR]));
|
|
}
|
|
}
|
|
}
|
|
$this->ajaxReturn(GlobalFunction::buildResult(ResultCodeManager::SUCCESS, ResultCodeManager::$messages[ResultCodeManager::SUCCESS]));
|
|
|
|
}
|
|
|
|
/**
|
|
* 批量使用前获取的商品页面
|
|
*/
|
|
public function batchUsageGoodsInfo()
|
|
{
|
|
$post = I('post.');//接收前端传参
|
|
$paramArr = array(
|
|
'keyword',//商品名称或货号
|
|
'template_id',//模板ID
|
|
'pricing_method',//商品类型
|
|
'page',//当前页面
|
|
'limit',//当前页面显示数量
|
|
'goods_type',
|
|
);
|
|
$checkRet = GlobalFunction::checkParams($paramArr, $post);//验证参数
|
|
if ($checkRet['status'] == 1) {//值为1 过滤出错 前端字段传参不全
|
|
$this->ajaxReturn($checkRet['result']);//返回缺失字段
|
|
}
|
|
$GoodsModel = new RetailGoodsModel();
|
|
$getData = $GoodsModel->batchUsageGoodsInfo($this->store_id, $post['keyword'], $post['goods_type'], $post['template_id'], $post['pricing_method'], $post['page'], $post['limit']);
|
|
|
|
$shopData = array(
|
|
'shopBaseData' => $getData['list'],
|
|
'totalQuantity' => intval($getData['count']),
|
|
);
|
|
$this->ajaxReturn(GlobalFunction::buildResult(ResultCodeManager::SUCCESS, ResultCodeManager::$messages[ResultCodeManager::SUCCESS], $shopData));
|
|
}
|
|
|
|
/**
|
|
* 获取当前门店已有的模板
|
|
*/
|
|
public function getStoreFreightTem()
|
|
{
|
|
$DeliveryTemplateModel = new DeliveryTemplateModel();
|
|
$temData = $DeliveryTemplateModel->getStoreFreightTem($this->store_id);
|
|
if (!$temData) {
|
|
$this->ajaxReturn(GlobalFunction::buildResult(ResultCodeManager::NO_DATA, ResultCodeManager::$messages[ResultCodeManager::NO_DATA]));
|
|
}
|
|
$this->ajaxReturn(GlobalFunction::buildResult(ResultCodeManager::SUCCESS, ResultCodeManager::$messages[ResultCodeManager::SUCCESS], $temData));
|
|
}
|
|
|
|
/**
|
|
* 批量使用-提交
|
|
*/
|
|
public function batchUsage()
|
|
{
|
|
$post = I('post.');//接收前端传参
|
|
$paramArr = array(
|
|
'goodsList',//商品、拼团、秒杀ID, json数组[{"id":"1","delivery_type":"1,2,3"}]
|
|
'templateId',//模板ID
|
|
'goods_type',//类型 1普通商品 2拼团 3秒杀
|
|
);
|
|
$checkRet = GlobalFunction::checkParams($paramArr, $post);//验证参数
|
|
if ($checkRet['status'] == 1) {//值为1 过滤出错 前端字段传参不全
|
|
$this->ajaxReturn($checkRet['result']);//返回缺失字段
|
|
}
|
|
$RetailGoodsModel = new RetailGoodsModel();
|
|
$updateRet = $RetailGoodsModel->batchUsage($post['goodsList'], $post['templateId'], $post['goods_type']);
|
|
if (!$updateRet) {
|
|
$this->ajaxReturn(GlobalFunction::buildResult(ResultCodeManager::E_SAVE_ERROR, ResultCodeManager::$messages[ResultCodeManager::E_SAVE_ERROR]));
|
|
}
|
|
$this->ajaxReturn(GlobalFunction::buildResult(ResultCodeManager::SUCCESS, ResultCodeManager::$messages[ResultCodeManager::SUCCESS], $updateRet));
|
|
|
|
}
|
|
|
|
/**
|
|
* 查看区域详细信息
|
|
*/
|
|
public function viewDistributionArea()
|
|
{
|
|
$post = I('post.');//接收前端传参
|
|
$paramArr = array(
|
|
'temId',//模板ID
|
|
);
|
|
$checkRet = GlobalFunction::checkParams($paramArr, $post);//验证参数
|
|
if ($checkRet['status'] == 1) {//值为1 过滤出错 前端字段传参不全
|
|
$this->ajaxReturn($checkRet['result']);//返回缺失字段
|
|
}
|
|
$TemplateAreaModel = new TemplateAreaModel();
|
|
$areaData = $TemplateAreaModel->getTemAreas($post['temId']);//查看当前模板下面的区域
|
|
foreach ($areaData as &$item) {
|
|
$areaCom = str_replace('#', '', $item['area_id']);
|
|
$CitysModel = new CitysModel();
|
|
$cityData = $CitysModel->distributionAreaCityInfo($areaCom);
|
|
$areaDetails = self::getAreaDetails($cityData);//组装城市信息
|
|
$item['areaData'] = $areaDetails;
|
|
}
|
|
if (!$areaData) {
|
|
$this->ajaxReturn(GlobalFunction::buildResult(ResultCodeManager::NO_DATA, ResultCodeManager::$messages[ResultCodeManager::NO_DATA]));
|
|
}
|
|
$this->ajaxReturn(GlobalFunction::buildResult(ResultCodeManager::SUCCESS, ResultCodeManager::$messages[ResultCodeManager::SUCCESS], $areaData));
|
|
}
|
|
|
|
/**
|
|
* 删除模板
|
|
*/
|
|
public function deleteTemplate()
|
|
{
|
|
$post = I('post.');//接收前端传参
|
|
$paramArr = array(
|
|
'templateId'//模板ID
|
|
);
|
|
$checkRet = GlobalFunction::checkParams($paramArr, $post);//验证参数
|
|
if ($checkRet['status'] == 1) {//值为1 过滤出错 前端字段传参不全
|
|
$this->ajaxReturn($checkRet['result']);//返回缺失字段
|
|
}
|
|
$DeliveryTemplateModel = new DeliveryTemplateModel();
|
|
$updateRet = $DeliveryTemplateModel->deleteTemplate($post['templateId']);
|
|
|
|
if (!$updateRet['status']) {
|
|
$this->ajaxReturn(array(
|
|
'code' => 43001,
|
|
'msg' => $updateRet['msg']
|
|
));
|
|
}
|
|
$this->ajaxReturn(GlobalFunction::buildResult(ResultCodeManager::SUCCESS, ResultCodeManager::$messages[ResultCodeManager::SUCCESS]));
|
|
}
|
|
|
|
|
|
/**
|
|
* 获取门店模板的配置信息
|
|
*/
|
|
public function getStoreTemConfiguration()
|
|
{
|
|
$StoreConfigModel = new StoreConfigModel();
|
|
$storeTemConfigData = $StoreConfigModel->getStoreTemConfiguration($this->store_id);
|
|
if (!$storeTemConfigData) {
|
|
$this->ajaxReturn(GlobalFunction::buildResult(ResultCodeManager::NO_DATA, ResultCodeManager::$messages[ResultCodeManager::NO_DATA]));
|
|
}
|
|
$this->ajaxReturn(GlobalFunction::buildResult(ResultCodeManager::SUCCESS, ResultCodeManager::$messages[ResultCodeManager::SUCCESS], $storeTemConfigData));
|
|
}
|
|
|
|
/**
|
|
* 改变计费方式 1 商品累加运费 2 组合运费
|
|
*/
|
|
public function changeBillingMode()
|
|
{
|
|
$post = I('post.');//接收前端传参
|
|
$paramArr = array(
|
|
'billingMode'//配送模板计费方式 1 商品累加运费 2 组合运费
|
|
);
|
|
$checkRet = GlobalFunction::checkParams($paramArr, $post);//验证参数
|
|
if ($checkRet['status'] == 1) {//值为1 过滤出错 前端字段传参不全
|
|
$this->ajaxReturn($checkRet['result']);//返回缺失字段
|
|
}
|
|
$changeData = array(
|
|
'template_charging_type' => $post['billingMode']
|
|
);
|
|
$StoreConfigModel = new StoreConfigModel();
|
|
$changeRet = $StoreConfigModel->setConfig($this->store_id, $changeData);
|
|
if (!$changeRet) {
|
|
$this->ajaxReturn(GlobalFunction::buildResult(ResultCodeManager::E_SAVE_ERROR, ResultCodeManager::$messages[ResultCodeManager::E_SAVE_ERROR]));
|
|
}
|
|
$this->ajaxReturn(GlobalFunction::buildResult(ResultCodeManager::SUCCESS, ResultCodeManager::$messages[ResultCodeManager::SUCCESS]));
|
|
}
|
|
|
|
|
|
/**
|
|
* 拼接区域信息
|
|
* @param $areaData 查询的多条记录
|
|
* @return string
|
|
*/
|
|
function splicingAreas($areaData)
|
|
{
|
|
$areaSet = '';
|
|
for ($i = 0; $i < count($areaData); $i++) {
|
|
if (!empty($areaData[$i + 1]) && ($i + 1) <= count($areaData)) {
|
|
$areaSet .= $areaData[$i]['area_id'] . ',';
|
|
} else {
|
|
$areaSet .= $areaData[$i]['area_id'];
|
|
}
|
|
}
|
|
$areaSet = str_replace('#', '', $areaSet);
|
|
return $areaSet;
|
|
}
|
|
|
|
|
|
/**
|
|
* 拼接城市信息
|
|
* @param $cityData
|
|
* @return string
|
|
*/
|
|
function getAreaDetails($cityData)
|
|
{
|
|
$areaDetails = [];
|
|
foreach ($cityData as &$item) {
|
|
$areaDetails[] = $item['city_name'];
|
|
}
|
|
return implode('、', $areaDetails);
|
|
}
|
|
|
|
/**
|
|
*获取判断条件并返回值
|
|
* @param $post 前端参数
|
|
* @return array
|
|
*/
|
|
function getObtainJudgmentCondAndRetReq($post)
|
|
{
|
|
$map = array(
|
|
'store_id' => $this->store_id,
|
|
'status' => array('neq', 3),
|
|
);
|
|
if ($post['startTime'] != null) {
|
|
$map['add_time'] = array('between', array(strtotime($post['startTime']), strtotime($post['endTime']) + 86400));
|
|
}
|
|
if ($post['templateName'] != null) {
|
|
$map['title'] = array('like', '%' . $post['templateName'] . '%');
|
|
}
|
|
return $map;
|
|
}
|
|
|
|
|
|
/**
|
|
* 获取商品的总数量的条件
|
|
* @param $shopId
|
|
* @param $post
|
|
* @return array
|
|
*/
|
|
function getShopTotalQuantityCondition($shopId, $post)
|
|
{
|
|
$map = array(
|
|
'store_id' => $shopId,
|
|
'status' => array('neq', 2)
|
|
);
|
|
if ($post['mixContent'] != null) {
|
|
if ($post['contentType'] == 1) {
|
|
$map['goods_name'] = array('like', '%' . $post['mixContent'] . '%');
|
|
} elseif ($post['contentType'] == 2) {
|
|
$map['goods_number'] = array('like', '%' . $post['mixContent'] . '%');
|
|
}
|
|
}
|
|
if ($post['temType'] != null) {
|
|
if ($post['temType'] == 0) {
|
|
$map['delivery_template_id'] = array('exp', 'is null');
|
|
} else {
|
|
$map['delivery_template_id'] = $post['temType'];
|
|
}
|
|
}
|
|
if ($post['commodityType'] != null) {
|
|
$map['pricing_method'] = $post['commodityType'];
|
|
}
|
|
return $map;
|
|
}
|
|
|
|
|
|
/**
|
|
* 获取配送费
|
|
*/
|
|
public function getFreight()
|
|
{
|
|
$post = I('post.');//接收前端传参
|
|
$paramArr = array(
|
|
'goodsData',//商品信息 包含模板ID,商品ID,商品数量
|
|
'temsId',//模板ID 以","拼接
|
|
'province',//用户地址的省
|
|
'city',//用户地址的市
|
|
'area',//用户地址的区
|
|
);
|
|
$checkRet = GlobalFunction::checkParams($paramArr, $post);//验证参数
|
|
if ($checkRet['status'] == 1) {//值为1 过滤出错 前端字段传参不全
|
|
$this->ajaxReturn($checkRet['result']);//返回缺失字段
|
|
}
|
|
//获取模板对应的区域信息
|
|
$TemplateAreaModel = new TemplateAreaModel();
|
|
$areaData = $TemplateAreaModel->getTemplateAreas($post['temsId']);
|
|
//获取用户地址省市区对应的区域信息
|
|
$CitysModel = new CitysModel();
|
|
$userAreaData = $CitysModel->getCityDataByCityName($post['province'], $post['city'], $post['area']);
|
|
//判断出包含用户地址的模板区域信息
|
|
$areaDetails = self::getUserRespeRegion($areaData, $userAreaData);
|
|
//组合商品和商品的配送模板信息
|
|
$goodsData = json_decode($post['goodsData'], true);
|
|
$goodAndTemData = self::comGooodAndArea($areaDetails, $goodsData);
|
|
$comGoodAndTemData = self::comGoodAndTemData($goodAndTemData);//组合运费模板相同的商品
|
|
$distributionFee = self::getDistributionFee($comGoodAndTemData);//获取最终配送费
|
|
$this->ajaxReturn(GlobalFunction::buildResult(ResultCodeManager::SUCCESS, ResultCodeManager::$messages[ResultCodeManager::SUCCESS], $distributionFee));
|
|
}
|
|
|
|
|
|
/**
|
|
* 返回当前用户地址所属的区域信息
|
|
* @param $areaData
|
|
* @param $userAreaData
|
|
* @return array
|
|
*/
|
|
function getUserRespeRegion($areaData, $userAreaData)
|
|
{
|
|
$areaDetails = array();
|
|
foreach ($areaData as $item) {
|
|
if (strpos($item['area_id'], $userAreaData[2]['cityid'])) {
|
|
$areaDetails[] = $item;
|
|
continue;
|
|
} elseif (strpos($item['area_id'], $userAreaData[1]['cityid'])) {
|
|
$areaDetails[] = $item;
|
|
continue;
|
|
} elseif (strpos($item['area_id'], $userAreaData[0]['cityid'])) {
|
|
$areaDetails[] = $item;
|
|
continue;
|
|
}
|
|
}
|
|
return $areaDetails;
|
|
}
|
|
|
|
|
|
/**
|
|
* 组合商品和配送区域费用问题
|
|
* @param $areaDetails
|
|
* @param $goodsData
|
|
* @return mixed
|
|
*/
|
|
function comGooodAndArea($areaDetails, $goodsData)
|
|
{
|
|
foreach ($goodsData as &$items) {
|
|
foreach ($areaDetails as $item) {
|
|
if ($items['temId'] == $item['template_id']) {
|
|
$items['areaData'] = $item;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
return $goodsData;
|
|
}
|
|
|
|
|
|
/**
|
|
* 获取配送费
|
|
* @param $goodAndTemData
|
|
* @return mixed
|
|
*/
|
|
function getDistributionFee($goodAndTemData)
|
|
{
|
|
$StoreConfigModel = new StoreConfigModel();
|
|
$storeConfigData = $StoreConfigModel->getStoreTemConfiguration($this->store_id);
|
|
if ($storeConfigData['delivery_template_status'] == 2) {
|
|
if ($storeConfigData['template_charging_type'] == 1) { //累加运费
|
|
$distributionFee = self::cumulativeFreightCalculation($goodAndTemData);
|
|
} elseif ($storeConfigData['template_charging_type'] == 2) {//组合运费
|
|
$distributionFee = self::combinedFreightCalculation($goodAndTemData);
|
|
}
|
|
}
|
|
return $distributionFee;
|
|
}
|
|
|
|
|
|
/**
|
|
* 累加运费计算
|
|
* @param $goodAndTemData
|
|
* @return int
|
|
*/
|
|
private function cumulativeFreightCalculation($goodAndTemData)
|
|
{
|
|
$count = array_count_values(array_column($goodAndTemData, "temId"));
|
|
$arrSize = sizeof($goodAndTemData);
|
|
if ($count[$goodAndTemData[0]['temId']] == $arrSize) {//判断商品使用的模板是否相同
|
|
$distributionFee = 0;
|
|
foreach ($goodAndTemData as $item) {
|
|
if ($item['areaData']['type'] == 1) { //免运费
|
|
$distributionFee = 0;
|
|
break;
|
|
} elseif ($item['areaData']['type'] == 2 || $item['areaData']['type'] == 3) { //按件数 || 按重量
|
|
$totalQuantity = 0; //商品总数量totalQuantity
|
|
foreach ($goodAndTemData as $items) {
|
|
$totalQuantity += $items['goodNum'];
|
|
}
|
|
$firstNum = $item['areaData']['first_num']; //首重
|
|
$appendNum = $item['areaData']['append_num']; //续重
|
|
$check = ($totalQuantity > $firstNum) ? 1 : 0;
|
|
if ($check) {
|
|
$merchant = ceil(($totalQuantity - $firstNum) / $appendNum);
|
|
$distributionFee = $item['areaData']['first_fee'] + $merchant * $item['areaData']['append_fee'];
|
|
break;
|
|
} else {
|
|
$distributionFee = $item['areaData']['first_fee'];
|
|
break;
|
|
}
|
|
} elseif ($item['areaData']['type'] == 4) {//统一运费
|
|
$distributionFee = $item['areaData']['unified_fee'];
|
|
break;
|
|
}
|
|
}
|
|
} else {
|
|
$freeArr = [];//免运费数组
|
|
$numArr = [];//按件数
|
|
$weightArr = [];//按重量
|
|
$unifiedArr = [];//统一运费
|
|
$freeFee = 0;//免运费
|
|
$numFee = 0; //按件收费
|
|
$weightFee = 0; //按重收费
|
|
$maxUnifiedFee = 0; //最大统一运费
|
|
foreach ($goodAndTemData as $goods) {
|
|
if ($goods['areaData']['type'] == 1) {
|
|
array_push($freeArr, $goods);
|
|
} elseif ($goods['areaData']['type'] == 2) {
|
|
array_push($numArr, $goods);
|
|
} elseif ($goods['areaData']['type'] == 3) {
|
|
array_push($weightArr, $goods);
|
|
} elseif ($goods['areaData']['type'] == 4) {
|
|
array_push($unifiedArr, $goods);
|
|
}
|
|
}
|
|
|
|
if (!empty($freeArr)) {
|
|
$freeFee = 0;
|
|
}
|
|
if (!empty($numArr)) {//按件数
|
|
foreach ($numArr as $num) {
|
|
$merchant = ceil(($num['goodNum'] - $num['areaData']['first_num']) / $num['areaData']['append_num']);
|
|
$count = ($num['goodNum'] > $num['areaData']['first_num']) ? 1 : 0;
|
|
if ($count) {
|
|
$numFee += $num['areaData']['first_fee'] + $merchant * $num['areaData']['append_fee'];
|
|
} else {
|
|
$numFee += $num['areaData']['first_fee'];
|
|
}
|
|
}
|
|
}
|
|
if (!empty($weightArr)) {//按重量
|
|
foreach ($weightArr as $weight) {
|
|
$merchant = ceil(($weight['goodNum'] - $weight['areaData']['first_num']) / $weight['areaData']['append_num']);
|
|
$count = ($weight['goodNum'] > $weight['areaData']['first_num']) ? 1 : 0;
|
|
if ($count) {
|
|
$weightFee += $weight['areaData']['first_fee'] + $merchant * $weight['areaData']['append_fee'];
|
|
} else {
|
|
$weightFee += $weight['areaData']['first_fee'];
|
|
}
|
|
}
|
|
}
|
|
if (!empty($unifiedArr)) {//统一运费
|
|
foreach ($unifiedArr as $unified) {
|
|
$maxUnifiedFee = ($maxUnifiedFee > $unified['areaData']['unified_fee']) ? $maxUnifiedFee : $unified['areaData']['unified_fee'];
|
|
}
|
|
}
|
|
$distributionFee = round($freeFee + $numFee + $weightFee + $maxUnifiedFee, 2);
|
|
}
|
|
return $distributionFee;
|
|
}
|
|
|
|
|
|
/**
|
|
* 组合运费计算
|
|
* @param $goodAndTemData
|
|
* @return int
|
|
*/
|
|
private function combinedFreightCalculation($goodAndTemData)
|
|
{
|
|
$count = array_count_values(array_column($goodAndTemData, "temId"));
|
|
$arrSize = sizeof($goodAndTemData);
|
|
if ($count[$goodAndTemData[0]['temId']] == $arrSize) {//判断商品使用的模板是否相同
|
|
foreach ($goodAndTemData as $item) {
|
|
if ($item['areaData']['type'] == 1) { //免运费
|
|
$distributionFee = 0;
|
|
break;
|
|
} elseif ($item['areaData']['type'] == 2 || $item['areaData']['type'] == 3) { //按件数 || 按重量
|
|
$totalQuantity = 0; //商品总数量
|
|
foreach ($goodAndTemData as $items) {
|
|
$totalQuantity += $items['goodNum'];
|
|
}
|
|
$firstNum = $item['areaData']['first_num']; //首重
|
|
$appendNum = $item['areaData']['append_num']; //续重
|
|
$check = ($totalQuantity > $firstNum) ? 1 : 0;
|
|
if ($check) {
|
|
$merchant = ceil(($totalQuantity - $firstNum) / $appendNum);
|
|
$distributionFee = $firstNum * $item['areaData']['first_fee'] + $merchant * $item['areaData']['append_fee'];
|
|
break;
|
|
}
|
|
} elseif ($item['areaData']['type'] == 4) {//统一运费
|
|
$distributionFee = $item['areaData']['unified_fee'];
|
|
break;
|
|
}
|
|
}
|
|
} else {
|
|
$freeArr = [];//免运费数组
|
|
$numArr = [];//按件数 | 按重量
|
|
$unifiedArr = [];//统一运费
|
|
// $freeFee = 0;//免运费
|
|
$comFee = 0; //组合运费
|
|
$maxUnifiedFee = 0; //最大统一运费
|
|
foreach ($goodAndTemData as $goods) {
|
|
if ($goods['areaData']['type'] == 1) {
|
|
array_push($freeArr, $goods);
|
|
} elseif ($goods['areaData']['type'] == 2 || $goods['areaData']['type'] == 3) {
|
|
array_push($numArr, $goods);
|
|
} elseif ($goods['areaData']['type'] == 4) {
|
|
array_push($unifiedArr, $goods);
|
|
}
|
|
}
|
|
// if (!empty($freeArr)){//免运费
|
|
// $freeFee = 0;
|
|
// }
|
|
if (!empty($numArr)) {//按件数 | 按重量
|
|
$maxFirstFee = 0;//最大首运费
|
|
foreach ($numArr as $num) {
|
|
$maxFirstFee = ($maxFirstFee > $num['areaData']['first_fee']) ? $maxFirstFee : $num['areaData']['first_fee'];
|
|
}
|
|
//根据最大续费获取最大续费当前模板的首件|首重 以及 续件|续重
|
|
$maxIdent = 0;//最大的标识
|
|
foreach ($numArr as $nums) {
|
|
if ($maxFirstFee == $nums['areaData']['first_fee'] && $maxIdent == 0) {
|
|
if ($nums['goodNum'] > $nums['areaData']['first_num']) {
|
|
$merchant = ceil(($nums['goodNum'] - $nums['areaData']['first_num']) / $nums['areaData']['append_num']);
|
|
$comFee += $nums['areaData']['first_fee'] + $merchant * $nums['areaData']['append_fee'];
|
|
} else {
|
|
$comFee += $nums['areaData']['first_fee'];
|
|
}
|
|
$maxIdent = 1;
|
|
} else {
|
|
$merchant = ceil($nums['goodNum'] / $nums['areaData']['append_num']);
|
|
$comFee += $merchant * $nums['areaData']['append_fee'];
|
|
}
|
|
}
|
|
}
|
|
if (!empty($unifiedArr)) {//统一运费
|
|
foreach ($unifiedArr as $unified) {
|
|
$maxUnifiedFee = ($maxUnifiedFee > $unified['areaData']['unified_fee']) ? $maxUnifiedFee : $unified['areaData']['unified_fee'];
|
|
}
|
|
}
|
|
$distributionFee = round(($comFee > $maxUnifiedFee) ? $comFee : $maxUnifiedFee, 2);
|
|
}
|
|
return $distributionFee;
|
|
}
|
|
|
|
|
|
/**
|
|
* 把配送模板ID相同的数据融合一起
|
|
* @param $goodAndTemData
|
|
* @return mixed
|
|
*/
|
|
function comGoodAndTemData($goodAndTemData)
|
|
{
|
|
for ($i = 0; $i < sizeof($goodAndTemData); $i++) {
|
|
for ($j = $i + 1; $j <= sizeof($goodAndTemData); $j++) {
|
|
if ($goodAndTemData[$i]['temId'] == $goodAndTemData[$j]['temId']) {
|
|
$goodAndTemData[$i]['goodNum'] += $goodAndTemData[$j]['goodNum'];
|
|
unset($goodAndTemData[$j]);
|
|
}
|
|
}
|
|
}
|
|
return $goodAndTemData;
|
|
}
|
|
|
|
|
|
} |