cxgj/models/common/integral/mall/CommonIntegralMallGoodsEditForm.php
2023-11-27 09:45:13 +08:00

133 lines
4.4 KiB
PHP

<?php
/**
* @author Any
* @description KISS
* @date 2021年10月12日
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\models\common\integral\mall;
use app\models\common\CommonGoodsEditForm;
use app\models\integral\mall\IntegralMallGoods;
use app\models\integral\mall\IntegralMallGoodsAttr;
use app\models\GoodsAttr;
use app\models\GoodsCat;
use app\models\Cat;
use app\models\Model;
use app\components\SiteHelper;
use app\components\SysConst;
use app\components\Serializer;
class CommonIntegralMallGoodsEditForm extends CommonGoodsEditForm
{
public $integral_num;
public function rules() {
$rules = [
[['integral_num'], 'integer', 'min' => 0],
[['integral_num'], 'required'],
];
return array_merge($rules, parent::rules());
}
public function attributeLabels() {
$labels = [
'integral_num' => '积分'
];
return array_merge($labels,parent::attributeLabels());
}
protected function saveExtra() {
$res = $this->saveIntegralGoods();
if($res['code'] != 0)
return $res;
$res = $this->saveGoodsAttr();
return $res;
}
//保存商品分类
protected function saveGoodsCat()
{
$type = Cat::TYPE_INTEGRAL_GOODS;
$goods_hub_id = $this->model->goods_hub_id;
GoodsCat::updateAll(['is_delete' => 1],['is_delete'=>0, 'goods_hub_id' => $goods_hub_id]);
foreach ($this->cat_ids as $cat_id)
{
//分类是否存在
$cat = Cat::findOne(['id' => $cat_id, 'is_delete' => 0, 'cx_mch_id' => $this->cx_mch_id, 'type' => $type]);
if($cat == null)
continue;
$goods_cat = GoodsCat::findOne(['goods_hub_id' => $goods_hub_id, 'cat_id' => $cat_id]);
if($goods_cat == null){
$goods_cat = new GoodsCat();
$goods_cat->goods_hub_id = $goods_hub_id;
$goods_cat->cat_id = $cat_id;
}
$goods_cat->is_delete = 0;
$goods_cat->save();
}
return $this->apiReturnSuccess();
}
private function saveIntegralGoods()
{
$model = IntegralMallGoods::findOne(['goods_id' => $this->model->id]);
if($model == null){
$model = new IntegralMallGoods();
$model->cx_mch_id = $this->cx_mch_id;
$model->goods_id = $this->model->id;
}
$model->integral_num = $this->model->use_attr == 1 ? $this->newAttrs[0]['integral_num'] : $this->integral_num;
$model->is_delete = 0;
$model->deleted_at = 0;
if(!$model->save()){
return $this->getModelError($model);
}
return $this->apiReturnSuccess();
}
private function saveGoodsAttr()
{
$attr = GoodsAttr::find()
->where([
'goods_id' => $this->model->id,
'is_delete' => 0
])
->select('id,sign_id')
->asArray()->all();
IntegralMallGoodsAttr::updateAll(['is_delete' => 1], ['goods_id' => $this->model->id, 'is_delete' => 0]);
foreach($attr as $index => $item){
$model = IntegralMallGoodsAttr::findOne(['goods_id' => $this->model->id, 'goods_attr_id' => $item['id']]);
if($model == null){
$model = new IntegralMallGoodsAttr();
$model->cx_mch_id = $this->cx_mch_id;
$model->goods_attr_id = $item['id'];
$model->goods_id = $this->model->id;
}
$integral_num = $this->integral_num;
foreach ($this->newAttrs as $j => $attr_item){
if($item['sign_id'] == $attr_item['sign_id'] && isset($attr_item['integral_num'])){
$integral_num = $attr_item['integral_num'];
if($integral_num < 0){
return $this->apiReturnError('积分值不能小于0');
}
break;
}
}
$model->integral_num = $integral_num;
$model->is_delete = 0;
if(!$model->save()){
return $this->getModelError($model);
}
}
return $this->apiReturnSuccess();
}
}