MGoods.Class.php 189 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619
  1. <?php
  2. /**
  3. * 商品管理模块
  4. * Created by PhpStorm.
  5. * User: wxj
  6. * Date: 2019/10/30
  7. * Time: 14:02
  8. */
  9. namespace JinDouYun\Model\Goods;
  10. use Exception;
  11. use JinDouYun\Cache\PageCache;
  12. use JinDouYun\Controller\GoodsManage\GoodsBasic;
  13. use JinDouYun\Dao\Stock\DInventoryWarehouse;
  14. use Mall\Framework\Core\ErrorCode;
  15. use Mall\Framework\Core\StatusCode;
  16. use Mall\Framework\Core\ResultWrapper;
  17. use Mall\Framework\Factory;
  18. use JinDouYun\Cache\PriceCache;
  19. use JinDouYun\Cache\ActivityLimitCache;
  20. use JinDouYun\Cache\SpecCache;
  21. use JinDouYun\Cache\GoodsBasicRelevant;
  22. use JinDouYun\Controller\Common\Logger;
  23. use JinDouYun\Model\Commission\MCommissionSetting;
  24. use JinDouYun\Model\Merchant\MMerchant;
  25. use JinDouYun\Model\Purchase\MPurchase;
  26. use JinDouYun\Model\Purchase\MSupplier;
  27. use JinDouYun\Model\System\MDeliverySetting;
  28. use JinDouYun\Model\GoodsManage\MSku;
  29. use JinDouYun\Model\GoodsManage\MGoodsBasic;
  30. use JinDouYun\Model\GoodsManage\MGoodsBrand;
  31. use JinDouYun\Model\GoodsCategory\MGoodsCategory;
  32. use JinDouYun\Model\Shop\MShop;
  33. use JinDouYun\Model\Stock\MInventory;
  34. use JinDouYun\Model\Price\MPrice;
  35. use JinDouYun\Model\Price\MPriceAdjustment;
  36. use JinDouYun\Model\MBaseModel;
  37. use JinDouYun\Model\Cart\MCart;
  38. use JinDouYun\Model\Customer\MCustomer;
  39. use JinDouYun\Model\Market\MActivity;
  40. use JinDouYun\Model\Customer\MCustomerVisitsLog;
  41. use JinDouYun\Model\Goods\MGoodsSupport;
  42. use JinDouYun\Model\Price\MCustomerTypePriceAdjustment;
  43. use JinDouYun\Dao\Enterprise\DEnterprise;
  44. use JinDouYun\Dao\GoodsManage\DSku;
  45. use JinDouYun\Dao\Shop\DShop;
  46. use JinDouYun\Dao\Goods\DGoods;
  47. use JinDouYun\Dao\GoodsManage\DGoodsBasic;
  48. use Util\Common\ChineseCharacter;
  49. class MGoods extends MBaseModel
  50. {
  51. /**
  52. * @var DGoods
  53. */
  54. private $objDGoods;
  55. /**
  56. * @var MGoodsBasic
  57. */
  58. private $objMGoodsBasic;
  59. /**
  60. * @var MGoodsCategory
  61. */
  62. private $objMGoodsCategory;
  63. /**
  64. * @var MGoodsBrand
  65. */
  66. private $objMGoodsBrand;
  67. /**
  68. * @var MShop
  69. */
  70. private $objMShop;
  71. /**
  72. * @var MInventory
  73. */
  74. private $objMInventory;
  75. /**
  76. * @var MPrice
  77. */
  78. private $objMPrice;
  79. /**
  80. * @var MPriceAdjustment
  81. */
  82. private $objMPriceAdjustment;
  83. /**
  84. * @var MGoodsCollect
  85. */
  86. private $objMGoodsCollect;
  87. /**
  88. * @var MCustomer
  89. */
  90. private $objMCustomer;
  91. /**
  92. * 客户ID
  93. * @var integer
  94. */
  95. private $useCustomerId = null;
  96. /**
  97. * 客户类型
  98. * @var integer
  99. */
  100. private $customerType = null;
  101. /**
  102. * @var int
  103. */
  104. private $cutTable = 1;//商品按照企业id分表
  105. /**
  106. * @var int
  107. */
  108. private $enterpriseId;
  109. /**
  110. * true=>app false=>manage
  111. * @var bool
  112. */
  113. public $isFront;//是否是前台调用此Model
  114. /**
  115. * @var
  116. */
  117. private $userCenterId;
  118. /**
  119. * @var null
  120. */
  121. private $selectUserCenterId = null;//选择的用户id
  122. /**
  123. * @var ActivityLimitCache
  124. */
  125. private $objActivityLimitCache;
  126. /**
  127. * @var SpecCache
  128. */
  129. private $objSpecCache;
  130. /**
  131. * @var MActivity
  132. */
  133. private $objMActivity;
  134. /**
  135. * 公共数据
  136. * @var array
  137. */
  138. private $commonMapping = [];
  139. /**
  140. * @var MSku
  141. */
  142. private $objMSku;
  143. /**
  144. * @var array
  145. */
  146. private $allMappingSpec = [];
  147. /**
  148. * @var DGoodsBasic
  149. */
  150. private $objDGoodsBasic;
  151. /**
  152. * @var MCommissionSetting
  153. */
  154. private $objMCommissionSetting;
  155. /**
  156. * @var DSku
  157. */
  158. private $objDSku;
  159. /**
  160. * @var DShop
  161. */
  162. private $objDShop;
  163. /**
  164. * MGoods constructor.
  165. * @param $enterpriseId
  166. * @param bool $isFront
  167. * @param $userCenterId
  168. * @throws Exception
  169. */
  170. public function __construct($enterpriseId, $isFront = false, $userCenterId)
  171. {
  172. $this->userCenterId = $userCenterId;
  173. $this->enterpriseId = $enterpriseId;
  174. $this->isFront = $isFront;
  175. parent::__construct($enterpriseId, $userCenterId);
  176. $this->objMGoodsBasic = new MGoodsBasic($userCenterId, $enterpriseId);
  177. $this->objMGoodsBrand = new MGoodsBrand($userCenterId, $enterpriseId);
  178. $this->objMGoodsCategory = new MGoodsCategory($userCenterId, $enterpriseId);
  179. $this->objMShop = new MShop($enterpriseId, $userCenterId);
  180. $this->objMPrice = new MPrice($this->userCenterId, $enterpriseId);
  181. $this->objMPriceAdjustment = new MPriceAdjustment($userCenterId, $enterpriseId);
  182. $this->objMGoodsCollect = new MGoodsCollect($enterpriseId, $userCenterId);
  183. $this->objMCustomer = new MCustomer($this->enterpriseId, $this->userCenterId);
  184. $this->objActivityLimitCache = new ActivityLimitCache($this->enterpriseId);
  185. $this->objMActivity = new MActivity($this->userCenterId, $this->enterpriseId);
  186. $this->objMSku = new MSku($this->userCenterId, $this->enterpriseId);
  187. $this->objSpecCache = new SpecCache($this->enterpriseId);
  188. $this->objMInventory = new MInventory($this->enterpriseId,$this->userCenterId);
  189. $this->objMCommissionSetting = new MCommissionSetting($this->enterpriseId,$this->userCenterId);
  190. $this->objDSku = new DSku();
  191. $this->objDShop = new DShop();
  192. $this->objDGoodsBasic = new DGoodsBasic();
  193. $this->objDGoods = new DGoods('default');
  194. $tableName = $this->objDGoods->getTableName('qianniao_goods_', $this->enterpriseId, $this->cutTable);
  195. $this->objDGoods->setTable($tableName);
  196. $this->objDGoods->setSearchIndex('goods_search')->setType('goods');
  197. $this->objDShop->setTable('qianniao_shop_1');
  198. $this->objDSku->setTable('qianniao_sku_'.$this->enterpriseId);
  199. if ($this->isFront && $this->userCenterId) {
  200. self::__initCustomer($this->userCenterId, $this->enterpriseId);
  201. }
  202. }
  203. /**
  204. * @param $userCenterId
  205. * @param $enterpriseId
  206. * @throws Exception
  207. */
  208. public function __initCustomer($userCenterId, $enterpriseId)
  209. {
  210. $objMCustomer = new MCustomer($enterpriseId, $userCenterId);
  211. $dbResult = $objMCustomer->getCustomerInfoByUserCenterId($userCenterId);
  212. if (!$dbResult->isSuccess()) {
  213. return ResultWrapper::fail($dbResult->getData(), $dbResult->getErrorCode());
  214. }
  215. $customer = $dbResult->getData();
  216. $this->customerType = isset($customer['type']) ? $customer['type'] : 0;
  217. $this->useCustomerId = $customer['id'];
  218. }
  219. /**
  220. * Doc: (des="")
  221. * User: XMing
  222. * Date: 2020/8/25
  223. * Time: 4:24 下午
  224. * @param array $rows
  225. * @param $v
  226. * @param $item
  227. * @param int $index
  228. */
  229. private function buildExportFinalData(&$rows, $v, $item, int $index)
  230. {
  231. $rows['id'] = '';
  232. $rows['goodsCode'] = '';
  233. $rows['goodsName'] = '';
  234. $rows['categoryName'] = '';
  235. $rows['brandName'] = '';
  236. $rows['expireTime'] = '';
  237. $rows['inventoryTotalNum'] = '';
  238. $rows['salesTotalNum'] = '';
  239. $rows['enableStatusMsg'] = '';
  240. $rows['shopName'] = '';
  241. $rows['tag'] = '';
  242. $rows['describe'] = '';
  243. $rows['storageCode'] = '';
  244. $rows['createTime'] = '';
  245. if ($index == 0){
  246. $rows['id'] = $v['id'];
  247. $rows['goodsCode'] = $v['goodsCode'];
  248. $rows['goodsName'] = $v['goodsName'];
  249. $rows['categoryName'] = $v['categoryName'];
  250. $rows['brandName'] = empty($v['brandName']) ? $v['brandName'] : '无';
  251. $rows['expireTime'] = $v['expireTime'];
  252. $rows['inventoryTotalNum'] = !empty($v['inventoryNum']) ?$v['inventoryNum']:0;
  253. $rows['salesTotalNum'] = $v['salesTotalNum'];
  254. $rows['enableStatusMsg'] = $v['enableStatusMsg'];
  255. $rows['shopName'] = $v['shopName'];
  256. $rows['tag'] = empty($v['tag']) ? $v['tag'] : '无';
  257. $rows['describe'] = $v['describe'];;
  258. $rows['storageCode'] = empty($v['storage']) ? $v['storage'] : '无';
  259. $rows['createTime'] = $v['createTime'];
  260. }
  261. $rows['unitName'] = $item['unitName'];
  262. $rows['specGroup'] = $item['specGroup'];
  263. $rows['marketPrice'] = $item['marketPrice'];
  264. $rows['salePrice'] = $item['salePrice'];
  265. $rows['ladderPriceMsg'] = $item['ladderPriceMsg'];
  266. $rows['barCode'] = $item['barCode'];
  267. $rows['skuMsg'] = $item['skuMsg'];
  268. $rows['conversion'] = $item['conversion'];
  269. }
  270. /**
  271. * Doc: (des="")
  272. * User: XMing
  273. * Date: 2020/8/25
  274. * Time: 4:17 下午
  275. * @param array $result
  276. *
  277. */
  278. private function exportCsv( $result)
  279. {
  280. // //导出到本地
  281. header("Content-type:application/vnd.ms-excel");
  282. header("Content-Disposition:filename=GoodsBasicCsv.csv");
  283. header('Cache-Control: max-age=0');
  284. $fp = fopen('php://output', 'a');
  285. $head = ['ID', '商品编码', '商品名称', '商品分类', '商品品牌', '保质期', '总库存','总销量','当前状态', '所属店铺', '关键词','商品卖点', '货架编码','发布日期',
  286. '计量单位', '规格', '市场价格(元)','销售价格(元)' , '阶梯价格', '商品条码' , '主单位(主/辅)', '主辅换算比率',];//定义标题
  287. foreach ($head as $i => $v) {
  288. $head[$i] = mb_convert_encoding($v, 'GBK', 'utf-8'); //将中文标题转换编码,否则乱码
  289. }
  290. fputcsv($fp, $head);
  291. $limit = 10000;
  292. $num = 0;//计数器
  293. $rows = [];
  294. foreach ($result as $v) {
  295. $num++;
  296. if($num == $limit){
  297. ob_flush(); //释放内存
  298. flush();
  299. }
  300. foreach ($v['specMultiple'] as $index => $item){
  301. self::buildExportFinalData($rows,$v,$item,$index);
  302. foreach ($rows as $kk => $vv) {
  303. $rs[$kk] = mb_convert_encoding($vv, 'GBK', 'utf-8'); //转译编码
  304. }
  305. fputcsv($fp, $rs);
  306. $rows = [];
  307. }
  308. }
  309. exit;
  310. }
  311. /**
  312. * 商品列表
  313. * @param array $result
  314. */
  315. private function exportGoodsList($result)
  316. {
  317. //导出到本地
  318. header("Content-type:application/vnd.ms-excel");
  319. header("Content-Disposition:filename=商品列表.csv");
  320. header('Cache-Control: max-age=0');
  321. $fp = fopen('php://output', 'a');
  322. $head = ['ID', '商品编码', '商品名称', '商品分类', '商品品牌', '总库存','总销量','当前状态', '所属店铺', '关键词', '发布日期',
  323. '计量单位', '规格' , '商品条码'];//定义标题
  324. foreach ($head as $i => $v) {
  325. $head[$i] = mb_convert_encoding($v, 'GBK', 'utf-8'); //将中文标题转换编码,否则乱码
  326. }
  327. fputcsv($fp, $head);
  328. $limit = 10000;
  329. $num = 0; //计数器
  330. foreach ($result as $v) { //循环数据
  331. $num++;
  332. if ($num == $limit) {
  333. ob_flush(); //释放内存
  334. flush();
  335. }
  336. $rows['id'] = isset($v['id']) ? $v['id'] : ''; //ID
  337. $rows['code'] = isset($v['code']) ? $v['code'] : ''; //商品编码
  338. $rows['title'] = isset($v['title']) ? $v['title'] : ''; //商品名称
  339. $rows['categoryName'] = isset($v['categoryName']) ? $v['categoryName'] : '无'; //商品分类
  340. $rows['brandName'] = isset($v['brandName']) ? $v['brandName'] : '无'; //商品品牌
  341. // $rows['materielName'] = isset($v['materielName']) ? $v['materielName'] : ''; //保质期
  342. $rows['inventorTotal'] = isset($v['inventorTotal']) ? $v['inventorTotal'] : 0; //总库存
  343. $rows['realSalesNum'] = isset($v['realSalesNum']) ? $v['realSalesNum'] : 0; //总销量
  344. $rows['enableStatus'] = isset($v['enableStatus']) && $v['enableStatus']== 5 ? '上架' : '下架'; //当前状态
  345. $rows['shopName'] = isset($v['shopName']) ? $v['shopName'] : ''; //所属店铺
  346. $rows['tag'] = empty($v['tag']) ? $v['tag'] : '无';; //关键词
  347. // $rows['materielName'] = isset($v['materielName']) ? $v['materielName'] : ''; //商品卖点
  348. // $rows['materielName'] = isset($v['materielName']) ? $v['materielName'] : ''; //货架编码
  349. $rows['createTime'] = date("Y-m-d H:i:s",$v['createTime']) ; //发布日期
  350. $rows['unitNameMaster'] = isset($v['unitNameMaster']) ? $v['unitNameMaster'] : ''; //计量单位
  351. $rows['specGroup'] = empty($v['specGroup']) ? '无' : self::createSpecName(json_decode($v['specGroup'],true));; //规格
  352. // $rows['materielName'] = isset($v['materielName']) ? $v['materielName'] : ''; //市场价格(元)
  353. // $rows['materielName'] = isset($v['materielName']) ? $v['materielName'] : ''; //销售价格(元)
  354. // $rows['materielName'] = isset($v['materielName']) ? $v['materielName'] : ''; //阶梯价格
  355. $rows['barCode'] = isset($v['barCode']) ? $v['barCode'] : '无'; //商品条码
  356. // $rows['materielName'] = isset($v['materielName']) ? $v['materielName'] : ''; //主单位(主/辅)
  357. // $rows['materielName'] = isset($v['materielName']) ? $v['materielName'] : ''; //主辅换算比率
  358. foreach ($rows as $kk => $vv) {
  359. $rs[$kk] = mb_convert_encoding($vv, 'GBK', 'utf-8'); //转译编码
  360. }
  361. fputcsv($fp, $rs);
  362. $rows = [];
  363. }
  364. }
  365. /**
  366. * 兼容原来的数据
  367. * 将sku转化为spec格式
  368. * @param $data
  369. * @return mixed
  370. */
  371. public function formatSkuTransSpecMapping($data)
  372. {
  373. if (empty($data)) {
  374. return $data;
  375. }
  376. foreach ($data as &$details) {
  377. if (!isset($details['specType'])) {
  378. $details['specType'] = StatusCode::$specType['single'];//之前es中没有
  379. }
  380. if ($details['specType'] == StatusCode::$specType['multiple']) {
  381. continue;
  382. }
  383. $skuData = $details['skuData'];
  384. if (empty($skuData)) {
  385. continue;
  386. }
  387. $params = [];
  388. $specMultiple = [];
  389. foreach ($skuData as $row) {
  390. $params[] = [
  391. 'name' => $row['unitName'],
  392. 'specValueId' => $this->objSpecCache->getIdBySpecName($row['unitName']),
  393. ];
  394. //规格明细
  395. $specMultiple[] = [
  396. 'id' => $row['skuId'],//skuid
  397. 'isMaster' => $row['isMaster'],
  398. 'conversion' => $row['conversion'],
  399. 'specGroup' => [
  400. [
  401. 'specId' => $this->objSpecCache->getIdBySpecName('计量单位'),
  402. 'specIdName' => '计量单位',
  403. 'specValueId' => $this->objSpecCache->getIdBySpecName($row['unitName']),
  404. 'specValueIdName' => $row['unitName'],
  405. ]
  406. ],
  407. 'specImage' => $details['images'],
  408. ];
  409. }
  410. //规格组
  411. $specGroup = [
  412. [
  413. 'groupSpec' => [
  414. 'name' => '计量单位',
  415. 'specId' => $this->objSpecCache->getIdBySpecName('计量单位'),
  416. ],
  417. 'params' => $params
  418. ]
  419. ];
  420. $details['specGroup'] = $specGroup;
  421. $details['specMultiple'] = $specMultiple;
  422. }
  423. return $data;
  424. }
  425. /**
  426. * Doc: (des="商品数据es写入")
  427. * User: XMing
  428. * Date: 2020/7/20
  429. * Time: 9:59 上午
  430. * @param int $goodsId
  431. * @return bool
  432. */
  433. public function updateEsData_1(int $goodsId)
  434. {
  435. $fields = 'g.*,
  436. s.warehouseId,
  437. b.title,b.categoryId,b.categoryPath,b.brandId,b.images,b.describe,b.tag,b.expireTime,b.noSalesShop,b.description';
  438. $sql = 'select '.$fields.' from qianniao_goods_'.$this->enterpriseId.' as g
  439. left join qianniao_goods_basic_'.$this->enterpriseId.' as b
  440. on b.id = g.basicGoodsId
  441. left join qianniao_shop_1 as s
  442. on g.shopId = s.id
  443. where g.id = '.$goodsId;
  444. $goods = $this->objDGoods->query($sql);
  445. if ($goods === false){
  446. return false;
  447. }
  448. $goods = isset($goods[0]) ? $goods[0] : [];
  449. if (empty($goods)){
  450. return false;
  451. }
  452. isset($goods['images']) && is_string($goods['images']) && $goods['images'] = json_decode($goods['images'],true);
  453. isset($goods['salesNum']) && is_string($goods['salesNum']) && $goods['salesNum'] = json_decode($goods['salesNum'],true);
  454. $goods['code'] = createCode(StatusCode::$code['goodsBasic']['prefix'], $goods['basicGoodsId'], StatusCode::$code['goodsBasic']['length']);//当前状态
  455. $_id = self::createEsDocumentId($goodsId);
  456. $result = $this->objDGoods->addUpSearchIndexDocument($goods,$_id);
  457. if (isset($result['_shards']) && isset($result['_shards']['successful']) && $result['_shards']['successful'] == 1) {
  458. return true;
  459. }
  460. return false;
  461. }
  462. /**
  463. * todo(这个方法需要更新掉)
  464. * @param $goodsId
  465. * @throws Exception
  466. */
  467. public function updateEsData($goodsId)
  468. {
  469. //每次查询一遍,避免商品在数据库存在,在ES中不存在的情况
  470. $goods = $this->objDGoods->get_by('id', $goodsId);
  471. if ($goods !== false) {
  472. //查出商铺对应的仓库id
  473. $shop = $this->objMShop->getShopName([$goods['shopId']]);
  474. $shop = !empty($shop) ? array_shift($shop) : [];
  475. $goods['warehouseId'] = isset($shop['warehouseId']) ? $shop['warehouseId'] : 0;
  476. $goods['enterpriseId'] = $this->enterpriseId;
  477. //$goods['specification'] = json_decode($goods['specification'], true);
  478. $goods['salesNum'] = empty($goods['salesNum']) ? $goods['salesNum'] : json_decode($goods['salesNum'], true);
  479. $_id = self::createEsDocumentId($goodsId);
  480. $result = $this->objDGoods->addUpSearchIndexDocument($goods, $_id);
  481. if (isset($result['_shards']) && isset($result['_shards']['successful']) && $result['_shards']['successful'] == 1) {
  482. //echo "es操作成功";die;
  483. } else {
  484. file_put_contents('/www/wwwroot/logs/api.junhailan.com/elasticsearch.log', date('Y-m-d H:i:s') . '操作商品es错误,错误原因' . var_export($result, true) . PHP_EOL, FILE_APPEND);
  485. }
  486. }
  487. }
  488. /**
  489. * Doc: (des="生成文档id")
  490. * User: XMing
  491. * Date: 2020/7/20
  492. * Time: 10:49 上午
  493. * @param $goodsId
  494. * @return string
  495. */
  496. private function createEsDocumentId($goodsId)
  497. {
  498. return 'EnterpriseId_' . $this->enterpriseId . '_GoodsId_' . $goodsId;
  499. }
  500. /**
  501. * 添加商品
  502. * @param $params
  503. * @return ResultWrapper
  504. * @throws Exception
  505. */
  506. public function addGoods($params)
  507. {
  508. $existResult = $this->existGoodsInShop($params['shopId'], $params['basicGoodsId']);
  509. if ($existResult->isSuccess() == false) {
  510. return ResultWrapper::fail($existResult->getData(), $existResult->getErrorCode());
  511. }
  512. $basic = $this->objMGoodsBasic->getBasicById($params['basicGoodsId']);
  513. if (empty($basic)){
  514. return ResultWrapper::fail('未获取到物料信息',ErrorCode::$paramError);
  515. }
  516. $params['merchantId'] = $basic['merchantId'];
  517. $params['supplierId'] = getArrayItem($basic,'supplierId',0);
  518. $priceData = $params['priceData'];
  519. unset($params['priceData']);
  520. $priceAdjustmentData['createUserName'] = $params['createUserName'];
  521. unset($params['createUserName']);
  522. $priceAdjustmentData['shopName'] = $params['shopName'];
  523. unset($params['shopName']);
  524. $priceAdjustmentData['goodsName'] = $params['goodsName'];
  525. unset($params['goodsName']);
  526. $commissionResult = $this->objMCommissionSetting->getSetting('goods');
  527. if (!$commissionResult->isSuccess()){
  528. return ResultWrapper::fail($commissionResult->getData(),$commissionResult->getErrorCode());
  529. }
  530. $params['isJoinCommission'] = !empty($commissionResult->getData()) ? $commissionResult->getData() : StatusCode::$delete;
  531. $this->objDGoods->beginTransaction();
  532. $params['staffId'] = $this->userCenterId;
  533. $goodsId = $this->objDGoods->insert($params);
  534. if ($goodsId === false) {
  535. $this->objDGoods->rollBack();
  536. return ResultWrapper::fail($this->objDGoods->error(), ErrorCode::$dberror);
  537. }
  538. //self::updateEsData($goodsId);
  539. if (empty($priceData)) {
  540. $this->objDGoods->commit();
  541. return ResultWrapper::success($goodsId);
  542. }
  543. // 同步生成商品价格调价单
  544. $priceAdjustmentData = self::savePriceData($goodsId, $params, $priceData, $priceAdjustmentData, 'add');
  545. $effectiveParams = [
  546. 'createTime' => time(),
  547. 'effectiveUserName' => $priceAdjustmentData['createUserName'],
  548. 'effectiveUserId' => $this->userCenterId,
  549. ];
  550. $result = $this->objMPriceAdjustment->addAndEffective([$priceAdjustmentData], $effectiveParams);
  551. if (!$result->isSuccess()) {
  552. $this->objDGoods->rollBack();
  553. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  554. }
  555. $this->objDGoods->commit();
  556. // 每发布一件商品需要清除一下小程序商品首页的缓存;
  557. $objPageCache = new PageCache();
  558. $objPageCache->delPage();
  559. return ResultWrapper::success($goodsId);
  560. }
  561. public function existGoodsInShop($shopId, $basicGoodsId)
  562. {
  563. $where = [
  564. 'shopId' => $shopId,
  565. 'basicGoodsId' => $basicGoodsId,
  566. 'deleteStatus' => StatusCode::$standard
  567. ];
  568. $result = $this->objDGoods->select($where);
  569. if ($result === false) {
  570. return ResultWrapper::fail($this->objDGoods->error(), ErrorCode::$dberror);
  571. }
  572. if (!empty($result)) {
  573. return ResultWrapper::fail('商铺下已存在相同商品', ErrorCode::$dberror);
  574. }
  575. return ResultWrapper::success('未添加此商品');
  576. }
  577. /**
  578. * 生效商品价格
  579. */
  580. public function savePriceData($goodsId, $params, $priceData, $priceAdjustmentData, $action)
  581. {
  582. $priceAdjustmentCommonData = [
  583. 'createUserId' => $this->userCenterId,
  584. 'goodsId' => $goodsId,
  585. 'saleType' => 1,
  586. 'salePriceType' => 1,
  587. 'salePriceAreaType' => 1,
  588. 'shopId' => $params['shopId'],
  589. 'createTime' => time(),
  590. 'updatetime' => time(),
  591. ];
  592. foreach ($priceData as $key => $value) {
  593. $savePriceData = [
  594. 'salePrice' => isset($value['salePrice']) ? floatval($value['salePrice']) : 0,
  595. 'memberPrice' => isset($value['memberPrice']) ? floatval($value['memberPrice']) : 0,
  596. 'marketPrice' => isset($value['marketPrice']) ? $value['marketPrice'] : 0,
  597. 'ladderPrice' => $value['ladderPrice'],
  598. 'unitName' => isset($value['unitName']) ? $value['unitName'] : '',
  599. 'unitId' => isset($value['unitId']) ? $value['unitId'] : 0,
  600. 'enabledLadder' => $value['enabledLadder'],
  601. 'setNum' => isset($value['setNum']) ? $value['setNum'] : 0,//起订数量
  602. 'isSale' => getArrayItem($value, 'isSale', StatusCode::$standard),
  603. 'minUnitPriceConvert' => getArrayItem($value, 'minUnitPriceConvert', 0),
  604. ];
  605. $priceAdjustmentData['salePrice'][$value['id']] = $savePriceData;
  606. }
  607. $priceAdjustmentData['salePrice'] = json_encode($priceAdjustmentData['salePrice']);
  608. $priceAdjustmentData = array_merge($priceAdjustmentData, $priceAdjustmentCommonData);
  609. return $priceAdjustmentData;
  610. }
  611. /**
  612. * 编辑商品
  613. * @param $params
  614. * @return ResultWrapper
  615. * @throws Exception
  616. */
  617. public function editGoods($params)
  618. {
  619. if (empty($params['id'])) {
  620. return ResultWrapper::fail('没有指定要修改的商品id', ErrorCode::$paramError);
  621. }
  622. $updateGoodsId = $params['id'];
  623. unset($params['id']);
  624. $priceData = $params['priceData'];
  625. unset($params['priceData']);
  626. $priceAdjustmentData['createUserName'] = $params['createUserName'];
  627. unset($params['createUserName']);
  628. $priceAdjustmentData['shopName'] = $params['shopName'];
  629. unset($params['shopName']);
  630. $priceAdjustmentData['goodsName'] = $params['goodsName'];
  631. unset($params['goodsName']);
  632. $beginTransactionStatus = $this->objDGoods->beginTransaction();
  633. $dbResult = $this->objDGoods->update($params, $updateGoodsId);
  634. if ($dbResult === false) {
  635. $this->objDGoods->rollBack();
  636. return ResultWrapper::fail($this->objDGoods->error(), ErrorCode::$dberror);
  637. }
  638. // self::updateEsData($updateGoodsId);
  639. if (empty($priceData)) {
  640. return ResultWrapper::success($updateGoodsId);
  641. }
  642. // 同步生成商品价格调价单
  643. $priceAdjustmentData = self::savePriceData($updateGoodsId, $params, $priceData, $priceAdjustmentData, 'edit');
  644. $effectiveParams = [
  645. 'createTime' => time(),
  646. 'effectiveUserName' => $priceAdjustmentData['createUserName'],
  647. 'effectiveUserId' => $this->userCenterId,
  648. ];
  649. $result = $this->objMPriceAdjustment->addAndEffective([$priceAdjustmentData], $effectiveParams);
  650. if (!$result->isSuccess()) {
  651. $this->objDGoods->rollBack();
  652. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  653. }
  654. if($beginTransactionStatus){
  655. $this->objDGoods->commit();
  656. }
  657. return ResultWrapper::success($dbResult);
  658. }
  659. /**
  660. * 修改商品的销量
  661. * @param $goodsId
  662. * @param $skuId
  663. * @param $num 下单 加销量传正数,退货减销量传负数
  664. * @return bool
  665. */
  666. public function updateSalesNum($goodsId, $skuId, $num)
  667. {
  668. $goodsInfo = $this->objDGoods->get($goodsId);
  669. $salesNumInfo = empty($goodsInfo['salesNum']) ? [] : json_decode($goodsInfo['salesNum'], true);
  670. $exist = false;
  671. $realSalesNum = $goodsInfo['realSalesNum'];
  672. foreach ($salesNumInfo as &$salesNum) {
  673. if (isset($salesNum['skuId']) && $salesNum['skuId'] == $skuId) {
  674. $salesNum['salesNum'] = $salesNum['salesNum'] + $num;
  675. $exist = true;
  676. }
  677. }
  678. // if(!empty($salesNumInfo)){
  679. // $skuIds = array_column($salesNumInfo,'skuId');
  680. // var_dump(array_search($skuId, $skuIds));
  681. // var_dump($skuIds);
  682. // var_dump($skuId);
  683. // if(array_search($skuId, $skuIds) === false){
  684. // var_dump(111);
  685. // array_push($salesNumInfo, ['skuId' => $skuId, 'salesNum' => $num]);
  686. // }
  687. // }
  688. if (!$exist) {
  689. array_push($salesNumInfo, ['skuId' => $skuId, 'salesNum' => $num]);
  690. }
  691. //循环销量数据,更新真实销量
  692. $objDSku = new DSku();
  693. $objDSku->setTable('qianniao_sku_'.$this->enterpriseId);
  694. //查询sku表
  695. $skuResult = $objDSku->get(['id'=>$skuId]);
  696. if ($skuResult === false) {
  697. return false;
  698. }
  699. if(empty($skuResult)){
  700. return false;
  701. }
  702. if($skuResult['isMaster'] == StatusCode::$standard){//主单位直接累加
  703. $realSalesNum = $realSalesNum + $num;
  704. }
  705. if($skuResult['isMaster'] == StatusCode::$delete){ //副单位换算
  706. $realSalesNum = $realSalesNum + bcmul($num,$skuResult['conversion']);
  707. }
  708. $result = $this->objDGoods->update(['salesNum' => json_encode($salesNumInfo),'realSalesNum'=>$realSalesNum], $goodsId);
  709. if ($result) {
  710. //更新ES
  711. //$_id = self::createEsDocumentId($goodsId);
  712. //$this->objDGoods->esupdateTypeFieldVaule(['salesNum' => $salesNumInfo], $_id);
  713. return true;
  714. }
  715. return false;
  716. }
  717. /**
  718. * Doc: (des="审核商品")
  719. * User: XMing
  720. * Date: 2020/12/7
  721. * Time: 11:32 上午
  722. * @param int $id
  723. * @return ResultWrapper
  724. */
  725. public function audit(int $id): ResultWrapper
  726. {
  727. $dbResult = $this->objDGoods->update(['auditStatus' => StatusCode::$auditStatus['auditPass'] ,'updateTime' => time()], $id);
  728. if ($dbResult === false) {
  729. return ResultWrapper::fail($this->objDGoods->error(), ErrorCode::$dberror);
  730. }
  731. /*
  732. //更新ES
  733. $_id = self::createEsDocumentId($id);
  734. $this->objDGoods->esupdateTypeFieldVaule(['auditStatus' => StatusCode::$auditStatus['auditPass'],'updateTime' => time()], $_id); */
  735. return ResultWrapper::success(true);
  736. }
  737. /**
  738. * 删除商品 只支持单个
  739. * @param $delGoodsIds
  740. * @return ResultWrapper
  741. * @throws Exception
  742. */
  743. public function delGoods($delGoodsIds)
  744. {
  745. if (empty($delGoodsIds)) {
  746. return ResultWrapper::fail('参数错误', ErrorCode::$paramError);
  747. }
  748. $this->objDGoods->beginTransaction();
  749. $dbResult = $this->objDGoods->update(['deleteStatus' => StatusCode::$delete], $delGoodsIds);
  750. if ($dbResult === false) {
  751. $this->objDGoods->rollBack();
  752. return ResultWrapper::fail($this->objDGoods->error(), ErrorCode::$dberror);
  753. } else {
  754. $result = self::delGoodsBasic($delGoodsIds);
  755. if (!$result->isSuccess()){
  756. $this->objDGoods->rollBack();
  757. return ResultWrapper::fail($result->getData(),$result->getErrorCode());
  758. }
  759. $this->objDGoods->commit();
  760. /*
  761. //在ES删除
  762. $_id = self::createEsDocumentId($delGoodsIds);
  763. $this->objDGoods->esdeleteTypeDocument($_id); */
  764. return ResultWrapper::success($dbResult);
  765. }
  766. }
  767. /**
  768. * 单店铺删除基础商品资料
  769. * @param int $id
  770. * @return ResultWrapper
  771. * @throws Exception
  772. */
  773. public function delGoodsBasic(int $id)
  774. {
  775. $objDEnterprise = new DEnterprise();
  776. $objDEnterprise->setTable('qianniao_enterprise_1');
  777. $field = $objDEnterprise->get_field('scope',['id'=>$this->enterpriseId]);
  778. if ($field === false){
  779. Logger::logs(E_USER_ERROR,'查询错误',__CLASS__,__LINE__,$objDEnterprise->error());
  780. return ResultWrapper::fail($objDEnterprise->error(),ErrorCode::$dberror);
  781. }
  782. if ($field == StatusCode::$standard){
  783. return ResultWrapper::success($id);
  784. }
  785. $basicGoodsId = $this->objDGoods->get_field('basicGoodsId',['id'=>$id]);
  786. if ($field === false){
  787. Logger::logs(E_USER_ERROR,'查询错误',__CLASS__,__LINE__,$this->objDGoods->error());
  788. return ResultWrapper::fail($this->objDGoods->error(),ErrorCode::$dberror);
  789. }
  790. $result = $this->objMGoodsBasic->delGoodsBasic($basicGoodsId);
  791. if (!$result->isSuccess()){
  792. return ResultWrapper::fail($result->getData(),$result->getErrorCode());
  793. }
  794. return ResultWrapper::success('操作成功');
  795. }
  796. /**
  797. * Doc: (des="")
  798. * User: XMing
  799. * Date: 2020/12/24
  800. * Time: 2:56 下午
  801. * @param $selectParams
  802. * @return ResultWrapper
  803. * @throws Exception
  804. */
  805. private function getCustomerIdAndCustomerTypeByUserCenterId($selectParams): ResultWrapper
  806. {
  807. $customer = [];
  808. if (isset($selectParams['userCenterId']) && !empty($selectParams['userCenterId'])){
  809. $objMCustomer = new MCustomer($this->enterpriseId,$this->userCenterId);
  810. $customerResult = $objMCustomer->getCustomerDataByUserCenterIdAndEnterpriseId($selectParams['userCenterId']);
  811. if (!$customerResult->isSuccess()){
  812. return ResultWrapper::fail($customerResult->getData(),$customerResult->getErrorCode());
  813. }
  814. $customer = $customerResult->getData();
  815. $this->useCustomerId = getArrayItem($customer,'id',0);
  816. $this->customerType = getArrayItem($customer,'type',0);
  817. }
  818. return ResultWrapper::success($customer);
  819. }
  820. /**
  821. * 获取指定商品信息
  822. * @param $goodsId
  823. * @param int $userCenterId
  824. * @return ResultWrapper
  825. * @throws Exception
  826. */
  827. public function getGoodsInfo($goodsId,$userCenterId = null)
  828. {
  829. // 获取商品表数据
  830. $dbResult = $this->objDGoods->get_by('id', $goodsId);
  831. if ($dbResult === false) {
  832. return ResultWrapper::fail($this->objDGoods->error(), ErrorCode::$dberror);
  833. }
  834. if( empty($dbResult) ){
  835. return ResultWrapper::fail('查询的商品数据为空', ErrorCode::$contentNotExists);
  836. }
  837. // 处理客户列表数据
  838. $formatGoodsResult = self::formatManageGoodsLists([$dbResult]);
  839. if (!$formatGoodsResult->isSuccess()){
  840. return ResultWrapper::fail($formatGoodsResult->getData(),$formatGoodsResult->getErrorCode());
  841. }
  842. $formatGoods = $formatGoodsResult->getData();
  843. $goodsData = array_shift($formatGoods);
  844. // 处理商品详情数据
  845. $formatDetailsResult = self::formatDetails($goodsData);
  846. if (!$formatDetailsResult->isSuccess()){
  847. return ResultWrapper::fail($formatDetailsResult->getData(),$formatDetailsResult->getErrorCode());
  848. }
  849. $goodsDetails = $formatDetailsResult->getData();
  850. // 格式化经营屏蔽数据
  851. $customerNotResult = self::formatNotCustomer($goodsDetails);
  852. if (!$customerNotResult->isSuccess()){
  853. return ResultWrapper::fail($customerNotResult->getData(),$customerNotResult->getErrorCode());
  854. }
  855. $dbResult = $customerNotResult->getData();
  856. return ResultWrapper::success($dbResult);
  857. }
  858. /**
  859. * Doc: (des="获取所有商品数据")
  860. * User: XMing
  861. * Date: 2020/11/30
  862. * Time: 10:29 上午
  863. * @param array $selectParams 查询条件
  864. * @param string $fields 查询字段
  865. * @param string $order 排序方式
  866. * @param bool $export 是否为导出 true=>导出
  867. * @return ResultWrapper
  868. * @throws Exception
  869. */
  870. public function getAllGoods(array $selectParams,string $fields = '*',string $order = 'topTime DESC, sort desc, createTime desc',bool $export = false): ResultWrapper
  871. {
  872. $limit = $selectParams['limit'];
  873. unset($selectParams['limit']);
  874. $offset = $selectParams['offset'];
  875. unset($selectParams['offset']);
  876. $returnData = [
  877. 'data' => [],
  878. 'total' => 0,
  879. ];
  880. $where = "deleteStatus =" . StatusCode::$standard;
  881. if (isset($selectParams['enableStatus'])) {
  882. $where .= " AND enableStatus = " . $selectParams['enableStatus'];
  883. }
  884. if (isset($selectParams['shopId'])) {
  885. if (is_array($selectParams['shopId'])) {
  886. $where .= " AND shopId in (" . implode(',', $selectParams['shopId']) . ')';
  887. } else {
  888. $where .= " AND shopId = " . $selectParams['shopId'];
  889. }
  890. }
  891. if (isset($selectParams['isOption']) && !empty($selectParams['isOption'])){
  892. $where .= " AND isOption = " . $selectParams['isOption'];
  893. }
  894. if (isset($selectParams['supplierId']) && !empty($selectParams['supplierId'])){
  895. $where .= ' AND supplierId = '.$selectParams['supplierId'];
  896. }
  897. if (isset($selectParams['auditStatus']) && !empty($selectParams['auditStatus'])){
  898. $where .= " AND auditStatus = " . $selectParams['auditStatus'];
  899. }
  900. if (isset($selectParams['inStock'])) {
  901. // 获取有无库存的基本资料id
  902. $goodsBasicIdResult = self::getInOrOutOfStockGoodsBasicId($selectParams['inStock']);
  903. if (!$goodsBasicIdResult->isSuccess()) {
  904. return ResultWrapper::fail($goodsBasicIdResult->getData(), $goodsBasicIdResult->getErrorCode());
  905. }
  906. $goodsBasicIds = $goodsBasicIdResult->getData();
  907. if (!empty($goodsBasicIds)) {
  908. $where .= " AND (";
  909. foreach ($goodsBasicIds as $key => $goodsBasicId) {
  910. $condition = $key ? " OR " : "";
  911. $where .= $condition . '(basicGoodsId = ' . $goodsBasicId['basicGoodsId'] . ' and shopId=' . $goodsBasicId['shopId'] . ')';
  912. }
  913. $where .= ")";
  914. }
  915. }
  916. if( isset($selectParams['merchantId']) ){
  917. $where .= ' AND merchantId = '.$selectParams['merchantId'];
  918. } else {
  919. if (isset($selectParams['ifMerchant']) ){
  920. if($selectParams['ifMerchant'] == StatusCode::$delete){
  921. $where .= ' AND merchantId = 0';
  922. } else {
  923. $where .= ' AND merchantId <> 0';
  924. }
  925. }
  926. }
  927. // 查询总条数
  928. $total = $this->objDGoods->count($where);
  929. if ($total === false) {
  930. return ResultWrapper::fail($this->objDGoods->error(), ErrorCode::$dberror);
  931. }
  932. if(empty($total)){
  933. return ResultWrapper::success($returnData);
  934. }
  935. //数据域
  936. //$selectParams = parent::getStaffQueryParams($where, 'staffId');//商品不应该有数据域
  937. //$selectParams = parent::getShopIdQueryParams($selectParams);//店铺数据域
  938. // 导出
  939. if ($export === true){
  940. self::exportGoods($where,'db');
  941. exit;
  942. }
  943. $dbResult = $this->objDGoods->select($where, $fields, $order, $limit, $offset);
  944. if ($dbResult === false) {
  945. return ResultWrapper::fail($this->objDGoods->error(), ErrorCode::$dberror);
  946. }
  947. // 格式化商品数据
  948. $formatResult = self::formatManageGoodsLists($dbResult);
  949. if (!$formatResult->isSuccess()){
  950. return ResultWrapper::fail($formatResult->getData(),$formatResult->getErrorCode());
  951. }
  952. $return = [
  953. 'data' => $formatResult->getData(),
  954. 'total' =>($total) ? intval($total) : 0,
  955. ];
  956. return ResultWrapper::success($return);
  957. }
  958. /**
  959. * 商品列表页
  960. */
  961. public function getAllGoodsList($selectParams,$export=0)
  962. {
  963. $goodsTab = 'qianniao_goods_'.$this->enterpriseId;
  964. $goodsBasicTab = 'qianniao_goods_basic_'.$this->enterpriseId;
  965. $inventoryWarehouse = 'qianniao_inventory_warehouse_'.$this->enterpriseId;
  966. $shopTab = 'qianniao_shop_1';
  967. // 初始化表
  968. $this->objDGoods->setTable($goodsTab);
  969. $objDGoodsBasic = new DGoodsBasic('default');
  970. $objDGoodsBasic->setTable($goodsBasicTab);
  971. $objDInventoryWarehouse = new DInventoryWarehouse('stock');
  972. $objDInventoryWarehouse->setTable($inventoryWarehouse);
  973. $stockDbName = Factory::config()->get('db.stock.dbname');
  974. // 组装检索sql
  975. $whereSql = '';
  976. if(isset($selectParams['keywordType']) && !empty($selectParams['keywordType'])){
  977. $whereSql .= self::assemblySql($selectParams['keywordType']);
  978. }
  979. // 商品关键字
  980. if (isset($selectParams['keyword']) && !empty($selectParams['keyword'])){
  981. $keywordId = 0;
  982. preg_match_all( '/\d+/' , $selectParams['keyword'] , $arr);
  983. if (isset($arr) && is_array($arr)){
  984. $arr = array_shift($arr);
  985. $keywordId = isset($arr[0]) ? (int)floatval($arr[0]) : 0;
  986. }
  987. $whereSql .= ' AND ( tmp.title like "%'.$selectParams['keyword'].'%" ';
  988. if (!empty($keywordId)){
  989. $whereSql .= ' OR tmp.basicId = '.$keywordId;
  990. }
  991. $whereSql .= ' ) ';
  992. }
  993. $where ='';
  994. if( isset($selectParams['merchantId']) && !empty($selectParams['merchantId']) ){
  995. $where .= ' AND g.merchantId = '.$selectParams['merchantId'];
  996. } else {
  997. if (isset($selectParams['ifMerchant']) && !empty($selectParams['ifMerchant'])){
  998. if($selectParams['ifMerchant'] == StatusCode::$delete){
  999. $where .= ' AND g.merchantId = 0 ';
  1000. } else {
  1001. $where .= ' AND g.merchantId <> 0 ';
  1002. }
  1003. }
  1004. }
  1005. //分类路径
  1006. if (isset($selectParams['categoryPath']) && !empty($selectParams['categoryPath'])){
  1007. if (is_string($selectParams['categoryPath']) || is_int($selectParams['categoryPath'])){
  1008. $selectParams['categoryPath'] = explode(',',$selectParams['categoryPath']);
  1009. }
  1010. $endPathId = array_pop($selectParams['categoryPath']);
  1011. if (!empty($endPathId)){
  1012. $whereSql .= ' and (find_in_set('.$endPathId.',tmp.categoryPath) or find_in_set('.$endPathId.',tmp.categoryId)) ';
  1013. }
  1014. }
  1015. // 商品品牌
  1016. if(isset($selectParams['brandId']) && !empty($selectParams['brandId'])){
  1017. $whereSql .=' and tmp.brandId ='.$selectParams['brandId'];
  1018. }
  1019. // 销售店铺
  1020. if(isset($selectParams['shopId']) && !empty($selectParams['shopId'])){
  1021. $whereSql .=' and tmp.shopId ='.$selectParams['shopId'];
  1022. }
  1023. // 上下架
  1024. if(isset($selectParams['enableStatus']) && !empty($selectParams['enableStatus'])){
  1025. $whereSql .=' and tmp.enableStatus ='.$selectParams['enableStatus'];
  1026. }
  1027. if (isset($selectParams['limit']) && isset($selectParams['offset'])) {
  1028. $limit = ' limit ' . $selectParams['offset'] . ',' . $selectParams['limit'];
  1029. }else{
  1030. $limit = '';
  1031. }
  1032. $order = ' GROUP BY tmp.id ORDER BY tmp.topTime DESC, tmp.sort desc, tmp.createTime desc';
  1033. $fields = 'b.id as basicId, g.merchantId,g.staffId,g.images,g.serviceInfo,g.specification,g.content,g.isRecommend,
  1034. g.id,g.basicGoodsId,g.shopId,b.title,b.`code`,b.categoryPath,
  1035. b.categoryId,b.brandId,g.enableStatus,g.topTime,g.sort,g.createTime,
  1036. g.deleteStatus,g.updateTime,g.salesNum,g.specType,g.isJoinCommission,
  1037. g.isDefine,g.virtualSalesNum,g.deliverySupIds,g.expressType,g.ruleId,g.expressFee,g.showExpress,
  1038. g.auditStatus,g.isOption,g.supplierId,g.isShield,g.notArea,g.notCustomerType,g.notCustomer,g.isDistribution,
  1039. g.support,g.retType,g.goodsGroups,g.realSalesNum';
  1040. // 导出
  1041. if ($export){
  1042. $sql = 'select tmp.*,s.warehouseId,sum(i.inventoryNum) inventoryNum,FIND_IN_SET(i.warehouseId,s.warehouseId) from (
  1043. select '.$fields.' from '.$goodsTab.' g left join '.$goodsBasicTab.' b on g.basicGoodsId = b.id
  1044. where g.deleteStatus= '.StatusCode::$standard.' and b.deleteStatus ='.StatusCode::$standard.$where.') tmp LEFT JOIN '.$shopTab.' s on s.id = tmp.shopId left join `'.$stockDbName.'`.'.$inventoryWarehouse.' i on i.materielId = tmp.basicGoodsId
  1045. where tmp.deleteStatus='.StatusCode::$standard.$whereSql.$order;
  1046. $dbResult = $this->objDGoods->exportQuery($sql);
  1047. if ($dbResult === false) {
  1048. return ResultWrapper::fail($this->objDGoods->error(), ErrorCode::$dberror);
  1049. }
  1050. //格式化导出数据
  1051. $formatListsResult = self::formatExportLists($dbResult);
  1052. if (!$formatListsResult->isSuccess()){
  1053. return ResultWrapper::fail($formatListsResult->getData(),$formatListsResult->getErrorCode());
  1054. }
  1055. $formatLists = $formatListsResult->getData();
  1056. self::exportCsv($formatLists);
  1057. // // 格式化商品数据
  1058. // $formatResult = self::formatManageGoodsLists($dbResult);
  1059. // if (!$formatResult->isSuccess()){
  1060. // return ResultWrapper::fail($formatResult->getData(),$formatResult->getErrorCode());
  1061. // }
  1062. // self::exportGoodsList($formatResult->getData());
  1063. // exit;
  1064. }
  1065. $sqlTotal = 'select count(*) as total from (
  1066. select '.$fields.' from '.$goodsTab.' g left join '.$goodsBasicTab.' b on g.basicGoodsId = b.id
  1067. where g.deleteStatus= '.StatusCode::$standard.' and b.deleteStatus ='.StatusCode::$standard.$where.') tmp LEFT JOIN '.$shopTab.' s on s.id = tmp.shopId left join `'.$stockDbName.'`.'.$inventoryWarehouse.' i on i.materielId = tmp.basicGoodsId
  1068. where tmp.deleteStatus='.StatusCode::$standard.$whereSql;
  1069. $total = $this->objDGoods->query($sqlTotal);
  1070. if ($total === false) {
  1071. return ResultWrapper::fail($this->objDGoods->error(), ErrorCode::$dberror);
  1072. }
  1073. if(empty($total[0]['total'])){
  1074. $return = [
  1075. 'data' => [],
  1076. 'total' => 0,
  1077. ];
  1078. return ResultWrapper::success($return);
  1079. }
  1080. $sql = 'select tmp.*,s.warehouseId,sum(i.inventoryNum) inventoryNum,FIND_IN_SET(i.warehouseId,s.warehouseId) from (
  1081. select '.$fields.' from '.$goodsTab.' g left join '.$goodsBasicTab.' b on g.basicGoodsId = b.id
  1082. where g.deleteStatus= '.StatusCode::$standard.' and b.deleteStatus ='.StatusCode::$standard.$where.') tmp LEFT JOIN '.$shopTab.' s on s.id = tmp.shopId left join `'.$stockDbName.'`.'.$inventoryWarehouse.' i on i.materielId = tmp.basicGoodsId
  1083. where tmp.deleteStatus='.StatusCode::$standard.$whereSql.$order.$limit;
  1084. $dbResult = $this->objDGoods->query($sql);
  1085. if ($dbResult === false) {
  1086. return ResultWrapper::fail($this->objDGoods->error(), ErrorCode::$dberror);
  1087. }
  1088. // 格式化商品数据
  1089. $formatResult = self::formatManageGoodsLists($dbResult);
  1090. if (!$formatResult->isSuccess()){
  1091. return ResultWrapper::fail($formatResult->getData(),$formatResult->getErrorCode());
  1092. }
  1093. $return = [
  1094. 'data' => $formatResult->getData(),
  1095. 'total' => isset($total[0]['total']) ? $total[0]['total'] : 0,
  1096. ];
  1097. return ResultWrapper::success($return);
  1098. }
  1099. /**
  1100. * 根据商品列表 keywordType 拼装sql
  1101. * @param $keywordType
  1102. */
  1103. public function assemblySql($keywordType)
  1104. {
  1105. $whereSql = '';
  1106. switch ($keywordType){
  1107. case 1: // 全部商品 审核通过
  1108. $whereSql .= '';
  1109. break;
  1110. case 2: // 销售中 上架且有库存 enableStatus 上下架(5上架 4下架)
  1111. $whereSql .= ' and (FIND_IN_SET(i.warehouseId,s.warehouseId) OR tmp.isDistribution = '.StatusCode::$standard.') and tmp.enableStatus ='.StatusCode::$standard;
  1112. break;
  1113. case 3: // 已售罄 上架无库存
  1114. $whereSql .= ' and ( FIND_IN_SET(i.warehouseId,s.warehouseId) =0 or FIND_IN_SET(i.warehouseId,s.warehouseId) is NUll ) and tmp.enableStatus ='.StatusCode::$standard;
  1115. break;
  1116. case 4: // 仓库中 下架
  1117. $whereSql .= ' and FIND_IN_SET(i.warehouseId,s.warehouseId) and tmp.enableStatus ='.StatusCode::$delete;
  1118. break;
  1119. }
  1120. return $whereSql;
  1121. }
  1122. /**
  1123. * Doc: (des="查询数据")
  1124. * User: XMing
  1125. * Date: 2020/8/25
  1126. * Time: 11:00 上午
  1127. * @param $data
  1128. * @param string $source
  1129. * @return ResultWrapper
  1130. */
  1131. private function getExportDataBySelectParams($data,string $source)
  1132. {
  1133. switch ($source){
  1134. case 'db':
  1135. $selectParams = $data;
  1136. break;
  1137. case 'es':
  1138. $allIds = [];
  1139. foreach ($data as $value){
  1140. $allIds[] = $value['id'];
  1141. }
  1142. if (empty($allIds)){
  1143. return ResultWrapper::fail('导出数据为空',ErrorCode::$paramError);
  1144. }
  1145. $selectParams['id'] = $allIds;
  1146. break;
  1147. }
  1148. $goodsLists = $this->objDGoods->exportSelect($selectParams,'*','createTime DESC');
  1149. if ($goodsLists === false){
  1150. return ResultWrapper::fail($this->objDGoods->error(),ErrorCode::$dberror);
  1151. }
  1152. $list = self::getGeneratorData($goodsLists);
  1153. return ResultWrapper::success($list);
  1154. }
  1155. /**
  1156. * Doc: (des="获取export查询方法返回的数据集合")
  1157. * User: XMing
  1158. * Date: 2020/8/22
  1159. * Time: 11:42 上午
  1160. * @param $object
  1161. * @return array
  1162. */
  1163. private static function getGeneratorData($object)
  1164. {
  1165. $arrayList = [];
  1166. foreach ($object as $value){
  1167. $arrayList[] = $value;
  1168. }
  1169. return $arrayList;
  1170. }
  1171. /**
  1172. * Doc: (des="导出商品数据")
  1173. * User: XMing
  1174. * Date: 2020/8/25
  1175. * Time: 10:57 上午
  1176. * @param $data
  1177. * @param string $source
  1178. * @return ResultWrapper
  1179. * @throws Exception
  1180. */
  1181. private function exportGoods($data,string $source)
  1182. {
  1183. $exportListsResult = self::getExportDataBySelectParams($data,$source);
  1184. if (!$exportListsResult->isSuccess()){
  1185. return ResultWrapper::fail($exportListsResult->getData(),$exportListsResult->getErrorCode());
  1186. }
  1187. $exportLists = $exportListsResult->getData();
  1188. if (empty($exportLists)){
  1189. return ResultWrapper::fail('导出数据为空',ErrorCode::$paramError);
  1190. }
  1191. //格式化导出数据
  1192. $formatListsResult = self::formatExportLists($exportLists);
  1193. if (!$formatListsResult->isSuccess()){
  1194. return ResultWrapper::fail($formatListsResult->getData(),$formatListsResult->getErrorCode());
  1195. }
  1196. $formatLists = $formatListsResult->getData();
  1197. self::exportCsv($formatLists);
  1198. }
  1199. /**git
  1200. * Doc: (des="生成规格名字")
  1201. * User: XMing
  1202. * Date: 2020/8/22
  1203. * Time: 2:47 下午
  1204. * @param array $specGroup
  1205. * @return string
  1206. */
  1207. private static function createSpecName(array $specGroup)
  1208. {
  1209. $str = '';
  1210. foreach ($specGroup as $value){
  1211. empty($str) && $str .= ' ';
  1212. if (!empty($value['specName'])){
  1213. $str .= $value['specName'].':';
  1214. }
  1215. if (!empty($value['specValueName'])){
  1216. $str .= $value['specValueName'];
  1217. }
  1218. }
  1219. return empty($str) ? '无' : $str;
  1220. }
  1221. /**
  1222. * Doc: (des="格式化导出数据")
  1223. * User: XMing
  1224. * Date: 2020/8/25
  1225. * Time: 11:26 上午
  1226. * @param array $data
  1227. * @return ResultWrapper
  1228. * @throws Exception
  1229. */
  1230. private function formatExportLists(array $data)
  1231. {
  1232. if (empty($data)){
  1233. return ResultWrapper::success([]);
  1234. }
  1235. $allGoodsBasicIds = [];
  1236. $selectParam = [];
  1237. $allShopIds = [];
  1238. foreach ($data as $value){
  1239. $allShopIds[] = $value['shopId'];
  1240. $allGoodsBasicIds[] = $value['basicGoodsId'];
  1241. $selectParam['material'][$value['shopId']][] = $value['id'];
  1242. }
  1243. $allShopIds = array_values(array_unique($allShopIds));
  1244. $allGoodsBasicIds = array_values(array_unique($allGoodsBasicIds));
  1245. if (empty($allGoodsBasicIds)){
  1246. return ResultWrapper::fail('没有导出商品id',ErrorCode::$paramError);
  1247. }
  1248. $allGoodsBasicIdsStr = implode(',',$allGoodsBasicIds);
  1249. $fields = ' b.*,c.title as categoryName,d.title as brandName ,b.expireTime';
  1250. $sql = 'SELECT '.$fields.' FROM qianniao_goods_basic_'.$this->enterpriseId.' AS b
  1251. LEFT JOIN qianniao_goods_category_'.$this->enterpriseId.' as c
  1252. ON c.id = b.categoryId
  1253. LEFT JOIN qianniao_goods_brand_'.$this->enterpriseId.' as d
  1254. ON d.id = b.brandId
  1255. WHERE b.id in ('.$allGoodsBasicIdsStr.')';
  1256. $queryLists = $this->objDGoods->exportQuery($sql);
  1257. if ($queryLists === false){
  1258. return ResultWrapper::fail($this->objDGoods->error(),ErrorCode::$dberror);
  1259. }
  1260. $queryLists = self::getGeneratorData($queryLists);
  1261. if (empty($queryLists)){
  1262. return ResultWrapper::fail('查询导出商品基础资料数据为空',ErrorCode::$paramError);
  1263. }
  1264. $goodsBasicMap = [];
  1265. foreach ($queryLists as $list){
  1266. $goodsBasicMap[$list['id']] = $list;
  1267. }
  1268. //获取sku数据
  1269. $skuMap = [];
  1270. if (!empty($allGoodsBasicIds)){
  1271. $skuLists = $this->objDSku->exportSelect(['goodsId' => $allGoodsBasicIds,'deleteStatus' => StatusCode::$standard],'id,goodsId,conversion,unitName,isMaster,specData,barCode');
  1272. if ($skuLists === false){
  1273. return ResultWrapper::fail($this->objDSku->error(),ErrorCode::$dberror);
  1274. }
  1275. $skuLists = self::getGeneratorData($skuLists);
  1276. foreach ($skuLists as &$value){
  1277. $value['skuMsg'] = $value['isMaster'] == StatusCode::$standard ? '主' : '辅';
  1278. $value['specGroup'] = empty($value['specData']) ? '无' : self::createSpecName(json_decode($value['specData'],true));
  1279. $value['inventoryNum'] = 0;
  1280. $value['salePrice'] = 0;
  1281. $value['marketPrice'] = 0;
  1282. $value['ladderPrice'] = '';
  1283. $skuMap[$value['goodsId']][] = $value;
  1284. }
  1285. unset($skuLists);
  1286. unset($value);
  1287. }
  1288. //获取商铺数据
  1289. //查询店铺
  1290. $shopMap = [];
  1291. if (!empty($allShopIds)){
  1292. $shopLists = $this->objDShop->exportSelect(['id'=>$allShopIds],'id,name,warehouseId');
  1293. if ($shopLists === false){
  1294. return ResultWrapper::fail($this->objDShop->error(),ErrorCode::$dberror);
  1295. }
  1296. $shopLists = self::getGeneratorData($shopLists);
  1297. foreach ($shopLists as $value){
  1298. $shopMap[$value['id']] = $value;
  1299. }
  1300. unset($value);
  1301. unset($shopLists);
  1302. }
  1303. //获取商品的价格
  1304. $priceResult = $this->objMPrice->getPrice($selectParam);
  1305. if (!$priceResult->isSuccess()){
  1306. return ResultWrapper::fail($priceResult->getData(),$priceResult->getErrorCode());
  1307. }
  1308. $priceMap = $priceResult->getData();
  1309. $inventorySelectParams = [];
  1310. foreach ($data as &$datum){
  1311. $datum['shopName'] = isset($shopMap[$datum['shopId']]) ? $shopMap[$datum['shopId']]['name'] : '无';
  1312. $datum['warehouseId'] = isset($shopMap[$datum['shopId']]) ? $shopMap[$datum['shopId']]['warehouseId'] : '';
  1313. $datum['specMultiple'] = isset($skuMap[$datum['basicGoodsId']]) ? $skuMap[$datum['basicGoodsId']] : [];
  1314. foreach ($datum['specMultiple'] as $item){
  1315. $inventorySelectParams[$datum['shopId']][] = $item['id'];
  1316. }
  1317. }
  1318. unset($datum);
  1319. $inventoryMap = [];
  1320. foreach ($inventorySelectParams as $shopId => $skuIds){
  1321. $inventoryResult = $this->objMInventory->getInventoryByShopIdAndSkuIds($shopId,$skuIds);
  1322. if (!$inventoryResult->isSuccess()){
  1323. return ResultWrapper::fail($inventoryResult->getData(),$inventoryResult->getErrorCode());
  1324. }
  1325. $inventoryMap[$shopId] = $inventoryResult->getData();
  1326. }
  1327. foreach ($data as &$value){
  1328. $value['inventoryTotalNum'] = 0;
  1329. if (!isset($inventoryMap[$value['shopId']])){
  1330. return ResultWrapper::fail('库存获取异常',ErrorCode::$paramError);
  1331. }
  1332. $row = $inventoryMap[$value['shopId']];
  1333. $value['inventoryTotalNum'] = 0;
  1334. foreach ($value['specMultiple'] as &$item){
  1335. $item['inventoryNum'] = isset($row[$item['id']]['num']) ? $row[$item['id']]['num'] : 0;
  1336. $item['ladderPriceMsg'] = '';
  1337. $thisPrice = isset($priceMap[$value['shopId']][$value['id']]) ? $priceMap[$value['shopId']][$value['id']] : [];
  1338. if (!empty($thisPrice)){
  1339. $item['salePrice'] = isset($thisPrice[$item['id']]) ? $thisPrice[$item['id']]['salePrice'] : '0.00';
  1340. $item['marketPrice'] = isset($thisPrice[$item['id']]) ? $thisPrice[$item['id']]['marketPrice'] : '0.00';
  1341. $item['ladderPrice'] = isset($thisPrice[$item['id']]) ? $thisPrice[$item['id']]['ladderPrice'] : [];
  1342. }
  1343. if (!empty($item['ladderPrice'])){
  1344. $str = '';
  1345. foreach ($item['ladderPrice'] as $v){
  1346. !empty($str) && $str .= ' ';
  1347. $str .= '('.$v['from'].'-'.$v['to'].')';
  1348. $str .= $v['price'];
  1349. }
  1350. $item['ladderPriceMsg'] = $str;
  1351. }
  1352. }
  1353. $value['categoryName'] = isset($goodsBasicMap[$value['basicGoodsId']]) ? $goodsBasicMap[$value['basicGoodsId']]['categoryName'] : '无';
  1354. $value['brandName'] = isset($goodsBasicMap[$value['basicGoodsId']]) ? $goodsBasicMap[$value['basicGoodsId']]['brandName'] : '无';
  1355. $value['goodsName'] = isset($goodsBasicMap[$value['basicGoodsId']]) ? $goodsBasicMap[$value['basicGoodsId']]['title'] : '无';
  1356. $value['goodsCode'] = createCode(StatusCode::$code['goodsBasic']['prefix'], $value['basicGoodsId'], StatusCode::$code['goodsBasic']['length']);;
  1357. $value['enableStatusMsg'] = $value['enableStatus'] == StatusCode::$standard ? '上架' : '下架';
  1358. $value['createTime'] = date('Y-m-d H:i:s',$value['createTime']);
  1359. $value['tag'] = isset($goodsBasicMap[$value['basicGoodsId']]) ? $goodsBasicMap[$value['basicGoodsId']]['tag'] : '无';
  1360. $value['describe'] = isset($goodsBasicMap[$value['basicGoodsId']]) ? $goodsBasicMap[$value['basicGoodsId']]['describe'] : '无';
  1361. $value['storage'] = '无';
  1362. if (isset($goodsBasicMap[$value['basicGoodsId']])){
  1363. if (empty($goodsBasicMap[$value['basicGoodsId']]['extends'])){
  1364. $value['storage'] = empty($goodsBasicMap[$value['basicGoodsId']]['extends']) ? '' : json_decode($goodsBasicMap[$value['basicGoodsId']]['extends'],true)['storage'];
  1365. }
  1366. }
  1367. $value['salesTotalNum'] = 0;
  1368. if (!empty($value['salesNum'])){
  1369. $salesNum = json_decode($value['salesNum'],true);
  1370. $value['salesTotalNum'] = array_sum(array_column($salesNum,'salesNum'));
  1371. }
  1372. }
  1373. return ResultWrapper::success($data);
  1374. }
  1375. /**
  1376. * Doc: (des="")
  1377. * User: XMing
  1378. * Date: 2020/8/25
  1379. * Time: 11:50 上午
  1380. * @param $selectParams
  1381. * @throws Exception
  1382. */
  1383. public function fixEsData($selectParams)
  1384. {
  1385. $limit = $selectParams['limit'];
  1386. unset($selectParams['limit']);
  1387. $offset = $selectParams['offset'];
  1388. unset($selectParams['offset']);
  1389. $selectParams['deleteStatus'] = StatusCode::$standard;
  1390. $dbResult = $this->objDGoods->select($selectParams, 'id', null, $limit, $offset);
  1391. foreach ($dbResult as $goods) {
  1392. self::updateEsData($goods['id']);
  1393. }
  1394. print_r(count($dbResult) . '条数据');
  1395. }
  1396. /**
  1397. * 通过goodsIds获取名称
  1398. * 注意:本方法并不能获取到商品名称,可用此方法拿到BasicGoodsId,再通过getNameByGoodsIds方法拿到商品名称
  1399. * @param array $goodsIds
  1400. * @param string $field
  1401. * @return array
  1402. * @throws Exception
  1403. */
  1404. public function getGoodsNames($goodsIds = [], $field = "*")
  1405. {
  1406. $return = [];
  1407. if (!$goodsIds) {
  1408. return $return;
  1409. }
  1410. $sql = 'select g.*,b.images from qianniao_goods_'.$this->enterpriseId.' g left join qianniao_goods_basic_'.$this->enterpriseId.' b on g.basicGoodsId = b.id where g.id in ('.implode(',', $goodsIds).')';
  1411. $goodsData = $this->objDGoods->query($sql);
  1412. if ($goodsData === false) {
  1413. return $return;
  1414. }
  1415. $supplierIds = [];
  1416. foreach ($goodsData as $value){
  1417. !empty($value['supplierId']) && $supplierIds[] = $value['supplierId'];
  1418. }
  1419. $supplierMap = [];
  1420. $objMSupplier = new MSupplier($this->userCenterId,$this->enterpriseId);
  1421. $supplierResult = $objMSupplier->getSupplierNameByIds($supplierIds);
  1422. if ($supplierResult->isSuccess()){
  1423. $supplier = $supplierResult->getData();
  1424. $supplierMap = MSupplier::supplierMap($supplier);
  1425. }
  1426. foreach ($goodsData as &$goods) {
  1427. $goods['supplierName'] = isset($supplierMap[$goods['supplierId']]['title']) ? $supplierMap[$goods['supplierId']]['title'] : '';
  1428. $return[$goods['id']] = $goods;
  1429. }
  1430. return $return;
  1431. }
  1432. /**
  1433. * 获取商品sku信息
  1434. * @param $goodsBasic
  1435. * @return array
  1436. * @throws Exception
  1437. */
  1438. public function getGoodsBasicAndSkuInfo($goodsBasic)
  1439. {
  1440. $skuArray = [];//所有sku数组
  1441. if (isset($goodsBasic['masterUnit']) && is_array($goodsBasic['masterUnit'])) {
  1442. $goodsBasic['masterUnit']['skuId'] = $goodsBasic['masterUnit']['id'];
  1443. unset($goodsBasic['masterUnit']['id']);
  1444. $skuArray[] = $goodsBasic['masterUnit'];
  1445. }
  1446. if (isset($goodsBasic['branchUnit']) && is_array($goodsBasic['branchUnit'])) {
  1447. $branchUnit = $goodsBasic['branchUnit'];
  1448. foreach ($branchUnit as $branch) {
  1449. $branch['skuId'] = $branch['id'];
  1450. unset($branch['id']);
  1451. $skuArray[] = $branch;
  1452. }
  1453. }
  1454. return $skuArray;
  1455. }
  1456. /**
  1457. * 处理销量
  1458. * @param $goodsData
  1459. * @param array $allGoodsIds
  1460. * @return mixed
  1461. * @throws Exception
  1462. */
  1463. public function getSalesNumBySku($goodsData, $allGoodsIds = [])
  1464. {
  1465. $cartData = [];
  1466. if (!empty($allGoodsIds)) {
  1467. $objMCart = new MCart($this->userCenterId, $this->enterpriseId);
  1468. $cartData = $objMCart->getCartNumByGoodsId($allGoodsIds);
  1469. }
  1470. foreach ($goodsData as &$goods) {
  1471. //[{"skuId":1,"salesNum":10},{"skuId":2,"salesNum":10}] salesNum
  1472. $salesNumArr = [];
  1473. if (!empty($goods['salesNum'])) {
  1474. $salesNumData = is_array($goods['salesNum']) ? $goods['salesNum'] : json_decode($goods['salesNum'], true);
  1475. if ($salesNumData) {
  1476. foreach ($salesNumData as $salesNum) {
  1477. $salesNumArr[$salesNum['skuId']] = $salesNum['salesNum'];
  1478. }
  1479. }
  1480. }
  1481. $goods['oldSalesNum'] = $goods['salesNum'];//复制原数据
  1482. if(empty($goods['salesNum'])){
  1483. $goods['salesNum'] = 0;
  1484. }else{
  1485. if(is_array($goods['salesNum'])){
  1486. $goods['salesNum'] = array_sum(array_column($goods['salesNum'],'salesNum'));
  1487. }else{
  1488. $goods['salesNum'] = array_sum(array_column(json_decode($goods['salesNum'],true),'salesNum'));
  1489. }
  1490. }
  1491. if (isset($goods['skuData'])) {
  1492. foreach ($goods['skuData'] as &$goodsSku) {
  1493. $goodsSku['salesNum'] = isset($salesNumArr[$goodsSku['skuId']]) ? $salesNumArr[$goodsSku['skuId']] : 0;
  1494. //$goods['salesNum'] += $goodsSku['salesNum'];
  1495. $goodsSku['cartNum'] = isset($cartData[$goods['id']][$goodsSku['skuId']]) ? $cartData[$goods['id']][$goodsSku['skuId']] : 0;
  1496. }
  1497. }
  1498. }
  1499. return $goodsData;
  1500. }
  1501. /**
  1502. * 处理价格
  1503. * isActivityPrice 是否满足活动价 5:满足 4:不满足
  1504. * salePrice 销售价格 小程序上展示的价格
  1505. * marketPrice 市场价格
  1506. * @param $goodsData
  1507. * @return mixed
  1508. * @throws Exception
  1509. */
  1510. public function getPriceBySku($goodsData)
  1511. {
  1512. foreach ($goodsData as &$goods) {
  1513. foreach ($goods['skuData'] as &$goodsSku) {
  1514. $goodsSku["salePrice"] = '';
  1515. $goodsSku["ladderPrice"] = [];
  1516. $goodsSku["marketPrice"] = '';
  1517. $goodsSku["enabledLadder"] = '';
  1518. $goodsSku['setNum'] = 0;//起订数量
  1519. $goodsSku['isActivityPrice'] = StatusCode::$delete;//初始化,是否满足活动价
  1520. $goodsSku['limitNum'] = 0;//活动商品限购数量
  1521. }
  1522. }
  1523. unset($goods);
  1524. //获取价格
  1525. $selectParam['material'] = [];
  1526. foreach ($goodsData as $goods) {
  1527. $selectParam['material'][$goods['shopId']][] = $goods['id'];
  1528. }
  1529. if($this->userCenterId){
  1530. $selectParam['customerId'] = $this->useCustomerId;//客户有特殊调价
  1531. }
  1532. $priceResult = $this->objMPrice->getPrice($selectParam);
  1533. if (!$priceResult->isSuccess()) {
  1534. return $goodsData;
  1535. }
  1536. $goodsArr = [];
  1537. $priceResult = $priceResult->getData();
  1538. foreach ($priceResult as $shopPriceArr) {
  1539. foreach ($shopPriceArr as $goodsId => $goodsSkuArr) {
  1540. $goodsArr[$goodsId] = $goodsSkuArr;
  1541. }
  1542. }
  1543. //是否有营销活动价
  1544. $objMCustomer = new MCustomer($this->enterpriseId, $this->userCenterId);
  1545. $customerResult = $objMCustomer->getCustomerInfoByUserCenterId($this->userCenterId);
  1546. $activityArr = [];
  1547. if ($customerResult->isSuccess()) {
  1548. $customer = $customerResult->getData();
  1549. $objMActivity = new MActivity($this->userCenterId, $this->enterpriseId);
  1550. //获取促销活动价格
  1551. $dbResult = $objMActivity->getActivity([
  1552. 'goodsId' => implode(',', array_column($goodsData, 'id')),
  1553. 'customerType' => isset($customer['type']) ? $customer['type'] : 0,
  1554. ]);//TODO
  1555. $activityResult = $dbResult->getData();
  1556. unset($dbResult);
  1557. foreach ($activityResult as $activity) {
  1558. $activityArr[$activity['goodsId'] . $activity['skuId']] = $activity;
  1559. }
  1560. }
  1561. foreach ($goodsData as &$goods) {
  1562. if (isset($goodsArr[$goods['id']])) {
  1563. $skuInfo = $goodsArr[$goods['id']];
  1564. foreach ($goods['skuData'] as &$skuData) {
  1565. if (isset($skuInfo[$skuData['skuId']])) {
  1566. if (isset($activityArr[$goods['id'] . $skuData['skuId']])) {
  1567. //存在活动价格
  1568. $skuData["salePrice"] = $activityArr[$goods['id'] . $skuData['skuId']]['price'];
  1569. $skuData["activityId"] = $activityArr[$goods['id'] . $skuData['skuId']]['activityId'];//活动id
  1570. $skuData['isActivity'] = StatusCode::$standard;//活动价格
  1571. //如果用户登录,检测用户是否达到限购,商品是否枪光(是: 添加标识)
  1572. //获取用户活动购买数量
  1573. if (!empty($this->userCenterId)) {
  1574. $userLimit = (int)$this->objActivityLimitCache->getLimit($skuData["activityId"], $goods['id'], $skuData['skuId'], $this->userCenterId);
  1575. //活动商品用户限购数
  1576. $goodsLimit = (int)$activityArr[$goods['id'] . $skuData['skuId']]['limitNum'];
  1577. if ($userLimit < $goodsLimit) {
  1578. //此商品相对用户,满足活动价
  1579. $skuData['isActivityPrice'] = StatusCode::$standard;//满足活动价
  1580. }
  1581. $skuData['limitNum'] = $goodsLimit;
  1582. }
  1583. } else {
  1584. $skuData["salePrice"] = $skuInfo[$skuData['skuId']]['salePrice'];
  1585. $skuData["activityId"] = 0;//不存在活动
  1586. }
  1587. $skuData["isActivity"] = isset($skuData['isActivity']) ? $skuData['isActivity'] : StatusCode::$delete;
  1588. $skuData["originPrice"] = $skuInfo[$skuData['skuId']]['salePrice'];//原价(促销活动时价格有差异)
  1589. $skuData["ladderPrice"] = $skuInfo[$skuData['skuId']]['ladderPrice'];
  1590. $skuData["marketPrice"] = isset($skuInfo[$skuData['skuId']]['marketPrice']) ? $skuInfo[$skuData['skuId']]['marketPrice'] : bcadd($skuInfo[$skuData['skuId']]['salePrice'], mt_rand(1, 3), 2);
  1591. $skuData["enabledLadder"] = $skuInfo[$skuData['skuId']]['enabledLadder'];
  1592. if (isset($skuData['isActivity']) && $skuData['isActivity'] == StatusCode::$delete) {
  1593. $skuData["setNum"] = isset($skuInfo[$skuData['skuId']]['setNum']) ? $skuInfo[$skuData['skuId']]['setNum'] : 0;//起订数量
  1594. }
  1595. }
  1596. }
  1597. }
  1598. }
  1599. return $goodsData;
  1600. }
  1601. /**
  1602. * Doc: (des="搜索的where条件拼接")
  1603. * User: XMing
  1604. * Date: 2020/11/30
  1605. * Time: 10:53 上午
  1606. * @param $selectParams
  1607. * @param bool $export
  1608. * @return array|ResultWrapper
  1609. * @throws Exception
  1610. */
  1611. public function setWhere($selectParams,$export = false)
  1612. {
  1613. $defaultDSL = [];
  1614. if($export === false){
  1615. $defaultDSL = [
  1616. 'from' => $selectParams['offset'],
  1617. 'size' => $selectParams['limit'],
  1618. 'sort' => [
  1619. 'topTime' => [
  1620. 'order' => 'desc'
  1621. ]
  1622. ],
  1623. ];
  1624. }
  1625. $dsl = [];
  1626. $dsl['query']['bool']['must'][] = [
  1627. 'term' => ['enterpriseId' => $this->enterpriseId],
  1628. ];
  1629. if (isset($selectParams['enableStatus'])) {
  1630. if (!empty($selectParams['enableStatus'])) {
  1631. $dsl['query']['bool']['filter'][] =
  1632. ['term' => ['enableStatus' => $selectParams['enableStatus']]];
  1633. }
  1634. }
  1635. if (isset($selectParams['auditStatus'])) {
  1636. if (!empty($selectParams['auditStatus'])) {
  1637. $dsl['query']['bool']['filter'][] =
  1638. ['term' => ['auditStatus' => $selectParams['auditStatus']]];
  1639. }
  1640. }
  1641. $selectParams = parent::getStaffQueryParams($selectParams, 'staffId');
  1642. //个人数据域
  1643. if (isset($selectParams['staffId'])) {
  1644. $dsl['query']['bool']['filter'][] =
  1645. ['terms' => ['staffId' => $selectParams['staffId']]];
  1646. }
  1647. //按商品ids进行搜索
  1648. if (isset($selectParams['goodsIds'])) {
  1649. $dsl['query']['bool']['filter'][] =
  1650. ['terms' => ['id' => $selectParams['goodsIds']]];
  1651. }
  1652. //去ES的basic类型下查找匹配的basicIds
  1653. if (!empty($selectParams['keyword'])
  1654. || !empty($selectParams['categoryId'])
  1655. || !empty($selectParams['brandId'])
  1656. || !empty($selectParams['categoryPath'])
  1657. ) {
  1658. $basicSearchParams = [
  1659. 'keyword' => isset($selectParams['keyword']) ? $selectParams['keyword'] : '',
  1660. 'categoryPath' => isset($selectParams['categoryPath']) ? $selectParams['categoryPath'] : '',
  1661. 'categoryId' => isset($selectParams['categoryId']) ? $selectParams['categoryId'] : '',
  1662. 'brandId' => isset($selectParams['brandId']) ? $selectParams['brandId'] : '',
  1663. 'limit' => 1000
  1664. ];
  1665. $goodsBasics = $this->objMGoodsBasic->searchGoodsBasics($basicSearchParams);
  1666. $goodsBasicLists = [];
  1667. if (!$goodsBasics->isSuccess()) {
  1668. Logger::logs(E_USER_ERROR,'es-查询错误',__CLASS__,__LINE__,$goodsBasics->getData());
  1669. return ResultWrapper::fail($goodsBasics->getData(),$goodsBasics->getErrorCode());
  1670. } else {
  1671. $goodsBasicData = $goodsBasics->getData();
  1672. foreach ($goodsBasicData as $goodsBasic) {
  1673. $goodsBasicLists[$goodsBasic['id']] = $goodsBasic;
  1674. }
  1675. }
  1676. $goodsBasicIds = array_keys($goodsBasicLists);
  1677. $dsl['query']['bool']['must'][] = [
  1678. 'terms' => [
  1679. 'basicGoodsId' => $goodsBasicIds
  1680. ]
  1681. ];
  1682. }
  1683. //后台按商铺搜索
  1684. if (!empty($selectParams['shopId'])) {
  1685. if (is_array($selectParams['shopId'])) {
  1686. $dsl['query']['bool']['must'][] = [
  1687. 'terms' => [
  1688. 'shopId' => $selectParams['shopId']
  1689. ]
  1690. ];
  1691. } else {
  1692. $dsl['query']['bool']['must'][] = [
  1693. 'term' => [
  1694. 'shopId' => $selectParams['shopId']
  1695. ],
  1696. ];
  1697. }
  1698. }
  1699. //前台按销售区域筛选
  1700. if ($this->isFront) {
  1701. $dsl['query']['bool']['must'][] = [
  1702. 'term' => ['enableStatus' => StatusCode::$standard],
  1703. ];
  1704. $areaCode = isset($selectParams['areaCode']) ? $selectParams['areaCode'] : '';
  1705. $shopIdResult = $this->objMShop->getShopIdsByArea($selectParams['userCenterId'], $areaCode);
  1706. if (!$shopIdResult->isSuccess()) {
  1707. Logger::logs(E_USER_ERROR,'es-查询错误',__CLASS__,__LINE__,$shopIdResult->getData());
  1708. return ResultWrapper::fail($shopIdResult->getData(),$shopIdResult->getErrorCode());
  1709. }
  1710. $shopIds = $shopIdResult->getData();
  1711. /*$dsl['query']['bool']['must'][] = [
  1712. 'terms' => [
  1713. 'shopId' => $shopIds
  1714. ]
  1715. ];*/
  1716. } else {
  1717. //数据域
  1718. /*parent::getAccessShopIds();
  1719. if (!empty(parent::$shopIds)) {
  1720. $dsl['query']['bool']['filter'][] = [
  1721. 'terms' => ['shopId' => parent::$shopIds]
  1722. ];
  1723. }*/
  1724. }
  1725. if (isset($selectParams['inStock'])) {
  1726. $goodsBasicIdResult = self::getInOrOutOfStockGoodsBasicId($selectParams['inStock']);
  1727. if (!$goodsBasicIdResult->isSuccess()) {
  1728. Logger::logs(E_USER_ERROR,'es-查询错误',__CLASS__,__LINE__,$goodsBasicIdResult->getData());
  1729. return ResultWrapper::fail($goodsBasicIdResult->getData(),$goodsBasicIdResult->getErrorCode());
  1730. }
  1731. }
  1732. $dsl = array_merge($defaultDSL, $dsl);
  1733. return ResultWrapper::success($dsl);
  1734. }
  1735. /**
  1736. *
  1737. * 废弃
  1738. * @param $inStock
  1739. * @return ResultWrapper
  1740. */
  1741. //获取有无库存的基本资料id
  1742. public function getInOrOutOfStockGoodsBasicId($inStock)
  1743. {
  1744. //查询所有商品获取库存
  1745. $lists = $this->objDGoods->query('SELECT `id`,`shopId`,`basicGoodsId` FROM qianniao_goods_'.$this->enterpriseId.' WHERE `deleteStatus` = '.StatusCode::$standard);
  1746. if ($lists === false){
  1747. return ResultWrapper::fail($this->objDGoods->error(),ErrorCode::$dberror);
  1748. }
  1749. if (empty($lists)){
  1750. return ResultWrapper::success([]);
  1751. }
  1752. $selectParams = [];
  1753. foreach ($lists as $list){
  1754. $selectParams[$list['shopId']][] = $list['basicGoodsId'];
  1755. }
  1756. //查询库存
  1757. $inventoryMap = [];
  1758. $objMInventory = new MInventory($this->enterpriseId,$this->userCenterId);
  1759. foreach ($selectParams as $shopId => $materielIds){
  1760. // 根据商铺id和物料ids获取库存信息
  1761. $inventoryResult = $objMInventory->getInventoryByShopIdAndMaterielIds($shopId,$materielIds);
  1762. if (!$inventoryResult->isSuccess()){
  1763. return ResultWrapper::fail($inventoryResult->getData(),$inventoryResult->getErrorCode());
  1764. }
  1765. $inventory = $inventoryResult->getData();
  1766. foreach ($inventory as $id => $item){
  1767. if ($inStock == StatusCode::$standard){
  1768. $item['num'] > 0 && $inventoryMap[] = [
  1769. 'shopId' => $shopId,
  1770. 'basicGoodsId' => $id
  1771. ];
  1772. }else{
  1773. $item['num'] == 0 && $inventoryMap[] = [
  1774. 'shopId' => $shopId,
  1775. 'basicGoodsId' => $id
  1776. ];
  1777. }
  1778. }
  1779. }
  1780. return ResultWrapper::success($inventoryMap);
  1781. }
  1782. /**
  1783. * 创建订单搜索商品 首字母搜索
  1784. * @param $data
  1785. * @param bool $type
  1786. * @return ResultWrapper
  1787. */
  1788. public function getGoodsByCondition($data, $type = false)
  1789. {
  1790. $Condition = isset($data['condition']) && !empty($data['condition']) ? $data['condition'] : '';
  1791. if (empty($Condition)) return ResultWrapper::success([]);
  1792. $objChineseCharacter = new ChineseCharacter();
  1793. $Condition = $objChineseCharacter->getInitials($Condition);
  1794. $objDGoodsBasic = new DGoodsBasic();
  1795. //判断是否上架 没有参数默认查询所有
  1796. $whereSql = '';
  1797. if (isset($data['enableStatus']) && !empty($data['enableStatus'])) {
  1798. $whereSql .= ' and g.enableStatus = ' . $data['enableStatus'];
  1799. }
  1800. //判断是否有商铺检索
  1801. if(isset($data['shopId']) && !empty($data['shopId'])){
  1802. if(is_array($data['shopId'])){
  1803. $str = 'in('.rtrim(implode(',',$data['shopId']),',').')';
  1804. }else{
  1805. $str = '= '.$data['shopId'];
  1806. }
  1807. $whereSql .= ' and g.shopId '.$str;
  1808. }
  1809. //查询商品sql
  1810. $sql = 'select g.id,b.title from ' . $this->objDGoods->get_Table() . ' g left join ' . $objDGoodsBasic->get_Table() . '_' . $this->enterpriseId . ' b on g.basicGoodsId = b.id where b.condition like "%' . $Condition . '%" and g.deleteStatus = ' . StatusCode::$standard . $whereSql . ' order by g.id desc';
  1811. //判断来源 false查询商品 true查询基础资料
  1812. if ($type) {
  1813. //查询基础资料sql
  1814. $sql = 'select id,title from ' . $objDGoodsBasic->get_Table() . '_' . $this->enterpriseId . ' where `condition` like "%' . $Condition . '%" and deleteStatus = ' . StatusCode::$standard . ' order by id desc';
  1815. }
  1816. $dbResult = $this->objDGoods->query($sql);
  1817. if ($dbResult === false) {
  1818. return ResultWrapper::fail($this->objDGoods->error(), ErrorCode::$dberror);
  1819. }
  1820. return ResultWrapper::success($dbResult);
  1821. }
  1822. /**
  1823. * 格式化所有商品搜索条件
  1824. * @param $enterpriseId
  1825. * @return ResultWrapper
  1826. * @throws Exception
  1827. */
  1828. public function formatGoodsCondition($enterpriseId)
  1829. {
  1830. $objDGoodsBasic = new DGoodsBasic();
  1831. $objDGoodsBasic->setTable($objDGoodsBasic->get_Table() . '_' . $enterpriseId);
  1832. $sql = 'select id,title from ' . $objDGoodsBasic->get_Table();
  1833. $dbResult = $objDGoodsBasic->query($sql);
  1834. if ($dbResult === false) {
  1835. return ResultWrapper::fail($objDGoodsBasic->error(), ErrorCode::$dberror);
  1836. }
  1837. $objChineseCharacter = new ChineseCharacter();
  1838. foreach ($dbResult as $value) {
  1839. $Condition = $objChineseCharacter->getInitials(trim($value['title']));
  1840. $midDbResult = $objDGoodsBasic->update(['condition' => $Condition], ['id' => $value['id']]);
  1841. if ($midDbResult === false) {
  1842. return ResultWrapper::fail($objDGoodsBasic->error(), ErrorCode::$dberror);
  1843. }
  1844. }
  1845. return ResultWrapper::success('格式化成功');
  1846. }
  1847. /**
  1848. * Doc: (des="根据条件查询商品")
  1849. * User: XMing
  1850. * Date: 2020/11/30
  1851. * Time: 11:00 上午
  1852. * @param string $keyword
  1853. * @return ResultWrapper
  1854. * @throws Exception
  1855. */
  1856. private function searchByKeyword(string $keyword)
  1857. {
  1858. //如果是关键词搜索,尝试从es中检索商品
  1859. $dslResult = $this->setWhere(['keyword'=>$keyword,'offset' => 0,'limit' => 10,'userCenterId' => $this->userCenterId]);
  1860. if (!$dslResult->isSuccess()){
  1861. return ResultWrapper::fail($dslResult->getData(),$dslResult->getErrorCode());
  1862. }
  1863. $dsl = $dslResult->getData();
  1864. $result = $this->objDGoods->getSearchQueryDsl($dsl);
  1865. if (isset($result['status']) && $result['status'] == 400) {
  1866. if ($result['error']['reason'] == 'all shards failed') {
  1867. return ResultWrapper::success([
  1868. 'data' => [],
  1869. 'total' => 0
  1870. ]);
  1871. }
  1872. }
  1873. if (!isset($result['hits']) || $result['hits']['total'] == 0) {
  1874. return ResultWrapper::success([
  1875. 'data' => [],
  1876. 'total' => 0
  1877. ]);
  1878. }
  1879. $esLists = $result['hits']['hits'];
  1880. $allGoodsIds = [];
  1881. foreach ($esLists as $key => &$value) {
  1882. //$data['id'] = $value['_id'];
  1883. $allGoodsIds[] = $value['_source']['id'];
  1884. }
  1885. $goodsResult = self::getGoodsBySelectParams(['goodsIds' => $allGoodsIds,'offset' => 0,'limit' => 10],true);
  1886. if (!$goodsResult->isSuccess()){
  1887. return ResultWrapper::fail($goodsResult->getData(),$goodsResult->getErrorCode());
  1888. }
  1889. $goodsResult = $goodsResult->getData();
  1890. $total = $goodsResult['total'];
  1891. $goodsData = $goodsResult['data'];
  1892. return ResultWrapper::success(['data'=>$goodsData,'total' => $total]);
  1893. }
  1894. /**
  1895. * Doc: (des="根据条件查询过滤商品id")
  1896. * User: XMing
  1897. * Date: 2021/1/18
  1898. * Time: 10:27 上午
  1899. * @param array $selectParams
  1900. * @return ResultWrapper
  1901. * @throws Exception
  1902. */
  1903. public function getFilterIdsBySelectParams(array $selectParams): ResultWrapper
  1904. {
  1905. $retIds = [];
  1906. if (isset($selectParams['userCenterId']) && !empty($selectParams['userCenterId'])){
  1907. //通过userCenterId获取客户id以及客户类型
  1908. $customerResult = $this->objMCustomer->getCustomerInfoByUserCenterId($selectParams['userCenterId']);
  1909. if (!$customerResult->isSuccess()){
  1910. return ResultWrapper::fail($customerResult->getData(),$customerResult->getErrorCode());
  1911. }
  1912. $customerInfo = $customerResult->getData();
  1913. $customerId = getArrayItem($customerInfo,'id',0);
  1914. $customerType = getArrayItem($customerInfo,'type',0);
  1915. $filter = '';
  1916. if (isset($customerId) && !empty($customerId)){
  1917. $filter .= ' FIND_IN_SET('.$customerId.',`notCustomer`) ';
  1918. if (isset($customerType) && !empty($customerType)){
  1919. $filter .= ' OR FIND_IN_SET('.$customerType.',`notCustomerType`) ';
  1920. }
  1921. }
  1922. if (isset($selectParams['areaCode']) && !empty($selectParams['areaCode'])){
  1923. $areaCode = $selectParams['areaCode'];
  1924. if (!empty($filter)){
  1925. $filter .= ' OR ';
  1926. }
  1927. $filter .= " (JSON_CONTAINS(notArea->'$[*]', '\"".$areaCode."\"', '$') OR JSON_CONTAINS(notArea->'$[*]','\"0-0-0\"','$')) ";
  1928. }
  1929. $fields = 'id';
  1930. $sql = 'SELECT '.$fields.' FROM qianniao_goods_'.$this->enterpriseId.' WHERE isShield = '.StatusCode::$standard.' AND ( '.$filter.')';
  1931. $ids = $this->objDGoods->query($sql);
  1932. if ($ids === false){
  1933. return ResultWrapper::fail($this->objDGoods->error(),ErrorCode::$dberror);
  1934. }
  1935. foreach ($ids as $val){
  1936. $retIds[] = $val['id'];
  1937. }
  1938. }
  1939. return ResultWrapper::success($retIds);
  1940. }
  1941. /**
  1942. * Doc: (des="商品检索 商城、后台")
  1943. * User: XMing
  1944. * Date: 2021/3/3
  1945. * Time: 3:21 下午
  1946. * @param array $selectParams
  1947. * @param bool $export
  1948. * @return ResultWrapper
  1949. * @version 1.0.1
  1950. */
  1951. public function search_new(array $selectParams,bool $export = false): ResultWrapper
  1952. {
  1953. switch ($this->isFront){
  1954. case true:
  1955. //mini app
  1956. //1.获取当前范围内的店铺
  1957. $shopResult = self::getShopIdsByArea($selectParams['areaCode']);
  1958. if (!$shopResult->isSuccess()){
  1959. return ResultWrapper::fail($shopResult->getData(),ErrorCode::$dberror);
  1960. }
  1961. $shopData = $shopResult->getData();
  1962. if (empty($shopData)){
  1963. return ResultWrapper::success(['data' => [], 'total' => 0]);
  1964. }
  1965. $shopIds = isset($shopData['shopIds']) ? $shopData['shopIds'] : [];
  1966. //商品查询参数
  1967. $condition = [
  1968. 'shopId' => isset($selectParams['shopId']) ? $selectParams['shopId'] : 0,
  1969. 'shopIds' => $shopIds,
  1970. 'categoryId' => isset($selectParams['categoryId']) ? $selectParams['categoryId'] : '',
  1971. 'keyword' => isset($selectParams['keyword']) ? $selectParams['keyword'] : '',
  1972. 'goodsIds' => isset($selectParams['goodsIds']) ? $selectParams['goodsIds'] : [],
  1973. 'categoryPath' => isset($selectParams['categoryPath']) ? $selectParams['categoryPath'] : [],
  1974. 'offset' => $selectParams['offset'],
  1975. 'limit' => $selectParams['limit']
  1976. ];
  1977. //查询店铺下经营屏蔽的商品Id
  1978. $filterIdsResult = self::getFilterIdsBySelectParams($selectParams);
  1979. if (!$filterIdsResult->isSuccess()){
  1980. return ResultWrapper::fail($filterIdsResult->getData(),$filterIdsResult->getErrorCode());
  1981. }
  1982. //过滤商品ids
  1983. $filterIds = $filterIdsResult->getData();
  1984. $condition['notGoodsIds'] = $filterIds;
  1985. break;
  1986. case false:
  1987. //manage
  1988. break;
  1989. }
  1990. }
  1991. /**
  1992. * Doc: (des="es-搜索商品")
  1993. * User: XMing
  1994. * Date: 2020/11/30
  1995. * Time: 10:40 上午
  1996. * @param array $selectParams 搜索条件
  1997. * @param bool $export 是否导出
  1998. * @return ResultWrapper
  1999. * @throws Exception
  2000. */
  2001. public function search(array $selectParams,$export = false)
  2002. {
  2003. if($this->isFront){
  2004. $shopResult = self::getShopIdsByArea($selectParams['areaCode']);
  2005. if (!$shopResult->isSuccess()){
  2006. return ResultWrapper::fail($shopResult->getData(),ErrorCode::$dberror);
  2007. }
  2008. $shopData = $shopResult->getData();//销售区域店铺数据
  2009. if (empty($shopData)){
  2010. return ResultWrapper::success(['data' => [], 'total' => 0]);
  2011. }
  2012. $shopIds = isset($shopData['shopIds']) ? $shopData['shopIds'] : [];
  2013. //条件
  2014. $condition = [
  2015. 'shopId' => isset($selectParams['shopId']) ? $selectParams['shopId'] : 0,
  2016. 'shopIds' => $shopIds,
  2017. 'categoryId'=> isset($selectParams['categoryId']) ? $selectParams['categoryId'] : '',
  2018. 'keyword' => isset($selectParams['keyword']) ? $selectParams['keyword'] : '',
  2019. 'goodsIds' => isset($selectParams['goodsIds']) ? $selectParams['goodsIds'] : [],
  2020. 'categoryPath' => isset($selectParams['categoryPath']) ? $selectParams['categoryPath'] : [],
  2021. 'offset' => $selectParams['offset'],
  2022. 'limit' => $selectParams['limit'],
  2023. 'brandId' => isset($selectParams['brandId']) ? $selectParams['brandId'] : [],//商品品牌
  2024. 'sort' => isset($selectParams['sort']) ? $selectParams['sort'] : '', //销售排序
  2025. 'priceSort' => isset($selectParams['priceSort']) ? $selectParams['priceSort'] : '', //价格排序
  2026. 'support' => isset($selectParams['support']) ? $selectParams['support'] : [], //商品服务
  2027. 'goodsGroups' => isset($selectParams['goodsGroups']) ? $selectParams['goodsGroups'] : '', //商品分组
  2028. 'enableStatus' => getArrayItem($selectParams, 'enableStatus', 5),
  2029. ];
  2030. // 通过区域获取销售店铺
  2031. $filterIdsResult = self::getFilterIdsBySelectParams($selectParams);
  2032. if (!$filterIdsResult->isSuccess()){
  2033. return ResultWrapper::fail($filterIdsResult->getData(),$filterIdsResult->getErrorCode());
  2034. }
  2035. $filterIds = $filterIdsResult->getData();
  2036. $condition['notGoodsIds'] = $filterIds;//过滤商品id
  2037. $goodsResult = self::getGoodsBySelectParams($condition,true);
  2038. if (!$goodsResult->isSuccess()){
  2039. return ResultWrapper::fail($goodsResult->getData(),$goodsResult->getErrorCode());
  2040. }
  2041. $goodsResult = $goodsResult->getData();
  2042. $total = $goodsResult['total'];
  2043. $goodsData = $goodsResult['data'];
  2044. if (empty($total)){
  2045. if (isset($condition['keyword']) && !empty($condition['keyword'])){
  2046. $searchResult = self::searchByKeyword($condition['keyword']);
  2047. if (!$searchResult->isSuccess()){
  2048. return ResultWrapper::fail($searchResult->getData(),$searchResult->getErrorCode());
  2049. }
  2050. $searchData = $searchResult->getData();
  2051. $total = $searchData['total'];
  2052. $goodsData = $searchData['data'];
  2053. if (empty($goodsData)){
  2054. return ResultWrapper::success(['data' => [], 'total' => 0]);
  2055. }
  2056. }
  2057. }
  2058. // 商品数据/最高最低价格
  2059. $formatGoodsRes = self::formatGoodsData($goodsData,$shopData['shopIdBindWarehouseId']);
  2060. if (!$formatGoodsRes->isSuccess()){
  2061. return ResultWrapper::fail($formatGoodsRes->getData(),$formatGoodsRes->getErrorCode());
  2062. }
  2063. $goodsData = $formatGoodsRes->getData();
  2064. $return = [
  2065. 'data' => self::formatJsonGoods($goodsData),
  2066. 'total' => $total,
  2067. ];
  2068. }else{
  2069. $searchResult = self::searchManage($selectParams,$export);
  2070. if (!$searchResult->isSuccess()){
  2071. return ResultWrapper::fail($searchResult->getData(),$searchResult->getErrorCode());
  2072. }
  2073. $return = $searchResult->getData();
  2074. }
  2075. return ResultWrapper::success($return);
  2076. }
  2077. /**
  2078. * Doc: (des="")
  2079. * User: XMing
  2080. * Date: 2021/1/23
  2081. * Time: 3:57 下午
  2082. * @param array $selectParams
  2083. * @param bool $export
  2084. * @return ResultWrapper
  2085. * @throws Exception
  2086. */
  2087. public function searchManage(array $selectParams,$export = false): ResultWrapper
  2088. {
  2089. $goodsResult = self::getGoodsBySelectParams($selectParams,true);
  2090. if (!$goodsResult->isSuccess()){
  2091. return ResultWrapper::fail($goodsResult->getData(),$goodsResult->getErrorCode());
  2092. }
  2093. $goodsResult = $goodsResult->getData();
  2094. $total = $goodsResult['total'];
  2095. $goodsData = $goodsResult['data'];
  2096. if($export === true){
  2097. $result = self::exportGoods($selectParams,'db');
  2098. if(!$result->isSuccess()){
  2099. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  2100. }
  2101. die;
  2102. }
  2103. if (empty($goodsData)){
  2104. return ResultWrapper::success(['data' => [], 'total' => 0]);
  2105. }
  2106. $formatResult = self::formatManageGoodsLists($goodsData);
  2107. if (!$formatResult->isSuccess()){
  2108. return ResultWrapper::fail($formatResult->getData(),$formatResult->getErrorCode());
  2109. }
  2110. $return = [
  2111. 'data' => $formatResult->getData(),
  2112. 'total' => ($total) ? intval($total) : 0,
  2113. ];
  2114. return ResultWrapper::success($return);
  2115. }
  2116. /**
  2117. * 统一的格式化商品列表数据
  2118. * @param array $goodsData 商品主数据
  2119. * @return ResultWrapper
  2120. */
  2121. private function formatManageGoodsLists(array $goodsData)
  2122. {
  2123. if (empty($goodsData)){
  2124. return ResultWrapper::success([]);
  2125. }
  2126. $basicGoodsIds = []; // 所有商品资料ids
  2127. $shopIds = []; // 店铺ids
  2128. $merchantIds = []; // 商户ids
  2129. $selectInventorParams = []; // 查询库存
  2130. foreach($goodsData as $key => $value){
  2131. $basicGoodsIds[] = $value['basicGoodsId'];
  2132. $shopIds[] = $value['shopId'];
  2133. $merchantIds[] = $value['merchantId'];
  2134. $selectInventorParams[$value['shopId']][] = $value['basicGoodsId'];
  2135. }
  2136. // 获取商品库数据
  2137. $basicGodsBind = [];
  2138. $materielResult = $this->objMGoodsBasic->getGoodsBasicNames(array_unique($basicGoodsIds));
  2139. if (!$materielResult->isSuccess()){
  2140. return ResultWrapper::fail($materielResult->getData(),$materielResult->getErrorCode());
  2141. }
  2142. $materielMap = $materielResult->getData();
  2143. if(!empty($materielMap)){
  2144. $materielMapResult = $this->objMGoodsBasic->formatMaterielLists($materielMap);
  2145. if (!$materielMapResult->isSuccess()){
  2146. return ResultWrapper::fail($materielMapResult->getData(),$materielMapResult->getErrorCode());
  2147. }
  2148. $materielMap = $materielMapResult->getData();
  2149. foreach($materielMap as $key => $value){
  2150. $basicGodsBind[$value['id']] = $value;
  2151. }
  2152. }
  2153. // 店铺数据映射
  2154. $shopMap = [];
  2155. if (!empty($shopIds)){
  2156. $shopResult = $this->objMShop->getShopByIds(array_unique($shopIds));
  2157. if (!$shopResult->isSuccess()){
  2158. return ResultWrapper::fail($shopResult->getData(),$shopResult->getErrorCode());
  2159. }
  2160. $shopData = $shopResult->getData();
  2161. if(!empty($shopData)){
  2162. foreach($shopData as $key => $value){
  2163. $shopMap[$value['id']] = $value;
  2164. }
  2165. }
  2166. }
  2167. // 商户数据映射
  2168. $merchantIds = array_filter($merchantIds);
  2169. if(!empty($merchantIds)){
  2170. $objMMerchant = new MMerchant($this->enterpriseId);
  2171. $merchantData = $objMMerchant->getMerchant($merchantIds);
  2172. }
  2173. //查询库存
  2174. $inventoryMap = [];
  2175. foreach ($selectInventorParams as $shopId => $materielIds){
  2176. $inventoryResult = $this->objMInventory->getInventoryByShopIdAndMaterielIds($shopId,$materielIds);
  2177. if (!$inventoryResult->isSuccess()){
  2178. return ResultWrapper::fail($inventoryResult->getData(),$inventoryResult->getErrorCode());
  2179. }
  2180. $inventoryMap[$shopId] = $inventoryResult->getData();
  2181. }
  2182. $objGoodsBasicRelevantCache = new GoodsBasicRelevant($this->enterpriseId);
  2183. // 统一拿商品资料数据去覆盖商品列表的字段
  2184. foreach($goodsData as $key => $value){
  2185. $goodsData[$key]['code'] = createCode(StatusCode::$code['goodsBasic']['prefix'], $value['basicGoodsId'], StatusCode::$code['goodsBasic']['length']);
  2186. $goodsData[$key]['images'] = isset($basicGodsBind[$value['basicGoodsId']]['images']) ? json_decode($basicGodsBind[$value['basicGoodsId']]['images'], true) : [];
  2187. $goodsData[$key]['title'] = $basicGodsBind[$value['basicGoodsId']]['title'];
  2188. $goodsData[$key]['barCode'] = $basicGodsBind[$value['basicGoodsId']]['barCode'];
  2189. $goodsData[$key]['unitNameMaster'] = $basicGodsBind[$value['basicGoodsId']]['unitNameMaster'];
  2190. $goodsData[$key]['isEq'] = $basicGodsBind[$value['basicGoodsId']]['isEq'];
  2191. $goodsData[$key]['specType'] = $basicGodsBind[$value['basicGoodsId']]['specType'];
  2192. $goodsData[$key]['specGroup'] = $basicGodsBind[$value['basicGoodsId']]['specGroup'];
  2193. $goodsData[$key]['specTotal'] = $basicGodsBind[$value['basicGoodsId']]['specTotal'];
  2194. $goodsData[$key]['categoryName'] = $basicGodsBind[$value['basicGoodsId']]['categoryTitle'];
  2195. $goodsData[$key]['categoryId'] = $basicGodsBind[$value['basicGoodsId']]['categoryId'];
  2196. $goodsData[$key]['categoryPath'] = $basicGodsBind[$value['basicGoodsId']]['categoryPath'];
  2197. $goodsData[$key]['assistCategoryName'] = $basicGodsBind[$value['basicGoodsId']]['assistCategoryTitle'];
  2198. $goodsData[$key]['assistCategoryId'] = $basicGodsBind[$value['basicGoodsId']]['assistCategoryId'];
  2199. $goodsData[$key]['assistCategoryPath'] = $basicGodsBind[$value['basicGoodsId']]['assistCategoryPath'];
  2200. $goodsData[$key]['tag'] = explode(',', getArrayItem($basicGodsBind[$value['basicGoodsId']],'tag',''));
  2201. $goodsData[$key]['brandId'] = $basicGodsBind[$value['basicGoodsId']]['brandId'];
  2202. $goodsData[$key]['brandName'] = $basicGodsBind[$value['basicGoodsId']]['brandTitle'];
  2203. $goodsData[$key]['shopName'] = isset($shopMap[$value['shopId']]) ? $shopMap[$value['shopId']]['name'] : '';
  2204. $goodsData[$key]['inventorTotal'] = isset($inventoryMap[$value['shopId']][$value['basicGoodsId']]['num']) ? $inventoryMap[$value['shopId']][$value['basicGoodsId']]['num'] : 0;
  2205. $goodsData[$key]['goodsGroups'] = !empty($value['goodsGroups']) ? explode(',',$value['goodsGroups']) : [];
  2206. $goodsData[$key]['support'] = !empty($value['support']) ? explode(',',$value['support']) : [];
  2207. if(isset($value['merchantId'])){
  2208. $goodsData[$key]['merchantName'] = isset($merchantData[$value['merchantId']]) ? $merchantData[$value['merchantId']]['name'] : '';
  2209. }
  2210. // 对详情反转义
  2211. if (isset($value['content'])) {
  2212. $goodsData[$key]['content'] = htmlspecialchars_decode($value['content'], ENT_QUOTES);
  2213. }
  2214. $goodsData[$key]['oldSalesNum'] = getArrayItem($value,'salesNum',[]);
  2215. $salesNumArray = (array)json_decode(getArrayItem($value,'salesNum',''),true);
  2216. $goodsData[$key]['salesNum'] = array_sum(array_column($salesNumArray,'salesNum'));
  2217. }
  2218. return ResultWrapper::success($goodsData);
  2219. }
  2220. /**
  2221. * 商品详情拼接数据
  2222. * @param $data
  2223. * @return mixed
  2224. * @throws Exception
  2225. */
  2226. public function formatDetails(array $data): ResultWrapper
  2227. {
  2228. if (empty($data)) {
  2229. return ResultWrapper::success([]);
  2230. }
  2231. $formatMultipleResult = self::formatMultipleSkuMapping($data, StatusCode::$standard);//处理属性
  2232. if (!$formatMultipleResult->isSuccess()){
  2233. return ResultWrapper::fail($formatMultipleResult->getData(),$formatMultipleResult->getErrorCode());
  2234. }
  2235. $formatMultiple = $formatMultipleResult->getData();
  2236. $initDetails = self::formatInitSpec($formatMultiple);//初始化数据
  2237. $formatInventoryResult = self::formatInventorySpec($initDetails);//查询库存
  2238. if (!$formatInventoryResult->isSuccess()){
  2239. return ResultWrapper::fail($formatInventoryResult->getData(),$formatInventoryResult->getErrorCode());
  2240. }
  2241. $formatInventory = $formatInventoryResult->getData();
  2242. // 处理价格
  2243. $priceResult = self::getPriceBySkuSpec([$formatInventory]);
  2244. if (!$priceResult->isSuccess()){
  2245. return ResultWrapper::fail($priceResult->getData(),$priceResult->getErrorCode());
  2246. }
  2247. $priceData = $priceResult->getData();
  2248. $data = self::getSalesNumDetails($priceData);//拼接销量(详情)
  2249. $data = self::formatGoodsSpec($data);//提取字段
  2250. $data = array_shift($data);
  2251. return ResultWrapper::success($data);
  2252. }
  2253. /**
  2254. * Doc: (des="格式化客户屏蔽列表")
  2255. * User: XMing
  2256. * Date: 2021/1/15
  2257. * Time: 5:07 下午
  2258. * @return ResultWrapper
  2259. * @throws Exception
  2260. */
  2261. public function formatNotCustomer($data): ResultWrapper
  2262. {
  2263. if (empty($data)){
  2264. return ResultWrapper::success($data);
  2265. }
  2266. $data['notCustomerLists'] = [];
  2267. if ($data['isShield'] == StatusCode::$standard){
  2268. $notCustomer = getArrayItem($data,'notCustomer','');
  2269. if (empty($notCustomer)){
  2270. $data['notCustomerLists'] = [];
  2271. return ResultWrapper::success($data);
  2272. }
  2273. $notCustomerArr = explode(',',$notCustomer);
  2274. $objMCustomer = new MCustomer($this->enterpriseId,$this->userCenterId);
  2275. $customerListsResult = $objMCustomer->getCustomerByIds($notCustomerArr);
  2276. if (!$customerListsResult->isSuccess()){
  2277. return ResultWrapper::fail($customerListsResult->getData(),$customerListsResult->getErrorCode());
  2278. }
  2279. $customerLists = $customerListsResult->getData();
  2280. foreach ($customerLists as $val){
  2281. $data['notCustomerLists'][] = [
  2282. 'id' => $val['id'],
  2283. 'code' => $val['code'],
  2284. 'customerType' => $val['customerType'],
  2285. 'mobile' => $val['mobile'],
  2286. 'name' => $val['name'],
  2287. 'status' => $val['status'],
  2288. 'shopName' => $val['shopName']
  2289. ];
  2290. }
  2291. }
  2292. return ResultWrapper::success($data);
  2293. }
  2294. /**
  2295. * Doc: (des="")
  2296. * User: XMing
  2297. * Date: 2020/9/10
  2298. * Time: 11:55 上午
  2299. * @throws Exception
  2300. */
  2301. private function formatGoodsExpress(array $data)
  2302. {
  2303. $data['express'] = [
  2304. 'expressType' => $data['expressType'],
  2305. 'ruleId' => $data['ruleId'],
  2306. 'expressFee' => $data['expressFee'],
  2307. 'showExpress' => $data['showExpress'],
  2308. 'ruleData' => [
  2309. 'not_dispatch_areas' => (object)[]
  2310. ],
  2311. ];
  2312. $ruleInfo = [];
  2313. if ($data['expressType'] == StatusCode::$expressType['rule']){
  2314. $objMDeliverySetting = new MDeliverySetting($this->userCenterId,$this->enterpriseId);
  2315. $ruleInfoResult = $objMDeliverySetting->getRuleInfo($data['express']['ruleId']);
  2316. if (!$ruleInfoResult->isSuccess()){
  2317. return ResultWrapper::fail($ruleInfoResult->getData(),$ruleInfoResult->getErrorCode());
  2318. }
  2319. $ruleInfo = $ruleInfoResult->getData();
  2320. }
  2321. $data['express']['ruleData']['not_dispatch_areas'] = isset($ruleInfo['setData']) ? $ruleInfo['setData']['not_dispatch_areas'] : (object)[];
  2322. return ResultWrapper::success($data);
  2323. }
  2324. /**
  2325. * @param $data
  2326. * @param $isAddGoods
  2327. * @return mixed
  2328. * @throws \Exception
  2329. */
  2330. public function formatMultipleSkuMapping($data, $isAddGoods = 4): ResultWrapper
  2331. {
  2332. switch ($isAddGoods) {
  2333. case StatusCode::$standard:
  2334. $sql = "SELECT id,unitId,unitName,conversion,isDefault,isMaster,specGroupHash,specImage,specData,barCode,weight,isNew FROM qianniao_sku_" . $this->enterpriseId . " WHERE goodsId=" . $data['basicGoodsId'] . " AND deleteStatus=" . StatusCode::$standard;
  2335. break;
  2336. case StatusCode::$delete:
  2337. $sql = "SELECT id,unitId,unitName,conversion,isDefault,isMaster,specGroupHash,specImage,specData,barCode,weight,isNew FROM qianniao_sku_" . $this->enterpriseId . " WHERE goodsId=" . $data['basicGoodsId'] . " AND deleteStatus=" . StatusCode::$standard . " AND isMaster=" . StatusCode::$standard . " GROUP BY specGroupHash";
  2338. break;
  2339. }
  2340. $skuResult = $this->objMSku->getQuery($sql);
  2341. if (!$skuResult->isSuccess()){
  2342. return ResultWrapper::fail($skuResult->getData(),$skuResult->getErrorCode());
  2343. }
  2344. $skuData = $skuResult->getData();
  2345. $specMultiple = [];
  2346. foreach ($skuData as $spec) {
  2347. $specGroup = json_decode($spec['specData'], true);
  2348. $specMultiple[] = [
  2349. 'id' => $spec['id'],
  2350. 'unitId' => $spec['unitId'],
  2351. 'unitName' => $spec['unitName'],
  2352. 'isMaster' => $spec['isMaster'],
  2353. 'conversion' => $spec['conversion'],
  2354. 'specGroup' => empty($specGroup) ? [] : $specGroup,
  2355. 'specGroupHash' => $spec['specGroupHash'],
  2356. 'specImage' => empty(json_decode($spec['specImage'], true)) ? '' : json_decode($spec['specImage'], true),
  2357. 'barCode' => empty($spec['barCode']) ? '' : $spec['barCode'],
  2358. 'isDefault' => $spec['isDefault'],
  2359. 'weight' => $spec['weight'],
  2360. 'isNew' => $spec['isNew']
  2361. ];
  2362. }
  2363. $data['specMultiple'] = $specMultiple;
  2364. return ResultWrapper::success($data);
  2365. }
  2366. /**
  2367. * 查询基础商品对应规格数量
  2368. * @param $data
  2369. * @return array
  2370. */
  2371. public function getSpecTotal(array $data): ResultWrapper
  2372. {
  2373. $goodsIds = array_values(array_unique(array_column($data, 'basicGoodsId')));
  2374. $goodsIds = trim(implode(',', $goodsIds), ',');
  2375. if (empty($goodsIds)){
  2376. return ResultWrapper::success([]);
  2377. }
  2378. $sql = 'SELECT COUNT(id) as total,goodsId FROM qianniao_sku_' . $this->enterpriseId . ' WHERE deleteStatus = ' . StatusCode::$standard . ' AND goodsId IN(' . $goodsIds . ') GROUP BY goodsId;';
  2379. $dbResult = $this->objMSku->getQuery($sql);
  2380. if (!$dbResult->isSuccess()) {
  2381. Logger::logs(E_USER_ERROR,'sql error',__CLASS__,__LINE__,$dbResult->getData());
  2382. }
  2383. $allSpec = $dbResult->getData();
  2384. $mapping = [];
  2385. foreach ($allSpec as $spec) {
  2386. $mapping[$spec['goodsId']] = $spec['total'];
  2387. }
  2388. return ResultWrapper::success($mapping);
  2389. }
  2390. /**
  2391. * @param $data
  2392. * @return mixed
  2393. */
  2394. public function formatGoodsSpec($data)
  2395. {
  2396. if (empty($data)) {
  2397. return $data;
  2398. }
  2399. $objMSku = new MSku($this->userCenterId,$this->enterpriseId);
  2400. foreach ($data as &$details) {
  2401. $details['minSalePrice'] = empty($this->commonMapping[$details['id']]) ? 0 : min($this->commonMapping[$details['id']]);
  2402. $details['maxSalePrice'] = empty($this->commonMapping[$details['id']]) ? 0 : max($this->commonMapping[$details['id']]);
  2403. unset($details['oldSalesNum']);
  2404. if (isset($details['specGroup']) && !empty($details['specGroup'])) {
  2405. $details['specGroup'] = is_array($details['specGroup']) ? $details['specGroup'] : json_decode($details['specGroup'], true);
  2406. }
  2407. //生成规格唯一索引
  2408. $specMultiple = $details['specMultiple'];
  2409. if (empty($specMultiple)) {
  2410. continue;
  2411. }
  2412. if (!isset($details['salesNum']) || !is_numeric($details['salesNum'])){
  2413. $details['salesNum'] = 0;
  2414. }
  2415. $details['salePrice'] = isset($specMultiple[0]['salePrice']) ? $specMultiple[0]['salePrice'] : 0;
  2416. $details['setNum'] = isset($specMultiple[0]['setNum']) ? $specMultiple[0]['setNum'] : 0;
  2417. $details['inventory'] = isset($specMultiple[0]['inventory']) ? $specMultiple[0]['inventory'] : 0;
  2418. $details['unitName'] = isset($specMultiple[0]['unitName']) ? $specMultiple[0]['unitName'] : 0;
  2419. if(!isset($details['marketPrice'])){
  2420. $details['marketPrice'] = isset($specMultiple[0]['marketPrice']) ? $specMultiple[0]['marketPrice'] : 0;
  2421. }
  2422. foreach ($specMultiple as &$spec) {
  2423. $index = $details['id'] . ':' . $spec['unitId'];//商品ID
  2424. //$subName = '';
  2425. if (!empty($spec['specGroup'])) {
  2426. foreach ($spec['specGroup'] as $row) {
  2427. $index .= empty($index) ? $row['specValueId'] : ':' . $row['specValueId'];
  2428. }
  2429. }
  2430. $spec['index'] = $index;
  2431. $spec['indexGroup'] = explode(':', $index);
  2432. //$spec['subName'] = $subName;
  2433. }
  2434. $details['specMultiple'] = $specMultiple;
  2435. $details['notArea'] = empty($details['notArea']) ? [] : json_decode($details['notArea'],true);
  2436. $sql = "SELECT id,barCode,unitId,unitName,conversion,isMaster,barCode,specType,isDefault,`weight`,isNew FROM qianniao_sku_" . $this->enterpriseId . " WHERE goodsId=" . $details['basicGoodsId'] . " AND deleteStatus=" . StatusCode::$standard . " ORDER BY isMaster DESC";
  2437. $skuResult = $objMSku->getQuery($sql);
  2438. $skuData = [];
  2439. if ($skuResult->isSuccess()) {
  2440. $skuData = $skuResult->getData();
  2441. $haveMaster = 0;
  2442. $newSkuData = [];
  2443. $noMasterUnidIds = [];
  2444. foreach ($skuData as $key => &$value) {
  2445. if ($value['specType'] == StatusCode::$specType['multiple']) {
  2446. $value['barCode'] = '';
  2447. }
  2448. if ($value['isMaster'] == StatusCode::$standard && $haveMaster == 0) {
  2449. $newSkuData[] = $value;
  2450. $haveMaster = 1;
  2451. }
  2452. if ($value['isMaster'] == StatusCode::$delete && !in_array($value['unitId'], $noMasterUnidIds)) {
  2453. $newSkuData[] = $value;
  2454. $noMasterUnidIds[] = $value['unitId'];
  2455. }
  2456. }
  2457. unset($skuData);
  2458. $skuData = $newSkuData;
  2459. }
  2460. $details['unitData'] = $skuData;
  2461. }
  2462. return $data;
  2463. }
  2464. /**
  2465. * Doc: (des="初始化规格数据")
  2466. * User: XMing
  2467. * Date: 2021/3/4
  2468. * Time: 5:24 下午
  2469. * @param $data
  2470. * @return mixed
  2471. */
  2472. public static function formatInitSpec($data)
  2473. {
  2474. if (empty($data)) {
  2475. return $data;
  2476. }
  2477. $specMultiple = $data['specMultiple'];
  2478. if (empty($specMultiple)){
  2479. return $data;
  2480. }
  2481. //初始化
  2482. $data['minSalePrice'] = 0;//最低价格
  2483. $data['maxSalePrice'] = 0;//最高价
  2484. foreach ($specMultiple as &$spec) {
  2485. $spec['index'] = '';//唯一索引
  2486. $spec['salePrice'] = 0;//销售价格
  2487. $spec['inventory'] = 0;//库存
  2488. $spec['ladderPrice'] = [];//阶梯价格
  2489. $spec['marketPrice'] = 0;//市场价格
  2490. $spec['enabledLadder'] = '';//是否开启阶梯价格
  2491. $spec['setNum'] = 0;//起定数量
  2492. $spec['isActivityPrice'] = StatusCode::$delete;//初始化,是否享受活动价格
  2493. $spec['limitNum'] = 0;//活动商品限购数量
  2494. $spec['originPrice'] = 0;//商品原价
  2495. $spec['costPrice'] = 0;//商品原价
  2496. $spec['salesNum'] = 0;//规格销售量
  2497. $spec['activity'] = [];//活动信息
  2498. }
  2499. $data['specMultiple'] = $specMultiple;
  2500. return $data;
  2501. }
  2502. /**
  2503. * Doc: (des="获取库存")
  2504. * User: XMing
  2505. * Date: 2021/3/4
  2506. * Time: 5:36 下午
  2507. * @param array $data
  2508. * @return ResultWrapper
  2509. * @throws Exception
  2510. */
  2511. private function formatInventorySpec(array $data): ResultWrapper
  2512. {
  2513. if (empty($data)){
  2514. return ResultWrapper::success($data);
  2515. }
  2516. $shopId = getArrayItem($data,'shopId',0);
  2517. $specMultiple = getArrayItem($data,'specMultiple',[]);
  2518. $skuIds = [];
  2519. foreach ($specMultiple as $item){
  2520. if (!in_array($item['id'],$skuIds)){
  2521. $skuIds[] = $item['id'];
  2522. }
  2523. }
  2524. $this->objMInventory = new MInventory($this->enterpriseId, $this->userCenterId);
  2525. $inventoryResult = $this->objMInventory->getInventoryByShopIdAndSkuIds($shopId,$skuIds);
  2526. if (!$inventoryResult->isSuccess()){
  2527. return ResultWrapper::fail($inventoryResult->getData(),$inventoryResult->getErrorCode());
  2528. }
  2529. $inventoryMap = $inventoryResult->getData();
  2530. foreach ($specMultiple as &$item){
  2531. $row = getArrayItem($inventoryMap,$item['id'],[]);
  2532. $item['inventory'] = getArrayItem($row,'num',0);
  2533. $item['otherNum'] = getArrayItem($row,'otherNum',0);
  2534. }
  2535. $data['specMultiple'] = $specMultiple;
  2536. return ResultWrapper::success($data);
  2537. }
  2538. /**
  2539. * @param $data
  2540. * @return mixed
  2541. * @throws Exception
  2542. */
  2543. public function getSkuInventorySpec($data): ResultWrapper
  2544. {
  2545. if (empty($data)) {
  2546. return ResultWrapper::success($data);
  2547. }
  2548. $selectParams = [];
  2549. foreach ($data as $item){
  2550. $selectParams[$item['shopId']][] = $item['basicGoodsId'];
  2551. }
  2552. $this->objMInventory = new MInventory($this->enterpriseId, '');
  2553. $inventoryMap = [];
  2554. foreach ($selectParams as $shopId => $materielIds){
  2555. $inventoryResult = $this->objMInventory->getInventoryByShopIdAndMaterielIds($shopId,$materielIds);
  2556. if (!$inventoryResult->isSuccess()) {
  2557. return ResultWrapper::fail($inventoryResult->getData(),$inventoryResult->getErrorCode());
  2558. }
  2559. $inventoryMap[$shopId] = $inventoryResult->getData();//库存数据
  2560. }
  2561. foreach ($data as &$goods) {
  2562. if (!isset($goods['specType'])){
  2563. //老数据es中可能没有
  2564. $goods['specType'] = StatusCode::$specType['single'];
  2565. }
  2566. $goods['inventorTotal'] = isset($inventoryMap[$goods['shopId']][$goods['basicGoodsId']]['num']) ? $inventoryMap[$goods['shopId']][$goods['basicGoodsId']]['num'] : 0;//当前商品规格总库存
  2567. $goods['inventorTotal'] = floatval($goods['inventorTotal']);
  2568. if (!isset($goods['specMultiple'])) {
  2569. continue;
  2570. }
  2571. $specMultiple = $goods['specMultiple'];
  2572. $skuIds = [];
  2573. foreach ($specMultiple as $spec) {
  2574. $skuIds[] = $spec['skuId'];
  2575. }
  2576. unset($spec);
  2577. $inventoryResult = $this->objMInventory->getInventoryByShopIdAndSkuIds($goods['shopId'],$skuIds);
  2578. if (!$inventoryResult->isSuccess()){
  2579. return ResultWrapper::fail($inventoryResult->getData(),$inventoryResult->getErrorCode());
  2580. }
  2581. $inventoryData = $inventoryResult->getData();
  2582. foreach ($specMultiple as &$spec) {
  2583. $spec['inventory'] = isset($inventoryData[$spec['skuId']]) ? $inventoryData[$spec['skuId']]['num'] : 0;
  2584. }
  2585. $goods['specMultiple'] = $specMultiple;
  2586. }
  2587. return ResultWrapper::success($data);
  2588. }
  2589. /**
  2590. * 单位属性换算
  2591. * @param $spec
  2592. * @param $specType
  2593. * @param $inventoryMapping
  2594. * @param $goods
  2595. * @return mixed
  2596. */
  2597. public function formatConversion($spec, $specType, $inventoryMapping, $goods)
  2598. {
  2599. if (empty($spec)) {
  2600. return $spec;
  2601. }
  2602. //当权商品库存数据
  2603. $thisSpec = isset($inventoryMapping[$goods['basicGoodsId']]['skuData']) ? $inventoryMapping[$goods['basicGoodsId']]['skuData'] : [];
  2604. switch ($specType) {
  2605. case StatusCode::$specType['single']:
  2606. //取出主单位skuId
  2607. $masterSkuId = '';
  2608. foreach ($goods['unitData'] as $value){
  2609. if ($value['isMaster'] == StatusCode::$standard){
  2610. $masterSkuId = $value['id'];
  2611. }
  2612. }
  2613. if ($spec['isNew'] == StatusCode::$standard){
  2614. //新
  2615. //主换辅 除
  2616. //无属性的主单位有采购记录不可编辑,辅助单位可比编辑 4=>有记录 5=>无记录
  2617. if ($spec['isMaster'] == StatusCode::$standard) {
  2618. $spec['isEditSpec'] = isset($thisSpec[$masterSkuId]) ? StatusCode::$delete : StatusCode::$standard;
  2619. $spec['inventory'] = isset($thisSpec[$masterSkuId]) ? $thisSpec[$masterSkuId]['inventoryNum'] : 0;//库存
  2620. } else {
  2621. $spec['isEditSpec'] = StatusCode::$delete;
  2622. $spec['inventory'] = isset($thisSpec[$masterSkuId]) ? bcdiv($thisSpec[$masterSkuId]['inventoryNum'], $spec['conversion'], 8) : 0;//转换库存
  2623. }
  2624. }else{
  2625. //旧
  2626. //主换辅 乘
  2627. if ($spec['isMaster'] == StatusCode::$standard) {
  2628. $spec['isEditSpec'] = isset($thisSpec[$masterSkuId]) ? StatusCode::$delete : StatusCode::$standard;
  2629. $spec['inventory'] = isset($thisSpec[$masterSkuId]) ? $thisSpec[$masterSkuId]['inventoryNum'] : 0;//库存
  2630. } else {
  2631. $spec['isEditSpec'] = StatusCode::$delete;
  2632. $spec['inventory'] = isset($thisSpec[$masterSkuId]) ? bcmul($thisSpec[$masterSkuId]['inventoryNum'], $spec['conversion'], 8) : 0;//转换库存
  2633. }
  2634. }
  2635. $spec['costPrice'] = isset($thisSpec[$spec['id']]['costPrice']) ? $thisSpec[$spec['id']]['costPrice'] : 0;//成本单价
  2636. break;
  2637. case StatusCode::$specType['multiple']:
  2638. //有属性 同属性主辅换算
  2639. //有属性,主 4=>有记录 5=>无记录
  2640. if ($spec['isNew'] == StatusCode::$standard){
  2641. //新
  2642. //主换辅 除
  2643. if ($spec['isMaster'] == StatusCode::$standard) {
  2644. $spec['isEditSpec'] = isset($thisSpec[$spec['id']]) ? StatusCode::$delete : StatusCode::$standard;
  2645. $spec['inventory'] = isset($thisSpec[$spec['id']]) ? $thisSpec[$spec['id']]['inventoryNum'] : 0;//库存
  2646. $this->allMappingSpec[$spec['specGroupHash']] = $spec;//保存属性组
  2647. } else {
  2648. $spec['isEditSpec'] = isset($this->allMappingSpec[$spec['specGroupHash']]) ? $this->allMappingSpec[$spec['specGroupHash']]['isEditSpec'] : StatusCode::$delete;
  2649. $spec['inventory'] = isset($this->allMappingSpec[$spec['specGroupHash']]) ? bcdiv($this->allMappingSpec[$spec['specGroupHash']]['inventory'], $spec['conversion'], 8) : 0;
  2650. }
  2651. }else{
  2652. if ($spec['isMaster'] == StatusCode::$standard) {
  2653. $spec['isEditSpec'] = isset($thisSpec[$spec['id']]) ? StatusCode::$delete : StatusCode::$standard;
  2654. $spec['inventory'] = isset($thisSpec[$spec['id']]) ? $thisSpec[$spec['id']]['inventoryNum'] : 0;//库存
  2655. $this->allMappingSpec[$spec['specGroupHash']] = $spec;//保存属性组
  2656. } else {
  2657. $spec['isEditSpec'] = isset($this->allMappingSpec[$spec['specGroupHash']]) ? $this->allMappingSpec[$spec['specGroupHash']]['isEditSpec'] : StatusCode::$delete;
  2658. $spec['inventory'] = isset($this->allMappingSpec[$spec['specGroupHash']]) ? bcmul($this->allMappingSpec[$spec['specGroupHash']]['inventory'], $spec['conversion'], 8) : 0;
  2659. }
  2660. }
  2661. $spec['costPrice'] = isset($thisSpec[$spec['id']]['costPrice']) ? $thisSpec[$spec['id']]['costPrice'] : 0;//成本单价
  2662. break;
  2663. }
  2664. $spec['inventory'] = floor($spec['inventory']);
  2665. return $spec;
  2666. }
  2667. /**
  2668. * spec salesNum
  2669. * @param $data
  2670. * @param array $allGoodsIds
  2671. * @return mixed
  2672. * @throws Exception
  2673. */
  2674. public function getSalesNumBySkuSpec($data, $allGoodsIds = [])
  2675. {
  2676. $cartData = [];
  2677. if (!empty($allGoodsIds)) {
  2678. $objMCart = new MCart($this->userCenterId, $this->enterpriseId);
  2679. $cartData = $objMCart->getCartNumByGoodsId($allGoodsIds);
  2680. }
  2681. foreach ($data as &$goods) {
  2682. //[{"skuId":1,"salesNum":10},{"skuId":2,"salesNum":10}] salesNum
  2683. $salesNumArr = [];
  2684. if (!empty($goods['salesNum'])) {
  2685. $salesNumData = is_string($goods['salesNum']) ? json_decode($goods['salesNum'], true) : $goods['salesNum'];
  2686. $goods['oldSalesNum'] = $salesNumData;//复制旧数据
  2687. $goods['salesNum'] = array_sum(array_column($salesNumData, 'salesNum'));
  2688. if ($salesNumData) {
  2689. foreach ($salesNumData as $salesNum) {
  2690. $salesNumArr[$salesNum['skuId']] = $salesNum['salesNum'];
  2691. }
  2692. }
  2693. } else {
  2694. $goods['salesNum'] = 0;
  2695. }
  2696. if (isset($goods['specMultiple']) && !empty($goods['specMultiple'])) {
  2697. foreach ($goods['specMultiple'] as &$spec) {
  2698. $spec['salesNum'] = isset($salesNumArr[$spec['id']]) ? $salesNumArr[$spec['id']] : 0;//销量
  2699. $spec['cartNum'] = isset($cartData[$goods['id']][$spec['id']]) ? $cartData[$goods['id']][$spec['id']] : 0;//购物车数量
  2700. }
  2701. }
  2702. }
  2703. return $data;
  2704. }
  2705. /**
  2706. * Doc: (des="")
  2707. * User: XMing
  2708. * Date: 2021/3/4
  2709. * Time: 7:13 下午
  2710. * @param array $data
  2711. * @return ResultWrapper
  2712. */
  2713. public function getSalesNumDetails_new(array $data): ResultWrapper
  2714. {
  2715. if (empty($data)){
  2716. return ResultWrapper::success([]);
  2717. }
  2718. }
  2719. /**
  2720. * 格式化详情数据,销量
  2721. * @param $data
  2722. * @param array $allGoodsIds
  2723. * @return mixed
  2724. * @throws Exception
  2725. */
  2726. public function getSalesNumDetails($data, $allGoodsIds = [])
  2727. {
  2728. $cartData = [];
  2729. if (!empty($allGoodsIds)) {
  2730. $objMCart = new MCart($this->userCenterId, $this->enterpriseId);
  2731. $cartData = $objMCart->getCartNumByGoodsId($allGoodsIds);
  2732. }
  2733. foreach ($data as &$goods) {
  2734. $salesNumArr = [];
  2735. if (!empty($goods['oldSalesNum'])) {
  2736. $salesNumData = $goods['oldSalesNum'];
  2737. if (is_string($salesNumData)){
  2738. $salesNumData = json_decode($salesNumData,true);
  2739. }
  2740. unset($goods['oldSalesNum']);
  2741. if (is_array($salesNumData)) {
  2742. foreach ($salesNumData as $salesNum) {
  2743. $salesNumArr[$salesNum['skuId']] = $salesNum['salesNum'];
  2744. }
  2745. }
  2746. }
  2747. if (!empty($goods['salesNum'])){
  2748. $salesNumData = $goods['salesNum'];
  2749. if (is_string($salesNumData)){
  2750. $salesNumData = json_decode($salesNumData,true);
  2751. }
  2752. if (is_array($salesNumData)){
  2753. $goods['salesNum'] = array_sum(array_column($salesNumData,'salesNum'));
  2754. }else{
  2755. $goods['salesNum'] = 0;
  2756. }
  2757. if (is_array($salesNumData)) {
  2758. foreach ($salesNumData as $salesNum) {
  2759. $salesNumArr[$salesNum['skuId']] = $salesNum['salesNum'];
  2760. }
  2761. }
  2762. }
  2763. if (isset($goods['specMultiple']) && !empty($goods['specMultiple'])) {
  2764. foreach ($goods['specMultiple'] as &$spec) {
  2765. $spec['salesNum'] = isset($salesNumArr[$spec['id']]) ? $salesNumArr[$spec['id']] : 0;//销量
  2766. $spec['cartNum'] = isset($cartData[$goods['id']][$spec['id']]) ? $cartData[$goods['id']][$spec['id']] : 0;//购物车数量
  2767. }
  2768. }
  2769. if ($this->isFront){
  2770. $goods['salesNum'] = isset($goods['virtualSalesNum']) ? $goods['virtualSalesNum'] : 0;
  2771. }
  2772. }
  2773. return $data;
  2774. }
  2775. /**
  2776. * @param $data
  2777. * @return mixed
  2778. * @throws Exception
  2779. */
  2780. public function getPriceBySkuSpec($data): ResultWrapper
  2781. {
  2782. if (empty($data)) {
  2783. return ResultWrapper::success($data);
  2784. }
  2785. //查询价格参数
  2786. $allGoodsId = [];
  2787. $selectParam['material'] = [];
  2788. foreach ($data as $goods) {
  2789. $allGoodsId[] = $goods['id'];
  2790. $selectParam['material'][$goods['shopId']][] = $goods['id'];
  2791. }
  2792. if (!empty($this->useCustomerId)){
  2793. $selectParam['customerId'] = $this->useCustomerId;//客户有特殊调价
  2794. }
  2795. $priceResult = $this->objMPrice->getPrice($selectParam);
  2796. if (!$priceResult->isSuccess()) {
  2797. return ResultWrapper::fail($priceResult->getData(),$priceResult->getErrorCode());
  2798. }
  2799. $priceResult = $priceResult->getData();
  2800. $goodsPriceMapping = [];
  2801. foreach ($priceResult as $shopPriceArr) {
  2802. foreach ($shopPriceArr as $goodsId => $goodsSkuArr) {
  2803. $goodsPriceMapping[$goodsId] = $goodsSkuArr;
  2804. }
  2805. }
  2806. switch ($this->isFront) {
  2807. case true: // 前台
  2808. $objMCustomer = new MCustomer($this->enterpriseId, $this->userCenterId);
  2809. $objMActivity = new MActivity($this->userCenterId, $this->enterpriseId);
  2810. if (!empty($this->userCenterId)){
  2811. $customerResult = $objMCustomer->getCustomerInfoByUserCenterId($this->userCenterId);
  2812. if (!$customerResult->isSuccess()){
  2813. return ResultWrapper::fail($customerResult->getData(),$customerResult->getErrorCode());
  2814. }
  2815. $customer = $customerResult->getData();
  2816. }
  2817. $activityArr = [];
  2818. //获取促销活动价格
  2819. $dbResult = $objMActivity->getActivity([
  2820. 'goodsId' => implode(',', $allGoodsId),
  2821. 'customerType' => isset($customer['type']) ? $customer['type'] : 0,
  2822. ]);
  2823. $activityResult = $dbResult->getData();
  2824. unset($dbResult);
  2825. foreach ($activityResult as $activity) {
  2826. $activityArr[$activity['goodsId'] . $activity['skuId']] = $activity;
  2827. }
  2828. foreach ($data as &$goods) {
  2829. if (!isset($goodsPriceMapping[$goods['id']])) {
  2830. //当前商品的价格信息不存在
  2831. continue;
  2832. }
  2833. $goodsPriceInfo = $goodsPriceMapping[$goods['id']];//当前商品价格信息
  2834. $specMultiple = $goods['specMultiple'];//规格组
  2835. foreach ($specMultiple as &$spec) {
  2836. $bool = isset($activityArr[$goods['id'] . $spec['id']]);//是否具有活动价格
  2837. $spec["marketPrice"] = isset($goodsPriceInfo[$spec['id']]) ? $goodsPriceInfo[$spec['id']]['marketPrice'] : 0;//市场价格
  2838. $spec["originPrice"] = isset($goodsPriceInfo[$spec['id']]) ? $goodsPriceInfo[$spec['id']]['salePrice'] : 0;//原价
  2839. // 计算属性最小单位展示价格
  2840. if(isset($goodsPriceInfo[$spec['id']]['minUnitPriceConvert']) && !empty($goodsPriceInfo[$spec['id']]['minUnitPriceConvert'])){
  2841. $goodsPriceInfo[$spec['id']]['minUnitPrice'] = bcdiv($goodsPriceInfo[$spec['id']]['salePrice'], $goodsPriceInfo[$spec['id']]['minUnitPriceConvert'], 2);
  2842. }else{
  2843. $goodsPriceInfo[$spec['id']]['minUnitPrice'] = '0.00';
  2844. }
  2845. if ($bool) {
  2846. $activityDetails = $activityArr[$goods['id'] . $spec['id']];//活动详情
  2847. $spec['isActivity'] = StatusCode::$standard;//活动价格
  2848. //获取用户活动购买数量
  2849. if (!empty($this->userCenterId)) {
  2850. $userLimit = (int)$this->objActivityLimitCache->getLimit($activityDetails["activityId"], $goods['id'], $spec['id'], $this->userCenterId);
  2851. //活动商品用户限购数
  2852. $goodsLimit = (int)$activityDetails['limitNum'];
  2853. if ($userLimit < $goodsLimit) {
  2854. //此商品相对用户,满足活动价
  2855. $spec['isActivityPrice'] = StatusCode::$standard;//满足活动价
  2856. }
  2857. $spec['limitNum'] = $goodsLimit;
  2858. }
  2859. //具有活动价格
  2860. $spec["salePrice"] = $activityDetails['price'];//销售价格
  2861. $spec["minUnitPrice"] = $goodsPriceInfo[$spec['id']]['minUnitPrice'];//属性最小单位展示价
  2862. $spec["ladderPrice"] = [];//阶梯价格
  2863. $spec["enabledLadder"] = 0;//是否开启阶梯价阶梯
  2864. $spec["setNum"] = isset($goodsPriceInfo[$spec['id']]['setNum']) ? $goodsPriceInfo[$spec['id']]['setNum'] : 0;//起定数量
  2865. $spec["isSale"] = isset($goodsPriceInfo[$spec['id']]['isSale']) ? $goodsPriceInfo[$spec['id']]['isSale'] : 5;//起定数量
  2866. $spec['minUnitPriceConvert'] = getArrayItem($goodsPriceInfo[$spec['id']], 'minUnitPriceConvert', 0);
  2867. $spec['activity'] = [
  2868. 'activityId' => $activityDetails['activityId'],//活动ID
  2869. 'images' => empty($activityDetails['images']) ? [] : json_decode($activityDetails['images'],true),//活动图片
  2870. 'title' => $activityDetails['title'],//活动标题
  2871. 'startTime' => $activityDetails['startTime'],
  2872. 'endTime' => $activityDetails['endTime'],
  2873. 'activityType' => $activityDetails['activityType'],//活动类型
  2874. 'activityNum' => $activityDetails['activityNum'],//活动总数量
  2875. 'limitNum' => $activityDetails['limitNum'],//用户限购数量
  2876. 'salesPrice' => $activityDetails['price'],//活动价格
  2877. 'originPrice' => $spec['originPrice'],//商品原价
  2878. 'goodsActivityId' => $activityDetails['goodsActivityId'],//活动商品表id
  2879. ];//活动详情
  2880. } else {
  2881. if (!isset($goodsPriceInfo[$spec['id']])) {
  2882. //当前商品sku价格信息不存在
  2883. continue;
  2884. }
  2885. $spec["salePrice"] = $goodsPriceInfo[$spec['id']]['salePrice'];//销售价格
  2886. $spec["minUnitPrice"] = $goodsPriceInfo[$spec['id']]['minUnitPrice'];// 属性最小单位展示价
  2887. $spec["memberPrice"] = getArrayItem($goodsPriceInfo[$spec['id']], 'memberPrice', 0); // 会员价格
  2888. $spec["marketPrice"] = $goodsPriceInfo[$spec['id']]['marketPrice'];//市场价格
  2889. $spec["ladderPrice"] = $goodsPriceInfo[$spec['id']]['ladderPrice'];//阶梯价格
  2890. $spec["enabledLadder"] = $goodsPriceInfo[$spec['id']]['enabledLadder'];//是否开启阶梯价阶梯
  2891. $spec["setNum"] = isset($goodsPriceInfo[$spec['id']]['setNum']) ? $goodsPriceInfo[$spec['id']]['setNum'] : 0;//起定数量
  2892. $spec["isSale"] = isset($goodsPriceInfo[$spec['id']]['isSale']) ? $goodsPriceInfo[$spec['id']]['isSale'] : 5;//起定数量
  2893. $spec['minUnitPriceConvert'] = getArrayItem($goodsPriceInfo[$spec['id']], 'minUnitPriceConvert', 0);
  2894. $spec["isActivity"] = StatusCode::$delete;
  2895. }
  2896. $this->commonMapping[$goods['id']][] = $spec["salePrice"];//商品价格集合
  2897. }
  2898. $goods['specMultiple'] = $specMultiple;
  2899. }
  2900. break;
  2901. case false;
  2902. //后台
  2903. foreach ($data as &$goods) {
  2904. if (!isset($goodsPriceMapping[$goods['id']])) {
  2905. //当前商品的价格信息不存在
  2906. continue;
  2907. }
  2908. $goodsPriceInfo = $goodsPriceMapping[$goods['id']];//当前商品价格信息
  2909. $specMultiple = $goods['specMultiple'];//规格组
  2910. foreach ($specMultiple as &$spec) {
  2911. if (!isset($goodsPriceInfo[$spec['id']])) {
  2912. //当前商品sku价格信息不存在
  2913. continue;
  2914. }
  2915. $spec["salePrice"] = $goodsPriceInfo[$spec['id']]['salePrice'];//销售价格
  2916. $spec["memberPrice"] = getArrayItem($goodsPriceInfo[$spec['id']], 'memberPrice', 0); // 会员价格
  2917. $spec["marketPrice"] = $goodsPriceInfo[$spec['id']]['marketPrice'];//市场价格
  2918. $spec["originPrice"] = $goodsPriceInfo[$spec['id']]['salePrice'];//原价
  2919. $spec["ladderPrice"] = $goodsPriceInfo[$spec['id']]['ladderPrice'];//阶梯价格
  2920. $spec["enabledLadder"] = $goodsPriceInfo[$spec['id']]['enabledLadder'];//是否开启阶梯价阶梯
  2921. $spec["setNum"] = isset($goodsPriceInfo[$spec['id']]['setNum']) ? $goodsPriceInfo[$spec['id']]['setNum'] : 0;//起定数量
  2922. $spec["isSale"] = isset($goodsPriceInfo[$spec['id']]['isSale']) ? $goodsPriceInfo[$spec['id']]['isSale'] : 5;//起定数量
  2923. $spec['minUnitPriceConvert'] = getArrayItem($goodsPriceInfo[$spec['id']], 'minUnitPriceConvert', 0);
  2924. $spec["isActivity"] = StatusCode::$delete;
  2925. $this->commonMapping[$goods['id']][] = $spec["salePrice"];//商品价格集合
  2926. }
  2927. $goods['specMultiple'] = $specMultiple;
  2928. }
  2929. break;
  2930. }
  2931. return ResultWrapper::success($data);
  2932. }
  2933. /**
  2934. * @param $data
  2935. * @return mixed
  2936. */
  2937. public function getActivityPrice($data)
  2938. {
  2939. if (empty($data)) {
  2940. return [];
  2941. }
  2942. if (empty($this->customerType)) {
  2943. return [];
  2944. }
  2945. //获取促销活动价格
  2946. $dbResult = $this->objMActivity->getActivity([
  2947. 'goodsId' => implode(',', array_column($data, 'id')),
  2948. 'customerType' => $this->customerType,
  2949. ]);
  2950. $activityResult = $dbResult->getData();
  2951. unset($dbResult);
  2952. foreach ($activityResult as $activity) {
  2953. $activityArr[$activity['goodsId'] . $activity['skuId']] = $activity;
  2954. }
  2955. }
  2956. /**
  2957. * 拼接活动
  2958. * @param $goodsData
  2959. * @return mixed
  2960. * @throws Exception
  2961. */
  2962. private function getActivity($goodsData)
  2963. {
  2964. if (empty($goodsData)) return $goodsData;
  2965. $objMActivity = new MActivity($this->userCenterId, $this->enterpriseId);
  2966. $objMCustomer = new MCustomer($this->enterpriseId, $this->userCenterId);
  2967. $customer = $objMCustomer->getCustomerInfoByUserCenterId($this->userCenterId);
  2968. if (!$customer->isSuccess()) {
  2969. return $goodsData;
  2970. }
  2971. $customerType = $customer->getData()['type'];
  2972. $dbResult = $objMActivity->getActivity(['goodsId' => $goodsData['id'], 'customerType' => $customerType]);
  2973. if (!$dbResult->isSuccess()) {
  2974. return $goodsData;
  2975. }
  2976. $allActivity = [];
  2977. foreach ($dbResult->getData() as $activity) {
  2978. $allActivity[$activity['skuId']][] = $activity;
  2979. }
  2980. if (isset($goodsData['skuData'])){
  2981. foreach ($goodsData['skuData'] as &$sku) {
  2982. if (!isset($sku['skuId'])){
  2983. $sku['activity'] = (object)[];
  2984. continue;
  2985. }
  2986. $sku['activity'] = isset($allActivity[$sku['skuId']]) ? $allActivity[$sku['skuId']] : (object)[];
  2987. }
  2988. }
  2989. return $goodsData;
  2990. }
  2991. /**
  2992. * 通过区域获取销售店铺
  2993. * @param ?string $areaCode
  2994. * @return ResultWrapper
  2995. */
  2996. public function getShopIdsByArea(?string $areaCode = '')
  2997. {
  2998. $fields = 'id,warehouseId';
  2999. $sql = 'select '.$fields.' from qianniao_shop_1 where
  3000. enterpriseId = '.$this->enterpriseId;
  3001. if (!empty($areaCode)){
  3002. $sql .= " and (JSON_CONTAINS(salesArea->'$[*]', '\"".$areaCode."\"', '$') OR JSON_CONTAINS(salesArea->'$[*]','\"0-0-0\"','$'))";
  3003. }
  3004. $sql .= ' and warehouseId IS NOT NULL';
  3005. $result = $this->objDGoods->query($sql);
  3006. if ($result === false){
  3007. Logger::logs(E_USER_ERROR,'店铺查询失败',__CLASS__,__LINE__,$this->objDGoods->error());
  3008. return ResultWrapper::fail($this->objDGoods->error(),ErrorCode::$dberror);
  3009. }
  3010. //返回数据为空
  3011. if (empty($result)){
  3012. return ResultWrapper::success([]);
  3013. }
  3014. $shopIds = [];
  3015. $shopIdBindWarehouseId = [];
  3016. foreach ($result as $value){
  3017. $shopIds[] = $value['id'];
  3018. $shopIdBindWarehouseId[$value['id']] = $value['warehouseId'];
  3019. }
  3020. $map = [
  3021. 'shopIds' => $shopIds,
  3022. 'shopIdBindWarehouseId' => $shopIdBindWarehouseId
  3023. ];
  3024. return ResultWrapper::success($map);
  3025. }
  3026. /**
  3027. * 查询商品
  3028. * @param array $condition
  3029. * shopIds array 店铺ids
  3030. * keyword string 商品名称
  3031. * goodsId int 商品id
  3032. * categoryId int 分类id
  3033. * orderBy string 排序
  3034. * @param bool $getTotal
  3035. * @return ResultWrapper
  3036. */
  3037. public function getGoodsBySelectParams(array $condition = [],bool $getTotal = false): ResultWrapper
  3038. {
  3039. //数据总条数
  3040. $total = 0;
  3041. $fields = "g.*,(g.realSalesNum) as realSalesNum,
  3042. b.title,b.description,b.images,b.categoryId,b.brandId,b.specType,b.categoryPath,b.isEq,
  3043. b.describe,b.categoryPath,b.specGroup,
  3044. c.title as categoryName,
  3045. d.title as brandName";
  3046. $sql = 'select '.$fields.' from qianniao_goods_'.$this->enterpriseId.' as g
  3047. left join qianniao_goods_basic_'.$this->enterpriseId.' as b
  3048. on g.basicGoodsId = b.id
  3049. left join qianniao_goods_category_'.$this->enterpriseId.' as c
  3050. on c.id = b.categoryId
  3051. left join qianniao_goods_brand_'.$this->enterpriseId.' as d
  3052. on d.id = b.brandId
  3053. where
  3054. g.deleteStatus = '.StatusCode::$standard;
  3055. if (isset($condition['enableStatus']) && !empty($condition['enableStatus'])){
  3056. $sql .= ' AND g.enableStatus = '.$condition['enableStatus'];
  3057. }
  3058. //品牌Id
  3059. if ( isset($condition['brandId']) && !empty($condition['brandId']) ){
  3060. $sql .= ' AND ';
  3061. if (is_array($condition['brandId'])){
  3062. foreach($condition['brandId'] as $brandIdKey => $brandIdValue){
  3063. if($brandIdValue == end($condition['brandId'])){//是最后一个
  3064. $sql .= ' FIND_IN_SET('.$brandIdValue.',b.brandId'.')';
  3065. }else{
  3066. $sql .= ' FIND_IN_SET('.$brandIdValue.',b.brandId'.') or ';
  3067. }
  3068. }
  3069. }else{
  3070. $sql .= ' b.brandId = '.$condition['brandId'];
  3071. }
  3072. }
  3073. //商品服务
  3074. if (isset($condition['support']) && !empty($condition['support'])){
  3075. $sql .= ' AND ';
  3076. if (is_array($condition['support'])) {
  3077. foreach ($condition['support'] as $supportKey => $supportValue) {
  3078. if ($supportValue == end($condition['support'])) {//是最后一个
  3079. $sql .= ' FIND_IN_SET(' . $supportValue . ',g.support' . ')';
  3080. } else {
  3081. $sql .= ' FIND_IN_SET(' . $supportValue . ',g.support' . ') or ';
  3082. }
  3083. }
  3084. }else{
  3085. $sql .= ' FIND_IN_SET('.$condition['support'].',g.support'.')';
  3086. }
  3087. }
  3088. //商品分组
  3089. if (isset($condition['goodsGroups']) && !empty($condition['goodsGroups'])){
  3090. // $sql .= ' AND ';
  3091. // foreach($condition['goodsGroups'] as $supportKey => $supportValue){
  3092. // if($supportValue == end($condition['goodsGroups'])){//是最后一个
  3093. // $sql .= ' FIND_IN_SET('.$supportValue.',g.goodsGroups'.')';
  3094. // }else{
  3095. // $sql .= ' FIND_IN_SET('.$supportValue.',g.goodsGroups'.') or ';
  3096. // }
  3097. // }
  3098. $sql .= ' AND FIND_IN_SET('.$condition['goodsGroups'].',g.goodsGroups'.')';
  3099. }
  3100. //商品id
  3101. if (isset($condition['goodsId']) && !empty($condition['goodsId'])){
  3102. $sql .= ' and g.id = '.$condition['goodsId'];
  3103. }
  3104. //过滤商品id
  3105. if (isset($condition['notGoodsIds']) && !empty($condition['notGoodsIds'])){
  3106. $notGoodsIdsStr = implode(',',$condition['notGoodsIds']);
  3107. $sql .= ' and g.id not in('.$notGoodsIdsStr.') ';
  3108. }
  3109. //查询的商品id
  3110. if (isset($condition['goodsIds']) && !empty($condition['goodsIds'])){
  3111. $goodsIdsStr = implode(',',$condition['goodsIds']);
  3112. $sql .= ' and g.id in('.$goodsIdsStr.') ';
  3113. }
  3114. if( isset($condition['merchantId']) && !empty($condition['merchantId'])){
  3115. $sql .= ' AND g.merchantId = '.$condition['merchantId'];
  3116. } else {
  3117. if (isset($condition['ifMerchant']) ){
  3118. if($condition['ifMerchant'] == StatusCode::$delete){
  3119. $sql .= ' AND g.merchantId = 0';
  3120. } else {
  3121. $sql .= ' AND g.merchantId <> 0';
  3122. }
  3123. }
  3124. }
  3125. //店铺ids array
  3126. if(isset($condition['shopId']) && !empty($condition['shopId'])){
  3127. if (is_array($condition['shopId'])){
  3128. $str = implode(',',$condition['shopId']);
  3129. $sql .= ' and g.shopId in('.$str.')';
  3130. }else{
  3131. $sql .= ' and g.shopId = '.(int)$condition['shopId'];
  3132. }
  3133. }else{
  3134. if (isset($condition['shopIds']) && !empty($condition['shopIds'])){
  3135. $str = implode(',',$condition['shopIds']);
  3136. $sql .= ' and g.shopId in('.$str.')';
  3137. }
  3138. }
  3139. //商品关键字
  3140. if (isset($condition['keyword']) && !empty($condition['keyword'])){
  3141. $keywordId = 0;
  3142. preg_match_all( '/\d+/' , $condition['keyword'] , $arr);
  3143. if (isset($arr) && is_array($arr)){
  3144. $arr = array_shift($arr);
  3145. $keywordId = isset($arr[0]) ? (int)floatval($arr[0]) : 0;
  3146. }
  3147. $sql .= ' AND ( b.title like "%'.$condition['keyword'].'%" ';
  3148. if (!empty($keywordId)){
  3149. $sql .= ' OR b.id = '.$keywordId;
  3150. }
  3151. $sql .= ' ) ';
  3152. }
  3153. //分类路径
  3154. if (isset($condition['categoryPath']) && !empty($condition['categoryPath'])){
  3155. if (is_string($condition['categoryPath']) || is_int($condition['categoryPath'])){
  3156. $condition['categoryPath'] = explode(',',$condition['categoryPath']);
  3157. }
  3158. $endPathId = array_pop($condition['categoryPath']);
  3159. if (!empty($endPathId)){
  3160. $sql .= ' and (find_in_set('.$endPathId.',b.categoryPath) or find_in_set('.$endPathId.',b.assistCategoryId)) ';
  3161. }
  3162. }
  3163. //分类id
  3164. if (isset($condition['categoryId']) && !empty($condition['categoryId'])){
  3165. $sql .= ' and (find_in_set('.$condition['categoryId'].',b.categoryPath) or find_in_set('.$condition['categoryId'].',b.assistCategoryId)) ';
  3166. }
  3167. if (isset($condition['inStock'])) {
  3168. $goodsBasicIdResult = self::getInOrOutOfStockGoodsBasicId($condition['inStock']);
  3169. if (!$goodsBasicIdResult->isSuccess()) {
  3170. return ResultWrapper::fail($goodsBasicIdResult->getData(), $goodsBasicIdResult->getErrorCode());
  3171. }
  3172. $goodsBasicIds = $goodsBasicIdResult->getData();
  3173. $where = '';
  3174. if (!empty($goodsBasicIds)) {
  3175. $where .= " AND (";
  3176. foreach ($goodsBasicIds as $key => $goodsBasicId) {
  3177. $cond = $key ? " OR " : "";
  3178. $where .= $cond . '(g.basicGoodsId = ' . $goodsBasicId['basicGoodsId'] . ' and g.shopId=' . $goodsBasicId['shopId'] . ')';
  3179. }
  3180. $where .= ") ";
  3181. }
  3182. $sql .= $where;
  3183. }
  3184. //查询数据总条数
  3185. if ($getTotal === true){
  3186. $total = count((array)$this->objDGoods->query($sql));
  3187. }
  3188. //排序
  3189. // if (isset($condition['orderBy']) && !empty($condition['orderBy'])){
  3190. // $sql .= ' order by '.$condition['orderBy'];
  3191. // }else{
  3192. // $sql .= ' order by g.topTime DESC,g.createTime DESC ';
  3193. // }
  3194. //销售排序
  3195. if ( isset($condition['sort']) && !empty($condition['sort']) ){
  3196. if ($condition['sort'] == 1){//倒序
  3197. $sql .= ' order by g.topTime DESC,realSalesNum DESC,g.createTime DESC';
  3198. }else{
  3199. $sql .= ' order by g.topTime DESC,realSalesNum ASC,g.createTime DESC';
  3200. }
  3201. }else{
  3202. $sql .= ' order by g.topTime DESC,g.createTime DESC';
  3203. }
  3204. // if ( isset($condition['priceSort']) && !empty($condition['priceSort']) ){
  3205. // $sql .= ',g. DESC';
  3206. // }
  3207. //分页
  3208. if (isset($condition['offset']) && isset($condition['limit']) && !empty($condition['limit'])){
  3209. $sql .= ' limit '.$condition['offset'].','.$condition['limit'];
  3210. }
  3211. $dbResult = $this->objDGoods->query($sql);
  3212. if ($dbResult === false){
  3213. Logger::logs(E_USER_ERROR,'查询商品失败',__CLASS__,__LINE__,$this->objDGoods->error());
  3214. return ResultWrapper::fail($this->objDGoods->error(),ErrorCode::$dberror);
  3215. }
  3216. //销量放进去
  3217. // V($dbResult);
  3218. // $salesNumArray = (array)json_decode(getArrayItem($goods,'salesNum',''),true);
  3219. // unset($goods['salesNum']);
  3220. // $goods['salesNum'] = array_sum(array_column($salesNumArray,'salesNum'));
  3221. $map = [
  3222. 'total' => $total,
  3223. 'data' => $dbResult
  3224. ];
  3225. return ResultWrapper::success($map);
  3226. }
  3227. /**
  3228. * 商品数据/最高最低价格
  3229. * @param array $data
  3230. * @param array $shopIdBindWarehouseId
  3231. * @return ResultWrapper
  3232. * @throws Exception
  3233. */
  3234. private function formatGoodsData(array $data,array $shopIdBindWarehouseId)
  3235. {
  3236. if(empty($data)){
  3237. return ResultWrapper::success($data);
  3238. }
  3239. // 最高最低价格
  3240. $priceResult = $this->objMPrice->getPolePrice(array_column($data,'id'),$this->useCustomerId);
  3241. if (!$priceResult->isSuccess()){
  3242. return ResultWrapper::fail($priceResult->getData(),$priceResult->getErrorCode());
  3243. }
  3244. $allRangePriceData = $priceResult->getData();
  3245. //unitData
  3246. $condition = ['basicGoodsIds'=> array_column($data,'basicGoodsId')];
  3247. $unitDataResult = self::getUnitDataBySelectParams($condition);
  3248. if (!$unitDataResult->isSuccess()){
  3249. return ResultWrapper::fail($unitDataResult->getData(),$unitDataResult->getErrorCode());
  3250. }
  3251. $allUnitData = $unitDataResult->getData();
  3252. foreach ($data as $key => &$value){
  3253. $value['unitData'] = isset($allUnitData[$value['basicGoodsId']]) ? $allUnitData[$value['basicGoodsId']] : [];
  3254. $value['images'] = empty(json_decode($value['images'], true)) ? [] : json_decode($value['images'], true);
  3255. $value['specGroup'] = empty(json_decode($value['specGroup'], true)) ? [] : json_decode($value['specGroup'], true);
  3256. $value['title'] = htmlspecialchars_decode($value['title'], ENT_QUOTES);
  3257. $description = htmlspecialchars_decode($value['description'], ENT_QUOTES);
  3258. $description = str_replace('?imageView2/2/w/750/h/750','',$description);
  3259. $description = str_replace('?imageView2/2/w/750','',$description);
  3260. $value['content'] = $description;
  3261. $value['description'] = $description;
  3262. $value['warehouseId'] = isset($shopIdBindWarehouseId[$value['shopId']]) ? $shopIdBindWarehouseId[$value['shopId']] : null;
  3263. $value['minSalePrice'] = isset($allRangePriceData[$value['id']]) ? $allRangePriceData[$value['id']]['minSalePrice'] : 0;
  3264. $value['maxSalePrice'] = isset($allRangePriceData[$value['id']]) ? $allRangePriceData[$value['id']]['maxSalePrice'] : 0;
  3265. $value['minMarketPrice'] = isset($allRangePriceData[$value['id']]) ? $allRangePriceData[$value['id']]['minMarketPrice'] : 0;
  3266. $value['maxMarketPrice'] = isset($allRangePriceData[$value['id']]) ? $allRangePriceData[$value['id']]['maxMarketPrice'] : 0;
  3267. $value['minMemberPrice'] = isset($allRangePriceData[$value['id']]) ? $allRangePriceData[$value['id']]['minMemberPrice'] : 0;
  3268. $value['maxMemberPrice'] = isset($allRangePriceData[$value['id']]) ? $allRangePriceData[$value['id']]['maxMemberPrice'] : 0;
  3269. $value['marketPrice'] = isset($allRangePriceData[$value['id']]) ? $allRangePriceData[$value['id']]['minMarketPrice'] : 0;
  3270. $value['minMinUnitPrice'] = isset($allRangePriceData[$value['id']]) ? $allRangePriceData[$value['id']]['minMinUnitPrice'] : 0;
  3271. $value['maxMinUnitPrice'] = isset($allRangePriceData[$value['id']]) ? $allRangePriceData[$value['id']]['maxMinUnitPrice'] : 0;
  3272. }
  3273. $inventoryResult = self::getSkuInventorySpec($data);
  3274. if (!$inventoryResult->isSuccess()){
  3275. return ResultWrapper::fail($inventoryResult->getData(),$inventoryResult->getErrorCode());
  3276. }
  3277. $data = $inventoryResult->getData();
  3278. return ResultWrapper::success($data);
  3279. }
  3280. /**
  3281. * Doc: (des="格式化json数据以及销量")
  3282. * User: XMing
  3283. * Date: 2020/7/14
  3284. * Time: 11:08 上午
  3285. * @param array $data
  3286. * @return array
  3287. */
  3288. public static function formatJsonGoods(array $data)
  3289. {
  3290. if (empty($data)){
  3291. return $data;
  3292. }
  3293. foreach ($data as &$item){
  3294. if (!isset($item['virtualSalesNum'])){
  3295. $item['salesNum'] = 0;
  3296. continue;
  3297. }
  3298. $item['salesNum'] = (int) $item['virtualSalesNum'];
  3299. }
  3300. return $data;
  3301. }
  3302. /**
  3303. * @param array $condition
  3304. * @return ResultWrapper
  3305. */
  3306. private function getUnitDataBySelectParams(array $condition = [])
  3307. {
  3308. $fields = 'id,unitId,unitName,conversion,goodsId,isMaster,barCode,specType';
  3309. $sql = 'SELECT '.$fields.' FROM qianniao_sku_' . $this->enterpriseId . '
  3310. WHERE deleteStatus=' . StatusCode::$standard;
  3311. if (isset($condition['basicGoodsIds']) && !empty($condition['basicGoodsIds'])){
  3312. $str = implode(',',$condition['basicGoodsIds']);
  3313. $sql .= ' and goodsId in('.$str.')';
  3314. }
  3315. $dbResult = $this->objDGoods->query($sql);
  3316. if ($dbResult === false){
  3317. Logger::logs(E_USER_ERROR,'获取单位出错',__CLASS__,__LINE__,$this->objDGoods->error());
  3318. return ResultWrapper::fail($this->objDGoods->error(),ErrorCode::$dberror);
  3319. }
  3320. $haveMaster = [];
  3321. $noMasterUnitIds = [];
  3322. foreach ($dbResult as $key => $item){
  3323. $haveMaster[$item['goodsId']] = 0;
  3324. $noMasterUnitIds[$item['goodsId']] = [];
  3325. }
  3326. $newSkuData = [];
  3327. foreach ($dbResult as $key => &$value) {
  3328. if ($value['specType'] == StatusCode::$specType['multiple']) {
  3329. $value['barCode'] = '';
  3330. }
  3331. if ($value['isMaster'] == StatusCode::$standard && $haveMaster[$value['goodsId']] == 0) {
  3332. $newSkuData[$value['goodsId']][] = $value;
  3333. $haveMaster[$value['goodsId']] = 1;
  3334. }
  3335. if ($value['isMaster'] == StatusCode::$delete && !in_array($value['unitId'], $noMasterUnitIds[$value['goodsId']])) {
  3336. $newSkuData[$value['goodsId']][] = $value;
  3337. $noMasterUnitIds[$value['goodsId']][] = $value['unitId'];
  3338. }
  3339. }
  3340. return ResultWrapper::success($newSkuData);
  3341. }
  3342. /**
  3343. * 前台获取商品详情
  3344. * @param $goodsId
  3345. * @param $areaCode
  3346. * @return ResultWrapper
  3347. * @throws Exception
  3348. */
  3349. public function getApiGoodsInfo($goodsId, $areaCode = ''): ResultWrapper
  3350. {
  3351. $shopResult = self::getShopIdsByArea($areaCode);
  3352. if (!$shopResult->isSuccess()){
  3353. return ResultWrapper::fail($shopResult->getData(),ErrorCode::$dberror);
  3354. }
  3355. $shopData = $shopResult->getData();//销售区域店铺数据
  3356. if (empty($shopData)){
  3357. return ResultWrapper::fail('商品不再此区域销售', ErrorCode::$notAllowAccess);
  3358. }
  3359. // 查询商品数据
  3360. $condition = [
  3361. 'goodsId' => $goodsId,
  3362. 'shopIds' => $shopData['shopIds']
  3363. ];
  3364. $goodsResult = self::getGoodsBySelectParams($condition,false);
  3365. if (!$goodsResult->isSuccess()){
  3366. return ResultWrapper::fail($goodsResult->getData(),$goodsResult->getErrorCode());
  3367. }
  3368. $goodsData = $goodsResult->getData()['data'];
  3369. if (empty($goodsData)){
  3370. return ResultWrapper::fail('商品不在此区域销售',ErrorCode::$paramError);
  3371. }
  3372. //获取客户id
  3373. !empty($this->userCenterId) && $this->useCustomerId = $this->objMCustomer->getCustomerIdByUserCenterId($this->userCenterId);
  3374. // 格式化商品数据
  3375. $formatGoodsRes = self::formatGoodsData($goodsData,$shopData['shopIdBindWarehouseId']);
  3376. if (!$formatGoodsRes->isSuccess()){
  3377. return ResultWrapper::fail($formatGoodsRes->getData(),$formatGoodsRes->getErrorCode());
  3378. }
  3379. $goodsData = $formatGoodsRes->getData();
  3380. $goodsData = array_shift($goodsData);
  3381. $goodsDataResult = self::formatDetails($goodsData);
  3382. if (!$goodsDataResult->isSuccess()){
  3383. return ResultWrapper::fail($goodsDataResult->getData(),$goodsDataResult->getErrorCode());
  3384. }
  3385. $goodsData = $goodsDataResult->getData();
  3386. $goodsDataResult = self::formatGoodsExpress($goodsData);
  3387. if (!$goodsDataResult->isSuccess()){
  3388. return ResultWrapper::fail($goodsDataResult->getData(),$goodsDataResult->getErrorCode());
  3389. }
  3390. $goodsData = $goodsDataResult->getData();
  3391. $goodsData = self::ruleSalePrice($goodsData);//按固定规则获取价格
  3392. $goodsData = self::getActivity($goodsData);
  3393. $goodsData['isCollect'] = $this->objMGoodsCollect->hasCollect($goodsData['id']);
  3394. // 写入客户浏览商品记录数
  3395. $objMCustomerVisitsLog = new MCustomerVisitsLog($this->enterpriseId, $this->userCenterId);
  3396. $customerVisitsData = [
  3397. 'usercenterid' => $this->userCenterId,
  3398. 'customerid' => $this->useCustomerId,
  3399. 'shopid' => $goodsData['shopId'],
  3400. 'goodsid' => $goodsData['basicGoodsId'],
  3401. 'createTime' => time(),
  3402. ];
  3403. $objMCustomerVisitsLog->addCustomerVisitsLog($customerVisitsData);
  3404. // 展示商品服务信息
  3405. $objMGoodsSupport = new MGoodsSupport($this->enterpriseId, $this->userCenterId);
  3406. $supportId = $goodsData['support'];
  3407. $supportresult = $objMGoodsSupport->getGoodsSupport($supportId);
  3408. if(!$supportresult->isSuccess()){
  3409. return ResultWrapper::fail($supportresult->getData(),$supportresult->getErrorCode());
  3410. }
  3411. $goodsData['support'] = $supportresult->getData();
  3412. return ResultWrapper::success($goodsData);
  3413. }
  3414. /**
  3415. * Doc: (des="根据规则处理商品详情默认显示的价格")
  3416. * User: XMing
  3417. * Date: 2020/8/14
  3418. * Time: 5:32 下午
  3419. * @param array $data
  3420. * @return array
  3421. */
  3422. private function ruleSalePrice(array $data)
  3423. {
  3424. if (empty($data)){
  3425. return $data;
  3426. }
  3427. if (!isset($data['specMultiple'])){
  3428. return $data;
  3429. }
  3430. $specMultiple = $data['specMultiple'];
  3431. foreach ($specMultiple as &$value){
  3432. if (isset($value['salePrice']) && empty($value['salePrice'])){
  3433. $value['salePrice'] = '0.00';
  3434. }
  3435. }
  3436. $data['specMultiple'] = $specMultiple;
  3437. unset($value);
  3438. $salePrice = 0;
  3439. foreach ($specMultiple as $value){
  3440. if (isset($value['isDefault']) && $value['isDefault'] == StatusCode::$standard){
  3441. $salePrice = $value['salePrice'];//默认规格的价格
  3442. break;
  3443. }
  3444. }
  3445. if ($salePrice != 0){
  3446. //1。如果默认规格价格不为0,用默认价格
  3447. $data['salePrice'] = $salePrice;
  3448. return $data;
  3449. }
  3450. //2。默认价格没有,用最低价格
  3451. $salePrice = $data['minSalePrice'];//最低价格
  3452. if($salePrice != 0){
  3453. $data['salePrice'] = $salePrice;
  3454. return $data;
  3455. }
  3456. //3.取谷歌中有价格的
  3457. foreach ($specMultiple as $value){
  3458. if (isset($value['salePrice']) && $value['salePrice'] != 0){
  3459. $salePrice = $value['salePrice'];
  3460. break;
  3461. }
  3462. }
  3463. $data['salePrice'] = $salePrice;
  3464. return $data;
  3465. }
  3466. /**
  3467. * 根据商品ids数组获取商品names
  3468. * @param $ids
  3469. * @return array|ResultWrapper
  3470. * @throws Exception
  3471. */
  3472. public function getNameByGoodsIds($ids)
  3473. {
  3474. if (empty($ids)){
  3475. return ResultWrapper::success([]);
  3476. }
  3477. $objDGoodsBasic = new DGoodsBasic();
  3478. $objDGoodsBasic->setTable($objDGoodsBasic->get_Table() . '_' . $this->enterpriseId);
  3479. $GoodsBasic = $objDGoodsBasic->select($ids, 'title,extends,specType,isEq,categoryId,categoryPath,brandId,id,images,`describe`');
  3480. if ($GoodsBasic === false) {
  3481. return ResultWrapper::fail($objDGoodsBasic->error(), ErrorCode::$dberror);
  3482. }
  3483. $returnData = [];
  3484. foreach ($GoodsBasic as $value) {
  3485. $extJson = empty($value['extends']) ? [] : json_decode($value['extends'],true);
  3486. $returnData[$value['id']]['storageCode'] = isset($extJson['storage']) ? $extJson['storage'] : '';
  3487. $returnData[$value['id']]['title'] = $value['title'];
  3488. $returnData[$value['id']]['describe'] = htmlspecialchars_decode($value['describe']);
  3489. $returnData[$value['id']]['images'] = json_decode($value['images']);
  3490. $returnData[$value['id']]['categoryId'] = $value['categoryId'];
  3491. $returnData[$value['id']]['brandId'] = $value['brandId'];
  3492. $returnData[$value['id']]['categoryPath'] = $value['categoryPath'];
  3493. $returnData[$value['id']]['specType'] = $value['specType'];
  3494. $returnData[$value['id']]['isEq'] = $value['isEq'];
  3495. }
  3496. return ResultWrapper::success($returnData);
  3497. }
  3498. /**
  3499. * 商品上下架
  3500. * @param array $params
  3501. * @return ResultWrapper
  3502. * @throws Exception
  3503. * @throws Exception
  3504. */
  3505. public function updateEnableStatus($params)
  3506. {
  3507. if (!empty($params['deleteStatus'])){
  3508. $is_status['deleteStatus'] = $params['deleteStatus'];
  3509. }else{
  3510. $is_status['enableStatus'] = $params['enableStatus'];
  3511. }
  3512. $dbResult = $this->objDGoods->update($is_status, $params['id']);
  3513. if ($dbResult === false) {
  3514. return ResultWrapper::fail($this->objDGoods->error(), ErrorCode::$dberror);
  3515. } else {
  3516. /*if (is_array($params['id'])) {
  3517. foreach ($params['id'] as $id) {
  3518. $_id = self::createEsDocumentId($id);
  3519. $this->objDGoods->esupdateTypeFieldVaule(['enableStatus' => $params['enableStatus']], $_id);
  3520. }
  3521. } else {
  3522. $_id = self::createEsDocumentId($params['id']);
  3523. $this->objDGoods->esupdateTypeFieldVaule(['enableStatus' => $params['enableStatus']], $_id);
  3524. }*/
  3525. return ResultWrapper::success($dbResult);
  3526. }
  3527. }
  3528. /**
  3529. * 商品库存为0自动下架
  3530. * @param $updateData
  3531. * @param $where
  3532. * @return ResultWrapper
  3533. */
  3534. public function updateGoodsData($updateData, $where)
  3535. {
  3536. $goods = $this->objDGoods->get($where);
  3537. if ($goods === false) {
  3538. return ResultWrapper::fail($this->objDGoods->error(), ErrorCode::$dberror);
  3539. }
  3540. if (empty($goods)) {
  3541. return ResultWrapper::fail('该商品不存在', ErrorCode::$dberror);
  3542. }
  3543. $dbResult = $this->objDGoods->update($updateData, $where);
  3544. if ($dbResult === false) {
  3545. return ResultWrapper::fail($this->objDGoods->error(), ErrorCode::$dberror);
  3546. }
  3547. /*
  3548. $_id = self::createEsDocumentId($goods['id']);
  3549. $this->objDGoods->esupdateTypeFieldVaule($updateData, $_id);*/
  3550. return ResultWrapper::success($dbResult);
  3551. }
  3552. /**
  3553. * @param string $barcode
  3554. * @return ResultWrapper
  3555. * @throws Exception
  3556. */
  3557. public function getGoodsByBarCode(string $barcode)
  3558. {
  3559. $sql = "SELECT g.id as goodsId,g.enableStatus,g.deleteStatus,
  3560. s.id as skuId,s.unitName,s.specData,s.unitId
  3561. FROM qianniao_goods_".$this->enterpriseId." as g
  3562. LEFT JOIN qianniao_sku_".$this->enterpriseId." as s ON s.goodsId=g.basicGoodsId
  3563. WHERE s.barCode='".$barcode."' AND g.deleteStatus = ".StatusCode::$standard." AND s.deleteStatus = ".StatusCode::$standard;
  3564. $dbResult = $this->objDGoodsBasic->query($sql);
  3565. if ($dbResult === false){
  3566. return ResultWrapper::fail($this->objDGoodsBasic->error(),ErrorCode::$dberror);
  3567. }
  3568. $dbResult = (array) $dbResult;
  3569. if (empty($dbResult) || empty($dbResult[0]['goodsId'])){
  3570. return ResultWrapper::fail("没有找到对应商品",ErrorCode::$paramError);
  3571. }
  3572. //搜索出商品可能不唯一
  3573. $goods = array_shift($dbResult);
  3574. if ($goods['enableStatus'] == StatusCode::$delete){
  3575. return ResultWrapper::fail('商品已下架',ErrorCode::$paramError);
  3576. }
  3577. $goods = self::createIndex($goods);
  3578. $goodsDetail = self::formatGoodsDetail($goods);
  3579. if (!$goodsDetail->isSuccess()){
  3580. return ResultWrapper::fail($goodsDetail->getData(),$goodsDetail->getErrorCode());
  3581. }
  3582. $goods['detail'] = $goodsDetail->getData();
  3583. return ResultWrapper::success($goods);
  3584. }
  3585. /**
  3586. * @param array $goods
  3587. * @throws Exception
  3588. */
  3589. public function formatGoodsDetail(array $goods)
  3590. {
  3591. $goodsDetail = self::getGoodsInfo($goods['goodsId']);
  3592. if (!$goodsDetail->isSuccess()){
  3593. return ResultWrapper::fail($goodsDetail->getData(),$goodsDetail->getErrorCode());
  3594. }
  3595. $goodsDetail = $goodsDetail->getData();
  3596. return ResultWrapper::success($goodsDetail);
  3597. }
  3598. /**
  3599. * @param array $goods
  3600. * @return array
  3601. */
  3602. public static function createIndex(array $goods)
  3603. {
  3604. $map = [
  3605. 'goodsId' => '',
  3606. 'index' => '',
  3607. 'skuId' => '',
  3608. ];
  3609. if (empty($goods)){
  3610. return $map;
  3611. }
  3612. $specData = empty($goods['specData']) ? [] : json_decode($goods['specData'],true);
  3613. $map['goodsId'] = $goods['goodsId'];
  3614. $map['skuId'] = $goods['skuId'];
  3615. $index = $goods['goodsId'].':'.$goods['unitId'];
  3616. if (!empty($specData)){
  3617. foreach ($specData as $row) {
  3618. $index .= empty($index) ? $row['specValueId'] : ':' . $row['specValueId'];
  3619. }
  3620. }
  3621. $map['index'] = $index;
  3622. return $map;
  3623. }
  3624. /**
  3625. * @param array $selectParams
  3626. * @return ResultWrapper
  3627. * @throws Exception
  3628. */
  3629. public function getAllGoodsBySku(array $selectParams)
  3630. {
  3631. $customerResult = self::getCustomerIdAndCustomerTypeByUserCenterId($selectParams);
  3632. if (!$customerResult->isSuccess()){
  3633. return ResultWrapper::fail($customerResult->getData(),$customerResult->getErrorCode());
  3634. }
  3635. $fields = 's.id as skuId,s.conversion,s.unitName,s.isMaster,s.unitId,s.specData as specGroup,s.isNew,s.barCode,
  3636. b.title,b.images,b.id as basicGoodsId,b.specType,b.brandId,b.categoryPath,b.extends,b.supplierId,b.isEq,g.isDistribution,
  3637. c.title as categoryName,
  3638. g.shopId,g.id as id,
  3639. p.name as shopName,p.warehouseId,
  3640. d.title AS brandName,b.merchantId';
  3641. $sql = 'SELECT '.$fields.' FROM qianniao_sku_'.$this->enterpriseId.' as s
  3642. LEFT JOIN qianniao_goods_basic_'.$this->enterpriseId.' as b ON s.goodsId=b.id
  3643. LEFT JOIN qianniao_goods_'.$this->enterpriseId.' as g ON b.id=g.basicGoodsId
  3644. LEFT JOIN qianniao_goods_category_'.$this->enterpriseId.' as c ON c.id=b.categoryId
  3645. LEFT JOIN qianniao_shop_1 as p ON p.id = g.shopId
  3646. LEFT JOIN qianniao_goods_brand_'.$this->enterpriseId.' AS d ON b.brandId = d.id
  3647. WHERE s.deleteStatus= '.StatusCode::$standard.'
  3648. AND g.deleteStatus = '.StatusCode::$standard.'
  3649. AND g.enableStatus= '.StatusCode::$standard.'';
  3650. //店铺id
  3651. if (isset($selectParams['shopId']) && !empty($selectParams['shopId'])){
  3652. $shopStr = is_array($selectParams['shopId']) ? rtrim(implode(',',$selectParams['shopId']),',') : $selectParams['shopId'];
  3653. $sql .= ' AND g.shopId In ('.$shopStr.')';
  3654. }
  3655. if (isset($selectParams['isRevealSku']) && !empty($selectParams['isRevealSku']) && $selectParams['isRevealSku'] == StatusCode::$standard){
  3656. $sql .= ' AND s.isMaster = '.StatusCode::$standard;
  3657. }
  3658. //筛选分类
  3659. if (isset($selectParams['categoryId']) && !empty($selectParams['categoryId'])){
  3660. $sql .= ' and find_in_set('.$selectParams['categoryId'].',b.categoryPath) ';
  3661. }
  3662. //商品名称,字母
  3663. if (isset($selectParams['keyword']) && !empty($selectParams['keyword'])){
  3664. $keyword = '%'.$selectParams['keyword'].'%';
  3665. $sql .= ' AND (b.title LIKE "'.$keyword.'" OR b.condition LIKE "'.$keyword.'" OR s.barCode LIKE "'.$keyword.'")';
  3666. }
  3667. $total = count((array)$this->objDGoodsBasic->query($sql));
  3668. $sql .= ' ORDER BY g.createTime DESC LIMIT '.$selectParams['offset'].','.$selectParams['limit'].'';
  3669. $dbResult = $this->objDGoodsBasic->query($sql);
  3670. if ($dbResult === false){
  3671. return ResultWrapper::fail($this->objDGoodsBasic->error(),ErrorCode::$dberror);
  3672. }
  3673. //抄码商品支持主单位购买
  3674. /*foreach ($dbResult as $key => $item){
  3675. if ($item['isEq'] == StatusCode::$standard && $item['isMaster'] == StatusCode::$standard){
  3676. unset($dbResult[$key]);
  3677. }
  3678. }*/
  3679. $dbResult = array_values($dbResult);
  3680. $dbResult = self::formatList($dbResult);
  3681. if (!$dbResult->isSuccess()){
  3682. return ResultWrapper::fail($dbResult->getData(),$dbResult->getErrorCode());
  3683. }
  3684. $return = [
  3685. 'data' => $dbResult->getData(),
  3686. 'total' => $total,
  3687. ];
  3688. return ResultWrapper::success($return);
  3689. }
  3690. /**
  3691. * 获取所有sku
  3692. */
  3693. public function getAllSku($params)
  3694. {
  3695. $where['goodsId'] = $params['id'];
  3696. $dbResult = $this->objDSku->select($where);
  3697. if($dbResult === false){
  3698. return ResultWrapper::fail($this->objDSku->error(), ErrorCode::$dberror);
  3699. }
  3700. $return = [];
  3701. if(!empty($dbResult)){
  3702. foreach($dbResult as $value){
  3703. $value['specData'] = json_decode($value['specData'], true);
  3704. $return[$value['goodsId']][] = $value;
  3705. }
  3706. }
  3707. return ResultWrapper::success($return);
  3708. }
  3709. /**
  3710. * @param $data
  3711. * @return ResultWrapper
  3712. * @throws Exception
  3713. */
  3714. public function formatList(array $data): ResultWrapper
  3715. {
  3716. if (empty($data)){
  3717. return ResultWrapper::success($data);
  3718. }
  3719. //获取库存
  3720. $inventorySelectParams = [];
  3721. $selectParam['material'] = [];
  3722. $supplierIds = [];
  3723. foreach ($data as $goods){
  3724. $selectParam['material'][$goods['shopId']][] = $goods['id'];
  3725. !empty($goods['supplierId']) && $supplierIds[] = $goods['supplierId'];
  3726. $inventorySelectParams[$goods['shopId']][] = $goods['skuId'];
  3727. }
  3728. if (!empty($this->useCustomerId)){
  3729. $selectParam['customerId'] = $this->useCustomerId;//客户有特殊调价
  3730. }
  3731. $this->objMInventory = new MInventory($this->enterpriseId, $this->userCenterId);
  3732. $inventoryMapping = [];
  3733. foreach ($inventorySelectParams as $shopId => $skuIds){
  3734. $inventorySkuResult = $this->objMInventory->getInventoryByShopIdAndSkuIds($shopId,$skuIds);
  3735. if (!$inventorySkuResult->isSuccess()){
  3736. return ResultWrapper::fail($inventorySkuResult->getData(),$inventorySkuResult->getErrorCode());
  3737. }
  3738. $inventoryMapping[$shopId] = $inventorySkuResult->getData();
  3739. }
  3740. $priceResult = $this->objMPrice->getPrice($selectParam);
  3741. if (!$priceResult->isSuccess()) {
  3742. return ResultWrapper::fail($priceResult->getData(),$priceResult->getErrorCode());
  3743. }
  3744. $goodsPriceMapping = [];
  3745. $priceResult = $priceResult->getData();
  3746. foreach ($priceResult as $shopPriceArr) {
  3747. foreach ($shopPriceArr as $goodsId => $goodsSkuArr) {
  3748. $goodsPriceMapping[$goodsId] = $goodsSkuArr;
  3749. }
  3750. }
  3751. $objMSupplier = new MSupplier($this->userCenterId,$this->enterpriseId);
  3752. $supplierResult = $objMSupplier->getSupplierNameByIds($supplierIds);
  3753. if (!$supplierResult->isSuccess()){
  3754. return ResultWrapper::fail($supplierResult->getData(),$supplierResult->getErrorCode());
  3755. }
  3756. $supplierMap = MSupplier::supplierMap($supplierResult->getData());
  3757. foreach ($data as &$value){
  3758. $extends = (isset($value['extends']) && !empty($value['extends'])) ? json_decode($value['extends'],true) : [];
  3759. unset($value['extends']);
  3760. $value['brandName'] = is_null($value['brandName']) ? '' : $value['brandName'];
  3761. $value['storageCode'] = isset($extends['storage']) ? $extends['storage'] : '';
  3762. $value['specGroup'] = empty($value['specGroup']) ? [] : json_decode($value['specGroup'],true);
  3763. $value['images'] = empty($value['images']) ? [] : json_decode($value['images'],true);
  3764. $value['conversion'] = empty($value['conversion']) ? 0 : $value['conversion'];
  3765. $value['code'] = createCode(StatusCode::$code['goodsBasic']['prefix'], $value['basicGoodsId'], StatusCode::$code['goodsBasic']['length']);
  3766. $value['salePrice'] = isset($goodsPriceMapping[$value['id']][$value['skuId']]['salePrice']) ? $goodsPriceMapping[$value['id']][$value['skuId']]['salePrice'] : 0;
  3767. $value['originPrice'] = $value['salePrice'];
  3768. $value['enabledLadder'] = isset($goodsPriceMapping[$value['id']][$value['skuId']]['enabledLadder']) ? $goodsPriceMapping[$value['id']][$value['skuId']]['enabledLadder'] : 0;
  3769. $value['ladderPrice'] = isset($goodsPriceMapping[$value['id']][$value['skuId']]['ladderPrice']) ? $goodsPriceMapping[$value['id']][$value['skuId']]['ladderPrice'] : [];
  3770. $value['supplierName'] = isset($supplierMap[$value['supplierId']]) ? $supplierMap[$value['supplierId']]['title'] : '';
  3771. $value['enabledLadder'] = isset($goodsPriceMapping[$value['id']][$value['skuId']]['enabledLadder']) ? $goodsPriceMapping[$value['id']][$value['skuId']]['enabledLadder'] : 0;
  3772. if (!isset($inventoryMapping[$value['shopId']])){
  3773. return ResultWrapper::fail('店铺库存查询异常',ErrorCode::$paramError);
  3774. }
  3775. $row = $inventoryMapping[$value['shopId']];
  3776. $value['inventory'] = isset($row[$value['skuId']]) ? $row[$value['skuId']]['num'] : 0;
  3777. $value['inventory'] = floor($value['inventory']);
  3778. }
  3779. return ResultWrapper::success($data);
  3780. }
  3781. /**
  3782. * Doc: (des="设置商品置顶/取消商品置顶")
  3783. * User: XMing
  3784. * Date: 2020/8/28
  3785. * Time: 11:07 上午
  3786. * @param int $goodsId
  3787. * @return ResultWrapper
  3788. */
  3789. public function setTop(int $goodsId)
  3790. {
  3791. $goodsInfo = $this->objDGoods->get(['id' => $goodsId],'*');
  3792. if ($goodsInfo === false){
  3793. return ResultWrapper::fail($this->objDGoods->error(),ErrorCode::$dberror);
  3794. }
  3795. if (empty($goodsInfo)){
  3796. return ResultWrapper::fail('未查询到指定商品',ErrorCode::$paramError);
  3797. }
  3798. if (empty($goodsInfo['topTime'])){
  3799. $topTime = time();
  3800. }else{
  3801. $topTime = 0;
  3802. }
  3803. $updateTime = time();
  3804. $updateResult = $this->objDGoods->update(['topTime' => $topTime,'updateTime' => $updateTime],['id' => $goodsId]);
  3805. if ($updateResult === false){
  3806. return ResultWrapper::fail($this->objDGoods->error(),ErrorCode::$dberror);
  3807. }
  3808. /*
  3809. $_id = self::createEsDocumentId($goodsId);
  3810. $this->objDGoods->esupdateTypeFieldVaule(['topTime' => $topTime,'updateTime' => $updateTime], $_id);*/
  3811. return ResultWrapper::success('操作成功');
  3812. }
  3813. /**
  3814. * Doc: (des="修改排序字段")
  3815. */
  3816. public function setSort($params)
  3817. {
  3818. $goodsInfo = $this->objDGoods->get(['id' => $params['id']]);
  3819. if ($goodsInfo === false){
  3820. return ResultWrapper::fail($this->objDGoods->error(),ErrorCode::$dberror);
  3821. }
  3822. if (empty($goodsInfo)){
  3823. return ResultWrapper::fail('未查询到指定商品',ErrorCode::$paramError);
  3824. }
  3825. $updateTime = time();
  3826. $updateResult = $this->objDGoods->update(['sort' => $params['sort'],'updateTime' => $updateTime],['id' => $params['id']]);
  3827. if ($updateResult === false){
  3828. return ResultWrapper::fail($this->objDGoods->error(),ErrorCode::$dberror);
  3829. }
  3830. return ResultWrapper::success('操作成功');
  3831. }
  3832. /**
  3833. * Doc: (des="批量设置虚拟销量")
  3834. * User: XMing
  3835. * Date: 2020/9/3
  3836. * Time: 9:35 上午
  3837. * @param array $data
  3838. * @return ResultWrapper
  3839. * @throws Exception
  3840. */
  3841. public function setSalesNumBatch(array $data)
  3842. {
  3843. $beginTransactionStatus = $this->objDGoods->beginTransaction();
  3844. foreach ($data['ids'] as $id){
  3845. $tmpSalesNum = 0;
  3846. $rand = random_int($data['minVal'],$data['maxVal']);
  3847. $updateResult = $this->objDGoods->update(['virtualSalesNum' => $rand],['id' => $id]);
  3848. if ($updateResult === false){
  3849. return ResultWrapper::fail($this->objDGoods->error(),ErrorCode::$dberror);
  3850. }
  3851. // 同步更新真实销量
  3852. $dbResult = $this->objDGoods->get($id);
  3853. if ($dbResult === false){
  3854. return ResultWrapper::fail($this->objDGoods->error(),ErrorCode::$dberror);
  3855. }
  3856. if(empty($dbResult)){
  3857. return ResultWrapper::fail('当前操作的商品不存在。id='.$id,ErrorCode::$notAllowAccess);
  3858. }
  3859. if(isset($dbResult['salesNum']) && !empty($dbResult['salesNum']) ){
  3860. $salesNumInfo = json_decode($dbResult['salesNum'],true);
  3861. }
  3862. foreach ($salesNumInfo as $key => $value){
  3863. $tmpSalesNum = bcadd($value['salesNum'],$tmpSalesNum);
  3864. }
  3865. //更新真实销量
  3866. $update = $this->objDGoods->update(['realSalesNum' => $tmpSalesNum],['id' => $id]);
  3867. if ($update === false){
  3868. return ResultWrapper::fail($this->objDGoods->error(),ErrorCode::$dberror);
  3869. }
  3870. //
  3871. /*
  3872. $_id = self::createEsDocumentId($id);
  3873. $this->objDGoods->esupdateTypeFieldVaule(['virtualSalesNum' => $rand], $_id);*/
  3874. }
  3875. if($beginTransactionStatus){
  3876. $this->objDGoods->commit();
  3877. }
  3878. return ResultWrapper::success(true);
  3879. }
  3880. /**
  3881. * Doc: (des="设置虚拟销量")
  3882. * User: XMing
  3883. * Date: 2020/9/3
  3884. * Time: 10:43 上午
  3885. * @param array $data
  3886. * @return ResultWrapper
  3887. */
  3888. public function setSalesNum(array $data)
  3889. {
  3890. $updateResult = $this->objDGoods->update(['virtualSalesNum' => $data['val']],['id' => $data['id']]);
  3891. if ($updateResult === false){
  3892. return ResultWrapper::fail($this->objDGoods->error(),ErrorCode::$dberror);
  3893. }
  3894. /*
  3895. $_id = self::createEsDocumentId($data['id']);
  3896. $this->objDGoods->esupdateTypeFieldVaule(['virtualSalesNum' => $data['val']], $_id);*/
  3897. return ResultWrapper::success(true);
  3898. }
  3899. /**
  3900. * Doc: (des="批量设置运费")
  3901. * User: XMing
  3902. * Date: 2020/9/10
  3903. * Time: 3:16 下午
  3904. * @param array $params
  3905. * @return ResultWrapper
  3906. */
  3907. public function batchGoodsExpress(array $params)
  3908. {
  3909. $ids = $params['ids'];
  3910. $params['updateTime'] = time();
  3911. unset($params['ids']);
  3912. $updateResult = $this->objDGoods->update($params,['id'=>$ids]);
  3913. if ($updateResult === false){
  3914. return ResultWrapper::fail($this->objDGoods->error(),ErrorCode::$dberror);
  3915. }
  3916. foreach ($ids as $id){
  3917. $_id = self::createEsDocumentId($id);
  3918. $this->objDGoods->esupdateTypeFieldVaule($params, $_id);
  3919. }
  3920. return ResultWrapper::success(true);
  3921. }
  3922. /**
  3923. * Doc: (des="根据商品ids获取所有商品")
  3924. * User: XMing
  3925. * Date: 2020/12/17
  3926. * Time: 11:07 上午
  3927. * @param array $goodsIds
  3928. * @return ResultWrapper
  3929. */
  3930. public function getGoodsByGoodsIds(array $goodsIds,$shopId = 0): ResultWrapper
  3931. {
  3932. if (empty($goodsIds)){
  3933. return ResultWrapper::success([]);
  3934. }
  3935. $goodsIdStr = implode(',',$goodsIds);
  3936. if (empty($goodsIdStr)){
  3937. return ResultWrapper::success([]);
  3938. }
  3939. $fields = ' g.id,g.enableStatus,b.title,b.images,b.id as materielId ';
  3940. $sql = 'SELECT '.$fields.' FROM qianniao_goods_'.$this->enterpriseId.' AS g
  3941. LEFT JOIN qianniao_goods_basic_'.$this->enterpriseId.' AS b
  3942. ON b.id = g.basicGoodsId
  3943. WHERE b.deleteStatus = '.StatusCode::$standard.' AND g.deleteStatus = '.StatusCode::$standard.' AND
  3944. g.id IN('.$goodsIdStr.')';
  3945. if (!empty($shopId)){
  3946. $sql .= ' AND g.shopId = '.$shopId;
  3947. }
  3948. $lists = $this->objDGoods->query($sql);
  3949. if ($lists === false){
  3950. return ResultWrapper::fail($this->objDGoods->error(),ErrorCode::$dberror);
  3951. }
  3952. $map = [];
  3953. foreach ($lists as $list){
  3954. $list['images'] = !empty($list['images']) ? json_decode($list['images'],true) : [];
  3955. $map[$list['id']] = $list;
  3956. }
  3957. return ResultWrapper::success($map);
  3958. }
  3959. /**
  3960. * Doc: (des="根据基础物料id及店铺id,获取物料id对应商品id")
  3961. * User: XMing
  3962. * Date: 2020/12/25
  3963. * Time: 4:03 下午
  3964. * @param array $params
  3965. * @return ResultWrapper
  3966. */
  3967. public function getRelMap(array $params): ResultWrapper
  3968. {
  3969. $materielStr = implode(',', $params['materielIds']);
  3970. $fields = ' id AS goodsId,basicGoodsId AS materielId,shopId ';
  3971. $sql = 'SELECT '.$fields.' FROM qianniao_goods_'.$this->enterpriseId.'
  3972. WHERE shopId = '.$params['shopId'].'
  3973. AND basicGoodsId IN ( '.$materielStr.') ';
  3974. $lists = $this->objDGoods->query($sql);
  3975. if ($lists === false){
  3976. return ResultWrapper::fail($this->objDGoods->error(),ErrorCode::$dberror);
  3977. }
  3978. $map = [];
  3979. foreach ($lists as $list){
  3980. $map[$list['materielId']] = $list['goodsId'];
  3981. }
  3982. return ResultWrapper::success($map);
  3983. }
  3984. /**
  3985. * Doc: (des="")
  3986. * User: XMing
  3987. * Date: 2021/1/29
  3988. * Time: 11:26 上午
  3989. * @param int $shopId
  3990. * @return ResultWrapper
  3991. */
  3992. public function getInSaleNum(int $shopId): ResultWrapper
  3993. {
  3994. $where = [
  3995. 'shopId' => $shopId,
  3996. 'enableStatus' => StatusCode::$standard,
  3997. 'deleteStatus' => StatusCode::$standard
  3998. ];
  3999. $count = $this->objDGoods->count($where);
  4000. if($count === false){
  4001. return ResultWrapper::fail($this->objDGoods->error(),ErrorCode::$dberror);
  4002. }
  4003. return ResultWrapper::success($count);
  4004. }
  4005. /**
  4006. * Doc: (des="获取负库存销售状态")
  4007. * User: XMing
  4008. * Date: 2021/3/10
  4009. * Time: 9:38 上午
  4010. * @param int $shopId
  4011. * @param array $materielIds
  4012. * @return ResultWrapper
  4013. */
  4014. public function getDistributionByShopIdRelMaterielIds(int $shopId,array $materielIds): ResultWrapper
  4015. {
  4016. if (empty($shopId)){
  4017. return ResultWrapper::fail('shopId参数异常',ErrorCode::$paramError);
  4018. }
  4019. if (empty($materielIds)){
  4020. return ResultWrapper::fail('materielIds参数异常',ErrorCode::$paramError);
  4021. }
  4022. $fields = ' b.id,g.isDistribution ';
  4023. $materielIdsStr = trim(implode(',',$materielIds),',');
  4024. $sql = 'SELECT '.$fields.' FROM qianniao_goods_basic_'.$this->enterpriseId.' AS b
  4025. LEFT JOIN qianniao_goods_'.$this->enterpriseId.' AS g
  4026. ON g.basicGoodsId = b.id
  4027. WHERE g.basicGoodsId IN ('.$materielIdsStr.') AND g.shopId = '.$shopId;
  4028. $lists = $this->objDGoods->query($sql);
  4029. if ($lists === false){
  4030. return ResultWrapper::fail($this->objDGoods->error(),ErrorCode::$dberror);
  4031. }
  4032. if (empty($lists)){
  4033. return ResultWrapper::success([]);
  4034. }
  4035. $mapping = [];
  4036. foreach ($lists as $item){
  4037. $mapping[$item['id']] = $item['isDistribution'];
  4038. }
  4039. return ResultWrapper::success($mapping);
  4040. }
  4041. /**
  4042. * Doc: (des="获取价格走势")
  4043. * User: XMing
  4044. * Date: 2021/3/10
  4045. * Time: 6:48 下午
  4046. * @param int $goodsId
  4047. * @param array $params
  4048. * @return ResultWrapper
  4049. * @throws Exception
  4050. */
  4051. public function getPriceTrend(int $goodsId,array $params): ResultWrapper
  4052. {
  4053. $info = $this->objDGoods->get($goodsId);
  4054. if ($info === false){
  4055. return ResultWrapper::fail($this->objDGoods->error(),ErrorCode::$dberror);
  4056. }
  4057. if (empty($info)){
  4058. return ResultWrapper::fail('未获取到指定的商品信息',ErrorCode::$paramError);
  4059. }
  4060. $objPriceCache = new PriceCache($this->enterpriseId);
  4061. $objMPurchase = new MPurchase($this->userCenterId,$this->enterpriseId);
  4062. $mapping = $objPriceCache->getPriceTrend($goodsId);
  4063. if (!empty($mapping)){
  4064. return ResultWrapper::success($mapping);
  4065. }
  4066. $skuLists = $this->objDSku->select(['goodsId' => $info['basicGoodsId'],'deleteStatus' => StatusCode::$standard],'id');
  4067. if ($skuLists === false){
  4068. return ResultWrapper::fail($this->objDSku->error(),ErrorCode::$dberror);
  4069. }
  4070. $skuIds = [];
  4071. foreach ($skuLists as $item){
  4072. $skuIds[] = $item['id'];
  4073. }
  4074. $mapping = [];
  4075. $day = ceil(($params['endTime'] - $params['startTime']) / 86400);
  4076. for($i = $day; $i>=0; $i--) {
  4077. $date = strtotime(date('Y-m-d 23:59:59',strtotime("-$i days")));
  4078. $sheetListsResult = $this->objMPrice->getPriceSheetByShopIdRelGoodsId($info['shopId'],$info['id'],$date);
  4079. if (!$sheetListsResult->isSuccess()){
  4080. return ResultWrapper::fail($sheetListsResult->getData(),$sheetListsResult->getErrorCode());
  4081. }
  4082. $sheetList = $sheetListsResult->getData();
  4083. $costPriceSheetResult = $objMPurchase->getCostPriceSheetByMaterielId($info['basicGoodsId'],$date);
  4084. if (!$costPriceSheetResult->isSuccess()){
  4085. return ResultWrapper::fail($costPriceSheetResult->getData(),$costPriceSheetResult->getErrorCode());
  4086. }
  4087. $costPriceSheet = $costPriceSheetResult->getData();
  4088. foreach ($skuIds as $skuId){
  4089. $mapping['saleTrend'][$skuId][] = [
  4090. 'salePrice' => isset($sheetList['salePrice'][$skuId]['salePrice']) ? $sheetList['salePrice'][$skuId]['salePrice'] : 0,
  4091. 'createTime' => date('Y-m-d',$date),
  4092. ];
  4093. $mapping['costTrend'][$skuId][] = [
  4094. 'costPrice' => isset($costPriceSheet[$skuId][0]['costPrice']) ? $costPriceSheet[$skuId][0]['costPrice'] : 0,
  4095. 'createTime' => date('Y-m-d',$date)
  4096. ];
  4097. }
  4098. }
  4099. return ResultWrapper::success($mapping);
  4100. }
  4101. /**
  4102. * Doc: (des="")
  4103. * User: XMing
  4104. * Date: 2021/3/13
  4105. * Time: 10:13 上午
  4106. * @param array $data
  4107. * @return ResultWrapper
  4108. */
  4109. public function batchSetDistribution(array $data): ResultWrapper
  4110. {
  4111. if (!in_array($data['isDistribution'],[ StatusCode::$delete,StatusCode::$standard ])){
  4112. return ResultWrapper::fail('isDistribution参数非法',ErrorCode::$paramError);
  4113. }
  4114. $result = $this->objDGoods->update(['isDistribution' => $data['isDistribution'],'updateTime'=>time()],['id' => $data['ids']]);
  4115. if ($result === false){
  4116. return ResultWrapper::fail($this->objDGoods->error(),ErrorCode::$dberror);
  4117. }
  4118. return ResultWrapper::success(true);
  4119. }
  4120. /**
  4121. * 获取商品数据
  4122. * @param $where
  4123. * @return ResultWrapper
  4124. */
  4125. public function getGoodsData($where)
  4126. {
  4127. $dbResult = $this->objDGoods->select($where);
  4128. if($dbResult === false){
  4129. return ResultWrapper::fail($this->objDGoods->error(), ErrorCode::$dberror);
  4130. }
  4131. return ResultWrapper::success($dbResult);
  4132. }
  4133. /**
  4134. * getAllBrandByCategory
  4135. * 根据商品分类加载商品品牌
  4136. * (1) select b1.categoryId,b1.categoryPath,b1.brandId,b2.* from qianniao_goods_basic_64 as b1 LEFT JOIN qianniao_goods_brand_64 as b2 on b1.brandId=b2.id where FIND_IN_SET(categoryPath,1)
  4137. * (2) SELECT * from qianniao_goods_brand_64 where id in (SELECT brandId from qianniao_goods_basic_64 where FIND_IN_SET(categoryPath,1))
  4138. */
  4139. public function getAllBrandByCategory($categoryId)
  4140. {
  4141. $goodsBasic = new DGoodsBasic();
  4142. $this->objDGoodsBasic->setTable($this->objDGoodsBasic->get_Table() . '_' . $this->enterpriseId);
  4143. // 查询所有的该分类所有的商品品牌
  4144. $sql = 'select * from qianniao_goods_brand_'.$this->enterpriseId.' where id in (SELECT brandId from qianniao_goods_basic_'.$this->enterpriseId.' where FIND_IN_SET(categoryPath,'.$categoryId.'))';
  4145. $result = $goodsBasic->query($sql);
  4146. if($result === false){
  4147. return ResultWrapper::fail($goodsBasic->error(), ErrorCode::$dberror);
  4148. }
  4149. $countSql = 'select count(1) as count from qianniao_goods_brand_'.$this->enterpriseId.' where id in (SELECT brandId from qianniao_goods_basic_'.$this->enterpriseId.' where FIND_IN_SET(categoryPath,'.$categoryId.'))';
  4150. $resultCount = $goodsBasic->query($countSql);
  4151. if($resultCount === false){
  4152. return ResultWrapper::fail($goodsBasic->error(), ErrorCode::$dberror);
  4153. }
  4154. if(empty($resultCount)){
  4155. return ResultWrapper::success([]);
  4156. }
  4157. $data = [
  4158. 'data' => $result,
  4159. 'total' => $resultCount[0]['count'],
  4160. ];
  4161. return ResultWrapper::success($data);
  4162. }
  4163. /**
  4164. * 商品真实销量
  4165. */
  4166. public function getAllRealSalesNum($params)
  4167. {
  4168. if( !isset($params['number'])&& empty($params['number'])){
  4169. $params['number'] = 10;
  4170. }
  4171. $dbResult = $this->objDGoods->query('select * from '.$this->objDGoods->get_Table().' where enableStatus='.StatusCode::$standard.' and deleteStatus='.StatusCode::$standard.' order by realSalesNum desc limit 1,'.$params['number']);
  4172. if($dbResult === false){
  4173. return ResultWrapper::fail($this->objDGoods->error(), ErrorCode::$dberror);
  4174. }
  4175. if(!empty($dbResult)){
  4176. foreach ($dbResult as $key => $value){
  4177. if(isset($value['images'])&&!empty($value['images'])){
  4178. $dbResult[$key]['images'] = json_decode($value['images'],true);
  4179. }
  4180. }
  4181. }
  4182. $data = [
  4183. 'data' => $dbResult
  4184. ];
  4185. return ResultWrapper::success($data);
  4186. }
  4187. }