12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915 |
- <?php
- namespace app\admin\service\goods;
- use app\common\Tree;
- use app\model\Appointment;
- use app\model\Coupon;
- use app\model\Goods;
- use app\model\GoodsComponent;
- use app\model\GoodsDetail;
- use app\model\GoodsLabel;
- use app\model\GoodsRunning;
- use app\model\GoodsSku;
- use app\model\OrderSheet;
- use app\model\Supplier;
- use app\model\SysCategory;
- use app\model\SysDept;
- use app\model\SysSerial;
- use app\model\SysUser;
- use support\Db;
- use support\exception\BusinessException;
- use support\Redis;
- use support\Request;
- use support\Response;
- use Tinywan\Jwt\JwtToken;
- class GoodsService
- {
- public static function selectAll($goodsIds)
- {
- $goods = Goods::where('goods_status', 'ON')
- ->when($goodsIds != '', function ($query) use ($goodsIds) {
- $query->whereIn('goods_id', $goodsIds);
- })
- ->select('goods_id', 'goods_name')
- ->get();
- return json_success('', $goods);
- }
- /**
- * @Desc 产品搜索
- * @Author Gorden
- * @Date 2024/11/8 10:10
- *
- * @param Request $request
- * @return Response
- */
- public static function selectIdNameList(Request $request)
- {
- $keywords = $request->get('keywords');
- if (!$keywords) {
- return json_fail('参数异常');
- }
- $goods = Goods::where('goods_name', 'like', '%' . $keywords . '%')
- ->select('goods_id', 'goods_name')
- ->get()
- ->toArray();
- return json_success('success', $goods);
- }
- public static function selectAllByGoodsName($goodsName)
- {
- $goods = Goods::with('sku')
- ->where('goods_status', 'ON')
- ->when($goodsName != '', function ($query) use ($goodsName) {
- $query->where('goods_name', 'like', "%" . $goodsName . '%');
- })
- ->select('goods_id', 'goods_name', 'goods_classify')
- ->get()
- ->toArray();
- foreach ($goods as &$item) {
- if (!empty($item['sku'])) {
- foreach ($item['sku'] as $key => $sku) {
- $specsJson = json_decode($sku['goods_sku_specs_json'], true);
- $skuTitle = '';
- foreach ($specsJson as $item2) {
- if (is_array($item2)) {
- $item2 = implode(',', $item2);
- }
- $skuTitle .= $item2 . '-';
- }
- $item['sku'][$key]['goods_sku_id'] = strval($item['sku'][$key]['goods_sku_id']);
- $item['sku'][$key]['goods_sku_title'] = rtrim($skuTitle, '-');
- }
- }
- }
- return json_success('', $goods);
- }
- public static function selectAllByCategoryForRuleAddComponent($category = "GOODS")
- {
- $categoryIds = [];
- $categorySuperIds = [];
- if ($category == 'GOODS') {
- $categorySuperIds = [5];
- } elseif ($category == 'SERVICE') {
- $categorySuperIds = [31, 154, 42, 65, 30, 66, 72, 70];
- }
- $categorys = SysCategory::whereIn('category_id', $categorySuperIds)->get()->toArray();
- foreach ($categorys as $item) {
- if (empty($item['category_super_path'])) {
- $item['category_super_path'] = '#' . $item['category_id'] . '#';
- } else {
- $item['category_super_path'] = $item['category_super_path'] . '#' . $item['category_id'] . '#';
- }
- $categoryIds = SysCategory::where('category_super_path', 'like', '%' . $item['category_super_path'] . '%')->pluck('category_id');
- $categoryIds = $categoryIds ? $categoryIds->toArray() : [];
- $categorySuperIds = array_merge($categorySuperIds, $categoryIds);
- }
- $categoryIds = array_unique($categorySuperIds);
- $goods = Goods::with('sku')
- // ->where('goods_classify', $category)
- ->when(!empty($categoryIds), function ($query) use ($categoryIds) {
- $query->whereIn('join_goods_category_id', $categoryIds);
- })->when(empty($categoryIds), function ($query) {
- $query->where('goods_classify', '<>', 'RECHARGE');
- })->where('goods_status', 'ON')
- ->select('goods_id', 'goods_name', 'join_goods_category_id')
- ->get()
- ->toArray();
- foreach ($goods as &$good) {
- if (!empty($good['sku'])) {
- foreach ($good['sku'] as $key => $sku) {
- if (!empty($sku['goods_sku_specs_json'])) {
- $specsJson = json_decode($sku['goods_sku_specs_json'], true);
- $skuTitle = '';
- foreach ($specsJson as $item) {
- if (is_array($item)) {
- $item = implode(',', $item);
- }
- $skuTitle .= $item . '-';
- }
- $good['sku'][$key]['goods_sku_id'] = strval($good['sku'][$key]['goods_sku_id']);
- $good['sku'][$key]['goods_sku_title'] = rtrim($skuTitle, '-');
- }
- unset($good['sku'][$key]['goods_sku_specs_json'], $good['sku'][$key]['join_sku_goods_id']);
- }
- } else {
- $good['sku'] = [];
- }
- }
- return json_success('', $goods);
- }
- public static function selectPremisesByGoodsId($goodsId)
- {
- $goods = Goods::where('goods_id', $goodsId)
- ->select('goods_id', 'goods_attribute_json')
- ->first();
- $premisses = [];
- if (!empty($goods->goods_attribute_json)) {
- $attributeJson = json_decode($goods->goods_attribute_json, true);
- if (isset($attributeJson['premisses'])) {
- $premisses = SysDept::whereIn('dept_id', $attributeJson['premisses'])
- ->select('dept_id', 'dept_name')
- ->get()
- ->toArray();
- }
- }
- return json_success('', $premisses);
- }
- public static function select(Request $request, $classify = "GOODS")
- {
- $page = $request->get('page', 1);
- $pageSize = $request->get('pageSize', 20);
- $goodsName = $request->get('goods_name', '');
- $categoryId = $request->get('join_goods_category_id', null);
- $goodsCategory = $request->get('goods_category', null);
- $goodsSupplierId = $request->get('join_goods_supplier_id', null);
- $goodsStatus = $request->get('goods_status', null);
- $type = $request->get('type', '');
- if ($categoryId != null) {
- $categoryPath = SysCategory::where('category_id', $categoryId)->value('category_super_path');
- $categoryPath .= '#' . $categoryId . '#';
- $categoryIds = SysCategory::where('category_super_path', 'like', $categoryPath . '%')->pluck('category_id')->toArray();
- $categoryIds[] = intval($categoryId);
- if (!empty($categoryIds)) {
- $categoryId = $categoryIds;
- } else {
- $categoryId = [intval($categoryId)];
- }
- }
- $rows = Goods::with([
- 'category' => function ($query) {
- $query->select('category_id', 'category_name');
- },
- 'running' => function ($query) {
- $query->select('join_running_goods_id', 'goods_running_storage');
- },
- 'supplier' => function ($query) {
- $query->select('supplier_id', 'supplier_name');
- },
- 'user' => function ($query) {
- $query->select('user_id', 'user_name');
- },
- 'updateUser' => function ($query) {
- $query->select('user_id', 'user_name');
- },
- ])->leftJoin('goods_running', 'goods_running.join_running_goods_id', '=', 'goods.goods_id')
- ->select('goods_id', 'join_goods_category_id', 'join_goods_supplier_id', 'creator_user_id', 'updator_user_id', 'goods_status', 'goods_sales_price', 'goods_category', 'goods_name', 'goods_title', 'goods_cover', 'goods_sort', 'goods_attribute_json', 'goods_addtimes', 'goods_updatetimes')
- ->when($goodsName != '', function ($query) use ($goodsName) {
- $query->where(function ($q) use ($goodsName) {
- $q->where('goods_name', 'like', '%' . $goodsName . '%');
- // ->OrWhere('goods_title', 'like', '%' . $goodsName . '%');
- });
- })->when($categoryId != null, function ($query) use ($categoryId) {
- $query->whereIn('join_goods_category_id', $categoryId);
- })->when($goodsCategory != null, function ($query) use ($goodsCategory) {
- $query->where('goods_category', $goodsCategory);
- })
- ->when($classify != '', function ($query) use ($classify, $categoryId) {
- if ($classify == 'GOODS' && empty($categoryId)) {
- $query->whereIn('join_goods_category_id', ['6', '7', '8', '9', '10', '11', '12', '30']);
- } else if ($classify == 'RECHARGE' && empty($categoryId)) {
- $query->where('goods_status', 'ON');
- $query->where('goods_classify', $classify);
- } else if ($classify != 'GOODS' && empty($categoryId)) {
- $query->where('goods_classify', $classify);
- }
- })->when(!empty($type), function ($query) use ($type) {
- if ($type == 'storageWarning') {
- $query->where('goods_running.goods_running_storage', '<=', 2);
- }
- })->when(!empty($goodsSupplierId), function ($query) use ($goodsSupplierId) {
- $query->where('join_goods_supplier_id', $goodsSupplierId);
- })->when(!empty($goodsStatus), function ($query) use ($goodsStatus) {
- $query->where('goods_status', $goodsStatus);
- })
- ->orderBy('goods_sort', 'DESC')
- ->orderBy('goods_addtimes', 'DESC')
- ->forPage($page, $pageSize)
- ->get()
- ->toArray();
- $totalModel = Goods::leftJoin('goods_running', 'goods_running.join_running_goods_id', '=', 'goods.goods_id')
- ->when($goodsName != '', function ($query) use ($goodsName) {
- $query->where(function ($q) use ($goodsName) {
- $q->where('goods_name', 'like', '%' . $goodsName . '%');
- // ->OrWhere('goods_title', 'like', '%' . $goodsName . '%');
- });
- })->when($categoryId != null, function ($query) use ($categoryId) {
- $query->whereIn('join_goods_category_id', $categoryId);
- })->when($goodsCategory != null, function ($query) use ($goodsCategory) {
- $query->where('goods_category', $goodsCategory);
- })->when($classify != '', function ($query) use ($classify, $categoryId) {
- if ($classify == 'GOODS' && empty($categoryId)) {
- $query->whereIn('join_goods_category_id', ['6', '7', '8', '9', '10', '11', '12', '30']);
- } else if ($classify == 'RECHARGE' && empty($categoryId)) {
- $query->where('goods_status', 'ON');
- $query->where('goods_classify', $classify);
- } else if ($classify != 'GOODS' && empty($categoryId)) {
- $query->where('goods_classify', $classify);
- }
- })->when(!empty($type), function ($query) use ($type) {
- if ($type == 'storageWarning') {
- $query->where('goods_running.goods_running_storage', '<=', 2);
- }
- })->when(!empty($goodsSupplierId), function ($query) use ($goodsSupplierId) {
- $query->where('join_goods_supplier_id', $goodsSupplierId);
- })->when(!empty($goodsStatus), function ($query) use ($goodsStatus) {
- $query->where('goods_status', $goodsStatus);
- });
- $goodsOnTotalModel = clone $totalModel;
- $goodsOnTotal = $goodsOnTotalModel->where('goods_status', 'ON')->count();
- $goodsWarningTotalModel = clone $totalModel;
- $goodsWarningTotal = $goodsWarningTotalModel->where('goods_running_storage', '<=', 2)->count();
- $total = $totalModel->count();
- foreach ($rows as &$row) {
- $row['goods_cover'] = getenv('STORAGE_DOMAIN') . $row['goods_cover'];
- if (isset($row['running'])) {
- $row['running']['goods_running_storage'] = intval($row['running']['goods_running_storage']);
- }
- if (!empty($row['goods_attribute_json'])) {
- $row['goods_attribute_json'] = json_decode($row['goods_attribute_json']);
- }
- if (!empty($row['goods_category']) && $row['goods_category'] == 'INDEX') {
- $row['goods_recommend_index'] = 'INDEX';
- }
- }
- return json_success('', compact('rows', 'page', 'pageSize', 'total', 'goodsOnTotal', 'goodsWarningTotal'));
- }
- public static function selectSpecial(Request $request)
- {
- $page = $request->get('page');
- $pageSize = $request->get('pageSize');
- $goodsName = $request->get('goods_name', '');
- $categoryId = $request->get('join_goods_category_id', null);
- if ($categoryId == null) {
- $categoryId = [65, 43];
- } elseif (is_string($categoryId)) {
- $categoryId = [$categoryId];
- }
- $rows = Goods::with([
- 'category' => function ($query) {
- $query->select('category_id', 'category_name');
- },
- 'running' => function ($query) {
- $query->select('join_running_goods_id', 'goods_running_storage');
- },
- 'supplier' => function ($query) {
- $query->select('supplier_id', 'supplier_name');
- },
- 'user' => function ($query) {
- $query->select('user_id', 'user_name');
- },
- 'updateUser' => function ($query) {
- $query->select('user_id', 'user_name');
- }
- ])->select('goods_id', 'join_goods_category_id', 'join_goods_supplier_id', 'creator_user_id', 'updator_user_id', 'goods_status', 'goods_sales_price', 'goods_category', 'goods_name', 'goods_title', 'goods_cover', 'goods_sort', 'goods_addtimes', 'goods_updatetimes')
- ->when($goodsName != '', function ($query) use ($goodsName) {
- $query->where(function ($q) use ($goodsName) {
- $q->where('goods_name', 'like', '%' . $goodsName . '%');
- // ->OrWhere('goods_title', 'like', '%' . $goodsName . '%');
- });
- })->whereIn('join_goods_category_id', $categoryId)
- ->orderBy('goods_sort', 'DESC')
- ->orderBy('goods_addtimes', 'DESC')
- ->forPage($page, $pageSize)
- ->get()
- ->toArray();
- $totalModel = Goods::when($goodsName != '', function ($query) use ($goodsName) {
- $query->where(function ($q) use ($goodsName) {
- $q->where('goods_name', 'like', '%' . $goodsName . '%');
- });
- })->leftJoin('goods_running', 'goods_running.join_running_goods_id', '=', 'goods.goods_id')
- ->whereIn('join_goods_category_id', $categoryId);
- $goodsOnTotalModel = clone $totalModel;
- $goodsOnTotal = $goodsOnTotalModel->where('goods_status', 'ON')->count();
- $goodsWarningTotalModel = clone $totalModel;
- $goodsWarningTotal = $goodsWarningTotalModel->where('goods_running_storage', '<=', 2)->count();
- $total = $totalModel->count();
- foreach ($rows as &$row) {
- $row['goods_cover'] = getenv('STORAGE_DOMAIN') . $row['goods_cover'];
- if (isset($row['running'])) {
- $row['running']['goods_running_storage'] = intval($row['running']['goods_running_storage']);
- }
- }
- return json_success('', compact('rows', 'page', 'pageSize', 'total', 'goodsOnTotal', 'goodsWarningTotal'));
- }
- public static function selectPicking(Request $request)
- {
- $page = $request->get('page');
- $pageSize = $request->get('pageSize');
- $goodsName = $request->get('goods_name', '');
- $categorySuperId = $request->get('category_super_id', '');
- $categoryIds = $request->get('join_goods_category_id', []);
- if (!empty($categorySuperId) && is_array($categoryIds)) {
- $category = SysCategory::where('category_id', $categorySuperId)->first();
- if (empty($category->category_super_path)) {
- $category->category_super_path = '#' . $categorySuperId . '#';
- } else {
- $category->category_super_path = $category->category_super_path . '#' . $categorySuperId . '#';
- }
- $categoryIds = SysCategory::where('category_super_path', 'like', '%' . $category->category_super_path)->pluck('category_id');
- $categoryIds = $categoryIds ? $categoryIds->toArray() : [];
- $categoryIds = array_merge($categoryIds, [$categorySuperId]);
- } elseif (!is_array($categoryIds)) {
- $categoryIds = [$categoryIds];
- }
- $rows = Goods::with([
- 'category' => function ($query) {
- $query->select('category_id', 'category_name');
- },
- 'running' => function ($query) {
- $query->select('join_running_goods_id', 'goods_running_storage');
- },
- 'supplier' => function ($query) {
- $query->select('supplier_id', 'supplier_name');
- },
- 'user' => function ($query) {
- $query->select('user_id', 'user_name');
- },
- 'updateUser' => function ($query) {
- $query->select('user_id', 'user_name');
- }
- ])->select('goods_id', 'join_goods_category_id', 'join_goods_supplier_id', 'creator_user_id', 'updator_user_id', 'goods_status', 'goods_sales_price', 'goods_category', 'goods_name', 'goods_title', 'goods_cover', 'goods_sort', 'goods_addtimes', 'goods_updatetimes')
- ->when($goodsName != '', function ($query) use ($goodsName) {
- $query->where(function ($q) use ($goodsName) {
- $q->where('goods_name', 'like', '%' . $goodsName . '%')
- ->OrWhere('goods_title', 'like', '%' . $goodsName . '%');
- });
- })->whereIn('join_goods_category_id', $categoryIds)
- ->orderBy('goods_sort', 'DESC')
- ->orderBy('goods_addtimes', 'DESC')
- ->forPage($page, $pageSize)
- ->get()
- ->toArray();
- $totalModel = Goods::when($goodsName != '', function ($query) use ($goodsName) {
- $query->where(function ($q) use ($goodsName) {
- $q->where('goods_name', 'like', '%' . $goodsName . '%')
- ->OrWhere('goods_title', 'like', '%' . $goodsName . '%');
- });
- })->leftJoin('goods_running', 'goods_running.join_running_goods_id', '=', 'goods.goods_id')
- ->whereIn('join_goods_category_id', $categoryIds);
- $goodsOnTotalModel = clone $totalModel;
- $goodsOnTotal = $goodsOnTotalModel->where('goods_status', 'ON')->count();
- $goodsWarningTotalModel = clone $totalModel;
- $goodsWarningTotal = $goodsWarningTotalModel->where('goods_running_storage', '<=', 2)->count();
- $total = $totalModel->count();
- foreach ($rows as &$row) {
- $row['goods_cover'] = getenv('STORAGE_DOMAIN') . $row['goods_cover'];
- if (isset($row['running'])) {
- $row['running']['goods_running_storage'] = intval($row['running']['goods_running_storage']);
- }
- }
- return json_success('', compact('rows', 'page', 'pageSize', 'total', 'goodsOnTotal', 'goodsWarningTotal'));
- }
- public static function selectPackage(Request $request)
- {
- $page = $request->get('page');
- $pageSize = $request->get('pageSize');
- $goodsName = $request->get('goods_name', '');
- $categoryId = $request->get('join_goods_category_id', null);
- $rows = Goods::with([
- 'category' => function ($query) {
- $query->select('category_id', 'category_name');
- },
- 'running' => function ($query) {
- $query->select('join_running_goods_id', 'goods_running_storage');
- },
- 'supplier' => function ($query) {
- $query->select('supplier_id', 'supplier_name');
- },
- 'user' => function ($query) {
- $query->select('user_id', 'user_name');
- },
- 'updateUser' => function ($query) {
- $query->select('user_id', 'user_name');
- }
- ])->select('goods_id', 'join_goods_category_id', 'join_goods_supplier_id', 'creator_user_id', 'updator_user_id', 'goods_status', 'goods_sales_price', 'goods_category', 'goods_name', 'goods_title', 'goods_cover', 'goods_sort', 'goods_addtimes', 'goods_updatetimes')
- ->when($goodsName != '', function ($query) use ($goodsName) {
- $query->where(function ($q) use ($goodsName) {
- $q->where('goods_name', 'like', '%' . $goodsName . '%')
- ->OrWhere('goods_title', 'like', '%' . $goodsName . '%');
- });
- })->when($categoryId != null, function ($query) use ($categoryId) {
- $query->where('join_goods_category_id', $categoryId);
- })
- ->where('goods_classify', 'PACKAGE')
- ->orderBy('goods_sort', 'DESC')
- ->orderBy('goods_addtimes', 'DESC')
- ->forPage($page, $pageSize)
- ->get()
- ->toArray();
- $totalModel = Goods::when($goodsName != '', function ($query) use ($goodsName) {
- $query->where(function ($q) use ($goodsName) {
- $q->where('goods_name', 'like', '%' . $goodsName . '%')
- ->OrWhere('goods_title', 'like', '%' . $goodsName . '%');
- });
- })->when($categoryId != null, function ($query) use ($categoryId) {
- $query->where('join_goods_category_id', $categoryId);
- })->leftJoin('goods_running', 'goods_running.join_running_goods_id', '=', 'goods.goods_id')
- ->where('goods_classify', 'PACKAGE');
- $goodsOnTotalModel = clone $totalModel;
- $goodsOnTotal = $goodsOnTotalModel->where('goods_status', 'ON')->count();
- $goodsWarningTotalModel = clone $totalModel;
- $goodsWarningTotal = $goodsWarningTotalModel->where('goods_running_storage', '<=', 2)->count();
- $total = $totalModel->count();
- foreach ($rows as &$row) {
- $row['goods_cover'] = getenv('STORAGE_DOMAIN') . $row['goods_cover'];
- if (isset($row['running'])) {
- $row['running']['goods_running_storage'] = intval($row['running']['goods_running_storage']);
- }
- }
- return json_success('', compact('rows', 'page', 'pageSize', 'total', 'goodsOnTotal', 'goodsWarningTotal'));
- }
- /**
- * @Desc 下拉选择服务商品
- * @Author Gorden
- * @Date 2024/4/24 13:32
- *
- * @param Request $request
- * @return Response
- */
- public static function selectList(Request $request, $goodsClassify = "SERVICE")
- {
- $keywords = $request->get('keywords', '');
- $isSupportAppointment = $request->get('is_support_appointment', '');
- // if (!$keywords){
- // return json_success('暂无数据');
- // }
- // $categoryIds = SysCategory::whereIn('category_super_id', [5, 31, 32, 42, 66, 70, 72])->pluck('category_id');
- $goods = Goods::with('sku')
- // ->whereIn('join_goods_category_id', $categoryIds)
- ->when($keywords != '', function ($query) use ($keywords) {
- $query->where('goods_name', 'like', "%" . $keywords . "%");
- })
- ->when($goodsClassify != '', function ($query) use ($goodsClassify) {
- if ($goodsClassify == 'NOPACKAGE') {
- $query->where('goods_classify', '<>', 'PACKAGE');
- } else if ($goodsClassify == 'SERVICE') {
- $query->whereIn('goods_classify', ['SERVICE', 'CHNMED', 'CHNNCD']);
- } else {
- $query->where('goods_classify', $goodsClassify);
- }
- })
- ->when($isSupportAppointment != '', function ($query) use ($isSupportAppointment) {
- $query->where('is_support_appointment', $isSupportAppointment);
- })
- ->select('goods_id', 'goods_name', 'goods_sales_price', 'join_goods_category_id', 'goods_attribute_json')
- ->get()
- ->toArray();
- foreach ($goods as &$good) {
- if (!empty($good['sku'])) {
- foreach ($good['sku'] as $key => $sku) {
- if (!empty($sku['goods_sku_specs_json'])) {
- $good['sku'][$key]['goods_sku_specs_json'] = json_decode($sku['goods_sku_specs_json']);
- $skuName = '';
- foreach ($good['sku'][$key]['goods_sku_specs_json'] as $specsKey => $skuSpecs) {
- if (is_array($skuSpecs)) {
- $skuName = $skuName . ' ' . $specsKey . ':' . implode(' ', $skuSpecs) . ';';
- } else {
- $skuName = $skuName . ' ' . $specsKey . ':' . $skuSpecs . ';';
- }
- }
- $good['sku'][$key]['sku_name'] = $skuName;
- }
- }
- } else {
- $good['sku'] = [];
- }
- $good['premisses'] = [];
- if (!empty($good['goods_attribute_json'])) {
- $attributeJson = json_decode($good['goods_attribute_json'], true);
- if (isset($attributeJson['premisses'])) {
- $premisses = SysDept::when(!empty($attributeJson['premisses']), function ($query) use ($attributeJson) {
- $query->whereIn('dept_id', $attributeJson['premisses']);
- })->where('dept_category', '营业场所')
- ->select('dept_id', 'dept_name')
- ->get();
- $good['premisses'] = $premisses;
- }
- }
- }
- return json_success('', $goods);
- }
- public static function selectCascaderList(Request $request)
- {
- $categoryIds = $request->get('category_id', '');
- $categorySuperId = $request->get('category_super_id', '');
- $joinGoodsCategoryId = $request->get('join_goods_category_id', '');
- $type = $request->get('type', '');
- if (!$categoryIds && !$categorySuperId && !$joinGoodsCategoryId) {
- return json_fail('参数异常');
- }
- $data = [];
- $categorys = [];
- if (!empty($categoryIds)) {
- $categorys = SysCategory::whereIn('category_id', $categoryIds)
- ->whereIn('category_status', ['ACTIVED', 'UNSHOW'])
- ->select('category_id as id', 'category_name as name', 'category_super_id as pid', 'category_super_path')
- ->orderBy('category_sort', 'DESC')
- ->get()
- ->toArray();
- $data = array_merge($data, $categorys);
- } else if (!empty($categorySuperId)) {
- $categorys = SysCategory::where('category_super_id', $categorySuperId)
- ->whereIn('category_status', ['ACTIVED', 'UNSHOW'])
- ->select('category_id as id', 'category_name as name', 'category_super_id as pid', 'category_super_path')
- ->orderBy('category_sort', 'DESC')
- ->get()
- ->toArray();
- $data = array_merge($data, $categorys);
- }
- foreach ($categorys as $category) {
- // if(empty($category['category_super_path'])){
- $category['category_super_path'] = '#' . $category['id'] . '#';
- // }
- $subCategory = SysCategory::where('category_super_path', 'like', '%' . $category['category_super_path'] . '%')
- ->whereIn('category_status', ['ACTIVED', 'UNSHOW'])
- ->select('category_id as id', 'category_name as name', 'category_super_id as pid', 'category_super_path')
- ->orderBy('category_sort', 'DESC')
- ->get()
- ->toArray();
- $data = array_merge($data, $subCategory);
- }
- if (empty($data) && !empty($joinGoodsCategoryId)) {
- $goodsCategoryIds = $joinGoodsCategoryId;
- } else {
- $goodsCategoryIds = array_column($data, 'id');
- }
- $goods = Goods::with([
- 'sku' => function ($query) {
- $query->where('goods_sku_status', 'ON');
- }
- ])
- ->leftJoin('goods_running', 'goods_running.join_running_goods_id', '=', 'goods.goods_id')
- ->where('goods_running.goods_running_storage', '>', 0)
- ->whereIn('join_goods_category_id', $goodsCategoryIds)
- ->where('goods_status', 'ON');
- if ($type == 'dishes') {
- $uid = JwtToken::getCurrentId();
- $user = SysUser::where('user_id', $uid)->first();
- $restaurant = SysDept::where('dept_category', '餐厅')->where(function ($query) use ($user) {
- $query->where('dept_id', $user->join_user_dept_id)->orWhere('dept_super_id', $user->join_user_dept_id);
- })->first();
- $supplier = Supplier::where('join_supplier_dept_id', $restaurant->dept_id)->first();
- if ($supplier) {
- $goods = $goods->where('join_goods_supplier_id', $supplier->supplier_id);
- }
- }
- $goods = $goods->select('goods_id', 'goods_id as id', 'goods_name as name', 'join_goods_category_id as pid', 'goods_attribute_json', 'goods_classify', 'goods_sales_price', 'goods_cover', 'goods_running.goods_running_storage')
- ->orderBy('goods_sort', 'DESC')
- ->orderBy('goods_addtimes', 'DESC')
- ->get()
- ->toArray();
- foreach ($goods as &$good) {
- $good['goods_cover'] = getenv('STORAGE_DOMAIN') . $good['goods_cover'];
- $good['goods_running_storage'] = intval($good['goods_running_storage']);
- $good['nbr'] = 0;
- if (!empty($good['sku'])) {
- foreach ($good['sku'] as $key => $sku) {
- if (!empty($sku['goods_sku_specs_json'])) {
- $good['sku'][$key]['goods_sku_specs_json'] = json_decode($sku['goods_sku_specs_json']);
- $skuName = '';
- foreach ($good['sku'][$key]['goods_sku_specs_json'] as $specsKey => $skuSpecs) {
- $keyStr = ($specsKey == '规格') ? '' : ($specsKey . ':');
- if (is_array($skuSpecs)) {
- $skuName = $skuName . $keyStr . ' ' . implode(' ', $skuSpecs) . '; ';
- } else {
- $skuName = $skuName . $keyStr . ' ' . $skuSpecs . '; ';
- }
- }
- $good['sku'][$key]['sku_name'] = rtrim($skuName, '; ');
- }
- }
- } else {
- $good['sku'] = [];
- }
- $good['premisses'] = [];
- if (!empty($good['goods_attribute_json'])) {
- $attributeJson = json_decode($good['goods_attribute_json'], true);
- if (isset($attributeJson['premisses'])) {
- $premisses = SysDept::when(!empty($attributeJson['premisses']), function ($query) use ($attributeJson) {
- $query->whereIn('dept_id', $attributeJson['premisses']);
- })->where('dept_category', '营业场所')
- ->select('dept_id', 'dept_name')
- ->get();
- $good['premisses'] = $premisses;
- }
- }
- }
- $data = array_merge($data, $goods);
- $tree = new Tree($data);
- $cascaderData = $tree->getTree();
- foreach ($cascaderData as $key1 => $cascader1) {
- if (isset($cascader1['children'])) {
- foreach ($cascader1['children'] as $key2 => $cascader2) {
- if (isset($cascader2['children'])) {
- foreach ($cascader2['children'] as $key3 => $cascader3) {
- if (isset($cascader3['children'])) {
- foreach ($cascader3['children'] as $key4 => $cascader4) {
- if (!isset($cascader4['goods_id'])) {
- unset($cascaderData[$key1]['children'][$key2]['children'][$key3]['children'][$key4]);
- }
- if (isset($cascader4['goods_id']) && empty($cascader4['sku'])) {
- unset($cascaderData[$key1]['children'][$key2]['children'][$key3]['children'][$key4]);
- }
- }
- } else if (!isset($cascader3['goods_id'])) {
- unset($cascaderData[$key1]['children'][$key2]['children'][$key3]);
- }
- if (isset($cascader3['goods_id']) && empty($cascader3['sku'])) {
- unset($cascaderData[$key1]['children'][$key2]['children'][$key3]);
- continue;
- }
- if (isset($cascader3['children']) && count($cascaderData[$key1]['children'][$key2]['children']) == 0) {
- unset($cascaderData[$key1]['children'][$key2]);
- }
- if (isset($cascader3['children']) && count($cascaderData[$key1]['children'][$key2]['children'][$key3]['children']) > 0) {
- $cascaderData[$key1]['children'][$key2]['children'][$key3]['children'] = array_values($cascaderData[$key1]['children'][$key2]['children'][$key3]['children']);
- }
- }
- } else if (!isset($cascader2['goods_id'])) {
- unset($cascaderData[$key1]['children'][$key2]);
- }
- if (isset($cascader2['goods_id']) && empty($cascader2['sku'])) {
- unset($cascaderData[$key1]['children'][$key2]);
- continue;
- }
- if (isset($cascader2['children']) && count($cascaderData[$key1]['children'][$key2]['children']) == 0) {
- unset($cascaderData[$key1]['children'][$key2]);
- }
- if (isset($cascader2['children']) && isset($cascaderData[$key1]['children'][$key2]) && count($cascaderData[$key1]['children'][$key2]['children']) > 0) {
- $cascaderData[$key1]['children'][$key2]['children'] = array_values($cascaderData[$key1]['children'][$key2]['children']);
- }
- }
- } else if (!isset($cascader1['goods_id'])) {
- unset($cascaderData[$key1]);
- }
- if (isset($cascader1['goods_id']) && empty($cascader1['sku'])) {
- unset($cascaderData[$key1]);
- continue;
- }
- if (isset($cascader1['children']) && count($cascaderData[$key1]['children']) == 0) {
- unset($cascaderData[$key1]);
- }
- if (isset($cascader1['children']) && isset($cascaderData[$key1]) && count($cascaderData[$key1]['children']) > 0) {
- $cascaderData[$key1]['children'] = array_values($cascaderData[$key1]['children']);
- }
- }
- $cascaderData = array_values($cascaderData);
- return json_success('success', $cascaderData);
- }
- /**
- * @Desc 商品详情
- * @Author Gorden
- * @Date 2024/3/28 10:25
- *
- * @param $goodsId
- * @return Response
- */
- public static function info($goodsId)
- {
- try {
- // 商品主表
- $main = Goods::with('category')->where('goods_id', $goodsId)->first();
- if (!empty($main)) {
- $main = $main->toArray();
- $main['goods_sku_json'] = json_decode($main['goods_sku_json'], true);
- $main['specList'] = [];
- foreach ($main['goods_sku_json'] as $key => $sku) {
- $main['specList'][] = [
- 'label' => $key,
- 'tags' => $sku
- ];
- }
- } else {
- $main = [];
- }
- // 详情表
- $detail = GoodsDetail::where('join_detail_goods_id', $goodsId)->first();
- if (!empty($detail)) {
- $detail = $detail->toArray();
- } else {
- $detail = [];
- }
- // 标签表
- $label = GoodsLabel::where('join_label_goods_id', $goodsId)->first();
- if (!empty($label)) {
- $label = $label->toArray();
- } else {
- $label = [];
- }
- // Running表
- $running = GoodsRunning::where('join_running_goods_id', $goodsId)->first();
- if (!empty($running)) {
- $running = $running->toArray();
- if (!empty($running['goods_running_off_json']) && is_json($running['goods_running_off_json'])) {
- $goodsRunningOffJson = json_decode($running['goods_running_off_json'], true);
- $running['goods_off_addtimes'] = isset($goodsRunningOffJson['time']) ? date("Y-m-d H:i", $goodsRunningOffJson['time']) : '';
- }
- $running['goods_running_storage'] = !empty($running['goods_running_storage']) ? intval($running['goods_running_storage']) : '';
- $running['goods_running_sale'] = !empty($running['goods_running_sale']) ? intval($running['goods_running_sale']) : '';
- } else {
- $running = [];
- }
- // Sku表
- $skus = GoodsSku::where('join_sku_goods_id', $goodsId)->where('goods_sku_status', 'ON')->get();
- if (!empty($skus)) {
- $skus = $skus->toArray();
- $submitList = [];
- foreach ($skus as $key => $sku) {
- $skuSpecsJson = json_decode($sku['goods_sku_specs_json'], true);
- $skuSpecs = '';
- $skuNameValue = [];
- $i = 1;
- foreach ($skuSpecsJson as $k => $item) {
- if (is_array($item)) {
- $item = implode(',', $item);
- }
- $skuSpecs = $skuSpecs . $item . ',';
- $skuNameKey = 'skuName' . $i;
- $skuValueKey = 'skuValue' . $i;
- $skuNameValue[$skuNameKey] = $k;
- $skuNameValue[$skuValueKey] = $item;
- $skuNameValue[$k] = $item;
- $i++;
- }
- $storage = json_decode($sku['goods_sku_storage_json'], true);
- $priceStorage = [
- 'sku_id' => $sku['goods_sku_id'],
- 'sku' => rtrim($skuSpecs, ',') ?? '',
- 'stock' => $storage['storage'] ?? '',
- 'price' => $sku['goods_sku_sales_price'] ?? '',
- ];
- $submitList[] = array_merge($skuNameValue, $priceStorage);
- }
- $skus['submitList'] = $submitList;
- } else {
- $skus = [];
- }
- // 供应商表
- $supplier = [];
- if (isset($main['join_goods_supplier_id']) && !empty($main['join_goods_supplier_id'])) {
- $supplierModel = Supplier::where('supplier_id', $main['join_goods_supplier_id'])->select('supplier_id', 'supplier_name')->first();
- if (!empty($supplierModel)) {
- $supplier = $supplierModel->toArray();
- }
- }
- // 合并数据
- $data = array_merge($main, $detail, $label, $running, ['sku' => $skus], ['supplier' => $supplier]);
- $data['goods_sku_json_label'] = [];
- $data['goods_label'] = !empty($data['goods_label']) ? explode(',', $data['goods_label']) : [];
- $data['goods_json'] = $data['goods_json'] ? json_decode($data['goods_json'], true) : [];
- $data['goods_cover'] = getenv('STORAGE_DOMAIN') . $data['goods_cover'];
- if (!empty($data['goods_category']) && $data['goods_category'] == 'INDEX') {
- $data['goods_recommend_index'] = 'INDEX';
- }
- // 创建者
- $data['creator_username'] = '';
- if (!empty($data['creator_user_id'])) {
- $data['creator_username'] = SysUser::where('user_id', $data['creator_user_id'])->value('user_name');
- }
- $data['updator_username'] = '';
- if (!empty($data['updator_user_id'])) {
- $data['updator_username'] = SysUser::where('user_id', $data['updator_user_id'])->value('user_name');
- }
- if (!empty($data['goods_detail_slider_json'])) {
- $data['goods_detail_slider_json'] = json_decode($data['goods_detail_slider_json'], true);
- // ……
- if (isset($data['goods_detail_slider_json']['slider'])) {
- $data['goods_detail_slider_json'] = explode(',', $data['goods_detail_slider_json']['slider']);
- }
- $slider = '';
- foreach ($data['goods_detail_slider_json'] as $item) {
- $slider .= getenv('STORAGE_DOMAIN') . $item . ',';
- }
- $data['goods_detail_slider_json'] = rtrim($slider, ',');
- }
- $extendJson = [];
- if (!empty($data['goods_attribute_json'])) {
- $extendJson = json_decode($data['goods_attribute_json'], true);
- $data['goods_attribute_json'] = $extendJson;
- if (isset($extendJson['premisses'])) {
- $data['goods_premisses'] = $extendJson['premisses'];
- $data['goods_premisses_str'] = SysDept::whereIn('dept_id', $extendJson['premisses'])->pluck('dept_name');
- }
- if (isset($extendJson['bg'])) {
- $data['goods_theme_color'] = $extendJson['bg'];
- }
- if (isset($extendJson['icon'])) {
- $data['goods_theme_icon'] = getenv('STORAGE_DOMAIN') . $extendJson['icon'];
- }
- if (isset($extendJson['service_premises_id'])) {
- $data['goods_service_premises'] = $extendJson['service_premises_id'];
- }
- }
- if (!empty($extendJson['coupon'])) {
- $data['coupon_list'] = $extendJson['coupon'];
- }
- $data['express_json'] = [];
- if (!empty($data['goods_express_json'])) {
- $goodsExpressJson = json_decode($data['goods_express_json'], true);
- if (isset($goodsExpressJson['express']) && $goodsExpressJson['express'] == 'Y') {
- $data['express_json'][] = 'express';
- }
- if (isset($goodsExpressJson['self']) && $goodsExpressJson['self'] == 'Y') {
- $data['express_json'][] = 'self';
- }
- if (isset($goodsExpressJson['arrival']) && $goodsExpressJson['arrival'] == 'Y') {
- $data['express_json'][] = 'arrival';
- }
- }
- $data['appointment_times'] = [];
- if ($data['is_support_appointment'] == 'Y' && isset($extendJson['dates'])) {
- $data['dates'] = $extendJson['dates'] ?? [];
- if (isset($extendJson['times'])) {
- $times = [];
- foreach ($extendJson['times'] as $time) {
- if (!empty($time['duration'])) {
- $startEndTime = explode('-', $time['duration']);
- $times[] = [
- 'person' => $time['person'],
- 'appointmentTimeStart' => $startEndTime[0],
- 'appointmentTimeEnd' => $startEndTime[1],
- ];
- }
- }
- $data['appointment_times'] = $times;
- }
- if (isset($extendJson['time'])) {
- $extendJsonTime = explode('至', $extendJson['time']);
- if (isset($extendJsonTime[0]) && isset($extendJsonTime[1])) {
- $data['work_time'] = [
- date('Y-m-d H:i:s', strtotime(date('Y-m-d ') . $extendJsonTime[0])),
- date('Y-m-d H:i:s', strtotime(date('Y-m-d ') . $extendJsonTime[1])),
- ];
- }
- }
- // if (isset($extendJson['teachers'])){
- // $data['teachers'] = $extendJson['teachers'];
- // }
- // if (isset($extendJson['address'])) {
- // $data['address'] = $extendJson['address'];
- // }
- // if (isset($extendJson['min-count'])){
- // $data['min_count'] = $extendJson['min-count'];
- // }
- // if (isset($extendJson['position'])) {
- // $data['position'] = $extendJson['position'];
- // }
- if (isset($extendJson['label'])) {
- $data['appointment_label'] = $extendJson['label'];
- }
- }
- if (isset($extendJson['month']) && $data['is_support_appointment'] == 'Y') {
- $times = [];
- foreach ($extendJson['month'] as $monKey => $months) {
- foreach ($months as $day => $person) {
- $times[] = [
- 'days' => date('Y-m-d', strtotime($monKey . $day)),
- 'person' => $person
- ];
- }
- }
- $data['appointment_times'] = $times;
- $data['travel_begin'] = $extendJson['travel-begin'];
- $data['travel_day'] = $extendJson['travel-day'];
- $data['travel_night'] = $extendJson['travel-night'];
- $data['travel_trans'] = $extendJson['travel-trans'];
- }
- if (!empty($data['goods_json']) && $data['join_goods_category_id'] == 65) {
- // foreach ($data['goods_json'] as $key => $datum) {
- // $data['goods_json'][$key]['color'] = rgbToHex($datum['color']);
- // }
- } elseif (!empty($data['goods_json']) && $data['join_goods_category_id'] == 43) {
- $goodsJsonNew = [];
- foreach ($data['goods_json'] as $key1 => $item1) {
- $itemsNew = [];
- if (isset($item1['items'])) {
- foreach ($item1['items'] as $key2 => $item2) {
- $itemsNew[] = [
- 'key' => $key2,
- 'params' => $item2
- ];
- }
- }
- $goodsJsonNew[] = [
- 'title' => $key1,
- 'service' => $item1['service'] ?? '',
- 'items' => $itemsNew
- ];
- }
- $data['goods_json'] = $goodsJsonNew;
- }
- if (isset($extendJson['teachers'])) {
- $data['teachers'] = $extendJson['teachers'];
- }
- if (isset($extendJson['address'])) {
- $data['address'] = $extendJson['address'];
- }
- if (isset($extendJson['min-count'])) {
- $data['min_count'] = $extendJson['min-count'];
- }
- if (isset($extendJson['max-limit'])) {
- $data['max_limit'] = $extendJson['max-limit'];
- }
- if (isset($extendJson['position'])) {
- $data['position'] = $extendJson['position'];
- }
- $data['goods_on_addtimes'] = date('Y-m-d\TH:i:s.u\Z', $data['goods_on_addtimes'] - 60 * 60 * 8);
- // 详情表数据
- if (!empty($data['goods_detail_specs_json'])) {
- $specsJson = json_decode($data['goods_detail_specs_json'], true);
- foreach ($specsJson as $itemSpecsJson) {
- if (isset($itemSpecsJson['key']) && $itemSpecsJson['key'] == '课时') {
- $data['curriculum']['period'] = $itemSpecsJson['val'];
- } elseif (isset($itemSpecsJson['key']) && $itemSpecsJson['key'] == '群体') {
- $data['curriculum']['group'] = $itemSpecsJson['val'];
- } elseif (isset($itemSpecsJson['key']) && $itemSpecsJson['key'] == '课程') {
- $data['curriculum']['course'] = $itemSpecsJson['val'];
- }
- }
- }
- return json_success('', $data);
- } catch (\Exception $e) {
- dump($e->getMessage());
- return json_fail("查询错误~");
- }
- }
- /**
- * @Desc 商品详情
- * @Author Gorden
- * @Date 2024/3/28 10:25
- *
- * @param $goodsId
- * @return Response
- */
- public static function newCustomerInfo($goodsId)
- {
- try {
- // 商品主表
- $main = Goods::with('category')->where('goods_id', $goodsId)->first();
- if (!empty($main)) {
- $main = $main->toArray();
- $main['goods_sku_json'] = json_decode($main['goods_sku_json'], true);
- $main['specList'] = [];
- foreach ($main['goods_sku_json'] as $key => $sku) {
- $main['specList'][] = [
- 'label' => $key,
- 'tags' => $sku
- ];
- }
- } else {
- $main = [];
- }
- // 详情表
- $detail = GoodsDetail::where('join_detail_goods_id', $goodsId)->first();
- if (!empty($detail)) {
- $detail = $detail->toArray();
- } else {
- $detail = [];
- }
- // 标签表
- $label = GoodsLabel::where('join_label_goods_id', $goodsId)->first();
- if (!empty($label)) {
- $label = $label->toArray();
- } else {
- $label = [];
- }
- // Running表
- $running = GoodsRunning::where('join_running_goods_id', $goodsId)->first();
- if (!empty($running)) {
- $running = $running->toArray();
- if (!empty($running['goods_running_off_json']) && is_json($running['goods_running_off_json'])) {
- $goodsRunningOffJson = json_decode($running['goods_running_off_json'], true);
- $running['goods_off_addtimes'] = isset($goodsRunningOffJson['time']) ? date("Y-m-d H:i", $goodsRunningOffJson['time']) : '';
- }
- $running['goods_running_storage'] = !empty($running['goods_running_storage']) ? intval($running['goods_running_storage']) : '';
- $running['goods_running_sale'] = !empty($running['goods_running_sale']) ? intval($running['goods_running_sale']) : '';
- } else {
- $running = [];
- }
- // Sku表
- $skus = GoodsSku::where('join_sku_goods_id', $goodsId)->where('goods_sku_status', 'ON')->get();
- if (!empty($skus)) {
- $skus = $skus->toArray();
- $submitList = [];
- foreach ($skus as $key => $sku) {
- $skuSpecsJson = json_decode($sku['goods_sku_specs_json'], true);
- $skuSpecs = '';
- $skuNameValue = [];
- $i = 1;
- foreach ($skuSpecsJson as $k => $item) {
- if (is_array($item)) {
- $item = implode(',', $item);
- }
- $skuSpecs = $skuSpecs . $item . ',';
- $skuNameKey = 'skuName' . $i;
- $skuValueKey = 'skuValue' . $i;
- $skuNameValue[$skuNameKey] = $k;
- $skuNameValue[$skuValueKey] = $item;
- $skuNameValue[$k] = $item;
- $i++;
- }
- $storage = json_decode($sku['goods_sku_storage_json'], true);
- $priceStorage = [
- 'sku_id' => $sku['goods_sku_id'],
- 'sku' => rtrim($skuSpecs, ',') ?? '',
- 'stock' => $storage['storage'] ?? '',
- 'price' => $sku['goods_sku_sales_price'] ?? '',
- ];
- $submitList[] = array_merge($skuNameValue, $priceStorage);
- }
- $skus['submitList'] = $submitList;
- } else {
- $skus = [];
- }
- // 合并数据
- $data = array_merge($main, $detail, $label, $running, ['sku' => $skus]);
- $data['goods_sku_json_label'] = [];
- $data['goods_label'] = !empty($data['goods_label']) ? explode(',', $data['goods_label']) : [];
- $data['goods_json'] = $data['goods_json'] ? json_decode($data['goods_json'], true) : [];
- $data['goods_cover'] = getenv('STORAGE_DOMAIN') . $data['goods_cover'];
- if (!empty($data['goods_category']) && $data['goods_category'] == 'INDEX') {
- $data['goods_recommend_index'] = 'INDEX';
- }
- // 创建者
- $data['creator_username'] = '';
- if (!empty($data['creator_user_id'])) {
- $data['creator_username'] = SysUser::where('user_id', $data['creator_user_id'])->value('user_name');
- }
- if (!empty($data['goods_detail_slider_json'])) {
- $data['goods_detail_slider_json'] = json_decode($data['goods_detail_slider_json'], true);
- // ……
- if (isset($data['goods_detail_slider_json']['slider'])) {
- $data['goods_detail_slider_json'] = explode(',', $data['goods_detail_slider_json']['slider']);
- }
- $slider = '';
- foreach ($data['goods_detail_slider_json'] as $item) {
- $slider .= getenv('STORAGE_DOMAIN') . $item . ',';
- }
- $data['goods_detail_slider_json'] = rtrim($slider, ',');
- }
- $extendJson = [];
- if (!empty($data['goods_attribute_json'])) {
- $extendJson = json_decode($data['goods_attribute_json'], true);
- $data['goods_attribute_json'] = $extendJson;
- }
- if (!empty($extendJson['coupon'])) {
- $data['coupon_list'] = $extendJson['coupon'];
- }
- $data['express_json'] = [];
- if (!empty($data['goods_express_json'])) {
- $goodsExpressJson = json_decode($data['goods_express_json'], true);
- if (isset($goodsExpressJson['express']) && $goodsExpressJson['express'] == 'Y') {
- $data['express_json'][] = 'express';
- }
- if (isset($goodsExpressJson['self']) && $goodsExpressJson['self'] == 'Y') {
- $data['express_json'][] = 'self';
- }
- if (isset($goodsExpressJson['arrival']) && $goodsExpressJson['arrival'] == 'Y') {
- $data['express_json'][] = 'arrival';
- }
- }
- // 详情表数据
- if (!empty($data['goods_detail_specs_json'])) {
- $specsJson = json_decode($data['goods_detail_specs_json'], true);
- foreach ($specsJson as $itemSpecsJson) {
- if (isset($itemSpecsJson['key']) && $itemSpecsJson['key'] == '课时') {
- $data['curriculum']['period'] = $itemSpecsJson['val'];
- } elseif (isset($itemSpecsJson['key']) && $itemSpecsJson['key'] == '群体') {
- $data['curriculum']['group'] = $itemSpecsJson['val'];
- } elseif (isset($itemSpecsJson['key']) && $itemSpecsJson['key'] == '课程') {
- $data['curriculum']['course'] = $itemSpecsJson['val'];
- }
- }
- }
- return json_success('', $data);
- } catch (\Exception $e) {
- dump($e->getMessage());
- return json_fail("查询错误~");
- }
- }
- public static function infoPackage($goodsId)
- {
- try {
- // 商品主表
- $main = Goods::with('category')->where('goods_id', $goodsId)->first();
- if (!empty($main)) {
- $main = $main->toArray();
- $main['goods_sku_json'] = json_decode($main['goods_sku_json'], true);
- $main['specList'] = [];
- foreach ($main['goods_sku_json'] as $key => $sku) {
- $main['specList'][] = [
- 'label' => $key,
- 'tags' => $sku
- ];
- }
- } else {
- $main = [];
- }
- // 详情表
- $detail = GoodsDetail::where('join_detail_goods_id', $goodsId)->first();
- if (!empty($detail)) {
- $detail = $detail->toArray();
- } else {
- $detail = [];
- }
- // 标签表
- $label = GoodsLabel::where('join_label_goods_id', $goodsId)->first();
- if (!empty($label)) {
- $label = $label->toArray();
- } else {
- $label = [];
- }
- // Running表
- $running = GoodsRunning::where('join_running_goods_id', $goodsId)->first();
- if (!empty($running)) {
- $running = $running->toArray();
- if (!empty($running['goods_running_off_json']) && is_json($running['goods_running_off_json'])) {
- $goodsRunningOffJson = json_decode($running['goods_running_off_json'], true);
- $running['goods_off_addtimes'] = isset($goodsRunningOffJson['time']) ? date("Y-m-d H:i", $goodsRunningOffJson['time']) : '';
- }
- } else {
- $running = [];
- }
- // 组件表
- $component = GoodsComponent::where('join_component_master_goods_id', $goodsId)->get()->toArray();
- $componentArr['join_component_goods_id'] = [];
- $componentArr['goodsContentList'] = [];
- if ($component) {
- $ids = [];
- $contentList = [];
- foreach ($component as $item) {
- if (empty($item['join_component_goods_id'])) {
- continue;
- }
- $skus = GoodsSku::with([
- 'goods' => function ($query) {
- $query->select('goods_id', 'goods_cover');
- }
- ])->where('join_sku_goods_id', $item['join_component_goods_id'])->where('goods_sku_status', 'ON')
- ->select('goods_sku_id', 'join_sku_goods_id', 'goods_sku_specs_json', 'goods_sku_sales_price')
- ->get()
- ->toArray();
- $goodsCover = '';
- foreach ($skus as $key => $sku2) {
- if (!empty($sku2['goods_sku_specs_json'])) {
- $skus[$key]['goods_sku_specs_json'] = json_decode($sku2['goods_sku_specs_json'], true);
- $skuName = '';
- foreach ($skus[$key]['goods_sku_specs_json'] as $specsKey => $skuSpecs) {
- if (is_array($skuSpecs)) {
- $skuName = $skuName . implode(' ', $skuSpecs) . ',';
- } else {
- $skuName = $skuName . $skuSpecs . ',';
- }
- }
- $skus[$key]['sku_name'] = rtrim($skuName, ',');
- }
- if (!empty($sku2['goods'])) {
- $goodsCover = getenv('STORAGE_DOMAIN') . $sku2['goods']['goods_cover'];
- }
- }
- $ids[] = $item['join_component_goods_id'];
- $configJson = json_decode($item['goods_component_json'], true);
- $contentList[] = [
- 'goods_id' => $item['join_component_goods_id'],
- 'goods_name' => $configJson['goods_name'] ?? '',
- 'goods_sales_price' => $item['goods_component_price'],
- 'nbr' => $configJson['nbr'] ?? 0,
- 'sku_id' => $configJson['sku_id'] ?? '',
- 'sku_name' => !empty($skuName) ? rtrim($skuName, ',') : '',
- 'skus' => $skus,
- 'goods_cover' => $goodsCover
- ];
- }
- $componentArr['join_component_goods_id'] = $ids;
- $componentArr['goodsContentList'] = $contentList;
- }
- // 合并数据
- $data = array_merge($main, $detail, $label, $running, $componentArr);
- $data['goods_sku_json_label'] = [];
- // 创建者
- $data['creator_username'] = '';
- if (!empty($data['creator_user_id'])) {
- $data['creator_username'] = SysUser::where('user_id', $data['creator_user_id'])->value('user_name');
- }
- $data['updator_username'] = '';
- if (!empty($data['updator_user_id'])) {
- $data['updator_username'] = SysUser::where('user_id', $data['updator_user_id'])->value('user_name');
- }
- $data['goods_label'] = !empty($data['goods_label']) ? explode(',', $data['goods_label']) : [];
- $data['goods_cover'] = getenv('STORAGE_DOMAIN') . $data['goods_cover'];
- if (!empty($data['goods_detail_slider_json'])) {
- $data['goods_detail_slider_json'] = json_decode($data['goods_detail_slider_json'], true);
- $slider = '';
- foreach ($data['goods_detail_slider_json'] as $item) {
- $slider .= getenv('STORAGE_DOMAIN') . $item . ',';
- }
- $data['goods_detail_slider_json'] = rtrim($slider, ',');
- }
- if (!empty($data['goods_attribute_json'])) {
- $goodsAttributeJson = json_decode($data['goods_attribute_json'], true);
- $data['goods_attribute_json'] = $goodsAttributeJson;
- if (isset($goodsAttributeJson['premisses'])) {
- $data['goods_premisses'] = $goodsAttributeJson['premisses'];
- $data['goods_premisses_str'] = SysDept::whereIn('dept_id', $goodsAttributeJson['premisses'])->pluck('dept_name');
- }
- // if (!empty($goodsAttributeJson['coupon'])) {
- // $data['coupon_list'] = $goodsAttributeJson['coupon'];
- // }
- $data['couponContentList'] = [];
- $data['coupon_id'] = [];
- if (!empty($goodsAttributeJson['coupon'])) {
- foreach ($goodsAttributeJson['coupon'] as $key => $coupon) {
- $data['coupon_id'][] = $key;
- $data['couponContentList'][] = [
- 'coupon_id' => $key,
- 'coupon_name' => $coupon['name'],
- 'nbr' => $coupon['num']
- ];
- }
- }
- }
- $data['goods_on_addtimes'] = date('Y-m-d\TH:i:s.u\Z', $data['goods_on_addtimes'] - 60 * 60 * 8);
- return json_success('', $data);
- } catch (\Exception $e) {
- dump($e->getTrace());
- return json_fail("查询错误~");
- }
- }
- /**
- * @Desc 添加商品
- * @Author Gorden
- * @Date 2024/3/11 10:20
- *
- * @param $params
- * @return Response
- */
- public static function insert($params): Response
- {
- Db::beginTransaction();
- try {
- $params['goods_id'] = "GD" . date('ymdHi') . random_string(4, 'up');
- // 主表
- self::mainInsert($params);
- // 商品详情表
- self::detailInsert($params);
- // 商品标签表
- self::labelInsert($params);
- // 产品运行控制信息表
- self::goodsRunningInsert($params);
- // sku表
- self::goodsSkuSet($params, 'insert');
- // 待上架状态,上架时间大于当前时间
- if ($params['goods_status'] == 'PENDING' && strtotime($params['goods_on_addtimes']) > time()) {
- $redis = Redis::connection();
- $key = date('YmdHi', strtotime($params['goods_on_addtimes']));
- $redis->sAdd(Goods::LISTING_KEY_PREFIX . $key, $params['goods_id']);
- }
- // 自动下架
- if (!empty($params['goods_running_off_type']) && $params['goods_running_off_type'] == 'T' && !empty($params['goods_off_addtimes'])) {
- $redis = Redis::connection();
- $key = Goods::LISTING_OFF_KEY_PREFIX . date('YmdHi', strtotime($params['goods_off_addtimes']));
- $redis->sAdd($key, $params['goods_id']);
- }
- Db::commit();
- } catch (\PDOException $e) {
- Db::rollBack();
- dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
- return json_fail('数据写入失败~');
- } catch (BusinessException $e) {
- Db::rollBack();
- dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
- return json_fail($e->getMessage());
- } catch (\Exception $e) {
- Db::rollBack();
- dump($e->getTrace());
- return json_fail('数据写入失败~');
- }
- _syslog("添加商品", "商品名【" . $params['goods_name'] . "】");
- return json_success('success');
- }
- public static function insertRecharge($params)
- {
- try {
- Db::beginTransaction();
- $goods = new Goods();
- $goods->goods_id = "GD" . date('ymdHi') . random_string(4, 'up');
- $goods->join_goods_category_id = 59;
- $goods->goods_classify = 'RECHARGE';
- $goods->goods_status = $params['goods_status'];
- $goods->goods_sort = $params['goods_sort'];
- $goods->goods_category = $params['goods_category'];
- $goods->goods_name = $params['goods_name'];
- $goods->goods_market_price = $params['goods_sales_price'];
- $goods->goods_sales_price = $params['goods_sales_price'];
- $goods->goods_sku_json = json_encode(['规格' => [$params['goods_sales_price'] . '元']]);
- $goods->goods_attribute_json = json_encode(['added' => ['nbr' => $params['goods_rate'] / 100, 'mode' => 'rate'], 'min-count' => 1]);
- $goods->goods_cover = '/images/app/common/null-service.png';
- $goods->goods_process_json = json_encode(['mode' => 'do_shopping']);
- $goods->goods_addtimes = time();
- $goods->save();
- $sku = new GoodsSku();
- $sku->join_sku_goods_id = $goods->goods_id;
- $sku->goods_sku_status = $params['goods_status'];
- $sku->goods_sku_specs_json = json_encode(['规格' => $params['goods_sales_price'] . '元']);
- $sku->goods_sku_market_price = $params['goods_sales_price'];
- $sku->goods_sku_sales_price = $params['goods_sales_price'];
- $sku->goods_sku_storage_json = json_encode(['storage' => 9999]);
- $sku->save();
- Db::commit();
- return json_success('success');
- } catch (\Exception $e) {
- Db::rollBack();
- return json_fail('添加充值产品失败');
- }
- }
- public static function updateRecharge($params)
- {
- try {
- Db::beginTransaction();
- $goods = Goods::where('goods_id', $params['goods_id'])->first();
- if (!$goods) {
- return json_fail("数据异常");
- }
- $goods->goods_market_price = $params['goods_sales_price'];
- $goods->goods_sales_price = $params['goods_sales_price'];
- $goods->goods_status = $params['goods_status'];
- $goods->goods_sku_json = json_encode(['规格' => [$params['goods_sales_price'] . '元']]);
- $goods->goods_sort = $params['goods_sort'];
- $goods->goods_updatetimes = time();
- $goods->updator_user_id = JwtToken::getCurrentId();
- if (!empty($goods->goods_attribute_json)) {
- $attributeJson = json_decode($goods->goods_attribute_json, true);
- $attributeJson['added']['nbr'] = $params['goods_rate'] / 100;
- $goods->goods_attribute_json = json_encode($attributeJson);
- }
- $goods->save();
- $sku = GoodsSku::where('join_sku_goods_id', $params['goods_id'])->where('goods_sku_status', 'ON')->first();
- $sku->goods_sku_status = $params['goods_status'];
- $sku->goods_sku_specs_json = json_encode(['规格' => $params['goods_sales_price'] . '元']);
- $sku->goods_sku_market_price = $params['goods_sales_price'];
- $sku->goods_sku_sales_price = $params['goods_sales_price'];
- $sku->save();
- Db::commit();
- return json_success("success");
- } catch (\Exception $e) {
- Db::rollBack();
- dump($e->getMessage());
- return json_fail('编辑充值产品失败');
- }
- }
- public static function insertPackage($params): Response
- {
- Db::beginTransaction();
- try {
- $params['goods_id'] = "GD" . date('ymdHi') . random_string(4, 'up');
- // 主表
- self::mainInsert($params);
- // 商品详情表
- self::detailInsert($params);
- // 套包组件表
- self::componentUpdate($params, 'insert');
- // 商品标签表
- self::labelInsert($params);
- // 产品运行控制信息表
- self::goodsRunningInsert($params);
- // sku表
- // self::goodsSkuSet($params, 'insert');
- // 待上架状态,上架时间大于当前时间
- if ($params['goods_status'] == 'PENDING' && strtotime($params['goods_on_addtimes']) > time()) {
- $redis = Redis::connection();
- $key = date('YmdHi', strtotime($params['goods_on_addtimes']));
- $redis->sAdd(Goods::LISTING_KEY_PREFIX . $key, $params['goods_id']);
- }
- Db::commit();
- } catch (BusinessException $e) {
- Db::rollBack();
- dump($e->getMessage());
- return json_fail($e->getMessage());
- } catch (\Exception $e) {
- Db::rollBack();
- dump($e->getTrace());
- return json_fail('数据写入失败~');
- }
- _syslog("添加套餐", "商品名【" . $params['goods_name'] . "】");
- return json_success('success');
- }
- public static function update($params)
- {
- Db::beginTransaction();
- try {
- // 主表
- self::mainUpdate($params);
- // 商品详情表
- self::detailUpdate($params);
- // 商品标签表
- self::labelUpdate($params);
- // 产品运行控制信息表
- self::goodsRunningUpdate($params);
- // sku表
- self::goodsSkuSet($params, 'update');
- Db::commit();
- } catch (BusinessException $e) {
- Db::rollBack();
- return json_fail($e->getMessage());
- } catch (\Exception $e) {
- Db::rollBack();
- return json_fail('数据更新失败~');
- }
- _syslog("编辑商品", "商品名【" . $params['goods_name'] . "】" ?? "商品ID:【" . $params['goods_id'] . "】");
- return json_success('success');
- }
- public static function changeStatus($params)
- {
- try {
- Goods::where('goods_id', $params['goods_id'])->update(['goods_status' => $params['goods_status']]);
- return json_success('修改成功');
- } catch (\Exception $e) {
- return json_fail('修改状态失败');
- }
- }
- public static function updatePackage($params)
- {
- Db::beginTransaction();
- try {
- // 主表
- self::mainUpdate($params);
- // 商品详情表
- self::detailUpdate($params);
- // 套包组件表
- self::componentUpdate($params, 'update');
- // 商品标签表
- self::labelUpdate($params);
- // 产品运行控制信息表
- self::goodsRunningUpdate($params);
- // sku表
- // self::goodsSkuSet($params, 'update');
- Db::commit();
- } catch (BusinessException $e) {
- Db::rollBack();
- return json_fail($e->getMessage());
- } catch (\Exception $e) {
- Db::rollBack();
- dump($e->getTrace());
- return json_fail('数据更新失败~');
- }
- _syslog("编辑套餐", "商品名【" . $params['goods_name'] . "】" ?? "商品ID:【" . $params['goods_id'] . "】");
- return json_success('success');
- }
- /**
- * @Desc 删除商品
- * @Author Gorden
- * @Date 2024/3/28 13:20
- *
- * @param $ids
- * @return Response
- */
- public static function delete($ids)
- {
- if (!$ids) {
- return json_fail("数据错误~");
- }
- if (!is_array($ids)) {
- $ids = [$ids];
- }
- $goods = Goods::whereIn('goods_id', $ids)->get()->toArray();
- if (!$goods) {
- return json_fail("数据错误~");
- }
- // 是否在套包里
- if (GoodsComponent::whereIn('join_component_goods_id', $ids)->exists()) {
- return json_fail("当前商品存在于套包中,请先在套包中删除");
- }
- // 是否已被购买过
- if (OrderSheet::whereIn('join_sheet_goods_id', $ids)->exists()) {
- return json_fail("当前商品已有购买历史,如不在APP显示,请选择【下架】操作");
- }
- // 是否预约过
- if (Appointment::whereIn('join_appointment_goods_id', $ids)->exists()) {
- return json_fail("当前商品已有预约历史,如不在APP显示,请选择【下架】操作");
- }
- Db::beginTransaction();
- try {
- Goods::whereIn('goods_id', $ids)->delete();
- GoodsDetail::whereIn('join_detail_goods_id', $ids)->delete();
- GoodsLabel::whereIn('join_label_goods_id', $ids)->delete();
- GoodsRunning::whereIn('join_running_goods_id', $ids)->delete();
- GoodsSku::whereIn('join_sku_goods_id', $ids)->delete();
- Db::commit();
- _syslog("删除商品 / 套餐", "ID:【" . implode(',', $ids) . "】", $goods);
- return json_success("商品删除成功");
- } catch (\Exception $e) {
- Db::rollBack();
- return json_fail("商品删除失败~");
- }
- }
- /**
- * @Desc 商品主表
- * @Author Gorden
- * @Date 2024/3/11 11:20
- *
- * @param $params
- * @return mixed|string
- * @throws BusinessException
- */
- public static function mainInsert($params)
- {
- if (!empty($params['goods_cover'])) {
- $params['goods_cover'] = str_replace(getenv('STORAGE_DOMAIN'), '', $params['goods_cover']);
- }
- // 如果产品是待处理状态
- if ($params['goods_status'] == 'PENDING') {
- if (strtotime($params['goods_on_addtimes']) <= time()) {
- $params['goods_status'] = 'ON';
- }
- }
- $category = SysCategory::where('category_id', $params['join_goods_category_id'])->first();
- if (!$category) {
- throw new BusinessException("产品分类不存在~");
- }
- if (empty($params['goods_category'])) {
- $params['goods_category'] = $category->category_classify;
- }
- try {
- $model = new Goods();
- $model->goods_id = $params['goods_id'];
- $model->join_goods_category_id = $params['join_goods_category_id'] ?? 0;
- $model->join_goods_supplier_id = $params['join_goods_supplier_id'] ?? 0;
- $model->goods_classify = $params['goods_classify'] ?? '';
- $model->goods_status = $params['goods_status'] ?? '';
- $model->goods_category = $params['goods_category'] ?? '';
- // $model->goods_prefix = $params['goods_prefix'] ?? 】($category->category_name ? '【' . $category->category_name . '' : '');
- $model->goods_prefix = $params['goods_prefix'] ?? '';
- $model->goods_name = $params['goods_name'];
- $model->goods_market_price = $params['goods_market_price'] ?? 0;
- $model->goods_sales_price = $params['goods_sales_price'] ?? 0;
- $model->goods_sku_json = !empty($params['goods_sku_json_label']) ? json_encode($params['goods_sku_json_label']) : json_encode(['规格' => ['标准']]);
- $model->goods_attribute_json = !empty($params['goods_attribute_json']) ? $params['goods_attribute_json'] : '[]';
- $model->goods_title = $params['goods_title'] ?? '';
- $model->goods_cover = $params['goods_cover'] ?? '';
- $model->goods_on_addtimes = isset($params['goods_on_addtimes']) ? strtotime($params['goods_on_addtimes']) : null;
- $model->goods_sort = $params['goods_sort'] ?? null;
- $model->goods_groupby = $params['goods_groupby'] ?? '';
- $model->goods_remark = $params['goods_remark'] ?? '';
- $model->goods_extend_json = $params['goods_extend_json'] ?? '{}';
- $model->is_support_appointment = $params['is_support_appointment'] ?? 'N';
- $model->creator_user_id = JwtToken::getCurrentId();
- $model->goods_addtimes = time();
- $model->goods_updatetimes = time();
- // {"express":"Y","self":"Y","arrival":"Y"}
- $expressJson = [];
- if (!empty($params['express_json'])) {
- if (in_array('express', $params['express_json'])) {
- $expressJson['express'] = 'Y';
- } else {
- $expressJson['express'] = 'N';
- }
- if (in_array('self', $params['express_json'])) {
- $expressJson['self'] = 'Y';
- } else {
- $expressJson['self'] = 'N';
- }
- if (in_array('arrival', $params['express_json'])) {
- $expressJson['arrival'] = 'Y';
- } else {
- $expressJson['arrival'] = 'N';
- }
- $model->goods_express_json = json_encode($expressJson);
- }
- if (!empty($params['is_support_appointment']) && $params['is_support_appointment'] == 'Y' && !empty($params['appointment_times']) && $params['goods_category'] != 'TRAVEL') {
- $times = [];
- $attributeJsonTimeArr = [];
- $personTotal = 0;
- foreach ($params['appointment_times'] as $time) {
- $attributeJsonTimeArr[] = strtotime(date('Y-m-d ') . $time['appointmentTimeStart']);
- $attributeJsonTimeArr[] = strtotime(date('Y-m-d ') . $time['appointmentTimeEnd']);
- $personTotal += $time['person'];
- $times[$time['appointmentTimeStart']] = [
- 'person' => $time['person'],
- 'duration' => $time['appointmentTimeStart'] . '-' . $time['appointmentTimeEnd']
- ];
- }
- $attributeJsonTime = date('H:i', min($attributeJsonTimeArr)) . '至' . date('H:i', max($attributeJsonTimeArr));
- $newDates = [];
- foreach ($params['dates'] as $date) {
- $key = self::$week[$date];
- $newDates[$key] = $date;
- }
- ksort($newDates);
- $currentDate = current($newDates);
- $lastDate = end($newDates);
- $attributeJsonDate = '';
- if (self::$week[$lastDate] - self::$week[$currentDate] + 1 > count($newDates)) {
- $attributeJsonDate = implode(',', $newDates);
- } else if (self::$week[$lastDate] - self::$week[$currentDate] + 1 == count($newDates)) {
- $attributeJsonDate = $currentDate . '至' . $lastDate;
- }
- // if (isset($params['work_time'])){
- // $workTimeStart = date('H:i',strtotime($params['work_time'][0]));
- // $workTimeEnd = date('H:i',strtotime($params['work_time'][1]));
- // $attributeJsonTime = $workTimeStart.'至'.$workTimeEnd;
- // }
- $attributeJson = [
- 'icon' => $model->goods_cover,
- 'date' => $attributeJsonDate,
- // 'time' => $attributeJsonTime,
- 'dates' => $newDates ? array_values($newDates) : [],
- 'times' => $times,
- 'person' => $personTotal
- ];
- // if(isset($params['address'])){
- // $attributeJson['address'] = $params['address'];
- // }
- // if (isset($params['min_count'])){
- // $attributeJson['min-count'] = $params['min_count'];
- // }else{
- // $attributeJson['min-count'] = 1;
- // }
- // if (isset($params['teachers'])){
- // $attributeJson['teachers'] = $params['teachers'];
- // }
- if (!empty($params['appointment_label'])) {
- $attributeJson['label'] = $params['appointment_label'];
- }
- // if (!empty($params['address'])) {
- // $attributeJson['address'] = $params['address'];
- // }
- // if (!empty($params['position'])){
- // $attributeJson['position'] = $params['position'];
- // }
- // if (isset($params['goods_service_premises'])){
- // $attributeJson['service_premises_id'] = $params['goods_service_premises'];
- // }
- $model->goods_attribute_json = json_encode($attributeJson, JSON_UNESCAPED_UNICODE);
- // $times = [];
- // foreach ($params['appointment_times'] as $time) {
- // $times[$time['appointmentTimeStart']] = [
- // 'person' => $time['person'],
- // 'duration' => $time['appointmentTimeStart'] . '-' . $time['appointmentTimeEnd']
- // ];
- // }
- // $model->goods_attribute_json = json_encode([
- // 'icon' => '',
- // 'dates' => $params['dates'] ?? [],
- // 'times' => $times
- // ]);
- }
- if (!empty($params['is_support_appointment']) && $params['is_support_appointment'] == 'Y' && !empty($params['appointment_times']) && $params['goods_category'] == 'TRAVEL') {
- if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
- $attributeJson = json_decode($model->goods_attribute_json, true);
- } elseif (empty($model->goods_attribute_json)) {
- $attributeJson = [];
- }
- $attributeJson['travel-day'] = $params['travel_day'];
- $attributeJson['travel-begin'] = $params['travel_begin'];
- $attributeJson['travel-night'] = $params['travel_night'];
- $attributeJson['travel-trans'] = $params['travel_trans'];
- $unixs = [];
- foreach ($params['appointment_times'] as $times) {
- $unix = strtotime($times['days']);
- $unixs[$unix] = $times['person'];
- }
- ksort($unixs);
- $months = [];
- foreach ($unixs as $key => $unix) {
- $month = date('Ym', $key);
- $day = date('d', $key);
- $months[$month][$day] = $unix;
- }
- $attributeJson['month'] = $months;
- $model->goods_attribute_json = json_encode($attributeJson, JSON_UNESCAPED_UNICODE);
- }
- if (!empty($params['goods_json']) && $params['join_goods_category_id'] == 65) {
- $goodsJson = json_decode($params['goods_json'], true);
- // foreach ($goodsJson as $key => $item) {
- // $goodsJson[$key]['color'] = hexToRgb($item['color']);
- // }
- //
- $model->goods_json = json_encode($goodsJson);
- } elseif (!empty($params['goods_json']) && $params['join_goods_category_id'] == 43) {
- $goodsJson = json_decode($params['goods_json'], true);
- $newGoodsJson = [];
- foreach ($goodsJson as $item1) {
- if (empty($item1['title'])) {
- continue;
- }
- $newItem1 = [];
- foreach ($item1['items'] as $item2) {
- $newParams = [];
- foreach ($item2['params'] as $param) {
- if (!empty($param[0]) || !empty($param[1])) {
- $newParams[] = $param;
- }
- }
- $newItem1['items'][$item2['key']] = $newParams;
- }
- $newItem1['service'] = $item1['service'] ?? '';
- $newGoodsJson[$item1['title']] = $newItem1;
- }
- $model->goods_json = json_encode($newGoodsJson);
- } else {
- $model->goods_json = '[]';
- }
- if (!empty($params['goods_premisses'])) {
- $attributeJson = [];
- if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
- $attributeJson = json_decode($model->goods_attribute_json, true);
- } elseif (empty($model->goods_attribute_json)) {
- $attributeJson = [];
- }
- $attributeJson['premisses'] = $params['goods_premisses'];
- $model->goods_attribute_json = json_encode($attributeJson);
- }
- if (!empty($params['goods_theme_color']) && !empty($params['goods_theme_icon'])) {
- if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
- $attributeJson = json_decode($model->goods_attribute_json, true);
- } elseif (empty($model->goods_attribute_json)) {
- $attributeJson = [];
- }
- $attributeJson['bg'] = $params['goods_theme_color'];
- $attributeJson['icon'] = str_replace(getenv('STORAGE_DOMAIN'), '', $params['goods_theme_icon']);
- $model->goods_attribute_json = json_encode($attributeJson);
- }
- if (!empty($params['address'])) {
- if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
- $attributeJson = json_decode($model->goods_attribute_json, true);
- } elseif (empty($model->goods_attribute_json)) {
- $attributeJson = [];
- }
- $attributeJson['address'] = $params['address'];
- $model->goods_attribute_json = json_encode($attributeJson);
- }
- if (!empty($params['position'])) {
- if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
- $attributeJson = json_decode($model->goods_attribute_json, true);
- } elseif (empty($model->goods_attribute_json)) {
- $attributeJson = [];
- }
- $attributeJson['position'] = $params['position'];
- $model->goods_attribute_json = json_encode($attributeJson);
- }
- if (isset($params['goods_service_premises'])) {
- if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
- $attributeJson = json_decode($model->goods_attribute_json, true);
- } elseif (empty($model->goods_attribute_json)) {
- $attributeJson = [];
- }
- $attributeJson['service_premises_id'] = $params['goods_service_premises'];
- $model->goods_attribute_json = json_encode($attributeJson);
- }
- if (isset($params['min_count'])) {
- $attributeJson = [];
- if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
- $attributeJson = json_decode($model->goods_attribute_json, true);
- }
- $attributeJson['min-count'] = $params['min_count'];
- $model->goods_attribute_json = json_encode($attributeJson);
- }
- if (isset($params['max_limit'])) {
- $attributeJson = [];
- if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
- $attributeJson = json_decode($model->goods_attribute_json, true);
- }
- $attributeJson['max-limit'] = $params['max_limit'];
- if ($attributeJson['max-limit'] == 0 || $attributeJson['max-limit'] == '') {
- unset($attributeJson['max-limit']);
- }
- }
- if (isset($params['teachers'])) {
- if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
- $attributeJson = json_decode($model->goods_attribute_json, true);
- } elseif (empty($model->goods_attribute_json)) {
- $attributeJson = [];
- }
- $attributeJson['teachers'] = $params['teachers'];
- $model->goods_attribute_json = json_encode($attributeJson);
- }
- if (isset($params['work_time'])) {
- if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
- $attributeJson = json_decode($model->goods_attribute_json, true);
- } elseif (empty($model->goods_attribute_json)) {
- $attributeJson = [];
- }
- $workTimeStart = date('H:i', strtotime($params['work_time'][0]));
- $workTimeEnd = date('H:i', strtotime($params['work_time'][1]));
- $attributeJsonTime = $workTimeStart . '至' . $workTimeEnd;
- $attributeJson['time'] = $attributeJsonTime;
- $model->goods_attribute_json = json_encode($attributeJson);
- }
- if (!empty($params['couponContentList'])) {
- $couponList = [];
- foreach ($params['couponContentList'] as $coupon) {
- $couponList[$coupon['coupon_id']] = [
- 'num' => $coupon['nbr'],
- 'name' => $coupon['coupon_name']
- ];
- }
- $attributeJson['coupon'] = $couponList;
- $model->goods_attribute_json = json_encode($attributeJson, JSON_UNESCAPED_UNICODE);
- }
- if (!empty($params['coupon_id']) && !empty($params['coupon_nbr'])) {
- if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
- $attributeJson = json_decode($model->goods_attribute_json, true);
- } elseif (empty($model->goods_attribute_json)) {
- $attributeJson = [];
- }
- $coupons = Coupon::whereIn('coupon_id', $params['coupon_id'])
- ->select('coupon_id', 'coupon_name')
- ->get()
- ->toArray();
- $couponList = [];
- foreach ($coupons as $coupon) {
- if (isset($params['coupon_nbr'][$coupon['coupon_id']])) {
- $couponList[$coupon['coupon_id']] = [
- 'num' => $params['coupon_nbr'][$coupon['coupon_id']],
- 'name' => $coupon['coupon_name']
- ];
- }
- }
- $attributeJson['coupon'] = $couponList;
- $model->goods_attribute_json = json_encode($attributeJson, JSON_UNESCAPED_UNICODE);
- }
- if ($model->save()) {
- return $model->goods_id;
- }
- // 异常
- throw new BusinessException("数据写入失败~");
- } catch (\Exception $e) {
- dump($e->getMessage());
- throw new BusinessException("数据写入失败~");
- }
- }
- /**
- * @Desc 详情表
- * @Author Gorden
- * @Date 2024/3/11 11:19
- *
- * @param $params
- * @return void
- * @throws BusinessException
- */
- public static function detailInsert($params)
- {
- if (!empty($params['goods_detail_slider_json'])) {
- $params['goods_detail_slider_json'] = str_replace(getenv('STORAGE_DOMAIN'), '', $params['goods_detail_slider_json']);
- $params['goods_detail_slider_json'] = json_encode(['slider' => $params['goods_detail_slider_json']]);
- }
- if (isset($params['curriculum'])) {
- $params['goods_detail_specs_json'] = json_encode([
- [
- 'key' => '课时',
- 'val' => $params['curriculum']['period'] ?? '',
- ],
- [
- 'key' => '群体',
- 'val' => $params['curriculum']['group'] ?? '',
- ],
- [
- 'key' => '课程',
- 'val' => $params['curriculum']['course'] ?? '',
- ],
- ]);
- }
- try {
- $model = new GoodsDetail();
- $model->join_detail_goods_id = $params['goods_id'];
- $model->goods_detail_code_json = $params['goods_detail_code_json'] ?? '{}';
- $model->goods_detail_slider_json = $params['goods_detail_slider_json'] ?? '{}';
- $model->goods_detail_specs_json = $params['goods_detail_specs_json'] ?? '{}';
- $model->goods_detail_content = $params['goods_detail_content'] ?? '';
- if (!$model->save()) {
- // 异常
- throw new BusinessException("轮播图/详情数据写入失败~");
- }
- } catch (\Exception $e) {
- dump($e->getMessage());
- throw new BusinessException("轮播图/详情数据写入失败~");
- }
- }
- public static function componentUpdate($params, $type = 'insert')
- {
- Db::beginTransaction();
- try {
- $goodsSku = '';
- // 有先删除
- if ($type == 'update') {
- GoodsComponent::where('join_component_master_goods_id', $params['goods_id'])->delete();
- $goodsSku = GoodsSku::where('join_sku_goods_id', $params['goods_id'])->first();
- if ($goodsSku) {
- $goodsSku->goods_sku_market_price = $params['goods_market_price'] ?? 0;
- $goodsSku->goods_sku_sales_price = $params['goods_sales_price'] ?? 0;
- $goodsSku->save();
- }
- }
- if ($type == 'insert' || !$goodsSku) {
- Goods::where('goods_id', $params['goods_id'])->update(['goods_sku_json' => '{"规格": ["标准"]}']);
- $skuData = [
- 'join_sku_goods_id' => $params['goods_id'],
- 'goods_sku_status' => 'ON',
- 'goods_sku_specs_json' => '{"规格": "标准"}',
- 'goods_sku_title' => "标准" . $params['goods_name'],
- 'goods_sku_market_price' => $params['goods_market_price'] ?? 0,
- 'goods_sku_sales_price' => $params['goods_sales_price'] ?? 0,
- ];
- GoodsSku::insert($skuData);
- }
- $data = [];
- foreach ($params['goods_content_list'] as $item) {
- if (!in_array($item['goods_id'], $params['join_component_goods_id'])) {
- continue;
- }
- $goods = Goods::where('goods_id', $params['goods_id'])->first();
- if (!$goods) {
- continue;
- }
- $data[] = [
- 'join_component_master_goods_id' => $params['goods_id'],
- 'join_component_goods_id' => $item['goods_id'] ?? '',
- 'join_component_goods_sku_id' => $item['sku_id'] ?? '',
- 'goods_component_price' => $item['goods_sales_price'] ?? '',
- 'goods_component_cover' => $goods->goods_cover,
- 'goods_component_json' => json_encode(['goods_name' => $item['goods_name'], 'nbr' => $item['nbr'], 'sku_id' => $item['sku_id'] ?? '']),
- 'goods_component_addtimes' => time()
- ];
- }
- if ($data) {
- GoodsComponent::insert($data);
- }
- Db::commit();
- } catch (\Exception $e) {
- Db::rollBack();
- dump($e->getMessage());
- throw new BusinessException("数据写入失败~");
- }
- }
- /**
- * @Desc 标签表
- * @Author Gorden
- * @Date 2024/3/11 11:32
- *
- * @param $params
- * @return void
- * @throws BusinessException
- */
- public static function labelInsert($params)
- {
- $model = new GoodsLabel();
- $model->join_label_goods_id = $params['goods_id'];
- $model->goods_label = $params['goods_label'] ? implode(',', $params['goods_label']) : '';
- $model->goods_label_extend_json = !empty($params['goods_label_extend_json']) ? $params['goods_label_extend_json'] : '{}';
- if (!$model->save()) {
- // 异常
- throw new BusinessException('数据写入失败~');
- }
- }
- /**
- * @Desc 产品运行控制信息表
- * @Author Gorden
- * @Date 2024/3/11 11:38
- *
- * @param $params
- * @return void
- * @throws BusinessException
- */
- public static function goodsRunningInsert($params)
- {
- try {
- $model = new GoodsRunning();
- $model->join_running_goods_id = $params['goods_id'];
- $model->goods_running_storage = $params['goods_running_storage'] ?? 0;
- $model->goods_running_sale = $params['goods_running_sale'] ?? 0;
- $model->goods_running_off_type = !empty($params['goods_running_off_type']) ? $params['goods_running_off_type'] : '';
- $model->goods_running_off_json = !empty($params['goods_running_off_type']) && $params['goods_running_off_type'] == 'T' && !empty($params['goods_off_addtimes']) ? json_encode(['time' => strtotime($params['goods_off_addtimes'])]) : '[]';
- if (!$model->save()) {
- throw new BusinessException('数据写入失败');
- }
- } catch (\Exception $e) {
- dump($e->getMessage());
- throw new BusinessException('数据写入失败');
- }
- }
- /**
- * @Desc SKU
- * @Author Gorden
- * @Date 2024/3/11 12:01
- *
- * @param $params
- * @return void
- * @throws BusinessException
- */
- public static function skuInsert($params)
- {
- $model = new GoodsSku();
- $model->join_sku_goods_id = $params['goods_id'];
- $model->goods_sku_status = $params['goods_sku_status'];
- $model->goods_sku_specs_json = $params['goods_sku_specs_json'];
- $model->goods_sku_title = $params['goods_sku_title'];
- $model->goods_sku_images_json = $params['goods_sku_images_json'];
- $model->goods_sku_content = $params['goods_sku_content'];
- $model->goods_sku_market_price = $params['goods_sku_market_price'];
- $model->goods_sku_sales_price = $params['goods_sku_sales_price'];
- $model->goods_sku_storage_json = $params['goods_sku_storage_json'];
- $model->goods_sku_service_json = $params['goods_sku_service_json'];
- $model->goods_sku_extend_json = $params['goods_sku_extend_json'];
- if (!$model->save()) {
- throw new BusinessException('规格数据写入失败~');
- }
- }
- /**
- * @Desc
- * @Author Gorden
- * @Date 2024/3/12 8:44
- *
- * @param $params
- * @return void
- * @throws BusinessException
- */
- public static function mainUpdate($params)
- {
- try {
- $data = self::inputFilter($params, new Goods());
- if (!empty($data['goods_cover'])) {
- $data['goods_cover'] = str_replace(getenv('STORAGE_DOMAIN'), '', $data['goods_cover']);
- }
- $data['goods_on_addtimes'] = isset($data['goods_on_addtimes']) ? strtotime($data['goods_on_addtimes']) : 0;
- $data['goods_sku_json'] = !empty($params['goods_sku_json_label']) ? json_encode($params['goods_sku_json_label']) : json_encode(['规格' => ['标准']]);
- $row = Goods::find($data['goods_id']);
- if ($row->join_goods_category_id != $data['join_goods_category_id']) {
- $category = SysCategory::where('category_id', $params['join_goods_category_id'])->first();
- if (!$category) {
- throw new BusinessException("产品分类不存在~");
- }
- $data['goods_category'] = $category->category_classify ?? '';
- $data['goods_prefix'] = $data['goods_prefix'] ?? ($category->category_name ? '【' . $category->category_name . '】' : '');
- }
- if (!empty($params['goods_recommend_index'])) {
- $data['goods_category'] = $params['goods_recommend_index'];
- }
- // 上架时间有变动
- if ($data['goods_status'] == 'PENDING' && $row->goods_on_addtimes != $data['goods_on_addtimes']) {
- $redis = Redis::connection();
- // 删掉原来的
- $oldKey = Goods::LISTING_KEY_PREFIX . date('YmdHi', $row->goods_on_addtimes);
- $redis->srem($oldKey, $data['goods_id']);
- // 加入新的
- $newKey = Goods::LISTING_KEY_PREFIX . date('YmdHi', $data['goods_on_addtimes']);
- $redis->sadd($newKey, $data['goods_id']);
- }
- $expressJson = [];
- if (!empty($params['express_json'])) {
- if (in_array('express', $params['express_json'])) {
- $expressJson['express'] = 'Y';
- } else {
- $expressJson['express'] = 'N';
- }
- if (in_array('self', $params['express_json'])) {
- $expressJson['self'] = 'Y';
- } else {
- $expressJson['self'] = 'N';
- }
- if (in_array('arrival', $params['express_json'])) {
- $expressJson['arrival'] = 'Y';
- } else {
- $expressJson['arrival'] = 'N';
- }
- $data['goods_express_json'] = json_encode($expressJson);
- }
- if (!empty($params['is_support_appointment']) && $params['is_support_appointment'] == 'Y' && !empty($params['appointment_times']) && $params['goods_category'] != 'TRAVEL') {
- $attributeJson = [];
- if (!empty($row->goods_attribute_json)) {
- $attributeJson = json_decode($row->goods_attribute_json, true);
- }
- $times = [];
- $attributeJsonTimeArr = [];
- $personTotal = 0;
- foreach ($params['appointment_times'] as $time) {
- $attributeJsonTimeArr[] = strtotime(date('Y-m-d ') . $time['appointmentTimeStart']);
- $attributeJsonTimeArr[] = strtotime(date('Y-m-d ') . $time['appointmentTimeEnd']);
- $personTotal += $time['person'];
- $times[$time['appointmentTimeStart']] = [
- 'person' => $time['person'],
- 'duration' => $time['appointmentTimeStart'] . '-' . $time['appointmentTimeEnd']
- ];
- }
- $attributeJsonTime = date('H:i', min($attributeJsonTimeArr)) . '至' . date('H:i', max($attributeJsonTimeArr));
- $newDates = [];
- foreach ($params['dates'] as $date) {
- $key = self::$week[$date];
- $newDates[$key] = $date;
- }
- ksort($newDates);
- $currentDate = current($newDates);
- $lastDate = end($newDates);
- $attributeJsonDate = '';
- if (self::$week[$lastDate] - self::$week[$currentDate] + 1 > count($newDates)) {
- $attributeJsonDate = implode(',', $newDates);
- } else if (self::$week[$lastDate] - self::$week[$currentDate] + 1 == count($newDates)) {
- $attributeJsonDate = $currentDate . '至' . $lastDate;
- }
- $attributeJson['date'] = $attributeJsonDate;
- $attributeJson['icon'] = $data['goods_cover'] ?? '';
- $attributeJson['time'] = $attributeJsonTime;
- $attributeJson['dates'] = $newDates ? array_values($newDates) : [];
- $attributeJson['times'] = $times;
- $attributeJson['person'] = $personTotal;
- if (!empty($params['appointment_label'])) {
- $attributeJson['label'] = $params['appointment_label'];
- }
- // if (!empty($params['address'])) {
- // $attributeJson['address'] = $params['address'];
- // }
- // if (!empty($params['position'])){
- // $attributeJson['position'] = $params['position'];
- // }
- // if (isset($params['goods_service_premises'])){
- // $attributeJson['service_premises_id'] = $params['goods_service_premises'];
- // }
- // if (isset($params['work_time'])){
- // $workTimeStart = date('H:i',strtotime($params['work_time'][0]));
- // $workTimeEnd = date('H:i',strtotime($params['work_time'][1]));
- // $attributeJson['time'] = $workTimeStart.'至'.$workTimeEnd;
- // }
- // if (isset($params['min_count'])){
- // $attributeJson['min-count'] = $params['min_count'];
- // }else{
- // $attributeJson['min-count'] = 1;
- // }
- // if (isset($params['teachers'])){
- // $attributeJson['teachers'] = $params['teachers'];
- // }
- $data['goods_attribute_json'] = json_encode($attributeJson, JSON_UNESCAPED_UNICODE);
- }
- if (!empty($params['is_support_appointment']) && $params['is_support_appointment'] == 'Y' && !empty($params['appointment_times']) && $params['goods_category'] == 'TRAVEL') {
- $attributeJson = [];
- if (!empty($row->goods_attribute_json)) {
- $attributeJson = json_decode($row->goods_attribute_json, true);
- }
- $attributeJson['travel-day'] = $params['travel_day'];
- $attributeJson['travel-begin'] = $params['travel_begin'];
- $attributeJson['travel-night'] = $params['travel_night'];
- $attributeJson['travel-trans'] = $params['travel_trans'];
- $unixs = [];
- foreach ($params['appointment_times'] as $times) {
- $unix = strtotime($times['days']);
- $unixs[$unix] = $times['person'];
- }
- ksort($unixs);
- $months = [];
- foreach ($unixs as $key => $unix) {
- $month = date('Ym', $key);
- $day = date('d', $key);
- $months[$month][$day] = $unix;
- }
- $attributeJson['month'] = $months;
- $data['goods_attribute_json'] = json_encode($attributeJson, JSON_UNESCAPED_UNICODE);
- }
- if (!empty($params['couponContentList'])) {
- $attributeJson = [];
- if (!empty($row->goods_attribute_json)) {
- $attributeJson = json_decode($row->goods_attribute_json, true);
- }
- $couponList = [];
- foreach ($params['couponContentList'] as $coupon) {
- $couponList[$coupon['coupon_id']] = [
- 'num' => $coupon['nbr'],
- 'name' => $coupon['coupon_name']
- ];
- }
- $attributeJson['coupon'] = $couponList;
- $data['goods_attribute_json'] = json_encode($attributeJson, JSON_UNESCAPED_UNICODE);
- }
- if (!empty($params['coupon_id']) && !empty($params['coupon_nbr'])) {
- $attributeJson = [];
- if (!empty($row->goods_attribute_json)) {
- $attributeJson = json_decode($row->goods_attribute_json, true);
- }
- $coupons = Coupon::whereIn('coupon_id', $params['coupon_id'])
- ->select('coupon_id', 'coupon_name')
- ->get()
- ->toArray();
- $couponList = [];
- foreach ($coupons as $coupon) {
- if (isset($params['coupon_nbr'][$coupon['coupon_id']])) {
- $couponList[$coupon['coupon_id']] = [
- 'num' => $params['coupon_nbr'][$coupon['coupon_id']],
- 'name' => $coupon['coupon_name']
- ];
- }
- }
- $attributeJson['coupon'] = $couponList;
- $data['goods_attribute_json'] = json_encode($attributeJson, JSON_UNESCAPED_UNICODE);
- }
- if (!empty($params['goods_premisses'])) {
- if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
- if (is_json($data['goods_attribute_json'])) {
- $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
- } else {
- $data['goods_attribute_json'] = [];
- }
- } elseif (empty($data['goods_attribute_json'])) {
- $data['goods_attribute_json'] = [];
- }
- $data['goods_attribute_json']['premisses'] = $params['goods_premisses'];
- $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
- }
- if (!empty($data['goods_json']) && $data['join_goods_category_id'] == 65) {
- // $goodsJson = json_decode($data['goods_json'], true);
- // foreach ($goodsJson as $key => $item) {
- // $goodsJson[$key]['color'] = hexToRgb($item['color']);
- // }
- //
- // $data['goods_json'] = json_encode($goodsJson);
- } elseif (!empty($data['goods_json']) && $data['join_goods_category_id'] == 43) {
- $goodsJson = json_decode($data['goods_json'], true);
- $newGoodsJson = [];
- foreach ($goodsJson as $item1) {
- if (empty($item1['title'])) {
- continue;
- }
- $newItem1 = [];
- foreach ($item1['items'] as $item2) {
- $newParams = [];
- foreach ($item2['params'] as $param) {
- if (!empty($param[0]) || !empty($param[1])) {
- $newParams[] = $param;
- }
- }
- $newItem1['items'][$item2['key']] = $newParams;
- }
- $newItem1['service'] = $item1['service'];
- $newGoodsJson[$item1['title']] = $newItem1;
- }
- $data['goods_json'] = json_encode($newGoodsJson);
- } else {
- $data['goods_json'] = '[]';
- }
- if (!empty($params['goods_theme_color']) && !empty($params['goods_theme_icon'])) {
- if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
- if (is_json($data['goods_attribute_json'])) {
- $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
- } else {
- $data['goods_attribute_json'] = [];
- }
- } elseif (empty($data['goods_attribute_json'])) {
- $data['goods_attribute_json'] = [];
- }
- $data['goods_attribute_json']['bg'] = $params['goods_theme_color'];
- $data['goods_attribute_json']['icon'] = str_replace(getenv('STORAGE_DOMAIN'), '', $params['goods_theme_icon']);
- $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
- }
- if (!empty($params['address'])) {
- if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
- if (is_json($data['goods_attribute_json'])) {
- $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
- } else {
- $data['goods_attribute_json'] = [];
- }
- } elseif (empty($data['goods_attribute_json'])) {
- $data['goods_attribute_json'] = [];
- }
- $data['goods_attribute_json']['address'] = $params['address'];
- $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
- }
- if (!empty($params['position'])) {
- if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
- if (is_json($data['goods_attribute_json'])) {
- $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
- } else {
- $data['goods_attribute_json'] = [];
- }
- } elseif (empty($data['goods_attribute_json'])) {
- $data['goods_attribute_json'] = [];
- }
- $data['goods_attribute_json']['position'] = $params['position'];
- $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
- }
- if (isset($params['goods_service_premises'])) {
- if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
- if (is_json($data['goods_attribute_json'])) {
- $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
- } else {
- $data['goods_attribute_json'] = [];
- }
- } elseif (empty($data['goods_attribute_json'])) {
- $data['goods_attribute_json'] = [];
- }
- $data['goods_attribute_json']['service_premises_id'] = $params['goods_service_premises'];
- $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
- }
- if (isset($params['work_time'])) {
- if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
- if (is_json($data['goods_attribute_json'])) {
- $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
- } else {
- $data['goods_attribute_json'] = [];
- }
- } elseif (empty($data['goods_attribute_json'])) {
- $data['goods_attribute_json'] = [];
- }
- $workTimeStart = date('H:i', strtotime($params['work_time'][0]));
- $workTimeEnd = date('H:i', strtotime($params['work_time'][1]));
- $data['goods_attribute_json']['time'] = $workTimeStart . '至' . $workTimeEnd;
- $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
- }
- if (isset($params['min_count'])) {
- if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
- if (is_json($data['goods_attribute_json'])) {
- $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
- } else {
- $data['goods_attribute_json'] = [];
- }
- } elseif (empty($data['goods_attribute_json'])) {
- $data['goods_attribute_json'] = [];
- }
- $data['goods_attribute_json']['min-count'] = $params['min_count'];
- $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
- }
- if (isset($params['max_limit'])) {
- if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
- $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
- } elseif (empty($data['goods_attribute_json'])) {
- $data['goods_attribute_json'] = [];
- }
- $data['goods_attribute_json']['max-limit'] = $params['max_limit'];
- if ($data['goods_attribute_json']['max-limit'] == 0 || $data['goods_attribute_json']['max-limit'] == '') {
- unset($data['goods_attribute_json']['max-limit']);
- }
- $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
- }
- if (isset($params['teachers'])) {
- if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
- if (is_json($data['goods_attribute_json'])) {
- $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
- } else {
- $data['goods_attribute_json'] = [];
- }
- } elseif (empty($data['goods_attribute_json'])) {
- $data['goods_attribute_json'] = [];
- }
- $data['goods_attribute_json']['teachers'] = $params['teachers'];
- $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
- }
- foreach ($data as $key => $val) {
- $row->{$key} = $val;
- }
- $row->goods_updatetimes = time();
- $row->updator_user_id = JwtToken::getCurrentId();
- $row->save();
- } catch (BusinessException $e) {
- throw new BusinessException($e->getMessage());
- } catch (\Exception $e) {
- dump($e->getTrace());
- throw new BusinessException('数据更新异常~1');
- }
- }
- /**
- * @Desc
- * @Author Gorden
- * @Date 2024/3/12 9:57
- *
- * @param $params
- * @return void
- * @throws BusinessException
- */
- public static function detailUpdate($params)
- {
- try {
- $data = self::inputFilter($params, new GoodsDetail());
- if (!empty($data['goods_detail_slider_json'])) {
- $data['goods_detail_slider_json'] = str_replace(getenv('STORAGE_DOMAIN'), '', $data['goods_detail_slider_json']);
- $data['goods_detail_slider_json'] = json_encode(['slider' => $data['goods_detail_slider_json']]);
- }
- if (isset($params['curriculum'])) {
- $data['goods_detail_specs_json'] = json_encode([
- [
- 'key' => '课时',
- 'val' => $params['curriculum']['period'] ?? '',
- ],
- [
- 'key' => '群体',
- 'val' => $params['curriculum']['group'] ?? '',
- ],
- [
- 'key' => '课程',
- 'val' => $params['curriculum']['course'] ?? '',
- ],
- ]);
- }
- // 根据goods_id 查详情ID
- $detail = GoodsDetail::where('join_detail_goods_id', $params['goods_id'])->first();
- if ($detail) {
- self::doUpdate($detail->join_detail_goods_id, $data, new GoodsDetail());
- } else {
- $data['join_detail_goods_id'] = $params['goods_id'];
- GoodsDetail::insert($data);
- }
- } catch (BusinessException $e) {
- throw new BusinessException($e->getMessage());
- } catch (\Exception $e) {
- dump($e->getMessage());
- throw new BusinessException('轮播图/详情数据更新异常~');
- }
- }
- /**
- * @Desc
- * @Author Gorden
- * @Date 2024/3/12 9:58
- *
- * @param $params
- * @return void
- * @throws BusinessException
- */
- public static function labelUpdate($params)
- {
- try {
- $data = self::inputFilter($params, new GoodsLabel());
- // 根据goods_id 查详情ID
- $detail = GoodsLabel::where('join_label_goods_id', $params['goods_id'])->first();
- if ($detail) {
- self::doUpdate($detail->goods_label_id, $data, new GoodsLabel());
- } else {
- $data['join_label_goods_id'] = $params['goods_id'];
- GoodsLabel::insert($data);
- }
- } catch (BusinessException $e) {
- throw new BusinessException($e->getMessage());
- } catch (\Exception $e) {
- dump($e->getMessage());
- throw new BusinessException('数据更新异常~3');
- }
- }
- /**
- * @Desc
- * @Author Gorden
- * @Date 2024/3/12 9:59
- *
- * @param $params
- * @return void
- * @throws BusinessException
- */
- public static function goodsRunningUpdate($params)
- {
- try {
- $data = self::inputFilter($params, new GoodsRunning());
- // 根据goods_id 查详情ID
- $detail = GoodsRunning::where('join_running_goods_id', $params['goods_id'])->first();
- if (!empty($params['goods_running_off_type']) && $params['goods_running_off_type'] == 'T') {
- $redis = Redis::connection();
- if (!empty($detail->goods_running_off_json)) {
- $goodsRunningOffJson = json_decode($detail->goods_running_off_json, true);
- if (isset($goodsRunningOffJson['time'])) {
- $oldKey = Goods::LISTING_OFF_KEY_PREFIX . date('YmdHi', $goodsRunningOffJson['time']);
- $goodsRunningOffJson['time'] = strtotime($params['goods_off_addtimes']);
- $data['goods_running_off_json'] = json_encode($goodsRunningOffJson);
- // 有老的下架时间,删除老的
- $redis->srem($oldKey, $params['goods_id']);
- } else {
- $goodsRunningOffJson['time'] = strtotime($params['goods_off_addtimes']);
- $data['goods_running_off_json'] = json_encode($goodsRunningOffJson);
- }
- } else {
- $data['goods_running_off_json'] = json_encode(['time' => strtotime($params['goods_off_addtimes'])]);
- }
- // 加入自动下架
- $newKey = Goods::LISTING_OFF_KEY_PREFIX . date('YmdHi', strtotime($params['goods_off_addtimes']));
- $redis->sAdd($newKey, $params['goods_id']);
- } else if (!empty($params['goods_running_off_type']) && !empty($detail->goods_running_off_type) && $params['goods_running_off_type'] == 'H' && $detail->goods_running_off_type == 'T') {
- $goodsRunningOffJson = json_decode($detail->goods_running_off_json, true);
- if (isset($goodsRunningOffJson['time'])) {
- $oldKey = Goods::LISTING_OFF_KEY_PREFIX . date('YmdHi', $goodsRunningOffJson['time']);
- $redis = Redis::connection();
- $redis->srem($oldKey, $params['goods_id']);
- }
- }
- if ($detail) {
- self::doUpdate($detail->join_running_goods_id, $data, new GoodsRunning());
- } else {
- // 兼容老数据……
- $data['join_running_goods_id'] = $params['goods_id'];
- GoodsRunning::insert($data);
- }
- } catch (BusinessException $e) {
- throw new BusinessException($e->getMessage());
- } catch (\Exception $e) {
- dump($e->getMessage());
- throw new BusinessException('数据更新异常~');
- }
- }
- /**
- * @Desc sku 设置
- * @Author Gorden
- * @Date 2024/4/10 10:43
- *
- * @param $params
- * @return void
- * @throws BusinessException
- */
- public static function goodsSkuSet($params, $operation = 'insert')
- {
- try {
- Db::beginTransaction();
- $skusOldIds = [];
- if ($operation == 'update') {
- // 查出所有的
- $skusOldIds = GoodsSku::where('join_sku_goods_id', $params['goods_id'])->pluck('goods_sku_id', 'goods_sku_id');
- // 删掉原有的
- // GoodsSku::where('join_sku_goods_id', $params['goods_id'])->delete();
- }
- if (empty($skusOldIds) && empty($params['goods_sku_json_value'])) {
- $skuData = [
- 'join_sku_goods_id' => $params['goods_id'],
- 'goods_sku_status' => 'ON',
- 'goods_sku_specs_json' => '{"规格": "标准"}',
- 'goods_sku_title' => "标准" . $params['goods_name'],
- 'goods_sku_market_price' => $params['goods_market_price'] ?? 0,
- 'goods_sku_sales_price' => $params['goods_sales_price'] ?? 0,
- ];
- GoodsSku::insert($skuData);
- }
- // 入新的
- if (!empty($params['goods_sku_json_value'])) {
- foreach ($params['goods_sku_json_value'] as $item) {
- $skus = explode(',', $item['sku']);
- $skuArr = [];
- for ($i = 1; $i <= count($skus); $i++) {
- $skuName = "skuName" . $i;
- $key = $item[$skuName];
- $skuArr[$key] = $skus[$i - 1];
- }
- $specsJson = json_encode($skuArr);
- $skuTitle = str_replace('-', ',', $item['sku']) . $params['goods_name'];
- if ($operation == 'update' && !empty($item['sku_id'])) {
- $model = GoodsSku::where('goods_sku_id', $item['sku_id'])->where('goods_sku_status', 'ON')->first();
- if (!$model) {
- $model = new GoodsSku();
- } else {
- unset($skusOldIds[$model->goods_sku_id]);
- }
- } else {
- $model = GoodsSku::where('join_sku_goods_id', $params['goods_id'])->where('goods_sku_status', 'ON')->where('goods_sku_title', $skuTitle)->first();
- if (!$model) {
- $model = new GoodsSku();
- } else {
- unset($skusOldIds[$model->goods_sku_id]);
- }
- }
- $model->join_sku_goods_id = $params['goods_id'];
- $model->goods_sku_status = $params['goods_status'];
- $model->goods_sku_specs_json = $specsJson;
- $model->goods_sku_title = $skuTitle;
- $model->goods_sku_market_price = $params['goods_market_price'] ?? 0;
- $model->goods_sku_sales_price = $item['price'];
- $model->goods_sku_storage_json = json_encode(['storage' => $item['stock']]);
- $model->save();
- }
- }
- if ($operation == 'update' && !empty($skusOldIds)) {
- // GoodsSku::whereIn('goods_sku_id', $skusOldIds)->delete();
- GoodsSku::whereIn('goods_sku_id', $skusOldIds)->update(['goods_sku_status' => 'DISABLED']);
- }
- Db::commit();
- } catch (\Exception $e) {
- dump($e->getTrace());
- Db::rollBack();
- throw new BusinessException('规格数据更新异常~');
- }
- }
- /**
- * @Desc
- * @Author Gorden
- * @Date 2024/3/12 8:45
- *
- * @param array $data
- * @param $model
- * @return array
- * @throws BusinessException
- */
- private static function inputFilter(array $data, $model): array
- {
- $table = config('database.connections.mysql.prefix') . $model->getTable();
- $allow_column = $model->getConnection()->select("desc `$table`");
- if (!$allow_column) {
- throw new BusinessException('表不存在', 2);
- }
- $columns = array_column($allow_column, 'Type', 'Field');
- foreach ($data as $col => $item) {
- if (!isset($columns[$col])) {
- unset($data[$col]);
- continue;
- }
- // 非字符串类型传空则为null
- if ($item === '' && strpos(strtolower($columns[$col]), 'varchar') === false && strpos(strtolower($columns[$col]), 'text') === false) {
- $data[$col] = null;
- }
- if (is_array($item)) {
- $data[$col] = implode(',', $item);
- }
- if ($item != '' && (strpos(strtolower($columns[$col]), 'varchar') || strpos(strtolower($columns[$col]), 'text'))) {
- // $data[$col] = htmlspecialchars($item);
- }
- }
- if (empty($data['created_at'])) {
- unset($data['created_at']);
- }
- if (empty($data['updated_at'])) {
- unset($data['updated_at']);
- }
- return $data;
- }
- /**
- * @Desc 执行更新
- * @Author Gorden
- * @Date 2024/3/12 8:43
- *
- * @param $id
- * @param $data
- * @param $model
- * @return void
- */
- private static function doUpdate($id, $data, $model)
- {
- $row = $model->find($id);
- foreach ($data as $key => $val) {
- $row->{$key} = $val;
- }
- $row->save();
- }
- /**
- * @Desc 上架定时
- * @Author Gorden
- * @Date 2024/4/11 15:13
- *
- * @return void
- */
- public static function checkListing()
- {
- $key = Goods::LISTING_KEY_PREFIX . date('YmdHi');
- $redis = Redis::connection();
- if (!$redis->exists($key)) {
- return;
- }
- $goodsIds = $redis->sMembers($key);
- if (Goods::whereIn('goods_id', $goodsIds)->update(['goods_status' => 'ON'])) {
- $redis->del($key);
- }
- }
- /**
- * @Desc 自动下架
- * @Author Gorden
- * @Date 2024/4/26 15:26
- *
- * @return void
- */
- public static function checkOffListing()
- {
- $key = Goods::LISTING_OFF_KEY_PREFIX . date('YmdHi');
- $redis = Redis::connection();
- if (!$redis->exists($key)) {
- return;
- }
- $goodsIds = $redis->sMembers($key);
- if (Goods::whereIn('goods_id', $goodsIds)->update(['goods_status' => 'OFF'])) {
- $redis->del($key);
- }
- }
- public static $week = [
- '周一' => 1,
- '周二' => 2,
- '周三' => 3,
- '周四' => 4,
- '周五' => 5,
- '周六' => 6,
- '周日' => 7,
- ];
- }
|