GoodsService.php 112 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520
  1. <?php
  2. namespace app\admin\service\goods;
  3. use app\common\Tree;
  4. use app\model\Appointment;
  5. use app\model\Goods;
  6. use app\model\GoodsComponent;
  7. use app\model\GoodsDetail;
  8. use app\model\GoodsLabel;
  9. use app\model\GoodsRunning;
  10. use app\model\GoodsSku;
  11. use app\model\OrderSheet;
  12. use app\model\Supplier;
  13. use app\model\SysCategory;
  14. use app\model\SysDept;
  15. use app\model\SysSerial;
  16. use app\model\SysUser;
  17. use support\Db;
  18. use support\exception\BusinessException;
  19. use support\Redis;
  20. use support\Request;
  21. use support\Response;
  22. use Tinywan\Jwt\JwtToken;
  23. class GoodsService
  24. {
  25. public static function selectAll($goodsIds)
  26. {
  27. $goods = Goods::where('goods_status', 'ON')
  28. ->when($goodsIds != '', function ($query) use ($goodsIds) {
  29. $query->whereIn('goods_id', $goodsIds);
  30. })
  31. ->select('goods_id', 'goods_name')
  32. ->get();
  33. return json_success('', $goods);
  34. }
  35. public static function selectAllByGoodsName($goodsName)
  36. {
  37. $goods = Goods::with('sku')
  38. ->where('goods_status', 'ON')
  39. ->when($goodsName != '', function ($query) use ($goodsName) {
  40. $query->where('goods_name', 'like', "%" . $goodsName . '%');
  41. })
  42. ->select('goods_id', 'goods_name', 'goods_classify')
  43. ->get()
  44. ->toArray();
  45. foreach ($goods as &$item) {
  46. if (!empty($item['sku'])) {
  47. foreach ($item['sku'] as $key => $sku) {
  48. $specsJson = json_decode($sku['goods_sku_specs_json'], true);
  49. $skuTitle = '';
  50. foreach ($specsJson as $item2) {
  51. if (is_array($item2)) {
  52. $item2 = implode(',', $item2);
  53. }
  54. $skuTitle .= $item2 . '-';
  55. }
  56. $item['sku'][$key]['goods_sku_id'] = strval($item['sku'][$key]['goods_sku_id']);
  57. $item['sku'][$key]['goods_sku_title'] = rtrim($skuTitle, '-');
  58. }
  59. }
  60. }
  61. return json_success('', $goods);
  62. }
  63. public static function selectAllByCategoryForRuleAddComponent($category = "GOODS")
  64. {
  65. $categoryIds = [];
  66. $categorySuperIds = [];
  67. if ($category == 'GOODS') {
  68. $categorySuperIds = [5];
  69. } elseif ($category == 'SERVICE') {
  70. $categorySuperIds = [31, 154, 42, 65, 30, 66, 72, 70];
  71. }
  72. $categorys = SysCategory::whereIn('category_id', $categorySuperIds)->get()->toArray();
  73. foreach ($categorys as $item) {
  74. if (empty($item['category_super_path'])) {
  75. $item['category_super_path'] = '#' . $item['category_id'] . '#';
  76. } else {
  77. $item['category_super_path'] = $item['category_super_path'] . '#' . $item['category_id'] . '#';
  78. }
  79. $categoryIds = SysCategory::where('category_super_path', 'like', '%' . $item['category_super_path'] . '%')->pluck('category_id');
  80. $categoryIds = $categoryIds ? $categoryIds->toArray() : [];
  81. $categorySuperIds = array_merge($categorySuperIds, $categoryIds);
  82. }
  83. $categoryIds = array_unique($categorySuperIds);
  84. $goods = Goods::with('sku')
  85. // ->where('goods_classify', $category)
  86. ->when(!empty($categoryIds), function ($query) use ($categoryIds) {
  87. $query->whereIn('join_goods_category_id', $categoryIds);
  88. })->when(empty($categoryIds), function ($query) {
  89. $query->where('goods_classify', '<>', 'RECHARGE');
  90. })->where('goods_status', 'ON')
  91. ->select('goods_id', 'goods_name', 'join_goods_category_id')
  92. ->get()
  93. ->toArray();
  94. foreach ($goods as &$good) {
  95. if (!empty($good['sku'])) {
  96. foreach ($good['sku'] as $key => $sku) {
  97. if (!empty($sku['goods_sku_specs_json'])) {
  98. $specsJson = json_decode($sku['goods_sku_specs_json'], true);
  99. $skuTitle = '';
  100. foreach ($specsJson as $item) {
  101. if (is_array($item)) {
  102. $item = implode(',', $item);
  103. }
  104. $skuTitle .= $item . '-';
  105. }
  106. $good['sku'][$key]['goods_sku_id'] = strval($good['sku'][$key]['goods_sku_id']);
  107. $good['sku'][$key]['goods_sku_title'] = rtrim($skuTitle, '-');
  108. }
  109. unset($good['sku'][$key]['goods_sku_specs_json'], $good['sku'][$key]['join_sku_goods_id']);
  110. }
  111. } else {
  112. $good['sku'] = [];
  113. }
  114. }
  115. return json_success('', $goods);
  116. }
  117. public static function selectPremisesByGoodsId($goodsId)
  118. {
  119. $goods = Goods::where('goods_id', $goodsId)
  120. ->select('goods_id', 'goods_attribute_json')
  121. ->first();
  122. $premisses = [];
  123. if (!empty($goods->goods_attribute_json)) {
  124. $attributeJson = json_decode($goods->goods_attribute_json, true);
  125. if (isset($attributeJson['premisses'])) {
  126. $premisses = SysDept::whereIn('dept_id', $attributeJson['premisses'])
  127. ->select('dept_id', 'dept_name')
  128. ->get()
  129. ->toArray();
  130. }
  131. }
  132. return json_success('', $premisses);
  133. }
  134. public static function select(Request $request, $classify = "GOODS")
  135. {
  136. $page = $request->get('page', 1);
  137. $pageSize = $request->get('pageSize', 20);
  138. $goodsName = $request->get('goods_name', '');
  139. $categoryId = $request->get('join_goods_category_id', null);
  140. $goodsCategory = $request->get('goods_category', null);
  141. $type = $request->get('type','');
  142. if ($categoryId != null) {
  143. $categoryPath = SysCategory::where('category_id', $categoryId)->value('category_super_path');
  144. $categoryPath .= '#' . $categoryId . '#';
  145. $categoryIds = SysCategory::where('category_super_path', 'like', $categoryPath . '%')->pluck('category_id')->toArray();
  146. $categoryIds[] = intval($categoryId);
  147. if (!empty($categoryIds)) {
  148. $categoryId = $categoryIds;
  149. } else {
  150. $categoryId = [intval($categoryId)];
  151. }
  152. }
  153. $rows = Goods::with([
  154. 'category' => function ($query) {
  155. $query->select('category_id', 'category_name');
  156. },
  157. 'running' => function ($query) {
  158. $query->select('join_running_goods_id', 'goods_running_storage');
  159. },
  160. 'supplier' => function ($query) {
  161. $query->select('supplier_id', 'supplier_name');
  162. },
  163. 'user' => function ($query) {
  164. $query->select('user_id', 'user_name');
  165. }
  166. ])->leftJoin('goods_running','goods_running.join_running_goods_id','=','goods.goods_id')
  167. ->select('goods_id', 'join_goods_category_id', 'join_goods_supplier_id', 'creator_user_id', 'goods_status', 'goods_sales_price', 'goods_category', 'goods_name', 'goods_title', 'goods_cover', 'goods_sort', 'goods_attribute_json', 'goods_addtimes', 'goods_updatetimes')
  168. ->when($goodsName != '', function ($query) use ($goodsName) {
  169. $query->where(function ($q) use ($goodsName) {
  170. $q->where('goods_name', 'like', '%' . $goodsName . '%')
  171. ->OrWhere('goods_title', 'like', '%' . $goodsName . '%');
  172. });
  173. })->when($categoryId != null, function ($query) use ($categoryId) {
  174. $query->whereIn('join_goods_category_id', $categoryId);
  175. })->when($goodsCategory != null, function ($query) use ($goodsCategory) {
  176. $query->where('goods_category', $goodsCategory);
  177. })
  178. ->when($classify != '', function ($query) use ($classify, $categoryId) {
  179. if ($classify == 'GOODS' && empty($categoryId)) {
  180. $query->whereIn('join_goods_category_id', ['6', '7', '8', '9', '10', '11', '12', '30']);
  181. } else if ($classify != 'GOODS' && empty($categoryId)) {
  182. $query->where('goods_classify', $classify);
  183. }
  184. })->when( !empty($type),function ($query) use ($type){
  185. if ($type == 'storageWarning'){
  186. $query->where('goods_running.goods_running_storage','<=',2);
  187. }
  188. })
  189. ->orderBy('goods_sort', 'DESC')
  190. ->orderBy('goods_addtimes', 'DESC')
  191. ->forPage($page, $pageSize)
  192. ->get()
  193. ->toArray();
  194. $total = Goods::leftJoin('goods_running','goods_running.join_running_goods_id','=','goods.goods_id')
  195. ->when($goodsName != '', function ($query) use ($goodsName) {
  196. $query->where(function ($q) use ($goodsName) {
  197. $q->where('goods_name', 'like', '%' . $goodsName . '%')
  198. ->OrWhere('goods_title', 'like', '%' . $goodsName . '%');
  199. });
  200. })->when($categoryId != null, function ($query) use ($categoryId) {
  201. $query->whereIn('join_goods_category_id', $categoryId);
  202. })->when($goodsCategory != null, function ($query) use ($goodsCategory) {
  203. $query->where('goods_category', $goodsCategory);
  204. })->when($classify != '', function ($query) use ($classify, $categoryId) {
  205. if ($classify == 'GOODS' && empty($categoryId)) {
  206. $query->whereIn('join_goods_category_id', ['6', '7', '8', '9', '10', '11', '12', '30']);
  207. } else if ($classify != 'GOODS' && empty($categoryId)) {
  208. $query->where('goods_classify', $classify);
  209. }
  210. })->when( !empty($type),function ($query) use ($type){
  211. if ($type == 'storageWarning'){
  212. $query->where('goods_running.goods_running_storage','<=',2);
  213. }
  214. })
  215. ->count();
  216. foreach ($rows as &$row) {
  217. $row['goods_cover'] = getenv('STORAGE_DOMAIN') . $row['goods_cover'];
  218. if (isset($row['running'])) {
  219. $row['running']['goods_running_storage'] = intval($row['running']['goods_running_storage']);
  220. }
  221. if (!empty($row['goods_attribute_json'])) {
  222. $row['goods_attribute_json'] = json_decode($row['goods_attribute_json']);
  223. }
  224. if (!empty($row['goods_category']) && $row['goods_category'] == 'INDEX') {
  225. $row['goods_recommend_index'] = 'INDEX';
  226. }
  227. }
  228. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  229. }
  230. public static function selectSpecial(Request $request)
  231. {
  232. $page = $request->get('page');
  233. $pageSize = $request->get('pageSize');
  234. $goodsName = $request->get('goods_name', '');
  235. $categoryId = $request->get('join_goods_category_id', null);
  236. if ($categoryId == null) {
  237. $categoryId = [65, 43];
  238. } elseif (is_string($categoryId)) {
  239. $categoryId = [$categoryId];
  240. }
  241. $rows = Goods::with([
  242. 'category' => function ($query) {
  243. $query->select('category_id', 'category_name');
  244. },
  245. 'running' => function ($query) {
  246. $query->select('join_running_goods_id', 'goods_running_storage');
  247. },
  248. 'supplier' => function ($query) {
  249. $query->select('supplier_id', 'supplier_name');
  250. },
  251. 'user' => function ($query) {
  252. $query->select('user_id', 'user_name');
  253. }
  254. ])->select('goods_id', 'join_goods_category_id', 'join_goods_supplier_id', 'creator_user_id', 'goods_status', 'goods_sales_price', 'goods_category', 'goods_name', 'goods_title', 'goods_cover', 'goods_sort', 'goods_addtimes', 'goods_updatetimes')
  255. ->when($goodsName != '', function ($query) use ($goodsName) {
  256. $query->where(function ($q) use ($goodsName) {
  257. $q->where('goods_name', 'like', '%' . $goodsName . '%')
  258. ->OrWhere('goods_title', 'like', '%' . $goodsName . '%');
  259. });
  260. })->whereIn('join_goods_category_id', $categoryId)
  261. ->orderBy('goods_sort', 'DESC')
  262. ->orderBy('goods_addtimes', 'DESC')
  263. ->forPage($page, $pageSize)
  264. ->get()
  265. ->toArray();
  266. $total = Goods::when($goodsName != '', function ($query) use ($goodsName) {
  267. $query->where(function ($q) use ($goodsName) {
  268. $q->where('goods_name', 'like', '%' . $goodsName . '%')
  269. ->OrWhere('goods_title', 'like', '%' . $goodsName . '%');
  270. });
  271. })->whereIn('join_goods_category_id', $categoryId)->count();
  272. foreach ($rows as &$row) {
  273. $row['goods_cover'] = getenv('STORAGE_DOMAIN') . $row['goods_cover'];
  274. if (isset($row['running'])) {
  275. $row['running']['goods_running_storage'] = intval($row['running']['goods_running_storage']);
  276. }
  277. }
  278. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  279. }
  280. public static function selectPicking(Request $request)
  281. {
  282. $page = $request->get('page');
  283. $pageSize = $request->get('pageSize');
  284. $goodsName = $request->get('goods_name', '');
  285. $categorySuperId = $request->get('category_super_id', '');
  286. $categoryIds = $request->get('join_goods_category_id', []);
  287. if (!empty($categorySuperId) && is_array($categoryIds)) {
  288. $category = SysCategory::where('category_id', $categorySuperId)->first();
  289. if (empty($category->category_super_path)) {
  290. $category->category_super_path = '#' . $categorySuperId . '#';
  291. } else {
  292. $category->category_super_path = $category->category_super_path . '#' . $categorySuperId . '#';
  293. }
  294. $categoryIds = SysCategory::where('category_super_path', 'like', '%' . $category->category_super_path)->pluck('category_id');
  295. $categoryIds = $categoryIds ? $categoryIds->toArray() : [];
  296. $categoryIds = array_merge($categoryIds, [$categorySuperId]);
  297. } elseif (!is_array($categoryIds)) {
  298. $categoryIds = [$categoryIds];
  299. }
  300. $rows = Goods::with([
  301. 'category' => function ($query) {
  302. $query->select('category_id', 'category_name');
  303. },
  304. 'running' => function ($query) {
  305. $query->select('join_running_goods_id', 'goods_running_storage');
  306. },
  307. 'supplier' => function ($query) {
  308. $query->select('supplier_id', 'supplier_name');
  309. },
  310. 'user' => function ($query) {
  311. $query->select('user_id', 'user_name');
  312. }
  313. ])->select('goods_id', 'join_goods_category_id', 'join_goods_supplier_id', 'creator_user_id', 'goods_status', 'goods_sales_price', 'goods_category', 'goods_name', 'goods_title', 'goods_cover', 'goods_sort', 'goods_addtimes', 'goods_updatetimes')
  314. ->when($goodsName != '', function ($query) use ($goodsName) {
  315. $query->where(function ($q) use ($goodsName) {
  316. $q->where('goods_name', 'like', '%' . $goodsName . '%')
  317. ->OrWhere('goods_title', 'like', '%' . $goodsName . '%');
  318. });
  319. })->whereIn('join_goods_category_id', $categoryIds)
  320. ->orderBy('goods_sort', 'DESC')
  321. ->orderBy('goods_addtimes', 'DESC')
  322. ->forPage($page, $pageSize)
  323. ->get()
  324. ->toArray();
  325. $total = Goods::when($goodsName != '', function ($query) use ($goodsName) {
  326. $query->where(function ($q) use ($goodsName) {
  327. $q->where('goods_name', 'like', '%' . $goodsName . '%')
  328. ->OrWhere('goods_title', 'like', '%' . $goodsName . '%');
  329. });
  330. })->whereIn('join_goods_category_id', $categoryIds)
  331. ->count();
  332. foreach ($rows as &$row) {
  333. $row['goods_cover'] = getenv('STORAGE_DOMAIN') . $row['goods_cover'];
  334. if (isset($row['running'])) {
  335. $row['running']['goods_running_storage'] = intval($row['running']['goods_running_storage']);
  336. }
  337. }
  338. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  339. }
  340. public static function selectPackage(Request $request)
  341. {
  342. $page = $request->get('page');
  343. $pageSize = $request->get('pageSize');
  344. $goodsName = $request->get('goods_name', '');
  345. $categoryId = $request->get('join_goods_category_id', null);
  346. $rows = Goods::with([
  347. 'category' => function ($query) {
  348. $query->select('category_id', 'category_name');
  349. },
  350. 'running' => function ($query) {
  351. $query->select('join_running_goods_id', 'goods_running_storage');
  352. },
  353. 'supplier' => function ($query) {
  354. $query->select('supplier_id', 'supplier_name');
  355. },
  356. 'user' => function ($query) {
  357. $query->select('user_id', 'user_name');
  358. }
  359. ])->select('goods_id', 'join_goods_category_id', 'join_goods_supplier_id', 'creator_user_id', 'goods_status', 'goods_sales_price', 'goods_category', 'goods_name', 'goods_title', 'goods_cover', 'goods_sort', 'goods_addtimes', 'goods_updatetimes')
  360. ->when($goodsName != '', function ($query) use ($goodsName) {
  361. $query->where(function ($q) use ($goodsName) {
  362. $q->where('goods_name', 'like', '%' . $goodsName . '%')
  363. ->OrWhere('goods_title', 'like', '%' . $goodsName . '%');
  364. });
  365. })->when($categoryId != null, function ($query) use ($categoryId) {
  366. $query->where('join_goods_category_id', $categoryId);
  367. })
  368. ->where('goods_classify', 'PACKAGE')
  369. ->orderBy('goods_sort', 'DESC')
  370. ->orderBy('goods_addtimes', 'DESC')
  371. ->forPage($page, $pageSize)
  372. ->get()
  373. ->toArray();
  374. $total = Goods::when($goodsName != '', function ($query) use ($goodsName) {
  375. $query->where(function ($q) use ($goodsName) {
  376. $q->where('goods_name', 'like', '%' . $goodsName . '%')
  377. ->OrWhere('goods_title', 'like', '%' . $goodsName . '%');
  378. });
  379. })->when($categoryId != null, function ($query) use ($categoryId) {
  380. $query->where('join_goods_category_id', $categoryId);
  381. })->where('goods_classify', 'PACKAGE')->count();
  382. foreach ($rows as &$row) {
  383. $row['goods_cover'] = getenv('STORAGE_DOMAIN') . $row['goods_cover'];
  384. if (isset($row['running'])) {
  385. $row['running']['goods_running_storage'] = intval($row['running']['goods_running_storage']);
  386. }
  387. // if (!empty($row['component'])) {
  388. // $ids = [];
  389. // $contentList = [];
  390. // foreach ($row['component'] as $component) {
  391. // $ids[] = $component['join_component_goods_id'];
  392. // $configJson = json_decode($component['goods_component_config_json'], true);
  393. // $contentList[] = [
  394. // 'goods_name' => $configJson['goods_name'] ?? '',
  395. // 'goods_sales_price' => $component['goods_component_price'],
  396. // 'nbr' => $configJson['nbr'] ?? 0
  397. // ];
  398. // }
  399. //
  400. // $row['join_component_goods_id'] = $ids;
  401. // $row['goodsContentList'] = $contentList;
  402. // }
  403. }
  404. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  405. }
  406. /**
  407. * @Desc 下拉选择服务商品
  408. * @Author Gorden
  409. * @Date 2024/4/24 13:32
  410. *
  411. * @param Request $request
  412. * @return Response
  413. */
  414. public static function selectList(Request $request, $goodsClassify = "SERVICE")
  415. {
  416. $keywords = $request->get('keywords', '');
  417. $isSupportAppointment = $request->get('is_support_appointment', '');
  418. // if (!$keywords){
  419. // return json_success('暂无数据');
  420. // }
  421. // $categoryIds = SysCategory::whereIn('category_super_id', [5, 31, 32, 42, 66, 70, 72])->pluck('category_id');
  422. $goods = Goods::with('sku')
  423. // ->whereIn('join_goods_category_id', $categoryIds)
  424. ->when($keywords != '', function ($query) use ($keywords) {
  425. $query->where('goods_name', 'like', "%" . $keywords . "%");
  426. })
  427. ->when($goodsClassify != '', function ($query) use ($goodsClassify) {
  428. if ($goodsClassify == 'NOPACKAGE') {
  429. $query->where('goods_classify', '<>', 'PACKAGE');
  430. } else if ($goodsClassify == 'SERVICE') {
  431. $query->whereIn('goods_classify', ['SERVICE', 'CHNMED', 'CHNNCD']);
  432. } else {
  433. $query->where('goods_classify', $goodsClassify);
  434. }
  435. })
  436. ->when($isSupportAppointment != '', function ($query) use ($isSupportAppointment) {
  437. $query->where('is_support_appointment', $isSupportAppointment);
  438. })
  439. ->select('goods_id', 'goods_name', 'goods_sales_price', 'join_goods_category_id', 'goods_attribute_json')
  440. ->get()
  441. ->toArray();
  442. foreach ($goods as &$good) {
  443. if (!empty($good['sku'])) {
  444. foreach ($good['sku'] as $key => $sku) {
  445. if (!empty($sku['goods_sku_specs_json'])) {
  446. $good['sku'][$key]['goods_sku_specs_json'] = json_decode($sku['goods_sku_specs_json']);
  447. $skuName = '';
  448. foreach ($good['sku'][$key]['goods_sku_specs_json'] as $specsKey => $skuSpecs) {
  449. if (is_array($skuSpecs)) {
  450. $skuName = $skuName . ' ' . $specsKey . ':' . implode(' ', $skuSpecs) . ';';
  451. } else {
  452. $skuName = $skuName . ' ' . $specsKey . ':' . $skuSpecs . ';';
  453. }
  454. }
  455. $good['sku'][$key]['sku_name'] = $skuName;
  456. }
  457. }
  458. } else {
  459. $good['sku'] = [];
  460. }
  461. $good['premisses'] = [];
  462. if (!empty($good['goods_attribute_json'])) {
  463. $attributeJson = json_decode($good['goods_attribute_json'], true);
  464. if (isset($attributeJson['premisses'])) {
  465. $premisses = SysDept::when(!empty($attributeJson['premisses']), function ($query) use ($attributeJson) {
  466. $query->whereIn('dept_id', $attributeJson['premisses']);
  467. })->where('dept_category', '营业场所')
  468. ->select('dept_id', 'dept_name')
  469. ->get();
  470. $good['premisses'] = $premisses;
  471. }
  472. }
  473. }
  474. return json_success('', $goods);
  475. }
  476. public static function selectCascaderList(Request $request)
  477. {
  478. $categoryIds = $request->get('category_id','');
  479. $categorySuperId = $request->get('category_super_id','');
  480. $joinGoodsCategoryId = $request->get('join_goods_category_id','');
  481. $type = $request->get('type','');
  482. if (!$categoryIds && !$categorySuperId && !$joinGoodsCategoryId){
  483. return json_fail('参数异常');
  484. }
  485. $data = [];
  486. $categorys = [];
  487. if (!empty($categoryIds)){
  488. $categorys = SysCategory::whereIn('category_id',$categoryIds)
  489. ->where('category_status','ACTIVED')
  490. ->select('category_id as id','category_name as name','category_super_id as pid','category_super_path')
  491. ->orderBy('category_sort','DESC')
  492. ->get()
  493. ->toArray();
  494. $data = array_merge($data,$categorys);
  495. }else if(!empty($categorySuperId)){
  496. $categorys = SysCategory::where('category_super_id',$categorySuperId)
  497. ->where('category_status','ACTIVED')
  498. ->select('category_id as id','category_name as name','category_super_id as pid','category_super_path')
  499. ->orderBy('category_sort','DESC')
  500. ->get()
  501. ->toArray();
  502. $data = array_merge($data,$categorys);
  503. }
  504. foreach ($categorys as $category){
  505. // if(empty($category['category_super_path'])){
  506. $category['category_super_path'] = '#'.$category['id'].'#';
  507. // }
  508. $subCategory = SysCategory::where('category_super_path','like','%'.$category['category_super_path'].'%')
  509. ->where('category_status','ACTIVED')
  510. ->select('category_id as id','category_name as name','category_super_id as pid','category_super_path')
  511. ->orderBy('category_sort','DESC')
  512. ->get()
  513. ->toArray();
  514. $data = array_merge($data,$subCategory);
  515. }
  516. if (empty($data) && !empty($joinGoodsCategoryId)){
  517. $goodsCategoryIds = $joinGoodsCategoryId;
  518. }else{
  519. $goodsCategoryIds = array_column($data,'id');
  520. }
  521. $goods = Goods::with('sku')
  522. ->whereIn('join_goods_category_id',$goodsCategoryIds)
  523. ->where('goods_status','ON');
  524. if ($type == 'dishes'){
  525. $uid = JwtToken::getCurrentId();
  526. $user = SysUser::where('user_id', $uid)->first();
  527. $restaurant = SysDept::where('dept_category', '餐厅')->where(function ($query) use ($user) {
  528. $query->where('dept_id', $user->join_user_dept_id)->orWhere('dept_super_id', $user->join_user_dept_id);
  529. })->first();
  530. dump($restaurant->dept_id);
  531. $supplier = Supplier::where('join_supplier_dept_id',$restaurant->dept_id)->first();
  532. if ($supplier){
  533. $goods = $goods->where('join_goods_supplier_id',$supplier->supplier_id);
  534. }
  535. }
  536. $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')
  537. ->orderBy('goods_sort','DESC')
  538. ->orderBy('goods_addtimes','DESC')
  539. ->get()
  540. ->toArray();
  541. foreach ($goods as &$good) {
  542. $good['goods_cover'] = getenv('STORAGE_DOMAIN').$good['goods_cover'];
  543. $good['nbr'] = 0;
  544. if (!empty($good['sku'])) {
  545. foreach ($good['sku'] as $key => $sku) {
  546. if (!empty($sku['goods_sku_specs_json'])) {
  547. $good['sku'][$key]['goods_sku_specs_json'] = json_decode($sku['goods_sku_specs_json']);
  548. $skuName = '';
  549. foreach ($good['sku'][$key]['goods_sku_specs_json'] as $specsKey => $skuSpecs) {
  550. $keyStr = ($specsKey == '规格') ? '' : ($specsKey.':');
  551. if (is_array($skuSpecs)) {
  552. $skuName = $skuName .$keyStr . ' ' . implode(' ', $skuSpecs) . '; ';
  553. } else {
  554. $skuName = $skuName .$keyStr . ' ' . $skuSpecs . '; ';
  555. }
  556. }
  557. $good['sku'][$key]['sku_name'] = rtrim($skuName,'; ');
  558. }
  559. }
  560. } else {
  561. $good['sku'] = [];
  562. }
  563. $good['premisses'] = [];
  564. if (!empty($good['goods_attribute_json'])) {
  565. $attributeJson = json_decode($good['goods_attribute_json'], true);
  566. if (isset($attributeJson['premisses'])) {
  567. $premisses = SysDept::when(!empty($attributeJson['premisses']), function ($query) use ($attributeJson) {
  568. $query->whereIn('dept_id', $attributeJson['premisses']);
  569. })->where('dept_category', '营业场所')
  570. ->select('dept_id', 'dept_name')
  571. ->get();
  572. $good['premisses'] = $premisses;
  573. }
  574. }
  575. }
  576. $data = array_merge($data,$goods);
  577. $tree = new Tree($data);
  578. $cascaderData = $tree->getTree();
  579. foreach($cascaderData as $key1 => $cascader1){
  580. if(isset($cascader1['children'])){
  581. foreach($cascader1['children'] as $key2 => $cascader2){
  582. if(isset($cascader2['children'])){
  583. foreach($cascader2['children'] as $key3 => $cascader3){
  584. if(isset($cascader3['children'])){
  585. foreach($cascader3['children'] as $key4 => $cascader4){
  586. if (!isset($cascader4['goods_id'])){
  587. unset($cascaderData[$key1]['children'][$key2]['children'][$key3]['children'][$key4]);
  588. }
  589. }
  590. }else if (!isset($cascader3['goods_id'])){
  591. unset($cascaderData[$key1]['children'][$key2]['children'][$key3]);
  592. }
  593. if (isset($cascader3['children']) && count($cascaderData[$key1]['children'][$key2]['children']) == 0){
  594. unset($cascaderData[$key1]['children'][$key2]);
  595. }
  596. if (isset($cascader3['children']) && count($cascaderData[$key1]['children'][$key2]['children'][$key3]['children']) > 0){
  597. $cascaderData[$key1]['children'][$key2]['children'][$key3]['children'] = array_values($cascaderData[$key1]['children'][$key2]['children'][$key3]['children']);
  598. }
  599. }
  600. }else if (!isset($cascader2['goods_id'])){
  601. unset($cascaderData[$key1]['children'][$key2]);
  602. }
  603. if (isset($cascader2['children']) && count($cascaderData[$key1]['children'][$key2]['children']) == 0){
  604. unset($cascaderData[$key1]['children'][$key2]);
  605. }
  606. if (isset($cascader2['children']) && isset($cascaderData[$key1]['children'][$key2]) && count($cascaderData[$key1]['children'][$key2]['children']) > 0){
  607. $cascaderData[$key1]['children'][$key2]['children'] = array_values($cascaderData[$key1]['children'][$key2]['children']);
  608. }
  609. }
  610. }else if (!isset($cascader1['goods_id'])){
  611. unset($cascaderData[$key1]);
  612. }
  613. if (isset($cascader1['children']) && count($cascaderData[$key1]['children']) == 0){
  614. unset($cascaderData[$key1]);
  615. }
  616. if (isset($cascader1['children']) && isset($cascaderData[$key1]) && count($cascaderData[$key1]['children']) > 0){
  617. $cascaderData[$key1]['children'] = array_values($cascaderData[$key1]['children']);
  618. }
  619. }
  620. $cascaderData = array_values($cascaderData);
  621. return json_success('success', $cascaderData);
  622. }
  623. /**
  624. * @Desc 商品详情
  625. * @Author Gorden
  626. * @Date 2024/3/28 10:25
  627. *
  628. * @param $goodsId
  629. * @return Response
  630. */
  631. public static function info($goodsId)
  632. {
  633. try {
  634. // 商品主表
  635. $main = Goods::with('category')->where('goods_id', $goodsId)->first();
  636. if (!empty($main)) {
  637. $main = $main->toArray();
  638. $main['goods_sku_json'] = json_decode($main['goods_sku_json'], true);
  639. $main['specList'] = [];
  640. foreach ($main['goods_sku_json'] as $key => $sku) {
  641. $main['specList'][] = [
  642. 'label' => $key,
  643. 'tags' => $sku
  644. ];
  645. }
  646. } else {
  647. $main = [];
  648. }
  649. // 详情表
  650. $detail = GoodsDetail::where('join_detail_goods_id', $goodsId)->first();
  651. if (!empty($detail)) {
  652. $detail = $detail->toArray();
  653. } else {
  654. $detail = [];
  655. }
  656. // 标签表
  657. $label = GoodsLabel::where('join_label_goods_id', $goodsId)->first();
  658. if (!empty($label)) {
  659. $label = $label->toArray();
  660. } else {
  661. $label = [];
  662. }
  663. // Running表
  664. $running = GoodsRunning::where('join_running_goods_id', $goodsId)->first();
  665. if (!empty($running)) {
  666. $running = $running->toArray();
  667. if (!empty($running['goods_running_off_json']) && is_json($running['goods_running_off_json'])) {
  668. $goodsRunningOffJson = json_decode($running['goods_running_off_json'], true);
  669. $running['goods_off_addtimes'] = isset($goodsRunningOffJson['time']) ? date("Y-m-d H:i", $goodsRunningOffJson['time']) : '';
  670. }
  671. $running['goods_running_storage'] = !empty($running['goods_running_storage']) ? intval($running['goods_running_storage']) : '';
  672. $running['goods_running_sale'] = !empty($running['goods_running_sale']) ? intval($running['goods_running_sale']) : '';
  673. } else {
  674. $running = [];
  675. }
  676. // Sku表
  677. $skus = GoodsSku::where('join_sku_goods_id', $goodsId)->get();
  678. if (!empty($skus)) {
  679. $skus = $skus->toArray();
  680. $submitList = [];
  681. foreach ($skus as $key => $sku) {
  682. $skuSpecsJson = json_decode($sku['goods_sku_specs_json'], true);
  683. $skuSpecs = '';
  684. $skuNameValue = [];
  685. $i = 1;
  686. foreach ($skuSpecsJson as $k => $item) {
  687. if (is_array($item)) {
  688. $item = implode(',', $item);
  689. }
  690. $skuSpecs = $skuSpecs . $item . ',';
  691. $skuNameKey = 'skuName' . $i;
  692. $skuValueKey = 'skuValue' . $i;
  693. $skuNameValue[$skuNameKey] = $k;
  694. $skuNameValue[$skuValueKey] = $item;
  695. $skuNameValue[$k] = $item;
  696. $i++;
  697. }
  698. $storage = json_decode($sku['goods_sku_storage_json'], true);
  699. $priceStorage = [
  700. 'sku_id' => $sku['goods_sku_id'],
  701. 'sku' => rtrim($skuSpecs, ',') ?? '',
  702. 'stock' => $storage['storage'] ?? '',
  703. 'price' => $sku['goods_sku_sales_price'] ?? '',
  704. ];
  705. $submitList[] = array_merge($skuNameValue, $priceStorage);
  706. }
  707. $skus['submitList'] = $submitList;
  708. } else {
  709. $skus = [];
  710. }
  711. // 合并数据
  712. $data = array_merge($main, $detail, $label, $running, ['sku' => $skus]);
  713. $data['goods_sku_json_label'] = [];
  714. $data['goods_label'] = !empty($data['goods_label']) ? explode(',', $data['goods_label']) : [];
  715. $data['goods_json'] = $data['goods_json'] ? json_decode($data['goods_json'], true) : [];
  716. $data['goods_cover'] = getenv('STORAGE_DOMAIN') . $data['goods_cover'];
  717. if (!empty($data['goods_category']) && $data['goods_category'] == 'INDEX') {
  718. $data['goods_recommend_index'] = 'INDEX';
  719. }
  720. // 创建者
  721. $data['creator_username'] = '';
  722. if (!empty($data['creator_user_id'])) {
  723. $data['creator_username'] = SysUser::where('user_id', $data['creator_user_id'])->value('user_name');
  724. }
  725. if (!empty($data['goods_detail_slider_json'])) {
  726. $data['goods_detail_slider_json'] = json_decode($data['goods_detail_slider_json'], true);
  727. // ……
  728. if (isset($data['goods_detail_slider_json']['slider'])) {
  729. $data['goods_detail_slider_json'] = explode(',', $data['goods_detail_slider_json']['slider']);
  730. }
  731. $slider = '';
  732. foreach ($data['goods_detail_slider_json'] as $item) {
  733. $slider .= getenv('STORAGE_DOMAIN') . $item . ',';
  734. }
  735. $data['goods_detail_slider_json'] = rtrim($slider, ',');
  736. }
  737. $extendJson = [];
  738. if (!empty($data['goods_attribute_json'])) {
  739. $extendJson = json_decode($data['goods_attribute_json'], true);
  740. $data['goods_attribute_json'] = $extendJson;
  741. if (isset($extendJson['premisses'])) {
  742. $data['goods_premisses'] = $extendJson['premisses'];
  743. $data['goods_premisses_str'] = SysDept::whereIn('dept_id', $extendJson['premisses'])->pluck('dept_name');
  744. }
  745. if (isset($extendJson['bg'])) {
  746. $data['goods_theme_color'] = $extendJson['bg'];
  747. }
  748. if (isset($extendJson['icon'])) {
  749. $data['goods_theme_icon'] = getenv('STORAGE_DOMAIN') . $extendJson['icon'];
  750. }
  751. if (isset($extendJson['service_premises_id'])) {
  752. $data['goods_service_premises'] = $extendJson['service_premises_id'];
  753. }
  754. }
  755. $data['express_json'] = [];
  756. if (!empty($data['goods_express_json'])) {
  757. $goodsExpressJson = json_decode($data['goods_express_json'], true);
  758. if (isset($goodsExpressJson['express']) && $goodsExpressJson['express'] == 'Y') {
  759. $data['express_json'][] = 'express';
  760. }
  761. if (isset($goodsExpressJson['self']) && $goodsExpressJson['self'] == 'Y') {
  762. $data['express_json'][] = 'self';
  763. }
  764. if (isset($goodsExpressJson['arrival']) && $goodsExpressJson['arrival'] == 'Y') {
  765. $data['express_json'][] = 'arrival';
  766. }
  767. }
  768. $data['appointment_times'] = [];
  769. if ($data['is_support_appointment'] == 'Y' && isset($extendJson['dates'])) {
  770. $data['dates'] = $extendJson['dates'] ?? [];
  771. if (isset($extendJson['times'])) {
  772. $times = [];
  773. foreach ($extendJson['times'] as $time) {
  774. if (!empty($time['duration'])) {
  775. $startEndTime = explode('-', $time['duration']);
  776. $times[] = [
  777. 'person' => $time['person'],
  778. 'appointmentTimeStart' => $startEndTime[0],
  779. 'appointmentTimeEnd' => $startEndTime[1],
  780. ];
  781. }
  782. }
  783. $data['appointment_times'] = $times;
  784. }
  785. if (isset($extendJson['time'])) {
  786. $extendJsonTime = explode('至', $extendJson['time']);
  787. if (isset($extendJsonTime[0]) && isset($extendJsonTime[1])) {
  788. $data['work_time'] = [
  789. date('Y-m-d H:i:s', strtotime(date('Y-m-d ') . $extendJsonTime[0])),
  790. date('Y-m-d H:i:s', strtotime(date('Y-m-d ') . $extendJsonTime[1])),
  791. ];
  792. }
  793. }
  794. // if (isset($extendJson['teachers'])){
  795. // $data['teachers'] = $extendJson['teachers'];
  796. // }
  797. // if (isset($extendJson['address'])) {
  798. // $data['address'] = $extendJson['address'];
  799. // }
  800. // if (isset($extendJson['min-count'])){
  801. // $data['min_count'] = $extendJson['min-count'];
  802. // }
  803. // if (isset($extendJson['position'])) {
  804. // $data['position'] = $extendJson['position'];
  805. // }
  806. if (isset($extendJson['label'])) {
  807. $data['appointment_label'] = $extendJson['label'];
  808. }
  809. }
  810. if (isset($extendJson['month']) && $data['is_support_appointment'] == 'Y') {
  811. $times = [];
  812. foreach ($extendJson['month'] as $monKey => $months) {
  813. foreach ($months as $day => $person) {
  814. $times[] = [
  815. 'days' => date('Y-m-d', strtotime($monKey . $day)),
  816. 'person' => $person
  817. ];
  818. }
  819. }
  820. $data['appointment_times'] = $times;
  821. $data['travel_begin'] = $extendJson['travel-begin'];
  822. $data['travel_day'] = $extendJson['travel-day'];
  823. $data['travel_night'] = $extendJson['travel-night'];
  824. $data['travel_trans'] = $extendJson['travel-trans'];
  825. }
  826. if (!empty($data['goods_json']) && $data['join_goods_category_id'] == 65) {
  827. // foreach ($data['goods_json'] as $key => $datum) {
  828. // $data['goods_json'][$key]['color'] = rgbToHex($datum['color']);
  829. // }
  830. } elseif (!empty($data['goods_json']) && $data['join_goods_category_id'] == 43) {
  831. $goodsJsonNew = [];
  832. foreach ($data['goods_json'] as $key1 => $item1) {
  833. $itemsNew = [];
  834. if (isset($item1['items'])) {
  835. foreach ($item1['items'] as $key2 => $item2) {
  836. $itemsNew[] = [
  837. 'key' => $key2,
  838. 'params' => $item2
  839. ];
  840. }
  841. }
  842. $goodsJsonNew[] = [
  843. 'title' => $key1,
  844. 'service' => $item1['service'] ?? '',
  845. 'items' => $itemsNew
  846. ];
  847. }
  848. $data['goods_json'] = $goodsJsonNew;
  849. }
  850. if (isset($extendJson['teachers'])) {
  851. $data['teachers'] = $extendJson['teachers'];
  852. }
  853. if (isset($extendJson['address'])) {
  854. $data['address'] = $extendJson['address'];
  855. }
  856. if (isset($extendJson['min-count'])) {
  857. $data['min_count'] = $extendJson['min-count'];
  858. }
  859. if (isset($extendJson['position'])) {
  860. $data['position'] = $extendJson['position'];
  861. }
  862. $data['goods_on_addtimes'] = date('Y-m-d\TH:i:s.u\Z', $data['goods_on_addtimes'] - 60 * 60 * 8);
  863. // 详情表数据
  864. if (!empty($data['goods_detail_specs_json'])) {
  865. $specsJson = json_decode($data['goods_detail_specs_json'], true);
  866. foreach ($specsJson as $itemSpecsJson) {
  867. if (isset($itemSpecsJson['key']) && $itemSpecsJson['key'] == '课时') {
  868. $data['curriculum']['period'] = $itemSpecsJson['val'];
  869. } elseif (isset($itemSpecsJson['key']) && $itemSpecsJson['key'] == '群体') {
  870. $data['curriculum']['group'] = $itemSpecsJson['val'];
  871. } elseif (isset($itemSpecsJson['key']) && $itemSpecsJson['key'] == '课程') {
  872. $data['curriculum']['course'] = $itemSpecsJson['val'];
  873. }
  874. }
  875. }
  876. return json_success('', $data);
  877. } catch (\Exception $e) {
  878. dump($e->getMessage());
  879. return json_fail("查询错误~");
  880. }
  881. }
  882. public static function infoPackage($goodsId)
  883. {
  884. try {
  885. // 商品主表
  886. $main = Goods::where('goods_id', $goodsId)->first();
  887. if (!empty($main)) {
  888. $main = $main->toArray();
  889. $main['goods_sku_json'] = json_decode($main['goods_sku_json'], true);
  890. $main['specList'] = [];
  891. foreach ($main['goods_sku_json'] as $key => $sku) {
  892. $main['specList'][] = [
  893. 'label' => $key,
  894. 'tags' => $sku
  895. ];
  896. }
  897. } else {
  898. $main = [];
  899. }
  900. // 详情表
  901. $detail = GoodsDetail::where('join_detail_goods_id', $goodsId)->first();
  902. if (!empty($detail)) {
  903. $detail = $detail->toArray();
  904. } else {
  905. $detail = [];
  906. }
  907. // 标签表
  908. $label = GoodsLabel::where('join_label_goods_id', $goodsId)->first();
  909. if (!empty($label)) {
  910. $label = $label->toArray();
  911. } else {
  912. $label = [];
  913. }
  914. // Running表
  915. $running = GoodsRunning::where('join_running_goods_id', $goodsId)->first();
  916. if (!empty($running)) {
  917. $running = $running->toArray();
  918. if (!empty($running['goods_running_off_json']) && is_json($running['goods_running_off_json'])) {
  919. $goodsRunningOffJson = json_decode($running['goods_running_off_json'], true);
  920. $running['goods_off_addtimes'] = isset($goodsRunningOffJson['time']) ? date("Y-m-d H:i", $goodsRunningOffJson['time']) : '';
  921. }
  922. } else {
  923. $running = [];
  924. }
  925. // 组件表
  926. $component = GoodsComponent::where('join_component_master_goods_id', $goodsId)->get()->toArray();
  927. $componentArr['join_component_goods_id'] = [];
  928. $componentArr['goodsContentList'] = [];
  929. if ($component) {
  930. $ids = [];
  931. $contentList = [];
  932. foreach ($component as $item) {
  933. if (empty($item['join_component_goods_id'])) {
  934. continue;
  935. }
  936. $skus = GoodsSku::where('join_sku_goods_id', $item['join_component_goods_id'])
  937. ->select('goods_sku_id', 'join_sku_goods_id', 'goods_sku_specs_json', 'goods_sku_sales_price')
  938. ->get()
  939. ->toArray();
  940. foreach ($skus as $key => $sku2) {
  941. if (!empty($sku2['goods_sku_specs_json'])) {
  942. $skus[$key]['goods_sku_specs_json'] = json_decode($sku2['goods_sku_specs_json'], true);
  943. $skuName = '';
  944. foreach ($skus[$key]['goods_sku_specs_json'] as $specsKey => $skuSpecs) {
  945. if (is_array($skuSpecs)) {
  946. $skuName = $skuName . ' ' . $specsKey . ' ' . implode(' ', $skuSpecs);
  947. } else {
  948. $skuName = $skuName . ' ' . $specsKey . ' ' . $skuSpecs;
  949. }
  950. }
  951. $skus[$key]['sku_name'] = $skuName;
  952. }
  953. }
  954. $ids[] = $item['join_component_goods_id'];
  955. $configJson = json_decode($item['goods_component_json'], true);
  956. $contentList[] = [
  957. 'goods_id' => $item['join_component_goods_id'],
  958. 'goods_name' => $configJson['goods_name'] ?? '',
  959. 'goods_sales_price' => $item['goods_component_price'],
  960. 'nbr' => $configJson['nbr'] ?? 0,
  961. 'sku_id' => $configJson['sku_id'] ?? '',
  962. 'skus' => $skus
  963. ];
  964. }
  965. $componentArr['join_component_goods_id'] = $ids;
  966. $componentArr['goodsContentList'] = $contentList;
  967. }
  968. // 合并数据
  969. $data = array_merge($main, $detail, $label, $running, $componentArr);
  970. $data['goods_sku_json_label'] = [];
  971. $data['goods_label'] = !empty($data['goods_label']) ? explode(',', $data['goods_label']) : [];
  972. $data['goods_cover'] = getenv('STORAGE_DOMAIN') . $data['goods_cover'];
  973. if (!empty($data['goods_detail_slider_json'])) {
  974. $data['goods_detail_slider_json'] = json_decode($data['goods_detail_slider_json'], true);
  975. $slider = '';
  976. foreach ($data['goods_detail_slider_json'] as $item) {
  977. $slider .= getenv('STORAGE_DOMAIN') . $item . ',';
  978. }
  979. $data['goods_detail_slider_json'] = rtrim($slider, ',');
  980. }
  981. $extendJson = [];
  982. if (!empty($data['goods_attribute_json'])) {
  983. $extendJson = json_decode($data['goods_attribute_json'], true);
  984. $data['goods_attribute_json'] = $extendJson;
  985. if (isset($extendJson['premisses'])) {
  986. $data['goods_premisses'] = $extendJson['premisses'];
  987. $data['goods_premisses_str'] = SysDept::whereIn('dept_id', $extendJson['premisses'])->pluck('dept_name');
  988. }
  989. }
  990. $data['goods_on_addtimes'] = date('Y-m-d\TH:i:s.u\Z', $data['goods_on_addtimes'] - 60 * 60 * 8);
  991. return json_success('', $data);
  992. } catch (\Exception $e) {
  993. dump($e->getTrace());
  994. return json_fail("查询错误~");
  995. }
  996. }
  997. /**
  998. * @Desc 添加商品
  999. * @Author Gorden
  1000. * @Date 2024/3/11 10:20
  1001. *
  1002. * @param $params
  1003. * @return Response
  1004. */
  1005. public static function insert($params): Response
  1006. {
  1007. Db::beginTransaction();
  1008. try {
  1009. $params['goods_id'] = "GD" . sprintf('%016d', SysSerial::getSerial()) . random_string(6, 'up');
  1010. // 主表
  1011. self::mainInsert($params);
  1012. // 商品详情表
  1013. self::detailInsert($params);
  1014. // 商品标签表
  1015. self::labelInsert($params);
  1016. // 产品运行控制信息表
  1017. self::goodsRunningInsert($params);
  1018. // sku表
  1019. self::goodsSkuSet($params, 'insert');
  1020. // 待上架状态,上架时间大于当前时间
  1021. if ($params['goods_status'] == 'PENDING' && strtotime($params['goods_on_addtimes']) > time()) {
  1022. $redis = Redis::connection();
  1023. $key = date('YmdHi', strtotime($params['goods_on_addtimes']));
  1024. $redis->sAdd(Goods::LISTING_KEY_PREFIX . $key, $params['goods_id']);
  1025. }
  1026. // 自动下架
  1027. if (!empty($params['goods_running_off_type']) && $params['goods_running_off_type'] == 'T' && !empty($params['goods_off_addtimes'])) {
  1028. $redis = Redis::connection();
  1029. $key = Goods::LISTING_OFF_KEY_PREFIX . date('YmdHi', strtotime($params['goods_off_addtimes']));
  1030. $redis->sAdd($key, $params['goods_id']);
  1031. }
  1032. Db::commit();
  1033. } catch (\PDOException $e) {
  1034. Db::rollBack();
  1035. dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
  1036. return json_fail('数据写入失败~');
  1037. } catch (BusinessException $e) {
  1038. Db::rollBack();
  1039. dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
  1040. return json_fail($e->getMessage());
  1041. } catch (\Exception $e) {
  1042. Db::rollBack();
  1043. dump($e->getTrace());
  1044. return json_fail('数据写入失败~');
  1045. }
  1046. _syslog("添加商品", "商品名【" . $params['goods_name'] . "】");
  1047. return json_success('success');
  1048. }
  1049. public static function insertRecharge($params)
  1050. {
  1051. try {
  1052. Db::beginTransaction();
  1053. $goods = new Goods();
  1054. $goods->goods_id = "GD" . date('YmdHis') . random_string(6, 'up');
  1055. $goods->join_goods_category_id = 59;
  1056. $goods->goods_classify = 'RECHARGE';
  1057. $goods->goods_status = $params['goods_status'];
  1058. $goods->goods_sort = $params['goods_sort'];
  1059. $goods->goods_category = $params['goods_category'];
  1060. $goods->goods_name = $params['goods_name'];
  1061. $goods->goods_market_price = $params['goods_sales_price'];
  1062. $goods->goods_sales_price = $params['goods_sales_price'];
  1063. $goods->goods_sku_json = json_encode(['规格' => [$params['goods_sales_price'] . '元']]);
  1064. $goods->goods_attribute_json = json_encode(['added' => ['nbr' => $params['goods_rate'] / 100, 'mode' => 'rate'], 'min-count' => 1]);
  1065. $goods->goods_cover = '/images/app/common/null-service.png';
  1066. $goods->goods_process_json = json_encode(['mode' => 'do_shopping']);
  1067. $goods->goods_addtimes = time();
  1068. $goods->save();
  1069. $sku = new GoodsSku();
  1070. $sku->join_sku_goods_id = $goods->goods_id;
  1071. $sku->goods_sku_status = $params['goods_status'];
  1072. $sku->goods_sku_specs_json = json_encode(['规格' => $params['goods_sales_price'] . '元']);
  1073. $sku->goods_sku_market_price = $params['goods_sales_price'];
  1074. $sku->goods_sku_sales_price = $params['goods_sales_price'];
  1075. $sku->goods_sku_storage_json = json_encode(['storage' => 9999]);
  1076. $sku->save();
  1077. Db::commit();
  1078. return json_success('success');
  1079. } catch (\Exception $e) {
  1080. Db::rollBack();
  1081. return json_fail('添加充值产品失败');
  1082. }
  1083. }
  1084. public static function updateRecharge($params)
  1085. {
  1086. try {
  1087. Db::beginTransaction();
  1088. $goods = Goods::where('goods_id', $params['goods_id'])->first();
  1089. if (!$goods) {
  1090. return json_fail("数据异常");
  1091. }
  1092. $goods->goods_market_price = $params['goods_sales_price'];
  1093. $goods->goods_sales_price = $params['goods_sales_price'];
  1094. $goods->goods_status = $params['goods_status'];
  1095. $goods->goods_sku_json = json_encode(['规格' => [$params['goods_sales_price'] . '元']]);
  1096. $goods->goods_sort = $params['goods_sort'];
  1097. if (!empty($goods->goods_attribute_json)) {
  1098. $attributeJson = json_decode($goods->goods_attribute_json, true);
  1099. $attributeJson['added']['nbr'] = $params['goods_rate'] / 100;
  1100. $goods->goods_attribute_json = json_encode($attributeJson);
  1101. }
  1102. $goods->save();
  1103. $sku = GoodsSku::where('join_sku_goods_id', $params['goods_id'])->first();
  1104. $sku->goods_sku_status = $params['goods_status'];
  1105. $sku->goods_sku_specs_json = json_encode(['规格' => $params['goods_sales_price'] . '元']);
  1106. $sku->goods_sku_market_price = $params['goods_sales_price'];
  1107. $sku->goods_sku_sales_price = $params['goods_sales_price'];
  1108. $sku->save();
  1109. Db::commit();
  1110. return json_success("success");
  1111. } catch (\Exception $e) {
  1112. Db::rollBack();
  1113. dump($e->getMessage());
  1114. return json_fail('编辑充值产品失败');
  1115. }
  1116. }
  1117. public static function insertPackage($params): Response
  1118. {
  1119. Db::beginTransaction();
  1120. try {
  1121. $params['goods_id'] = "GD" . sprintf('%016d', SysSerial::getSerial()) . random_string(6, 'up');
  1122. // 主表
  1123. self::mainInsert($params);
  1124. // 商品详情表
  1125. self::detailInsert($params);
  1126. // 套包组件表
  1127. self::componentUpdate($params, 'insert');
  1128. // 商品标签表
  1129. self::labelInsert($params);
  1130. // 产品运行控制信息表
  1131. self::goodsRunningInsert($params);
  1132. // sku表
  1133. // self::goodsSkuSet($params, 'insert');
  1134. // 待上架状态,上架时间大于当前时间
  1135. if ($params['goods_status'] == 'PENDING' && strtotime($params['goods_on_addtimes']) > time()) {
  1136. $redis = Redis::connection();
  1137. $key = date('YmdHi', strtotime($params['goods_on_addtimes']));
  1138. $redis->sAdd(Goods::LISTING_KEY_PREFIX . $key, $params['goods_id']);
  1139. }
  1140. Db::commit();
  1141. } catch (\PDOException $e) {
  1142. Db::rollBack();
  1143. dump($e->getMessage());
  1144. return json_fail('数据写入失败~');
  1145. } catch (BusinessException $e) {
  1146. Db::rollBack();
  1147. dump($e->getMessage());
  1148. return json_fail($e->getMessage());
  1149. } catch (\Exception $e) {
  1150. Db::rollBack();
  1151. dump($e->getTrace());
  1152. return json_fail('数据写入失败~');
  1153. }
  1154. _syslog("添加套餐", "商品名【" . $params['goods_name'] . "】");
  1155. return json_success('success');
  1156. }
  1157. public static function update($params)
  1158. {
  1159. Db::beginTransaction();
  1160. try {
  1161. // 主表
  1162. self::mainUpdate($params);
  1163. // 商品详情表
  1164. self::detailUpdate($params);
  1165. // 商品标签表
  1166. self::labelUpdate($params);
  1167. // 产品运行控制信息表
  1168. self::goodsRunningUpdate($params);
  1169. // sku表
  1170. self::goodsSkuSet($params, 'update');
  1171. Db::commit();
  1172. } catch (BusinessException $e) {
  1173. Db::rollBack();
  1174. return json_fail($e->getMessage());
  1175. } catch (\Exception $e) {
  1176. Db::rollBack();
  1177. return json_fail('数据更新失败~');
  1178. }
  1179. _syslog("编辑商品", "商品名【" . $params['goods_name'] . "】" ?? "商品ID:【" . $params['goods_id'] . "】");
  1180. return json_success('success');
  1181. }
  1182. public static function changeStatus($params)
  1183. {
  1184. try {
  1185. Goods::where('goods_id', $params['goods_id'])->update(['goods_status' => $params['goods_status']]);
  1186. return json_success('修改成功');
  1187. } catch (\Exception $e) {
  1188. return json_fail('修改状态失败');
  1189. }
  1190. }
  1191. public static function updatePackage($params)
  1192. {
  1193. Db::beginTransaction();
  1194. try {
  1195. // 主表
  1196. self::mainUpdate($params);
  1197. // 商品详情表
  1198. self::detailUpdate($params);
  1199. // 套包组件表
  1200. self::componentUpdate($params, 'update');
  1201. // 商品标签表
  1202. self::labelUpdate($params);
  1203. // 产品运行控制信息表
  1204. self::goodsRunningUpdate($params);
  1205. Db::commit();
  1206. } catch (BusinessException $e) {
  1207. Db::rollBack();
  1208. return json_fail($e->getMessage());
  1209. } catch (\Exception $e) {
  1210. Db::rollBack();
  1211. dump($e->getTrace());
  1212. return json_fail('数据更新失败~');
  1213. }
  1214. _syslog("编辑套餐", "商品名【" . $params['goods_name'] . "】" ?? "商品ID:【" . $params['goods_id'] . "】");
  1215. return json_success('success');
  1216. }
  1217. /**
  1218. * @Desc 删除商品
  1219. * @Author Gorden
  1220. * @Date 2024/3/28 13:20
  1221. *
  1222. * @param $ids
  1223. * @return Response
  1224. */
  1225. public static function delete($ids)
  1226. {
  1227. if (!$ids) {
  1228. return json_fail("数据错误~");
  1229. }
  1230. if (!is_array($ids)) {
  1231. $ids = [$ids];
  1232. }
  1233. $goods = Goods::whereIn('goods_id', $ids)->get()->toArray();
  1234. if (!$goods) {
  1235. return json_fail("数据错误~");
  1236. }
  1237. // 是否在套包里
  1238. if (GoodsComponent::whereIn('join_component_goods_id', $ids)->exists()) {
  1239. return json_fail("当前商品存在于套包中,请先在套包中删除");
  1240. }
  1241. // 是否已被购买过
  1242. if (OrderSheet::whereIn('join_sheet_goods_id', $ids)->exists()) {
  1243. return json_fail("当前商品已有购买历史,如不在APP显示,请选择【下架】操作");
  1244. }
  1245. // 是否预约过
  1246. if (Appointment::whereIn('join_appointment_goods_id', $ids)->exists()) {
  1247. return json_fail("当前商品已有预约历史,如不在APP显示,请选择【下架】操作");
  1248. }
  1249. Db::beginTransaction();
  1250. try {
  1251. Goods::whereIn('goods_id', $ids)->delete();
  1252. GoodsDetail::whereIn('join_detail_goods_id', $ids)->delete();
  1253. GoodsLabel::whereIn('join_label_goods_id', $ids)->delete();
  1254. GoodsRunning::whereIn('join_running_goods_id', $ids)->delete();
  1255. GoodsSku::whereIn('join_sku_goods_id', $ids)->delete();
  1256. Db::commit();
  1257. _syslog("删除商品 / 套餐", "ID:【" . implode(',', $ids) . "】", $goods);
  1258. return json_success("商品删除成功");
  1259. } catch (\Exception $e) {
  1260. Db::rollBack();
  1261. return json_fail("商品删除失败~");
  1262. }
  1263. }
  1264. /**
  1265. * @Desc 商品主表
  1266. * @Author Gorden
  1267. * @Date 2024/3/11 11:20
  1268. *
  1269. * @param $params
  1270. * @return mixed|string
  1271. * @throws BusinessException
  1272. */
  1273. public static function mainInsert($params)
  1274. {
  1275. if (!empty($params['goods_cover'])) {
  1276. $params['goods_cover'] = str_replace(getenv('STORAGE_DOMAIN'), '', $params['goods_cover']);
  1277. }
  1278. // 如果产品是待处理状态
  1279. if ($params['goods_status'] == 'PENDING') {
  1280. if (strtotime($params['goods_on_addtimes']) <= time()) {
  1281. $params['goods_status'] = 'ON';
  1282. }
  1283. }
  1284. $category = SysCategory::where('category_id', $params['join_goods_category_id'])->first();
  1285. if (!$category) {
  1286. throw new BusinessException("产品分类不存在~");
  1287. }
  1288. if (empty($params['goods_category'])) {
  1289. $params['goods_category'] = $category->category_classify;
  1290. }
  1291. try {
  1292. $model = new Goods();
  1293. $model->goods_id = $params['goods_id'];
  1294. $model->join_goods_category_id = $params['join_goods_category_id'] ?? 0;
  1295. $model->join_goods_supplier_id = $params['join_goods_supplier_id'] ?? 0;
  1296. $model->goods_classify = $params['goods_classify'] ?? '';
  1297. $model->goods_status = $params['goods_status'] ?? '';
  1298. $model->goods_category = $params['goods_category'] ?? '';
  1299. // $model->goods_prefix = $params['goods_prefix'] ?? 】($category->category_name ? '【' . $category->category_name . '' : '');
  1300. $model->goods_prefix = $params['goods_prefix'] ?? '';
  1301. $model->goods_name = $params['goods_name'];
  1302. $model->goods_market_price = $params['goods_market_price'] ?? 0;
  1303. $model->goods_sales_price = $params['goods_sales_price'] ?? 0;
  1304. $model->goods_sku_json = !empty($params['goods_sku_json_label']) ? json_encode($params['goods_sku_json_label']) : json_encode(['规格' => ['标准']]);
  1305. $model->goods_attribute_json = !empty($params['goods_attribute_json']) ? $params['goods_attribute_json'] : '[]';
  1306. $model->goods_title = $params['goods_title'] ?? '';
  1307. $model->goods_cover = $params['goods_cover'] ?? '';
  1308. $model->goods_on_addtimes = isset($params['goods_on_addtimes']) ? strtotime($params['goods_on_addtimes']) : null;
  1309. $model->goods_sort = $params['goods_sort'] ?? null;
  1310. $model->goods_groupby = $params['goods_groupby'] ?? '';
  1311. $model->goods_remark = $params['goods_remark'] ?? '';
  1312. $model->goods_extend_json = $params['goods_extend_json'] ?? '{}';
  1313. $model->is_support_appointment = $params['is_support_appointment'] ?? 'N';
  1314. $model->creator_user_id = JwtToken::getCurrentId();
  1315. $model->goods_addtimes = time();
  1316. $model->goods_updatetimes = time();
  1317. // {"express":"Y","self":"Y","arrival":"Y"}
  1318. $expressJson = [];
  1319. if (!empty($params['express_json'])) {
  1320. if (in_array('express', $params['express_json'])) {
  1321. $expressJson['express'] = 'Y';
  1322. } else {
  1323. $expressJson['express'] = 'N';
  1324. }
  1325. if (in_array('self', $params['express_json'])) {
  1326. $expressJson['self'] = 'Y';
  1327. } else {
  1328. $expressJson['self'] = 'N';
  1329. }
  1330. if (in_array('arrival', $params['express_json'])) {
  1331. $expressJson['arrival'] = 'Y';
  1332. } else {
  1333. $expressJson['arrival'] = 'N';
  1334. }
  1335. $model->goods_express_json = json_encode($expressJson);
  1336. }
  1337. if (!empty($params['is_support_appointment']) && $params['is_support_appointment'] == 'Y' && !empty($params['appointment_times']) && $params['goods_category'] != 'TRAVEL') {
  1338. $times = [];
  1339. $attributeJsonTimeArr = [];
  1340. $personTotal = 0;
  1341. foreach ($params['appointment_times'] as $time) {
  1342. $attributeJsonTimeArr[] = strtotime(date('Y-m-d ') . $time['appointmentTimeStart']);
  1343. $attributeJsonTimeArr[] = strtotime(date('Y-m-d ') . $time['appointmentTimeEnd']);
  1344. $personTotal += $time['person'];
  1345. $times[$time['appointmentTimeStart']] = [
  1346. 'person' => $time['person'],
  1347. 'duration' => $time['appointmentTimeStart'] . '-' . $time['appointmentTimeEnd']
  1348. ];
  1349. }
  1350. $attributeJsonTime = date('H:i', min($attributeJsonTimeArr)) . '至' . date('H:i', max($attributeJsonTimeArr));
  1351. $newDates = [];
  1352. foreach ($params['dates'] as $date) {
  1353. $key = self::$week[$date];
  1354. $newDates[$key] = $date;
  1355. }
  1356. ksort($newDates);
  1357. $currentDate = current($newDates);
  1358. $lastDate = end($newDates);
  1359. $attributeJsonDate = '';
  1360. if (self::$week[$lastDate] - self::$week[$currentDate] + 1 > count($newDates)) {
  1361. $attributeJsonDate = implode(',', $newDates);
  1362. } else if (self::$week[$lastDate] - self::$week[$currentDate] + 1 == count($newDates)) {
  1363. $attributeJsonDate = $currentDate . '至' . $lastDate;
  1364. }
  1365. // if (isset($params['work_time'])){
  1366. // $workTimeStart = date('H:i',strtotime($params['work_time'][0]));
  1367. // $workTimeEnd = date('H:i',strtotime($params['work_time'][1]));
  1368. // $attributeJsonTime = $workTimeStart.'至'.$workTimeEnd;
  1369. // }
  1370. $attributeJson = [
  1371. 'icon' => $model->goods_cover,
  1372. 'date' => $attributeJsonDate,
  1373. // 'time' => $attributeJsonTime,
  1374. 'dates' => $newDates ? array_values($newDates) : [],
  1375. 'times' => $times,
  1376. 'person' => $personTotal
  1377. ];
  1378. // if(isset($params['address'])){
  1379. // $attributeJson['address'] = $params['address'];
  1380. // }
  1381. // if (isset($params['min_count'])){
  1382. // $attributeJson['min-count'] = $params['min_count'];
  1383. // }else{
  1384. // $attributeJson['min-count'] = 1;
  1385. // }
  1386. // if (isset($params['teachers'])){
  1387. // $attributeJson['teachers'] = $params['teachers'];
  1388. // }
  1389. if (!empty($params['appointment_label'])) {
  1390. $attributeJson['label'] = $params['appointment_label'];
  1391. }
  1392. // if (!empty($params['address'])) {
  1393. // $attributeJson['address'] = $params['address'];
  1394. // }
  1395. // if (!empty($params['position'])){
  1396. // $attributeJson['position'] = $params['position'];
  1397. // }
  1398. // if (isset($params['goods_service_premises'])){
  1399. // $attributeJson['service_premises_id'] = $params['goods_service_premises'];
  1400. // }
  1401. $model->goods_attribute_json = json_encode($attributeJson, JSON_UNESCAPED_UNICODE);
  1402. // $times = [];
  1403. // foreach ($params['appointment_times'] as $time) {
  1404. // $times[$time['appointmentTimeStart']] = [
  1405. // 'person' => $time['person'],
  1406. // 'duration' => $time['appointmentTimeStart'] . '-' . $time['appointmentTimeEnd']
  1407. // ];
  1408. // }
  1409. // $model->goods_attribute_json = json_encode([
  1410. // 'icon' => '',
  1411. // 'dates' => $params['dates'] ?? [],
  1412. // 'times' => $times
  1413. // ]);
  1414. }
  1415. if (!empty($params['is_support_appointment']) && $params['is_support_appointment'] == 'Y' && !empty($params['appointment_times']) && $params['goods_category'] == 'TRAVEL') {
  1416. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  1417. $attributeJson = json_decode($model->goods_attribute_json, true);
  1418. } elseif (empty($model->goods_attribute_json)) {
  1419. $attributeJson = [];
  1420. }
  1421. $attributeJson['travel-day'] = $params['travel_day'];
  1422. $attributeJson['travel-begin'] = $params['travel_begin'];
  1423. $attributeJson['travel-night'] = $params['travel_night'];
  1424. $attributeJson['travel-trans'] = $params['travel_trans'];
  1425. $unixs = [];
  1426. foreach ($params['appointment_times'] as $times) {
  1427. $unix = strtotime($times['days']);
  1428. $unixs[$unix] = $times['person'];
  1429. }
  1430. ksort($unixs);
  1431. $months = [];
  1432. foreach ($unixs as $key => $unix) {
  1433. $month = date('Ym', $key);
  1434. $day = date('d', $key);
  1435. $months[$month][$day] = $unix;
  1436. }
  1437. $attributeJson['month'] = $months;
  1438. $model->goods_attribute_json = json_encode($attributeJson, JSON_UNESCAPED_UNICODE);
  1439. }
  1440. if (!empty($params['goods_json']) && $params['join_goods_category_id'] == 65) {
  1441. $goodsJson = json_decode($params['goods_json'], true);
  1442. // foreach ($goodsJson as $key => $item) {
  1443. // $goodsJson[$key]['color'] = hexToRgb($item['color']);
  1444. // }
  1445. //
  1446. $model->goods_json = json_encode($goodsJson);
  1447. } elseif (!empty($params['goods_json']) && $params['join_goods_category_id'] == 43) {
  1448. $goodsJson = json_decode($params['goods_json'], true);
  1449. $newGoodsJson = [];
  1450. foreach ($goodsJson as $item1) {
  1451. if (empty($item1['title'])) {
  1452. continue;
  1453. }
  1454. $newItem1 = [];
  1455. foreach ($item1['items'] as $item2) {
  1456. $newParams = [];
  1457. foreach ($item2['params'] as $param) {
  1458. if (!empty($param[0]) || !empty($param[1])) {
  1459. $newParams[] = $param;
  1460. }
  1461. }
  1462. $newItem1['items'][$item2['key']] = $newParams;
  1463. }
  1464. $newItem1['service'] = $item1['service'] ?? '';
  1465. $newGoodsJson[$item1['title']] = $newItem1;
  1466. }
  1467. $model->goods_json = json_encode($newGoodsJson);
  1468. } else {
  1469. $model->goods_json = '[]';
  1470. }
  1471. if (!empty($params['goods_premisses'])) {
  1472. $attributeJson = [];
  1473. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  1474. $attributeJson = json_decode($model->goods_attribute_json, true);
  1475. } elseif (empty($model->goods_attribute_json)) {
  1476. $attributeJson = [];
  1477. }
  1478. $attributeJson['premisses'] = $params['goods_premisses'];
  1479. $model->goods_attribute_json = json_encode($attributeJson);
  1480. }
  1481. if (!empty($params['goods_theme_color']) && !empty($params['goods_theme_icon'])) {
  1482. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  1483. $attributeJson = json_decode($model->goods_attribute_json, true);
  1484. } elseif (empty($model->goods_attribute_json)) {
  1485. $attributeJson = [];
  1486. }
  1487. $attributeJson['bg'] = $params['goods_theme_color'];
  1488. $attributeJson['icon'] = str_replace(getenv('STORAGE_DOMAIN'), '', $params['goods_theme_icon']);
  1489. $model->goods_attribute_json = json_encode($attributeJson);
  1490. }
  1491. if (!empty($params['address'])) {
  1492. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  1493. $attributeJson = json_decode($model->goods_attribute_json, true);
  1494. } elseif (empty($model->goods_attribute_json)) {
  1495. $attributeJson = [];
  1496. }
  1497. $attributeJson['address'] = $params['address'];
  1498. $model->goods_attribute_json = json_encode($attributeJson);
  1499. }
  1500. if (!empty($params['position'])) {
  1501. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  1502. $attributeJson = json_decode($model->goods_attribute_json, true);
  1503. } elseif (empty($model->goods_attribute_json)) {
  1504. $attributeJson = [];
  1505. }
  1506. $attributeJson['position'] = $params['position'];
  1507. $model->goods_attribute_json = json_encode($attributeJson);
  1508. }
  1509. if (isset($params['goods_service_premises'])) {
  1510. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  1511. $attributeJson = json_decode($model->goods_attribute_json, true);
  1512. } elseif (empty($model->goods_attribute_json)) {
  1513. $attributeJson = [];
  1514. }
  1515. $attributeJson['service_premises_id'] = $params['goods_service_premises'];
  1516. $model->goods_attribute_json = json_encode($attributeJson);
  1517. }
  1518. if (isset($params['min_count'])) {
  1519. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  1520. $attributeJson = json_decode($model->goods_attribute_json, true);
  1521. } elseif (empty($model->goods_attribute_json)) {
  1522. $attributeJson = [];
  1523. }
  1524. $attributeJson['min-count'] = $params['min_count'];
  1525. $model->goods_attribute_json = json_encode($attributeJson);
  1526. }
  1527. if (isset($params['teachers'])) {
  1528. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  1529. $attributeJson = json_decode($model->goods_attribute_json, true);
  1530. } elseif (empty($model->goods_attribute_json)) {
  1531. $attributeJson = [];
  1532. }
  1533. $attributeJson['teachers'] = $params['teachers'];
  1534. $model->goods_attribute_json = json_encode($attributeJson);
  1535. }
  1536. if (isset($params['work_time'])) {
  1537. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  1538. $attributeJson = json_decode($model->goods_attribute_json, true);
  1539. } elseif (empty($model->goods_attribute_json)) {
  1540. $attributeJson = [];
  1541. }
  1542. $workTimeStart = date('H:i', strtotime($params['work_time'][0]));
  1543. $workTimeEnd = date('H:i', strtotime($params['work_time'][1]));
  1544. $attributeJsonTime = $workTimeStart . '至' . $workTimeEnd;
  1545. $attributeJson['time'] = $attributeJsonTime;
  1546. $model->goods_attribute_json = json_encode($attributeJson);
  1547. }
  1548. if ($model->save()) {
  1549. return $model->goods_id;
  1550. }
  1551. // 异常
  1552. throw new BusinessException("数据写入失败~");
  1553. } catch (\Exception $e) {
  1554. dump($e->getMessage());
  1555. throw new BusinessException("数据写入失败~");
  1556. }
  1557. }
  1558. /**
  1559. * @Desc 详情表
  1560. * @Author Gorden
  1561. * @Date 2024/3/11 11:19
  1562. *
  1563. * @param $params
  1564. * @return void
  1565. * @throws BusinessException
  1566. */
  1567. public static function detailInsert($params)
  1568. {
  1569. if (!empty($params['goods_detail_slider_json'])) {
  1570. $params['goods_detail_slider_json'] = str_replace(getenv('STORAGE_DOMAIN'), '', $params['goods_detail_slider_json']);
  1571. $params['goods_detail_slider_json'] = json_encode(['slider' => $params['goods_detail_slider_json']]);
  1572. }
  1573. if (isset($params['curriculum'])) {
  1574. $params['goods_detail_specs_json'] = json_encode([
  1575. [
  1576. 'key' => '课时',
  1577. 'val' => $params['curriculum']['period'] ?? '',
  1578. ],
  1579. [
  1580. 'key' => '群体',
  1581. 'val' => $params['curriculum']['group'] ?? '',
  1582. ],
  1583. [
  1584. 'key' => '课程',
  1585. 'val' => $params['curriculum']['course'] ?? '',
  1586. ],
  1587. ]);
  1588. }
  1589. try {
  1590. $model = new GoodsDetail();
  1591. $model->join_detail_goods_id = $params['goods_id'];
  1592. $model->goods_detail_code_json = $params['goods_detail_code_json'] ?? '{}';
  1593. $model->goods_detail_slider_json = $params['goods_detail_slider_json'] ?? '{}';
  1594. $model->goods_detail_specs_json = $params['goods_detail_specs_json'] ?? '{}';
  1595. $model->goods_detail_content = $params['goods_detail_content'] ?? '';
  1596. if (!$model->save()) {
  1597. // 异常
  1598. throw new BusinessException("轮播图/详情数据写入失败~");
  1599. }
  1600. } catch (\Exception $e) {
  1601. dump($e->getMessage());
  1602. throw new BusinessException("轮播图/详情数据写入失败~");
  1603. }
  1604. }
  1605. public static function componentUpdate($params, $type = 'insert')
  1606. {
  1607. Db::beginTransaction();
  1608. try {
  1609. $goodsSku = '';
  1610. // 有先删除
  1611. if ($type == 'update') {
  1612. GoodsComponent::where('join_component_master_goods_id', $params['goods_id'])->delete();
  1613. $goodsSku = GoodsSku::where('join_sku_goods_id', $params['goods_id'])->first();
  1614. if ($goodsSku) {
  1615. $goodsSku->goods_sku_market_price = $params['goods_market_price'] ?? 0;
  1616. $goodsSku->goods_sku_sales_price = $params['goods_sales_price'] ?? 0;
  1617. $goodsSku->save();
  1618. }
  1619. }
  1620. if ($type == 'insert' || !$goodsSku) {
  1621. Goods::where('goods_id', $params['goods_id'])->update(['goods_sku_json' => '{"规格": ["标准"]}']);
  1622. $skuData = [
  1623. 'join_sku_goods_id' => $params['goods_id'],
  1624. 'goods_sku_status' => 'ON',
  1625. 'goods_sku_specs_json' => '{"规格": "标准"}',
  1626. 'goods_sku_title' => "标准" . $params['goods_name'],
  1627. 'goods_sku_market_price' => $params['goods_market_price'] ?? 0,
  1628. 'goods_sku_sales_price' => $params['goods_sales_price'] ?? 0,
  1629. ];
  1630. GoodsSku::insert($skuData);
  1631. }
  1632. $data = [];
  1633. foreach ($params['goods_content_list'] as $item) {
  1634. if (!in_array($item['goods_id'], $params['join_component_goods_id'])) {
  1635. continue;
  1636. }
  1637. $goods = Goods::where('goods_id', $params['goods_id'])->first();
  1638. if (!$goods) {
  1639. continue;
  1640. }
  1641. $data[] = [
  1642. 'join_component_master_goods_id' => $params['goods_id'],
  1643. 'join_component_goods_id' => $item['goods_id'] ?? '',
  1644. 'join_component_goods_sku_id' => $item['sku_id'] ?? '',
  1645. 'goods_component_price' => $item['goods_sales_price'] ?? '',
  1646. 'goods_component_cover' => $goods->goods_cover,
  1647. 'goods_component_json' => json_encode(['goods_name' => $item['goods_name'], 'nbr' => $item['nbr'], 'sku_id' => $item['sku_id'] ?? '']),
  1648. 'goods_component_addtimes' => time()
  1649. ];
  1650. }
  1651. if ($data) {
  1652. GoodsComponent::insert($data);
  1653. }
  1654. Db::commit();
  1655. } catch (\Exception $e) {
  1656. Db::rollBack();
  1657. dump($e->getMessage());
  1658. throw new BusinessException("数据写入失败~");
  1659. }
  1660. }
  1661. /**
  1662. * @Desc 标签表
  1663. * @Author Gorden
  1664. * @Date 2024/3/11 11:32
  1665. *
  1666. * @param $params
  1667. * @return void
  1668. * @throws BusinessException
  1669. */
  1670. public static function labelInsert($params)
  1671. {
  1672. $model = new GoodsLabel();
  1673. $model->join_label_goods_id = $params['goods_id'];
  1674. $model->goods_label = $params['goods_label'] ? implode(',', $params['goods_label']) : '';
  1675. $model->goods_label_extend_json = !empty($params['goods_label_extend_json']) ? $params['goods_label_extend_json'] : '{}';
  1676. if (!$model->save()) {
  1677. // 异常
  1678. throw new BusinessException('数据写入失败~');
  1679. }
  1680. }
  1681. /**
  1682. * @Desc 产品运行控制信息表
  1683. * @Author Gorden
  1684. * @Date 2024/3/11 11:38
  1685. *
  1686. * @param $params
  1687. * @return void
  1688. * @throws BusinessException
  1689. */
  1690. public static function goodsRunningInsert($params)
  1691. {
  1692. try {
  1693. $model = new GoodsRunning();
  1694. $model->join_running_goods_id = $params['goods_id'];
  1695. $model->goods_running_storage = $params['goods_running_storage'] ?? 0;
  1696. $model->goods_running_sale = $params['goods_running_sale'] ?? 0;
  1697. $model->goods_running_off_type = !empty($params['goods_running_off_type']) ? $params['goods_running_off_type'] : '';
  1698. $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'])]) : '[]';
  1699. if (!$model->save()) {
  1700. throw new BusinessException('数据写入失败');
  1701. }
  1702. } catch (\Exception $e) {
  1703. dump($e->getMessage());
  1704. throw new BusinessException('数据写入失败');
  1705. }
  1706. }
  1707. /**
  1708. * @Desc SKU
  1709. * @Author Gorden
  1710. * @Date 2024/3/11 12:01
  1711. *
  1712. * @param $params
  1713. * @return void
  1714. * @throws BusinessException
  1715. */
  1716. public static function skuInsert($params)
  1717. {
  1718. $model = new GoodsSku();
  1719. $model->join_sku_goods_id = $params['goods_id'];
  1720. $model->goods_sku_status = $params['goods_sku_status'];
  1721. $model->goods_sku_specs_json = $params['goods_sku_specs_json'];
  1722. $model->goods_sku_title = $params['goods_sku_title'];
  1723. $model->goods_sku_images_json = $params['goods_sku_images_json'];
  1724. $model->goods_sku_content = $params['goods_sku_content'];
  1725. $model->goods_sku_market_price = $params['goods_sku_market_price'];
  1726. $model->goods_sku_sales_price = $params['goods_sku_sales_price'];
  1727. $model->goods_sku_storage_json = $params['goods_sku_storage_json'];
  1728. $model->goods_sku_service_json = $params['goods_sku_service_json'];
  1729. $model->goods_sku_extend_json = $params['goods_sku_extend_json'];
  1730. if (!$model->save()) {
  1731. throw new BusinessException('规格数据写入失败~');
  1732. }
  1733. }
  1734. /**
  1735. * @Desc
  1736. * @Author Gorden
  1737. * @Date 2024/3/12 8:44
  1738. *
  1739. * @param $params
  1740. * @return void
  1741. * @throws BusinessException
  1742. */
  1743. public static function mainUpdate($params)
  1744. {
  1745. try {
  1746. $data = self::inputFilter($params, new Goods());
  1747. if (!empty($data['goods_cover'])) {
  1748. $data['goods_cover'] = str_replace(getenv('STORAGE_DOMAIN'), '', $data['goods_cover']);
  1749. }
  1750. $data['goods_on_addtimes'] = isset($data['goods_on_addtimes']) ? strtotime($data['goods_on_addtimes']) : 0;
  1751. $data['goods_sku_json'] = !empty($params['goods_sku_json_label']) ? json_encode($params['goods_sku_json_label']) : json_encode(['规格' => ['标准']]);
  1752. $row = Goods::find($data['goods_id']);
  1753. if ($row->join_goods_category_id != $data['join_goods_category_id']) {
  1754. $category = SysCategory::where('category_id', $params['join_goods_category_id'])->first();
  1755. if (!$category) {
  1756. throw new BusinessException("产品分类不存在~");
  1757. }
  1758. $data['goods_category'] = $category->category_classify ?? '';
  1759. $data['goods_prefix'] = $data['goods_prefix'] ?? ($category->category_name ? '【' . $category->category_name . '】' : '');
  1760. }
  1761. if (!empty($params['goods_recommend_index'])) {
  1762. $data['goods_category'] = $params['goods_recommend_index'];
  1763. }
  1764. // 上架时间有变动
  1765. if ($data['goods_status'] == 'PENDING' && $row->goods_on_addtimes != $data['goods_on_addtimes']) {
  1766. $redis = Redis::connection();
  1767. // 删掉原来的
  1768. $oldKey = Goods::LISTING_KEY_PREFIX . date('YmdHi', $row->goods_on_addtimes);
  1769. $redis->srem($oldKey, $data['goods_id']);
  1770. // 加入新的
  1771. $newKey = Goods::LISTING_KEY_PREFIX . date('YmdHi', $data['goods_on_addtimes']);
  1772. $redis->sadd($newKey, $data['goods_id']);
  1773. }
  1774. $expressJson = [];
  1775. if (!empty($params['express_json'])) {
  1776. if (in_array('express', $params['express_json'])) {
  1777. $expressJson['express'] = 'Y';
  1778. } else {
  1779. $expressJson['express'] = 'N';
  1780. }
  1781. if (in_array('self', $params['express_json'])) {
  1782. $expressJson['self'] = 'Y';
  1783. } else {
  1784. $expressJson['self'] = 'N';
  1785. }
  1786. if (in_array('arrival', $params['express_json'])) {
  1787. $expressJson['arrival'] = 'Y';
  1788. } else {
  1789. $expressJson['arrival'] = 'N';
  1790. }
  1791. $data['goods_express_json'] = json_encode($expressJson);
  1792. }
  1793. if (!empty($params['is_support_appointment']) && $params['is_support_appointment'] == 'Y' && !empty($params['appointment_times']) && $params['goods_category'] != 'TRAVEL') {
  1794. $attributeJson = [];
  1795. if (!empty($row->goods_attribute_json)) {
  1796. $attributeJson = json_decode($row->goods_attribute_json, true);
  1797. }
  1798. $times = [];
  1799. $attributeJsonTimeArr = [];
  1800. $personTotal = 0;
  1801. foreach ($params['appointment_times'] as $time) {
  1802. $attributeJsonTimeArr[] = strtotime(date('Y-m-d ') . $time['appointmentTimeStart']);
  1803. $attributeJsonTimeArr[] = strtotime(date('Y-m-d ') . $time['appointmentTimeEnd']);
  1804. $personTotal += $time['person'];
  1805. $times[$time['appointmentTimeStart']] = [
  1806. 'person' => $time['person'],
  1807. 'duration' => $time['appointmentTimeStart'] . '-' . $time['appointmentTimeEnd']
  1808. ];
  1809. }
  1810. $attributeJsonTime = date('H:i', min($attributeJsonTimeArr)) . '至' . date('H:i', max($attributeJsonTimeArr));
  1811. $newDates = [];
  1812. foreach ($params['dates'] as $date) {
  1813. $key = self::$week[$date];
  1814. $newDates[$key] = $date;
  1815. }
  1816. ksort($newDates);
  1817. $currentDate = current($newDates);
  1818. $lastDate = end($newDates);
  1819. $attributeJsonDate = '';
  1820. if (self::$week[$lastDate] - self::$week[$currentDate] + 1 > count($newDates)) {
  1821. $attributeJsonDate = implode(',', $newDates);
  1822. } else if (self::$week[$lastDate] - self::$week[$currentDate] + 1 == count($newDates)) {
  1823. $attributeJsonDate = $currentDate . '至' . $lastDate;
  1824. }
  1825. $attributeJson['date'] = $attributeJsonDate;
  1826. $attributeJson['icon'] = $data['goods_cover'] ?? '';
  1827. $attributeJson['time'] = $attributeJsonTime;
  1828. $attributeJson['dates'] = $newDates ? array_values($newDates) : [];
  1829. $attributeJson['times'] = $times;
  1830. $attributeJson['person'] = $personTotal;
  1831. if (!empty($params['appointment_label'])) {
  1832. $attributeJson['label'] = $params['appointment_label'];
  1833. }
  1834. // if (!empty($params['address'])) {
  1835. // $attributeJson['address'] = $params['address'];
  1836. // }
  1837. // if (!empty($params['position'])){
  1838. // $attributeJson['position'] = $params['position'];
  1839. // }
  1840. // if (isset($params['goods_service_premises'])){
  1841. // $attributeJson['service_premises_id'] = $params['goods_service_premises'];
  1842. // }
  1843. // if (isset($params['work_time'])){
  1844. // $workTimeStart = date('H:i',strtotime($params['work_time'][0]));
  1845. // $workTimeEnd = date('H:i',strtotime($params['work_time'][1]));
  1846. // $attributeJson['time'] = $workTimeStart.'至'.$workTimeEnd;
  1847. // }
  1848. // if (isset($params['min_count'])){
  1849. // $attributeJson['min-count'] = $params['min_count'];
  1850. // }else{
  1851. // $attributeJson['min-count'] = 1;
  1852. // }
  1853. // if (isset($params['teachers'])){
  1854. // $attributeJson['teachers'] = $params['teachers'];
  1855. // }
  1856. $data['goods_attribute_json'] = json_encode($attributeJson, JSON_UNESCAPED_UNICODE);
  1857. }
  1858. if (!empty($params['is_support_appointment']) && $params['is_support_appointment'] == 'Y' && !empty($params['appointment_times']) && $params['goods_category'] == 'TRAVEL') {
  1859. $attributeJson = [];
  1860. if (!empty($row->goods_attribute_json)) {
  1861. $attributeJson = json_decode($row->goods_attribute_json, true);
  1862. }
  1863. $attributeJson['travel-day'] = $params['travel_day'];
  1864. $attributeJson['travel-begin'] = $params['travel_begin'];
  1865. $attributeJson['travel-night'] = $params['travel_night'];
  1866. $attributeJson['travel-trans'] = $params['travel_trans'];
  1867. $unixs = [];
  1868. foreach ($params['appointment_times'] as $times) {
  1869. $unix = strtotime($times['days']);
  1870. $unixs[$unix] = $times['person'];
  1871. }
  1872. ksort($unixs);
  1873. $months = [];
  1874. foreach ($unixs as $key => $unix) {
  1875. $month = date('Ym', $key);
  1876. $day = date('d', $key);
  1877. $months[$month][$day] = $unix;
  1878. }
  1879. $attributeJson['month'] = $months;
  1880. $data['goods_attribute_json'] = json_encode($attributeJson, JSON_UNESCAPED_UNICODE);
  1881. }
  1882. if (!empty($params['goods_premisses'])) {
  1883. if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
  1884. if (is_json($data['goods_attribute_json'])) {
  1885. $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
  1886. } else {
  1887. $data['goods_attribute_json'] = [];
  1888. }
  1889. } elseif (empty($data['goods_attribute_json'])) {
  1890. $data['goods_attribute_json'] = [];
  1891. }
  1892. $data['goods_attribute_json']['premisses'] = $params['goods_premisses'];
  1893. $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
  1894. }
  1895. if (!empty($data['goods_json']) && $data['join_goods_category_id'] == 65) {
  1896. // $goodsJson = json_decode($data['goods_json'], true);
  1897. // foreach ($goodsJson as $key => $item) {
  1898. // $goodsJson[$key]['color'] = hexToRgb($item['color']);
  1899. // }
  1900. //
  1901. // $data['goods_json'] = json_encode($goodsJson);
  1902. } elseif (!empty($data['goods_json']) && $data['join_goods_category_id'] == 43) {
  1903. $goodsJson = json_decode($data['goods_json'], true);
  1904. $newGoodsJson = [];
  1905. foreach ($goodsJson as $item1) {
  1906. if (empty($item1['title'])) {
  1907. continue;
  1908. }
  1909. $newItem1 = [];
  1910. foreach ($item1['items'] as $item2) {
  1911. $newParams = [];
  1912. foreach ($item2['params'] as $param) {
  1913. if (!empty($param[0]) || !empty($param[1])) {
  1914. $newParams[] = $param;
  1915. }
  1916. }
  1917. $newItem1['items'][$item2['key']] = $newParams;
  1918. }
  1919. $newItem1['service'] = $item1['service'];
  1920. $newGoodsJson[$item1['title']] = $newItem1;
  1921. }
  1922. $data['goods_json'] = json_encode($newGoodsJson);
  1923. } else {
  1924. $data['goods_json'] = '[]';
  1925. }
  1926. if (!empty($params['goods_theme_color']) && !empty($params['goods_theme_icon'])) {
  1927. if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
  1928. if (is_json($data['goods_attribute_json'])) {
  1929. $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
  1930. } else {
  1931. $data['goods_attribute_json'] = [];
  1932. }
  1933. } elseif (empty($data['goods_attribute_json'])) {
  1934. $data['goods_attribute_json'] = [];
  1935. }
  1936. $data['goods_attribute_json']['bg'] = $params['goods_theme_color'];
  1937. $data['goods_attribute_json']['icon'] = str_replace(getenv('STORAGE_DOMAIN'), '', $params['goods_theme_icon']);
  1938. $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
  1939. }
  1940. if (!empty($params['address'])) {
  1941. if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
  1942. if (is_json($data['goods_attribute_json'])) {
  1943. $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
  1944. } else {
  1945. $data['goods_attribute_json'] = [];
  1946. }
  1947. } elseif (empty($data['goods_attribute_json'])) {
  1948. $data['goods_attribute_json'] = [];
  1949. }
  1950. $data['goods_attribute_json']['address'] = $params['address'];
  1951. $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
  1952. }
  1953. if (!empty($params['position'])) {
  1954. if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
  1955. if (is_json($data['goods_attribute_json'])) {
  1956. $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
  1957. } else {
  1958. $data['goods_attribute_json'] = [];
  1959. }
  1960. } elseif (empty($data['goods_attribute_json'])) {
  1961. $data['goods_attribute_json'] = [];
  1962. }
  1963. $data['goods_attribute_json']['position'] = $params['position'];
  1964. $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
  1965. }
  1966. if (isset($params['goods_service_premises'])) {
  1967. if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
  1968. if (is_json($data['goods_attribute_json'])) {
  1969. $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
  1970. } else {
  1971. $data['goods_attribute_json'] = [];
  1972. }
  1973. } elseif (empty($data['goods_attribute_json'])) {
  1974. $data['goods_attribute_json'] = [];
  1975. }
  1976. $data['goods_attribute_json']['service_premises_id'] = $params['goods_service_premises'];
  1977. $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
  1978. }
  1979. if (isset($params['work_time'])) {
  1980. if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
  1981. if (is_json($data['goods_attribute_json'])) {
  1982. $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
  1983. } else {
  1984. $data['goods_attribute_json'] = [];
  1985. }
  1986. } elseif (empty($data['goods_attribute_json'])) {
  1987. $data['goods_attribute_json'] = [];
  1988. }
  1989. $workTimeStart = date('H:i', strtotime($params['work_time'][0]));
  1990. $workTimeEnd = date('H:i', strtotime($params['work_time'][1]));
  1991. $data['goods_attribute_json']['time'] = $workTimeStart . '至' . $workTimeEnd;
  1992. $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
  1993. }
  1994. if (isset($params['min_count'])) {
  1995. if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
  1996. if (is_json($data['goods_attribute_json'])) {
  1997. $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
  1998. } else {
  1999. $data['goods_attribute_json'] = [];
  2000. }
  2001. } elseif (empty($data['goods_attribute_json'])) {
  2002. $data['goods_attribute_json'] = [];
  2003. }
  2004. $data['goods_attribute_json']['min-count'] = $params['min_count'];
  2005. $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
  2006. }
  2007. if (isset($params['teachers'])) {
  2008. if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
  2009. if (is_json($data['goods_attribute_json'])) {
  2010. $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
  2011. } else {
  2012. $data['goods_attribute_json'] = [];
  2013. }
  2014. } elseif (empty($data['goods_attribute_json'])) {
  2015. $data['goods_attribute_json'] = [];
  2016. }
  2017. $data['goods_attribute_json']['teachers'] = $params['teachers'];
  2018. $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
  2019. }
  2020. foreach ($data as $key => $val) {
  2021. $row->{$key} = $val;
  2022. }
  2023. $row->goods_updatetimes = time();
  2024. $row->save();
  2025. } catch (BusinessException $e) {
  2026. throw new BusinessException($e->getMessage());
  2027. } catch (\Exception $e) {
  2028. dump($e->getTrace());
  2029. throw new BusinessException('数据更新异常~1');
  2030. }
  2031. }
  2032. /**
  2033. * @Desc
  2034. * @Author Gorden
  2035. * @Date 2024/3/12 9:57
  2036. *
  2037. * @param $params
  2038. * @return void
  2039. * @throws BusinessException
  2040. */
  2041. public static function detailUpdate($params)
  2042. {
  2043. try {
  2044. $data = self::inputFilter($params, new GoodsDetail());
  2045. if (!empty($data['goods_detail_slider_json'])) {
  2046. $data['goods_detail_slider_json'] = str_replace(getenv('STORAGE_DOMAIN'), '', $data['goods_detail_slider_json']);
  2047. $data['goods_detail_slider_json'] = json_encode(['slider' => $data['goods_detail_slider_json']]);
  2048. }
  2049. if (isset($params['curriculum'])) {
  2050. $data['goods_detail_specs_json'] = json_encode([
  2051. [
  2052. 'key' => '课时',
  2053. 'val' => $params['curriculum']['period'] ?? '',
  2054. ],
  2055. [
  2056. 'key' => '群体',
  2057. 'val' => $params['curriculum']['group'] ?? '',
  2058. ],
  2059. [
  2060. 'key' => '课程',
  2061. 'val' => $params['curriculum']['course'] ?? '',
  2062. ],
  2063. ]);
  2064. }
  2065. // 根据goods_id 查详情ID
  2066. $detail = GoodsDetail::where('join_detail_goods_id', $params['goods_id'])->first();
  2067. if ($detail) {
  2068. self::doUpdate($detail->join_detail_goods_id, $data, new GoodsDetail());
  2069. } else {
  2070. $data['join_detail_goods_id'] = $params['goods_id'];
  2071. GoodsDetail::insert($data);
  2072. }
  2073. } catch (BusinessException $e) {
  2074. throw new BusinessException($e->getMessage());
  2075. } catch (\Exception $e) {
  2076. dump($e->getMessage());
  2077. throw new BusinessException('轮播图/详情数据更新异常~');
  2078. }
  2079. }
  2080. /**
  2081. * @Desc
  2082. * @Author Gorden
  2083. * @Date 2024/3/12 9:58
  2084. *
  2085. * @param $params
  2086. * @return void
  2087. * @throws BusinessException
  2088. */
  2089. public static function labelUpdate($params)
  2090. {
  2091. try {
  2092. $data = self::inputFilter($params, new GoodsLabel());
  2093. // 根据goods_id 查详情ID
  2094. $detail = GoodsLabel::where('join_label_goods_id', $params['goods_id'])->first();
  2095. if ($detail) {
  2096. self::doUpdate($detail->goods_label_id, $data, new GoodsLabel());
  2097. } else {
  2098. $data['join_label_goods_id'] = $params['goods_id'];
  2099. GoodsLabel::insert($data);
  2100. }
  2101. } catch (BusinessException $e) {
  2102. throw new BusinessException($e->getMessage());
  2103. } catch (\Exception $e) {
  2104. dump($e->getMessage());
  2105. throw new BusinessException('数据更新异常~3');
  2106. }
  2107. }
  2108. /**
  2109. * @Desc
  2110. * @Author Gorden
  2111. * @Date 2024/3/12 9:59
  2112. *
  2113. * @param $params
  2114. * @return void
  2115. * @throws BusinessException
  2116. */
  2117. public static function goodsRunningUpdate($params)
  2118. {
  2119. try {
  2120. $data = self::inputFilter($params, new GoodsRunning());
  2121. // 根据goods_id 查详情ID
  2122. $detail = GoodsRunning::where('join_running_goods_id', $params['goods_id'])->first();
  2123. if (!empty($params['goods_running_off_type']) && $params['goods_running_off_type'] == 'T') {
  2124. $redis = Redis::connection();
  2125. if (!empty($detail->goods_running_off_json)) {
  2126. $goodsRunningOffJson = json_decode($detail->goods_running_off_json, true);
  2127. if (isset($goodsRunningOffJson['time'])) {
  2128. $oldKey = Goods::LISTING_OFF_KEY_PREFIX . date('YmdHi', $goodsRunningOffJson['time']);
  2129. $goodsRunningOffJson['time'] = strtotime($params['goods_off_addtimes']);
  2130. $data['goods_running_off_json'] = json_encode($goodsRunningOffJson);
  2131. // 有老的下架时间,删除老的
  2132. $redis->srem($oldKey, $params['goods_id']);
  2133. } else {
  2134. $goodsRunningOffJson['time'] = strtotime($params['goods_off_addtimes']);
  2135. $data['goods_running_off_json'] = json_encode($goodsRunningOffJson);
  2136. }
  2137. } else {
  2138. $data['goods_running_off_json'] = json_encode(['time' => strtotime($params['goods_off_addtimes'])]);
  2139. }
  2140. // 加入自动下架
  2141. $newKey = Goods::LISTING_OFF_KEY_PREFIX . date('YmdHi', strtotime($params['goods_off_addtimes']));
  2142. $redis->sAdd($newKey, $params['goods_id']);
  2143. } 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') {
  2144. $goodsRunningOffJson = json_decode($detail->goods_running_off_json, true);
  2145. if (isset($goodsRunningOffJson['time'])) {
  2146. $oldKey = Goods::LISTING_OFF_KEY_PREFIX . date('YmdHi', $goodsRunningOffJson['time']);
  2147. $redis = Redis::connection();
  2148. $redis->srem($oldKey, $params['goods_id']);
  2149. }
  2150. }
  2151. if ($detail) {
  2152. self::doUpdate($detail->join_running_goods_id, $data, new GoodsRunning());
  2153. } else {
  2154. // 兼容老数据……
  2155. $data['join_running_goods_id'] = $params['goods_id'];
  2156. GoodsRunning::insert($data);
  2157. }
  2158. } catch (BusinessException $e) {
  2159. throw new BusinessException($e->getMessage());
  2160. } catch (\Exception $e) {
  2161. dump($e->getMessage());
  2162. throw new BusinessException('数据更新异常~');
  2163. }
  2164. }
  2165. /**
  2166. * @Desc sku 设置
  2167. * @Author Gorden
  2168. * @Date 2024/4/10 10:43
  2169. *
  2170. * @param $params
  2171. * @return void
  2172. * @throws BusinessException
  2173. */
  2174. public static function goodsSkuSet($params, $operation = 'insert')
  2175. {
  2176. try {
  2177. Db::beginTransaction();
  2178. $skusOldIds = [];
  2179. if ($operation == 'update') {
  2180. // 查出所有的
  2181. $skusOldIds = GoodsSku::where('join_sku_goods_id', $params['goods_id'])->pluck('goods_sku_id', 'goods_sku_id');
  2182. // 删掉原有的
  2183. // GoodsSku::where('join_sku_goods_id', $params['goods_id'])->delete();
  2184. }
  2185. if (empty($skusOldIds) && empty($params['goods_sku_json_value'])) {
  2186. $skuData = [
  2187. 'join_sku_goods_id' => $params['goods_id'],
  2188. 'goods_sku_status' => 'ON',
  2189. 'goods_sku_specs_json' => '{"规格": "标准"}',
  2190. 'goods_sku_title' => "标准" . $params['goods_name'],
  2191. 'goods_sku_market_price' => $params['goods_market_price'] ?? 0,
  2192. 'goods_sku_sales_price' => $params['goods_sales_price'] ?? 0,
  2193. ];
  2194. GoodsSku::insert($skuData);
  2195. }
  2196. // 入新的
  2197. if (!empty($params['goods_sku_json_value'])) {
  2198. foreach ($params['goods_sku_json_value'] as $item) {
  2199. $skus = explode(',', $item['sku']);
  2200. $skuArr = [];
  2201. for ($i = 1; $i <= count($skus); $i++) {
  2202. $skuName = "skuName" . $i;
  2203. $key = $item[$skuName];
  2204. $skuArr[$key] = $skus[$i - 1];
  2205. }
  2206. $specsJson = json_encode($skuArr);
  2207. $skuTitle = str_replace('-', ',', $item['sku']) . $params['goods_name'];
  2208. if ($operation == 'update' && !empty($item['sku_id'])) {
  2209. $model = GoodsSku::where('goods_sku_id', $item['sku_id'])->first();
  2210. if (!$model) {
  2211. $model = new GoodsSku();
  2212. } else {
  2213. unset($skusOldIds[$model->goods_sku_id]);
  2214. }
  2215. } else {
  2216. $model = GoodsSku::where('join_sku_goods_id', $params['goods_id'])->where('goods_sku_title', $skuTitle)->first();
  2217. if (!$model) {
  2218. $model = new GoodsSku();
  2219. } else {
  2220. unset($skusOldIds[$model->goods_sku_id]);
  2221. }
  2222. }
  2223. $model->join_sku_goods_id = $params['goods_id'];
  2224. $model->goods_sku_status = $params['goods_status'];
  2225. $model->goods_sku_specs_json = $specsJson;
  2226. $model->goods_sku_title = $skuTitle;
  2227. $model->goods_sku_market_price = $params['goods_market_price'] ?? 0;
  2228. $model->goods_sku_sales_price = $item['price'];
  2229. $model->goods_sku_storage_json = json_encode(['storage' => $item['stock']]);
  2230. $model->save();
  2231. }
  2232. }
  2233. if ($operation == 'update' && !empty($skusOldIds)) {
  2234. GoodsSku::whereIn('goods_sku_id', $skusOldIds)->delete();
  2235. }
  2236. Db::commit();
  2237. } catch (\Exception $e) {
  2238. dump($e->getTrace());
  2239. Db::rollBack();
  2240. throw new BusinessException('规格数据更新异常~');
  2241. }
  2242. }
  2243. /**
  2244. * @Desc
  2245. * @Author Gorden
  2246. * @Date 2024/3/12 8:45
  2247. *
  2248. * @param array $data
  2249. * @param $model
  2250. * @return array
  2251. * @throws BusinessException
  2252. */
  2253. private static function inputFilter(array $data, $model): array
  2254. {
  2255. $table = config('database.connections.mysql.prefix') . $model->getTable();
  2256. $allow_column = $model->getConnection()->select("desc `$table`");
  2257. if (!$allow_column) {
  2258. throw new BusinessException('表不存在', 2);
  2259. }
  2260. $columns = array_column($allow_column, 'Type', 'Field');
  2261. foreach ($data as $col => $item) {
  2262. if (!isset($columns[$col])) {
  2263. unset($data[$col]);
  2264. continue;
  2265. }
  2266. // 非字符串类型传空则为null
  2267. if ($item === '' && strpos(strtolower($columns[$col]), 'varchar') === false && strpos(strtolower($columns[$col]), 'text') === false) {
  2268. $data[$col] = null;
  2269. }
  2270. if (is_array($item)) {
  2271. $data[$col] = implode(',', $item);
  2272. }
  2273. if ($item != '' && (strpos(strtolower($columns[$col]), 'varchar') || strpos(strtolower($columns[$col]), 'text'))) {
  2274. // $data[$col] = htmlspecialchars($item);
  2275. }
  2276. }
  2277. if (empty($data['created_at'])) {
  2278. unset($data['created_at']);
  2279. }
  2280. if (empty($data['updated_at'])) {
  2281. unset($data['updated_at']);
  2282. }
  2283. return $data;
  2284. }
  2285. /**
  2286. * @Desc 执行更新
  2287. * @Author Gorden
  2288. * @Date 2024/3/12 8:43
  2289. *
  2290. * @param $id
  2291. * @param $data
  2292. * @param $model
  2293. * @return void
  2294. */
  2295. private static function doUpdate($id, $data, $model)
  2296. {
  2297. $row = $model->find($id);
  2298. foreach ($data as $key => $val) {
  2299. $row->{$key} = $val;
  2300. }
  2301. $row->save();
  2302. }
  2303. /**
  2304. * @Desc 上架定时
  2305. * @Author Gorden
  2306. * @Date 2024/4/11 15:13
  2307. *
  2308. * @return void
  2309. */
  2310. public static function checkListing()
  2311. {
  2312. $key = Goods::LISTING_KEY_PREFIX . date('YmdHi');
  2313. $redis = Redis::connection();
  2314. if (!$redis->exists($key)) {
  2315. return;
  2316. }
  2317. $goodsIds = $redis->sMembers($key);
  2318. if (Goods::whereIn('goods_id', $goodsIds)->update(['goods_status' => 'ON'])) {
  2319. $redis->del($key);
  2320. }
  2321. }
  2322. /**
  2323. * @Desc 自动下架
  2324. * @Author Gorden
  2325. * @Date 2024/4/26 15:26
  2326. *
  2327. * @return void
  2328. */
  2329. public static function checkOffListing()
  2330. {
  2331. $key = Goods::LISTING_OFF_KEY_PREFIX . date('YmdHi');
  2332. $redis = Redis::connection();
  2333. if (!$redis->exists($key)) {
  2334. return;
  2335. }
  2336. $goodsIds = $redis->sMembers($key);
  2337. if (Goods::whereIn('goods_id', $goodsIds)->update(['goods_status' => 'OFF'])) {
  2338. $redis->del($key);
  2339. }
  2340. }
  2341. public static $week = [
  2342. '周一' => 1,
  2343. '周二' => 2,
  2344. '周三' => 3,
  2345. '周四' => 4,
  2346. '周五' => 5,
  2347. '周六' => 6,
  2348. '周日' => 7,
  2349. ];
  2350. }