| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335 |
- <?php
- /**
- * 厦门云联储网络科技有限公司
- * https://www.baokuaiyun.com
- * Copyright (c) 2023 爆块云 All rights reserved.
- */
- namespace app\models;
- use app\constants\OptionSetting;
- use app\jobs\storeSync\DiyCommon;
- use app\modules\admin\models\imageSearch\UploadImageForm;
- use app\modules\admin\models\MerchantForm;
- use yii\db\ActiveRecord;
- use yii\behaviors\TimestampBehavior;
- use yii\helpers\ArrayHelper;
- use yii\helpers\Json;
- use app\modules\admin\models\pospal\PospalForm;
- use app\models\common\Upload;
- use app\modules\admin\models\MchForm;
- /**
- * Class Goods
- * @package app\modules\common\models
- * @property integer $id
- * @property integer $store_id
- * @property integer $mini_id
- * @property string $product_id
- * @property string $name
- * @property string $price
- * @property string $goods_send_profit
- * @property string $goods_take_price
- * @property string $original_price
- * @property integer $integral_price
- * @property string $detail
- * @property string $cat_id
- * @property integer $status
- * @property integer $is_delete
- * @property string $attr
- * @property string $service
- * @property integer $sort
- * @property integer $virtual_sales
- * @property string $cover_pic
- * @property string $video_url
- * @property string $unit
- * @property integer $individual_share
- * @property string $share_commission_first
- * @property string $share_commission_second
- * @property string $share_commission_third
- * @property double $weight
- * @property string $freight
- * @property string $full_cut
- * @property string $integral
- * @property integer $use_attr
- * @property integer $share_type
- * @property integer $quick_purchase
- * @property integer $hot_cakes
- * @property integer $mch_id
- * @property integer $goods_num
- * @property string $cost_price
- * @property integer $member_discount
- * @property string $rebate
- * @property integer $mch_sort
- * @property integer $type
- * @property integer $is_level
- * @property integer $confine_count
- * @property integer $is_negotiable
- * @property string $goods_share_title
- * @property string $goods_share_logo
- * @property string $goods_share_desc
- * @property string $created_at
- * @property string $updated_at
- * @property string $goods_no
- * @property string $key_word
- * @property string $pieces
- * @property string $forehead
- * @property integer $attr_setting_type
- * @property string $verify_card_id
- * @property integer $is_verify
- * @property string $delivery_type
- * @property integer $product_type
- * @property integer $is_form
- * @property string $form_name
- * @property integer $is_recommend
- * @property integer $rate
- * @property integer $rate_type
- * @property integer $md_rate
- * @property integer $md_rate_type
- * @property integer $dl_p_rate
- * @property integer $dl_c_rate
- * @property integer $dl_d_rate
- * @property integer $dl_p_rate_type
- * @property integer $dl_c_rate_type
- * @property integer $dl_d_rate_type
- * @property integer $md_hx_rate
- * @property integer $md_hx_rate_type
- * @property integer $md_food_id
- * @property integer $cloud_goods_id
- * @property integer $cloud_supplier_id
- * @property string $material_id
- * @property string $material_key
- * @property integer $service_book_num
- * @property integer $can_set_md_price
- * @property integer $delivery_rules_id
- * @property integer $goods_area_rules_id
- * @property integer $order_rules_buyer_location_id
- * @property integer $food_ext_goods_id
- * @property integer $stbz_goods_id
- * @property integer $is_wholesale
- * @property string $accessories_image
- * @property string $service_book_desc
- * @property string $parameter_list
- * @property string $buy_type_info
- * @property string $share_commission_new_first
- * @property string $share_commission_new_second
- * @property string $share_commission_new_third
- * @property integer $send_verify_card_num
- * @property string $coupon_card_id
- * @property integer $send_coupon_card_num
- * @property integer $brand_id
- * @property integer $is_risk
- * @property integer $share_holder_profit_switch
- * @property string $share_holder_high_profit
- * @property integer $time_made_day
- * @property integer $time_shelf_life
- * @property integer $warehouse_id
- * @property integer $warehouse_zone_id
- * @property integer $is_front_delivery
- * @property integer $is_front_centralize
- * @property integer $agent_goods_status
- * @property integer $cloud_inventory_id
- */
- class VideoShopGoods extends ActiveRecord
- {
- //从银豹同步时设置为1
- public $yinbaoSync = 0;
- //活动价(临时字段)
- public $activityPrice = 0;
- /**
- * 正常商品
- */
- const GOODS_TYPE_NORMAL = 0;
- /**
- * 日期预约商品
- */
- const GOODS_TYPE_DATE = 1;
- /**
- * 时间预约商品
- */
- const GOODS_TYPE_TIME = 2;
- /**
- * 虚拟商品
- */
- const GOODS_TYPE_VIRTUAL = 3;
- /**
- * 积分商品
- */
- const GOODS_TYPE_INTEGRAL = 4;
- /**
- * 认养商品
- */
- const GOODS_TYPE_ADOPT = 5;
- /**
- * 上门服务商品
- */
- const GOODS_TYPE_WORKER = 6;
- /**
- * 展示类商品
- */
- const GOODS_TYPE_VIEW = 7;
- public static $validProductType = [
- self::GOODS_TYPE_NORMAL,
- self::GOODS_TYPE_DATE,
- self::GOODS_TYPE_TIME,
- self::GOODS_TYPE_VIRTUAL,
- self::GOODS_TYPE_INTEGRAL,
- self::GOODS_TYPE_ADOPT,
- self::GOODS_TYPE_WORKER,
- self::GOODS_TYPE_VIEW,
- ];
- const STATUS_NORMAL = 1; // 上架
- const STATUS_DISABLE = 0; // 下架
- /**
- * 价格面议
- */
- const GOODS_NEGOTIABLE = '价格面议';
- /**
- * @var int|mixed|null
- */
- public static function tableName()
- {
- return '{{%video_shop_goods}}';
- }
- public function behaviors()
- {
- return [
- [
- // 自动更新创建和更新时间
- 'class' => TimestampBehavior::class,
- 'value' => time()
- ]
- ];
- }
- public function rules()
- {
- return [
- [['store_id', 'name', 'detail', 'attr'], 'required', 'on' => self::whenSave()],
- [['integral_price','store_id', 'cat_id', 'status', 'is_delete', 'created_at', 'updated_at',
- 'sort', 'individual_share', 'freight', 'use_attr', 'share_type', 'is_verify',
- 'quick_purchase', 'hot_cakes', 'mch_id', 'mch_audit', 'mch_audit_time', 'goods_num', 'member_discount', 'virtual_sales',
- 'mch_sort', 'type', 'is_level', 'confine_count', 'is_negotiable', 'pieces', 'attr_setting_type', 'product_type', 'is_form', 'is_recommend', 'md_food_id', 'rate_type', 'md_rate_type', 'md_hx_rate_type', 'dl_p_rate_type', 'dl_c_rate_type', 'dl_d_rate_type', 'cloud_goods_id', 'service_book_num', 'delivery_rules_id', 'goods_area_rules_id'], 'integer', 'on' => self::whenSave()],
- [['price', 'original_price', 'share_commission_first', 'share_commission_second',
- 'share_commission_third', 'weight', 'cost_price', 'rebate', 'forehead', 'rate', 'md_rate', 'md_hx_rate', 'dl_p_rate', 'dl_c_rate', 'dl_d_rate','queue_rate'], 'number', 'on' => self::whenSave()],
- [['detail', 'attr', 'cover_pic', 'full_cut', 'integral'], 'string', 'on' => self::whenSave()],
- [['name', 'unit','goods_share_title','goods_share_logo','goods_share_desc', 'goods_no', 'key_word', 'delivery_type', 'form_name','accessories_image', 'buy_type_info'], 'string', 'max' => 255, 'on' => self::whenSave()],
- [['service'], 'string', 'max' => 2000, 'on' => self::whenSave()],
- [['video_url'], 'string', 'max' => 2048, 'on' => self::whenSave()],
- [['verify_card_id', 'material_id','material_key', 'parameter_list', 'share_commission_new_first', 'share_commission_new_second', 'share_commission_new_third', 'coupon_card_id', 'unit'], 'string'],
- [['status','cat_id','store_id'], 'integer', 'on' => self::whenList()],
- [['name'], 'string', 'max' => 60, 'on' => self::whenList()],
- [['status', 'is_verify'], 'default', 'value' => -1, 'on' => self::whenList()],
- [['mch_id'], 'default', 'value' => 0, 'on' => self::whenList()],
- [['store_id'], 'default', 'value' => 1, 'on' => self::whenList()],
- [['can_set_md_price', 'order_rules_buyer_location_id', 'food_ext_goods_id', 'goods_send_profit', 'goods_take_price', 'service_book_desc', 'share_holder_high_profit'], 'safe'],
- [['stbz_goods_id', 'is_wholesale', 'cloud_goods_id', 'cloud_supplier_id', 'send_verify_card_num', 'send_verify_card_num', 'integral_price', 'brand_id', 'is_risk', 'share_holder_profit_switch', 'warehouse_id', 'warehouse_zone_id', 'is_front_delivery', 'is_front_centralize', 'agent_goods_status'], 'integer'],
- [['time_made_day', 'time_shelf_life'], 'safe'],
- ];
- }
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'store_id' => 'Store ID',
- 'name' => '商品名称',
- 'goods_send_profit' => '商品让利比例返给用户',
- 'goods_take_price' => '商品让利比例扣除用户',
- 'price' => '售价',
- 'original_price' => '原价(只做显示用)',
- 'integral_price' => '积分兑换',
- 'detail' => '商品详情,图文',
- 'cat_id' => '商品类别',
- 'status' => '上架状态:0=下架,1=上架',
- 'is_delete' => 'Is Delete',
- 'attr' => '规格的库存及价格',
- 'service' => '商品服务选项',
- 'sort' => '排序 升序',
- 'virtual_sales' => '虚拟销量',
- 'cover_pic' => '商品缩略图',
- 'video_url' => '视频',
- 'unit' => '单位',
- 'individual_share' => '是否单独分销设置:0=否,1=是',
- 'share_commission_first' => '一级分销佣金比例',
- 'share_commission_second' => '二级分销佣金比例',
- 'share_commission_third' => '三级分销佣金比例',
- 'weight' => '重量',
- 'freight' => '运费模板ID',
- 'full_cut' => '满减',
- 'integral' => '积分设置',
- 'use_attr' => '是否使用规格:0=不使用,1=使用',
- 'share_type' => '佣金配比 0--百分比 1--固定金额',
- 'quick_purchase' => '是否加入快速购买 0--关闭 1--开启',
- 'hot_cakes' => '是否加入热销 0--关闭 1--开启',
- 'mch_id' => '入驻商户的id',
- 'goods_num' => '商品总库存',
- 'cost_price' => 'Cost Price',
- 'member_discount' => '是否参与会员折扣 0=参与 1=不参与',
- 'rebate' => '自购返利',
- 'mch_sort' => '多商户自己的排序',
- 'type' => '类型 0--商城或多商户 2--砍价商品',
- 'is_level' => '是否享受会员折扣',
- 'confine_count' => '购买限制:0.不限制|大于0等于限购数量',
- 'is_negotiable' => '面议方式',
- 'attr_setting_type' => '分销设置类型 0.普通设置|1.详细设置',
- 'verify_card_id' => '绑定核销卡',
- 'goods_share_title' => '商品分享标题',
- 'goods_share_desc' => '商品分享缩略图',
- 'goods_share_logo' => '商品分享简介',
- 'created_at' => '创建时间',
- 'updated_at' => '更新时间',
- 'goods_no' => '商品货号',
- 'key_word' => '关键字',
- 'pieces' => '单品满件包邮',
- 'forehead' => '单品满额包邮',
- 'is_verify' => '入驻商待审核 0 待审核 1审核通过并上架 2审核未通过',
- 'rate_type' => "分红类型",
- 'cloud_goods_id' => "云仓商品ID",
- 'service_book_num' => '预约商品数量',
- 'can_set_md_price' => '门店溢价销售 0禁止 1允许',
- 'delivery_rules_id' => '运送规则id',
- 'goods_area_rules_id' => '销售规则id',
- 'stbz_goods_id' => '胜天半子goods_id',
- 'order_rules_buyer_location_id' => '销售区域规则id',
- 'food_ext_goods_id' => '附加商品区id',
- 'is_wholesale' => '是否批发',
- 'accessories_image' => '装配图片',
- 'parameter_list' => '商品参数',
- 'service_book_desc' => '酒店商品特殊日期值',
- 'buy_type_info' => '购买类型',
- 'share_commission_new_first' => '新一级分销佣金比例, 带等级',
- 'share_commission_new_second' => '新二级分销佣金比例, 带等级',
- 'share_commission_new_third' => '新三级分销佣金比例, 带等级',
- 'send_coupon_card_num' => '赠送优惠券数量',
- 'send_verify_card_num' => '赠送核销卡数量',
- 'coupon_card_id' => '赠送优惠券id',
- 'is_risk' => '商品是否有风险',
- 'is_front_delivery' => '是否是云仓仓库配送商品(配送方式只能为到店自提和同城配送)',
- 'is_front_centralize' => '是否是云仓集采商品(配送方式只能为到店自提)',
- 'agent_goods_status' => '是否是云仓代理配送产品(配送方式只能为到店自提或快递 如果是可上门安装则为快递 如果不上门安装则为到店自提 大于0表示是代理配送商品 2表示需要上门安装)'
- ];
- }
- public static function whenSave ()
- {
- return 'save';
- }
- public static function whenList ()
- {
- return 'list';
- }
- public function afterFind()
- {
- parent::afterFind();
- try {
- // 处理cover_pic
- //不做判断时 手机端修改商品价格时会将封面图设置为null
- if ($this->cover_pic) {
- $this->cover_pic = Upload::getImgStyle($this->cover_pic);
- }
- // 处理规格图片
- if ($this->attr) {
- $attrs = json_decode($this->attr, true);
- if (\is_array($attrs)) {
- foreach ($attrs as &$attr) {
- $attr['pic'] = Upload::getImgStyle($attr['pic']);
- }
- $this->attr = json_encode($attrs, JSON_UNESCAPED_UNICODE);
- }
- }
- if ($this->detail) {
- // 处理detail,使用正则匹配并替换图片地址
- $detail = preg_replace_callback('/<img.*?src="(.*?)".*?\/>/', function ($matches) {
- return '<img src="' . Upload::getImgStyle($matches[1]) . '">';
- }, $this->detail);
- if ($detail) {
- $this->detail = $detail;
- }
- }
- } catch (\Throwable $e) {}
- }
- public function beforeSave($insert)
- {
- //在总后台 店铺后台 需要设置店铺 是否允许店铺上传产品,如果设置为不允许,就只能云仓选品,PC端后台和手机端后台都需要控制。
- if ($insert) {
- $is_add_goods = Option::get('is_add_goods', $this->store_id, 'store', 1)['value'];
- if (intval($this->cloud_goods_id) === 0 && (int)$is_add_goods === 0) {
- $this->addError('is_add_goods', '错误:当前店铺不允许上传产品,只能云仓选品');
- return false;
- }
- }
- if (intval($this->is_front_delivery)) {
- $this->delivery_type = json_encode(['shop', 'delivery'], JSON_UNESCAPED_UNICODE);
- }
- if (intval($this->is_front_centralize)) {
- $this->delivery_type = json_encode(['shop'], JSON_UNESCAPED_UNICODE);
- }
- if (intval($this->agent_goods_status)) {
- $delivery_type = ['shop'];
- if (intval($this->agent_goods_status) === 2) {//需要上门安装
- $delivery_type = ['express'];
- }
- $this->delivery_type = json_encode($delivery_type, JSON_UNESCAPED_UNICODE);
- }
- if ($this->use_attr == 1) {
- $this->goods_num = $this->getNum();
- if ($this->attr && !in_array($this->product_type, [1, 2])) {
- $attr_rows = json_decode($this->attr, true);
- $price_arr = array_column($attr_rows, 'price');
- $integral_price_arr = array_column($attr_rows, 'integral_price');
- if (!empty($price_arr)) {
- $this->price = min($price_arr);
- }
- if (!empty($integral_price_arr)) {
- $this->integral_price = intval(min($integral_price_arr));
- }
- }
- }
- if (in_array($this->product_type, [6])) {
- $this->use_attr = 0;
- }
- if (empty($this->buy_type_info)) {
- $this->buy_type_info = json_encode([
- 'type' => 0,
- 'member_level' => 0
- ]);
- }
- if (intval($this->individual_share) === 1) {
- $share_level = ShareLevel::find()->where(['store_id' => $this->store_id, 'is_delete' => ShareLevel::SHARE_NOT_DELETE, 'status' => ShareLevel::STATUS_ON])
- ->select('level id, name')->orderBy('level ASC')->asArray()->all();
- $share_level = array_merge([[
- 'id' => 0,
- 'name' => '默认等级'
- ]], $share_level);
- // if ((!is_null($this->attr_setting_type)) && intval($this->attr_setting_type) === 1) {
- // $attr = json_decode($this->attr, true);
- // foreach ($attr as $attr_) {
- // foreach ($share_level as $share_level_) {
- // if (empty($attr_['share_commission_level_' . $share_level_['id']])) {
- // $this->addError('type', '操作失败,分销详细模式等级【' . $share_level_['name'] . '】为空');
- // return false;
- // }
- // if (
- // !isset($attr_['share_commission_level_' . $share_level_['id']]['share_commission_first'])
- // || !isset($attr_['share_commission_level_' . $share_level_['id']]['share_commission_second'])
- // || !isset($attr_['share_commission_level_' . $share_level_['id']]['share_commission_third'])
- // ) {
- // $this->addError('type', '操作失败,分销详细模式等级【' . $share_level_['name'] . '】分销内容不完整');
- // return false;
- // }
- // }
- //
- // }
- // } else
- if ((!is_null($this->attr_setting_type)) && intval($this->attr_setting_type) === 0) {
- $share_commission_new_first = json_decode($this->share_commission_new_first, true);
- foreach ($share_level as $share_level_) {
- $share_commission_new_first['share_commission_level_' . $share_level_['id']] = $share_commission_new_first['share_commission_level_' . $share_level_['id']] ?? 0;
- }
- $this->share_commission_new_first = json_encode($share_commission_new_first);
- $share_commission_new_second = json_decode($this->share_commission_new_second, true);
- foreach ($share_level as $share_level_) {
- $share_commission_new_second['share_commission_level_' . $share_level_['id']] = $share_commission_new_second['share_commission_level_' . $share_level_['id']] ?? 0;
- }
- $this->share_commission_new_second = json_encode($share_commission_new_second);
- $share_commission_new_third = json_decode($this->share_commission_new_third, true);
- foreach ($share_level as $share_level_) {
- $share_commission_new_third['share_commission_level_' . $share_level_['id']] = $share_commission_new_third['share_commission_level_' . $share_level_['id']] ?? 0;
- }
- $this->share_commission_new_third = json_encode($share_commission_new_third);
- }
- }
- if ($this->dirtyAttributes && ($this->dirtyAttributes['attr'] || isset($this->dirtyAttributes['use_attr']))) {
- $this->setActivityAttr($this->id, $this->attr, $this->use_attr);
- }
- //加价保护start
- $platform_profit_strategy = Option::get('platform_profit_strategy', 0, 'store')['value'];
- $platform_profit_strategy = json_decode($platform_profit_strategy, true);
- if (!empty($platform_profit_strategy)) {
- $result = $this->getMarkupProtectionRules($this->price, $this->dirtyAttributes['attr'], $this->cloud_goods_id);
- if (intval($platform_profit_strategy['is_risk_control']) && $result['platform_negotiated_price_error'] && $this->id && intval($this->status) === 1) {
- //开启风控
- $id = \Yii::$app->cache->get('risk_control_model_id');
- if ($id && !\Yii::$app->queue->isDone($id)) {//\Yii::$app->queue->status($id)
- \Yii::$app->queue->remove($id);
- }
- $id =\queue_push(new \app\jobs\RiskControlModelJob(['risk_control_model' => $platform_profit_strategy['risk_control_model'], 'goods_id' => $this->id]));
- \Yii::$app->cache->set('risk_control_model_id', $id);
- // $form = new RiskControlForm();
- // $form->riskControlHandle($platform_profit_strategy['risk_control_model'], $this->id);
- }
- // if (intval($platform_profit_strategy['is_markup_protection'])) {
- // if (!empty($platform_profit_strategy['markup_protection_rules'])) {
- // $markup_protection_rules = $platform_profit_strategy['markup_protection_rules'];
- // $rules_price = 0;
- // $rules_attr_price = [];
- // $title = "";
- // if (intval($markup_protection_rules['title']) === 0) {
- // $title = "利润";
- // //规则名称:0 利润
- // $rules_price = $result['goods_rules_price'];
- // $rules_attr_price = $result['goods_attr_rules_price'];
- // }
- // $compare = "";
- // if (intval($markup_protection_rules['compare']) === 0) {
- // //比较符号 0小于 1小于等于 2等于 3大于等于 4大于
- // }
- // $open = false;
- // if ($markup_protection_rules['price'] > 0) {
- // //价格
- // if ($rules_price > 0) {
- // switch ($markup_protection_rules['compare']) {
- // case 1:
- // $compare = "小于等于";
- // if ($rules_price <= $markup_protection_rules['price']) {
- // $open = true;
- // }
- // break;
- // case 2:
- // $compare = "等于";
- // if (floatval($rules_price) == floatval($markup_protection_rules['price'])) {
- // $open = true;
- // }
- // break;
- // case 3:
- // $compare = "大于等于";
- // if ($rules_price >= $markup_protection_rules['price']) {
- // $open = true;
- // }
- // break;
- // case 4:
- // $compare = "大于";
- // if ($rules_price > $markup_protection_rules['price']) {
- // $open = true;
- // }
- // break;
- // default:
- // $compare = "小于";
- // if ($rules_price < $markup_protection_rules['price']) {
- // $open = true;
- // }
- // break;
- // }
- // }
- //
- // if (!empty($rules_attr_price)) {
- // foreach ($rules_attr_price as $price_item) {
- // switch ($markup_protection_rules['compare']) {
- // case 1:
- // if ($price_item <= $markup_protection_rules['price']) {
- // $open = true;
- // }
- // break;
- // case 2:
- // if (floatval($price_item) == floatval($markup_protection_rules['price'])) {
- // $open = true;
- // }
- // break;
- // case 3:
- // if ($price_item >= $markup_protection_rules['price']) {
- // $open = true;
- // }
- // break;
- // case 4:
- // if ($price_item > $markup_protection_rules['price']) {
- // $open = true;
- // }
- // break;
- // default:
- // if ($price_item < $markup_protection_rules['price']) {
- // $open = true;
- // }
- // break;
- // }
- // }
- // }
- //
- // }
- // if (intval($markup_protection_rules['operate']) === 0) {
- // //操作
- // $attribute_title = "price";
- // if ($open && $this->dirtyAttributes['price']) {
- // $this->price = $this->oldAttributes['price'];
- // }
- //
- // if ($open && !empty($this->dirtyAttributes['attr'])) {
- // $attribute_title = "attr.price";
- // $this->attr = $this->oldAttributes['attr'];
- // }
- // if ($open) {
- // $this->addError($attribute_title, '错误:' . $title . $compare . $markup_protection_rules['price']);
- // return false;
- // }
- // }
- //
- // }
- // }
- }
- //加价保护end
- return parent::beforeSave($insert);
- }
- public function getMarkupProtectionRules($price, $attr, $cloud_goods_id) {
- $goods_rules_price = 0;
- $goods_attr_rules_price = [];
- $platform_negotiated_price_error = false;
- if ($cloud_goods_id > 0) {
- //获取云仓商品
- $cloud_url = "/goods/getGoodsInfo";
- $cloud_data = [];
- $cloud_data['goods_id'] = $cloud_goods_id;
- $domain = (new OptionSetting)->getCloudDomainName();
- $cloud_info = cloud_post($domain . $cloud_url, $cloud_data);
- $cloud_info = json_decode($cloud_info, true);
- if($cloud_info['code'] != 0){
- //debug_log($cloud_info, 'risk_control_result.log');
- }else{
- //获取商家利润 商家售价-平台供货价
- $cloud_goods = $cloud_info['data']['goods'];
- $goods_rules_price = floatval(sprintf('%.2f', $price - $cloud_goods['platform_negotiated_price']));
- //新增风控判断 商品售价小于平台协议价
- if ($price < $cloud_goods['platform_negotiated_price']) {
- $platform_negotiated_price_error = true;
- }
- if (!empty($attr)) {
- //规格中的云仓商品协议价大于商品售价处理
- $attr = json_decode($attr, true);
- $cloud_goods['attrs'] = json_decode($cloud_goods['attrs'], true);
- foreach ($cloud_goods['attrs'] as $cloud_attr_item) {
- $cloud_no = $cloud_attr_item['no'];
- foreach ($attr as &$attr_item) {
- $_no = $attr_item['no'];
- if (!empty($cloud_no) && !empty($_no) && $_no == $cloud_no) {
- $attr_rules_price = floatval(sprintf('%.2f', $attr_item['price'] -
- (!empty($cloud_attr_item['platform_negotiated_price']) ? $cloud_attr_item['platform_negotiated_price'] : $cloud_attr_item['price'])
- ));
- array_push($goods_attr_rules_price, $attr_rules_price);
- //新增风控判断 商品售价小于平台协议价
- if ($attr_item['price'] < (!empty($cloud_attr_item['platform_negotiated_price']) ? $cloud_attr_item['platform_negotiated_price'] : $cloud_attr_item['price'])
- ) {
- $platform_negotiated_price_error = true;
- }
- }
- }
- }
- }
- }
- }
- return [
- 'goods_rules_price' => $goods_rules_price,
- 'goods_attr_rules_price' => $goods_attr_rules_price,
- 'platform_negotiated_price_error' => $platform_negotiated_price_error
- ];
- }
- public function afterSave($insert, $changedAttributes)
- {
- parent::afterSave($insert, $changedAttributes);
- \Yii::error([__METHOD__, $insert, $changedAttributes]);
- if (($this->yinbaoSync == 0)) {
- (new PospalForm(['store_id' => $this->store_id]))->afterGoodsSave($this, $changedAttributes);
- }
- \app\modules\admin\models\jushuitan\JuShuiTanForm::afterGoodsSave($this);
- \app\modules\admin\models\erp\InventoryForm::afterGoodsSave($this, $insert, $changedAttributes);
- if(!$insert && in_array('cover_pic',array_keys($changedAttributes))){
- // 通知修改了商品主图 删除之前的商品图 并上传新的商品主图
- (new UploadImageForm())->updateGoodsImage($this->id);
- }
- /* 2024年3月9日16:31:13 begin WPing id999新需求:降价通知 */
- // if(!$insert && in_array('price',array_keys($changedAttributes))){
- // if($this->price < $changedAttributes['price']) {//如果降价了
- // GoodsLoweringPrice::sendMsg($this->id, $this->store_id, $this->price, $changedAttributes['price']);
- // }
- // }
- if(!$insert && in_array('attr',array_keys($changedAttributes))){
- // //debug_log('规格被修改,走进来了');
- $before_goods = [
- 'id' => $this->id,
- 'attr' => array_key_exists('attr',$changedAttributes) ? $changedAttributes['attr'] : $this->attr,
- 'price' => array_key_exists('price',$changedAttributes) ? $changedAttributes['price'] : $this->price,
- 'mch_id' => array_key_exists('mch_id',$changedAttributes) ? $changedAttributes['mch_id'] : $this->mch_id,
- 'is_level' => array_key_exists('is_level',$changedAttributes) ? $changedAttributes['is_level'] : $this->is_level,
- ];
- GoodsLoweringPrice::sendMsg($this->id, $this->store_id, $before_goods);
- }
- /* end */
- //获取禁售目录
- $form = new MerchantForm();
- $form->store_id = $this->store_id;
- $result = $form->getCloudForbiddenGoods();
- if ($result['code'] === 0) {
- $cloud_goods_id = $result['data']['goods_id'];
- //如果当前云仓id在禁售目录就禁售
- if (in_array($this->cloud_goods_id, $cloud_goods_id)) {
- VideoShopGoods::updateAll(['is_forbidden' => 1, 'is_delete' => 1], ['id' => $this->id]);
- }
- }
- (new DiyCommon)->JobBehaviors($this->store_id, StoreSyncExtLog::TYPE_PRODUCT, [$this->id]);
- }
- //更改规格之后修改营销活动产品规格
- public function setActivityAttr($id, $attr, $use_attr) {
- try {
- $attr = json_decode($attr, true);
- //拼团
- $pt_goods_list = PtActivityGoods::find()->where(['is_delete' => 0, 'goods_id' => $id])->all();
- $this->activityAttrHandle($attr, $pt_goods_list, 'pt_price');
- //砍价
- $cut_goods_list = ActivityCutPriceGoods::find()->where(['is_delete' => 0, 'goods_id' => $id])->all();
- $this->activityAttrHandle($attr, $cut_goods_list, 'price', 1);
- //秒杀
- $seckill_goods_list = SeckillActivityGoods::find()->where(['is_delete' => 0, 'goods_id' => $id])->all();
- $this->activityAttrHandle($attr, $seckill_goods_list, 'seckill_price', 2);
- //新人
- $new_user_goods_list = ActivityNewUserGoods::find()->where(['is_delete' => 0, 'goods_id' => $id])->all();
- $this->activityAttrHandle($attr, $new_user_goods_list, 'price', 3);
- //新人专享
- } catch (\Exception $e) {
- //debug_log($e->getMessage(), 'Exception.log');
- }
- }
- /**
- * $goods_attr 商品规格
- * $activity_goods_list 活动产品
- * $price_name 价格名称
- * $type 活动类型
- */
- public function activityAttrHandle($goods_attr, $activity_goods_list, $price_name, $type = 0) {
- foreach ($activity_goods_list as $activity_goods_item) {
- $activity_goods = ArrayHelper::toArray($activity_goods_item);
- $ext_attrs = json_decode($activity_goods['attr'], true);
- $new_attr = [];
- foreach($goods_attr as $attr_index => &$attr_item){
- if (intval($this->use_attr) === 0) {
- if ($attr_index > 0) {
- continue;
- }
- }
- $open = false;
- foreach($ext_attrs as $ext_attr){
- $ext_attr_id = array_column($ext_attr['attr_list'], 'attr_id');
- $attr_id = array_column($attr_item['attr_list'], 'attr_id');
- sort($ext_attr_id);
- sort($attr_id);
- if($attr_id == $ext_attr_id){
- $open = true;
- //活动价格
- $attr_item[$price_name] = $ext_attr[$price_name] ?: $attr_item['price'];
- //活动库存
- $attr_item['num'] = $ext_attr['num'];
- //商品原售价
- switch ($type) {
- case 3://新人
- case 1://砍价
- $attr_item['oldPrice'] = $attr_item['price'];
- break;
- case 2://秒杀
- $attr_item['seckill_num'] = $ext_attr['seckill_num'];
- break;
- }
- }
- }
- if (!$open) {
- $attr_item[$price_name] = $attr_item['price'];
- }
- $new_attr[] = $attr_item;
- }
- $activity_goods_item->attr = json_encode($new_attr, JSON_UNESCAPED_UNICODE);
- $activity_goods_item->save();
- }
- }
- public static function skuAttr(&$goods, $goods_attr_item = []) {
- $sku_attr = [];
- try {
- $attr_ids = array_column($goods_attr_item['attr_list'] ?? $goods_attr_item, 'attr_id');
- sort($attr_ids);
- $sku_attr_ids = implode('_', $attr_ids);
- $attrs = json_decode($goods['attr'], true);
- foreach($attrs as &$attr){
- $attr['cyy_id'] = 'cyy_' . $goods['id'];
- $attr['cyy_skuId'] = self::skuid1($goods['id'], $attr);
- $attr_ids = array_column($attr['attr_list'], 'attr_id');
- sort($attr_ids);
- $attr['attr_ids'] = implode(',', $attr_ids);
- $item_attr_ids = implode('_', $attr_ids);
- if($item_attr_ids === $sku_attr_ids){
- $sku_attr = $attr;
- }
- }
- $goods['attr'] = json_encode($attrs);
- } catch (\Exception $e) {
- \Yii::error([$e, $goods, $attr]);
- \Yii::error([__METHOD__, $e->getMessage()]);
- }
- return $sku_attr;
- }
- public static function goodsIdOriginal($cyy_id = '') {
- return substr($cyy_id, strpos($cyy_id, '_') + 1);
- }
- public static function skuAttrOriginal(&$goods, $cyy_skuId = '') {
- $sku_attr = [];
- try {
- self::skuAttr($goods);
- $attrs = json_decode($goods['attr'], true);
- foreach($attrs as &$attr){
- if($cyy_skuId === $attr['cyy_skuId']){
- $sku_attr = $attr;
- }
- }
- } catch (\Exception $e) {
- \Yii::error($e);
- \Yii::error([__METHOD__, $e->getMessage()]);
- }
- return $sku_attr;
- }
- public static function skuid1($id, $goods_attr_item = []) {
- $attr_ids = array_column($goods_attr_item['attr_list'] ?? $goods_attr_item, 'attr_id');
- sort($attr_ids);
- return 'cyy_' . $id . '_' . implode('_', $attr_ids);
- }
- public static function skuid2($id, $goods_attr_item = []) {
- return $goods_attr_item['no'];
- }
- public static function getSkuIdAttr($goods, $skuid = '') {
- self::skuAttr($goods);
- $attrs = json_decode($goods['attr'], true);
- foreach($attrs as $attr){
- if($attr['cyy_skuId'] === $skuid){
- return $attr;
- }
- }
- return [];
- }
- /**
- * @param array $arr | status 状态 name 名称 store_id 商城id
- * @return array
- */
- public static function getAllianceList ($arr = [])
- {
- $query = VideoShopGoods::find()->alias('g')
- ->where(['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(['AND', ['<>', 'g.store_id', 0], ['<>', 'g.product_type', 4]]);
- if (isset($arr['store_id']) && $arr['store_id'] > 0){
- $query->andWhere(['store_id'=>$arr['store_id']]);
- }
- if (isset($arr['name']) && !empty($arr['name'])) {
- $query->andWhere([
- 'like',
- 'g.name',
- $arr['name']
- ]);
- }
- $select = ['g.id', 'g.name', 'g.status','g.store_id', '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','g.store_id'];
- $query->select($select)->orderBy(['g.sort' => SORT_ASC, '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' => Cat::tableName()], 'gc.cat_id=c.id')
- ->where([ 'gc.goods_id' => $v['id'] ])
- ->select(['c.name'])
- ->asArray()
- ->all();
- $pagination['data'][$k]['cat'] = $goods_cat;
- if($v['store_id']>0){
- $pagination['data'][$k]['storeInfo'] = Store::find()->where(['id'=>$v['store_id'],'is_delete'=>0])->select(['id', 'name', 'logo', 'coordinate', 'created_at', 'category_id', 'tags', 'sales', 'rank', 'per_spend','business_model','address'])->asArray()->one();
- $wechatInfo = WechatConfig::find()->where(['store_id'=>$v['store_id'],'is_delete'=>0,'type'=>1])->asArray()->one();
- if(isset($pagination['data'][$k]['storeInfo']['id']) && $pagination['data'][$k]['storeInfo']['id']>0){
- if($wechatInfo){
- $pagination['data'][$k]['storeInfo']['wechat_app_id'] = $wechatInfo['app_id'];
- }
- $alipayInfo = Option::get('alipay_config',$v['store_id'],'alipay');
- if($alipayInfo && $alipayInfo['value']){
- $alipayInfo = json_decode($alipayInfo['value'],true);
- $pagination['data'][$k]['storeInfo']['alipay_app_id'] = $alipayInfo['app_id'];
- }
- }
- }
- }
- unset($pagination['list']);
- $book_goods_ids = self::checkBookGoods();
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'book_goods_ids' => $book_goods_ids,
- 'data' => $pagination
- ];
- }
- /**
- * @param array $arr | status 状态 name 名称 cat_id 分类id mch_id 入驻商 is_verify 审核 is_league 已设置联盟券 select 查询数据
- * @return array
- */
- public static function getList ($arr = [])
- {
- $query = VideoShopGoods::find()->alias('g')
- ->leftJoin(['wh' => Warehouse::tableName()], 'wh.id = g.warehouse_id')
- ->leftJoin(['whz' => WarehouseZone::tableName()], 'whz.id = g.warehouse_zone_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['cloud_inventory_id'])){
- if (!empty($arr['cloud_inventory_id'])){
- $query = $query->andWhere(['>', 'g.cloud_inventory_id', 0]);
- }else {
- $query = $query->andWhere(['=', 'g.cloud_inventory_id', 0]);
- }
- }
- if (!empty($arr['select'])) {
- $select = $arr['select'];
- } else {
- $select = ['g.id', 'g.name', 'g.status', 'g.service','g.integral_price','wh.name as wh_name','whz.name as whz_name',
- '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.detail', 'g.use_attr',
- 'g.product_type', 'g.is_recommend', 'g.cloud_supplier_id', 'g.delivery_rules_id',
- 'g.time_made_day', 'g.time_shelf_life',
- 'g.mch_id', 'g.mch_audit', 'g.mch_audit_time',
- 'g.cost_price', 'g.goods_no', 'g.goods_take_price', 'g.goods_send_profit', 'g.cloud_goods_id', 'g.brand_id', 'g.is_risk', 'g.accessories_image', 'g.cloud_inventory_id'];
- if (intval($arr['product_type']) === VideoShopGoods::GOODS_TYPE_WORKER) {
- $select = array_merge($select, ['wge.desc']);
- }
- }
- $sort = '';
- $sort_warn_goods_timeout = 0;
- $warn_goods_timeout = self::warn_goods_timeout(get_store_id());
- if (isset($arr['warn_goods_timeout'])) {
- if($warn_goods_timeout){
- $sort_warn_goods_timeout = 1;
- $query->andWhere(['>', 'g.time_made_day', 0]);
- $query->andWhere(['<', '(g.time_made_day + g.time_shelf_life * 86400)', (time() + $warn_goods_timeout * 86400)]);
- }
- }
- if (($arr['warn_goods_timeout_begin_time'])) {
- $sort_warn_goods_timeout = 1;
- $query->andWhere(['>', 'g.time_made_day', 0]);
- $query->andWhere(['>=', '(g.time_made_day + g.time_shelf_life * 86400)', strtotime($arr['warn_goods_timeout_begin_time'])]);
- }
- if (($arr['warn_goods_timeout_end_time'])) {
- $sort_warn_goods_timeout = 1;
- $query->andWhere(['>', 'g.time_made_day', 0]);
- $query->andWhere(['<', '(g.time_made_day + g.time_shelf_life * 86400)', strtotime($arr['warn_goods_timeout_end_time']) + 86400]);
- }
- if($sort_warn_goods_timeout){
- $select = array_merge($select, ['(g.time_made_day + g.time_shelf_life * 86400) as sort_warn_goods_timeout']);
- $sort = 'sort_warn_goods_timeout asc';
- }
- if ((isset($arr['mch']) && $arr['mch'] == 1)) {
- if($arr['mch_id'] > 0){
- $query->andWhere(['g.mch_id' => $arr['mch_id']]);
- }else{
- $query->andWhere(['>', 'g.mch_id', 0]);
- $query->andWhere(['g.mch_id' => Mch::find()->select('id')->where(['store_id' => get_store_id(), 'is_delete' => 0])]);
- }
- if (isset($arr['mch_audit']) && $arr['mch_audit'] > -1) {
- $query->andWhere(['g.mch_audit' => $arr['mch_audit']]);
- }
- if ($arr['mch_audit_time_begin']) {
- $query->andWhere(['>=', 'mch_audit_time', strtotime($arr['mch_audit_time_begin'])]);
- }
- if ($arr['mch_audit_time_end']) {
- $query->andWhere(['<=', 'mch_audit_time', strtotime($arr['mch_audit_time_end'])]);
- }
- if (isset($arr['mch_common_cat_id']) && $arr['mch_common_cat_id'] > 0) {
- $query->andWhere(['g.mch_id' => Mch::find()->select('id')->where(['store_id' => get_store_id(), 'mch_common_cat_id' => $arr['mch_common_cat_id']])]);
- }
- }else{
- $query->andWhere(['g.mch_id' => 0]);
- }
- if (isset($arr['status']) && $arr['status'] > -1) {
- $query->andWhere([
- 'g.status' => $arr['status']
- ]);
- }
- if (!empty($arr['goods_no'])) {
- $query->andWhere([
- 'g.goods_no' => $arr['goods_no']
- ]);
- }
- //智配图片商品去除
- if (!empty($arr['is_accessories'])) {
- $query->andWhere(['OR', [
- 'g.accessories_image' => ''
- ], [
- 'IS', 'g.accessories_image', NULL
- ]
- ]);
- }
- if (!empty($arr['is_verify_card'])) {
- $query->andWhere([
- 'g.product_type' => 0,
- 'g.status' => 1
- ]);
- } else {
- if (isset($arr['product_type']) && in_array($arr['product_type'], VideoShopGoods::$validProductType)) {
- $query->andWhere([
- 'g.product_type' => $arr['product_type']
- ]);
- }else{
- //剔除掉积分商品
- $query->andWhere([
- 'g.product_type' => [VideoShopGoods::GOODS_TYPE_NORMAL,VideoShopGoods::GOODS_TYPE_DATE,VideoShopGoods::GOODS_TYPE_TIME,VideoShopGoods::GOODS_TYPE_VIRTUAL,VideoShopGoods::GOODS_TYPE_ADOPT]
- ]);
- }
- }
- //是否是云仓商品
- if (isset($arr['is_cloud']) && $arr['is_cloud'] ) {
- $query->andWhere([ '>', 'g.cloud_goods_id' , 0 ]);
- }
- //是否拼团商品
- if (isset($arr['is_pt']) && $arr['is_pt'] ) {
- $time = time();
- $pt_activity_id = PtActivity::find()->where(['is_delete' => 0, 'store_id' => get_store_id()])
- ->andWhere(['AND', ['<', 'start_time', $time], ['>', 'end_time', $time]])->select('id')
- ->column();
- $queryPt = PtActivityGoods::find()->alias('pag')->where(['pag.is_delete' => 0, 'g.status' => 1, 'g.is_delete' => 0])
- ->andWhere(['pag.activity_id' => $pt_activity_id])
- ->leftJoin(['g' => VideoShopGoods::tableName()], 'pag.goods_id = g.id')
- ->select('g.id');
- $query->andWhere(['g.id' => $queryPt]);
- $query->leftJoin(['pag' => PtActivityGoods::tableName()], 'g.id = pag.goods_id');
- $query->leftJoin(['pa' => PtActivity::tableName()], 'pa.id = pag.activity_id');
- $select = array_merge($select, ['pa.party_size', 'pa.party_type', 'pa.party_goods_count']);
- }
- // $query->leftJoin(['sp' => Supplier::tableName()], 'sp.cloud_supplier_id = g.cloud_supplier_id');
- //价格区间
- if (!empty($arr['price_type'])) {
- if ((int)$arr['price_type'] === 1) {
- if ($arr['low_price']) {
- $query->andWhere(['>=', 'g.cost_price', $arr['low_price']]);
- }
- if ($arr['high_price']) {
- $query->andWhere(['<=', 'g.cost_price', $arr['high_price']]);
- }
- }
- if ((int)$arr['price_type'] === 2) {
- if ($arr['low_price']) {
- $query->andWhere(['>=', 'g.price', $arr['low_price']]);
- }
- if ($arr['high_price']) {
- $query->andWhere(['<=', 'g.price', $arr['high_price']]);
- }
- }
- }
- if (isset($arr['low_num']) && $arr['low_num']) {
- $query->andWhere(['>=', 'g.goods_num', $arr['low_num']]);
- }
- if (isset($arr['high_num']) && $arr['high_num']) {
- $query->andWhere(['<=', 'g.goods_num', $arr['high_num']]);
- }
- if (isset($arr['supplier_id']) && $arr['supplier_id']) {
- $query->andWhere(['g.cloud_supplier_id' => $arr['supplier_id']]);
- }
- if (isset($arr['delivery_rules_id']) && $arr['delivery_rules_id']) {
- $query->andWhere(['g.delivery_rules_id' => $arr['delivery_rules_id']]);
- }
- if (isset($arr['name']) && !empty($arr['name'])) {
- $query->andWhere([
- 'or',
- ['like', 'g.name', $arr['name']],
- ['like', 'g.key_word', $arr['name']],
- ]);
- }
- if (intval($arr['product_type']) === VideoShopGoods::GOODS_TYPE_WORKER) {
- $query->leftJoin(['wge' => WorkerGoodsExt::tableName()], 'g.id = wge.goods_id');
- }
- if (intval($arr['product_type']) === VideoShopGoods::GOODS_TYPE_TIME) {
- $query->leftJoin(['bge' => BookingGoodsExt::tableName()], 'g.id = bge.goods_id');
- }
- if (isset($arr['cat_id']) && $arr['cat_id'] > 0) {
- if (intval($arr['product_type']) === VideoShopGoods::GOODS_TYPE_WORKER) {
- $query->andWhere(['wge.cat_id' => $arr['cat_id']]);
- } else if (intval($arr['product_type']) === VideoShopGoods::GOODS_TYPE_TIME){
- $query->andWhere(['bge.cat_id' => $arr['cat_id']]);
- } else {
- if ($arr['mch_id'] > 0) {
- $query->leftJoin(['mgc' => MchGoodsCat::tableName()], 'g.id = mgc.goods_id')->andWhere([
- 'mgc.cat_id' => MchCat::getCatId($arr['cat_id'])
- ])->groupBy('mgc.goods_id');
- } else {
- $query->leftJoin(['gc' => GoodsCat::tableName()], 'g.id = gc.goods_id')->andWhere([
- 'gc.cat_id' => Cat::getCatId($arr['cat_id'])
- ])->groupBy('gc.goods_id');
- }
- }
- }
- //门店增加收银台 查询商品列表逻辑
- if ($arr['md_id'] && $arr['md_id'] > 0) {
- $md = Md::findOne($arr['md_id']);
- if ($md && intval($md->is_single)) {
- $query->leftJoin(['mg' => MdGoods::tableName()], 'g.id = mg.goods_id')->andWhere(['mg.md_id' => $arr['md_id'], 'mg.status' => 1]);
- $select = array_merge($select, ['md_goods_price' => 'mg.price', 'md_goods_num' => 'mg.goods_num', 'md_goods_attr' => 'mg.attr']);
- }
- }
- if (isset($arr['mch_cat_id']) && $arr['mch_cat_id'] > 0) {
- $query->leftJoin(['mgc' => MchGoodsCat::tableName()], 'g.id = mgc.goods_id')->andWhere([
- 'mgc.cat_id' => MchCat::getCatId($arr['mch_cat_id'])
- ])->groupBy('mgc.goods_id');
- }
- // 根据入驻商查询 0 平台 -1 全部入驻商 >0 对应入驻商
- // if (empty($arr['mch_id'])) {
- // $query->andWhere(['g.mch_id' => 0]);
- // } elseif(isset($arr['mch_id']) && $arr['mch_id'] == -1) {
- // $query->andWhere([
- // '>',
- // 'g.mch_id',
- // 0
- // ]);
- // } elseif (isset($arr['mch_id']) && $arr['mch_id'] > 0){
- // $query->andWhere(['g.mch_id' => $arr['mch_id']]);
- // }
- // 获取入驻商待审核商品
- if (isset($arr['is_verify'])) {
- $query->andWhere([
- 'g.is_verify' => $arr['is_verify']
- ]);
- }
- if (isset($arr['is_league']) && $arr['is_league'] > -1) {
- if ($arr['is_league'] == 1) {
- $query->andWhere([
- 'or',
- ['>', 'goods_take_price', 0],
- ['>', 'goods_send_profit', 0],
- ]);
- }else{
- $query->andWhere([
- 'and',
- [
- 'or',
- ['goods_take_price' => 0],
- ['goods_take_price' => null],
- ],
- [
- 'or',
- ['goods_send_profit' => 0],
- ['goods_send_profit' => null],
- ],
- ]);
- }
- }
- $query->select($select);
- if (!empty($arr['sort'])) {
- $query->orderBy(['g.sort' => $arr['sort'] == 'asc' ? SORT_ASC : SORT_DESC, 'g.id' => SORT_DESC]);
- } else {
- $query->orderBy(['g.id' => SORT_DESC]);
- }
- if($sort){
- $query->orderBy($sort);
- }
- // return $query->createCommand()->getRawSql();
- $pagination = pagination_make($query);
- if (isset($arr['mch']) && $arr['mch'] == 1) {
- $mchList = (new MchForm(['store_id' => get_store_id()]))->mchSelectList();
- $mchCommonCatSelectList = (new MchForm(['store_id' => get_store_id()]))->mchCommonCatSelectList();
- }
- $list = $pagination['list'];
- foreach($list as &$item) {
- if (isset($arr['mch']) && $arr['mch'] == 1) {
- $item['mch'] = $mchList['data'][$item['mch_id']];
- $item['mch_common_cat'] = $mchCommonCatSelectList['data'][$item['mch']['mch_common_cat_id']];
- $goods_cat = MchGoodsCat::find()->alias('mgc')
- ->leftJoin(['mc' => MchCat::tableName()], 'mgc.cat_id=mc.id')
- ->where([ 'mgc.goods_id' => $item['id'] ])
- ->select(['mc.name'])
- ->asArray()
- ->all();
- $item['mch_cat'] = $goods_cat;
- }
- $item['warehouse_name'] = $item['wh_name'].'-'.$item['whz_name'];
- $item['timeout_day'] = $item['time_made_day'] ? date('Y-m-d', $item['time_made_day'] + 86400 * $item['time_shelf_life']) : '--';
- $item['is_warn_goods_timeout'] = ($item['time_made_day'] && ($item['time_made_day'] + 86400 * $item['time_shelf_life']) < (time() + $warn_goods_timeout * 86400)) ? 1 : 0;
- //品牌
- $item['brand_name'] = self::getBrand($item['brand_id']) ?: '暂无品牌';
- //门店增加收银台 查询商品列表逻辑
- if ($arr['md_id'] && $arr['md_id'] > 0) {
- $item['price'] = $item['md_goods_price'] ?: $item['price'];
- $item['goods_num'] = $item['md_goods_num'] ?: $item['goods_num'];
- $item['attr'] = $item['md_goods_attr'] ?: $item['attr'];
- }
- $delivery_rules = DeliveryRules::find()->where(['id' => $item['delivery_rules_id'], 'is_delete' => 0, 'store_id' => get_store_id()])->select('type, times, days')->asArray()->one();
- if ($delivery_rules) {
- $delivery_rules['times'] = (int)$delivery_rules['type'] === 1 ? '' : date("m月d日 H:i:s", $delivery_rules['times']);
- }
- $supplier = Supplier::findOne(['cloud_supplier_id'=>$item['cloud_supplier_id']]);
- $item['supplier_name'] = $supplier->supplier_name ?: ($supplier->name ?: '');
- $item['supplier_logo'] = $supplier->logo ?: '';
- $item['supplier_phone'] = $supplier->phone ?: '';
- $item['delivery_rules'] = $delivery_rules ?: null;
- $goods_cat = [];
- //如果是积分兑换商品
- if (isset($arr['product_type']) && $arr['product_type'] == VideoShopGoods::GOODS_TYPE_INTEGRAL) {
- $goods_cat = GoodsCat::find()->alias('gc')
- ->leftJoin(['c' => NewIntegralCat::tableName()], 'gc.cat_id=c.id')
- ->where([ 'gc.goods_id' => $item['id'] ])
- ->select(['c.name'])
- ->asArray()
- ->all();
- } elseif (isset($arr['product_type']) && $arr['product_type'] == VideoShopGoods::GOODS_TYPE_WORKER) {
- $worker_goods_ext = WorkerGoodsExt::findOne(['goods_id' => $item['id']]);
- if ($worker_goods_ext) {
- $goods_cat = WorkerGoodsCat::find()->where(['id' => $worker_goods_ext->cat_id])->select('name')
- ->asArray()->all();
- }
- } elseif (isset($arr['product_type']) && $arr['product_type'] == VideoShopGoods::GOODS_TYPE_TIME) {
- $booking_goods_ext = BookingGoodsExt::findOne(['goods_id' => $item['id']]);
- if ($booking_goods_ext) {
- $goods_cat = BookingGoodsCat::find()->where(['id' => $booking_goods_ext->cat_id])->select('name')
- ->asArray()->all();
- }
- } else {
- $goods_cat = GoodsCat::find()->alias('gc')
- ->leftJoin(['c' => Cat::tableName()], 'gc.cat_id=c.id')
- ->where([ 'gc.goods_id' => $item['id'] ])
- ->select(['c.name'])
- ->asArray()
- ->all();
- }
- $item['cat'] = $goods_cat;
- if (intval($arr['product_type']) === VideoShopGoods::GOODS_TYPE_WORKER) {
- $item['order_num'] = OrderDetail::find()->where(['goods_id' => $item['id']])->groupBy('order_id')->count();
- $item['sales'] = OrderDetail::find()->where(['goods_id' => $item['id']])->select('num')->sum('num');
- }
- if (intval($arr['product_type']) === VideoShopGoods::GOODS_TYPE_TIME) {
- $item['order_num'] = OrderDetail::find()->where(['goods_id' => $item['id']])->groupBy('order_id')->count();
- $item['sales'] = OrderDetail::find()->where(['goods_id' => $item['id']])->select('num')->sum('num');
- }
- }
- unset($pagination['list']);
- $book_goods_ids = self::checkBookGoods();
- return [
- 'q' => $query->createCommand()->getRawSql(),
- 'code' => 0,
- 'msg' => 'success',
- 'book_goods_ids' => $book_goods_ids,
- 'warn_goods_timeout' => $warn_goods_timeout,
- 'data' => [
- 'data' => $list,
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount']
- ]
- ];
- }
- public static function getActivityList($arr = []){
- // 先获取当前进行中的活动以及即将开始的活动
- $query = MdGroupActivitiesGoods::find()->alias('mgag')
- ->leftJoin(['mga'=>MdGroupActivities::tableName()], 'mgag.activities_id=mga.id')
- ->where(['mgag.is_delete'=>0,'mga.is_delete'=>0,'mga.store_id'=>get_store_id()])
- ->andWhere(['or',['and',['<','mga.start_time',time()],['>','mga.end_time',time()]],['and',['>=','mga.start_time',time()],['>=','mga.end_time',time()]]])
- ->leftJoin(['g'=>VideoShopGoods::tableName()],'mgag.goods_id=g.id')
- ->select(['g.id','g.name','g.cover_pic','g.original_price','price'=>'mgag.price','g.is_negotiable','g.delivery_rules_id','activity_id'=>'mga.id','activity_name'=>'mga.name']);
- $result = pagination_make($query,true);
- foreach($result['list'] as &$item){
- $delivery_rules = DeliveryRules::find()->where(['id' => $item['delivery_rules_id'], 'is_delete' => 0, 'store_id' => get_store_id()])->select('type, times, days')->asArray()->one();
- if ($delivery_rules) {
- $delivery_rules['times'] = (int)$delivery_rules['type'] === 1 ? '' : date("m月d日 H:i:s", $delivery_rules['times']);
- }
- $item['delivery_rules'] = $delivery_rules ?: null;
- }
- return [
- 'code'=>0,
- 'data'=>[
- 'data' => $result['list'],
- 'pageNo' => $result['pageNo'],
- 'totalCount' => $result['totalCount']
- ]
- ];
- }
- /**
- * @param array $arr | status 状态 name 名称 cat_id 分类id mch_id 入驻商 is_verify 审核 select 查询数据
- * @return array
- */
- public static function getFoodGoodsList ($arr = [])
- {
- $query = VideoShopGoods::find()->alias('g')
- ->where(['g.store_id' => get_store_id(), 'g.is_delete' => 0])->andWhere(['not like', 'g.name', '当面付']);
- if (isset($arr['status']) && $arr['status'] > -1) {
- $query->andWhere([
- 'g.status' => $arr['status']
- ]);
- }
- if (isset($arr['product_type']) && in_array($arr['product_type'], [0, 1, 2])) {
- $query->andWhere([
- 'g.product_type' => $arr['product_type']
- ]);
- }
- if (isset($arr['name']) && !empty($arr['name'])) {
- $query->andWhere([
- 'like',
- 'g.name',
- $arr['name']
- ]);
- }
- if (isset($arr['cat_id']) && $arr['cat_id'] > 0) {
- if ($arr['mch_id'] > 0) {
- $query->leftJoin(['mgc' => MchGoodsCat::tableName()], 'g.id = mgc.goods_id')->andWhere([
- 'mgc.cat_id' => MchCat::getCatId($arr['cat_id'])
- ])->groupBy('mgc.goods_id');
- } else {
- $query->leftJoin(['gc' => GoodsCat::tableName()], 'g.id = gc.goods_id')->andWhere([
- 'gc.cat_id' => Cat::getCatId($arr['cat_id'])
- ])->groupBy('gc.goods_id');
- }
- }
- if (isset($arr['mch_cat_id']) && $arr['mch_cat_id'] > 0) {
- $query->leftJoin(['mgc' => MchGoodsCat::tableName()], 'g.id = mgc.goods_id')->andWhere([
- 'mgc.cat_id' => MchCat::getCatId($arr['mch_cat_id'])
- ])->groupBy('mgc.goods_id');
- }
- // 根据入驻商查询 0 平台 -1 全部入驻商 >0 对应入驻商
- if (empty($arr['mch_id'])) {
- $query->andWhere(['g.mch_id' => 0]);
- } elseif(isset($arr['mch_id']) && $arr['mch_id'] == -1) {
- $query->andWhere([
- '>',
- 'g.mch_id',
- 0
- ]);
- } elseif (isset($arr['mch_id']) && $arr['mch_id'] > 0){
- $query->andWhere(['g.mch_id' => $arr['mch_id']]);
- }
- // 获取入驻商待审核商品
- if (isset($arr['is_verify'])) {
- $query->andWhere([
- 'g.is_verify' => $arr['is_verify']
- ]);
- }
- 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'];
- }
- $query->with(['mch'])->select($select)
- ->orderBy(['g.sort' => SORT_ASC, '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' => Cat::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 = self::checkBookGoods();
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'book_goods_ids' => $book_goods_ids,
- 'data' => $pagination
- ];
- }
- /**
- * 获取商品可选的规格列表
- */
- public function getAttrGroupList($is_pt = 0, $use_attr = 1, $is_platform = 0)
- {
- $attr_rows = json_decode($this->attr, true);
- if (empty($attr_rows)) {
- return [];
- }
- $attr_group_list = [];
- foreach ($attr_rows as $attr_row) {
- foreach ($attr_row['attr_list'] as $i => $attr) {
- $attr_id = $attr['attr_id'];
- $attr = Attr::findOne(['id' => $attr_id, 'is_delete' => 0]);
- if (!$attr) {
- continue;
- }
- $in_list = false;
- foreach ($attr_group_list as $j => $attr_group) {
- if ($attr_group->attr_group_id == $attr->attr_group_id) {
- $attr_obj = (object)[
- 'attr_id' => $attr->id,
- 'attr_name' => $attr->attr_name,
- ];
- if (!in_array($attr_obj, $attr_group_list[$j]->attr_list)) {
- $attr_group_list[$j]->attr_list[] = $attr_obj;
- }
- $in_list = true;
- continue;
- }
- }
- if (!$in_list) {
- $attr_group = AttrGroup::findOne(['is_delete' => 0, 'id' => $attr->attr_group_id]);
- if ($attr_group) {
- $attr_group_list[] = (object)[
- 'attr_group_id' => $attr_group->id,
- 'attr_group_name' => $attr_group->attr_group_name,
- 'attr_list' => [
- (object)[
- 'attr_id' => $attr->id,
- 'attr_name' => $attr->attr_name,
- ],
- ],
- ];
- }
- }
- }
- }
- if ((int)$use_attr === 0) {
- $attr_group_list = [
- $attr_group_list[0]
- ];
- }
- //判断是否为拼团产品
- //拼团活动信息
- if ($is_pt) {
- $query = PtActivityGoods::find()->alias('pag')->where(['pag.is_delete' => 0, 'pa.is_delete' => 0, 'pa.store_id' => get_store_id()])
- ->leftJoin(['pa' => PtActivity::tableName()], 'pag.activity_id = pa.id')
- ->andWhere(['pag.goods_id' => $this->id])
- ->select('pag.pt_price, pag.sale_num, pag.virtual_sales, pa.party_size, pa.party_type, pa.party_goods_count')
- ->andWhere(['AND', ['<', 'pa.start_time', time()], ['>', 'pa.end_time', time()]]);
- $query->andWhere(['OR', ['is_platform' => 1, 'is_platform_audit' => 1, 'status' => 1], ['is_platform' => 0]]);
- $ptGoodsActivity = $query->asArray()->one();
- if ($ptGoodsActivity) {
- $attr_name = $ptGoodsActivity['party_size'] . "人团";
- if($ptGoodsActivity['party_type'] == 1){
- $attr_name = $ptGoodsActivity['party_goods_count'] . "个商品成团";
- }
- $pt_attr = [
- [
- "attr_group_id" => -1,
- "attr_group_name" => "拼团",
- "attr_list" => [
- [
- "attr_id" => -1,
- "attr_name" => $attr_name
- ]
- ]
- ]
- ];
- $attr_group_list = array_values(array_merge($pt_attr, $attr_group_list));
- }
- }
- return $attr_group_list;
- }
- /**
- * 获取商品分类
- * @param $goods
- * @return array
- */
- public static function getCatList($goods)
- {
- $cat_list = [];
- $cat = GoodsCat::findAll(['goods_id' => $goods->id, 'store_id' => $goods->store_id, 'is_delete' => 0]);
- if ($cat) {
- foreach ($cat as $index => $value) {
- $cat_name = Cat::findOne(['id' => $value->cat_id, 'store_id' => $goods->store_id]);
- $cat_list[$index]['cat_id'] = $value->cat_id;
- $cat_list[$index]['cat_name'] = $cat_name->name;
- }
- } else {
- $cat_name = Cat::findOne(['id' => $goods->cat_id]);
- $cat_list[0]['cat_id'] = $goods->cat_id;
- $cat_list[0]['cat_name'] = $cat_name->name;
- }
- return $cat_list;
- }
- /**
- * 获取商品总库存
- * @param int $id 商品id
- */
- public function getNum($id = null)
- {
- $goods = null;
- if (!$id) {
- $goods = $this;
- } else {
- $goods = static::findOne($id);
- if (!$goods) {
- return 0;
- }
- }
- $attr = $goods->attr;
- if (get_md_id()) {
- $md_goods = MdGoods::findOne(['goods_id' => $id ?: $goods->id, 'md_id' => get_md_id()]);
- if ($md_goods) {
- $attr = $md_goods->attr;
- }
- }
- if ((int)$goods->product_type === 2) {
- return $goods->service_book_num;
- }
- if ((int)$goods->product_type === 4) {
- return $goods->goods_num;
- }
- if (!$attr) {
- return 0;
- }
- $num = 0;
- $attr_rows = json_decode($attr, true);
- foreach ($attr_rows as $attr_row) {
- $num += intval($attr_row['num']);
- }
- return $num;
- }
- /**
- * 库存增加操作
- */
- public function numAdd($attr_id_list, $num, $reset = 0)
- {
- if(!is_array($attr_id_list[0])){
- $attr_id_list = [
- ['attr_id_list' => $attr_id_list, 'num' => $num]
- ];
- }
- $_attr = $this->attr;
- $is_md = false;
- if (get_md_id()) {
- $md_goods = MdGoods::findOne(['goods_id' => $this->id, 'md_id' => get_md_id()]);
- if ($md_goods) {
- $_attr = $md_goods->attr;
- $is_md = true;
- }
- }
- $attr_group_list = json_decode($_attr);
- $add_attr_num = false;
- foreach($attr_id_list as $item){
- $num = $item['num'];
- sort($item['attr_id_list']);
- foreach ($attr_group_list as $i => $attr_group) {
- $group_attr_id_list = [];
- foreach ($attr_group->attr_list as $attr) {
- array_push($group_attr_id_list, $attr->attr_id);
- }
- sort($group_attr_id_list);
- if (!array_diff($item['attr_id_list'], $group_attr_id_list)) {
- if($reset){
- $attr_group_list[$i]->num = intval($num);
- }else{
- $attr_group_list[$i]->num = intval($attr_group_list[$i]->num) + $num;
- }
- $add_attr_num = true;
- break;
- }
- }
- }
- if (!$add_attr_num) {
- return false;
- }
- if (!$is_md) {
- $this->attr = json_encode($attr_group_list, JSON_UNESCAPED_UNICODE);
- if ($this->attr) {
- $attr_rows = json_decode($this->attr, true);
- $goods_num = 0;
- foreach ($attr_rows as $attr_row) {
- $goods_num += intval($attr_row['num']);
- }
- $this->goods_num = $goods_num;
- }
- $this->save();
- } else {
- // 门店商品逻辑
- $md_goods->attr = json_encode($attr_group_list, JSON_UNESCAPED_UNICODE);
- if ($md_goods->attr) {
- $attr_rows = json_decode($md_goods->attr, true);
- $goods_num = 0;
- foreach ($attr_rows as $attr_row) {
- $goods_num += intval($attr_row['num']);
- }
- $md_goods->goods_num = $goods_num;
- }
- $md_goods->save();
- }
- return true;
- }
- /**
- * 库存减少操作
- * @param array $attr_id_list eg. [1,4,2]
- */
- public function numSub($attr_id_list, $num)
- {
- if(!is_array($attr_id_list[0])){
- $attr_id_list = [
- ['attr_id_list' => $attr_id_list, 'num' => $num]
- ];
- }
- $_attr = $this->attr;
- $is_md = false;
- if (get_md_id()) {
- $md_goods = MdGoods::findOne(['goods_id' => $this->id, 'md_id' => get_md_id()]);
- if ($md_goods) {
- $_attr = $md_goods->attr;
- $is_md = true;
- }
- }
- $attr_group_list = json_decode($_attr);
- $sub_attr_num = false;
- foreach($attr_id_list as $item){
- $num = $item['num'];
- sort($item['attr_id_list']);
- foreach ($attr_group_list as $i => $attr_group) {
- $group_attr_id_list = [];
- foreach ($attr_group->attr_list as $attr) {
- array_push($group_attr_id_list, $attr->attr_id);
- }
- sort($group_attr_id_list);
- if (!array_diff($item['attr_id_list'], $group_attr_id_list)) {
- if ($num > intval($attr_group_list[$i]->num)) {
- return false;
- }
- $attr_group_list[$i]->num = intval($attr_group_list[$i]->num) - $num;
- $sub_attr_num = true;
- break;
- }
- }
- }
- if (!$sub_attr_num) {
- return false;
- }
- if (!$is_md) {
- $this->attr = json_encode($attr_group_list, JSON_UNESCAPED_UNICODE);
- if ($this->attr) {
- $attr_rows = json_decode($this->attr, true);
- $goods_num = 0;
- foreach ($attr_rows as $attr_row) {
- $goods_num += intval($attr_row['num']);
- }
- $this->goods_num = $goods_num;
- }
- $this->save();
- } else {
- $md_goods->attr = json_encode($attr_group_list, JSON_UNESCAPED_UNICODE);
- if ($md_goods->attr) {
- $attr_rows = json_decode($md_goods->attr, true);
- $goods_num = 0;
- foreach ($attr_rows as $attr_row) {
- $goods_num += intval($attr_row['num']);
- }
- $md_goods->goods_num = $goods_num;
- }
- $md_goods->save();
- }
- return true;
- }
- /**
- * 库存增加操作
- */
- public static function numAddStatic($goods_id, $attr_id_list, $num, $reset = 0)
- {
- $goods = VideoShopGoods::findOne($goods_id);
- if (!$goods) {
- return false;
- }
- return $goods->numAdd($attr_id_list, $num, $reset);
- }
- /**
- * 库存减少操作
- */
- public static function numSubStatic($goods_id, $attr_id_list, $num)
- {
- $goods = VideoShopGoods::findOne($goods_id);
- if (!$goods) {
- return false;
- }
- return $goods->numSub($attr_id_list, $num);
- }
- /**
- * 获取商品组图
- * @return \yii\db\ActiveQuery
- */
- public function getGoodsPicList()
- {
- return $this->hasMany(GoodsPic::className(), ['goods_id' => 'id'])->where(['is_delete' => 0]);
- }
- /**
- * 获取入驻商
- * @return \yii\db\ActiveQuery
- */
- public function getMch()
- {
- return $this->hasMany(Mch::className(), ['id' => 'mch_id'])->select(['id', 'name', 'logo', 'service_tel', 'is_delete']);
- }
- public function getMchCatList()
- {
- $cat_list = [];
- $cat = MchGoodsCat::findAll(['goods_id' => $this->id]);
- if ($cat) {
- foreach ($cat as $index => $value) {
- $cat_name = MchCat::findOne(['id' => $value->cat_id]);
- $cat_list[] = $cat_name->name;
- }
- } else {
- $cat_list[] = "";
- }
- return implode(',', $cat_list);
- }
- public static function getGoodsPicStatic($goods_id, $index = 0)
- {
- $goods = VideoShopGoods::findOne($goods_id);
- if (!$goods) {
- return null;
- }
- return $goods->getGoodsPic($index);
- }
- /**
- * 获取商品销量
- */
- public function getSalesVolume()
- {
- $cacheKey = 'getSalesVolume' . $this->id;
- $cacheVal = cache()->get($cacheKey);
- if($cacheVal){
- return $cacheVal;
- }
- $res = OrderDetail::find()->alias('od')
- ->select('SUM(od.num) AS sales_volume')
- ->leftJoin(['o' => Order::tableName()], 'od.order_id=o.id')
- ->where(['od.is_delete' => 0, 'od.goods_id' => $this->id, 'o.is_delete' => 0, 'o.is_pay' => 1,])
- ->asArray()->one();
- $v = empty($res['sales_volume']) ? 0 : intval($res['sales_volume']);
- if($v > 300){
- cache()->set($cacheKey, $v, 3 * 60);
- }
- return $v;
- }
- /**
- * 获取商品销量
- */
- public function getSalesVolumeVideoShopShare()
- {
- $cacheKey = 'getSalesVolumeVideoShopShare' . $this->id;
- $cacheVal = cache()->get($cacheKey);
- if($cacheVal){
- return $cacheVal;
- }
- $res = OrderDetail::find()->alias('od')
- ->select('SUM(od.num) AS sales_volume')
- ->leftJoin(['o' => Order::tableName()], 'od.order_id=o.id')
- ->leftJoin(['v' => VideoShopOrderExt::tableName()], 'v.order_id=o.id')
- ->where(['od.is_delete' => 0, 'od.goods_id' => $this->id, 'o.is_delete' => 0, 'o.is_pay' => 1,])
- ->andWhere('isnull(v.id)')
- ->asArray()->one();
- $v = empty($res['sales_volume']) ? 0 : intval($res['sales_volume']);
- if($v > 300){
- cache()->set($cacheKey, $v, 3 * 60);
- }
- return $v;
- }
- public static function getGoodsNum($goods = null){
- $types = [
- VideoShopGoods::GOODS_TYPE_DATE,
- ];
- if(!$goods){
- return [
- 'code' => 1,
- 'data' => '参数错误',
- ];
- }
- if(!in_array($goods->product_type, $types)){
- $nums = 0;
- }
- if ($goods->product_type == VideoShopGoods::GOODS_TYPE_DATE) {
- $stime = mktime(0, 0, 0, date('m') - 1, 1);
- $endtime = mktime(0, 0, 0, date('m') + 2, 1);
- $query = OrderDetail::find()->alias('od')->leftJoin(['o' => Order::tableName()], 'o.id = od.order_id')
- ->where([
- 'od.goods_id' => $goods->id,
- 'o.trade_status' => [
- Order::ORDER_FLOW_DEFAULT,
- Order::ORDER_FLOW_NO_SEND,
- Order::ORDER_FLOW_SEND,
- Order::ORDER_FLOW_CONFIRM,
- ],
- ]);
- $query->andWhere(['>', 'o.created_at', $stime]);
- $list = $query->all();
- $order = [];
- $nums = [];
- $goodsNum = $goods->getNum();
- foreach ($list as $item) {
- $attr = json_decode($item['attr'], true);
- foreach ($attr as $iattr) {
- $order[$iattr['date']]+=$item['num'];
- }
- }
- for($i = time(); $i < $endtime; $i+=86400){
- $date = date('Y-m-d', $i);
- $book_price = $goods->price;
- $newGoodsNum = $goodsNum;
- if (!empty($goods->service_book_desc)) {
- $service_book_desc = json_decode($goods->service_book_desc, true);
- foreach ($service_book_desc as $value) {
- if (strtotime($value['date']) === strtotime($date)) {
- $goodsNum = $value['num'];
- $book_price = $value['price'];
- }
- }
- }
- $num = $goodsNum - $order[$date];
- $nums[] = [
- 'date' => $date,
- 'num' => $num > 0 ? $num : 0,
- 'price' => $book_price
- ];
- $goodsNum = $newGoodsNum;
- }
- }
- return [
- 'code' => 0,
- 'data' => $nums,
- ];
- }
- /**
- * 获取商品卡券
- * @param null $id
- * @return array
- */
- public static function getGoodsCard($id = null)
- {
- return [];
- if (!$id) {
- return [];
- }
- //商品卡券
- $goods_card_list = GoodsCard::find()->alias('gc')->where(['gc.is_delete' => 0, 'goods_id' => $id])
- ->leftJoin(Card::tableName() . ' c', 'c.id=gc.card_id')->select([
- 'gc.card_id', 'c.name', 'c.pic_url', 'gc.goods_id', 'c.content'
- ])->asArray()->all();
- foreach ($goods_card_list as $index => $value) {
- $goods_card_list[$index]['id'] = $value['card_id'];
- $goods_card_list[$index]['goods_id'] = $id;
- }
- return $goods_card_list;
- }
- /**
- * @param $user User
- * @param $goods_id
- * @return false|null|string
- * 查找指定用户指定商品购买数量
- */
- public static function getBuyNum($user, $goods_id)
- {
- $goodsNum = OrderDetail::find()->alias('od')->innerJoin(['o' => Order::tableName()], 'o.id=od.order_id')
- ->where([
- 'od.goods_id' => $goods_id, 'store_id' => $user->store_id, 'o.user_id' => $user->id,
- 'o.is_delete' => 0, 'o.is_show' => 1
- ])->andWhere(['<>', 'o.trade_status', Order::ORDER_FLOW_CANCEL])->andWhere([
- 'or',
- ['o.is_pay' => 1],
- ['o.pay_type' => Order::PAY_TYPE_COD]
- ])->select('SUM(od.num) num')->scalar();
- return $goodsNum;
- }
- /**
- * @param $saas SaasUser
- * @param $goods_id
- * @return false|null|string
- * 查找指定用户指定商品购买数量
- */
- public static function getSaasBuyNum($saas, $goods_id)
- {
- $goodsNum = OrderDetail::find()->alias('od')->innerJoin(['o' => Order::tableName()], 'o.id=od.order_id')
- ->where([
- 'od.goods_id' => $goods_id,'o.saas_id' => $saas->id,
- 'o.is_delete' => 0, 'o.is_show' => 1
- ])->andWhere(['<>', 'o.trade_status', Order::ORDER_FLOW_CANCEL])->andWhere([
- 'or',
- ['o.is_pay' => 1],
- ['o.pay_type' => Order::PAY_TYPE_COD]
- ])->select('SUM(od.num) num')->scalar();
- return $goodsNum;
- }
- /**
- * 根据规格获取商品的库存及规格价格信息
- * @param array $attr_id_list 规格id列表 eg. [1,4,9]
- * @return array|null eg.
- */
- public function getAttrInfo($attr_id_list)
- {
- sort($attr_id_list);
- // 门店逻辑
- $attr = $this->attr;
- $price = $this->price;
- if (get_md_id()) {
- $md_goods = MdGoods::findOne(['goods_id' => $this->id, 'md_id' => get_md_id()]);
- if ($md_goods) {
- $attr = $md_goods->attr;
- $price = $md_goods->price;
- }
- }
- $attr_rows = json_decode($attr, true);
- if (empty($attr_rows)) {
- return null;
- }
- foreach ($attr_rows as $i => $attr_row) {
- $key = [];
- foreach ($attr_row['attr_list'] as $j => $attr) {
- $key[] = $attr['attr_id'];
- }
- sort($key);
- if (!array_diff($attr_id_list, $key)) {
- if (!$attr_rows[$i]['price']) {
- $attr_rows[$i]['price'] = $price;
- }
- return $attr_rows[$i];
- }
- }
- return null;
- }
- /**
- * 减满
- */
- public static function cutFull($goodsList)
- {
- // 将商品ID相同的商品合并
- $newGoodsList = [];
- foreach ($goodsList as $row) {
- $rowArray = (array) $row;
- if (isset($newGoodsList[$rowArray['goods_id']])) {
- $newGoodsList[$rowArray['goods_id']]['num'] += $rowArray['num'];
- $newGoodsList[$rowArray['goods_id']]['weight'] += $rowArray['weight'] * $rowArray['num'];
- $newGoodsList[$rowArray['goods_id']]['price'] += $rowArray['price'];
- } else {
- $rowArray['weight'] = $rowArray['weight'] * $rowArray['num'];
- $newGoodsList[$rowArray['goods_id']] = $rowArray;
- }
- }
- $resGoodsList = [];
- foreach ($newGoodsList as $val) {
- if ($val['full_cut']) {
- $full_cut = json_decode($val['full_cut'], true);
- } else {
- $full_cut = [
- 'pieces' => 0,
- 'forehead' => 0,
- ];
- }
- if ((empty($full_cut['pieces']) || $val['num'] < ($full_cut['pieces'] ?: 0)) && (empty((float)$full_cut['forehead']) || $val['price'] < ((float)$full_cut['forehead'] ?: 0))) {
- $resGoodsList[] = $val;
- }
- }
- return $resGoodsList;
- }
- public function getGoodsPic($index = 0)
- {
- $list = $this->goodsPicList;
- if (!$list) {
- return null;
- }
- return isset($list[$index]) ? $list[$index] : null;
- }
- public function getCatList1()
- {
- return $this->hasMany(GoodsCat::className(), ['goods_id' => 'id'])->where(['store_id' => get_store_id(), 'is_delete' => 0]);
- }
- public function getCat()
- {
- return $this->hasOne(Cat::className(), ['id' => 'cat_id'])->where(['store_id' => get_store_id()]);
- }
- public static function getBrand($brand_id = 0)
- {
- if ($brand_id < 1) {
- return '';
- }
- $brand = GoodsBrand::findOne(['id' => $brand_id]);
- return $brand->brand_name ?: '';
- }
- // 规格组
- public function getAttrData()
- {
- if ($this->isNewRecord) {
- return [];
- }
- if (!$this->use_attr) {
- return [];
- }
- if (!$this->attr) {
- return [];
- }
- $attr_group_list = [];
- $attr_data = json_decode($this->attr, true);
- foreach ($attr_data as $i => $attr_data_item) {
- foreach ($attr_data[$i]['attr_list'] as $j => $attr_list) {
- $attr_group = $this->getAttrGroupByAttId($attr_data[$i]['attr_list'][$j]['attr_id']);
- if ($attr_group) {
- $in_list = false;
- foreach ($attr_group_list as $k => $exist_attr_group) {
- if ($exist_attr_group['attr_group_name'] == $attr_group->attr_group_name) {
- $attr_item = [
- 'attr_id' => $attr_data[$i]['attr_list'][$j]['attr_id'],
- 'attr_name' => $attr_data[$i]['attr_list'][$j]['attr_name'],
- ];
- if (!in_array($attr_item, $attr_group_list[$k]['attr_list'])) {
- $attr_group_list[$k]['attr_list'][] = $attr_item;
- }
- $in_list = true;
- }
- }
- if (!$in_list) {
- $attr_group_list[] = [
- 'attr_group_name' => $attr_group->attr_group_name,
- 'attr_list' => [
- [
- 'attr_id' => $attr_data[$i]['attr_list'][$j]['attr_id'],
- 'attr_name' => $attr_data[$i]['attr_list'][$j]['attr_name'],
- ],
- ],
- ];
- }
- }
- }
- }
- return $attr_group_list;
- }
- /**
- * Undocumented function
- *
- * @Author LGL 24963@qq.com
- * @DateTime 2021-03-02
- * @desc:
- * @param [type] $att_id
- * @return void
- */
- public function getAttrGroupByAttId($att_id)
- {
- $cache_key = 'get_attr_group_by_attr_id_' . $att_id;
- $attr_group = cache()->get($cache_key);
- if ($attr_group) {
- return $attr_group;
- }
- $attr_group = AttrGroup::find()->alias('ag')
- ->where(['ag.id' => Attr::find()->select('attr_group_id')->distinct()->where(['id' => $att_id])])
- ->limit(1)->one();
- if (!$attr_group) {
- return $attr_group;
- }
- cache()->set($cache_key, $attr_group, 10);
- return $attr_group;
- }
- public function getCheckedAttrData()
- {
- if ($this->isNewRecord) {
- return [];
- }
- if (!$this->use_attr) {
- return [];
- }
- if (!$this->attr) {
- return [];
- }
- $attr_data = json_decode($this->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 = $this->getAttrGroupByAttId($attr_data[$i]['attr_list'][$j]['attr_id']);
- $attr_data[$i]['attr_list'][$j]['attr_group_name'] = $attr_group ? $attr_group->attr_group_name : null;
- }
- }
- return $attr_data;
- }
- public function getGoodsCover()
- {
- if ($this->cover_pic) {
- return $this->cover_pic;
- }
- $pic = $this->getGoodsPic(0);
- if ($pic) {
- return $pic->pic_url;
- }
- return null;
- }
- public function getCats()
- {
- return $this->hasMany(Cat::className(), ['id' => 'cat_id'])
- ->via('gc');
- }
- /**
- * @return array
- */
- public static function getDefaultAttr($store_id)
- {
- $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 = $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 static function checkBookGoods() {
- return [];
- $goods_book = VideoShopGoods::find()->alias('g')->leftJoin(['gb' => GoodsBook::tableName()], 'g.id=gb.goods_id')
- ->where(['g.is_delete' => 0, 'g.status' => 1, 'g.store_id' => get_store_id()])
- ->andWhere(['in', 'g.product_type', [1, 2]])->select('g.product_type, g.id, gb.date_book, gb.service_book')->asArray()->all();
- if (empty($goods_book)) {
- return [];
- }
- $book_goods_ids = [];
- $today = strtotime(date('Y-m-d', time()));
- foreach ($goods_book as $book) {
- // 酒店预约商品
- if ($book['product_type'] == self::GOODS_TYPE_DATE) {
- $date_book = Json::decode($book['date_book']);
- $book_date_arr = array_column($date_book, 'date');
- if (empty($book_date_arr)) {
- $book_goods_ids[] = $book['id'];
- continue;
- }
- $count = 0;
- foreach ($book_date_arr as $value) {
- if (strtotime($value) > $today) {
- $count ++;
- }
- }
- if ($count <= 1) {
- $book_goods_ids[] = $book['id'];
- }
- }
- // 服务预约商品
- if ($book['product_type'] == self::GOODS_TYPE_TIME) {
- $service_book = Json::decode($book['service_book']);
- $service_date_arr = array_column($service_book['data'], 'date');
- if (empty($service_date_arr)) {
- $book_goods_ids[] = $book['id'];
- continue;
- }
- $count = 0;
- foreach ($service_date_arr as $value) {
- if (strtotime($value) > $today) {
- $count ++;
- }
- }
- if ($count <= 1) {
- $book_goods_ids[] = $book['id'];
- }
- }
- }
- return $book_goods_ids;
- }
- /**
- * 添加可选择的抖品商品的商品列表
- */
- public static function getGoodsList($arr, $is_video_goods = false) {
- $query = VideoShopGoods::find()->alias('g')
- ->where(['g.is_delete' => 0, 'g.md_food_id' => 0, 'g.status' => 1])
- ->andWhere(['not like', 'g.name', '当面付'])->andWhere(['in', 'g.product_type', [0]]);
- if (isset($arr['store_id']) && !empty($arr['store_id'])) {
- $query->andWhere(['g.store_id' => get_store_id()]);
- }
- if (isset($arr['name']) && !empty($arr['name'])) {
- $query->andWhere([
- 'like',
- 'g.name',
- $arr['name']
- ]);
- }
- // if (isset($arr['status']) && $arr['status'] != -1) {
- // $query->andWhere(['g.status' => $arr['status']]);
- // }
- if ($is_video_goods || !empty($arr['goods_id'])) {
- $query->andWhere(['in', 'id', $arr['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'];
- }
- $query->select($select)
- ->orderBy(['g.sort' => SORT_ASC, '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' => Cat::tableName()], 'gc.cat_id=c.id')
- ->where([ 'gc.goods_id' => $v['id'] ])
- ->select(['c.name'])
- ->asArray()
- ->all();
- $pagination['data'][$k]['cat'] = $goods_cat;
- if ($is_video_goods) {
- $video_goods = VideoGoods::findOne(['store_id' => get_store_id(), 'goods_id' => $v['id'], 'is_delete' => 0]);
- $pagination['data'][$k]['video_goods_id'] = $video_goods->id;
- $pagination['data'][$k]['status'] = $video_goods->status;
- }
- }
- unset($pagination['list']);
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $pagination
- ];
- }
- public static function warn_goods_timeout($store_id){
- $conf = Option::get(OptionSetting::STORE_WARN_GOODS_TIMEOUT, $store_id, 'store', '0')['value'];
- return $conf;
- }
- public static function warn_goods_timeout_save($store_id, $conf){
- $set = Option::set(OptionSetting::STORE_WARN_GOODS_TIMEOUT, $conf, $store_id, 'store');
- return [
- 'code'=>$set ? 0 : 1,
- 'msg'=>$set ? '保存成功' : '保存失败',
- ];
- }
- }
|