admin/plugins/weight-value/src/models/GoodsWeightValue.php

79 lines
3.9 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 Yunshop\WeightValue\models;
use app\common\models\BaseModel;
use app\frontend\models\Goods;
class GoodsWeightValue extends BaseModel{
public $table = 'yz_goods_weight_value';
public $timestamps = false;
public static function relationSave($goodsId, $data, $operate,$isTeamDividend = false){
if (!$goodsId) return false;
if (!$data) return false;
$info = self::getModel($goodsId, $operate, $isTeamDividend);
// 判断deleted
if ($operate == 'deleted') return $info->delete();
// 公共
$info->uniacid = \YunShop::app()->uniacid;
$info->goods_id = $isTeamDividend ? 0 : $goodsId;// 经销商设置true goods_id = 0否则为goodsId
$info->team_dividend_level_id = $isTeamDividend ? $goodsId : 0;// 经销商设置true team_dividend_level_id = goodsId否则为0
$info->is_open = $data['is_open'] ?? 0;// 是否开启购买商品赠送权重值0=未开启1=开启
$info->is_self_purchase = $data['is_self_purchase'] ?? 0;// 是否开启自购0=不开启1=开启
// 经纪人
$info->gift_type = $data['gift_type'] ?? 0;// 赠送方式0=赠送固定值1=阶梯值赠送
$info->lv_type = $data['lv_type'] ?? 0;// 赠送方式0=二级1=三级
$info->calculation_type = $data['calculation_type'] ?? 0;// 计算类型0=固定值1=支付比例
$info->quantity = $data['quantity'] ?? 0;// 固定值赠送数量
foreach($data['ladder'] as $ladderIndex => $ladderItem){
if((float)$ladderItem['where'] <= 0 || (float)$ladderItem['num'] <= 0) unset($data['ladder'][$ladderIndex]);
}
$info->ladder = json_encode($data['ladder'] ?? []);// 阶梯赠送信息
// 代理商
$info->agent_gift_type = $data['agent_gift_type'] ?? 0;// 赠送方式0=赠送固定值1=阶梯值赠送
$info->agent_calculation_type = $data['agent_calculation_type'] ?? 0;// 计算类型0=固定值1=支付比例
$info->agent_quantity = $data['agent_quantity'] ?? 0;// 固定值赠送数量
foreach($data['agent_ladder'] as $ladderIndex => $ladderItem){
if((float)$ladderItem['where'] <= 0 || (float)$ladderItem['num'] <= 0) unset($data['agent_ladder'][$ladderIndex]);
}
$info->agent_ladder = json_encode($data['agent_ladder'] ?? []);// 阶梯赠送信息
// 省公司
$info->province_gift_type = $data['province_gift_type'] ?? 0;// 赠送方式0=赠送固定值1=阶梯值赠送
$info->province_calculation_type = $data['province_calculation_type'] ?? 0;// 计算类型0=固定值1=支付比例
$info->province_quantity = $data['province_quantity'] ?? 0;// 固定值赠送数量
foreach($data['province_ladder'] as $ladderIndex => $ladderItem){
if((float)$ladderItem['where'] <= 0 || (float)$ladderItem['num'] <= 0) unset($data['province_ladder'][$ladderIndex]);
}
$info->province_ladder = json_encode($data['province_ladder'] ?? []);// 阶梯赠送信息
return $info->save();
}
public static function getModel($goodsId, $operate,$isTeamDividend = false){
$model = false;
// 商品设置
if ($operate != 'created') {
if($isTeamDividend) $model = static::where(['team_dividend_level_id' => $goodsId])->first();// 经销商设置
else $model = static::where(['goods_id' => $goodsId])->first();// 经销商设置
}
!$model && $model = new static;
return $model;
}
public function getGoodsSet($goodsId,$isTeamDividend = false){
if($isTeamDividend) return self::where('team_dividend_level_id', $goodsId);
return self::where('goods_id', $goodsId);
}
//关联商品信息
public function belongsToGoods(){
return $this->belongsTo(Goods::class, 'goods_id', 'id');
}
}