Goods.php 101 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468
  1. <?php
  2. /**
  3. * 厦门云联储网络科技有限公司
  4. * https://www.baokuaiyun.com
  5. * Copyright (c) 2023 爆块云 All rights reserved.
  6. */
  7. namespace app\models;
  8. use app\constants\OptionSetting;
  9. use app\jobs\storeSync\DiyCommon;
  10. use app\modules\admin\models\GolangForm;
  11. use app\modules\admin\models\GolangOrderForm;
  12. use app\modules\admin\models\imageSearch\UploadImageForm;
  13. use app\modules\admin\models\MerchantForm;
  14. use yii\db\ActiveRecord;
  15. use yii\behaviors\TimestampBehavior;
  16. use yii\helpers\ArrayHelper;
  17. use yii\helpers\Json;
  18. use app\modules\admin\models\pospal\PospalForm;
  19. use app\models\common\Upload;
  20. use app\modules\admin\models\MchForm;
  21. /**
  22. * Class Goods
  23. * @package app\modules\common\models
  24. * @property integer $id
  25. * @property integer $store_id
  26. * @property string $name
  27. * @property string $price
  28. * @property string $pv_0
  29. * @property string $pv_1
  30. * @property string $goods_send_profit
  31. * @property string $goods_take_price
  32. * @property string $original_price
  33. * @property integer $integral_price
  34. * @property string $detail
  35. * @property string $cat_id
  36. * @property integer $status
  37. * @property integer $is_delete
  38. * @property string $attr
  39. * @property string $service
  40. * @property integer $sort
  41. * @property integer $virtual_sales
  42. * @property string $cover_pic
  43. * @property string $video_url
  44. * @property string $unit
  45. * @property integer $individual_share
  46. * @property string $share_commission_first
  47. * @property string $share_commission_second
  48. * @property string $share_commission_third
  49. * @property double $weight
  50. * @property string $freight
  51. * @property string $full_cut
  52. * @property string $integral
  53. * @property integer $use_attr
  54. * @property integer $share_type
  55. * @property integer $quick_purchase
  56. * @property integer $hot_cakes
  57. * @property integer $mch_id
  58. * @property integer $goods_num
  59. * @property string $cost_price
  60. * @property integer $member_discount
  61. * @property string $rebate
  62. * @property integer $mch_sort
  63. * @property integer $type
  64. * @property integer $is_level
  65. * @property integer $confine_count
  66. * @property integer $is_negotiable
  67. * @property string $goods_share_title
  68. * @property string $goods_share_logo
  69. * @property string $goods_share_desc
  70. * @property string $created_at
  71. * @property string $updated_at
  72. * @property string $goods_no
  73. * @property string $key_word
  74. * @property string $pieces
  75. * @property string $forehead
  76. * @property integer $attr_setting_type
  77. * @property string $verify_card_id
  78. * @property integer $is_verify
  79. * @property string $delivery_type
  80. * @property integer $product_type
  81. * @property integer $is_form
  82. * @property string $form_name
  83. * @property integer $is_recommend
  84. * @property integer $rate
  85. * @property integer $rate_type
  86. * @property integer $md_rate
  87. * @property integer $md_rate_type
  88. * @property integer $dl_p_rate
  89. * @property integer $dl_c_rate
  90. * @property integer $dl_d_rate
  91. * @property integer $dl_p_rate_type
  92. * @property integer $dl_c_rate_type
  93. * @property integer $dl_d_rate_type
  94. * @property integer $md_hx_rate
  95. * @property integer $md_hx_rate_type
  96. * @property integer $md_food_id
  97. * @property integer $cloud_goods_id
  98. * @property integer $cloud_supplier_id
  99. * @property string $material_id
  100. * @property string $material_key
  101. * @property integer $service_book_num
  102. * @property integer $can_set_md_price
  103. * @property integer $delivery_rules_id
  104. * @property integer $goods_area_rules_id
  105. * @property integer $order_rules_buyer_location_id
  106. * @property integer $food_ext_goods_id
  107. * @property integer $stbz_goods_id
  108. * @property integer $is_wholesale
  109. * @property string $accessories_image
  110. * @property string $service_book_desc
  111. * @property string $parameter_list
  112. * @property string $buy_type_info
  113. * @property string $share_commission_new_first
  114. * @property string $share_commission_new_second
  115. * @property string $share_commission_new_third
  116. * @property integer $send_verify_card_num
  117. * @property string $coupon_card_id
  118. * @property integer $send_coupon_card_num
  119. * @property integer $brand_id
  120. * @property integer $is_risk
  121. * @property integer $share_holder_profit_switch
  122. * @property string $share_holder_high_profit
  123. * @property integer $time_made_day
  124. * @property integer $time_shelf_life
  125. * @property integer $warehouse_id
  126. * @property integer $warehouse_zone_id
  127. * @property integer $is_front_delivery
  128. * @property integer $is_front_centralize
  129. * @property integer $agent_goods_status
  130. * @property integer $cloud_inventory_id
  131. * @property integer $profit_sharing_enabled
  132. */
  133. class Goods extends ActiveRecord
  134. {
  135. //从银豹同步时设置为1
  136. public $yinbaoSync = 0;
  137. //活动价(临时字段)
  138. public $activityPrice = 0;
  139. /**
  140. * 正常商品
  141. */
  142. const GOODS_TYPE_NORMAL = 0;
  143. /**
  144. * 日期预约商品
  145. */
  146. const GOODS_TYPE_DATE = 1;
  147. /**
  148. * 时间预约商品
  149. */
  150. const GOODS_TYPE_TIME = 2;
  151. /**
  152. * 虚拟商品
  153. */
  154. const GOODS_TYPE_VIRTUAL = 3;
  155. /**
  156. * 积分商品
  157. */
  158. const GOODS_TYPE_INTEGRAL = 4;
  159. /**
  160. * 认养商品
  161. */
  162. const GOODS_TYPE_ADOPT = 5;
  163. /**
  164. * 上门服务商品
  165. */
  166. const GOODS_TYPE_WORKER = 6;
  167. /**
  168. * 展示类商品
  169. */
  170. const GOODS_TYPE_VIEW = 7;
  171. public static $validProductType = [
  172. self::GOODS_TYPE_NORMAL,
  173. self::GOODS_TYPE_DATE,
  174. self::GOODS_TYPE_TIME,
  175. self::GOODS_TYPE_VIRTUAL,
  176. self::GOODS_TYPE_INTEGRAL,
  177. self::GOODS_TYPE_ADOPT,
  178. self::GOODS_TYPE_WORKER,
  179. self::GOODS_TYPE_VIEW,
  180. ];
  181. const STATUS_NORMAL = 1; // 上架
  182. const STATUS_DISABLE = 0; // 下架
  183. /**
  184. * 价格面议
  185. */
  186. const GOODS_NEGOTIABLE = '价格面议';
  187. /**
  188. * @var int|mixed|null
  189. */
  190. public static function tableName()
  191. {
  192. return '{{%goods}}';
  193. }
  194. public function behaviors()
  195. {
  196. return [
  197. [
  198. // 自动更新创建和更新时间
  199. 'class' => TimestampBehavior::class,
  200. 'value' => time()
  201. ]
  202. ];
  203. }
  204. public function rules()
  205. {
  206. return [
  207. [['store_id', 'name', 'detail', 'attr'], 'required', 'on' => self::whenSave()],
  208. [['integral_price','store_id', 'cat_id', 'status', 'is_delete', 'created_at', 'updated_at',
  209. 'sort', 'individual_share', 'freight', 'use_attr', 'share_type', 'is_verify',
  210. 'quick_purchase', 'hot_cakes', 'mch_id', 'mch_audit', 'mch_audit_time', 'goods_num', 'member_discount', 'virtual_sales',
  211. '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()],
  212. [['price', 'pv_0', 'pv_1', 'original_price', 'share_commission_first', 'share_commission_second',
  213. '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()],
  214. [['detail', 'attr', 'cover_pic', 'full_cut', 'integral'], 'string', 'on' => self::whenSave()],
  215. [['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()],
  216. [['service'], 'string', 'max' => 2000, 'on' => self::whenSave()],
  217. [['video_url'], 'string', 'max' => 2048, 'on' => self::whenSave()],
  218. [['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'],
  219. [['status','cat_id','store_id'], 'integer', 'on' => self::whenList()],
  220. [['name'], 'string', 'max' => 60, 'on' => self::whenList()],
  221. [['status', 'is_verify'], 'default', 'value' => -1, 'on' => self::whenList()],
  222. [['mch_id'], 'default', 'value' => 0, 'on' => self::whenList()],
  223. [['store_id'], 'default', 'value' => 1, 'on' => self::whenList()],
  224. [['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'],
  225. [['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'],
  226. [['time_made_day', 'time_shelf_life'], 'safe'],
  227. ];
  228. }
  229. public function attributeLabels()
  230. {
  231. return [
  232. 'id' => 'ID',
  233. 'store_id' => 'Store ID',
  234. 'name' => '商品名称',
  235. 'goods_send_profit' => '商品让利比例返给用户',
  236. 'goods_take_price' => '商品让利比例扣除用户',
  237. 'price' => '售价',
  238. 'pv_0' => '小模型PV',
  239. 'pv_1' => '大模型PV',
  240. 'original_price' => '原价(只做显示用)',
  241. 'integral_price' => '积分兑换',
  242. 'detail' => '商品详情,图文',
  243. 'cat_id' => '商品类别',
  244. 'status' => '上架状态:0=下架,1=上架',
  245. 'is_delete' => 'Is Delete',
  246. 'attr' => '规格的库存及价格',
  247. 'service' => '商品服务选项',
  248. 'sort' => '排序 升序',
  249. 'virtual_sales' => '虚拟销量',
  250. 'cover_pic' => '商品缩略图',
  251. 'video_url' => '视频',
  252. 'unit' => '单位',
  253. 'individual_share' => '是否单独分销设置:0=否,1=是',
  254. 'share_commission_first' => '一级分销佣金比例',
  255. 'share_commission_second' => '二级分销佣金比例',
  256. 'share_commission_third' => '三级分销佣金比例',
  257. 'weight' => '重量',
  258. 'freight' => '运费模板ID',
  259. 'full_cut' => '满减',
  260. 'integral' => '积分设置',
  261. 'use_attr' => '是否使用规格:0=不使用,1=使用',
  262. 'share_type' => '佣金配比 0--百分比 1--固定金额',
  263. 'quick_purchase' => '是否加入快速购买 0--关闭 1--开启',
  264. 'hot_cakes' => '是否加入热销 0--关闭 1--开启',
  265. 'mch_id' => '入驻商户的id',
  266. 'goods_num' => '商品总库存',
  267. 'cost_price' => 'Cost Price',
  268. 'member_discount' => '是否参与会员折扣 0=参与 1=不参与',
  269. 'rebate' => '自购返利',
  270. 'mch_sort' => '多商户自己的排序',
  271. 'type' => '类型 0--商城或多商户 2--砍价商品',
  272. 'is_level' => '是否享受会员折扣',
  273. 'confine_count' => '购买限制:0.不限制|大于0等于限购数量',
  274. 'is_negotiable' => '面议方式',
  275. 'attr_setting_type' => '分销设置类型 0.普通设置|1.详细设置',
  276. 'verify_card_id' => '绑定核销卡',
  277. 'goods_share_title' => '商品分享标题',
  278. 'goods_share_desc' => '商品分享缩略图',
  279. 'goods_share_logo' => '商品分享简介',
  280. 'created_at' => '创建时间',
  281. 'updated_at' => '更新时间',
  282. 'goods_no' => '商品货号',
  283. 'key_word' => '关键字',
  284. 'pieces' => '单品满件包邮',
  285. 'forehead' => '单品满额包邮',
  286. 'is_verify' => '入驻商待审核 0 待审核 1审核通过并上架 2审核未通过',
  287. 'rate_type' => "分红类型",
  288. 'cloud_goods_id' => "云仓商品ID",
  289. 'service_book_num' => '预约商品数量',
  290. 'can_set_md_price' => '门店溢价销售 0禁止 1允许',
  291. 'delivery_rules_id' => '运送规则id',
  292. 'goods_area_rules_id' => '销售规则id',
  293. 'stbz_goods_id' => '胜天半子goods_id',
  294. 'order_rules_buyer_location_id' => '销售区域规则id',
  295. 'food_ext_goods_id' => '附加商品区id',
  296. 'is_wholesale' => '是否批发',
  297. 'accessories_image' => '装配图片',
  298. 'parameter_list' => '商品参数',
  299. 'service_book_desc' => '酒店商品特殊日期值',
  300. 'buy_type_info' => '购买类型',
  301. 'share_commission_new_first' => '新一级分销佣金比例, 带等级',
  302. 'share_commission_new_second' => '新二级分销佣金比例, 带等级',
  303. 'share_commission_new_third' => '新三级分销佣金比例, 带等级',
  304. 'send_coupon_card_num' => '赠送优惠券数量',
  305. 'send_verify_card_num' => '赠送核销卡数量',
  306. 'coupon_card_id' => '赠送优惠券id',
  307. 'is_risk' => '商品是否有风险',
  308. 'is_front_delivery' => '是否是云仓仓库配送商品(配送方式只能为到店自提和同城配送)',
  309. 'is_front_centralize' => '是否是云仓集采商品(配送方式只能为到店自提)',
  310. 'agent_goods_status' => '是否是云仓代理配送产品(配送方式只能为到店自提或快递 如果是可上门安装则为快递 如果不上门安装则为到店自提 大于0表示是代理配送商品 2表示需要上门安装)'
  311. ];
  312. }
  313. public static function whenSave ()
  314. {
  315. return 'save';
  316. }
  317. public static function whenList ()
  318. {
  319. return 'list';
  320. }
  321. public function afterFind()
  322. {
  323. parent::afterFind();
  324. try {
  325. // 处理cover_pic
  326. //不做判断时 手机端修改商品价格时会将封面图设置为null
  327. if ($this->cover_pic) {
  328. $this->cover_pic = Upload::getImgStyle($this->cover_pic);
  329. }
  330. // 处理规格图片
  331. if ($this->attr) {
  332. $attrs = json_decode($this->attr, true);
  333. if (\is_array($attrs)) {
  334. foreach ($attrs as &$attr) {
  335. $attr['pic'] = Upload::getImgStyle($attr['pic']);
  336. }
  337. $this->attr = json_encode($attrs, JSON_UNESCAPED_UNICODE);
  338. }
  339. }
  340. if ($this->detail) {
  341. // 处理detail,使用正则匹配并替换图片地址
  342. $detail = preg_replace_callback('/<img.*?src="(.*?)".*?\/>/', function ($matches) {
  343. return '<img src="' . Upload::getImgStyle($matches[1]) . '">';
  344. }, $this->detail);
  345. if ($detail) {
  346. $this->detail = $detail;
  347. }
  348. }
  349. } catch (\Throwable $e) {}
  350. }
  351. public function beforeSave($insert)
  352. {
  353. //在总后台 店铺后台 需要设置店铺 是否允许店铺上传产品,如果设置为不允许,就只能云仓选品,PC端后台和手机端后台都需要控制。
  354. if ($insert) {
  355. $is_add_goods = Option::get('is_add_goods', $this->store_id, 'store', 1)['value'];
  356. if (intval($this->cloud_goods_id) === 0 && (int)$is_add_goods === 0) {
  357. $this->addError('is_add_goods', '错误:当前店铺不允许上传产品,只能云仓选品');
  358. return false;
  359. }
  360. }
  361. if (intval($this->is_front_delivery)) {
  362. $this->delivery_type = json_encode(['shop', 'delivery'], JSON_UNESCAPED_UNICODE);
  363. }
  364. if (intval($this->is_front_centralize)) {
  365. $this->delivery_type = json_encode(['shop'], JSON_UNESCAPED_UNICODE);
  366. }
  367. if (intval($this->agent_goods_status)) {
  368. $delivery_type = ['shop'];
  369. if (intval($this->agent_goods_status) === 2) {//需要上门安装
  370. $delivery_type = ['express'];
  371. }
  372. $this->delivery_type = json_encode($delivery_type, JSON_UNESCAPED_UNICODE);
  373. }
  374. if ($this->use_attr == 1) {
  375. $this->goods_num = $this->getNum();
  376. if ($this->attr && !in_array($this->product_type, [1, 2])) {
  377. $attr_rows = json_decode($this->attr, true);
  378. $price_arr = array_column($attr_rows, 'price');
  379. $pv_0_arr = array_column($attr_rows, 'pv_0');
  380. $pv_1_arr = array_column($attr_rows, 'pv_1');
  381. $integral_price_arr = array_column($attr_rows, 'integral_price');
  382. if (!empty($price_arr)) {
  383. $this->price = min($price_arr);
  384. }
  385. if (!empty($pv_0_arr)) {
  386. $this->pv_0 = min($pv_0_arr);
  387. }
  388. if (!empty($pv_1_arr)) {
  389. $this->pv_1 = min($pv_1_arr);
  390. }
  391. if (!empty($integral_price_arr)) {
  392. $this->integral_price = intval(min($integral_price_arr));
  393. }
  394. }
  395. }
  396. if (in_array($this->product_type, [6])) {
  397. $this->use_attr = 0;
  398. }
  399. if (empty($this->buy_type_info)) {
  400. $this->buy_type_info = json_encode([
  401. 'type' => 0,
  402. 'member_level' => 0
  403. ]);
  404. }
  405. if (intval($this->individual_share) === 1) {
  406. $share_level = ShareLevel::find()->where(['store_id' => $this->store_id, 'is_delete' => ShareLevel::SHARE_NOT_DELETE, 'status' => ShareLevel::STATUS_ON])
  407. ->select('level id, name')->orderBy('level ASC')->asArray()->all();
  408. $share_level = array_merge([[
  409. 'id' => 0,
  410. 'name' => '默认等级'
  411. ]], $share_level);
  412. // if ((!is_null($this->attr_setting_type)) && intval($this->attr_setting_type) === 1) {
  413. // $attr = json_decode($this->attr, true);
  414. // foreach ($attr as $attr_) {
  415. // foreach ($share_level as $share_level_) {
  416. // if (empty($attr_['share_commission_level_' . $share_level_['id']])) {
  417. // $this->addError('type', '操作失败,分销详细模式等级【' . $share_level_['name'] . '】为空');
  418. // return false;
  419. // }
  420. // if (
  421. // !isset($attr_['share_commission_level_' . $share_level_['id']]['share_commission_first'])
  422. // || !isset($attr_['share_commission_level_' . $share_level_['id']]['share_commission_second'])
  423. // || !isset($attr_['share_commission_level_' . $share_level_['id']]['share_commission_third'])
  424. // ) {
  425. // $this->addError('type', '操作失败,分销详细模式等级【' . $share_level_['name'] . '】分销内容不完整');
  426. // return false;
  427. // }
  428. // }
  429. //
  430. // }
  431. // } else
  432. if ((!is_null($this->attr_setting_type)) && intval($this->attr_setting_type) === 0) {
  433. $share_commission_new_first = json_decode($this->share_commission_new_first, true);
  434. foreach ($share_level as $share_level_) {
  435. $share_commission_new_first['share_commission_level_' . $share_level_['id']] = $share_commission_new_first['share_commission_level_' . $share_level_['id']] ?? 0;
  436. }
  437. $this->share_commission_new_first = json_encode($share_commission_new_first);
  438. $share_commission_new_second = json_decode($this->share_commission_new_second, true);
  439. foreach ($share_level as $share_level_) {
  440. $share_commission_new_second['share_commission_level_' . $share_level_['id']] = $share_commission_new_second['share_commission_level_' . $share_level_['id']] ?? 0;
  441. }
  442. $this->share_commission_new_second = json_encode($share_commission_new_second);
  443. $share_commission_new_third = json_decode($this->share_commission_new_third, true);
  444. foreach ($share_level as $share_level_) {
  445. $share_commission_new_third['share_commission_level_' . $share_level_['id']] = $share_commission_new_third['share_commission_level_' . $share_level_['id']] ?? 0;
  446. }
  447. $this->share_commission_new_third = json_encode($share_commission_new_third);
  448. }
  449. }
  450. if ($this->dirtyAttributes && ($this->dirtyAttributes['attr'] || isset($this->dirtyAttributes['use_attr']))) {
  451. $this->setActivityAttr($this->id, $this->attr, $this->use_attr);
  452. }
  453. //加价保护start
  454. $platform_profit_strategy = Option::get('platform_profit_strategy', 0, 'store')['value'];
  455. $platform_profit_strategy = json_decode($platform_profit_strategy, true);
  456. return parent::beforeSave($insert);
  457. if (!empty($platform_profit_strategy)) {
  458. $result = $this->getMarkupProtectionRules($this->price, $this->dirtyAttributes['attr'], $this->cloud_goods_id);
  459. if (intval($platform_profit_strategy['is_risk_control']) && $result['platform_negotiated_price_error'] && $this->id && intval($this->status) === 1) {
  460. //开启风控
  461. $id = \Yii::$app->cache->get('risk_control_model_id');
  462. if ($id && !\Yii::$app->queue->isDone($id)) {//\Yii::$app->queue->status($id)
  463. \Yii::$app->queue->remove($id);
  464. }
  465. $id =\queue_push(new \app\jobs\RiskControlModelJob(['risk_control_model' => $platform_profit_strategy['risk_control_model'], 'goods_id' => $this->id]));
  466. \Yii::$app->cache->set('risk_control_model_id', $id);
  467. // $form = new RiskControlForm();
  468. // $form->riskControlHandle($platform_profit_strategy['risk_control_model'], $this->id);
  469. }
  470. // if (intval($platform_profit_strategy['is_markup_protection'])) {
  471. // if (!empty($platform_profit_strategy['markup_protection_rules'])) {
  472. // $markup_protection_rules = $platform_profit_strategy['markup_protection_rules'];
  473. // $rules_price = 0;
  474. // $rules_attr_price = [];
  475. // $title = "";
  476. // if (intval($markup_protection_rules['title']) === 0) {
  477. // $title = "利润";
  478. // //规则名称:0 利润
  479. // $rules_price = $result['goods_rules_price'];
  480. // $rules_attr_price = $result['goods_attr_rules_price'];
  481. // }
  482. // $compare = "";
  483. // if (intval($markup_protection_rules['compare']) === 0) {
  484. // //比较符号 0小于 1小于等于 2等于 3大于等于 4大于
  485. // }
  486. // $open = false;
  487. // if ($markup_protection_rules['price'] > 0) {
  488. // //价格
  489. // if ($rules_price > 0) {
  490. // switch ($markup_protection_rules['compare']) {
  491. // case 1:
  492. // $compare = "小于等于";
  493. // if ($rules_price <= $markup_protection_rules['price']) {
  494. // $open = true;
  495. // }
  496. // break;
  497. // case 2:
  498. // $compare = "等于";
  499. // if (floatval($rules_price) == floatval($markup_protection_rules['price'])) {
  500. // $open = true;
  501. // }
  502. // break;
  503. // case 3:
  504. // $compare = "大于等于";
  505. // if ($rules_price >= $markup_protection_rules['price']) {
  506. // $open = true;
  507. // }
  508. // break;
  509. // case 4:
  510. // $compare = "大于";
  511. // if ($rules_price > $markup_protection_rules['price']) {
  512. // $open = true;
  513. // }
  514. // break;
  515. // default:
  516. // $compare = "小于";
  517. // if ($rules_price < $markup_protection_rules['price']) {
  518. // $open = true;
  519. // }
  520. // break;
  521. // }
  522. // }
  523. //
  524. // if (!empty($rules_attr_price)) {
  525. // foreach ($rules_attr_price as $price_item) {
  526. // switch ($markup_protection_rules['compare']) {
  527. // case 1:
  528. // if ($price_item <= $markup_protection_rules['price']) {
  529. // $open = true;
  530. // }
  531. // break;
  532. // case 2:
  533. // if (floatval($price_item) == floatval($markup_protection_rules['price'])) {
  534. // $open = true;
  535. // }
  536. // break;
  537. // case 3:
  538. // if ($price_item >= $markup_protection_rules['price']) {
  539. // $open = true;
  540. // }
  541. // break;
  542. // case 4:
  543. // if ($price_item > $markup_protection_rules['price']) {
  544. // $open = true;
  545. // }
  546. // break;
  547. // default:
  548. // if ($price_item < $markup_protection_rules['price']) {
  549. // $open = true;
  550. // }
  551. // break;
  552. // }
  553. // }
  554. // }
  555. //
  556. // }
  557. // if (intval($markup_protection_rules['operate']) === 0) {
  558. // //操作
  559. // $attribute_title = "price";
  560. // if ($open && $this->dirtyAttributes['price']) {
  561. // $this->price = $this->oldAttributes['price'];
  562. // }
  563. //
  564. // if ($open && !empty($this->dirtyAttributes['attr'])) {
  565. // $attribute_title = "attr.price";
  566. // $this->attr = $this->oldAttributes['attr'];
  567. // }
  568. // if ($open) {
  569. // $this->addError($attribute_title, '错误:' . $title . $compare . $markup_protection_rules['price']);
  570. // return false;
  571. // }
  572. // }
  573. //
  574. // }
  575. // }
  576. }
  577. //加价保护end
  578. return parent::beforeSave($insert);
  579. }
  580. public function getMarkupProtectionRules($price, $attr, $cloud_goods_id) {
  581. $goods_rules_price = 0;
  582. $goods_attr_rules_price = [];
  583. $platform_negotiated_price_error = false;
  584. return [
  585. 'goods_rules_price' => $goods_rules_price,
  586. 'goods_attr_rules_price' => $goods_attr_rules_price,
  587. 'platform_negotiated_price_error' => $platform_negotiated_price_error
  588. ];
  589. if ($cloud_goods_id > 0) {
  590. //获取云仓商品
  591. $cloud_url = "/goods/getGoodsInfo";
  592. $cloud_data = [];
  593. $cloud_data['goods_id'] = $cloud_goods_id;
  594. $domain = (new OptionSetting)->getCloudDomainName();
  595. $cloud_info = cloud_post($domain . $cloud_url, $cloud_data);
  596. $cloud_info = json_decode($cloud_info, true);
  597. if($cloud_info['code'] != 0){
  598. //debug_log($cloud_info, 'risk_control_result.log');
  599. }else{
  600. //获取商家利润 商家售价-平台供货价
  601. $cloud_goods = $cloud_info['data']['goods'];
  602. $goods_rules_price = floatval(sprintf('%.2f', $price - $cloud_goods['platform_negotiated_price']));
  603. //新增风控判断 商品售价小于平台协议价
  604. if ($price < $cloud_goods['platform_negotiated_price']) {
  605. $platform_negotiated_price_error = true;
  606. }
  607. if (!empty($attr)) {
  608. //规格中的云仓商品协议价大于商品售价处理
  609. $attr = json_decode($attr, true);
  610. $cloud_goods['attrs'] = json_decode($cloud_goods['attrs'], true);
  611. foreach ($cloud_goods['attrs'] as $cloud_attr_item) {
  612. $cloud_no = $cloud_attr_item['no'];
  613. foreach ($attr as &$attr_item) {
  614. $_no = $attr_item['no'];
  615. if (!empty($cloud_no) && !empty($_no) && $_no == $cloud_no) {
  616. $attr_rules_price = floatval(sprintf('%.2f', $attr_item['price'] -
  617. (!empty($cloud_attr_item['platform_negotiated_price']) ? $cloud_attr_item['platform_negotiated_price'] : $cloud_attr_item['price'])
  618. ));
  619. array_push($goods_attr_rules_price, $attr_rules_price);
  620. //新增风控判断 商品售价小于平台协议价
  621. if ($attr_item['price'] < (!empty($cloud_attr_item['platform_negotiated_price']) ? $cloud_attr_item['platform_negotiated_price'] : $cloud_attr_item['price'])
  622. ) {
  623. $platform_negotiated_price_error = true;
  624. }
  625. }
  626. }
  627. }
  628. }
  629. }
  630. }
  631. return [
  632. 'goods_rules_price' => $goods_rules_price,
  633. 'goods_attr_rules_price' => $goods_attr_rules_price,
  634. 'platform_negotiated_price_error' => $platform_negotiated_price_error
  635. ];
  636. }
  637. public function afterSave($insert, $changedAttributes)
  638. {
  639. parent::afterSave($insert, $changedAttributes);
  640. \Yii::error([__METHOD__, $insert, $changedAttributes]);
  641. if (($this->yinbaoSync == 0)) {
  642. (new PospalForm(['store_id' => $this->store_id]))->afterGoodsSave($this, $changedAttributes);
  643. }
  644. \app\modules\admin\models\jushuitan\JuShuiTanForm::afterGoodsSave($this);
  645. \app\modules\admin\models\erp\InventoryForm::afterGoodsSave($this, $insert, $changedAttributes);
  646. if(!$insert && in_array('cover_pic',array_keys($changedAttributes))){
  647. // 通知修改了商品主图 删除之前的商品图 并上传新的商品主图
  648. (new UploadImageForm())->updateGoodsImage($this->id);
  649. }
  650. /* 2024年3月9日16:31:13 begin WPing id999新需求:降价通知 */
  651. // if(!$insert && in_array('price',array_keys($changedAttributes))){
  652. // if($this->price < $changedAttributes['price']) {//如果降价了
  653. // GoodsLoweringPrice::sendMsg($this->id, $this->store_id, $this->price, $changedAttributes['price']);
  654. // }
  655. // }
  656. if(!$insert && in_array('attr',array_keys($changedAttributes))){
  657. // //debug_log('规格被修改,走进来了');
  658. $before_goods = [
  659. 'id' => $this->id,
  660. 'attr' => array_key_exists('attr',$changedAttributes) ? $changedAttributes['attr'] : $this->attr,
  661. 'price' => array_key_exists('price',$changedAttributes) ? $changedAttributes['price'] : $this->price,
  662. 'pv_0' => array_key_exists('pv_0',$changedAttributes) ? $changedAttributes['pv_0'] : $this->pv_0,
  663. 'pv_1' => array_key_exists('pv_1',$changedAttributes) ? $changedAttributes['pv_1'] : $this->pv_1,
  664. 'mch_id' => array_key_exists('mch_id',$changedAttributes) ? $changedAttributes['mch_id'] : $this->mch_id,
  665. 'is_level' => array_key_exists('is_level',$changedAttributes) ? $changedAttributes['is_level'] : $this->is_level,
  666. ];
  667. GoodsLoweringPrice::sendMsg($this->id, $this->store_id, $before_goods);
  668. }
  669. /* end */
  670. //获取禁售目录
  671. $form = new MerchantForm();
  672. $form->store_id = $this->store_id;
  673. $result = $form->getCloudForbiddenGoods();
  674. if ($result['code'] === 0) {
  675. $cloud_goods_id = $result['data']['goods_id'];
  676. //如果当前云仓id在禁售目录就禁售
  677. if (in_array($this->cloud_goods_id, $cloud_goods_id)) {
  678. Goods::updateAll(['is_forbidden' => 1, 'is_delete' => 1], ['id' => $this->id]);
  679. }
  680. }
  681. (new DiyCommon)->JobBehaviors($this->store_id, StoreSyncExtLog::TYPE_PRODUCT, [$this->id]);
  682. }
  683. //更改规格之后修改营销活动产品规格
  684. public function setActivityAttr($id, $attr, $use_attr) {
  685. try {
  686. $attr = json_decode($attr, true);
  687. //拼团
  688. $pt_goods_list = PtActivityGoods::find()->where(['is_delete' => 0, 'goods_id' => $id])->all();
  689. $this->activityAttrHandle($attr, $pt_goods_list, 'pt_price');
  690. //砍价
  691. $cut_goods_list = ActivityCutPriceGoods::find()->where(['is_delete' => 0, 'goods_id' => $id])->all();
  692. $this->activityAttrHandle($attr, $cut_goods_list, 'price', 1);
  693. //秒杀
  694. $seckill_goods_list = SeckillActivityGoods::find()->where(['is_delete' => 0, 'goods_id' => $id])->all();
  695. $this->activityAttrHandle($attr, $seckill_goods_list, 'seckill_price', 2);
  696. //新人
  697. $new_user_goods_list = ActivityNewUserGoods::find()->where(['is_delete' => 0, 'goods_id' => $id])->all();
  698. $this->activityAttrHandle($attr, $new_user_goods_list, 'price', 3);
  699. //新人专享
  700. } catch (\Exception $e) {
  701. //debug_log($e->getMessage(), 'Exception.log');
  702. }
  703. }
  704. /**
  705. * $goods_attr 商品规格
  706. * $activity_goods_list 活动产品
  707. * $price_name 价格名称
  708. * $type 活动类型
  709. */
  710. public function activityAttrHandle($goods_attr, $activity_goods_list, $price_name, $type = 0) {
  711. foreach ($activity_goods_list as $activity_goods_item) {
  712. $activity_goods = ArrayHelper::toArray($activity_goods_item);
  713. $ext_attrs = json_decode($activity_goods['attr'], true);
  714. $new_attr = [];
  715. foreach($goods_attr as $attr_index => &$attr_item){
  716. if (intval($this->use_attr) === 0) {
  717. if ($attr_index > 0) {
  718. continue;
  719. }
  720. }
  721. $open = false;
  722. foreach($ext_attrs as $ext_attr){
  723. $ext_attr_id = array_column($ext_attr['attr_list'], 'attr_id');
  724. $attr_id = array_column($attr_item['attr_list'], 'attr_id');
  725. sort($ext_attr_id);
  726. sort($attr_id);
  727. if($attr_id == $ext_attr_id){
  728. $open = true;
  729. //活动价格
  730. $attr_item[$price_name] = $ext_attr[$price_name] ?: $attr_item['price'];
  731. //活动库存
  732. $attr_item['num'] = $ext_attr['num'];
  733. //商品原售价
  734. switch ($type) {
  735. case 3://新人
  736. case 1://砍价
  737. $attr_item['oldPrice'] = $attr_item['price'];
  738. break;
  739. case 2://秒杀
  740. $attr_item['seckill_num'] = $ext_attr['seckill_num'];
  741. break;
  742. }
  743. }
  744. }
  745. if (!$open) {
  746. $attr_item[$price_name] = $attr_item['price'];
  747. }
  748. $new_attr[] = $attr_item;
  749. }
  750. $activity_goods_item->attr = json_encode($new_attr, JSON_UNESCAPED_UNICODE);
  751. $activity_goods_item->save();
  752. }
  753. }
  754. public static function skuAttr(&$goods, $goods_attr_item = []) {
  755. $sku_attr = [];
  756. try {
  757. $attr_ids = array_column($goods_attr_item['attr_list'] ?? $goods_attr_item, 'attr_id');
  758. sort($attr_ids);
  759. $sku_attr_ids = implode('_', $attr_ids);
  760. $attrs = json_decode($goods['attr'], true);
  761. foreach($attrs as &$attr){
  762. $attr['cyy_id'] = 'cyy_' . $goods['id'];
  763. $attr['cyy_skuId'] = self::skuid1($goods['id'], $attr);
  764. $attr_ids = array_column($attr['attr_list'], 'attr_id');
  765. sort($attr_ids);
  766. $attr['attr_ids'] = implode(',', $attr_ids);
  767. $item_attr_ids = implode('_', $attr_ids);
  768. if($item_attr_ids === $sku_attr_ids){
  769. $sku_attr = $attr;
  770. }
  771. }
  772. $goods['attr'] = json_encode($attrs);
  773. } catch (\Exception $e) {
  774. \Yii::error([$e, $goods, $attr]);
  775. \Yii::error([__METHOD__, $e->getMessage()]);
  776. }
  777. return $sku_attr;
  778. }
  779. public static function goodsIdOriginal($cyy_id = '') {
  780. return substr($cyy_id, strpos($cyy_id, '_') + 1);
  781. }
  782. public static function skuAttrOriginal(&$goods, $cyy_skuId = '') {
  783. $sku_attr = [];
  784. try {
  785. self::skuAttr($goods);
  786. $attrs = json_decode($goods['attr'], true);
  787. foreach($attrs as &$attr){
  788. if($cyy_skuId === $attr['cyy_skuId']){
  789. $sku_attr = $attr;
  790. }
  791. }
  792. } catch (\Exception $e) {
  793. \Yii::error($e);
  794. \Yii::error([__METHOD__, $e->getMessage()]);
  795. }
  796. return $sku_attr;
  797. }
  798. public static function skuid1($id, $goods_attr_item = []) {
  799. $attr_ids = array_column($goods_attr_item['attr_list'] ?? $goods_attr_item, 'attr_id');
  800. sort($attr_ids);
  801. return 'cyy_' . $id . '_' . implode('_', $attr_ids);
  802. }
  803. public static function skuid2($id, $goods_attr_item = []) {
  804. return $goods_attr_item['no'];
  805. }
  806. public static function getSkuIdAttr($goods, $skuid = '') {
  807. self::skuAttr($goods);
  808. $attrs = json_decode($goods['attr'], true);
  809. foreach($attrs as $attr){
  810. if($attr['cyy_skuId'] === $skuid){
  811. return $attr;
  812. }
  813. }
  814. return [];
  815. }
  816. /**
  817. * @param array $arr | status 状态 name 名称 store_id 商城id
  818. * @return array
  819. */
  820. public static function getAllianceList ($arr = [])
  821. {
  822. $query = Goods::find()->alias('g')
  823. ->where(['g.is_delete' => 0, 'g.md_food_id' => 0])->andWhere(['not like', 'g.name', '当面付']);
  824. if (isset($arr['status']) && $arr['status'] > -1) {
  825. $query->andWhere([
  826. 'g.status' => $arr['status']
  827. ]);
  828. }
  829. $query->andWhere(['AND', ['<>', 'g.store_id', 0], ['<>', 'g.product_type', 4]]);
  830. if (isset($arr['store_id']) && $arr['store_id'] > 0){
  831. $query->andWhere(['store_id'=>$arr['store_id']]);
  832. }
  833. if (isset($arr['name']) && !empty($arr['name'])) {
  834. $query->andWhere([
  835. 'like',
  836. 'g.name',
  837. $arr['name']
  838. ]);
  839. }
  840. $select = ['g.id', 'g.name', 'g.status','g.store_id', 'g.service',
  841. 'g.updated_at', 'g.virtual_sales', 'g.is_verify', 'g.is_negotiable',
  842. 'g.price', 'g.goods_num', 'g.sort' ,'g.cover_pic', 'g.original_price',
  843. 'g.quick_purchase', 'g.attr', 'g.mch_id', 'g.detail', 'g.use_attr', 'g.product_type', 'g.is_recommend','g.store_id'];
  844. $query->select($select)->orderBy(['g.sort' => SORT_ASC, 'g.id' => SORT_DESC]);
  845. $pagination = pagination_make($query);
  846. $pagination['data'] = $pagination['list'];
  847. foreach($pagination['data'] as $k => $v) {
  848. $goods_cat = GoodsCat::find()->alias('gc')
  849. ->leftJoin(['c' => Cat::tableName()], 'gc.cat_id=c.id')
  850. ->where([ 'gc.goods_id' => $v['id'] ])
  851. ->select(['c.name'])
  852. ->asArray()
  853. ->all();
  854. $pagination['data'][$k]['cat'] = $goods_cat;
  855. if($v['store_id']>0){
  856. $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();
  857. $wechatInfo = WechatConfig::find()->where(['store_id'=>$v['store_id'],'is_delete'=>0,'type'=>1])->asArray()->one();
  858. if(isset($pagination['data'][$k]['storeInfo']['id']) && $pagination['data'][$k]['storeInfo']['id']>0){
  859. if($wechatInfo){
  860. $pagination['data'][$k]['storeInfo']['wechat_app_id'] = $wechatInfo['app_id'];
  861. }
  862. $alipayInfo = Option::get('alipay_config',$v['store_id'],'alipay');
  863. if($alipayInfo && $alipayInfo['value']){
  864. $alipayInfo = json_decode($alipayInfo['value'],true);
  865. $pagination['data'][$k]['storeInfo']['alipay_app_id'] = $alipayInfo['app_id'];
  866. }
  867. }
  868. }
  869. }
  870. unset($pagination['list']);
  871. $book_goods_ids = self::checkBookGoods();
  872. return [
  873. 'code' => 0,
  874. 'msg' => 'success',
  875. 'book_goods_ids' => $book_goods_ids,
  876. 'data' => $pagination
  877. ];
  878. }
  879. /**
  880. * @param array $arr | status 状态 name 名称 cat_id 分类id mch_id 入驻商 is_verify 审核 is_league 已设置联盟券 select 查询数据
  881. * @return array
  882. */
  883. public static function getList ($arr = [])
  884. {
  885. $query = Goods::find()->alias('g')
  886. ->leftJoin(['wh' => Warehouse::tableName()], 'wh.id = g.warehouse_id')
  887. ->leftJoin(['whz' => WarehouseZone::tableName()], 'whz.id = g.warehouse_zone_id')
  888. ->where(['g.store_id' => get_store_id(), 'g.is_delete' => 0, 'g.md_food_id' => 0])->andWhere(['not like', 'g.name', '当面付']);
  889. if (isset($arr['cloud_inventory_id'])){
  890. if (!empty($arr['cloud_inventory_id'])){
  891. $query = $query->andWhere(['>', 'g.cloud_inventory_id', 0]);
  892. }else {
  893. $query = $query->andWhere(['=', 'g.cloud_inventory_id', 0]);
  894. }
  895. }
  896. if(!empty($arr['goods_ids'])){
  897. $query = $query->andWhere(['in', 'g.id', $arr['goods_ids']]);
  898. }
  899. if (!empty($arr['select'])) {
  900. $select = $arr['select'];
  901. } else {
  902. $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',
  903. 'g.updated_at', 'g.virtual_sales', 'g.is_verify', 'g.is_negotiable',
  904. 'g.price', 'g.goods_num', 'g.sort' ,'g.cover_pic', 'g.original_price',
  905. 'g.quick_purchase', 'g.attr', 'g.detail', 'g.use_attr',
  906. 'g.product_type', 'g.is_recommend', 'g.cloud_supplier_id', 'g.delivery_rules_id',
  907. 'g.time_made_day', 'g.time_shelf_life',
  908. 'g.mch_id', 'g.mch_audit', 'g.mch_audit_time',
  909. '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'];
  910. if (intval($arr['product_type']) === Goods::GOODS_TYPE_WORKER) {
  911. $select = array_merge($select, ['wge.desc']);
  912. }
  913. }
  914. $sort = '';
  915. $sort_warn_goods_timeout = 0;
  916. $warn_goods_timeout = self::warn_goods_timeout(get_store_id());
  917. if (isset($arr['warn_goods_timeout'])) {
  918. if($warn_goods_timeout){
  919. $sort_warn_goods_timeout = 1;
  920. $query->andWhere(['>', 'g.time_made_day', 0]);
  921. $query->andWhere(['<', '(g.time_made_day + g.time_shelf_life * 86400)', (time() + $warn_goods_timeout * 86400)]);
  922. }
  923. }
  924. if (($arr['warn_goods_timeout_begin_time'])) {
  925. $sort_warn_goods_timeout = 1;
  926. $query->andWhere(['>', 'g.time_made_day', 0]);
  927. $query->andWhere(['>=', '(g.time_made_day + g.time_shelf_life * 86400)', strtotime($arr['warn_goods_timeout_begin_time'])]);
  928. }
  929. if (($arr['warn_goods_timeout_end_time'])) {
  930. $sort_warn_goods_timeout = 1;
  931. $query->andWhere(['>', 'g.time_made_day', 0]);
  932. $query->andWhere(['<', '(g.time_made_day + g.time_shelf_life * 86400)', strtotime($arr['warn_goods_timeout_end_time']) + 86400]);
  933. }
  934. if($sort_warn_goods_timeout){
  935. $select = array_merge($select, ['(g.time_made_day + g.time_shelf_life * 86400) as sort_warn_goods_timeout']);
  936. $sort = 'sort_warn_goods_timeout asc';
  937. }
  938. if ((isset($arr['mch']) && $arr['mch'] == 1)) {
  939. if($arr['mch_id'] > 0){
  940. $query->andWhere(['g.mch_id' => $arr['mch_id']]);
  941. }else{
  942. $query->andWhere(['>', 'g.mch_id', 0]);
  943. $query->andWhere(['g.mch_id' => Mch::find()->select('id')->where(['store_id' => get_store_id(), 'is_delete' => 0])]);
  944. }
  945. if (isset($arr['mch_audit']) && $arr['mch_audit'] > -1) {
  946. $query->andWhere(['g.mch_audit' => $arr['mch_audit']]);
  947. }
  948. if ($arr['mch_audit_time_begin']) {
  949. $query->andWhere(['>=', 'mch_audit_time', strtotime($arr['mch_audit_time_begin'])]);
  950. }
  951. if ($arr['mch_audit_time_end']) {
  952. $query->andWhere(['<=', 'mch_audit_time', strtotime($arr['mch_audit_time_end'])]);
  953. }
  954. if (isset($arr['mch_common_cat_id']) && $arr['mch_common_cat_id'] > 0) {
  955. $query->andWhere(['g.mch_id' => Mch::find()->select('id')->where(['store_id' => get_store_id(), 'mch_common_cat_id' => $arr['mch_common_cat_id']])]);
  956. }
  957. }else{
  958. $query->andWhere(['g.mch_id' => 0]);
  959. }
  960. if (isset($arr['status']) && $arr['status'] > -1) {
  961. $query->andWhere([
  962. 'g.status' => $arr['status']
  963. ]);
  964. }
  965. if (!empty($arr['goods_no'])) {
  966. $query->andWhere([
  967. 'g.goods_no' => $arr['goods_no']
  968. ]);
  969. }
  970. //智配图片商品去除
  971. if (!empty($arr['is_accessories'])) {
  972. $query->andWhere(['OR', [
  973. 'g.accessories_image' => ''
  974. ], [
  975. 'IS', 'g.accessories_image', NULL
  976. ]
  977. ]);
  978. }
  979. if (!empty($arr['is_verify_card'])) {
  980. $query->andWhere([
  981. 'g.product_type' => 0,
  982. 'g.status' => 1
  983. ]);
  984. } else {
  985. if (isset($arr['product_type']) && in_array($arr['product_type'], Goods::$validProductType)) {
  986. $query->andWhere([
  987. 'g.product_type' => $arr['product_type']
  988. ]);
  989. }else{
  990. //剔除掉积分商品
  991. $query->andWhere([
  992. 'g.product_type' => [Goods::GOODS_TYPE_NORMAL,Goods::GOODS_TYPE_DATE,Goods::GOODS_TYPE_TIME,Goods::GOODS_TYPE_VIRTUAL,Goods::GOODS_TYPE_ADOPT]
  993. ]);
  994. }
  995. }
  996. //是否是云仓商品
  997. if (isset($arr['is_cloud']) && $arr['is_cloud'] ) {
  998. $query->andWhere([ '>', 'g.cloud_goods_id' , 0 ]);
  999. }
  1000. //是否拼团商品
  1001. if (isset($arr['is_pt']) && $arr['is_pt'] ) {
  1002. $time = time();
  1003. $pt_activity_id = PtActivity::find()->where(['is_delete' => 0, 'store_id' => get_store_id()])
  1004. ->andWhere(['AND', ['<', 'start_time', $time], ['>', 'end_time', $time]])->select('id')
  1005. ->column();
  1006. $queryPt = PtActivityGoods::find()->alias('pag')->where(['pag.is_delete' => 0, 'g.status' => 1, 'g.is_delete' => 0])
  1007. ->andWhere(['pag.activity_id' => $pt_activity_id])
  1008. ->leftJoin(['g' => Goods::tableName()], 'pag.goods_id = g.id')
  1009. ->select('g.id');
  1010. $query->andWhere(['g.id' => $queryPt]);
  1011. $query->leftJoin(['pag' => PtActivityGoods::tableName()], 'g.id = pag.goods_id');
  1012. $query->leftJoin(['pa' => PtActivity::tableName()], 'pa.id = pag.activity_id');
  1013. $select = array_merge($select, ['pa.party_size', 'pa.party_type', 'pa.party_goods_count']);
  1014. }
  1015. // $query->leftJoin(['sp' => Supplier::tableName()], 'sp.cloud_supplier_id = g.cloud_supplier_id');
  1016. //价格区间
  1017. if (!empty($arr['price_type'])) {
  1018. if ((int)$arr['price_type'] === 1) {
  1019. if ($arr['low_price']) {
  1020. $query->andWhere(['>=', 'g.cost_price', $arr['low_price']]);
  1021. }
  1022. if ($arr['high_price']) {
  1023. $query->andWhere(['<=', 'g.cost_price', $arr['high_price']]);
  1024. }
  1025. }
  1026. if ((int)$arr['price_type'] === 2) {
  1027. if ($arr['low_price']) {
  1028. $query->andWhere(['>=', 'g.price', $arr['low_price']]);
  1029. }
  1030. if ($arr['high_price']) {
  1031. $query->andWhere(['<=', 'g.price', $arr['high_price']]);
  1032. }
  1033. }
  1034. }
  1035. if (isset($arr['low_num']) && $arr['low_num']) {
  1036. $query->andWhere(['>=', 'g.goods_num', $arr['low_num']]);
  1037. }
  1038. if (isset($arr['high_num']) && $arr['high_num']) {
  1039. $query->andWhere(['<=', 'g.goods_num', $arr['high_num']]);
  1040. }
  1041. if (isset($arr['supplier_id']) && $arr['supplier_id']) {
  1042. $query->andWhere(['g.cloud_supplier_id' => $arr['supplier_id']]);
  1043. }
  1044. if (isset($arr['delivery_rules_id']) && $arr['delivery_rules_id']) {
  1045. $query->andWhere(['g.delivery_rules_id' => $arr['delivery_rules_id']]);
  1046. }
  1047. if (isset($arr['name']) && !empty($arr['name'])) {
  1048. $query->andWhere([
  1049. 'or',
  1050. ['like', 'g.name', $arr['name']],
  1051. ['like', 'g.key_word', $arr['name']],
  1052. ]);
  1053. }
  1054. if (intval($arr['product_type']) === Goods::GOODS_TYPE_WORKER) {
  1055. $query->leftJoin(['wge' => WorkerGoodsExt::tableName()], 'g.id = wge.goods_id');
  1056. }
  1057. if (intval($arr['product_type']) === Goods::GOODS_TYPE_TIME) {
  1058. $query->leftJoin(['bge' => BookingGoodsExt::tableName()], 'g.id = bge.goods_id');
  1059. }
  1060. if (isset($arr['cat_id']) && $arr['cat_id'] > 0) {
  1061. if (intval($arr['product_type']) === Goods::GOODS_TYPE_WORKER) {
  1062. $query->andWhere(['wge.cat_id' => $arr['cat_id']]);
  1063. } else if (intval($arr['product_type']) === Goods::GOODS_TYPE_TIME){
  1064. $query->andWhere(['bge.cat_id' => $arr['cat_id']]);
  1065. } else {
  1066. if ($arr['mch_id'] > 0) {
  1067. $query->leftJoin(['mgc' => MchGoodsCat::tableName()], 'g.id = mgc.goods_id')->andWhere([
  1068. 'mgc.cat_id' => MchCat::getCatId($arr['cat_id'])
  1069. ])->groupBy('mgc.goods_id');
  1070. } else {
  1071. $query->leftJoin(['gc' => GoodsCat::tableName()], 'g.id = gc.goods_id')->andWhere([
  1072. 'gc.cat_id' => Cat::getCatId($arr['cat_id'])
  1073. ])->groupBy('gc.goods_id');
  1074. }
  1075. }
  1076. }
  1077. //门店增加收银台 查询商品列表逻辑
  1078. if ($arr['md_id'] && $arr['md_id'] > 0) {
  1079. $md = Md::findOne($arr['md_id']);
  1080. if ($md && intval($md->is_single)) {
  1081. $query->leftJoin(['mg' => MdGoods::tableName()], 'g.id = mg.goods_id')->andWhere(['mg.md_id' => $arr['md_id'], 'mg.status' => 1]);
  1082. $select = array_merge($select, ['md_goods_price' => 'mg.price', 'md_goods_num' => 'mg.goods_num', 'md_goods_attr' => 'mg.attr']);
  1083. }
  1084. }
  1085. if (isset($arr['mch_cat_id']) && $arr['mch_cat_id'] > 0) {
  1086. $query->leftJoin(['mgc' => MchGoodsCat::tableName()], 'g.id = mgc.goods_id')->andWhere([
  1087. 'mgc.cat_id' => MchCat::getCatId($arr['mch_cat_id'])
  1088. ])->groupBy('mgc.goods_id');
  1089. }
  1090. // 根据入驻商查询 0 平台 -1 全部入驻商 >0 对应入驻商
  1091. // if (empty($arr['mch_id'])) {
  1092. // $query->andWhere(['g.mch_id' => 0]);
  1093. // } elseif(isset($arr['mch_id']) && $arr['mch_id'] == -1) {
  1094. // $query->andWhere([
  1095. // '>',
  1096. // 'g.mch_id',
  1097. // 0
  1098. // ]);
  1099. // } elseif (isset($arr['mch_id']) && $arr['mch_id'] > 0){
  1100. // $query->andWhere(['g.mch_id' => $arr['mch_id']]);
  1101. // }
  1102. // 获取入驻商待审核商品
  1103. if (isset($arr['is_verify'])) {
  1104. $query->andWhere([
  1105. 'g.is_verify' => $arr['is_verify']
  1106. ]);
  1107. }
  1108. if (isset($arr['is_league']) && $arr['is_league'] > -1) {
  1109. if ($arr['is_league'] == 1) {
  1110. $query->andWhere([
  1111. 'or',
  1112. ['>', 'goods_take_price', 0],
  1113. ['>', 'goods_send_profit', 0],
  1114. ]);
  1115. }else{
  1116. $query->andWhere([
  1117. 'and',
  1118. [
  1119. 'or',
  1120. ['goods_take_price' => 0],
  1121. ['goods_take_price' => null],
  1122. ],
  1123. [
  1124. 'or',
  1125. ['goods_send_profit' => 0],
  1126. ['goods_send_profit' => null],
  1127. ],
  1128. ]);
  1129. }
  1130. }
  1131. $query->select($select);
  1132. if (!empty($arr['sort'])) {
  1133. $query->orderBy(['g.sort' => $arr['sort'] == 'asc' ? SORT_ASC : SORT_DESC, 'g.id' => SORT_DESC]);
  1134. } else {
  1135. $query->orderBy(['g.id' => SORT_DESC]);
  1136. }
  1137. if($sort){
  1138. $query->orderBy($sort);
  1139. }
  1140. // return $query->createCommand()->getRawSql();
  1141. $pagination = pagination_make($query);
  1142. if (isset($arr['mch']) && $arr['mch'] == 1) {
  1143. $mchList = (new MchForm(['store_id' => get_store_id()]))->mchSelectList();
  1144. $mchCommonCatSelectList = (new MchForm(['store_id' => get_store_id()]))->mchCommonCatSelectList();
  1145. }
  1146. $list = $pagination['list'];
  1147. foreach($list as &$item) {
  1148. if (isset($arr['mch']) && $arr['mch'] == 1) {
  1149. $item['mch'] = $mchList['data'][$item['mch_id']];
  1150. $item['mch_common_cat'] = $mchCommonCatSelectList['data'][$item['mch']['mch_common_cat_id']];
  1151. $goods_cat = MchGoodsCat::find()->alias('mgc')
  1152. ->leftJoin(['mc' => MchCat::tableName()], 'mgc.cat_id=mc.id')
  1153. ->where([ 'mgc.goods_id' => $item['id'] ])
  1154. ->select(['mc.name'])
  1155. ->asArray()
  1156. ->all();
  1157. $item['mch_cat'] = $goods_cat;
  1158. }
  1159. $item['warehouse_name'] = $item['wh_name'].'-'.$item['whz_name'];
  1160. $item['timeout_day'] = $item['time_made_day'] ? date('Y-m-d', $item['time_made_day'] + 86400 * $item['time_shelf_life']) : '--';
  1161. $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;
  1162. //品牌
  1163. $item['brand_name'] = self::getBrand($item['brand_id']) ?: '暂无品牌';
  1164. //门店增加收银台 查询商品列表逻辑
  1165. if ($arr['md_id'] && $arr['md_id'] > 0) {
  1166. $item['price'] = $item['md_goods_price'] ?: $item['price'];
  1167. $item['goods_num'] = $item['md_goods_num'] ?: $item['goods_num'];
  1168. $item['attr'] = $item['md_goods_attr'] ?: $item['attr'];
  1169. }
  1170. $delivery_rules = DeliveryRules::find()->where(['id' => $item['delivery_rules_id'], 'is_delete' => 0, 'store_id' => get_store_id()])->select('type, times, days')->asArray()->one();
  1171. if ($delivery_rules) {
  1172. $delivery_rules['times'] = (int)$delivery_rules['type'] === 1 ? '' : date("m月d日 H:i:s", $delivery_rules['times']);
  1173. }
  1174. $supplier = Supplier::findOne(['cloud_supplier_id'=>$item['cloud_supplier_id']]);
  1175. $item['supplier_name'] = $supplier->supplier_name ?: ($supplier->name ?: '');
  1176. $item['supplier_logo'] = $supplier->logo ?: '';
  1177. $item['supplier_phone'] = $supplier->phone ?: '';
  1178. $item['delivery_rules'] = $delivery_rules ?: null;
  1179. $goods_cat = [];
  1180. //如果是积分兑换商品
  1181. if (isset($arr['product_type']) && $arr['product_type'] == Goods::GOODS_TYPE_INTEGRAL) {
  1182. $goods_cat = GoodsCat::find()->alias('gc')
  1183. ->leftJoin(['c' => NewIntegralCat::tableName()], 'gc.cat_id=c.id')
  1184. ->where([ 'gc.goods_id' => $item['id'] ])
  1185. ->select(['c.name'])
  1186. ->asArray()
  1187. ->all();
  1188. } elseif (isset($arr['product_type']) && $arr['product_type'] == Goods::GOODS_TYPE_WORKER) {
  1189. $worker_goods_ext = WorkerGoodsExt::findOne(['goods_id' => $item['id']]);
  1190. if ($worker_goods_ext) {
  1191. $goods_cat = WorkerGoodsCat::find()->where(['id' => $worker_goods_ext->cat_id])->select('name')
  1192. ->asArray()->all();
  1193. }
  1194. } elseif (isset($arr['product_type']) && $arr['product_type'] == Goods::GOODS_TYPE_TIME) {
  1195. $booking_goods_ext = BookingGoodsExt::findOne(['goods_id' => $item['id']]);
  1196. if ($booking_goods_ext) {
  1197. $goods_cat = BookingGoodsCat::find()->where(['id' => $booking_goods_ext->cat_id])->select('name')
  1198. ->asArray()->all();
  1199. }
  1200. } else {
  1201. $goods_cat = GoodsCat::find()->alias('gc')
  1202. ->leftJoin(['c' => Cat::tableName()], 'gc.cat_id=c.id')
  1203. ->where([ 'gc.goods_id' => $item['id'] ])
  1204. ->select(['c.name'])
  1205. ->asArray()
  1206. ->all();
  1207. }
  1208. $item['cat'] = $goods_cat;
  1209. if (intval($arr['product_type']) === Goods::GOODS_TYPE_WORKER) {
  1210. $item['order_num'] = OrderDetail::find()->where(['goods_id' => $item['id']])->groupBy('order_id')->count();
  1211. $item['sales'] = OrderDetail::find()->where(['goods_id' => $item['id']])->select('num')->sum('num');
  1212. }
  1213. if (intval($arr['product_type']) === Goods::GOODS_TYPE_TIME) {
  1214. $item['order_num'] = OrderDetail::find()->where(['goods_id' => $item['id']])->groupBy('order_id')->count();
  1215. $item['sales'] = OrderDetail::find()->where(['goods_id' => $item['id']])->select('num')->sum('num');
  1216. }
  1217. }
  1218. unset($pagination['list']);
  1219. $book_goods_ids = self::checkBookGoods();
  1220. foreach ($list as &$item) {
  1221. $count = \Yii::$app->db->createCommand("SELECT COUNT(*) FROM ysdl_external_commodity_selection where mer_no = :store_id and commodity_id= :commodity_id")
  1222. ->bindValues([
  1223. ':store_id' => $item['store_id'],
  1224. ':commodity_id' => $item['cloud_goods_id'],
  1225. ])
  1226. ->queryScalar();
  1227. if($count<=0){
  1228. \Yii::$app->db->createCommand()->insert('ysdl_external_commodity_selection', [
  1229. 'mer_no'=> $item['store_id'],
  1230. 'commodity_id'=>$item['cloud_goods_id'],
  1231. 'is_off_shelf'=>(empty($item['cloud_status']) ? 1 : 0),
  1232. ])->execute();
  1233. }
  1234. }
  1235. return [
  1236. 'q' => $query->createCommand()->getRawSql(),
  1237. 'code' => 0,
  1238. 'msg' => 'success',
  1239. 'book_goods_ids' => $book_goods_ids,
  1240. 'warn_goods_timeout' => $warn_goods_timeout,
  1241. 'data' => [
  1242. 'data' => $list,
  1243. 'pageNo' => $pagination['pageNo'],
  1244. 'totalCount' => $pagination['totalCount']
  1245. ]
  1246. ];
  1247. }
  1248. public static function getActivityList($arr = []){
  1249. // 先获取当前进行中的活动以及即将开始的活动
  1250. $query = MdGroupActivitiesGoods::find()->alias('mgag')
  1251. ->leftJoin(['mga'=>MdGroupActivities::tableName()], 'mgag.activities_id=mga.id')
  1252. ->where(['mgag.is_delete'=>0,'mga.is_delete'=>0,'mga.store_id'=>get_store_id()])
  1253. ->andWhere(['or',['and',['<','mga.start_time',time()],['>','mga.end_time',time()]],['and',['>=','mga.start_time',time()],['>=','mga.end_time',time()]]])
  1254. ->leftJoin(['g'=>Goods::tableName()],'mgag.goods_id=g.id')
  1255. ->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']);
  1256. $result = pagination_make($query,true);
  1257. foreach($result['list'] as &$item){
  1258. $delivery_rules = DeliveryRules::find()->where(['id' => $item['delivery_rules_id'], 'is_delete' => 0, 'store_id' => get_store_id()])->select('type, times, days')->asArray()->one();
  1259. if ($delivery_rules) {
  1260. $delivery_rules['times'] = (int)$delivery_rules['type'] === 1 ? '' : date("m月d日 H:i:s", $delivery_rules['times']);
  1261. }
  1262. $item['delivery_rules'] = $delivery_rules ?: null;
  1263. }
  1264. return [
  1265. 'code'=>0,
  1266. 'data'=>[
  1267. 'data' => $result['list'],
  1268. 'pageNo' => $result['pageNo'],
  1269. 'totalCount' => $result['totalCount']
  1270. ]
  1271. ];
  1272. }
  1273. /**
  1274. * @param array $arr | status 状态 name 名称 cat_id 分类id mch_id 入驻商 is_verify 审核 select 查询数据
  1275. * @return array
  1276. */
  1277. public static function getFoodGoodsList ($arr = [])
  1278. {
  1279. $query = Goods::find()->alias('g')
  1280. ->where(['g.store_id' => get_store_id(), 'g.is_delete' => 0])->andWhere(['not like', 'g.name', '当面付']);
  1281. if (isset($arr['status']) && $arr['status'] > -1) {
  1282. $query->andWhere([
  1283. 'g.status' => $arr['status']
  1284. ]);
  1285. }
  1286. if (isset($arr['product_type']) && in_array($arr['product_type'], [0, 1, 2])) {
  1287. $query->andWhere([
  1288. 'g.product_type' => $arr['product_type']
  1289. ]);
  1290. }
  1291. if (isset($arr['name']) && !empty($arr['name'])) {
  1292. $query->andWhere([
  1293. 'like',
  1294. 'g.name',
  1295. $arr['name']
  1296. ]);
  1297. }
  1298. if (isset($arr['cat_id']) && $arr['cat_id'] > 0) {
  1299. if ($arr['mch_id'] > 0) {
  1300. $query->leftJoin(['mgc' => MchGoodsCat::tableName()], 'g.id = mgc.goods_id')->andWhere([
  1301. 'mgc.cat_id' => MchCat::getCatId($arr['cat_id'])
  1302. ])->groupBy('mgc.goods_id');
  1303. } else {
  1304. $query->leftJoin(['gc' => GoodsCat::tableName()], 'g.id = gc.goods_id')->andWhere([
  1305. 'gc.cat_id' => Cat::getCatId($arr['cat_id'])
  1306. ])->groupBy('gc.goods_id');
  1307. }
  1308. }
  1309. if (isset($arr['mch_cat_id']) && $arr['mch_cat_id'] > 0) {
  1310. $query->leftJoin(['mgc' => MchGoodsCat::tableName()], 'g.id = mgc.goods_id')->andWhere([
  1311. 'mgc.cat_id' => MchCat::getCatId($arr['mch_cat_id'])
  1312. ])->groupBy('mgc.goods_id');
  1313. }
  1314. // 根据入驻商查询 0 平台 -1 全部入驻商 >0 对应入驻商
  1315. if (empty($arr['mch_id'])) {
  1316. $query->andWhere(['g.mch_id' => 0]);
  1317. } elseif(isset($arr['mch_id']) && $arr['mch_id'] == -1) {
  1318. $query->andWhere([
  1319. '>',
  1320. 'g.mch_id',
  1321. 0
  1322. ]);
  1323. } elseif (isset($arr['mch_id']) && $arr['mch_id'] > 0){
  1324. $query->andWhere(['g.mch_id' => $arr['mch_id']]);
  1325. }
  1326. // 获取入驻商待审核商品
  1327. if (isset($arr['is_verify'])) {
  1328. $query->andWhere([
  1329. 'g.is_verify' => $arr['is_verify']
  1330. ]);
  1331. }
  1332. if (!empty($arr['select'])) {
  1333. $select = $arr['select'];
  1334. } else {
  1335. $select = ['g.id', 'g.name', 'g.status', 'g.service',
  1336. 'g.updated_at', 'g.virtual_sales', 'g.is_verify', 'g.is_negotiable',
  1337. 'g.price', 'g.goods_num', 'g.sort' ,'g.cover_pic', 'g.original_price',
  1338. 'g.quick_purchase', 'g.attr', 'g.mch_id', 'g.detail', 'g.use_attr', 'g.product_type', 'g.is_recommend'];
  1339. }
  1340. $query->with(['mch'])->select($select)
  1341. ->orderBy(['g.sort' => SORT_ASC, 'g.id' => SORT_DESC]);
  1342. $pagination = pagination_make($query);
  1343. $pagination['data'] = $pagination['list'];
  1344. foreach($pagination['data'] as $k => $v) {
  1345. $goods_cat = GoodsCat::find()->alias('gc')
  1346. ->leftJoin(['c' => Cat::tableName()], 'gc.cat_id=c.id')
  1347. ->where([ 'gc.goods_id' => $v['id'] ])
  1348. ->select(['c.name'])
  1349. ->asArray()
  1350. ->all();
  1351. $pagination['data'][$k]['cat'] = $goods_cat;
  1352. }
  1353. unset($pagination['list']);
  1354. $book_goods_ids = self::checkBookGoods();
  1355. return [
  1356. 'code' => 0,
  1357. 'msg' => 'success',
  1358. 'book_goods_ids' => $book_goods_ids,
  1359. 'data' => $pagination
  1360. ];
  1361. }
  1362. /**
  1363. * 获取商品可选的规格列表
  1364. */
  1365. public function getAttrGroupList($is_pt = 0, $use_attr = 1, $is_platform = 0)
  1366. {
  1367. $attr_rows = json_decode($this->attr, true);
  1368. if (empty($attr_rows)) {
  1369. return [];
  1370. }
  1371. $attr_group_list = [];
  1372. foreach ($attr_rows as $attr_row) {
  1373. foreach ($attr_row['attr_list'] as $i => $attr) {
  1374. $attr_id = $attr['attr_id'];
  1375. $attr = Attr::findOne(['id' => $attr_id, 'is_delete' => 0]);
  1376. if (!$attr) {
  1377. continue;
  1378. }
  1379. $in_list = false;
  1380. foreach ($attr_group_list as $j => $attr_group) {
  1381. if ($attr_group->attr_group_id == $attr->attr_group_id) {
  1382. $attr_obj = (object)[
  1383. 'attr_id' => $attr->id,
  1384. 'attr_name' => $attr->attr_name,
  1385. ];
  1386. if (!in_array($attr_obj, $attr_group_list[$j]->attr_list)) {
  1387. $attr_group_list[$j]->attr_list[] = $attr_obj;
  1388. }
  1389. $in_list = true;
  1390. continue;
  1391. }
  1392. }
  1393. if (!$in_list) {
  1394. $attr_group = AttrGroup::findOne(['is_delete' => 0, 'id' => $attr->attr_group_id]);
  1395. if ($attr_group) {
  1396. $attr_group_list[] = (object)[
  1397. 'attr_group_id' => $attr_group->id,
  1398. 'attr_group_name' => $attr_group->attr_group_name,
  1399. 'attr_list' => [
  1400. (object)[
  1401. 'attr_id' => $attr->id,
  1402. 'attr_name' => $attr->attr_name,
  1403. ],
  1404. ],
  1405. ];
  1406. }
  1407. }
  1408. }
  1409. }
  1410. if ((int)$use_attr === 0) {
  1411. $attr_group_list = [
  1412. $attr_group_list[0]
  1413. ];
  1414. }
  1415. //判断是否为拼团产品
  1416. //拼团活动信息
  1417. if ($is_pt) {
  1418. $query = PtActivityGoods::find()->alias('pag')->where(['pag.is_delete' => 0, 'pa.is_delete' => 0, 'pa.store_id' => get_store_id()])
  1419. ->leftJoin(['pa' => PtActivity::tableName()], 'pag.activity_id = pa.id')
  1420. ->andWhere(['pag.goods_id' => $this->id])
  1421. ->select('pag.pt_price, pag.sale_num, pag.virtual_sales, pa.party_size, pa.party_type, pa.party_goods_count')
  1422. ->andWhere(['AND', ['<', 'pa.start_time', time()], ['>', 'pa.end_time', time()]]);
  1423. $query->andWhere(['OR', ['is_platform' => 1, 'is_platform_audit' => 1, 'status' => 1], ['is_platform' => 0]]);
  1424. $ptGoodsActivity = $query->asArray()->one();
  1425. if ($ptGoodsActivity) {
  1426. $attr_name = $ptGoodsActivity['party_size'] . "人团";
  1427. if($ptGoodsActivity['party_type'] == 1){
  1428. $attr_name = $ptGoodsActivity['party_goods_count'] . "个商品成团";
  1429. }
  1430. $pt_attr = [
  1431. [
  1432. "attr_group_id" => -1,
  1433. "attr_group_name" => "拼团",
  1434. "attr_list" => [
  1435. [
  1436. "attr_id" => -1,
  1437. "attr_name" => $attr_name
  1438. ]
  1439. ]
  1440. ]
  1441. ];
  1442. $attr_group_list = array_values(array_merge($pt_attr, $attr_group_list));
  1443. }
  1444. }
  1445. return $attr_group_list;
  1446. }
  1447. /**
  1448. * 获取商品分类
  1449. * @param $goods
  1450. * @return array
  1451. */
  1452. public static function getCatList($goods)
  1453. {
  1454. $cat_list = [];
  1455. $cat = GoodsCat::findAll(['goods_id' => $goods->id, 'store_id' => $goods->store_id, 'is_delete' => 0]);
  1456. if ($cat) {
  1457. foreach ($cat as $index => $value) {
  1458. $cat_name = Cat::findOne(['id' => $value->cat_id, 'store_id' => $goods->store_id]);
  1459. $cat_list[$index]['cat_id'] = $value->cat_id;
  1460. $cat_list[$index]['cat_name'] = $cat_name->name;
  1461. }
  1462. } else {
  1463. $cat_name = Cat::findOne(['id' => $goods->cat_id]);
  1464. $cat_list[0]['cat_id'] = $goods->cat_id;
  1465. $cat_list[0]['cat_name'] = $cat_name->name;
  1466. }
  1467. return $cat_list;
  1468. }
  1469. /**
  1470. * 获取商品总库存
  1471. * @param int $id 商品id
  1472. */
  1473. public function getNum($id = null)
  1474. {
  1475. $goods = null;
  1476. if (!$id) {
  1477. $goods = $this;
  1478. } else {
  1479. $goods = static::findOne($id);
  1480. if (!$goods) {
  1481. return 0;
  1482. }
  1483. }
  1484. $attr = $goods->attr;
  1485. if (get_md_id()) {
  1486. $md_goods = MdGoods::findOne(['goods_id' => $id ?: $goods->id, 'md_id' => get_md_id()]);
  1487. if ($md_goods) {
  1488. $attr = $md_goods->attr;
  1489. }
  1490. }
  1491. if ((int)$goods->product_type === 2) {
  1492. return $goods->service_book_num;
  1493. }
  1494. if ((int)$goods->product_type === 4) {
  1495. return $goods->goods_num;
  1496. }
  1497. if (!$attr) {
  1498. return 0;
  1499. }
  1500. $num = 0;
  1501. $attr_rows = json_decode($attr, true);
  1502. foreach ($attr_rows as $attr_row) {
  1503. $num += intval($attr_row['num']);
  1504. }
  1505. return $num;
  1506. }
  1507. /**
  1508. * 库存增加操作
  1509. */
  1510. public function numAdd($attr_id_list, $num, $reset = 0)
  1511. {
  1512. if(!is_array($attr_id_list[0])){
  1513. $attr_id_list = [
  1514. ['attr_id_list' => $attr_id_list, 'num' => $num]
  1515. ];
  1516. }
  1517. $_attr = $this->attr;
  1518. $is_md = false;
  1519. if (get_md_id()) {
  1520. $md_goods = MdGoods::findOne(['goods_id' => $this->id, 'md_id' => get_md_id()]);
  1521. if ($md_goods) {
  1522. $_attr = $md_goods->attr;
  1523. $is_md = true;
  1524. }
  1525. }
  1526. $attr_group_list = json_decode($_attr);
  1527. $add_attr_num = false;
  1528. foreach($attr_id_list as $item){
  1529. $num = $item['num'];
  1530. sort($item['attr_id_list']);
  1531. foreach ($attr_group_list as $i => $attr_group) {
  1532. $group_attr_id_list = [];
  1533. foreach ($attr_group->attr_list as $attr) {
  1534. array_push($group_attr_id_list, $attr->attr_id);
  1535. }
  1536. sort($group_attr_id_list);
  1537. if (!array_diff($item['attr_id_list'], $group_attr_id_list)) {
  1538. if($reset){
  1539. $attr_group_list[$i]->num = intval($num);
  1540. }else{
  1541. $attr_group_list[$i]->num = intval($attr_group_list[$i]->num) + $num;
  1542. }
  1543. $add_attr_num = true;
  1544. break;
  1545. }
  1546. }
  1547. }
  1548. if (!$add_attr_num) {
  1549. return false;
  1550. }
  1551. if (!$is_md) {
  1552. $this->attr = json_encode($attr_group_list, JSON_UNESCAPED_UNICODE);
  1553. if ($this->attr) {
  1554. $attr_rows = json_decode($this->attr, true);
  1555. $goods_num = 0;
  1556. foreach ($attr_rows as $attr_row) {
  1557. $goods_num += intval($attr_row['num']);
  1558. }
  1559. $this->goods_num = $goods_num;
  1560. }
  1561. $this->save();
  1562. } else {
  1563. // 门店商品逻辑
  1564. $md_goods->attr = json_encode($attr_group_list, JSON_UNESCAPED_UNICODE);
  1565. if ($md_goods->attr) {
  1566. $attr_rows = json_decode($md_goods->attr, true);
  1567. $goods_num = 0;
  1568. foreach ($attr_rows as $attr_row) {
  1569. $goods_num += intval($attr_row['num']);
  1570. }
  1571. $md_goods->goods_num = $goods_num;
  1572. }
  1573. $md_goods->save();
  1574. }
  1575. return true;
  1576. }
  1577. /**
  1578. * 库存减少操作
  1579. * @param array $attr_id_list eg. [1,4,2]
  1580. */
  1581. public function numSub($attr_id_list, $num)
  1582. {
  1583. if(!is_array($attr_id_list[0])){
  1584. $attr_id_list = [
  1585. ['attr_id_list' => $attr_id_list, 'num' => $num]
  1586. ];
  1587. }
  1588. $_attr = $this->attr;
  1589. $is_md = false;
  1590. if (get_md_id()) {
  1591. $md_goods = MdGoods::findOne(['goods_id' => $this->id, 'md_id' => get_md_id()]);
  1592. if ($md_goods) {
  1593. $_attr = $md_goods->attr;
  1594. $is_md = true;
  1595. }
  1596. }
  1597. $attr_group_list = json_decode($_attr);
  1598. $sub_attr_num = false;
  1599. foreach($attr_id_list as $item){
  1600. $num = $item['num'];
  1601. sort($item['attr_id_list']);
  1602. foreach ($attr_group_list as $i => $attr_group) {
  1603. $group_attr_id_list = [];
  1604. foreach ($attr_group->attr_list as $attr) {
  1605. array_push($group_attr_id_list, $attr->attr_id);
  1606. }
  1607. sort($group_attr_id_list);
  1608. if (!array_diff($item['attr_id_list'], $group_attr_id_list)) {
  1609. if ($num > intval($attr_group_list[$i]->num)) {
  1610. return false;
  1611. }
  1612. $attr_group_list[$i]->num = intval($attr_group_list[$i]->num) - $num;
  1613. $sub_attr_num = true;
  1614. break;
  1615. }
  1616. }
  1617. }
  1618. if (!$sub_attr_num) {
  1619. return false;
  1620. }
  1621. if (!$is_md) {
  1622. $this->attr = json_encode($attr_group_list, JSON_UNESCAPED_UNICODE);
  1623. if ($this->attr) {
  1624. $attr_rows = json_decode($this->attr, true);
  1625. $goods_num = 0;
  1626. foreach ($attr_rows as $attr_row) {
  1627. $goods_num += intval($attr_row['num']);
  1628. }
  1629. $this->goods_num = $goods_num;
  1630. }
  1631. $this->save();
  1632. } else {
  1633. $md_goods->attr = json_encode($attr_group_list, JSON_UNESCAPED_UNICODE);
  1634. if ($md_goods->attr) {
  1635. $attr_rows = json_decode($md_goods->attr, true);
  1636. $goods_num = 0;
  1637. foreach ($attr_rows as $attr_row) {
  1638. $goods_num += intval($attr_row['num']);
  1639. }
  1640. $md_goods->goods_num = $goods_num;
  1641. }
  1642. $md_goods->save();
  1643. }
  1644. return true;
  1645. }
  1646. /**
  1647. * 库存增加操作
  1648. */
  1649. public static function numAddStatic($goods_id, $attr_id_list, $num, $reset = 0)
  1650. {
  1651. $goods = Goods::findOne($goods_id);
  1652. if (!$goods) {
  1653. return false;
  1654. }
  1655. return $goods->numAdd($attr_id_list, $num, $reset);
  1656. }
  1657. /**
  1658. * 库存减少操作
  1659. */
  1660. public static function numSubStatic($goods_id, $attr_id_list, $num)
  1661. {
  1662. $goods = Goods::findOne($goods_id);
  1663. if (!$goods) {
  1664. return false;
  1665. }
  1666. return $goods->numSub($attr_id_list, $num);
  1667. }
  1668. /**
  1669. * 获取商品组图
  1670. * @return \yii\db\ActiveQuery
  1671. */
  1672. public function getGoodsPicList()
  1673. {
  1674. return $this->hasMany(GoodsPic::className(), ['goods_id' => 'id'])->where(['is_delete' => 0]);
  1675. }
  1676. /**
  1677. * 获取入驻商
  1678. * @return \yii\db\ActiveQuery
  1679. */
  1680. public function getMch()
  1681. {
  1682. return $this->hasMany(Mch::className(), ['id' => 'mch_id'])->select(['id', 'name', 'logo', 'service_tel', 'is_delete']);
  1683. }
  1684. public function getMchCatList()
  1685. {
  1686. $cat_list = [];
  1687. $cat = MchGoodsCat::findAll(['goods_id' => $this->id]);
  1688. if ($cat) {
  1689. foreach ($cat as $index => $value) {
  1690. $cat_name = MchCat::findOne(['id' => $value->cat_id]);
  1691. $cat_list[] = $cat_name->name;
  1692. }
  1693. } else {
  1694. $cat_list[] = "";
  1695. }
  1696. return implode(',', $cat_list);
  1697. }
  1698. public static function getGoodsPicStatic($goods_id, $index = 0)
  1699. {
  1700. $goods = Goods::findOne($goods_id);
  1701. if (!$goods) {
  1702. return null;
  1703. }
  1704. return $goods->getGoodsPic($index);
  1705. }
  1706. /**
  1707. * 获取商品销量
  1708. */
  1709. public function getSalesVolume()
  1710. {
  1711. $cacheKey = 'getSalesVolume' . $this->id;
  1712. $cacheVal = cache()->get($cacheKey);
  1713. if($cacheVal){
  1714. return $cacheVal;
  1715. }
  1716. $res = OrderDetail::find()->alias('od')
  1717. ->select('SUM(od.num) AS sales_volume')
  1718. ->leftJoin(['o' => Order::tableName()], 'od.order_id=o.id')
  1719. ->where(['od.is_delete' => 0, 'od.goods_id' => $this->id, 'o.is_delete' => 0, 'o.is_pay' => 1,])
  1720. ->asArray()->one();
  1721. $v = empty($res['sales_volume']) ? 0 : intval($res['sales_volume']);
  1722. if($v > 300){
  1723. cache()->set($cacheKey, $v, 3 * 60);
  1724. }
  1725. return $v;
  1726. }
  1727. /**
  1728. * 获取商品销量
  1729. */
  1730. public function getSalesVolumeVideoShopShare()
  1731. {
  1732. $cacheKey = 'getSalesVolumeVideoShopShare' . $this->id;
  1733. $cacheVal = cache()->get($cacheKey);
  1734. if($cacheVal){
  1735. return $cacheVal;
  1736. }
  1737. $res = OrderDetail::find()->alias('od')
  1738. ->select('SUM(od.num) AS sales_volume')
  1739. ->leftJoin(['o' => Order::tableName()], 'od.order_id=o.id')
  1740. ->leftJoin(['v' => VideoShopOrderExt::tableName()], 'v.order_id=o.id')
  1741. ->where(['od.is_delete' => 0, 'od.goods_id' => $this->id, 'o.is_delete' => 0, 'o.is_pay' => 1,])
  1742. ->andWhere('isnull(v.id)')
  1743. ->asArray()->one();
  1744. $v = empty($res['sales_volume']) ? 0 : intval($res['sales_volume']);
  1745. if($v > 300){
  1746. cache()->set($cacheKey, $v, 3 * 60);
  1747. }
  1748. return $v;
  1749. }
  1750. public static function getGoodsNum($goods = null){
  1751. $types = [
  1752. Goods::GOODS_TYPE_DATE,
  1753. ];
  1754. if(!$goods){
  1755. return [
  1756. 'code' => 1,
  1757. 'data' => '参数错误',
  1758. ];
  1759. }
  1760. if(!in_array($goods->product_type, $types)){
  1761. $nums = 0;
  1762. }
  1763. if ($goods->product_type == Goods::GOODS_TYPE_DATE) {
  1764. $stime = mktime(0, 0, 0, date('m') - 1, 1);
  1765. $endtime = mktime(0, 0, 0, date('m') + 2, 1);
  1766. $query = OrderDetail::find()->alias('od')->leftJoin(['o' => Order::tableName()], 'o.id = od.order_id')
  1767. ->where([
  1768. 'od.goods_id' => $goods->id,
  1769. 'o.trade_status' => [
  1770. Order::ORDER_FLOW_DEFAULT,
  1771. Order::ORDER_FLOW_NO_SEND,
  1772. Order::ORDER_FLOW_SEND,
  1773. Order::ORDER_FLOW_CONFIRM,
  1774. ],
  1775. ]);
  1776. $query->andWhere(['>', 'o.created_at', $stime]);
  1777. $list = $query->all();
  1778. $order = [];
  1779. $nums = [];
  1780. $goodsNum = $goods->getNum();
  1781. foreach ($list as $item) {
  1782. $attr = json_decode($item['attr'], true);
  1783. foreach ($attr as $iattr) {
  1784. $order[$iattr['date']]+=$item['num'];
  1785. }
  1786. }
  1787. for($i = time(); $i < $endtime; $i+=86400){
  1788. $date = date('Y-m-d', $i);
  1789. $book_price = $goods->price;
  1790. $newGoodsNum = $goodsNum;
  1791. if (!empty($goods->service_book_desc)) {
  1792. $service_book_desc = json_decode($goods->service_book_desc, true);
  1793. foreach ($service_book_desc as $value) {
  1794. if (strtotime($value['date']) === strtotime($date)) {
  1795. $goodsNum = $value['num'];
  1796. $book_price = $value['price'];
  1797. }
  1798. }
  1799. }
  1800. $num = $goodsNum - $order[$date];
  1801. $nums[] = [
  1802. 'date' => $date,
  1803. 'num' => $num > 0 ? $num : 0,
  1804. 'price' => $book_price
  1805. ];
  1806. $goodsNum = $newGoodsNum;
  1807. }
  1808. }
  1809. return [
  1810. 'code' => 0,
  1811. 'data' => $nums,
  1812. ];
  1813. }
  1814. /**
  1815. * 获取商品卡券
  1816. * @param null $id
  1817. * @return array
  1818. */
  1819. public static function getGoodsCard($id = null)
  1820. {
  1821. return [];
  1822. if (!$id) {
  1823. return [];
  1824. }
  1825. //商品卡券
  1826. $goods_card_list = GoodsCard::find()->alias('gc')->where(['gc.is_delete' => 0, 'goods_id' => $id])
  1827. ->leftJoin(Card::tableName() . ' c', 'c.id=gc.card_id')->select([
  1828. 'gc.card_id', 'c.name', 'c.pic_url', 'gc.goods_id', 'c.content'
  1829. ])->asArray()->all();
  1830. foreach ($goods_card_list as $index => $value) {
  1831. $goods_card_list[$index]['id'] = $value['card_id'];
  1832. $goods_card_list[$index]['goods_id'] = $id;
  1833. }
  1834. return $goods_card_list;
  1835. }
  1836. /**
  1837. * @param $user User
  1838. * @param $goods_id
  1839. * @return false|null|string
  1840. * 查找指定用户指定商品购买数量
  1841. */
  1842. public static function getBuyNum($user, $goods_id)
  1843. {
  1844. $goodsNum = OrderDetail::find()->alias('od')->innerJoin(['o' => Order::tableName()], 'o.id=od.order_id')
  1845. ->where([
  1846. 'od.goods_id' => $goods_id, 'store_id' => $user->store_id, 'o.user_id' => $user->id,
  1847. 'o.is_delete' => 0, 'o.is_show' => 1
  1848. ])->andWhere(['<>', 'o.trade_status', Order::ORDER_FLOW_CANCEL])->andWhere([
  1849. 'or',
  1850. ['o.is_pay' => 1],
  1851. ['o.pay_type' => Order::PAY_TYPE_COD]
  1852. ])->select('SUM(od.num) num')->scalar();
  1853. return $goodsNum;
  1854. }
  1855. /**
  1856. * @param $saas SaasUser
  1857. * @param $goods_id
  1858. * @return false|null|string
  1859. * 查找指定用户指定商品购买数量
  1860. */
  1861. public static function getSaasBuyNum($saas, $goods_id)
  1862. {
  1863. $goodsNum = OrderDetail::find()->alias('od')->innerJoin(['o' => Order::tableName()], 'o.id=od.order_id')
  1864. ->where([
  1865. 'od.goods_id' => $goods_id,'o.saas_id' => $saas->id,
  1866. 'o.is_delete' => 0, 'o.is_show' => 1
  1867. ])->andWhere(['<>', 'o.trade_status', Order::ORDER_FLOW_CANCEL])->andWhere([
  1868. 'or',
  1869. ['o.is_pay' => 1],
  1870. ['o.pay_type' => Order::PAY_TYPE_COD]
  1871. ])->select('SUM(od.num) num')->scalar();
  1872. return $goodsNum;
  1873. }
  1874. /**
  1875. * 根据规格获取商品的库存及规格价格信息
  1876. * @param array $attr_id_list 规格id列表 eg. [1,4,9]
  1877. * @return array|null eg.
  1878. */
  1879. public function getAttrInfo($attr_id_list)
  1880. {
  1881. sort($attr_id_list);
  1882. // 门店逻辑
  1883. $attr = $this->attr;
  1884. $price = $this->price;
  1885. if (get_md_id()) {
  1886. $md_goods = MdGoods::findOne(['goods_id' => $this->id, 'md_id' => get_md_id()]);
  1887. if ($md_goods) {
  1888. $attr = $md_goods->attr;
  1889. $price = $md_goods->price;
  1890. }
  1891. }
  1892. $attr_rows = json_decode($attr, true);
  1893. if (empty($attr_rows)) {
  1894. return null;
  1895. }
  1896. foreach ($attr_rows as $i => $attr_row) {
  1897. $key = [];
  1898. foreach ($attr_row['attr_list'] as $j => $attr) {
  1899. $key[] = $attr['attr_id'];
  1900. }
  1901. sort($key);
  1902. if (!array_diff($attr_id_list, $key)) {
  1903. if (!$attr_rows[$i]['price']) {
  1904. $attr_rows[$i]['price'] = $price;
  1905. }
  1906. return $attr_rows[$i];
  1907. }
  1908. }
  1909. return null;
  1910. }
  1911. /**
  1912. * 减满
  1913. */
  1914. public static function cutFull($goodsList)
  1915. {
  1916. // 将商品ID相同的商品合并
  1917. $newGoodsList = [];
  1918. foreach ($goodsList as $row) {
  1919. $rowArray = (array) $row;
  1920. if (isset($newGoodsList[$rowArray['goods_id']])) {
  1921. $newGoodsList[$rowArray['goods_id']]['num'] += $rowArray['num'];
  1922. $newGoodsList[$rowArray['goods_id']]['weight'] += $rowArray['weight'] * $rowArray['num'];
  1923. $newGoodsList[$rowArray['goods_id']]['price'] += $rowArray['price'];
  1924. } else {
  1925. $rowArray['weight'] = $rowArray['weight'] * $rowArray['num'];
  1926. $newGoodsList[$rowArray['goods_id']] = $rowArray;
  1927. }
  1928. }
  1929. $resGoodsList = [];
  1930. foreach ($newGoodsList as $val) {
  1931. if ($val['full_cut']) {
  1932. $full_cut = json_decode($val['full_cut'], true);
  1933. } else {
  1934. $full_cut = [
  1935. 'pieces' => 0,
  1936. 'forehead' => 0,
  1937. ];
  1938. }
  1939. if ((empty($full_cut['pieces']) || $val['num'] < ($full_cut['pieces'] ?: 0)) && (empty((float)$full_cut['forehead']) || $val['price'] < ((float)$full_cut['forehead'] ?: 0))) {
  1940. $resGoodsList[] = $val;
  1941. }
  1942. }
  1943. return $resGoodsList;
  1944. }
  1945. public function getGoodsPic($index = 0)
  1946. {
  1947. $list = $this->goodsPicList;
  1948. if (!$list) {
  1949. return null;
  1950. }
  1951. return isset($list[$index]) ? $list[$index] : null;
  1952. }
  1953. public function getCatList1()
  1954. {
  1955. return $this->hasMany(GoodsCat::className(), ['goods_id' => 'id'])->where(['store_id' => get_store_id(), 'is_delete' => 0]);
  1956. }
  1957. public function getCat()
  1958. {
  1959. return $this->hasOne(Cat::className(), ['id' => 'cat_id'])->where(['store_id' => get_store_id()]);
  1960. }
  1961. public static function getBrand($brand_id = 0)
  1962. {
  1963. if ($brand_id < 1) {
  1964. return '';
  1965. }
  1966. $brand = GoodsBrand::findOne(['id' => $brand_id]);
  1967. return $brand->brand_name ?: '';
  1968. }
  1969. // 规格组
  1970. public function getAttrData()
  1971. {
  1972. if ($this->isNewRecord) {
  1973. return [];
  1974. }
  1975. if (!$this->use_attr) {
  1976. return [];
  1977. }
  1978. if (!$this->attr) {
  1979. return [];
  1980. }
  1981. $attr_group_list = [];
  1982. $attr_data = json_decode($this->attr, true);
  1983. foreach ($attr_data as $i => $attr_data_item) {
  1984. foreach ($attr_data[$i]['attr_list'] as $j => $attr_list) {
  1985. $attr_group = $this->getAttrGroupByAttId($attr_data[$i]['attr_list'][$j]['attr_id']);
  1986. if ($attr_group) {
  1987. $in_list = false;
  1988. foreach ($attr_group_list as $k => $exist_attr_group) {
  1989. if ($exist_attr_group['attr_group_name'] == $attr_group->attr_group_name) {
  1990. $attr_item = [
  1991. 'attr_id' => $attr_data[$i]['attr_list'][$j]['attr_id'],
  1992. 'attr_name' => $attr_data[$i]['attr_list'][$j]['attr_name'],
  1993. ];
  1994. if (!in_array($attr_item, $attr_group_list[$k]['attr_list'])) {
  1995. $attr_group_list[$k]['attr_list'][] = $attr_item;
  1996. }
  1997. $in_list = true;
  1998. }
  1999. }
  2000. if (!$in_list) {
  2001. $attr_group_list[] = [
  2002. 'attr_group_name' => $attr_group->attr_group_name,
  2003. 'attr_list' => [
  2004. [
  2005. 'attr_id' => $attr_data[$i]['attr_list'][$j]['attr_id'],
  2006. 'attr_name' => $attr_data[$i]['attr_list'][$j]['attr_name'],
  2007. ],
  2008. ],
  2009. ];
  2010. }
  2011. }
  2012. }
  2013. }
  2014. return $attr_group_list;
  2015. }
  2016. /**
  2017. * Undocumented function
  2018. *
  2019. * @Author LGL 24963@qq.com
  2020. * @DateTime 2021-03-02
  2021. * @desc:
  2022. * @param [type] $att_id
  2023. * @return void
  2024. */
  2025. public function getAttrGroupByAttId($att_id)
  2026. {
  2027. $cache_key = 'get_attr_group_by_attr_id_' . $att_id;
  2028. $attr_group = cache()->get($cache_key);
  2029. if ($attr_group) {
  2030. return $attr_group;
  2031. }
  2032. $attr_group = AttrGroup::find()->alias('ag')
  2033. ->where(['ag.id' => Attr::find()->select('attr_group_id')->distinct()->where(['id' => $att_id])])
  2034. ->limit(1)->one();
  2035. if (!$attr_group) {
  2036. return $attr_group;
  2037. }
  2038. cache()->set($cache_key, $attr_group, 10);
  2039. return $attr_group;
  2040. }
  2041. public function getCheckedAttrData()
  2042. {
  2043. if ($this->isNewRecord) {
  2044. return [];
  2045. }
  2046. if (!$this->use_attr) {
  2047. return [];
  2048. }
  2049. if (!$this->attr) {
  2050. return [];
  2051. }
  2052. $attr_data = json_decode($this->attr, true);
  2053. foreach ($attr_data as $i => $attr_data_item) {
  2054. if (!isset($attr_data[$i]['no'])) {
  2055. $attr_data[$i]['no'] = '';
  2056. }
  2057. if (!isset($attr_data[$i]['pic'])) {
  2058. $attr_data[$i]['pic'] = '';
  2059. }
  2060. foreach ($attr_data[$i]['attr_list'] as $j => $attr_list) {
  2061. $attr_group = $this->getAttrGroupByAttId($attr_data[$i]['attr_list'][$j]['attr_id']);
  2062. $attr_data[$i]['attr_list'][$j]['attr_group_name'] = $attr_group ? $attr_group->attr_group_name : null;
  2063. }
  2064. }
  2065. return $attr_data;
  2066. }
  2067. public function getGoodsCover()
  2068. {
  2069. if ($this->cover_pic) {
  2070. return $this->cover_pic;
  2071. }
  2072. $pic = $this->getGoodsPic(0);
  2073. if ($pic) {
  2074. return $pic->pic_url;
  2075. }
  2076. return null;
  2077. }
  2078. public function getCats()
  2079. {
  2080. return $this->hasMany(Cat::className(), ['id' => 'cat_id'])
  2081. ->via('gc');
  2082. }
  2083. /**
  2084. * @return array
  2085. */
  2086. public static function getDefaultAttr($store_id)
  2087. {
  2088. $default_attr_name = '默认';
  2089. $default_attr_group_name = '规格';
  2090. $attr = Attr::findOne([
  2091. 'attr_name' => $default_attr_name,
  2092. 'is_delete' => 0,
  2093. 'is_default' => 1,
  2094. ]);
  2095. $attr_group = null;
  2096. if (!$attr) {
  2097. $attr_group = AttrGroup::findOne([
  2098. 'attr_group_name' => $default_attr_group_name,
  2099. 'is_delete' => 0,
  2100. ]);
  2101. if (!$attr_group) {
  2102. $attr_group = new AttrGroup();
  2103. $attr_group->store_id = $store_id;
  2104. $attr_group->attr_group_name = $default_attr_group_name;
  2105. $attr_group->is_delete = 0;
  2106. $attr_group->save(false);
  2107. }
  2108. $attr = new Attr();
  2109. $attr->attr_group_id = $attr_group->id;
  2110. $attr->attr_name = $default_attr_name;
  2111. $attr->is_delete = 0;
  2112. $attr->is_default = 1;
  2113. $attr->save(false);
  2114. } else {
  2115. $attr_group = AttrGroup::findOne($attr->attr_group_id);
  2116. }
  2117. return [$attr, $attr_group];
  2118. }
  2119. public static function checkBookGoods() {
  2120. return [];
  2121. $goods_book = Goods::find()->alias('g')->leftJoin(['gb' => GoodsBook::tableName()], 'g.id=gb.goods_id')
  2122. ->where(['g.is_delete' => 0, 'g.status' => 1, 'g.store_id' => get_store_id()])
  2123. ->andWhere(['in', 'g.product_type', [1, 2]])->select('g.product_type, g.id, gb.date_book, gb.service_book')->asArray()->all();
  2124. if (empty($goods_book)) {
  2125. return [];
  2126. }
  2127. $book_goods_ids = [];
  2128. $today = strtotime(date('Y-m-d', time()));
  2129. foreach ($goods_book as $book) {
  2130. // 酒店预约商品
  2131. if ($book['product_type'] == self::GOODS_TYPE_DATE) {
  2132. $date_book = Json::decode($book['date_book']);
  2133. $book_date_arr = array_column($date_book, 'date');
  2134. if (empty($book_date_arr)) {
  2135. $book_goods_ids[] = $book['id'];
  2136. continue;
  2137. }
  2138. $count = 0;
  2139. foreach ($book_date_arr as $value) {
  2140. if (strtotime($value) > $today) {
  2141. $count ++;
  2142. }
  2143. }
  2144. if ($count <= 1) {
  2145. $book_goods_ids[] = $book['id'];
  2146. }
  2147. }
  2148. // 服务预约商品
  2149. if ($book['product_type'] == self::GOODS_TYPE_TIME) {
  2150. $service_book = Json::decode($book['service_book']);
  2151. $service_date_arr = array_column($service_book['data'], 'date');
  2152. if (empty($service_date_arr)) {
  2153. $book_goods_ids[] = $book['id'];
  2154. continue;
  2155. }
  2156. $count = 0;
  2157. foreach ($service_date_arr as $value) {
  2158. if (strtotime($value) > $today) {
  2159. $count ++;
  2160. }
  2161. }
  2162. if ($count <= 1) {
  2163. $book_goods_ids[] = $book['id'];
  2164. }
  2165. }
  2166. }
  2167. return $book_goods_ids;
  2168. }
  2169. /**
  2170. * 添加可选择的抖品商品的商品列表
  2171. */
  2172. public static function getGoodsList($arr, $is_video_goods = false) {
  2173. $query = Goods::find()->alias('g')
  2174. ->where(['g.is_delete' => 0, 'g.md_food_id' => 0, 'g.status' => 1])
  2175. ->andWhere(['not like', 'g.name', '当面付'])->andWhere(['in', 'g.product_type', [0]]);
  2176. if (isset($arr['store_id']) && !empty($arr['store_id'])) {
  2177. $query->andWhere(['g.store_id' => get_store_id()]);
  2178. }
  2179. if (isset($arr['name']) && !empty($arr['name'])) {
  2180. $query->andWhere([
  2181. 'like',
  2182. 'g.name',
  2183. $arr['name']
  2184. ]);
  2185. }
  2186. // if (isset($arr['status']) && $arr['status'] != -1) {
  2187. // $query->andWhere(['g.status' => $arr['status']]);
  2188. // }
  2189. if ($is_video_goods || !empty($arr['goods_id'])) {
  2190. $query->andWhere(['in', 'id', $arr['goods_id']]);
  2191. }
  2192. if (!empty($arr['select'])) {
  2193. $select = $arr['select'];
  2194. } else {
  2195. $select = ['g.id', 'g.name', 'g.status', 'g.service',
  2196. 'g.updated_at', 'g.virtual_sales', 'g.is_verify', 'g.is_negotiable',
  2197. 'g.price', 'g.goods_num', 'g.sort' ,'g.cover_pic', 'g.original_price',
  2198. 'g.quick_purchase', 'g.attr', 'g.mch_id', 'g.detail', 'g.use_attr', 'g.product_type', 'g.is_recommend'];
  2199. }
  2200. $query->select($select)
  2201. ->orderBy(['g.sort' => SORT_ASC, 'g.id' => SORT_DESC]);
  2202. $pagination = pagination_make($query);
  2203. $pagination['data'] = $pagination['list'];
  2204. foreach($pagination['data'] as $k => $v) {
  2205. $goods_cat = GoodsCat::find()->alias('gc')
  2206. ->leftJoin(['c' => Cat::tableName()], 'gc.cat_id=c.id')
  2207. ->where([ 'gc.goods_id' => $v['id'] ])
  2208. ->select(['c.name'])
  2209. ->asArray()
  2210. ->all();
  2211. $pagination['data'][$k]['cat'] = $goods_cat;
  2212. if ($is_video_goods) {
  2213. $video_goods = VideoGoods::findOne(['store_id' => get_store_id(), 'goods_id' => $v['id'], 'is_delete' => 0]);
  2214. $pagination['data'][$k]['video_goods_id'] = $video_goods->id;
  2215. $pagination['data'][$k]['status'] = $video_goods->status;
  2216. }
  2217. }
  2218. unset($pagination['list']);
  2219. return [
  2220. 'code' => 0,
  2221. 'msg' => 'success',
  2222. 'data' => $pagination
  2223. ];
  2224. }
  2225. public static function warn_goods_timeout($store_id){
  2226. $conf = Option::get(OptionSetting::STORE_WARN_GOODS_TIMEOUT, $store_id, 'store', '0')['value'];
  2227. return $conf;
  2228. }
  2229. public static function warn_goods_timeout_save($store_id, $conf){
  2230. $set = Option::set(OptionSetting::STORE_WARN_GOODS_TIMEOUT, $conf, $store_id, 'store');
  2231. return [
  2232. 'code'=>$set ? 0 : 1,
  2233. 'msg'=>$set ? '保存成功' : '保存失败',
  2234. ];
  2235. }
  2236. public static function yifulai($order_id){
  2237. $order = Order::findOne(['id'=>$order_id,'is_delete'=>0]);
  2238. $store = Store::findOne(['id'=>$order->store_id,'is_delete'=>0]);
  2239. if (empty($store)) return [];
  2240. $param['sendUserInfo'] = [
  2241. 'userAddress'=>$order->address,
  2242. 'userName'=>$order->name,
  2243. 'userPhone'=>$order->mobile];
  2244. $province = District::findOne($store->province_id)->name;
  2245. $city = District::findOne($store->city_id)->name;
  2246. $district = District::findOne($store->district_id)->name;
  2247. $param['shopDetail'] = [
  2248. 'accountName'=>$store->name,
  2249. 'address'=>$store->address,
  2250. 'area'=>$city,
  2251. 'city'=>$district,
  2252. 'createTime'=>$store->created_at,
  2253. 'province'=>$province,
  2254. ];
  2255. $order_count = OrderDetail::find()->where(['order_id'=>$order_id])->asArray()->all();
  2256. $param['totalProductAmount'] = 0;
  2257. $param['totalCount'] = 0;
  2258. $param['productList'] = [];
  2259. foreach ($order_count as $item){
  2260. $goods_info = Json::decode($item['goods_info']);
  2261. $goods_info['order_num'] = $item['num'];
  2262. $param['productList'][] = $goods_info;
  2263. $goods_info = Json::decode($item['goods_info']);
  2264. $param['totalProductAmount'] += $goods_info['price']*$item['num'];
  2265. $param['totalCount']+=$item['num'];
  2266. }
  2267. $param['bookingInfo'] = ['startTime'=>(date("Y-m-d H:i:s",$order->pickup_time))];
  2268. $param['totalAmount'] = $order->total_price;
  2269. $param['totalDiscount'] = $order->take_price;
  2270. $param['totalPayAmount'] = $order->pay_price;
  2271. $param['yixiao_sid'] = $order->store_id;
  2272. $param['order_no'] = $order->order_no;
  2273. $url = 'https://vue3git.wdsp666.com/api/callback.yixiao/notify';
  2274. $nonce = base64_encode(random_bytes(16));
  2275. $timestamp = time();
  2276. $sign = self::yifulaiSign($param,$timestamp,$nonce);
  2277. $param['signature'] = $sign;
  2278. $header['X-App-Id'] = GolangOrderForm::$app_id;
  2279. $header['X-Timestamp'] = $timestamp;
  2280. $header['X-Nonce'] = $nonce;
  2281. $res = GolangForm::httpJsonPost($url,Json::encode($param),$header);
  2282. return true;
  2283. }
  2284. public static function yifulaiOrderStatus($order_no){
  2285. $order = Order::findOne(['order_no'=>$order_no,'is_delete'=>0]);
  2286. $store = Store::findOne(['id'=>$order->store_id,'is_delete'=>0]);
  2287. if (empty($store)) return [];
  2288. $param['order_no'] = $order_no;
  2289. $url = 'https://vue3git.wdsp666.com/api/callback.yixiao/orderDetail';
  2290. $nonce = base64_encode(random_bytes(16));
  2291. $timestamp = time();
  2292. $sign = self::yifulaiSign($param,$timestamp,$nonce);
  2293. $param['signature'] = $sign;
  2294. $header['X-App-Id'] = GolangOrderForm::$app_id;
  2295. $header['X-Timestamp'] = $timestamp;
  2296. $header['X-Nonce'] = $nonce;
  2297. $res = GolangForm::httpJsonPost($url,Json::encode($param),$header);
  2298. $data = Json::decode($res);
  2299. if (!$data || $data['code']==0 || !$data['data']['delivery_status_cn']){
  2300. return false;
  2301. }
  2302. return ['data'=>$data['data']['delivery_status_cn']];
  2303. }
  2304. private static function yifulaiSign($params,$timestamp,$nonce){
  2305. ksort($params);
  2306. $paramString = '';
  2307. foreach ($params as $key => $value) {
  2308. if (is_array($value)) continue;
  2309. $paramString .= $key . $value;
  2310. }
  2311. $stringToSign = $paramString . $timestamp . $nonce;
  2312. // 生成HMAC-SHA256签名
  2313. $generatedKey = hash_hmac('sha256', $stringToSign, GolangOrderForm::$app_secret);
  2314. return $generatedKey;
  2315. }
  2316. }