GoodsService.php 113 KB

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