324 lines
12 KiB
PHP
324 lines
12 KiB
PHP
<?php
|
||
|
||
namespace app\models;
|
||
|
||
use Yii;
|
||
use app\models\integral\mall\IntegralMallGoods;
|
||
|
||
/**
|
||
* This is the model class for table "{{%goods}}".
|
||
*
|
||
* @property int $id ID
|
||
* @property int $cx_mch_id 平台商户ID
|
||
* @property int $goods_hub_id 商品库ID
|
||
* @property int $status 上架状态:0=下架,1=上架
|
||
* @property float $price 售价
|
||
* @property int $use_attr 是否使用规格:0=不使用,1=使用
|
||
* @property string $attr_groups 商品规格组
|
||
* @property int $goods_stock 商品库存
|
||
* @property int $virtual_sales 已出售量
|
||
* @property int $confine_count 购物数量限制,0=不限制
|
||
* @property int $freight_id 运费模板ID
|
||
* @property string|null $plugin_sign 商品标示用于区分商品属于什么模块
|
||
* @property int $sort 排序
|
||
* @property int $created_at 添加时间
|
||
* @property int $updated_at 更新时间
|
||
* @property int $deleted_at 删除时间
|
||
* @property int $is_delete 是否删除,0=否,1=是
|
||
* @property int $payment_people 支付人数
|
||
* @property int $payment_num 支付件数
|
||
* @property float $payment_amount 支付金额
|
||
* @property int $payment_order 支付订单数
|
||
* @property int $sales 商品实际销量
|
||
* @property int $viewed_count 详情浏览量统计
|
||
*/
|
||
class Goods extends \yii\db\ActiveRecord
|
||
{
|
||
const STATUS_ONLINE = 1;
|
||
const STATUS_OFFLINE = 0;
|
||
|
||
const STOCK_INCREASE = 1; //库存增加
|
||
const STOCK_DECREASE = 2; //库存减少
|
||
|
||
public $goods_no; //商品货号
|
||
|
||
|
||
/**
|
||
* {@inheritdoc}
|
||
*/
|
||
public static function tableName()
|
||
{
|
||
return '{{%goods}}';
|
||
}
|
||
|
||
/**
|
||
* {@inheritdoc}
|
||
*/
|
||
public function rules()
|
||
{
|
||
return [
|
||
[['attr_groups', 'plugin_sign'], 'trim'],
|
||
[['cx_mch_id', 'goods_hub_id', 'status', 'use_attr', 'goods_stock', 'virtual_sales', 'confine_count', 'freight_id', 'sort', 'created_at', 'updated_at', 'deleted_at', 'is_delete', 'payment_people', 'payment_num', 'payment_order', 'sales', 'viewed_count'], 'integer'],
|
||
[['goods_hub_id', 'attr_groups'], 'required'],
|
||
[['price', 'payment_amount'], 'number'],
|
||
[['attr_groups'], 'string'],
|
||
[['plugin_sign'], 'string', 'max' => 255],
|
||
];
|
||
}
|
||
|
||
/**
|
||
* {@inheritdoc}
|
||
*/
|
||
public function attributeLabels()
|
||
{
|
||
return [
|
||
'id' => 'ID',
|
||
'cx_mch_id' => '平台商户ID',
|
||
'goods_hub_id' => '商品库ID',
|
||
'status' => '上架状态:0=下架,1=上架',
|
||
'price' => '售价',
|
||
'use_attr' => '是否使用规格:0=不使用,1=使用',
|
||
'attr_groups' => '商品规格组',
|
||
'goods_stock' => '商品库存',
|
||
'virtual_sales' => '已出售量',
|
||
'confine_count' => '购物数量限制,0=不限制',
|
||
'freight_id' => '运费模板ID',
|
||
'plugin_sign' => '商品标示用于区分商品属于什么模块',
|
||
'sort' => '排序',
|
||
'created_at' => '添加时间',
|
||
'updated_at' => '更新时间',
|
||
'deleted_at' => '删除时间',
|
||
'is_delete' => '是否删除,0=否,1=是',
|
||
'payment_people' => '支付人数',
|
||
'payment_num' => '支付件数',
|
||
'payment_amount' => '支付金额',
|
||
'payment_order' => '支付订单数',
|
||
'sales' => '商品实际销量',
|
||
'viewed_count' => '详情浏览量统计',
|
||
];
|
||
}
|
||
|
||
|
||
public function beforeSave($insert) {
|
||
if(parent::beforeSave($insert)){
|
||
if($this->isNewRecord){
|
||
$this->created_at = time();
|
||
}
|
||
$this->updated_at = time();
|
||
if($this->is_delete == 1)
|
||
$this->deleted_at = time();
|
||
return true;
|
||
} else {
|
||
return false;
|
||
}
|
||
}
|
||
|
||
//获取仓库商品
|
||
public function getGoodsHub()
|
||
{
|
||
return $this->hasOne(GoodsHub::className(), ['id' => 'goods_hub_id'])->where(['is_delete' => 0]);
|
||
}
|
||
|
||
//获取积分商品
|
||
public function getIntegralMallGoods()
|
||
{
|
||
return $this->hasOne(IntegralMallGoods::className(), ['goods_id' => 'id'])->where(['is_delete' => 0]);
|
||
}
|
||
|
||
//浏览事件
|
||
public static function handleViewEvent($goods_id, $cx_mch_id=0)
|
||
{
|
||
$goods = Goods::findOne(['id' => $goods_id, 'is_delete' => 0, 'cx_mch_id' => $cx_mch_id]);
|
||
if($goods == null){
|
||
return Model::asReturnError('商品不存在');
|
||
}
|
||
$goods->viewed_count += 1;
|
||
if(!$goods->save()){
|
||
return (new Model())->getModelError($goods);
|
||
}
|
||
return Model::asReturnSuccess();
|
||
}
|
||
|
||
/**
|
||
* 库存增减
|
||
* @param integer $goods_id 商品ID
|
||
* @param integer $goods_attr_id 商品规格ID
|
||
* @param integer $num 数量
|
||
* @param integer $type 类型,1=增加,2=减少
|
||
*/
|
||
public static function handleStock($goods_id, $goods_attr_id, $num, $type = 1, $cx_mch_id = 0)
|
||
{
|
||
$goods = Goods::findOne(['id' => $goods_id, 'is_delete' => 0, 'cx_mch_id' => $cx_mch_id]);
|
||
if($goods == null){
|
||
return Model::asReturnError('商品不存在');
|
||
}
|
||
$goods_attr = GoodsAttr::findOne(['goods_id' => $goods_id, 'id' => $goods_attr_id, 'is_delete' => 0, 'cx_mch_id' => $cx_mch_id]);
|
||
if($goods_attr == null){
|
||
return Model::asReturnError('商品规格不存在');
|
||
}
|
||
$goods_attr->stock += $type == 1 ? $num : $num * -1;
|
||
if(!$goods_attr->save()){
|
||
return (new Model())->getModelError($goods_attr);
|
||
}
|
||
$goods_stock = GoodsAttr::find()
|
||
->where([
|
||
'goods_id' => $goods_id,
|
||
'is_delete' => 0,
|
||
'cx_mch_id' => $cx_mch_id
|
||
])
|
||
->sum('stock');
|
||
$goods->goods_stock = $goods_stock;
|
||
if(!$goods->save()){
|
||
return (new Model())->getModelError($goods);
|
||
}
|
||
return Model::asReturnSuccess();
|
||
}
|
||
|
||
|
||
//处理订单付款后
|
||
public static function handleOrderPayAfter($order_id, $cx_mch_id = 0)
|
||
{
|
||
$order = Order::findOne(['id' => $order_id, 'is_delete' => 0, 'is_pay' => 1, 'cancel_status' => 0, 'cx_mch_id' => $cx_mch_id]);
|
||
if($order == null){
|
||
return Model::asReturnError('订单不存在');
|
||
}
|
||
//订单详情
|
||
$order_details = OrderDetail::find()
|
||
->where([
|
||
'order_id' => $order_id,
|
||
'is_delete' => 0,
|
||
'cx_mch_id' => $cx_mch_id,
|
||
'is_refund' => 0
|
||
])
|
||
->select('goods_id,num,unit_price,total_original_price,total_price,goods_attr_info')
|
||
->asArray()->all();
|
||
$payment_people = 1;
|
||
$payment_order = 1;
|
||
$details = [];
|
||
foreach($order_details as $order_detail){
|
||
$goods_id = $order_detail['goods_id'];
|
||
if(!isset($details[$goods_id])){
|
||
$details[$goods_id] = [
|
||
'goods_id' => $goods_id,
|
||
'payment_num' => $order_detail['num'],
|
||
'sales' => $order_detail['num'],
|
||
'payment_amount' => $order_detail['total_price']
|
||
];
|
||
} else {
|
||
$details[$goods_id]['payment_num'] += $order_detail['num'];
|
||
$details[$goods_id]['sales'] += $order_detail['num'];
|
||
$details[$goods_id]['payment_amount'] += $order_detail['total_price'];
|
||
}
|
||
}
|
||
foreach ($details as $detail){
|
||
$goods = Goods::findOne(['id' => $detail['goods_id'], 'is_delete' => 0, 'cx_mch_id' => $cx_mch_id]);
|
||
if($goods == null)
|
||
continue;
|
||
$goods->payment_amount += $detail['payment_amount'];
|
||
$goods->payment_num += $detail['payment_num'];
|
||
$goods->sales += $detail['sales'];
|
||
$goods->payment_order += $payment_order;
|
||
$goods->payment_people += $payment_people;
|
||
if(!$goods->save()){
|
||
return (new Model())->getModelError($goods);
|
||
}
|
||
}
|
||
return Model::asReturnSuccess();
|
||
}
|
||
|
||
//处理订单退款后
|
||
public static function handleOrderRefundAfter($order_id, $cx_mch_id = 0)
|
||
{
|
||
$order = Order::findOne(['id' => $order_id, 'is_delete' => 0, 'is_pay' => 1, 'cancel_status' => [0,1], 'cx_mch_id' => $cx_mch_id]);
|
||
if($order == null){
|
||
return Model::asReturnError('订单不存在');
|
||
}
|
||
//订单详情
|
||
$order_details = OrderDetail::find()
|
||
->where([
|
||
'order_id' => $order_id,
|
||
'is_delete' => 0,
|
||
'cx_mch_id' => $cx_mch_id,
|
||
'is_refund' => 1
|
||
])
|
||
->select('goods_id,num,unit_price,total_original_price,total_price,goods_attr_info')
|
||
->asArray()->all();
|
||
$payment_people = 1;
|
||
$payment_order = 1;
|
||
$details = [];
|
||
foreach($order_details as $order_detail){
|
||
$goods_id = $order_detail['goods_id'];
|
||
if(!isset($details[$goods_id])){
|
||
$details[$goods_id] = [
|
||
'goods_id' => $goods_id,
|
||
'payment_num' => 0,
|
||
'sales' => 0,
|
||
'payment_amount' => 0
|
||
];
|
||
} else {
|
||
$details[$goods_id]['payment_num'] += $order_detail['num'];
|
||
$details[$goods_id]['sales'] += $order_detail['num'];
|
||
$details[$goods_id]['payment_amount'] += $order_detail['total_price'];
|
||
}
|
||
}
|
||
foreach ($details as $detail){
|
||
$goods = Goods::findOne(['id' => $detail['goods_id'], 'is_delete' => 0, 'cx_mch_id' => $cx_mch_id]);
|
||
if($goods == null)
|
||
continue;
|
||
$goods->payment_amount -= $detail['payment_amount'];
|
||
$goods->payment_num -= $detail['payment_num'];
|
||
$goods->sales -= $detail['sales'];
|
||
$goods->payment_order -= $payment_order;
|
||
$goods->payment_people -= $payment_people;
|
||
if(!$goods->save()){
|
||
return (new Model())->getModelError($goods);
|
||
}
|
||
}
|
||
return Model::asReturnSuccess();
|
||
}
|
||
|
||
|
||
//获取商品规格信息
|
||
public static function getGoodsAttrInfoListBySignId($sign_id, $attr_groups)
|
||
{
|
||
if(empty($attr_groups))
|
||
return [];
|
||
$attr_info_list = [];
|
||
$sign_ids = explode(':', $sign_id);
|
||
foreach ($sign_ids as $attr_id){
|
||
$attr_info = self::getGoodsAttrInfoByAttrId($attr_id, $attr_groups);
|
||
if(!empty($attr_info)){
|
||
$attr_info_list[] = $attr_info;
|
||
}
|
||
}
|
||
return $attr_info_list;
|
||
}
|
||
|
||
public static function getGoodsAttrInfoByAttrId($attr_id, $attr_groups)
|
||
{
|
||
$attr_info = [];
|
||
foreach ($attr_groups as $i => $attr_group){
|
||
foreach ($attr_group['attr_list'] as $j => $attr ){
|
||
if($attr['attr_id'] == $attr_id){
|
||
$attr_info['attr_group_id'] = $attr_group['attr_group_id'];
|
||
$attr_info['attr_group_name'] = $attr_group['attr_group_name'];
|
||
$attr_info['attr_id'] = $attr['attr_id'];
|
||
$attr_info['attr_name'] = $attr['attr_name'];
|
||
return $attr_info;
|
||
}
|
||
}
|
||
}
|
||
return $attr_info;
|
||
}
|
||
|
||
public static function getGoodsAttrInfoToString($sign_id, $attr_groups, $delimiter, $default = "默认")
|
||
{
|
||
$attr_info_list = self::getGoodsAttrInfoListBySignId($sign_id, $attr_groups);
|
||
if(empty($attr_info_list))
|
||
return $default;
|
||
return implode($delimiter, array_column($attr_info_list, 'attr_name'));
|
||
}
|
||
|
||
}
|
||
|