xhGoodsService.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. namespace common\services;
  3. use bizHd\goods\services\GoodsCategoryService;
  4. use bizHd\goods\services\GoodsPriceService;
  5. use bizHd\goods\services\GoodsUsageService;
  6. use common\components\util;
  7. use Yii;
  8. use common\components\stringUtil;
  9. use common\components\dict;
  10. use common\models\xhGoods;
  11. class xhGoodsService
  12. {
  13. public static function getLimitGoods($sjId)
  14. {
  15. if ($sjId == 12358) {
  16. //$goodsList = xhGoods::find()->where(['sjId' => $sjId, 'id' => [163,168,190, 191, 192, 193, 184, 185, 167, 166, 186, 168]])->orderBy('inTurn desc')->asArray()->all();
  17. //情人节临时
  18. $goodsList = xhGoods::find()->where(['sjId' => $sjId, 'id' => [190, 183, 200, 202, 204, 206, 205, 203, 208, 207, 163, 168]])->asArray()->all();
  19. } else {
  20. $goodsList = xhGoods::find()->where(['sjId' => $sjId])->orderBy('inTurn desc')->limit(8)->asArray()->all();
  21. }
  22. if (!empty($goodsList)) {
  23. foreach ($goodsList as $key => $goods) {
  24. //取出较小的图片页 ssh 2019.8.3
  25. $url = $goods['cover'];
  26. $pos = strrpos($url, '.');
  27. $pre = substr($url, 0, $pos);
  28. $ext = substr($url, ($pos + 1));
  29. $goodsList[$key]['middleCover'] = $pre . '_200.' . $ext;
  30. }
  31. }
  32. return $goodsList;
  33. }
  34. /**
  35. * 获取所有商品
  36. */
  37. public static function getAllGoodsList($sjId)
  38. {
  39. $allGoodsIds = xhGoodsService::getAllByCondition(['sjId' => $sjId, 'delStatus' => 0], null, ['id']);
  40. if (empty($allGoodsIds)) {
  41. return false;
  42. }
  43. $goodsList = [];
  44. foreach ($allGoodsIds as $goods) {
  45. $goodsId = $goods['id'];
  46. $goodsInfo = xhGoodsService::getById($goodsId);
  47. //取出较小的图片页 ssh 2019.8.3
  48. $url = $goodsInfo['cover'];
  49. $pos = strrpos($url, '.');
  50. $pre = substr($url, 0, $pos);
  51. $ext = substr($url, ($pos + 1));
  52. $goodsInfo['middleCover'] = $pre . '_200.' . $ext;
  53. $goodsList[] = $goodsInfo;
  54. }
  55. return $goodsList;
  56. }
  57. /**
  58. * 获取所有商品id
  59. */
  60. public static function getAllGoodsIds($sjId)
  61. {
  62. $allGoodsIds = xhGoodsService::getAllByCondition(['sjId' => $sjId, 'delStatus' => 0], null, ['id']);
  63. if (empty($allGoodsIds)) {
  64. return [];
  65. }
  66. $goodsList = [];
  67. foreach ($allGoodsIds as $goods) {
  68. $goodsId = $goods['id'];
  69. $goodsList[] = $goodsId;
  70. }
  71. return $goodsList;
  72. }
  73. /**
  74. * 管理员添加商品
  75. */
  76. public static function addByAdmin($data, $merchant, $admin)
  77. {
  78. $adminId = $admin['id'];
  79. $sjId = $merchant['id'];
  80. $itemNumber = stringUtil::buildRandNo($sjId);//用商户id生成商品的编号
  81. $date = date("Y-m-d H:i:s");
  82. $data['itemNumber'] = (string)$itemNumber;
  83. $data['sjId'] = $sjId;
  84. $data['userId'] = isset($data['userId']) ? $data['userId'] : 0;
  85. $data['createTime'] = $date;
  86. $goods = self::add($data);
  87. if ($goods === false) {
  88. util::fail('商品添加失败');
  89. }
  90. $id = $goods['id'];
  91. /**商品分类**/
  92. $idString = $data['categoryId'];
  93. $ids = explode(',', $idString);
  94. $ids = array_filter($ids);//删除空元素
  95. $catData = [];
  96. foreach ($ids as $cId) {
  97. $catData[] = ['sjId' => $sjId, 'gId' => $id, 'cId' => $cId, 'inTurn' => 100];
  98. }
  99. GoodsCategoryService::batchAdd($catData);
  100. /**商品用途**/
  101. $usageString = $data['usageId'];
  102. $usageIdList = explode(',', $usageString);
  103. $usageIdList = array_filter($usageIdList);
  104. $addUsage = [];
  105. $time = time();
  106. foreach ($usageIdList as $usageId) {
  107. $addUsage[] = ['usageId' => $usageId, 'goodsId' => $id, 'sjId' => $sjId, 'addTime' => $time];
  108. }
  109. GoodsUsageService::batchAdd($addUsage);
  110. }
  111. /**
  112. * 更新操作
  113. * 同时更新商品和商品的分类
  114. */
  115. public static function updateAllById($gId, $data, $sjId)
  116. {
  117. self::updateById($gId, $data);
  118. //分类
  119. $addCategoryId = isset($data['addCategoryId']) ? $data['addCategoryId'] : '';
  120. GoodsCategoryService::deleteByCondition(['gId' => $gId]);
  121. if (!empty($addCategoryId)) {//需要新增的商品分类
  122. $addIds = explode(',', $addCategoryId);
  123. $catData = [];
  124. foreach ($addIds as $cId) {
  125. $catData[] = ['sjId' => $sjId, 'gId' => $gId, 'cId' => $cId];
  126. }
  127. GoodsCategoryService::batchAdd($catData);
  128. }
  129. //用途
  130. $time = time();
  131. $addUsageId = isset($data['addUsageId']) ? $data['addUsageId'] : '';
  132. GoodsUsageService::deleteByCondition(['goodsId' => $gId]);
  133. if (!empty($addUsageId)) {
  134. $ids = explode(',', $addUsageId);
  135. $addData = [];
  136. foreach ($ids as $id) {
  137. $addData[] = ['usageId' => $id, 'goodsId' => $gId, 'sjId' => $sjId, 'addTime' => $time];
  138. }
  139. GoodsUsageService::batchAdd($addData);
  140. }
  141. }
  142. //商品统一涨价设置,多规格拉取
  143. public static function getById($id)
  144. {
  145. $goods = xhGoods::find()->where(['id' => $id])->asArray()->one();
  146. $goods = xhGoodsSettingService::unitySet($goods);//统一对商品进行价格等方面设置
  147. $goods['multipleStandard'] = [];
  148. if (isset($goods['multiPrice']) && !empty($goods['multiPrice'])) {
  149. $multiPriceList = explode(',', $goods['multiPrice']);
  150. $goods['multipleStandard'] = GoodsPriceService::getByIds($multiPriceList);
  151. }
  152. return $goods;
  153. }
  154. public static function refreshById($id)
  155. {
  156. $cacheKey = dict::getCacheKey('goodsGetById') . $id;
  157. Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
  158. self::getById($id);
  159. }
  160. public static function updateById($id, $data)
  161. {
  162. xhGoods::updateById($id, $data);
  163. self::refreshById($id);
  164. }
  165. public static function deleteById($id)
  166. {
  167. $goods = self::getById($id);
  168. xhGoodsCategoryService::delGoodsCategoryId($goods);//清理商品所属的分类
  169. xhGoods::deleteById($id);//最后删除商品主体
  170. self::refreshById($id);
  171. }
  172. public static function add($data)
  173. {
  174. $goods = xhGoods::add($data);
  175. $id = $goods['id'];
  176. self::refreshById($id);
  177. return $goods;
  178. }
  179. /**
  180. * 传入最大的商品图片,输出三种头像:大 中 小
  181. */
  182. public static function getGoodsImgList($largeImg)
  183. {
  184. $extend = substr($largeImg, strrpos($largeImg, '.') + 1);
  185. $pre = substr($largeImg, 0, strrpos($largeImg, '.'));
  186. return ['small' => $pre . '_50.' . $extend, 'medium' => $pre . '_200.' . $extend, 'large' => $largeImg];
  187. }
  188. public static function getAllByCondition($condition, $order = null, $field = '*')
  189. {
  190. $goods = xhGoods::getAllByCondition($condition, $order, $field);
  191. return $goods;
  192. }
  193. /**
  194. * 删除商品
  195. */
  196. public static function delGoods($goods)
  197. {
  198. $id = $goods['id'];
  199. $del = dict::getDict('goodsDelStatus', 'del');
  200. xhGoodsService::updateById($id, ['delStatus' => $del]);
  201. xhGoodsCategoryService::delGoodsCategoryId($goods);
  202. }
  203. }