GoodsService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <?php
  2. namespace app\admin\service\goods;
  3. use app\model\Goods;
  4. use app\model\GoodsDetail;
  5. use app\model\GoodsLabel;
  6. use app\model\GoodsRunning;
  7. use app\model\GoodsSku;
  8. use app\model\SysSerial;
  9. use support\Db;
  10. use support\exception\BusinessException;
  11. use support\Response;
  12. class GoodsService
  13. {
  14. /**
  15. * @Desc 添加商品
  16. * @Author Gorden
  17. * @Date 2024/3/11 10:20
  18. *
  19. * @param $params
  20. * @return Response
  21. */
  22. public static function insert($params): Response
  23. {
  24. Db::beginTransaction();
  25. try {
  26. $params['goods_id'] = "GD" . sprintf('%016d', SysSerial::getSerial()) . random_string(8);
  27. // 主表
  28. self::mainInsert($params);
  29. // 商品详情表
  30. self::detailInsert($params);
  31. // 商品标签表
  32. self::labelInsert($params);
  33. // 产品运行控制信息表
  34. self::goodsRunningInsert($params);
  35. Db::commit();
  36. } catch (\PDOException $e) {
  37. return json_fail('数据写入失败~');
  38. } catch (BusinessException $e) {
  39. Db::rollBack();
  40. return json_fail($e->getMessage());
  41. } catch (\Exception $e) {
  42. Db::rollBack();
  43. return json_fail('数据写入失败~');
  44. }
  45. return json_success('success');
  46. }
  47. public static function update($params)
  48. {
  49. Db::beginTransaction();
  50. try {
  51. // 主表
  52. self::mainUpdate($params);
  53. // 商品详情表
  54. self::detailUpdate($params);
  55. // 商品标签表
  56. self::labelUpdate($params);
  57. // 产品运行控制信息表
  58. self::goodsRunningUpdate($params);
  59. Db::commit();
  60. } catch (BusinessException $e) {
  61. Db::rollBack();
  62. return json_fail($e->getMessage());
  63. } catch (\Exception $e) {
  64. Db::rollBack();
  65. return json_fail('数据更新失败~');
  66. }
  67. return json_success('success');
  68. }
  69. /**
  70. * @Desc 商品主表
  71. * @Author Gorden
  72. * @Date 2024/3/11 11:20
  73. *
  74. * @param $params
  75. * @return mixed|string
  76. * @throws BusinessException
  77. */
  78. public static function mainInsert($params)
  79. {
  80. try {
  81. $model = new Goods();
  82. $model->goods_id = $params['goods_id'];
  83. $model->join_goods_category_id = $params['join_goods_category_id'];
  84. $model->goods_classify = $params['goods_classify'];
  85. $model->goods_status = $params['goods_status'];
  86. $model->goods_category = $params['goods_category'];
  87. $model->goods_prefix = $params['goods_prefix'];
  88. $model->goods_name = $params['goods_name'];
  89. $model->goods_market_price = $params['goods_market_price'];
  90. $model->goods_sales_price = $params['goods_sales_price'];
  91. $model->goods_sku_json = !empty($params['goods_sku_json']) ? $params['goods_sku_json'] : '{}';
  92. $model->goods_attribute_json = !empty($params['goods_attribute_json']) ? $params['goods_attribute_json'] : '{}';
  93. $model->goods_service_json = !empty($params['goods_service_json']) ? $params['goods_service_json'] : '{}';
  94. $model->goods_title = $params['goods_title'];
  95. $model->goods_cover = $params['goods_cover'];
  96. $model->goods_on_addtimes = $params['goods_on_addtimes'] ? strtotime($params['goods_on_addtimes']) : null;
  97. $model->goods_sort = $params['goods_sort'];
  98. $model->goods_groupby = $params['goods_groupby'];
  99. $model->goods_remark = $params['goods_remark'];
  100. $model->goods_extend_json = $params['goods_extend_json'];
  101. $model->goods_addtimes = time();
  102. if ($model->save()) {
  103. return $model->goods_id;
  104. }
  105. // 异常
  106. throw new BusinessException("数据写入失败~");
  107. } catch (\Exception $e) {
  108. throw new BusinessException("数据写入失败~");
  109. }
  110. }
  111. /**
  112. * @Desc 详情表
  113. * @Author Gorden
  114. * @Date 2024/3/11 11:19
  115. *
  116. * @param $params
  117. * @return void
  118. * @throws BusinessException
  119. */
  120. public static function detailInsert($params)
  121. {
  122. try {
  123. $model = new GoodsDetail();
  124. $model->join_detail_goods_id = $params['goods_id'];
  125. $model->goods_detail_code_json = $params['goods_detail_code_json'];
  126. $model->goods_detail_slider_json = $params['goods_detail_slider_json'];
  127. $model->goods_detail_specs_json = $params['goods_detail_specs_json'];
  128. $model->goods_detail_content = $params['goods_detail_content'];
  129. if (!$model->save()) {
  130. // 异常
  131. throw new BusinessException("数据写入失败~");
  132. }
  133. } catch (\Exception $e) {
  134. throw new BusinessException("数据写入失败~");
  135. }
  136. }
  137. /**
  138. * @Desc 标签表
  139. * @Author Gorden
  140. * @Date 2024/3/11 11:32
  141. *
  142. * @param $params
  143. * @return void
  144. * @throws BusinessException
  145. */
  146. public static function labelInsert($params)
  147. {
  148. $model = new GoodsLabel();
  149. $model->join_label_goods_id = $params['goods_id'];
  150. $model->goods_label = $params['goods_label'];
  151. $model->goods_label_extend_json = !empty($params['goods_label_extend_json']) ? $params['goods_label_extend_json'] : '{}';
  152. if (!$model->save()) {
  153. // 异常
  154. throw new BusinessException('数据写入失败~');
  155. }
  156. }
  157. /**
  158. * @Desc 产品运行控制信息表
  159. * @Author Gorden
  160. * @Date 2024/3/11 11:38
  161. *
  162. * @param $params
  163. * @return void
  164. * @throws BusinessException
  165. */
  166. public static function goodsRunningInsert($params)
  167. {
  168. try {
  169. $model = new GoodsRunning();
  170. $model->join_running_goods_id = $params['goods_id'];
  171. $model->goods_running_storage = $params['goods_running_storage'];
  172. $model->goods_running_sale = $params['goods_running_sale'];
  173. $model->goods_running_off_type = $params['goods_running_off_type'];
  174. $model->goods_running_off_json = $params['goods_running_off_json'];
  175. if (!$model->save()) {
  176. throw new BusinessException('数据写入失败');
  177. }
  178. } catch (\Exception $e) {
  179. throw new BusinessException('数据写入失败');
  180. }
  181. }
  182. /**
  183. * @Desc SKU
  184. * @Author Gorden
  185. * @Date 2024/3/11 12:01
  186. *
  187. * @param $params
  188. * @return void
  189. * @throws BusinessException
  190. */
  191. public static function skuInsert($params)
  192. {
  193. $model = new GoodsSku();
  194. $model->join_sku_goods_id = $params['goods_id'];
  195. $model->goods_sku_status = $params['goods_sku_status'];
  196. $model->goods_sku_specs_json = $params['goods_sku_specs_json'];
  197. $model->goods_sku_title = $params['goods_sku_title'];
  198. $model->goods_sku_images_json = $params['goods_sku_images_json'];
  199. $model->goods_sku_content = $params['goods_sku_content'];
  200. $model->goods_sku_market_price = $params['goods_sku_market_price'];
  201. $model->goods_sku_sales_price = $params['goods_sku_sales_price'];
  202. $model->goods_sku_storage_json = $params['goods_sku_storage_json'];
  203. $model->goods_sku_service_json = $params['goods_sku_service_json'];
  204. $model->goods_sku_extend_json = $params['goods_sku_extend_json'];
  205. if (!$model->save()) {
  206. throw new BusinessException('数据写入失败~');
  207. }
  208. }
  209. /**
  210. * @Desc
  211. * @Author Gorden
  212. * @Date 2024/3/12 8:44
  213. *
  214. * @param $params
  215. * @return void
  216. * @throws BusinessException
  217. */
  218. public static function mainUpdate($params)
  219. {
  220. try {
  221. $data = self::inputFilter($params, new Goods());
  222. $data['goods_on_addtimes'] = strtotime($data['goods_on_addtimes']);
  223. self::doUpdate($data['goods_id'], $data, new Goods());
  224. } catch (BusinessException $e) {
  225. throw new BusinessException($e->getMessage());
  226. } catch (\Exception $e) {
  227. throw new BusinessException('数据更新异常~');
  228. }
  229. }
  230. /**
  231. * @Desc
  232. * @Author Gorden
  233. * @Date 2024/3/12 9:57
  234. *
  235. * @param $params
  236. * @return void
  237. * @throws BusinessException
  238. */
  239. public static function detailUpdate($params)
  240. {
  241. try {
  242. $data = self::inputFilter($params, new GoodsDetail());
  243. // 根据goods_id 查详情ID
  244. $detail = GoodsDetail::where('join_detail_goods_id', $params['goods_id'])->first();
  245. self::doUpdate($detail->goods_detail_id, $data, new GoodsDetail());
  246. } catch (BusinessException $e) {
  247. throw new BusinessException($e->getMessage());
  248. } catch (\Exception $e) {
  249. throw new BusinessException('数据更新异常~');
  250. }
  251. }
  252. /**
  253. * @Desc
  254. * @Author Gorden
  255. * @Date 2024/3/12 9:58
  256. *
  257. * @param $params
  258. * @return void
  259. * @throws BusinessException
  260. */
  261. public static function labelUpdate($params)
  262. {
  263. try {
  264. $data = self::inputFilter($params, new GoodsLabel());
  265. // 根据goods_id 查详情ID
  266. $detail = GoodsLabel::where('join_label_goods_id', $params['goods_id'])->first();
  267. self::doUpdate($detail->goods_label_id, $data, new GoodsLabel());
  268. } catch (BusinessException $e) {
  269. throw new BusinessException($e->getMessage());
  270. } catch (\Exception $e) {
  271. throw new BusinessException('数据更新异常~');
  272. }
  273. }
  274. /**
  275. * @Desc
  276. * @Author Gorden
  277. * @Date 2024/3/12 9:59
  278. *
  279. * @param $params
  280. * @return void
  281. * @throws BusinessException
  282. */
  283. public static function goodsRunningUpdate($params)
  284. {
  285. try {
  286. $data = self::inputFilter($params, new GoodsRunning());
  287. // 根据goods_id 查详情ID
  288. $detail = GoodsRunning::where('join_running_goods_id', $params['goods_id'])->first();
  289. self::doUpdate($detail->goods_running_id, $data, new GoodsRunning());
  290. } catch (BusinessException $e) {
  291. throw new BusinessException($e->getMessage());
  292. } catch (\Exception $e) {
  293. throw new BusinessException('数据更新异常~');
  294. }
  295. }
  296. /**
  297. * @Desc
  298. * @Author Gorden
  299. * @Date 2024/3/12 8:45
  300. *
  301. * @param array $data
  302. * @param $model
  303. * @return array
  304. * @throws BusinessException
  305. */
  306. private static function inputFilter(array $data, $model): array
  307. {
  308. $table = config('database.connections.mysql.prefix') . $model->getTable();
  309. $allow_column = $model->getConnection()->select("desc `$table`");
  310. if (!$allow_column) {
  311. throw new BusinessException('表不存在', 2);
  312. }
  313. $columns = array_column($allow_column, 'Type', 'Field');
  314. foreach ($data as $col => $item) {
  315. if (!isset($columns[$col])) {
  316. unset($data[$col]);
  317. continue;
  318. }
  319. // 非字符串类型传空则为null
  320. if ($item === '' && strpos(strtolower($columns[$col]), 'varchar') === false && strpos(strtolower($columns[$col]), 'text') === false) {
  321. $data[$col] = null;
  322. }
  323. if (is_array($item)) {
  324. $data[$col] = implode(',', $item);
  325. }
  326. if ($item != '' && (strpos(strtolower($columns[$col]), 'varchar') || strpos(strtolower($columns[$col]), 'text'))) {
  327. $data[$col] = htmlspecialchars($item);
  328. }
  329. }
  330. if (empty($data['created_at'])) {
  331. unset($data['created_at']);
  332. }
  333. if (empty($data['updated_at'])) {
  334. unset($data['updated_at']);
  335. }
  336. return $data;
  337. }
  338. /**
  339. * @Desc 执行更新
  340. * @Author Gorden
  341. * @Date 2024/3/12 8:43
  342. *
  343. * @param $id
  344. * @param $data
  345. * @param $model
  346. * @return void
  347. */
  348. private static function doUpdate($id, $data, $model)
  349. {
  350. $row = $model->find($id);
  351. foreach ($data as $key => $val) {
  352. $row->{$key} = $val;
  353. }
  354. $row->save();
  355. }
  356. }