| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- <?php
- namespace addons\Store\common\models;
- use common\enums\StatusEnum;
- use common\models\base\BaseActiveRecord;
- use common\models\goods\Goods;
- use common\models\goods\GoodsAttr;
- use Yii;
- /**
- * This is the model class for table "qimall_addons_store_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 $status 状态[1正常 -1删除]
- * @property int $version 版本
- * @property int $sort 排序
- * @property int $created_at 创建时间
- * @property int $updated_at 修改时间
- * @property string $attr_detail 规格详情
- * @property int $mall_attr_id 商城规格ID
- */
- class StoreGoodsAttr extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_store_goods_attr}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['goods_id', 'stock', 'status', 'version', 'sort', 'created_at', 'updated_at', 'mall_attr_id'], 'integer'],
- [['price', 'original_price', 'cost_price', 'weight'], 'number'],
- [['name'], 'string', 'max' => 500],
- [['sign_id', 'goods_no', 'pic_url'], 'string', 'max' => 255],
- [['attr_detail'], 'string'],
- ];
- }
- /**
- * {@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' => '修改时间',
- 'attr_detail' => '规格详情',
- 'mall_attr_id' => '商城规格ID',
- ];
- }
- // 关联模型
- public function getGoods()
- {
- return $this->hasOne(StoreGoods::class, ['id' => 'goods_id']);
- }
- public function getMainGoodsAttr()
- {
- return $this->hasOne(GoodsAttr::class, ['id' => 'mall_attr_id']);
- }
- /**
- * 保存规格数据
- * @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
- */
- public static function setData($goods_id, $attrs, $attr_groups,$mall_id, &$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
- 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['goods_no'] = (string)$attr['goods_no'];
- StoreGoods::priceLimit($mall_id,$attr['price']);
- $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;
- }else{
- $model = new self();
- $model->loadDefaultValues();
- $model->goods_id = $goods_id;
- }
- if(!$model->load($attr,'') || !$model->save()){
- throw new \Exception($model->getErrorMessage());
- }
- }
- // 更新商品库存
- $goods = StoreGoods::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
- */
- 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;
- }
- 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-09-03 15:52:03
- * @copyright: Copyright (c) 2020 广东七件事集团
- * @param array $attrs
- * @param array $format_attr_group
- * @return array
- */
- 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;
- }
- 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;
- }
- }
- }
|