store_id = get_store_id(); } /** * @return array */ public function rules() { return [ [[ 'status', 'quick_purchase', 'store_id', 'id', 'freight', 'pieces', 'goods_num', 'use_attr', 'individual_share', 'attr_setting_type', 'virtual_sales', 'confine_count', 'is_level', 'product_type', 'growth_cycle', 'share_holder_profit_switch', 'chain_type', 'rate_type' ], 'integer'], [['keyword', 'key_word'], 'string', 'max' => 60], [['name', 'service', 'video_url', 'goods_no', 'cover_pic', 'desc', 'yield', 'varieties',], 'string', 'max' => 255], [['content',], 'string'], [['attr', 'attr_member_price_List', 'goods_verify_card_list', 'service_book', 'date_book', 'form', 'adopt_income', 'self_pick_date', 'self_pick_times', 'chain_level_value',], 'safe',], [['unit'], 'string', 'max' => 10], [['status', 'quick_purchase', 'chain_type'], 'default', 'value' => 1], [['cat_id', 'integral', 'goods_pic_list', 'delivery_type', 'device_id'], 'default', 'value' => []], [[ 'use_attr', 'is_negotiable', 'individual_share', 'share_type', 'forehead', 'freight', 'attr_setting_type', 'virtual_sales', 'confine_count', 'is_level', 'share_holder_profit_switch', 'is_open', ], 'default', 'value' => 0], [['unit'], 'default', 'value' => '件'], [['service'], 'default', 'value' => ''], [['price', 'cost_price', 'original_price', 'forehead', 'share_commission_first', 'share_commission_second', 'share_commission_third', 'weight', 'rate', 'share_holder_high_profit',], 'number', 'max' => 9999999999] ]; } public function attributeLabels() { return [ 'name' => '商品名称', 'keyword' => '关键字', 'cat_id' => '分类', 'device_id' => '设备ID', 'unit' => '单位', 'price' => '售价', 'cost_price' => '成本价', 'original_price' => '原价', 'service' => '服务', 'forehead' => '满额包邮', 'quick_purchase' => '是否添加快速购买', 'content' => '图文详情', 'use_attr' => '是否使用规格', 'is_negotiable' => '是否开启面议', 'goods_pic_list' => '商品图片' ]; } /** * @param array $arr | status 状态 name 名称 cat_id 分类id mch_id 入驻商 is_verify 审核 select 查询数据 * @return array */ public static function getList ($arr = []) { $query = Goods::find()->alias('g') ->leftJoin(['ag' => AdoptGoods::tableName()], 'g.id = ag.goods_id') ->where(['g.store_id' => get_store_id(), 'g.is_delete' => 0, 'g.md_food_id' => 0]) ->andWhere(['not like', 'g.name', '当面付']); if (isset($arr['status']) && $arr['status'] > -1) { $query->andWhere([ 'g.status' => $arr['status'] ]); } $query->andWhere([ 'g.product_type' => 5 ]); if (isset($arr['name']) && !empty($arr['name'])) { $query->andWhere([ 'like', 'g.name', $arr['name'] ]); } if (isset($arr['cat_id']) && $arr['cat_id'] > 0) { $query->leftJoin(['gc' => GoodsCat::tableName()], 'g.id = gc.goods_id')->andWhere([ 'gc.cat_id' => $arr['cat_id'] ])->groupBy('gc.goods_id'); } if (!empty($arr['select'])) { $select = $arr['select']; } else { $select = [ 'g.id', 'g.name', 'g.status', 'g.service', 'g.updated_at', 'g.virtual_sales', 'g.is_verify', 'g.is_negotiable', 'g.price', 'g.goods_num', 'g.sort', 'g.cover_pic', 'g.original_price', 'g.quick_purchase', 'g.attr', 'g.mch_id', 'g.detail', 'g.use_attr', 'g.product_type', 'g.is_recommend', 'ag.desc', 'ag.yield', 'ag.varieties', 'ag.adopt_income', 'ag.growth_cycle' ]; } $query->with(['mch'])->select($select) ->orderBy(['g.sort' => SORT_DESC, 'g.id' => SORT_DESC]); $pagination = pagination_make($query); $pagination['data'] = $pagination['list']; foreach($pagination['data'] as $k => $v) { $goods_cat = GoodsCat::find()->alias('gc') ->leftJoin(['c' => AdoptCat::tableName()], 'gc.cat_id=c.id') ->where([ 'gc.goods_id' => $v['id'] ]) ->select(['c.name']) ->asArray() ->all(); $pagination['data'][$k]['cat'] = $goods_cat; } unset($pagination['list']); $book_goods_ids = []; return [ 'code' => 0, 'msg' => 'success', 'book_goods_ids' => $book_goods_ids, 'data' => $pagination ]; } public function saveAdopt() { if(!$this->validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0], ]; } $full_cut = [ 'pieces' => 0, 'forehead' => 0 ]; if($this->pieces){ $full_cut['pieces'] = $this->pieces; } if($this->forehead){ $full_cut['forehead'] = $this->forehead; } $t = \Yii::$app->db->beginTransaction(); $goods = $this->id ? Goods::findOne($this->id) ?: new Goods() : new Goods(); $goods->store_id = $this->store_id; $goods->name = $this->name; $goods->key_word = $this->key_word; $goods->unit = $this->unit; $goods->price = $this->price; $goods->cost_price = $this->cost_price; $goods->original_price = $this->original_price; $goods->service = $this->service; $goods->weight = $this->weight; $goods->freight = $this->freight; $goods->pieces = $this->pieces; $goods->full_cut = Json::encode($full_cut); $goods->forehead = $this->forehead; $goods->quick_purchase = $this->quick_purchase; $goods->video_url = $this->video_url; $goods->detail = $this->content; $goods->goods_num = $this->goods_num; $goods->goods_no = $this->goods_no; $goods->integral = json_encode( $this->integral); $goods->use_attr = $this->use_attr; $goods->is_negotiable = $this->is_negotiable; $goods->cover_pic = $this->cover_pic; $goods->individual_share = $this->individual_share; $goods->share_type = $this->share_type; $goods->share_commission_first = $this->share_commission_first ?? 0; $goods->share_commission_second = $this->share_commission_second ?? 0; $goods->share_commission_third = $this->share_commission_third ?? 0; $goods->attr_setting_type = $this->attr_setting_type ? 1 : 0; $goods->virtual_sales = $this->virtual_sales; $goods->confine_count = $this->confine_count; $goods->rate = $this->rate ?? 0; $goods->rate_type = $this->rate_type ?? 0; $goods->is_level = $this->is_level; $goods->delivery_type = $this->delivery_type ? json_encode($this->delivery_type) : '["express"]'; $goods->product_type = $this->product_type; $goods->form_name = $this->form['form_name']; $goods->is_form = $this->form['is_form']; $goods->share_holder_profit_switch = $this->share_holder_profit_switch ?: 0; $goods->share_holder_high_profit = $this->share_holder_high_profit ?: ''; if ($this->goods_verify_card_list) { $verify_card_id = ''; foreach($this->goods_verify_card_list as $val) { $verify_card_id .= $verify_card_id ? ','.$val['id'] : $val['id']; } $goods->verify_card_id = $verify_card_id; } if ($goods->save()) { $goodsChainLevel = GoodsChainLevel::findOne(['goods_id' => $goods['id']]); if (!$goodsChainLevel) { $goodsChainLevel = new GoodsChainLevel(); $goodsChainLevel->goods_id = $goods['id']; } $goodsChainLevel->type = $this->chain_type; $goodsChainLevel->is_open = $this->is_open; $goodsChainLevel->value = json_encode($this->chain_level_value); $goodsChainLevel->save(); // 自定义表单 Form::updateAll(['is_delete' => 1], ['store_id' => $this->store_id, 'goods_id' => $goods['id']]); if ($this->form['is_form'] == 1) { if (!$this->form['form_list']) { $t->rollBack(); return [ 'code'=>1, 'msg'=>'请填写表单设置' ]; } } if ($this->form['form_list']) { $this->form['form_list'] = array_values($this->form['form_list']); foreach ($this->form['form_list'] as $index => $value) { if (!$value['name']) { $t->rollBack(); return [ 'code' => 1, 'msg' => '请输入字段名称' ]; } if (in_array($value['type'], ['radio', 'checkbox'])) { if (!$value['default']) { $t->rollBack(); return [ 'code' => 1, 'msg' => '请输入单选或多选的默认值' ]; } } if ($value['id']) { $form = Form::findOne(['store_id' => $this->store_id, 'id' => $value['id']]); } else { $form = new Form(); $form->goods_id = $goods['id']; } $form->is_delete = 0; $form->created_at = time(); $form->type = $value['type']; $form->name = $value['name']; $form->default = $value['default']; $form->required = $value['required'] ? $value['required'] : 0; $form->tip = $value['tip']; $form->sort = $index; $form->store_id = $this->store_id; if (!$form->save()) { $t->rollBack(); return [ 'code' => 1, 'msg' => $form->errors[0] ]; } } } // 认养 if ($this->product_type == Goods::GOODS_TYPE_ADOPT) { if ($goods->id > 0) { $adopt_goods = AdoptGoods::findOne(['store_id' => $this->store_id, 'goods_id' => $goods->id]); if ($adopt_goods){ $adopt_goods->updated_at = time(); } else{ $adopt_goods = new AdoptGoods(); $adopt_goods->goods_id = $goods->id; $adopt_goods->created_at = time(); } } $adopt_goods->desc = $this->desc; $adopt_goods->yield = $this->yield; $adopt_goods->varieties = $this->varieties; $adopt_goods->growth_cycle = $this->growth_cycle; $adopt_goods->adopt_income =$this->adopt_income ? json_encode($this->adopt_income) : json_encode([]); $adopt_goods->self_pick_date =$this->self_pick_date ? json_encode($this->self_pick_date) : json_encode([]); $adopt_goods->self_pick_times =$this->self_pick_times ? json_encode($this->self_pick_times) : json_encode([]); $adopt_goods->is_delete = 0; $adopt_goods->store_id = $this->store_id; if (!$adopt_goods->save()) { $t->rollBack(); return [ 'code' => 1, 'msg' => $adopt_goods->errors[0] ]; } } // 设置规格 try { $this->setAttr($goods); } catch(\Exception $e){} if ($this->cat_id && count($this->cat_id)) { if ($goods->id > 0) { GoodsCat::deleteAll(['goods_id' => $goods->id]); } $goodsCatForm = new GoodsCat(); foreach ($this->cat_id as $cat_id) { $goodsCat = clone $goodsCatForm; $goodsCat->goods_id = $goods->id; $goodsCat->cat_id = $cat_id; $goodsCat->store_id = $goods->store_id; if (!$goodsCat->save()) { $t->rollBack(); return [ 'code' => 1, 'msg' => '保存失败' ]; } } } if ($goods->id > 0) { AdoptGoodsDevice::deleteAll(['goods_id' => $goods->id]); } if ($this->device_id && count($this->device_id)) { $goodsDeviceForm = new AdoptGoodsDevice(); foreach ($this->device_id as $device_id) { $goodsDev = clone $goodsDeviceForm; $goodsDev->goods_id = $goods->id; $goodsDev->device_id = $device_id; $goodsDev->store_id = $goods->store_id; if (!$goodsDev->save()) { $t->rollBack(); return [ 'code' => 1, 'msg' => '保存失败' ]; } } } if ($this->goods_pic_list) { if ($goods->id > 0) { GoodsPic::deleteAll(['goods_id' => $goods->id]); } $goodsPicForm = new GoodsPic(); foreach ($this->goods_pic_list as $pic) { $goodsPic = clone $goodsPicForm; $goodsPic->goods_id = $goods->id; $goodsPic->pic_url = $pic['pic_url']; if (!$goodsPic->save()) { $t->rollBack(); return [ 'code' => 1, 'msg' => '保存失败' ]; } } } $t->commit(); // // 同步门店商品 // queue_push(new SyncMdGoodsJob(['goods_ids' => [$goods->id]])); return [ 'code' => 0, 'msg' => '保存成功' ]; } else { $t->rollBack(); return [ 'code' => 1, 'msg' => '添加失败' ]; } } public function getAdoptEdit($id) { if ($id) { $goods = Goods::findOne($id); if (empty($goods)) { return [ 'code' => 1, 'msg' => '商品获取失败' ]; } $getAttrGroupList = $goods->getAttrGroupList(); $goods = $goods->toArray(); $cat_id = GoodsCat::find() ->where(['goods_id' => $goods['id'], 'is_delete' => 0]) ->select(['cat_id'])->asArray()->all(); $cat_id = array_column($cat_id, 'cat_id'); $goods['cat_id'] = $cat_id; $goods['device_id'] = AdoptGoodsDevice::find()->where(['goods_id'=>$goods['id'],'is_delete'=>0])->select('device_id')->column(); $goods['integral'] = json_decode($goods['integral'],true); $goods['goods_pic_list'] = GoodsPic::find()->select(['pic_url']) ->where(['goods_id' => $id])->asArray()->all(); foreach ($goods as &$val) { $val = is_int($val) ? (string)$val : $val; } $goods['content'] = $goods['detail']; $adopt_goods = AdoptGoods::find() ->where(['goods_id' => $goods['id'], 'is_delete' => 0]) ->asArray()->one(); if ($adopt_goods){ $goods['desc'] = $adopt_goods['desc']; $goods['yield'] = $adopt_goods['yield']; $goods['varieties'] = $adopt_goods['varieties']; $goods['growth_cycle'] = $adopt_goods['growth_cycle']; $goods['adopt_income'] = $adopt_goods['adopt_income'] ? json_decode($adopt_goods['adopt_income']) : []; $goods['adopt_goods_id'] = $adopt_goods['id'] ? $adopt_goods['id'] : []; $goods['self_pick_date'] = $adopt_goods['self_pick_date'] ? json_decode($adopt_goods['self_pick_date']) : []; $goods['self_pick_times'] = $adopt_goods['self_pick_times'] ? json_decode($adopt_goods['self_pick_times']) : []; } } else { $goods = null; $goods['is_open'] = '0'; $goods['chain_level_value'] = []; } $send_type = Option::get(OptionSetting::STORE_SEND_TYPE, get_store_id(), 'store')['value']; $send_type = Option::get(OptionSetting::STORE_SEND_TYPE, get_store_id(), 'pay', $send_type); $send_type = $send_type ? Json::decode($send_type['value']) : []; $send_type_arr = []; foreach ((array)$send_type as $key => $send) { if ($send['value'] == 1) { $send_type_arr[$key] = $send['text']; } } $arr = empty($send_type_arr) ? ['express' => '快递', 'shop' => '自提'] : $send_type_arr; $goods['goods_delivery_type'] = !empty($goods['delivery_type']) ? Json::decode($goods['delivery_type']) : array_keys($arr); $goods['delivery_type'] = $arr; // 获取运费规则 $postage = PostageRules::find()->where([ 'store_id' => get_store_id(), 'is_delete' => 0, 'mch_id' => 0 ])->select(['id', 'name', 'is_enable'])->asArray()->all(); // 获取所有的会员等级 $level = Level::find()->where([ 'store_id' => get_store_id(), 'is_delete' => 0, 'status' => 1 ])->select(['level as id', 'name'])->orderBy(['id' => SORT_DESC])->asArray()->all(); // 卡券 $verify_list = VerifyCard::find() ->where([ 'store_id' => get_store_id(), 'is_delete' => 0 ])->orderBy(['sort' => SORT_ASC]) ->select([ '*' ])->asArray() ->all(); $verify_list = Tools::getVerifyList($verify_list); $goods_verify_card_list = []; // 获取商品已添加核销卡 if (!empty($goods) && !empty($goods['verify_card_id'])) { $verify_cards = explode(',', $goods['verify_card_id']); foreach ($verify_cards as $k => $v) { $res = VerifyCard::find() ->select(['id','name']) ->where(['id' => $v,'is_delete' => 0]) ->asArray()->one(); if ($res) { $goods_verify_card_list[] = $res; } } } // 服务商品预约 $goods['service_book'] = [ 'date' => 0, 'data' => [], 'flag' => 1 ]; $goods['date_book'] = [ 'data' => [], 'date' => 30 ]; $goods['form'] = [ 'form_list' => [], 'is_form' => 0, 'form_name' => '' ]; $goods['rate_type'] = (string)(int)$goods['rate_type']; $goods['share_holder_profit_switch'] = (int)$goods['share_holder_profit_switch']; $goods['share_holder_high_profit'] = (float)$goods['share_holder_high_profit']; // 获取等级分润配置 $gooodsChainLevel = GoodsChainLevel::findOne(['goods_id' => $id]); $goods['is_open'] = '0'; $goods['chain_type'] = '1'; $goods['chain_level_value'] = []; if ($gooodsChainLevel) { $goods['is_open'] = (string)$gooodsChainLevel->is_open; $goods['chain_type'] = (string)$gooodsChainLevel->type; if ($chainLevelValue = json_decode($gooodsChainLevel->value)) { $goods['chain_level_value'] = $chainLevelValue; } } if (isset($goods['id'])) { $goods_book_config = GoodsBook::findOne(['goods_id' => $goods['id']]); if ($goods_book_config) { $service_book = $goods_book_config->service_book; $date_book = $goods_book_config->date_book; if ($service_book) { $goods['service_book'] = Json::decode($service_book); } if ($date_book) { $goods['date_book']['data'] = Json::decode($date_book); $goods['date_book']['date'] = count($goods['date_book']['data']); } } $goods['form']['form_list'] = Form::find()->where(['is_delete' => 0, 'store_id' => $this->store_id, 'goods_id' => $goods['id']])->orderBy(['sort' => SORT_ASC])->asArray()->all(); $goods['form']['is_form'] = intval($goods['is_form']); $goods['form']['form_name'] = $goods['form_name']; } // 获取等级 $shareHolderLevel = ShareHolderLevel::find()->where([ 'store_id' => get_store_id(), 'is_delete' => 0, 'status' => 1 ])->select(['id', 'name'])->orderBy(['id' => SORT_ASC])->asArray()->all(); return [ 'code' => 0, 'msg' => 'success', 'data' => [ 'shareHolderLevel' => $shareHolderLevel, 'info' => $goods, 'AttrGroupList' => isset($getAttrGroupList) ? $getAttrGroupList : [], 'postage' => $postage, 'level' => $level, 'verify_list' => $verify_list, 'goods_verify_card_list' => $goods_verify_card_list ] ]; } /** * @param Goods $goods */ private function setAttr($goods) { if ($goods->product_type == Goods::GOODS_TYPE_DATE || $goods->product_type == Goods::GOODS_TYPE_TIME) { $defaultAttr = '[{"attr_list":[{"attr_id":1,"attr_name":"默认"}],"num":0,"price":0,"no":"","pic":"","share_commission_first":"","share_commission_second":"","share_commission_third":""}]'; $goods->attr = $defaultAttr; $goods->save(); return; } if (!$this->use_attr) { list($default_attr, $default_attr_group) = $this->getDefaultAttr(); if (empty($this->attr)) { $this->attr = [ [ 'attr_list' => [ [ 'attr_group_name' => $default_attr_group->attr_group_name, 'attr_id' => $default_attr->id, 'attr_name' => $default_attr->attr_name, ], ], 'num' => intval($this->goods_num) ? intval($this->goods_num) : 0, 'price' => $this->price, 'no' => $this->goods_no, ], ]; } else { $attr_list = [ [ 'attr_group_name' => $default_attr_group->attr_group_name, 'attr_id' => $default_attr->id, 'attr_name' => $default_attr->attr_name, ], ]; $this->attr[0]['attr_list'] = $attr_list; $this->attr[0]['num'] = intval($this->goods_num) ? intval($this->goods_num) : 0; $this->attr[0]['price'] = intval($this->price) ? intval($this->price) : 0; $this->attr[0]['no'] = $this->goods_no; } } if (empty($this->attr) || !is_array($this->attr)) { return; } $new_attr = []; foreach ($this->attr as $i => $item) { $new_attr_item = [ 'attr_list' => [], 'num' => intval($item['num']), 'price' => doubleval($item['price']), 'no' => $item['no'] ? $item['no'] : '', 'pic' => $item['pic'] ? $item['pic'] : '', 'share_commission_first' => $item['share_commission_first'] ? $item['share_commission_first'] : '', 'share_commission_second' => $item['share_commission_second'] ? $item['share_commission_second'] : '', 'share_commission_third' => $item['share_commission_third'] ? $item['share_commission_third'] : '', ]; foreach ($item as $k => $v) { if (strpos($k,'member') !== false) { $new_attr_item[$k] = $v; } } foreach ($item['attr_list'] as $a) { $attr_group_model = AttrGroup::findOne(['store_id' => $this->store_id, 'attr_group_name' => $a['attr_group_name'], 'is_delete' => 0]); if (!$attr_group_model) { $attr_group_model = new AttrGroup(); $attr_group_model->attr_group_name = $a['attr_group_name']; $attr_group_model->store_id = $this->store_id; $attr_group_model->is_delete = 0; $attr_group_model->save(); } $attr_model = Attr::findOne(['attr_group_id' => $attr_group_model->id, 'attr_name' => $a['attr_name'], 'is_delete' => 0]); if (!$attr_model) { $attr_model = new Attr(); $attr_model->attr_name = $a['attr_name']; $attr_model->attr_group_id = $attr_group_model->id; $attr_model->is_delete = 0; $attr_model->save(); } $new_attr_item['attr_list'][] = [ 'attr_id' => $attr_model->id, 'attr_name' => $attr_model->attr_name, ]; } $new_attr[] = $new_attr_item; } $goods->attr = Json::encode($new_attr); $goods->save(); } /** * @return array */ private function getDefaultAttr() { $default_attr_name = '默认'; $default_attr_group_name = '规格'; $attr = Attr::findOne([ 'attr_name' => $default_attr_name, 'is_delete' => 0, 'is_default' => 1, ]); $attr_group = null; if (!$attr) { $attr_group = AttrGroup::findOne([ 'attr_group_name' => $default_attr_group_name, 'is_delete' => 0, ]); if (!$attr_group) { $attr_group = new AttrGroup(); $attr_group->store_id = $this->store_id; $attr_group->attr_group_name = $default_attr_group_name; $attr_group->is_delete = 0; $attr_group->save(false); } $attr = new Attr(); $attr->attr_group_id = $attr_group->id; $attr->attr_name = $default_attr_name; $attr->is_delete = 0; $attr->is_default = 1; $attr->save(false); } else { $attr_group = AttrGroup::findOne($attr->attr_group_id); } return [$attr, $attr_group]; } public function setAttrNum() { $data = post_params(); $goods = Goods::findOne($data['id']); $arrt = Json::encode($data['attr']); if ($goods) { $goods->attr = $arrt; $goods_num = 0; foreach($data['attr'] as $item) { $goods_num += $item['num']; } $goods->goods_num = $goods_num; // if ($goods->mch_id > 0) { // $mch = Mch::findOne($goods->mch_id); // if ($mch->is_goods_ex == 1) { // $goods->status = 0; // $goods->is_verify = -1; // } // } $res = ['code' => 1, 'msg' => '保存失败']; if ($goods->save()) { // 同步门店商品 queue_push(new SyncMdGoodsJob(['goods_ids' => [$goods->id]])); $res = ['code' => 0, 'msg' => '保存成功']; } } else { $res = ['code' => 1, 'msg' => '参数错误']; } return $res; } public function updateAttrNum() { $data = post_params(); $goods = Goods::findOne($data['id']); $arrt = Json::encode($data['attr']); if ($goods) { $goods->attr = $arrt; $goods_num = 0; foreach($data['attr'] as $item) { $goods_num += $item['num']; } $goods->goods_num = $goods_num; // if ($goods->mch_id > 0) { // $mch = Mch::findOne($goods->mch_id); // if ($mch->is_goods_ex == 1) { // $goods->status = 0; // $goods->is_verify = -1; // } // } $res = ['code' => 1, 'msg' => '保存失败']; if ($goods->save()) { // 同步门店商品 queue_push(new SyncMdGoodsJob(['goods_ids' => [$goods->id]])); $res = ['code' => 0, 'msg' => '保存成功']; } } else { $res = ['code' => 1, 'msg' => '参数错误']; } return $res; } // 修改商品属性 public function setType() { $type = get_params('type'); $goods_id = get_params('id'); $value = get_params('value', 0); if (!$type || !$goods_id) { return [ 'code' => 1, 'msg' => '参数错误' ]; } $goods = Goods::findOne([ 'id' => $goods_id, 'mch_id' => get_mch_id(), 'store_id' => get_store_id() ]); if (empty($goods)) { return [ 'code' => 1, 'msg' => '查询商品为空' ]; } $goods->$type = $value; if ($type == 'goods_num' && $goods->use_attr == 0) { $attr = Json::decode($goods->attr); $attr[0]['num'] = $value; $goods->attr = Json::encode($attr); } if ($type == 'price' && $goods->use_attr == 0) { $attr = Json::decode($goods->attr); $attr[0]['price'] = doubleval($value); $goods->attr = Json::encode($attr); } // if ($goods->mch_id > 0) { // $mch = Mch::findOne($goods->mch_id); // if ($mch->is_goods_ex == 1) { // $goods->status = 0; // $goods->is_verify = -1; // } // } if ($goods->save()) { // 同步门店商品 queue_push(new SyncMdGoodsJob(['goods_ids' => [$goods->id]])); return [ 'code' => 0, 'msg' => '保存成功' ]; } else { return [ 'code' => 1, 'msg' => '保存失败' ]; } } // 修改商品属性 public function updateAttribute() { $type = post_params('type'); $goods_id = post_params('id'); $value = post_params('value', 0); if (!$type || !$goods_id) { return [ 'code' => 1, 'msg' => '参数错误' ]; } $goods = Goods::findOne([ 'id' => $goods_id, 'store_id' => get_store_id() ]); if (empty($goods)) { return [ 'code' => 1, 'msg' => '查询商品为空' ]; } $goods->$type = $value; if ($type == 'goods_num' && $goods->use_attr == 0) { $attr = Json::decode($goods->attr); $attr[0]['num'] = $value; $goods->attr = Json::encode($attr); } if ($type == 'price' && $goods->use_attr == 0) { $attr = Json::decode($goods->attr); $attr[0]['price'] = doubleval($value); $goods->attr = Json::encode($attr); } // if ($goods->mch_id > 0) { // $mch = Mch::findOne($goods->mch_id); // if ($mch->is_goods_ex == 1) { // $goods->status = 0; // $goods->is_verify = -1; // } // } if ($goods->save()) { return [ 'code' => 0, 'msg' => '保存成功' ]; } else { return [ 'code' => 1, 'msg' => '保存失败' ]; } } /** * Undocumented function * * @Author LGL 24963@qq.com * @DateTime 2021-01-18 * @desc: 根据id获取商品列表 * @param array $id * @return void */ public static function getGoodsListById ($id = []) { if (empty($id) && !is_array($id)) { return []; } return Goods::find()->where(['in', 'id', $id])->select('id, name, cover_pic')->asArray()->all(); } /** * Undocumented function * * @Author LGL 24963@qq.com * @DateTime 2021-03-02 * @desc: 根据商品id获取规格属性列表 * @param [type] $id * @return void */ public static function getAttrByGoodsId ($id) { $goods = Goods::findOne(['id' => $id, 'store_id' => get_store_id(), 'mch_id' => get_mch_id()]); if (!$goods->attr) { return []; } $attr_data = json_decode($goods->attr, true); foreach ($attr_data as $i => $attr_data_item) { if (!isset($attr_data[$i]['no'])) { $attr_data[$i]['no'] = ''; } if (!isset($attr_data[$i]['pic'])) { $attr_data[$i]['pic'] = ''; } foreach ($attr_data[$i]['attr_list'] as $j => $attr_list) { $attr_group = $goods->getAttrGroupByAttId($attr_data[$i]['attr_list'][$j]['attr_id']); $t = $attr_data[$i]['attr_list'][$j]['attr_name']; unset($attr_data[$i]['attr_list'][$j]['attr_name']); $attr_data[$i]['attr_list'][$j]['attr_group_name'] = $attr_group ? $attr_group->attr_group_name : null; $attr_data[$i]['attr_list'][$j]['attr_name'] = $t; } } return $attr_data; } private function compareDate($arr): bool { array_multisort(array_column($arr, 'start_time'), SORT_ASC, $arr); foreach ($arr as $k => $v){ if ($v['start_time'] >= $v['end_time']) return false; if ($k > 0 && $arr[$k]['start_time'] < $arr[$k-1]['end_time']) return false; } return true; } }