You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

139 lines
4.8 KiB

<?php
namespace backend\modules\shop\logic;
use backend\modules\shop\models\ars\City;
use backend\modules\shop\models\ars\ExpressArea;
use backend\modules\shop\models\ars\Province;
use backend\modules\shop\models\ars\ExpressTemplate;
class ShopManager
{
//单位类型
const UNIT_TYPE_WEIGHT = 1; //重量
const UNIT_TYPE_MONEY = 2; //金额
const UNIT_TYPE_ITEM = 3; //件
const UNIT_TYPE_LENGTH = 4; //长度,宽度,高度,直径
/**
* @param $type
* @return int
* 比例转换
*/
public static function proportionalConversion($type)
{
switch ($type) {
case self::UNIT_TYPE_WEIGHT:
return 1000; //后台显示为kg,数据库保存为g
break;
case self::UNIT_TYPE_MONEY:
return 100; //后台显示为元,数据库保存为分
break;
case self::UNIT_TYPE_ITEM:
return 1; //后台显示和数据库都为件
break;
case self::UNIT_TYPE_LENGTH:
return 1; //后台和数据库都为毫米
break;
default:
return 1;
}
}
/**
* @param ExpressArea|$expressAreaModel
* @param ExpressTemplate|$expressTemplateModel
* 区域运费模板按比例转化数据
*/
public static function expressAreaScaleDate($expressAreaModel, $expressTemplateModel)
{
$expressAreaModel->basic_price *= self::proportionalConversion(self::UNIT_TYPE_MONEY);
$expressAreaModel->extra_price *= self::proportionalConversion(self::UNIT_TYPE_MONEY);
if ($expressTemplateModel->calculation_type == ExpressTemplate::CALCULATION_TYPE_WEIGHT) {
$expressAreaModel->basic_count *= self::proportionalConversion(self::UNIT_TYPE_WEIGHT);
$expressAreaModel->extra_count *= self::proportionalConversion(self::UNIT_TYPE_WEIGHT);
} else {
$expressAreaModel->basic_count *= self::proportionalConversion(self::UNIT_TYPE_ITEM);
$expressAreaModel->extra_count *= self::proportionalConversion(self::UNIT_TYPE_ITEM);
}
}
/**
* @param ExpressArea|$expressAreaModel
* @return array
* 除去已被选取的城市,筛选剩下的城市数据
*/
public static function filterCity($expressAreaModel)
{
$query = ExpressArea::find()
->select(['city'])
->where(['express_template' => $expressAreaModel->express_template]);
if ($expressAreaModel->id) { //修改操作时,除去自身的城市
$query = $query->andWhere(['!=', 'id', $expressAreaModel->id]);
}
$allDate = $query->all();
$expressAresCityIdArr = self::filterSelectedCtiyIdArr($allDate);
$leftoverCityArr = self::filterLeftoverCityArr($expressAresCityIdArr);
return $leftoverCityArr;
}
/**
* @param $expressAreas
* @return array 已选的城市id数组
* 获取已选的城市id数组
*/
private static function filterSelectedCtiyIdArr($expressAreas)
{
$expressAresCityIdArr = [];
if ($expressAreas) {
foreach ($expressAreas as $expressAreaCity) {
$cityIdArr = explode(',', $expressAreaCity->city);
$expressAresCityIdArr = array_unique(array_merge($cityIdArr, $expressAresCityIdArr));
}
}
return $expressAresCityIdArr;
}
/**
* @param $expressAresCityIdArr
* @return array 未选的城市id数组
* 筛选剩下的城市
*/
private static function filterLeftoverCityArr($expressAresCityIdArr)
{
$data = [];
$provinces = Province::find()->cache(0)->all();
foreach ($provinces as $key => $province) {
$cities = City::find()
->where(['province_id' => $province->province_id])
->andWhere(['not in', 'city_id', $expressAresCityIdArr])
->all();
if ($cities) {
$data[$key]['province'] = $province->name;
foreach ($cities as $city) {
$data[$key]['city'][] = ['id' => $city->city_id, 'name' => $city->name];
}
}
}
return array_values($data);
}
/**
* 处理所选区域数据
* @param $area
* @param ExpressArea|$expressAreaModel
* @param ExpressTemplate|$expressTemplateModel
* @return bool
*/
public static function dealAreaInExpressArea($area, $expressAreaModel, $expressTemplateModel)
{
$cityIds = array_keys($area);
$data['city'] = implode(',', $cityIds);
$expressAreaModel->load($data, '');
self::expressAreaScaleDate($expressAreaModel, $expressTemplateModel); //按比例转换数据
$expressAreaModel->save();
return true;
}
}