| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229 |
- <?php
- namespace common\models\goods;
- use addons\Chanjet\common\global_func\Request;
- use addons\Chanjet\common\models\AddonsChanjetRequest;
- use common\components\export\CsvTool;
- use common\enums\AddonsEnum;
- use common\enums\DownloadEnum;
- use common\enums\GoodsTypeEnum;
- use common\enums\import\GoodsImportEnum;
- use common\enums\RabbitMqEnum;
- use common\enums\StatusEnum;
- use common\enums\WhetherEnum;
- use common\events\goods\GoodsEditEvent;
- use common\globals\GlobalInvoke;
- use common\helpers\ArrayHelper;
- use common\helpers\csv\CsvImportForm;
- use common\models\common\Download;
- use common\models\common\ImportFailLog;
- use common\models\common\ImportLog;
- use common\models\goods\template\GoodsTemplateRelation;
- use common\models\mall\MallAddons;
- use common\models\user\User;
- use common\models\user\UserLevel;
- use Exception;
- use Yii;
- use yii\db\Expression;
- use yii\helpers\Json;
- /**
- * This is the model class for table "{{%goods}}".
- *
- * @property int $id
- * @property int $mall_id 商城id
- * @property int $mch_id 商家id
- * @property string $goods_name 商品名称
- * @property string $subtitle 商品副标题
- * @property float $price 售价
- * @property float $original_price 原价(划线价仅展示)
- * @property float $cost_price 成本价
- * @property string $unit 单位
- * @property string $cover_pic 商品缩略图
- * @property string $bannar_pic 商品展示bannar图
- * @property int $is_on_sale 上架状态【1是 0否】
- * @property int $is_attr 是否有规格【1是 0否】
- * @property int $status
- * @property int $attr_groups_format
- * @property int $stock 商品库存
- * @property int $stock_warning 商品库存预警值
- * @property int $virtual_sales 虚拟销量
- * @property int $buy_num_limit 购买数量限制
- * @property int $freight_id 运费模板ID
- * @property int $free_shipping_num 商品满件包邮
- * @property float $free_shipping_money 商品满额包邮
- * @property int $goods_type 商品类型
- * @property string $detail 商品详情,图文
- * @property string $video_url 商品视频
- * @property string $video_cover_pic 商品视频封面
- * @property string $goods_source 商品来源
- * @property int $sort 排序
- * @property int $created_at 创建时间
- * @property int $updated_at 修改时间
- * @property int $check_status 审核状态
- * @property int $is_show 是否在主商城显示:1:是;0:否
- * @property int $comment_score 是否在主商城显示:1:是;0:否
- * @property int $can_feedback 是否在主商城显示:1:是;0:否
- * @property string $check_remark 审核备注
- * @property int $goods_subtype 商品子类型 0普通商品 1药品商品
- */
- class Goods extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%goods}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [[
- 'mall_id', 'mch_id', 'is_on_sale', 'is_attr', 'status', 'stock', 'stock_warning', 'is_show_stock',
- 'is_show_sales', 'virtual_sales', 'sales_num', 'buy_num_limit', 'freight_rules_type', 'freight_id',
- 'free_shipping_num', 'goods_type', 'sort', 'created_at', 'updated_at', 'is_area_limit', 'check_status',
- 'is_show','is_pay_limit', 'can_feedback', 'goods_subtype'
- ], 'integer'],
- [['price', 'original_price', 'cost_price', 'shipping_fee', 'free_shipping_money', 'comment_score'], 'number'],
- [['attr_groups', 'attr_groups_format', 'detail', 'area_limit', 'goods_source', 'goods_no','pay_limit'], 'string'],
- [['goods_name'], 'string', 'max' => 255],
- [['check_remark'], 'safe'],
- [['stock'], 'integer', 'max' => 99999999, 'message' => '库存最大数为99999999'],
- [['subtitle', 'unit'], 'safe'],
- [['cover_pic', 'freight_type', 'video_url', 'services', 'labels', 'goods_no'], 'string', 'max' => 255],
- [['video_cover_pic'], 'string', 'max' => 500],
- [['bannar_pic'], 'string', 'max' => 1500000],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城id',
- 'mch_id' => '商家id',
- 'goods_name' => '商品名称',
- 'subtitle' => '商品副标题',
- 'price' => '售价',
- 'original_price' => '原价(划线价仅展示)',
- 'cost_price' => '成本价',
- 'unit' => '单位',
- 'cover_pic' => '商品缩略图',
- 'bannar_pic' => '商品展示bannar图',
- 'is_on_sale' => '上架状态【1是 0否】',
- 'is_attr' => '是否有规格【1是 0否】',
- 'attr_groups' => '规格组',
- 'attr_groups_format' => '格式化后的规格组',
- 'status' => '状态[1正常 -1删除]',
- 'stock' => '商品库存',
- 'stock_warning' => '商品库存预警值',
- 'is_show_stock' => '是否显示库存',
- 'is_show_sales' => '是否显示销量【1是 0否】',
- 'virtual_sales' => '虚拟销量',
- 'sales_num' => '累计销量',
- 'buy_num_limit' => '购买数量限制',
- 'freight_type' => '物流方式【1快递发货 2上门自提】逗号分隔',
- 'freight_rules_type' => '运费规则 【1运费模板 2自定义运费】',
- 'freight_id' => '运费模板ID',
- 'shipping_fee' => '自定义运费金额',
- 'free_shipping_num' => '商品满件包邮',
- 'free_shipping_money' => '商品满额包邮',
- 'goods_type' => '商品类型',
- 'detail' => '商品详情,图文',
- 'is_area_limit' => '是否开启区域限制',
- 'area_limit' => '允许购买区域',
- 'is_pay_limit'=>'是否开启支付限制',
- 'pay_limit'=>'限制的支付方式',
- 'video_url' => '商品视频',
- 'video_cover_pic' => '商品视频封面',
- 'services' => '商品服务',
- 'labels' => 'Labels',
- 'goods_source' => '商品来源',
- 'sort' => '排序',
- 'created_at' => '创建时间',
- 'updated_at' => '修改时间',
- 'check_status' => '审核状态',
- 'check_remark' => '审核备注',
- 'goods_no' => '货号',
- 'is_show' => '是否在主商城显示',
- 'goods_subtype' => '商品子类型 0普通商品 1药品商品',
- ];
- }
- /**关联模型 */
- // 分类关系
- public function getGoodsCateRelate()
- {
- return $this->hasMany(GoodsCateRelate::class, ['goods_id' => 'id']);
- }
- // 分类
- public function getCats()
- {
- return $this->hasMany(GoodsCate::class, ['id' => 'cate_id'])
- // ->where(['mch_id' => 0])
- ->via('goodsCateRelate');
- }
- // 预约商品信息
- public function getReserve()
- {
- return $this->hasOne(GoodsReserve::class, ['goods_id' => 'id']);
- }
- // sku
- public function getAttr()
- {
- return $this->hasMany(GoodsAttr::class, ['goods_id' => 'id'])->andWhere(['status' => [StatusEnum::ENABLED]]);
- }
- // 商品额外设置
- public function getExtra()
- {
- return $this->hasOne(GoodsExtra::class, ['goods_id' => 'id'])->andWhere(['status' => [StatusEnum::ENABLED]]);
- }
- public function getCollect()
- {
- return $this->hasOne(GoodsCollect::class, ['goods_id' => 'id'])->andWhere(['status' => [StatusEnum::ENABLED]]);
- }
- public function getSearchAddress()
- {
- return $this->hasOne(GoodsSearchAddress::class, ['goods_id' => 'id']);
- }
- public function getMch()
- {
- return $this->hasOne(\addons\Mch\common\models\Mch::class, ['id' => 'mch_id'])->select('id,store_name,shop_mobile,logo_img,user_id');
- }
- /**
- * 畅捷通请求
- *
- * @return \yii\db\ActiveQuery
- */
- public function getAddonsChanjetRequest()
- {
- return $this
- ->hasOne(AddonsChanjetRequest::class, ['table_id' => 'id'])
- ->orderBy('updated_at desc')
- ->select('id, table_id, request_status, updated_at')
- ->where([
- 'table' => self::tableName(),
- 'path' => [Request::PATHS['INVENTORY_CREATE_BATCH'], Request::PATHS['INVENTORY_UPDATE_BATCH']]
- ])
- ->andWhere(['>', 'request_status', 0]);
- }
- /**
- * 保存数据
- * @Author bing
- * @DateTime 2021-08-24 15:34:48
- * @param array $params
- * @param string $addons 来自插件
- * @param callback $call_back 插件回调函数
- * @return object|boolean
- * @copyright: Copyright (c) 2020 广东七件事集团
- */
- public static function setData(array $params)
- {
- try {
- $wait_del_attr_id_arr = [];
- if (!empty($params['id'])) {
- $model = self::findOne(['mall_id' => $params['mall_id'], 'id' => $params['id'], 'status' => [StatusEnum::ENABLED]]);
- if (empty($model)) throw new Exception('商品不存在或已删除');
- if ($model['is_on_sale'] != $params['is_on_sale']) $data['is_on_sale'] = $params['is_on_sale'];
- } else {
- $model = new self();
- $model->loadDefaultValues();
- if (isset($params['is_on_sale']) && $params['is_on_sale'] == 1) $data['is_on_sale'] = $params['is_on_sale'];
- }
- $params['can_feedback'] = (int) ($params['can_feedback'] ?? StatusEnum::ENABLED);
- isset($params['services']) && $params['services'] = implode(',', $params['services']);
- isset($params['labels']) && $params['labels'] = implode(',', $params['labels']);
- isset($params['freight_type']) && $params['freight_type'] = implode(',', $params['freight_type']);
- isset($params['freight_id']) && empty($params['freight_id']) && $params['freight_id'] = 0;
- isset($params['virtual_sales']) && empty($params['virtual_sales']) && $params['virtual_sales'] = 0;
- if (isset($params['is_attr']) && $params['is_attr'] == 1) {
- if (isset($params['attr_groups']) && !empty($params['attr_groups'])) {
- $attr_groups = $params['attr_groups'];
- $params['attr_groups'] = json_encode($params['attr_groups'], JSON_UNESCAPED_UNICODE);
- $params['attr_groups_format'] = json_encode(self::formatGoodsAttrGroup($attr_groups, JSON_UNESCAPED_UNICODE));
- }
- } else {
- $params['attr_groups'] = '';
- $params['attr_groups_format'] = '';
- }
- isset($params['bannar_pic']) && $params['bannar_pic'] = json_encode($params['bannar_pic'], JSON_UNESCAPED_UNICODE);
- isset($params['area_limit']) && $params['area_limit'] = json_encode($params['area_limit'], JSON_UNESCAPED_UNICODE);
- if (!empty($params['is_area_limit']) && !empty($params['area_limit']) && strlen($params['area_limit']) > 5000) {
- // throw new \Exception("地区所有字节数总和超过5000字了");
- }
- isset($params['pay_limit']) && $params['pay_limit'] = json_encode($params['pay_limit'], JSON_UNESCAPED_UNICODE);
- $trans = Yii::$app->db->beginTransaction();
- // 分类判断当条件不满足时不导入商品
- if (isset($params['cats']) && !empty($params['cats'])) {
- $params['cats'] = self::goodsCategorizeValidate($params);
- }
- if (!$model->load($params, '') || !$model->save()) {
- //添加商品统计
- if (empty($params['id'])) {
- Yii::$app->services->statistics->global->set($params['mall_id'], ['goods_num' => 1]);
- }
- throw new Exception($model->getErrorMessage());
- }
- //商品分类处理
- if (isset($params['cats']) && !empty($params['cats'])) {
- $res = GoodsCateRelate::setData($model->id, $params['cats']);
- if ($res === false) throw new Exception('分类处理失败:' . GoodsCateRelate::getStaticError());
- }
- if (isset($params['goods_template_id']) && !empty($params['goods_template_id'])) {
- $res = GoodsTemplateRelation::setData(['mall_id' => $params['mall_id'], 'template_id' => $params['goods_template_id'], 'goods_id' => $model->id, 'status' => StatusEnum::ENABLED]);
- if ($res === false) throw new Exception('广告模板处理失败:' . GoodsTemplateRelation::getStaticError());
- }
- $goods = ArrayHelper::toArray($model);
- if ($goods['goods_type'] === GoodsTypeEnum::GoodsTypeReserved) {
- // 预约商品
- $reserve = $params['reserve'];
- $res = GoodsReserve::setData($goods, $reserve);
- if ($res === false) throw new Exception('预约商品SKU处理失败:' . GoodsAttr::getStaticError());
- } else {
- // 商品规格处理
- if ($model->is_attr == 1) {
- $attrs = $params['attr'] ?? [];
- $res = GoodsAttr::setData($model->id, $attrs, $attr_groups, $wait_del_attr_id_arr);
- if ($res === false) throw new Exception('规格处理失败:' . GoodsAttr::getStaticError());
- } else {
- //无规格商品处理
- $goods['pic_url'] = $goods['cover_pic'];
- $goods['goods_no'] = isset($params['goods_no']) ? $params['goods_no'] : '';
- $goods['weight'] = $params['goods_weight'] ?? 0;
- $res = GoodsAttr::setAttr($model->id, $goods);
- if ($res === false) throw new Exception('设置货品失败:' . GoodsAttr::getStaticError());
- }
- }
- // 商品额外信息处理
- if (isset($params['extra']) && !empty($params['extra'])) {
- //检测商品限购设置
- $member_buy_limit_setting = $params['extra']['member_buy_limit_setting'] ?? [];
- if ($member_buy_limit_setting) {
- if ($member_buy_limit_setting['nums'] > 0 && $member_buy_limit_setting['every_num'] > 0 && $member_buy_limit_setting['nums'] >= $member_buy_limit_setting['every_num']) {
- throw new Exception('商品额外信息每次限购数量需大于起购数量');
- }
- }
- $params['extra']['mall_id'] = $params['mall_id'];
- $res = GoodsExtra::setData($model->id, $params['extra']);
- if ($res === false) throw new Exception('商品额外信息处理失败:' . GoodsExtra::getStaticError());
- }
- // 营销插件配置保存
- if (!empty($params['addons'])) {
- GlobalInvoke::exec(GlobalInvoke::GOODS_DETAIL_EDIT, $params['mall_id'], [
- 'type' => 'save',
- 'mall_id' => $params['mall_id'],
- 'goods_id' => $model->id,
- 'setting' => $params['addons'],
- ]);
- // $res = CouponGiveItemSetting::setGoodsSetting($params['mall_id'], $model->id, $params['coupon_selection']);
- // if ($res === false) throw new Exception('商品优惠券处理失败:' . CouponGiveItemSetting::getStaticError());
- }
- // 商品营销+用户设置处理
- // if (isset($params['goods_setting']) && !empty($params['goods_setting'])) {
- // $goodsId = $model->id ?? 0;
- // $mchId = $model->mch_id ?? 0;
- // $res = GoodsExtra::setData($params['goods_setting'], $params['mall_id'], $mchId, $goodsId);
- // if ($res === false) throw new Exception('营销/用户处理失败:' . GoodsExtra::getStaticError());
- // }
- if(!empty($params['searchAddress'])){
- $address_search = array_merge($params['searchAddress'], ['goods_id' => $model->id, 'mall_id' => $params['mall_id']]);
- if(GoodsSearchAddress::setData($address_search) === false) throw new Exception(GoodsSearchAddress::getStaticError());
- }
- $res = GoodsAssociation::setData($model->id, $params);
- $trans->commit();
- //编辑商品触发
- $event = new GoodsEditEvent($params['mall_id']);
- $data['goods_id'] = $model->id;
- $data['mall_id'] = $params['mall_id'];
- $data['wait_del_attr_id_arr'] = $wait_del_attr_id_arr;
- $data['copy_id'] = $params['copy_id'];
- Yii::$app->services->events->dispatch($event, $data, $event->listeners);
- return $model;
- } catch (Exception $e) {
- if (isset($trans)) $trans->rollback();
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * 商品分类过滤验证
- * @param array $params
- * @return array
- * @throws Exception
- */
- public static function goodsCategorizeValidate(array $params): array
- {
- // 获取商品分类ID合集
- $caseIds = GoodsCate::find()->where(['mall_id' => $params['mall_id'], 'status' => StatusEnum::ENABLED])
- ->select(['id'])->asArray()->all();
- $caseIds = array_column($caseIds, "id");
- // 判断输入ID是否是数字
- $is_numeric = array_filter($params['cats'], function ($val) {
- return !is_numeric($val);
- });
- if (!empty($is_numeric)) throw new Exception('商品分类必须为数字,不能包含其它字符');
- // 判断输入ID 是否在商品分类ID中 获取符合的ID
- $caseIds = array_filter($params['cats'], function ($val) use ($caseIds) {
- return in_array($val, $caseIds);
- });
- if (empty($caseIds)) throw new Exception('商品分类不存在或被删除');
- return $caseIds;
- }
- /**
- * 格式化商品规格组数据
- * @Author bing
- * @DateTime 2021-09-03 11:06:31
- * @param array $attr_group 规格组
- * @return array
- * @copyright: Copyright (c) 2020 广东七件事集团
- */
- public static function formatGoodsAttrGroup(array $attr_group)
- {
- $format_attr_group = [];
- $group_attr_ids = [];
- $all_attr_groups = [];
- $group_attr_list = array_column($attr_group, 'attr_list', 'attr_id');
- //按规格项生成全部规格组
- foreach ($group_attr_list as $key => $group_attrs) {
- array_push($group_attr_ids, array_column($group_attrs, 'attr_id'));
- $attr_group_id = $attr_group[$key]['attr_group_id'];
- $attr_group_name = $attr_group[$key]['attr_group_name'];
- foreach ($group_attrs as $k => &$group_attr) {
- $group_attr['attr_group_id'] = $attr_group_id;
- $group_attr['attr_group_name'] = $attr_group_name;
- }
- $all_attr_groups = array_merge($all_attr_groups, $group_attrs);
- }
- $all_attr_groups = array_column($all_attr_groups, null, 'attr_id');
- // 规格组之间的笛卡尔集组合
- $attr_dika = ArrayHelper::combineDika(array_values($group_attr_ids));
- // 拼装以规格id列表为key的规格组格式,格式为attr_id1:attr_id2 => [attr_id1的规格组信息, attr_id的规格组信息]
- foreach ($attr_dika as $key => $attr_id_arr) {
- asort($attr_id_arr);
- $sign_id = implode(':', $attr_id_arr);
- $format_attr_group[$sign_id] = array_map(function ($id) use ($all_attr_groups) {
- return $all_attr_groups[$id];
- }, $attr_id_arr);
- }
- return $format_attr_group;
- }
- /**
- * 商品详情
- * @param $params
- * @return mixed|void
- */
- public static function getGoodsDetail($params)
- {
- $goods = Goods::getOne($params['where'], $params['is_array'], $params['with'], $params['order'], $params['select']);
- if (empty($goods)) return $goods;
- $goods['is_member_price'] = '0';
- $goods['price_type'] = '新品';
- $goods['sales_num'] += $goods['virtual_sales'];
- $sku_arr = $attr_name_arr = [];
- unset($goods['virtual_sales']);
- $goods_services_ids = explode(',', $goods['services']);
- $service_list = GoodsService::findAll([
- 'mall_id' => $params['mall_id'],
- 'id' => $goods_services_ids,
- 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]
- ]);
- $service_arr = [];
- foreach ($service_list as $v) {
- $arr = explode('|', $v['name']);
- array_push($service_arr, ...$arr);
- }
- $goods['service_list'] = $service_arr;
- return $goods;
- }
- //商品导出表头标题
- public static function fieldsList($field = null)
- {
- $fields = [
- 'id' => '商品ID',
- 'goods_name' => '商品名',
- 'price' => '售价',
- 'cost_price' => '成本价',
- 'goods_no' => '商品货号',
- 'original_price' => '划线价',
- 'sales_num' => '销量',
- 'virtual_sales' => '虚拟销量',
- 'is_on_sale' => '状态',
- 'created_at' => '商品添加时间',
- 'unit' => '商品单位',
- 'stock' => '库存',
- 'cats' => '商品分类',
- 'attr_groups' => '规格',
- ];
- if ($field === null) return $fields;
- $temp = [];
- foreach ($field as $val) {
- if (isset($fields[$val])) $temp[$val] = $fields[$val];
- }
- return $temp;
- }
- //商品导出整理数据
- public static function exportHandle(array $data)
- {
- ini_set('memory_limit','512M');
- $download_id = $data['download_id'] ?? 0;
- $download = Download::findOne(['id' => $download_id]);
- if ($download) {
- try {
- $params = json_decode($download->params, true);
- $filename = $download->name;
- $type = $download->type;
- $goods_detail_model = Goods::find();
- $goods_detail_model->where(['mall_id' => $params['mall_id']]);
- $goods_detail_model->andWhere(['>', 'status', StatusEnum::DISABLED]);
- $goods_detail_model->andWhere(
- [
- 'or',
- ['!=', 'g.goods_source', 'Aiyunuo'],
- ['is', 'g.goods_source', new \yii\db\Expression('null')]
- ]
- );
- !empty($params['start']) && $goods_detail_model->andWhere(['>=', 'g.created_at', strtotime($params['start'])]);
- !empty($params['end']) && $goods_detail_model->andWhere(['<=', 'g.created_at', strtotime($params['end'])]);
- !empty($params['goods_name']) && $goods_detail_model->andWhere(['like', 'g.goods_name', '%' . trim($params['goods_name']) . '%', false]);
- // !empty($params['cats']) && $goods_detail_model->andWhere(['in', 'gcr.cate_id', $params['cats']]);
- if(!empty($params['cats'])){
- $goods_cate_relate_list = GoodsCateRelate::lists(['where'=>[['cate_id'=>$params['cats']]]]);
- $goods_ids = array_column($goods_cate_relate_list,'goods_id');
- $goods_detail_model->andWhere(['in', 'g.id', $goods_ids]);
- }
- !empty($params['goods_id']) && $goods_detail_model->andWhere(['=', 'g.id', $params['goods_id']]);
- !empty($params['choose_list']) && $goods_detail_model->andWhere(['in', 'g.id', $params['choose_list']]);
- !empty($params['goods_type']) && $goods_detail_model->andWhere(['=', 'g.goods_type', $params['goods_type']]);
- if (!empty($params['type'])) {
- switch ($params['type']) {
- case 'onsale':
- $goods_detail_model->andWhere(['=', 'g.is_on_sale', WhetherEnum::ENABLED]);
- break;
- case 'offline':
- $goods_detail_model->andWhere(['=', 'g.is_on_sale', WhetherEnum::DISABLED]);
- break;
- case 'sell-out':
- $goods_detail_model->andWhere(['=', 'g.stock', 0]);
- break;
- case 'stock-lack':
- $goods_detail_model->andWhere(new Expression('g.stock <= g.stock_warning'));
- break;
- }
- }
- //连表
- // $goods_detail_model->join('LEFT JOIN', '{{%goods_cate_relate}} gcr', 'gcr.goods_id=g.id');
- $goods_detail_model->alias('g');
- $goods_detail_model->with(['cats']);
- $goods_detail_model->orderBy('g.id desc');
- $fields = $params['fields'];
- $fields_arr = self::fieldsList(); //var_dump($fields);
- $batchSize = 5000; // 设置订单批量导出数量
- $i = 0; //循环次数
- $goodsCount = $goods_detail_model->count(); // 查询商品总条数
- $is_goods_page = $goodsCount > $batchSize; // 判断是否需要分页
- foreach ($goods_detail_model->asArray()->batch($batchSize) as $goods_details) {
- $goods_groups_max = 0;
- $goods_collective_list = [];
- foreach ($goods_details as $goods_detail) {
- $processed_goods_detail = self::processGoodsDetail($goods_detail);
- $goods_collective_list[] = $processed_goods_detail;
- $goods_groups_max = max($goods_groups_max, $processed_goods_detail['goods_group_num']);
- }
- // 处理商品规格
- $goods_export_list = [];
- foreach ($goods_collective_list as $goods_detail) {
- $goods_item = [];
- foreach ($fields as $field) {
- if ($field == 'attr_groups') {
- // 以最大的规格数量为标准,不足的规格数量用空格补齐
- foreach (range(1, $goods_groups_max) as $index) {
- $goods_item[] = $goods_detail['attr_groups'][$index - 1] ?? '';
- }
- } else {
- $goods_item[] = $goods_detail[$field] ?? '';
- }
- }
- $goods_export_list[] = $goods_item;
- }
- if (!empty($goods_export_list)) {
- $csv = new CsvTool();
- $csv->filename = $is_goods_page ? $filename . "_1" : $filename;;
- $csv->file_dirname = DownloadEnum::getTypeStorageDirMap($type);
- $csv->export_mode = CsvTool::EXPORT_MODE_ASYNC;
- $have_select_field_arr = [];
- foreach ($fields as $value) {
- if ($value == 'attr_groups' && $goods_groups_max > 0) {
- foreach (range(1, $goods_groups_max) as $index) {
- $have_select_field_arr[] = '规格' . $index;
- }
- } else {
- $have_select_field_arr[] = $fields_arr[$value];
- }
- }
- $csv->header = $have_select_field_arr;
- $csv->data = $goods_export_list;
- if ($is_goods_page && $i == 0) { // 因为订单页需要分页,所以需要设置文件名
- $download->name = $filename . '_1';
- $download->save();
- }
- if ($i >= 1) {
- $newFileName = $filename . '_' . ($i + 1);
- $csv->filename = $newFileName;
- $downloadData = array(
- 'mall_id' => $download->mall_id,
- 'mch_id' => $download->mch_id,
- 'store_id' => $download->store_id,
- 'name' => $newFileName,
- 'type' => $download->type,
- 'handler_class' => $download->handler_class,
- 'method' => $download->method,
- 'params' => $download->params,
- 'data' => $goods_export_list
- );
- $downloadModel = Download::setData($downloadData);
- if ($downloadModel === false) {
- Yii::$app->custom->logs('创建下载任务失败:' . $downloadData['name'], Download::getStaticError());
- throw new Exception('创建新下载任务失败');
- };
- $download = $downloadModel;
- }
- $csv->export();
- $csv->updateDown($download, $params['mall_id']);
- $i++;
- }
- unset($goods_export_list);
- }
- // 构建字段列表
- return true;
- } catch (\Exception $e) {
- $download->status = 3;
- $res = $download->save();
- self::setStaticError($e->getMessage());
- return $e->getMessage();
- }
- }
- }
- public static function processGoodsDetail(array $goods_detail = [])
- {
- if ($goods_detail['is_attr'] == 1 && !empty($goods_detail['attr_groups'])) {
- $attr = json_decode($goods_detail['attr_groups'], true);
- if ($attr === null) {
- // 如果解码失败,返回默认值
- $attr_arr = [];
- } else {
- $attr_arr = array_map(function ($item) {
- $item_attr_names = array_map(function ($item) {
- return htmlspecialchars($item['attr_name']); // 防止 XSS 攻击
- }, $item['attr_list']);
- return htmlspecialchars($item['attr_group_name']) . ':' . implode('、', $item_attr_names);
- }, $attr);
- }
- $goods_detail['goods_group_num'] = count($attr_arr);
- $goods_detail['attr_groups'] = $attr_arr;
- } else {
- $goods_detail['attr_groups'] = '-';
- $goods_detail['goods_group_num'] = 0;
- }
- if (!empty($goods_detail['cats'])) {
- $goods_detail['cats'] = implode('|', array_column($goods_detail['cats'], 'name')) ?? '';
- }
- if (!empty($goods_detail['created_at'])) {
- $goods_detail['created_at'] = Yii::$app->formatter->asDatetime($goods_detail['created_at'], 'Y-m-d H:i:s') ?? '';
- }
- if (in_array($goods_detail['is_on_sale'], [0, 1])) {
- $goods_detail['is_on_sale'] = $goods_detail['is_on_sale'] == 1 ? '上架' : '下架';
- }
- return $goods_detail;
- }
- /**
- * @return array
- */
- public static function goodsSearchFromAddons($field, $value, $mall_id)
- {
- $model = self::find()
- ->select(['id', 'goods_name', 'cover_pic', 'original_price', 'price'])
- ->where(['=', 'status', StatusEnum::ENABLED])
- ->andWhere(['=', 'mall_id', $mall_id])
- ->andWhere(['>', 'stock', 1]);
- if ($field == 'goods_name') $model->andWhere(['like', 'goods_name', $value]);
- $goodsLists = [];
- foreach ($model->batch(500) as $goodsList) {
- $goodsLists = array_merge($goodsLists, $goodsList);
- }
- return $goodsLists;
- }
- public static function getGoodsList($params, $is_page = true, $count = false, $is_array = true, $is_get_query = false)
- {
- if ($is_page) return Goods::listPage($params, $count, $is_array, $is_get_query);
- return Goods::lists($params);
- }
- /**
- * 商品处理
- * @Author: lun
- * @DateTime: 2022/1/19 0019 18:06
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $params
- */
- public static function goodsHandle($goods_id, $params)
- {
- try {
- $model = self::findOne(['id' => $goods_id, 'mall_id' => $params['mall_id'], 'status' => [StatusEnum::ENABLED]]);
- if (empty($model)) throw new Exception('商品不存在或已删除');
- if (!$model->load($params, '') || !$model->save()) throw new \yii\db\Exception($model->getErrorMessage());
- //判断是否安装云盟店插件
- if(MallAddons::isInstall($params['mall_id'],'Yunmeng') && $model->goods_type == 1){
- //判断云盟店插件是否已存在该商品数据
- $yunmeng_goods = \addons\Yunmeng\common\models\YunmengGoods::findOne(['mall_id' => $params['mall_id'], 'status'=>StatusEnum::ENABLED, 'goods_id'=>$goods_id]);
- if (!empty($yunmeng_goods)) {
- //修改云盟店商品状态
- \addons\Yunmeng\common\models\YunmengGoods::updateAll(['is_on_sale' => 0], ['mall_id' => $params['mall_id'], 'goods_id' => $goods_id]);
- \addons\Yunmeng\common\models\YunmengStoreGoods::updateAll(['is_on_sale' => 0], ['mall_id' => $params['mall_id'], 'goods_id' => $goods_id]);
- //下架门店商品规格
- \addons\Yunmeng\common\models\YunmengStoreGoodsAttr::updateAll(['is_on_sale' => 0], ['mall_id' => $params['mall_id'], 'goods_id' => $yunmeng_goods['goods_id']]);
- }
- }
- return true;
- } catch (Exception $e) {
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- //计算用户价
- public static function computeMemberPrice($price, $sign_id, $user_level, $is_attr, $is_member_price, $member_price_setting, $discount)
- {
- $after_price = $price;
- switch ($is_member_price) {
- case 0:
- $after_price = bcdiv($price * $discount, 10, 2);
- break;
- case 1:
- //单独设置用户价
- if (!empty($is_attr)) {
- //多规格
- if (isset($member_price_setting['level' . $user_level][$sign_id])) $member_price_discount = $member_price_setting['level' . $user_level][$sign_id];
- } else {
- //默认规格
- if (isset($member_price_setting[$user_level])) $member_price_discount = $member_price_setting[$user_level];
- }
- if (!empty($member_price_discount)) {
- // $after_price = 0;
- if ($price >= $member_price_discount) $after_price = bcmul($member_price_discount, 1, 2);
- }
- break;
- }
- return $after_price;
- }
- /**
- * 批量导入会员
- * @param $params
- * @return array|bool
- */
- public static function importGoods($params)
- {
- $t = Yii::$app->db->beginTransaction();
- try {
- $csvImport = new CsvImportForm();
- $data = $csvImport->setDataForm($params)
- ->setMaxCount(GoodsImportEnum::MAX_COUNT)
- ->setImportFieldConfig(GoodsImportEnum::HEAD_LIST)
- ->setColumnNum(GoodsImportEnum::COLUMN_NUM)
- ->readCsvData();
- if (!$data) throw new \Exception(CsvImportForm::getStaticError());
- $temp_data = array_column($data, null, 'serial_number');
- if (count($temp_data) != count($data)) throw new \Exception('存在重复序号');
- unset($temp_data);
- $result = self::handleImportData($params, $data, 1);
- $t->commit();
- return ['counts' => $result['counts'], 'success_counts' => $result['success_counts'], 'fail_counts' => $result['fail_counts']];
- } catch (\Exception $e) {
- self::$static_error = $e->getMessage();
- $t->rollBack();
- return false;
- }
- }
- public static function importGoodsSync($params)
- {
- $t = Yii::$app->db->beginTransaction();
- try {
- $basePath = \Yii::$app->basePath;
- if (empty($_FILES) || !isset($_FILES['file'])) throw new \Exception('请上传csv文件');
- $fileName = $_FILES['file']['name'];
- $tmpName = $_FILES['file']['tmp_name'];
- $basePath = $basePath . '/web/uploads/goods/';
- if (!is_dir($basePath)) mkdir($basePath, 0775, true);
- $ext = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
- if ($ext != 'csv') throw new \Exception('请上传csv文件');
- $uploadFile = $basePath . 'ADMIN-' . date('Y-m-d His') . '.' . $ext;
- //上传
- move_uploaded_file($tmpName, $uploadFile);
- $success_counts = $fail_counts = 0;
- $counts = 0;
- $param['counts'] = $counts;
- $param['success_counts'] = $success_counts;
- $param['fail_counts'] = $fail_counts;
- $param['upload_file'] = $uploadFile;
- $param['mall_id'] = $params['mall_id'];
- $param['source'] = self::class;
- $import_log_model = ImportLog::setData($param);
- if (!$import_log_model) throw new \Exception(ImportLog::getStaticError());
- $t->commit();
- $params = ['id' => $import_log_model['id']];
- Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK_IMPORT_USER, RabbitMqEnum::IMPORT_USER, $params, self::class, 'syncImportGoods');
- return ['counts' => $counts, 'success_counts' => $success_counts, 'fail_counts' => $fail_counts];
- } catch (\Exception $e) {
- self::$static_error = $e->getMessage();
- $t->rollBack();
- return false;
- }
- }
- public static function handleImportData($params, $data, $sync = 1)
- {
- $success_counts = $fail_counts = 0;
- $counts = count($data);
- $fail_arr = [];
- $temp_data = [];
- $attr_suffix_arr = ['a', 'b', 'c', 'd', 'e'];
- $bannar_pic_suffix_arr = ['a', 'b', 'c', 'd', 'e', 'f', 'h', 'i'];
- $goods_detail_suffix_arr = ['a', 'b', 'c', 'd', 'e', 'f', 'h', 'i', 'j'];
- // 方便记录错误
- $data = array_column($data, null, 'serial_number');
- foreach ($data as $val) {
- $val['data_row_id'] = $val['serial_number'];
- $val['mall_id'] = $params['mall_id'];
- if (empty($val['serial_number'])) {
- $val['reason'] = '序号为空';
- list($fail_arr, $fail_counts) = ImportFailLog::packFailArr($val, $fail_arr, $fail_counts);
- continue;
- }
- if (empty($val['goods_type'])) {
- $val['reason'] = '商品类型为空';
- list($fail_arr, $fail_counts) = ImportFailLog::packFailArr($val, $fail_arr, $fail_counts);
- continue;
- }
- if (empty($val['goods_no'])) {
- $val['reason'] = '商品编号为空';
- list($fail_arr, $fail_counts) = ImportFailLog::packFailArr($val, $fail_arr, $fail_counts);
- continue;
- }
- if (empty($val['goods_name'])) {
- $val['reason'] = '商品名称为空';
- list($fail_arr, $fail_counts) = ImportFailLog::packFailArr($val, $fail_arr, $fail_counts);
- continue;
- }
- if (empty($val['cate'])) {
- $val['reason'] = '商品分类为空';
- list($fail_arr, $fail_counts) = ImportFailLog::packFailArr($val, $fail_arr, $fail_counts);
- continue;
- }
- if (empty($val['price'])) {
- $val['reason'] = '商品售价为空';
- list($fail_arr, $fail_counts) = ImportFailLog::packFailArr($val, $fail_arr, $fail_counts);
- continue;
- }
- if (empty($val['stock'])) {
- $val['reason'] = '商品库存为空';
- list($fail_arr, $fail_counts) = ImportFailLog::packFailArr($val, $fail_arr, $fail_counts);
- continue;
- }
- if (empty($val['cover_pic'])) {
- $val['reason'] = '商品主图为空';
- list($fail_arr, $fail_counts) = ImportFailLog::packFailArr($val, $fail_arr, $fail_counts);
- continue;
- }
- if (!isset($temp_data[$val['goods_no']])) {
- $temp_data[$val['goods_no']] = [
- // 存储每一行的数据
- 'list' => [],
- // 存储规格名称与规格值
- 'attr_groups' => [],
- // 第一个规格名的规格值图片
- 'attr_value_pic' => [],
- ];
- }
- $name = '';
- $temp_attr_list = [];
- $exist_attr = 0;
- // 处理商品规格
- foreach ($attr_suffix_arr as $v) {
- $attr_column_name = 'attr_name_' . $v;
- $attr_column_value = 'attr_value_' . $v;
- if (!isset($val[$attr_column_name])) continue;
- if (empty($val[$attr_column_name])) continue;
- if (!isset($val[$attr_column_value])) continue;
- if (empty($val[$attr_column_value])) continue;
- if (!isset($attr_column_name, $temp_data[$val['goods_no']]['attr_groups'])) {
- $temp_data[$val['goods_no']]['attr_groups'][$val[$attr_column_name]] = [];
- }
- $temp_data[$val['goods_no']]['attr_groups'][$val[$attr_column_name]][] = $val[$attr_column_value];
- $temp_data[$val['goods_no']]['attr_groups'][$val[$attr_column_name]] = array_unique($temp_data[$val['goods_no']]['attr_groups'][$val[$attr_column_name]]);
- if ($v == 'a' && !empty($val['pic_url'])) {
- $temp_data[$val['goods_no']]['attr_value_pic'][$val[$attr_column_value]] = $val['pic_url'];
- }
- $name .= $val[$attr_column_name] . ':' . $val[$attr_column_value] . ':';
- $temp_attr_list[] = [
- 'attr_id' => 0,
- 'attr_name' => $val[$attr_column_value],
- 'attr_group_id' => 0,
- 'attr_group_name' => $val[$attr_column_name],
- 'pic_url' => ''
- ];
- $exist_attr++;
- }
- if ($exist_attr <= 0) {
- $val['reason'] = '商品规格全为空';
- list($fail_arr, $fail_counts) = ImportFailLog::packFailArr($val, $fail_arr, $fail_counts);
- unset($temp_data[$val['goods_no']]);
- continue;
- }
- $val['name'] = trim($name, ':');
- $val['temp_attr_list'] = $temp_attr_list;
- // 处理商品轮播图
- foreach ($bannar_pic_suffix_arr as $k => $bannar_item) {
- $bannar_pic_column = 'bannar_pic_' . $bannar_item;
- if (!isset($val[$bannar_pic_column])) continue;
- if (empty($val[$bannar_pic_column])) continue;
- $val['bannar_pic'][] = ['id' => $k, 'pic_url' => $val[$bannar_pic_column]];
- }
- // 处理商品详情图片
- $goods_detail_str = '<p>';
- foreach ($goods_detail_suffix_arr as $detail_item) {
- $goods_detail_column = 'goods_detail_' . $detail_item;
- if (!isset($val[$goods_detail_column])) continue;
- if (empty($val[$goods_detail_column])) continue;
- $goods_detail_str .= '<img src="' . $val[$goods_detail_column] . '" />';
- }
- $goods_detail_str .= '</p>';
- $val['goods_detail'] = $goods_detail_str;
- $val['cats'] = explode(',', $val['cate']);
- $temp_data[$val['goods_no']]['list'][] = $val;
- }
- $extra = [
- 'full_reduce_price_limit' => 0,
- 'full_reduce_money' => 0,
- 'is_member_price' => 0,
- 'is_show_price' => 0,
- 'member_price_switch' => 0,
- 'score_give_setting' => [
- 'code' => 'score',
- 'name' => '积分',
- 'give_num' => 0,
- 'give_type' => 1,
- ],
- 'member_buy_limit_setting' => [
- 'member_level' => [],
- 'every_num' => 0,
- 'every_day_num' => 0,
- 'every_week_num' => 0,
- 'every_month_num' => 0,
- 'nums' => 0,
- 'total_num' => 0,
- ],
- 'currency_deduct_setting' => [],
- 'score_deduct_setting' => [
- 'code'=>'score',
- 'name'=> '积分',
- 'is_enable'=> 0,
- 'deduct_num'=> 0,
- 'deduct_type' => 1,
- ],
- ];
- foreach ($temp_data as $goods_no => $list) {
- $sku = $list['list'];
- $attr_groups = $list['attr_groups'];
- $attr_value_pic = $list['attr_value_pic'];
- $is_attr = count($sku) > 1 ? 1 : 0;
- $goods_type_res = array_search($sku[0]['goods_type'], GoodsImportEnum::GOODS_TYPE);
- $temp = [
- 'goods_name' => $sku[0]['goods_name'],
- 'subtitle' => $sku[0]['subtitle'] ?? '',
- 'cats' => $sku[0]['cats'],
- 'is_attr' => $is_attr,
- 'price' => !empty($sku[0]['price']) ? $sku[0]['price'] : 0,
- 'cost_price' => !empty($sku[0]['cost_price']) ? $sku[0]['cost_price'] : 0,
- 'original_price' => !empty($sku[0]['original_price']) ? $sku[0]['original_price'] : 0,
- 'sales_num' => !empty($sku['sales_num']) ? $sku['sales_num'] : 0,
- 'virtual_sales' => !empty($sku[0]['virtual_sales']) ? $sku[0]['virtual_sales'] : 0,
- 'unit' => $sku[0]['unit'] ?? '',
- 'stock' => !empty($sku[0]['stock']) ? $sku[0]['stock'] : 0,
- 'cover_pic' => $sku[0]['cover_pic'] ?? '',
- 'bannar_pic' => $sku[0]['bannar_pic'] ? $sku[0]['bannar_pic'] : [],
- 'freight_type' => ['1'],
- 'freight_rules_type' => 2,
- 'freight_id' => 3,
- 'shipping_fee' => 0,
- 'free_shipping_num' => 0,
- 'free_shipping_money' => 0,
- 'is_on_sale' => 1, //上架状态
- 'goods_type' => $goods_type_res ? $goods_type_res : 1, //商品类型
- 'mall_id' => $params['mall_id'],
- 'detail' => $sku[0]['goods_detail'],
- 'extra' => $extra,
- 'serial_number' => $sku[0]['serial_number']
- ];
- // 处理多规格
- if ($is_attr == 1) {
- $attr = [];
- $temp_attr_groups = [];
- $attr_groups_index = 1;
- $attr_list_index = 2;
- $temporary_attr_list = [];
- $temporary_attr_group_list = [];
- foreach ($attr_groups as $group_name => $group) {
- $attr_list = [];
- foreach ($group as $attr_item) {
- $attr_list[] = [
- 'attr_id' => $attr_list_index,
- 'attr_name' => $attr_item,
- 'pic_url' => $attr_value_pic[$attr_item] ?? '',
- ];
- $temporary_attr_list[$attr_item] = $attr_list_index;
- $attr_list_index++;
- }
- $temp_attr_groups[] = [
- 'attr_group_id' => $attr_groups_index,
- 'attr_group_name' => $group_name,
- 'attr_list' => $attr_list
- ];
- $temporary_attr_group_list[$group_name] = $attr_groups_index;
- $attr_groups_index++;
- }
- $temp['attr_groups'] = $temp_attr_groups;
- foreach ($sku as $ku) {
- $temp_sku = [];
- $temp_sku['price'] = !empty($ku['price']) ? $ku['price'] : 0;
- $temp_sku['stock'] = !empty($ku['stock']) ? $ku['stock'] : 0;
- $temp_sku['original_price'] = !empty($ku['original_price']) ? $ku['original_price'] : 0;
- $temp_sku['cost_price'] = !empty($ku['cost_price']) ? $ku['cost_price'] : 0;
- $temp_sku['name'] = $ku['name'];
- $temp_sku['weight'] = 0;
- $temp_sku['goods_no'] = $goods_no;
- $temp_sku['attr_list'] = $ku['temp_attr_list'];
- $temp_sku['data_row_id'] = $ku['data_row_id'];
- foreach ($temp_sku['attr_list'] as &$sku_attr) {
- $sku_attr['attr_id'] = $temporary_attr_list[$sku_attr['attr_name']];
- $sku_attr['attr_group_id'] = $temporary_attr_group_list[$sku_attr['attr_group_name']];
- }
- unset($sku_attr);
- $temp_sku['sign_id'] = implode(':', array_column($temp_sku['attr_list'], 'attr_id'));
- $attr[] = $temp_sku;
- }
- $temp['attr'] = $attr;
- }
- $res = self::setData($temp);
- if ($res) {
- $success_counts += $is_attr == 1 ? count($temp['attr']) : 1;
- } else {
- $reason = self::getStaticError();
- if ($is_attr == 1) {
- foreach ($temp['attr'] as $temp_attr) {
- $temp_params = $data[$temp_attr['data_row_id']];
- $temp_params['reason'] = $reason;
- list($fail_arr, $fail_counts) = ImportFailLog::packFailArr($temp_params, $fail_arr, $fail_counts);
- }
- } else {
- $temp_params = $data[$temp['serial_number']];
- $temp_params['reason'] = $reason;
- list($fail_arr, $fail_counts) = ImportFailLog::packFailArr($temp_params, $fail_arr, $fail_counts);
- }
- }
- }
- $param['counts'] = $counts;
- $param['success_counts'] = $success_counts;
- $param['fail_counts'] = $fail_counts;
- $param['source'] = self::class;
- $param['mall_id'] = $params['mall_id'];
- if ($sync == 0) {
- $param['fail_arr'] = $fail_arr;
- // 异步返回数据
- return $param;
- } else {
- $import_log_model = ImportLog::setData($param);
- if (!$import_log_model) throw new \Exception(ImportLog::getStaticError());
- if (!empty($fail_arr)) {
- $res = ImportFailLog::setData($fail_arr, $import_log_model->id);
- if (!$res) throw new \Exception(ImportFailLog::getStaticError());
- }
- }
- }
- //异步执行
- public static function syncImportGoods($params)
- {
- try {
- if (empty($params['id'])) throw new Exception('缺少参数id');
- $model = ImportLog::findOne(['id' => $params['id']]);
- if (empty($model['upload_file'])) throw new Exception('缺少参数upload_file');
- $upload_file = $model['upload_file'];
- $mall_id = $model['mall_id'];
- $cvsFile = fopen($upload_file, 'r'); //开始读取csv文件数据
- $i = 0;
- $head_list = GoodsImportEnum::HEAD_LIST;
- $head_list_fields = array_column($head_list, 'field');
- $rows = [];
- while ($fileData = fgetcsv($cvsFile)) {
- $i++;
- if ($i == 1) continue; //过滤表头
- $temp_row = [];
- foreach ($fileData as $k => $val) {
- $val = mb_convert_encoding(trim($val), "UTF-8", "GBK");
- foreach ($head_list_fields as $key => $field) {
- if ($k == $key) {
- $temp_row[$field] = $val;
- continue;
- }
- }
- }
- $rows[] = $temp_row;
- }
- $temp_rows = array_column($rows, null, 'serial_number');
- if (count($temp_rows) == count($rows)) {
- $result = self::handleImportData(['mall_id' => $mall_id], $rows, 0);
- $model->counts = $result['counts'];
- $model->success_counts = $result['success_counts'];
- $model->fail_counts = $result['fail_counts'];
- } else {
- $model->counts = count($rows);
- $model->success_counts = 0;
- $model->fail_counts = $model->counts;
- $result = [];
- }
- if (!$model->save()) throw new \Exception($model->getErrorMessage());
- $fail_arr = $result['fail_arr'] ?? [];
- if (!empty($fail_arr)) {
- $res = ImportFailLog::setData($fail_arr, $model->id);
- if (!$res) throw new \Exception(ImportFailLog::getStaticError());
- }
- } catch (\Exception $e) {
- $msg = __METHOD__ . ' msg: ' . $e->getMessage();
- Yii::error($msg);
- }
- }
- public static function getAddonsGoodsSetting($mall_id, $addons_fields, $data, $source)
- {
- if (empty($addons_fields)) return $data;
- if (in_array('system_score', $addons_fields)) {
- $data = GoodsExtra::getMarketingScore($mall_id, $source, $data);
- }
- $new_data = GlobalInvoke::exec(GlobalInvoke::APPEND_FIELDS_GOODS_DATA, $mall_id, [
- 'source' => $source,
- 'mall_id' => $mall_id,
- 'data' => $data,
- 'addons_fields' => $addons_fields
- ]);
- return $new_data ? $new_data : $data;
- }
- /**
- * 获取默认的属性
- * @Author: lun
- * @DateTime: 2023/4/20 0020 17:04
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $mall_id
- * @param array $goods_detail
- * @return array
- */
- public static function getDefaultSetting($mall_id, $goods_detail = [], $mch_id = 0)
- {
- // 获取默认的服务
- $serive_ids = GoodsService::find()->select('id')->where(['mall_id' => $mall_id, 'mch_id' => $mch_id, 'is_default' => StatusEnum::ENABLED, 'status' => StatusEnum::ENABLED])->column();
- if ($serive_ids) $goods_detail['services'] = $serive_ids;
- // 获取默认的标签
- $label_ids = GoodsLabel::find()->select('id')->where(['mall_id' => $mall_id, 'mch_id' => $mch_id, 'is_default' => StatusEnum::ENABLED, 'status' => StatusEnum::ENABLED])->column();
- if ($label_ids) $goods_detail['labels'] = $label_ids;
- // 获取默认的运费模板
- $rule_id = FreightRules::find()->select('id')->where(['mall_id' => $mall_id, 'mch_id' => $mch_id, 'is_default' => StatusEnum::ENABLED, 'status' => StatusEnum::ENABLED])->scalar();
- if ($rule_id) $goods_detail['freight_id'] = $rule_id;
- return $goods_detail;
- }
- public static function getComponentData($params, $mall_id)
- {
- $params['type'] = $params['type'] ?? 'goods';
- switch ($params['type']) {
- case 'goods':
- $goods_ids = Json::decode($params['id'], true);
- break;
- case 'goods_cate':
- $cate_id_arr = [$params['id']];
- $goods_cate_relate_list = GoodsCateRelate::findAll(['cate_id' => $cate_id_arr]);
- $goods_ids = array_column($goods_cate_relate_list, 'goods_id') ?? [];
- break;
- default:
- break;
- }
- $goods_params = ['where' => [['=', 'mall_id', $mall_id], ['in', 'id', $goods_ids], ['=', 'status', StatusEnum::ENABLED], ['=', 'is_on_sale', StatusEnum::ENABLED]], 'select' => 'id,goods_name,subtitle,price,original_price,cover_pic,(sales_num+virtual_sales) as sales_num, is_attr', 'index' => 'id'];
- if (isset($params['limit'])) {
- $goods_params['limit'] = $params['limit'];
- $goods_params['page'] = 1;
- }
- return self::lists($goods_params, true);
- }
- }
|