| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468 |
- <?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\GolangForm;
- use app\modules\admin\models\GolangOrderForm;
- 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 string $name
- * @property string $price
- * @property string $pv_0
- * @property string $pv_1
- * @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
- * @property integer $profit_sharing_enabled
- */
- class Goods 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 '{{%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', 'pv_0', 'pv_1', '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','profit_sharing_enabled'], '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' => '售价',
- 'pv_0' => '小模型PV',
- 'pv_1' => '大模型PV',
- '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');
- $pv_0_arr = array_column($attr_rows, 'pv_0');
- $pv_1_arr = array_column($attr_rows, 'pv_1');
- $integral_price_arr = array_column($attr_rows, 'integral_price');
- if (!empty($price_arr)) {
- $this->price = min($price_arr);
- }
- if (!empty($pv_0_arr)) {
- $this->pv_0 = min($pv_0_arr);
- }
- if (!empty($pv_1_arr)) {
- $this->pv_1 = min($pv_1_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);
- return parent::beforeSave($insert);
- 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;
- return [
- 'goods_rules_price' => $goods_rules_price,
- 'goods_attr_rules_price' => $goods_attr_rules_price,
- 'platform_negotiated_price_error' => $platform_negotiated_price_error
- ];
- 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,
- 'pv_0' => array_key_exists('pv_0',$changedAttributes) ? $changedAttributes['pv_0'] : $this->pv_0,
- 'pv_1' => array_key_exists('pv_1',$changedAttributes) ? $changedAttributes['pv_1'] : $this->pv_1,
- '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)) {
- Goods::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 = Goods::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 = Goods::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['goods_ids'])){
- $query = $query->andWhere(['in', 'g.id', $arr['goods_ids']]);
- }
- if (!empty($arr['select'])) {
- $select = $arr['select'];
- } else {
- $select = ['g.cloud_status','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']) === Goods::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'], Goods::$validProductType)) {
- $query->andWhere([
- 'g.product_type' => $arr['product_type']
- ]);
- }else{
- //剔除掉积分商品
- $query->andWhere([
- 'g.product_type' => [Goods::GOODS_TYPE_NORMAL,Goods::GOODS_TYPE_DATE,Goods::GOODS_TYPE_TIME,Goods::GOODS_TYPE_VIRTUAL,Goods::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' => Goods::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']) === Goods::GOODS_TYPE_WORKER) {
- $query->leftJoin(['wge' => WorkerGoodsExt::tableName()], 'g.id = wge.goods_id');
- }
- if (intval($arr['product_type']) === Goods::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']) === Goods::GOODS_TYPE_WORKER) {
- $query->andWhere(['wge.cat_id' => $arr['cat_id']]);
- } else if (intval($arr['product_type']) === Goods::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'] == Goods::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'] == Goods::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'] == Goods::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']) === Goods::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']) === Goods::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();
- foreach ($list as &$item) {
- $count = \Yii::$app->db->createCommand("SELECT COUNT(*) FROM ysdl_external_commodity_selection where mer_no = :store_id and commodity_id= :commodity_id")
- ->bindValues([
- ':store_id' => $item['store_id'],
- ':commodity_id' => $item['cloud_goods_id'],
- ])
- ->queryScalar();
- if($count<=0){
- \Yii::$app->db->createCommand()->insert('ysdl_external_commodity_selection', [
- 'mer_no'=> $item['store_id'],
- 'commodity_id'=>$item['cloud_goods_id'],
- 'is_off_shelf'=>(empty($item['cloud_status']) ? 1 : 0),
- ])->execute();
- }
- }
- 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'=>Goods::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 = Goods::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 = Goods::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 = Goods::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 = Goods::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 = [
- Goods::GOODS_TYPE_DATE,
- ];
- if(!$goods){
- return [
- 'code' => 1,
- 'data' => '参数错误',
- ];
- }
- if(!in_array($goods->product_type, $types)){
- $nums = 0;
- }
- if ($goods->product_type == Goods::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 = Goods::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 = Goods::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 ? '保存成功' : '保存失败',
- ];
- }
- public static function yifulai($order_id){
- $order = Order::findOne(['id'=>$order_id,'is_delete'=>0]);
- $store = Store::findOne(['id'=>$order->store_id,'is_delete'=>0]);
- if (empty($store)) return [];
- $param['sendUserInfo'] = [
- 'userAddress'=>$order->address,
- 'userName'=>$order->name,
- 'userPhone'=>$order->mobile];
- $province = District::findOne($store->province_id)->name;
- $city = District::findOne($store->city_id)->name;
- $district = District::findOne($store->district_id)->name;
- $param['shopDetail'] = [
- 'accountName'=>$store->name,
- 'address'=>$store->address,
- 'area'=>$city,
- 'city'=>$district,
- 'createTime'=>$store->created_at,
- 'province'=>$province,
- ];
- $order_count = OrderDetail::find()->where(['order_id'=>$order_id])->asArray()->all();
- $param['totalProductAmount'] = 0;
- $param['totalCount'] = 0;
- $param['productList'] = [];
- foreach ($order_count as $item){
- $goods_info = Json::decode($item['goods_info']);
- $goods_info['order_num'] = $item['num'];
- $param['productList'][] = $goods_info;
- $goods_info = Json::decode($item['goods_info']);
- $param['totalProductAmount'] += $goods_info['price']*$item['num'];
- $param['totalCount']+=$item['num'];
- }
- $param['bookingInfo'] = ['startTime'=>(date("Y-m-d H:i:s",$order->pickup_time))];
- $param['totalAmount'] = $order->total_price;
- $param['totalDiscount'] = $order->take_price;
- $param['totalPayAmount'] = $order->pay_price;
- $param['yixiao_sid'] = $order->store_id;
- $param['order_no'] = $order->order_no;
- $url = 'https://vue3git.wdsp666.com/api/callback.yixiao/notify';
- $nonce = base64_encode(random_bytes(16));
- $timestamp = time();
- $sign = self::yifulaiSign($param,$timestamp,$nonce);
- $param['signature'] = $sign;
- $header['X-App-Id'] = GolangOrderForm::$app_id;
- $header['X-Timestamp'] = $timestamp;
- $header['X-Nonce'] = $nonce;
- $res = GolangForm::httpJsonPost($url,Json::encode($param),$header);
- return true;
- }
- public static function yifulaiOrderStatus($order_no){
- $order = Order::findOne(['order_no'=>$order_no,'is_delete'=>0]);
- $store = Store::findOne(['id'=>$order->store_id,'is_delete'=>0]);
- if (empty($store)) return [];
- $param['order_no'] = $order_no;
- $url = 'https://vue3git.wdsp666.com/api/callback.yixiao/orderDetail';
- $nonce = base64_encode(random_bytes(16));
- $timestamp = time();
- $sign = self::yifulaiSign($param,$timestamp,$nonce);
- $param['signature'] = $sign;
- $header['X-App-Id'] = GolangOrderForm::$app_id;
- $header['X-Timestamp'] = $timestamp;
- $header['X-Nonce'] = $nonce;
- $res = GolangForm::httpJsonPost($url,Json::encode($param),$header);
- $data = Json::decode($res);
- if (!$data || $data['code']==0 || !$data['data']['delivery_status_cn']){
- return false;
- }
- return ['data'=>$data['data']['delivery_status_cn']];
- }
- private static function yifulaiSign($params,$timestamp,$nonce){
- ksort($params);
- $paramString = '';
- foreach ($params as $key => $value) {
- if (is_array($value)) continue;
- $paramString .= $key . $value;
- }
- $stringToSign = $paramString . $timestamp . $nonce;
- // 生成HMAC-SHA256签名
- $generatedKey = hash_hmac('sha256', $stringToSign, GolangOrderForm::$app_secret);
- return $generatedKey;
- }
- }
|