| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models\adopt;
- use app\constants\OptionSetting;
- use app\plugins\adopt\models\AdoptGoodsDevice;
- use app\models\Attr;
- use app\models\AttrGroup;
- use app\models\Form;
- use app\models\Goods;
- use app\models\GoodsBook;
- use app\models\GoodsCat;
- use app\models\GoodsPic;
- use app\models\Level;
- use app\models\Mch;
- use app\models\Option;
- use app\models\PostageRules;
- use app\models\VerifyCard;
- use app\modules\mch\models\LevelListForm;
- use app\plugins\adopt\models\AdoptCat;
- use app\plugins\adopt\models\AdoptGoods;
- use app\utils\Delivery\Delivery;
- use yii\base\BaseObject;
- use yii\base\Model;
- use yii\helpers\Json;
- use app\jobs\SyncMdGoodsJob;
- use app\models\GoodsChainLevel;
- use app\models\ShareHolderLevel;
- use app\utils\Tools;
- class AdoptGoodsForm extends Model
- {
- public $store_id;
- public $status;
- public $keyword;
- public $cat_id;
- public $type;
- public $id;
- public $name;
- public $key_word;
- public $unit;
- public $price;
- public $cost_price;
- public $original_price;
- public $service;
- public $weight;
- public $freight;
- public $pieces;
- public $forehead;
- public $quick_purchase;
- public $content;
- public $goods_num;
- public $goods_no;
- public $integral;
- public $use_attr;
- public $is_negotiable;
- public $cover_pic;
- public $individual_share;
- public $share_type;
- public $share_commission_first;
- public $share_commission_second;
- public $share_commission_third;
- public $video_url;
- public $attr;
- public $attr_member_price_List;
- public $attr_setting_type;
- public $virtual_sales;
- public $confine_count;
- public $is_level;
- public $goods_pic_list;
- public $goods_verify_card_list;
- public $delivery_type;
- public $product_type;
- public $date_book;
- public $service_book;
- public $form;
- public $rate;
- public $rate_type;
- public $desc;
- public $yield;
- public $varieties;
- public $growth_cycle;
- public $adopt_income;
- public $self_pick_date;
- public $self_pick_times;
- // 监控设备ID
- public $device_id;
- public $share_holder_profit_switch;
- public $share_holder_high_profit;
- public $is_open;
- public $chain_level_value;
- public $chain_type;
- /**
- * 初始化
- */
- public function init()
- {
- $this->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;
- }
- }
|