cxgj/models/common/CommonGoodsActionForm.php
2023-11-30 15:32:32 +08:00

237 lines
7.8 KiB
PHP

<?php
/**
* @author Any
* @description KISS
* @date 2021年10月5日
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\models\common;
use app\models\Goods;
use app\models\GoodsAttr;
use app\models\GoodsHub;
use app\models\integral\mall\IntegralMallGoods;
use app\models\Model;
use yii\data\Pagination;
use app\components\SiteHelper;
use app\components\SysConst;
class CommonGoodsActionForm extends Model
{
public $goods_id;
public $cx_mch_id;
public $plugin_sign;
public function rules()
{
return [
[['cx_mch_id',], 'integer'],
[['plugin_sign'], 'string'],
[['goods_id',], 'safe'],
[['goods_id', 'cx_mch_id'], 'required'],
];
}
public function delete()
{
if (!$this->validate()) {
return $this->getModelError();
}
if (is_array($this->goods_id)) {
foreach ($this->goods_id as $goods_id) {
$t = \Yii::$app->db->beginTransaction();
$model = Goods::findOne([
'plugin_sign' => $this->plugin_sign,
'id' => $goods_id,
'is_delete' => 0,
'status' => Goods::STATUS_OFFLINE,
'cx_mch_id' => $this->cx_mch_id
]);
if ($model == null)
continue;
$model->is_delete = 1;
//删除之后
$this->doDeleteAfter($model->id, $model->plugin_sign, $model->cx_mch_id);
if (!$model->save()) {
$t->rollBack();
return $this->getModelError($model);
}
$t->commit();
}
} else {
$t = \Yii::$app->db->beginTransaction();
$model = Goods::findOne([
// 'plugin_sign' => $this->plugin_sign,
'id' => $this->goods_id,
'is_delete' => 0,
'status' => Goods::STATUS_OFFLINE,
// 'cx_mch_id' => $this->cx_mch_id
]);
if ($model == null) {
return $this->apiReturnError('冠军不存在或已经删除');
}
$model->is_delete = 1;
//删除之后
$this->doDeleteAfter($model->id, $model->plugin_sign, $model->cx_mch_id);
if (!$model->save()) {
$t->rollBack();
return $this->getModelError($model);
}
$t->commit();
}
return $this->apiReturnSuccess("删除成功");
}
public function online()
{
if (!$this->validate()) {
return $this->getModelError();
}
if (is_array($this->goods_id)) {
foreach ($this->goods_id as $goods_id) {
$t = \Yii::$app->db->beginTransaction();
$model = Goods::findOne([
// 'plugin_sign' => $this->plugin_sign,
'id' => $goods_id,
'is_delete' => 0,
'status' => Goods::STATUS_OFFLINE,
'cx_mch_id' => $this->cx_mch_id
]);
if ($model == null)
continue;
$model->status = Goods::STATUS_ONLINE;
//检查冠军库存是否充足
// $res = $this->checkGoodsStock($model->id, $model->cx_mch_id);
// if ($res['code'] != 0) {
// $t->rollBack();
// return $res;
// }
if (!$model->save()) {
$t->rollBack();
return $this->getModelError($model);
}
$t->commit();
}
} else {
$t = \Yii::$app->db->beginTransaction();
$model = Goods::findOne([
// 'plugin_sign' => $this->plugin_sign,
'id' => $this->goods_id,
'is_delete' => 0,
'status' => Goods::STATUS_OFFLINE,
'cx_mch_id' => $this->cx_mch_id
]);
if ($model == null) {
return $this->apiReturnError('冠军不存在或已经上架');
}
$model->status = Goods::STATUS_ONLINE;
//检查冠军库存是否充足
// $res = $this->checkGoodsStock($model->id, $model->cx_mch_id);
// if ($res['code'] != 0) {
// $t->rollBack();
// return $res;
// }
if (!$model->save()) {
$t->rollBack();
return $this->getModelError($model);
}
$t->commit();
}
return $this->apiReturnSuccess("操作成功");
}
public function offline()
{
if (!$this->validate()) {
return $this->getModelError();
}
if (is_array($this->goods_id)) {
foreach ($this->goods_id as $goods_id) {
$t = \Yii::$app->db->beginTransaction();
$model = Goods::findOne([
// 'plugin_sign' => $this->plugin_sign,
'id' => $goods_id,
'is_delete' => 0,
'status' => Goods::STATUS_ONLINE,
'cx_mch_id' => $this->cx_mch_id
]);
if ($model == null)
continue;
$model->status = Goods::STATUS_OFFLINE;
if (!$model->save()) {
$t->rollBack();
return $this->getModelError($model);
}
$t->commit();
}
} else {
$t = \Yii::$app->db->beginTransaction();
$model = Goods::findOne([
// 'plugin_sign' => $this->plugin_sign,
'id' => $this->goods_id,
'is_delete' => 0,
'status' => Goods::STATUS_ONLINE,
'cx_mch_id' => $this->cx_mch_id
]);
if ($model == null) {
return $this->apiReturnError('冠军不存在或已经下架');
}
$model->status = Goods::STATUS_OFFLINE;
if (!$model->save()) {
$t->rollBack();
return $this->getModelError($model);
}
$t->commit();
}
return $this->apiReturnSuccess("操作成功");
}
//冠军删除之后处理其他
private function doDeleteAfter($goods_id, $plugin_sign, $cx_mch_id)
{
if ($plugin_sign == SysConst::$cxPluginSceneIntegralMall) {
//积分冠军
IntegralMallGoods::updateAll(['is_delete' => 1], [
'is_delete' => 0,
'goods_id' => $goods_id,
'cx_mch_id' => $cx_mch_id
]);
}
}
//检查冠军库存是否充足
private function checkGoodsStock($goods_id, $cx_mch_id)
{
$goods = Goods::findOne(['id' => $goods_id, 'is_delete' => 0, 'cx_mch_id' => $cx_mch_id]);
if ($goods == null) {
return $this->apiReturnError('冠军不存在或已经删除');
}
if ($goods->use_attr != 1) {
if ($goods->goods_stock == 0) {
return $this->apiReturnError('冠军库存不足');
}
} else {
$stock = GoodsAttr::find()
->where([
'goods_id' => $goods_id,
'cx_mch_id' => $cx_mch_id,
'is_delete' => 0,
])
->sum('stock');
if ($stock == 0) {
return $this->apiReturnError('冠军库存不足');
}
}
return $this->apiReturnSuccess();
}
}