| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- <?php
- namespace common\services;
- use bizHd\goods\services\GoodsCategoryService;
- use bizHd\goods\services\GoodsPriceService;
- use bizHd\goods\services\GoodsUsageService;
- use common\components\util;
- use Yii;
- use common\components\stringUtil;
- use common\components\dict;
- use common\models\xhGoods;
- class xhGoodsService
- {
-
- public static function getLimitGoods($sjId)
- {
- if ($sjId == 12358) {
- //$goodsList = xhGoods::find()->where(['sjId' => $sjId, 'id' => [163,168,190, 191, 192, 193, 184, 185, 167, 166, 186, 168]])->orderBy('inTurn desc')->asArray()->all();
- //情人节临时
- $goodsList = xhGoods::find()->where(['sjId' => $sjId, 'id' => [190, 183, 200, 202, 204, 206, 205, 203, 208, 207, 163, 168]])->asArray()->all();
- } else {
- $goodsList = xhGoods::find()->where(['sjId' => $sjId])->orderBy('inTurn desc')->limit(8)->asArray()->all();
- }
- if (!empty($goodsList)) {
- foreach ($goodsList as $key => $goods) {
- //取出较小的图片页 ssh 2019.8.3
- $url = $goods['cover'];
- $pos = strrpos($url, '.');
- $pre = substr($url, 0, $pos);
- $ext = substr($url, ($pos + 1));
- $goodsList[$key]['middleCover'] = $pre . '_200.' . $ext;
- }
- }
- return $goodsList;
- }
-
- /**
- * 获取所有商品
- */
- public static function getAllGoodsList($sjId)
- {
- $allGoodsIds = xhGoodsService::getAllByCondition(['sjId' => $sjId, 'delStatus' => 0], null, ['id']);
- if (empty($allGoodsIds)) {
- return false;
- }
- $goodsList = [];
- foreach ($allGoodsIds as $goods) {
- $goodsId = $goods['id'];
- $goodsInfo = xhGoodsService::getById($goodsId);
-
- //取出较小的图片页 ssh 2019.8.3
- $url = $goodsInfo['cover'];
- $pos = strrpos($url, '.');
- $pre = substr($url, 0, $pos);
- $ext = substr($url, ($pos + 1));
- $goodsInfo['middleCover'] = $pre . '_200.' . $ext;
- $goodsList[] = $goodsInfo;
-
- }
- return $goodsList;
- }
-
- /**
- * 获取所有商品id
- */
- public static function getAllGoodsIds($sjId)
- {
- $allGoodsIds = xhGoodsService::getAllByCondition(['sjId' => $sjId, 'delStatus' => 0], null, ['id']);
- if (empty($allGoodsIds)) {
- return [];
- }
- $goodsList = [];
- foreach ($allGoodsIds as $goods) {
- $goodsId = $goods['id'];
- $goodsList[] = $goodsId;
- }
- return $goodsList;
- }
-
- /**
- * 管理员添加商品
- */
- public static function addByAdmin($data, $merchant, $admin)
- {
- $adminId = $admin['id'];
- $sjId = $merchant['id'];
- $itemNumber = stringUtil::buildRandNo($sjId);//用商户id生成商品的编号
- $date = date("Y-m-d H:i:s");
- $data['itemNumber'] = (string)$itemNumber;
- $data['sjId'] = $sjId;
- $data['userId'] = isset($data['userId']) ? $data['userId'] : 0;
- $data['createTime'] = $date;
- $goods = self::add($data);
- if ($goods === false) {
- util::fail('商品添加失败');
- }
- $id = $goods['id'];
- /**商品分类**/
- $idString = $data['categoryId'];
- $ids = explode(',', $idString);
- $ids = array_filter($ids);//删除空元素
- $catData = [];
- foreach ($ids as $cId) {
- $catData[] = ['sjId' => $sjId, 'gId' => $id, 'cId' => $cId, 'inTurn' => 100];
- }
- GoodsCategoryService::batchAdd($catData);
-
- /**商品用途**/
- $usageString = $data['usageId'];
- $usageIdList = explode(',', $usageString);
- $usageIdList = array_filter($usageIdList);
- $addUsage = [];
- $time = time();
- foreach ($usageIdList as $usageId) {
- $addUsage[] = ['usageId' => $usageId, 'goodsId' => $id, 'sjId' => $sjId, 'addTime' => $time];
- }
- GoodsUsageService::batchAdd($addUsage);
- }
-
- /**
- * 更新操作
- * 同时更新商品和商品的分类
- */
- public static function updateAllById($gId, $data, $sjId)
- {
- self::updateById($gId, $data);
- //分类
- $addCategoryId = isset($data['addCategoryId']) ? $data['addCategoryId'] : '';
- GoodsCategoryService::deleteByCondition(['gId' => $gId]);
- if (!empty($addCategoryId)) {//需要新增的商品分类
- $addIds = explode(',', $addCategoryId);
- $catData = [];
- foreach ($addIds as $cId) {
- $catData[] = ['sjId' => $sjId, 'gId' => $gId, 'cId' => $cId];
- }
- GoodsCategoryService::batchAdd($catData);
- }
- //用途
- $time = time();
- $addUsageId = isset($data['addUsageId']) ? $data['addUsageId'] : '';
- GoodsUsageService::deleteByCondition(['goodsId' => $gId]);
- if (!empty($addUsageId)) {
- $ids = explode(',', $addUsageId);
- $addData = [];
- foreach ($ids as $id) {
- $addData[] = ['usageId' => $id, 'goodsId' => $gId, 'sjId' => $sjId, 'addTime' => $time];
- }
- GoodsUsageService::batchAdd($addData);
- }
- }
-
- //商品统一涨价设置,多规格拉取
- public static function getById($id)
- {
- $goods = xhGoods::find()->where(['id' => $id])->asArray()->one();
- $goods = xhGoodsSettingService::unitySet($goods);//统一对商品进行价格等方面设置
- $goods['multipleStandard'] = [];
- if (isset($goods['multiPrice']) && !empty($goods['multiPrice'])) {
- $multiPriceList = explode(',', $goods['multiPrice']);
- $goods['multipleStandard'] = GoodsPriceService::getByIds($multiPriceList);
- }
- return $goods;
- }
-
- public static function refreshById($id)
- {
- $cacheKey = dict::getCacheKey('goodsGetById') . $id;
- Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
- self::getById($id);
- }
-
- public static function updateById($id, $data)
- {
- xhGoods::updateById($id, $data);
- self::refreshById($id);
- }
-
- public static function deleteById($id)
- {
- $goods = self::getById($id);
- xhGoodsCategoryService::delGoodsCategoryId($goods);//清理商品所属的分类
- xhGoods::deleteById($id);//最后删除商品主体
- self::refreshById($id);
- }
-
- public static function add($data)
- {
- $goods = xhGoods::add($data);
- $id = $goods['id'];
- self::refreshById($id);
- return $goods;
- }
-
- /**
- * 传入最大的商品图片,输出三种头像:大 中 小
- */
- public static function getGoodsImgList($largeImg)
- {
- $extend = substr($largeImg, strrpos($largeImg, '.') + 1);
- $pre = substr($largeImg, 0, strrpos($largeImg, '.'));
- return ['small' => $pre . '_50.' . $extend, 'medium' => $pre . '_200.' . $extend, 'large' => $largeImg];
- }
-
- public static function getAllByCondition($condition, $order = null, $field = '*')
- {
- $goods = xhGoods::getAllByCondition($condition, $order, $field);
- return $goods;
- }
-
- /**
- * 删除商品
- */
- public static function delGoods($goods)
- {
- $id = $goods['id'];
- $del = dict::getDict('goodsDelStatus', 'del');
- xhGoodsService::updateById($id, ['delStatus' => $del]);
- xhGoodsCategoryService::delGoodsCategoryId($goods);
- }
-
-
- }
|