GoodsClass.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace biz\goods\classes;
  3. use biz\goods\services\GoodsCategoryService;
  4. use biz\merchant\services\MerchantService;
  5. use common\components\business;
  6. use common\components\stringUtil;
  7. use common\components\util;
  8. use Yii;
  9. use biz\base\classes\BaseClass;
  10. class GoodsClass extends BaseClass
  11. {
  12. public static $baseFile = '\biz\goods\models\Goods';
  13. //获取商品完整详情,包括商品的分类 shish 2019.12.3
  14. public static function getGoodsInfo($id)
  15. {
  16. $info = self::getById($id);
  17. if (empty($info)) {
  18. return [];
  19. }
  20. $list = self::groupGoodsBaseInfo([$info]);
  21. $info = current($list);
  22. //商品的分类
  23. $category = GoodsCategoryService::getGoodsHasCategoryIdList($id);
  24. $info['categoryIdList'] = $category;
  25. return $info;
  26. }
  27. //组装商品基本信息 shish 2019.12.2
  28. public static function groupGoodsBaseInfo($list)
  29. {
  30. //注意这里涉及商家节日涨价的问题,不要去取不同商家的商品,确有必要另外起方法!!!!!!
  31. $currentData = current($list);
  32. $merchantId = $currentData['merchantId'];
  33. $rise = GoodsSettingClass::getRise($merchantId);
  34. foreach ($list as $key => $val) {
  35. $id = $val['id'];
  36. //大中小图片转化
  37. $val = business::formatGoodsImg($val);
  38. //免费配送范围
  39. $val['freeSendDist'] = 5;
  40. //多款式
  41. $val['goodsStyleList'] = isset($val['goodsStyle']) && $val['goodsStyle'] == 1 ? GoodsStyleClass::getStyleList($id) : [];
  42. $val['createDate'] = date("Y-m-d", strtotime($val['createTime']));
  43. $val['merchantName'] = '';
  44. //节日涨价
  45. if (!empty($rise)) {
  46. $price = $val['price'];
  47. if ($rise['riseType'] == 0) {
  48. //百分比
  49. $val['price'] = $price + sprintf("%.1f", ($price * $rise['riseAmount'] / 100));
  50. } else {
  51. //金额
  52. $val['price'] = $price + $rise['riseAmount'];
  53. }
  54. }
  55. $list[$key] = $val;
  56. }
  57. return $list;
  58. }
  59. public static function addGoods($data)
  60. {
  61. $categoryIdList = isset($data['categoryIdList']) && !empty($data['categoryIdList']) ? $data['categoryIdList'] : [];
  62. unset($data['categoryIdList']);
  63. if (empty($categoryIdList)) {
  64. util::fail('没有选择分类');
  65. }
  66. $goodsStyleList = isset($data['goodsStyleList']) && !empty($data['goodsStyleList']) ? $data['goodsStyleList'] : [];
  67. if (isset($data['goodsStyle']) && $data['goodsStyle'] == 1) {
  68. if (empty($goodsStyleList)) {
  69. util::fail('请填写款式');
  70. }
  71. foreach ($goodsStyleList as $goodsStyle) {
  72. if (empty($goodsStyle['styleName'])) {
  73. util::fail('请填写款式名称');
  74. }
  75. if (isset($goodsStyle['price']) == false || is_numeric($goodsStyle['price']) == false) {
  76. util::fail('请填写款式价格');
  77. }
  78. if (empty($goodsStyle['picture'])) {
  79. util::fail('请上传款式图片');
  80. }
  81. }
  82. } else {
  83. if (isset($data['price']) == false || is_numeric($data['price']) == false) {
  84. util::fail('请填写商品价格');
  85. }
  86. }
  87. //用商户id生成商品的编号
  88. $merchantId = $data['merchantId'];
  89. $data['createTime'] = date("Y-m-d H:i:s");
  90. $data['shopImg'] = json_encode($data['shopImg']);
  91. $data['briefContent'] = isset($data['briefContent']) ? $data['briefContent'] : '';
  92. $data['content'] = isset($data['content']) ? $data['content'] : '';
  93. $data['stock'] = isset($data['stock']) && is_numeric($data['stock']) && $data['stock'] >= 0 ? $data['stock'] : -1;
  94. $data['sold'] = isset($data['sold']) && is_numeric($data['sold']) && $data['sold'] >= 0 ? $data['sold'] : 0;
  95. $goodsInfo = self::add($data);
  96. $id = $goodsInfo['id'];
  97. $goodsName = $goodsInfo['goodsName'];
  98. //处理分类
  99. $hasCategoryIdList = CategoryClass::getCategoryIdList($merchantId);
  100. $diff = array_diff($categoryIdList, $hasCategoryIdList);
  101. if (!empty($diff)) {
  102. util::fail('您使用了别人的分类');
  103. }
  104. $addCategory = [];
  105. foreach ($categoryIdList as $categoryId) {
  106. $addCategory[] = ['gId' => $id, 'goodsName' => $goodsName, 'cId' => $categoryId, 'merchantId' => $data['merchantId']];
  107. }
  108. GoodsCategoryClass::batchAdd($addCategory);
  109. //处理款式
  110. if (!empty($goodsStyleList) && $data['goodsStyle'] == 1) {
  111. $addStyle = [];
  112. foreach ($goodsStyleList as $goodsStyle) {
  113. $addStyle[] = ['styleName' => $goodsStyle['styleName'], 'goodsId' => $id, 'picture' => $goodsStyle['picture'], 'price' => $goodsStyle['price']];
  114. }
  115. GoodsStyleClass::batchAdd($addStyle);
  116. }
  117. return $goodsInfo;
  118. }
  119. //更新商品 shish 2019.12.7
  120. public static function updateGoods($id, $data)
  121. {
  122. $categoryIdList = isset($data['categoryIdList']) && !empty($data['categoryIdList']) ? $data['categoryIdList'] : [];
  123. unset($data['categoryIdList']);
  124. if (empty($categoryIdList)) {
  125. util::fail('没有选择分类');
  126. }
  127. $goodsStyleList = isset($data['goodsStyleList']) && !empty($data['goodsStyleList']) ? $data['goodsStyleList'] : [];
  128. unset($data['goodsStyleList']);
  129. //处理分类
  130. $merchantId = $data['merchantId'];
  131. $merchantHasCategoryIdList = CategoryClass::getCategoryIdList($merchantId);
  132. $diff = array_diff($categoryIdList, $merchantHasCategoryIdList);
  133. if (!empty($diff)) {
  134. util::fail('您使用了别人的分类');
  135. }
  136. //先删除再添加分类
  137. GoodsCategoryClass::delGoodsCategory($id);
  138. $addCategory = [];
  139. foreach ($categoryIdList as $categoryId) {
  140. $addCategory[] = ['gId' => $id, 'goodsName' => $data['goodsName'], 'cId' => $categoryId, 'merchantId' => $data['merchantId']];
  141. }
  142. GoodsCategoryClass::batchAdd($addCategory);
  143. //处理款式
  144. if (!empty($goodsStyleList) && $data['goodsStyle'] == 1) {
  145. GoodsStyleClass::updateGoodsStyle($goodsStyleList, $id);
  146. }
  147. $data['shopImg'] = json_encode($data['shopImg']);
  148. self::updateById($id, $data);
  149. }
  150. //产品上下架 shish 2019.12.8
  151. public static function changeStatus($id, $status)
  152. {
  153. self::updateById($id, ['status' => $status]);
  154. GoodsCategoryClass::updateByCondition(['gId' => $id], ['status' => $status]);
  155. }
  156. }