| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- <?php
- namespace common\models\goods;
- use common\enums\StatusEnum;
- use addons\ScoreMall\common\models\ScoreMallGoodsAttr;
- use common\models\common\Addons;
- use Exception;
- use Yii;
- /**
- * This is the model class for table "{{%goods_attr}}".
- *
- * @property int $id
- * @property int $goods_id 商品ID
- * @property string $name 规格名称
- * @property string $sign_id 规格ID标识
- * @property int $stock 库存
- * @property float $price 价格
- * @property float $original_price 原价
- * @property float $cost_price 成本价
- * @property string $goods_no 货号
- * @property int $weight 重量(克)
- * @property string $pic_url 规格图片
- * @property int $version 版本
- * @property int $sort 排序
- * @property int $created_at 创建时间
- * @property int $updated_at 修改时间
- * @property int $is_show 是否显示[0隐藏; 1显示]
- */
- class GoodsAttr extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%goods_attr}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['goods_id', 'stock', 'status', 'version', 'sort', 'created_at', 'updated_at', 'is_show', 'is_show_code'], 'integer'],
- [['price', 'original_price', 'cost_price', 'weight'], 'number'],
- [['sign_id', 'pic_url', 'spec_no'], 'string', 'max' => 255],
- [['name'], 'string', 'max' => 500],
- [['goods_no'], 'safe'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'goods_id' => '商品ID',
- 'name' => '规格名称',
- 'sign_id' => '规格ID标识',
- 'stock' => '库存',
- 'price' => '价格',
- 'original_price' => '原价',
- 'cost_price' => '成本价',
- 'goods_no' => '货号',
- 'weight' => '重量(克)',
- 'pic_url' => '规格图片',
- 'status' => '状态[1正常 -1删除]',
- 'version' => '版本',
- 'sort' => '排序',
- 'created_at' => '创建时间',
- 'updated_at' => '修改时间',
- 'is_show' => '是否显示[0隐藏; 1显示]'
- ];
- }
- // 关联模型
- public function getGoods()
- {
- return $this->hasOne(Goods::class, ['id' => 'goods_id']);
- }
-
- public function getBaseGoods()
- {
- return $this->hasOne(Goods::class, ['id' => 'goods_id'])->select(['id', 'name', 'pic_url']);
- }
- public function getScoreGoodsAttr() {
- return $this->hasOne(ScoreMallGoodsAttr::class, ['goods_attr_id' => 'id'])->andWhere(['status' => [StatusEnum::ENABLED]]);
- }
- /**
- * 保存规格数据
- * @Author bing
- * @DateTime 2021-09-02 19:37:52
- * @copyright: Copyright (c) 2020 广东七件事集团
- * @param [type] $goods_id
- * @param [type] $attrs
- * @param [type] $attr_groups
- * @param string $addons 来自插件
- * @param callback $call_back 回调函数
- * @return void
- */
- public static function setData($goods_id, $attrs, $attr_groups, &$wait_del_attr_id_arr=[])
- {
- try{
- if($goods_id && $attrs && $attr_groups){
- $goods_attr_list = self::lists(['where'=>[['goods_id'=>$goods_id],['status'=>StatusEnum::ENABLED]],'select'=>'id']);
- $wait_del_attr_id_arr = array_column($goods_attr_list,'id', 'id');//等待删除的规格id
- self::updateAll(['status'=>StatusEnum::DELETE],['goods_id'=>$goods_id]);
- $total_stock = 0; // 总库存
- $attr_pic_list = array_column($attr_groups[0]['attr_list'], 'pic_url', 'attr_id');
- foreach($attrs as $attr){
- $attr_ids = array_column($attr['attr_list'],'attr_id');
- // 和前端保持一致排序
- asort($attr_ids);
- $attr['pic_url'] = $attr_pic_list[$attr_ids[0]] ?? '';
- $attr['sign_id'] = implode(':',$attr_ids);
-
- $attr_group_name_list = array_column($attr['attr_list'], 'attr_group_name');
- $attr_name_list = array_column($attr['attr_list'], 'attr_name');
- $attr_name_arr = [];
- foreach ($attr_group_name_list as $index => $attr_group_name) {
- $attr_name_arr[] = $attr_group_name . ':' . $attr_name_list[$index];
- }
- $attr['name'] = implode(',',$attr_name_arr);
- $attr['weight'] = empty($attr['weight']) ? 0: $attr['weight'];
- $total_stock += $attr['stock'];
- if(!empty($attr['id'])){
- $model = self::findOne(['id' => $attr['id'],'goods_id' => $goods_id]);
- $model->status = StatusEnum::ENABLED;
- //移除状态正常的规格id
- unset($wait_del_attr_id_arr[$attr['id']]);
- }else{
- $model = new self();
- $model->loadDefaultValues();
- $model->goods_id = $goods_id;
- }
- if(!$model->load($attr,'') || !$model->save()){
- throw new Exception($model->getErrorMessage());
- }
- }
- // 更新商品库存
- $goods = Goods::findOne($goods_id);
- $goods->stock = $total_stock;
- if(!$goods->save()) throw new Exception($goods->getErrorMessage());
- }
- return true;
- }catch(Exception $e){
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * 无规格商品添加货品数据
- * @Author bing
- * @DateTime 2021-09-02 19:37:52
- * @copyright: Copyright (c) 2020 广东七件事集团
- * @param [type] $goods_id
- * @param [type] $attrs
- * @param [type] $attr_groups
- * @return void
- */
- public static function setAttr($goods_id,$attr){
- try{
- self::updateAll(['status'=>StatusEnum::DELETE],['goods_id'=>$goods_id]);
- $model = self::findOne(['goods_id' => $goods_id]);
- if(empty($model)){
- $model = new self();
- $model->loadDefaultValues();
- $model->goods_id = $goods_id;
- }else{
- $model->status = StatusEnum::ENABLED;
- $model->name = '';
- }
- if(!$model->load($attr,'') || !$model->save()){
- throw new Exception($model->getErrorMessage());
- }
- return true;
- }catch(Exception $e){
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * 获取规格列表
- * @Author bing
- * @DateTime 2021-08-26 13:00:11
- * @copyright: Copyright (c) 2020 广东七件事集团
- * @param array $cond
- * @param array $with
- * @param integer $is_array
- * @return array|null
- */
- public static function getGoodsAttrs(array $cond=[],array $with=[],bool $is_array=true,$select='*'){
- $default_cond = [['<>','status',StatusEnum::DELETE]];
- $cond = array_merge($default_cond,$cond);
- $query = self::find()->select($select);
- self::paramPack(['where'=>$cond,'with'=>$with],$query);
- $attrs = $query->asArray($is_array)->all();
- return $attrs;
- }
- /**
- * 格式化表单需要的规格
- * @Author bing
- * @DateTime 2021-09-03 15:52:03
- * @copyright: Copyright (c) 2020 广东七件事集团
- * @param array $attrs
- * @param array $format_attr_group
- * @return void
- */
- public static function formatFormAttr(array $attrs,array $format_attr_group){
- if(empty($attrs)) return $attrs;
- foreach ($attrs as $key => $attr) {
- $attrs[$key]['attr_list'] = $format_attr_group[$attr['sign_id']] ?? [];
- }
- return $attrs;
- }
- /**
- * 获取所选的规格
- * @param $sign_id
- * @param $attr_groups_format
- * @return string
- */
- public static function getAttrStr($sign_id,$attr_groups_format){
- $attr_name = '';
- if(empty($attr_groups_format[$sign_id])) return '默认规格';
- foreach ($attr_groups_format[$sign_id] as $val) {
- $attr_name .= $val['attr_name'].',';
- }
- return trim($attr_name,',');
- }
- /**
- * 获取sku数据
- * @param $goods_attr_list
- * @param $attr_groups_format
- * @return array
- */
- public static function getSkuArr($goods_attr_list,$attr_groups_format){
- $sku_arr = [];
- foreach ($goods_attr_list as $v) {
- $sku_arr[$v['goods_id']][] = [
- 'id' => empty($v['sign_id'])?$v['goods_id']:$v['id'],
- 'sign_id' => empty($v['sign_id'])?$v['goods_id']:$v['sign_id'],
- 'attr_name' => self::getAttrStr($v['sign_id'],$attr_groups_format),
- 'price' => $v['price'],
- 'pic_url' => $v['pic_url'],
- 'stock' => $v['stock'],
- ];
- }
- return $sku_arr;
- }
- /**
- * @desc:扣减规格库存
- * @Author: hua
- * @Date: 2021/11/9
- * @Time: 11:46
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $goods_attr_id
- * @param $num +增加 -减少
- * @return bool
- * @throws Exception
- */
- public static function handleStock($goods_attr_id,$num){
- try{
- $model = self::findOne(['id'=>$goods_attr_id,'status'=>StatusEnum::ENABLED]);
- if(empty($model)) throw new Exception('规格不存在');
- if(!is_int($num)) throw new Exception('库存数量必须为整数');
- if($num < 0 && $model->stock < abs($num)) throw new Exception('库存不足');
- $model->stock += $num;
- $res = $model->save();
- if($res === false) throw new Exception($model->getErrorMessage());
- return true;
- }catch(Exception $e){
- self::setStaticError($e->getMessage());
- return false;
- }
-
- }
- }
|