GoodsService.php 116 KB

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