SpecOptionService.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Name: 芸众商城系统
  5. * Author: 广州市芸众信息科技有限公司
  6. * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
  7. * Date: 2021/9/28
  8. * Time: 16:04
  9. */
  10. namespace app\backend\modules\goods\services;
  11. use app\backend\modules\goods\models\GoodsOption;
  12. use app\backend\modules\goods\models\GoodsSpec;
  13. use app\backend\modules\goods\models\GoodsSpecItem;
  14. class SpecOptionService
  15. {
  16. protected static $instance = null;
  17. protected $parameter;
  18. protected $goods_id;
  19. protected $uniacid;
  20. public function __construct()
  21. {
  22. }
  23. public static function store($goods_id,$option, $uniacid)
  24. {
  25. $self = self::getInstance();
  26. $self->parameter = $option;
  27. $self->goods_id = $goods_id;
  28. $self->uniacid = $uniacid;
  29. // if (empty($option['specs']) || empty($option['option'])) {
  30. // return;
  31. // }
  32. if (!$option['has_option'] && empty($option['specs'])) {
  33. GoodsSpec::where('goods_id', '=', $goods_id)->delete();
  34. GoodsOption::where('goods_id', '=', $goods_id)->delete();
  35. return;
  36. }
  37. //规格项
  38. $spec_items = $self->saveSpec();
  39. //规格信息
  40. $self->saveOption($spec_items);
  41. }
  42. public function saveSpec()
  43. {
  44. $specs = $this->parameter['specs'];
  45. $spec_items = [];
  46. foreach ($specs as $specIndex => $spec) {
  47. $spec_data = [
  48. "uniacid" => $this->uniacid,
  49. "goods_id" => $this->goods_id,
  50. "display_order" => $specIndex,
  51. "title" => $spec['title']
  52. ];
  53. //新添加规格
  54. $tag = substr($spec['id'], 0, 2);
  55. if ('SC' == strtoupper($tag)) {
  56. $goods_spec = GoodsSpec::Create($spec_data);
  57. $spec_id = $goods_spec->id;
  58. } else {
  59. $goods_spec = GoodsSpec::updateOrCreate(['id' => $spec['id']], $spec_data);
  60. $spec_id = $goods_spec->id;
  61. }
  62. foreach ($spec['spec_item'] as $valueIndex => $value) {
  63. $specItem = [
  64. "uniacid" => $this->uniacid,
  65. "specid" => $spec_id,
  66. "display_order" => $valueIndex,
  67. "title" => $value['title'],
  68. "show" => $value['show']?:1,
  69. "thumb" =>'',
  70. "virtual" => '',
  71. ];
  72. //新添加规格
  73. $valueTag = substr($value['id'], 0, 2);
  74. if ('SV' == strtoupper($valueTag)) {
  75. $goods_spec_item = GoodsSpecItem::Create($specItem);
  76. $item_id = $goods_spec_item->id;
  77. } else {
  78. $goods_spec_item = GoodsSpecItem::updateOrCreate(['id' => $value['id']], $specItem);
  79. $item_id = $goods_spec_item->id;
  80. }
  81. $itemids[] = $item_id;
  82. $spec_items[$value['id']] = $item_id;
  83. }
  84. if (count($itemids) > 0) {
  85. GoodsSpecItem::where('specid', '=', $spec_id)->whereNotIn('id', $itemids)->delete();
  86. } else {
  87. GoodsSpecItem::where('specid', '=', $spec_id)->delete();
  88. }
  89. GoodsSpec::updateOrCreate(['id' => $spec_id], ['content' => serialize($itemids)]);
  90. $specids[] = $spec_id;
  91. }
  92. if (count($specids) > 0) {
  93. GoodsSpec::where('goods_id', '=', $this->goods_id)->whereNotIn('id', $specids)->delete();
  94. } else {
  95. GoodsSpec::where('goods_id', '=', $this->goods_id)->delete();
  96. }
  97. return $spec_items;
  98. }
  99. public function saveOption($spec_items)
  100. {
  101. $optionPost = $specs = $this->parameter['option'];
  102. foreach ($optionPost as $oKey => $option) {
  103. //规格项id处理
  104. $spec_item_ids = explode('_',$option['specs']);
  105. $specs_id_array = array_map(function ($spec_item_id) use ($spec_items) {
  106. return $spec_items[$spec_item_id];
  107. },$spec_item_ids);
  108. $goodsOption = [
  109. "uniacid" => $this->uniacid,
  110. "goods_id" => $this->goods_id,
  111. "title" =>$option['title'],
  112. "product_price" => floatVal($option['product_price']),
  113. "cost_price" => floatVal($option['cost_price']),
  114. "market_price" => floatVal($option['market_price']),
  115. "stock" => $option['stock']?:0,
  116. "weight" => floatVal($option['weight']),
  117. "volume" => floatVal($option['volume']),
  118. "goods_sn" => $option['goods_sn'],
  119. "product_sn" => $option['product_sn'],
  120. 'thumb' => $option['thumb'],
  121. "specs" => implode('_',$specs_id_array),
  122. 'virtual' => 0,
  123. "red_price" => '',
  124. "display_order" => $oKey,
  125. ];
  126. $optionTag = substr($option['id'], 0, 2);
  127. if ('SP' == strtoupper($optionTag)) {
  128. $goodsOptionModel = GoodsOption::create($goodsOption);
  129. $option_id = $goodsOptionModel->id;
  130. } else {
  131. $goodsOptionModel = GoodsOption::updateOrCreate(['id' => $option['id']], $goodsOption);
  132. $option_id = $goodsOptionModel->id;
  133. }
  134. $optionids[] = $option_id;
  135. }
  136. if (count($optionids) > 0) {
  137. GoodsOption::where('goods_id', '=', $this->goods_id)->whereNotIn('id', $optionids)->delete();
  138. } else {
  139. GoodsOption::where('goods_id', '=', $this->goods_id)->delete();
  140. }
  141. }
  142. /**
  143. * 单例缓存
  144. * @return null|self
  145. */
  146. public static function getInstance()
  147. {
  148. if (!isset(self::$instance)) {
  149. self::$instance = new self();
  150. }
  151. return self::$instance;
  152. }
  153. }