This commit is contained in:
尖叫 2024-02-02 10:31:17 +08:00
parent faa2bb5777
commit eedf770a8d
3 changed files with 68 additions and 66 deletions

View File

@ -37,10 +37,10 @@ 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; //商品货号
@ -61,8 +61,8 @@ class Goods extends \yii\db\ActiveRecord
[['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'],
[['payment_amount'], 'number'],
[['attr_groups', 'price'], 'string'],
[['plugin_sign'], 'string', 'max' => 255],
];
}
@ -98,22 +98,23 @@ class Goods extends \yii\db\ActiveRecord
'viewed_count' => '详情浏览量统计',
];
}
public function beforeSave($insert) {
if(parent::beforeSave($insert)){
if($this->isNewRecord){
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();
if ($this->is_delete == 1)
$this->deleted_at = time();
return true;
} else {
return false;
}
}
}
//获取仓库商品
public function getGoodsHub()
{
@ -130,21 +131,21 @@ class Goods extends \yii\db\ActiveRecord
{
return $this->hasOne(IntegralMallGoods::className(), ['goods_id' => 'id'])->where(['is_delete' => 0]);
}
//浏览事件
public static function handleViewEvent($goods_id, $cx_mch_id=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){
if ($goods == null) {
return Model::asReturnError('商品不存在');
}
$goods->viewed_count += 1;
if(!$goods->save()){
if (!$goods->save()) {
return (new Model())->getModelError($goods);
}
return Model::asReturnSuccess();
}
/**
* 库存增减
* @param integer $goods_id 商品ID
@ -155,37 +156,37 @@ class Goods extends \yii\db\ActiveRecord
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){
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){
if ($goods_attr == null) {
return Model::asReturnError('商品规格不存在');
}
}
$goods_attr->stock += $type == 1 ? $num : $num * -1;
if(!$goods_attr->save()){
if (!$goods_attr->save()) {
return (new Model())->getModelError($goods_attr);
}
$goods_stock = GoodsAttr::find()
->where([
'goods_id' => $goods_id,
'is_delete' => 0,
'goods_id' => $goods_id,
'is_delete' => 0,
'cx_mch_id' => $cx_mch_id
])
->sum('stock');
$goods->goods_stock = $goods_stock;
if(!$goods->save()){
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){
if ($order == null) {
return Model::asReturnError('订单不存在');
}
//订单详情
@ -201,9 +202,9 @@ class Goods extends \yii\db\ActiveRecord
$payment_people = 1;
$payment_order = 1;
$details = [];
foreach($order_details as $order_detail){
foreach ($order_details as $order_detail) {
$goods_id = $order_detail['goods_id'];
if(!isset($details[$goods_id])){
if (!isset($details[$goods_id])) {
$details[$goods_id] = [
'goods_id' => $goods_id,
'payment_num' => $order_detail['num'],
@ -216,27 +217,27 @@ class Goods extends \yii\db\ActiveRecord
$details[$goods_id]['payment_amount'] += $order_detail['total_price'];
}
}
foreach ($details as $detail){
foreach ($details as $detail) {
$goods = Goods::findOne(['id' => $detail['goods_id'], 'is_delete' => 0, 'cx_mch_id' => $cx_mch_id]);
if($goods == null)
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()){
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){
$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('订单不存在');
}
//订单详情
@ -252,9 +253,9 @@ class Goods extends \yii\db\ActiveRecord
$payment_people = 1;
$payment_order = 1;
$details = [];
foreach($order_details as $order_detail){
foreach ($order_details as $order_detail) {
$goods_id = $order_detail['goods_id'];
if(!isset($details[$goods_id])){
if (!isset($details[$goods_id])) {
$details[$goods_id] = [
'goods_id' => $goods_id,
'payment_num' => 0,
@ -267,45 +268,45 @@ class Goods extends \yii\db\ActiveRecord
$details[$goods_id]['payment_amount'] += $order_detail['total_price'];
}
}
foreach ($details as $detail){
foreach ($details as $detail) {
$goods = Goods::findOne(['id' => $detail['goods_id'], 'is_delete' => 0, 'cx_mch_id' => $cx_mch_id]);
if($goods == null)
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()){
if (!$goods->save()) {
return (new Model())->getModelError($goods);
}
}
}
return Model::asReturnSuccess();
return Model::asReturnSuccess();
}
//获取商品规格信息
public static function getGoodsAttrInfoListBySignId($sign_id, $attr_groups)
{
if(empty($attr_groups))
if (empty($attr_groups))
return [];
$attr_info_list = [];
$sign_ids = explode(':', $sign_id);
foreach ($sign_ids as $attr_id){
foreach ($sign_ids as $attr_id) {
$attr_info = self::getGoodsAttrInfoByAttrId($attr_id, $attr_groups);
if(!empty($attr_info)){
if (!empty($attr_info)) {
$attr_info_list[] = $attr_info;
}
}
}
return $attr_info_list;
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){
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'];
@ -314,16 +315,16 @@ class Goods extends \yii\db\ActiveRecord
}
}
}
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))
if (empty($attr_info_list))
return $default;
return implode($delimiter, array_column($attr_info_list, 'attr_name'));
}
}

View File

@ -72,16 +72,16 @@ class CommonGoodsEditForm extends Model
{
return [
[['name', 'subtitle', 'detail', 'cover_pic', 'video_url', 'unit', 'plugin_sign', 'goods_no',], 'trim'],
[['name', 'subtitle', 'detail', 'cover_pic', 'video_url', 'video_banner_urls', 'banner_urls', 'unit', 'plugin_sign', 'goods_no', 'date', 'signing_head_img', 'signing_foot_img'], 'string'],
[['original_price', 'cost_price', 'price', 'goods_weight'], 'number'],
[['name', 'subtitle', 'detail', 'price', 'cover_pic', 'video_url', 'video_banner_urls', 'banner_urls', 'unit', 'plugin_sign', 'goods_no', 'date', 'signing_head_img', 'signing_foot_img'], 'string'],
[['original_price', 'cost_price', 'goods_weight'], 'number'],
[['type', 'use_attr', 'goods_stock', 'virtual_sales', 'confine_count', 'freight_id', 'sort', 'cx_mch_id'], 'integer'],
[['cat_id', 'cat1_id', 'cat2_id', 'cat3_id', 'cat4_id',], 'integer'],
[['pic_urls', 'cat_ids', 'model', 'attr', 'attrGroups'], 'safe'],
[['cost_price', 'original_price', 'price'], 'number', 'min' => 0],
[['cost_price', 'original_price', 'price'], 'number', 'max' => 9999999],
[['cost_price', 'original_price'], 'number', 'min' => 0],
[['cost_price', 'original_price'], 'number', 'max' => 9999999],
[['sort', 'virtual_sales', 'freight_id'], 'default', 'value' => 100],
[['confine_count', 'use_attr'], 'default', 'value' => 0],
[['name', 'price', 'cover_pic', 'type', 'plugin_sign', 'banner_urls', 'model', 'cx_mch_id'], 'required'],
@ -124,6 +124,7 @@ class CommonGoodsEditForm extends Model
$this->model->cx_mch_id = $this->cx_mch_id;
$this->model->plugin_sign = $this->plugin_sign;
}
$this->model->price = $this->price;
$this->model->goods_stock = empty($this->goods_stock) ? 9999 : $this->goods_stock; // 商品库存
$this->model->virtual_sales = $this->virtual_sales;
@ -185,8 +186,8 @@ class CommonGoodsEditForm extends Model
}
$goods_hub->name = $this->name;
$goods_hub->subtitle = $this->subtitle;
$goods_hub->original_price = $this->original_price ? $this->original_price : $this->price;
$goods_hub->cost_price = $this->cost_price ? $this->cost_price : $this->price;
// $goods_hub->original_price = $this->original_price ? $this->original_price : $this->price;
// $goods_hub->cost_price = $this->cost_price ? $this->cost_price : $this->price;
$goods_hub->detail = $this->detail;
$goods_hub->cover_pic = $this->cover_pic;
$goods_hub->signing_head_img = $this->signing_head_img;

View File

@ -154,7 +154,7 @@ $this->params['breadcrumbs'][] = $this->title;
<div class="layui-form-item">
<label class="layui-form-label required">签约价格</label>
<div class="layui-input-inline">
<input type="number" name="price" placeholder="请输入售价" autocomplete="off"
<input type="subtitle" name="price" placeholder="请输入售价" autocomplete="off"
class="layui-input" value="<?= $model->price ?>" lay-verify="required">
</div>
<div class="layui-form-mid layui-word-aux"></div>