| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- <?php
- namespace biz\goods\classes;
- use biz\goods\services\GoodsCategoryService;
- use biz\merchant\services\MerchantService;
- use common\components\business;
- use common\components\stringUtil;
- use common\components\util;
- use Yii;
- use biz\base\classes\BaseClass;
- class GoodsClass extends BaseClass
- {
- public static $baseFile = '\biz\goods\models\Goods';
- //获取商品完整详情,包括商品的分类 shish 2019.12.3
- public static function getGoodsInfo($id)
- {
- $info = self::getById($id);
- if (empty($info)) {
- return [];
- }
- $list = self::groupGoodsBaseInfo([$info]);
- $info = current($list);
- //商品的分类
- $category = GoodsCategoryService::getGoodsHasCategoryIdList($id);
- $info['categoryIdList'] = $category;
- return $info;
- }
- //组装商品基本信息 shish 2019.12.2
- public static function groupGoodsBaseInfo($list)
- {
- //注意这里涉及商家节日涨价的问题,不要去取不同商家的商品,确有必要另外起方法!!!!!!
- $currentData = current($list);
- $merchantId = $currentData['merchantId'];
- $rise = GoodsSettingClass::getRise($merchantId);
- foreach ($list as $key => $val) {
- $id = $val['id'];
- //大中小图片转化
- $val = business::formatGoodsImg($val);
- //免费配送范围
- $val['freeSendDist'] = 5;
- //多款式
- $val['goodsStyleList'] = isset($val['goodsStyle']) && $val['goodsStyle'] == 1 ? GoodsStyleClass::getStyleList($id) : [];
- $val['createDate'] = date("Y-m-d", strtotime($val['createTime']));
- $val['merchantName'] = '';
- //节日涨价
- if (!empty($rise)) {
- $price = $val['price'];
- if ($rise['riseType'] == 0) {
- //百分比
- $val['price'] = $price + sprintf("%.1f", ($price * $rise['riseAmount'] / 100));
- } else {
- //金额
- $val['price'] = $price + $rise['riseAmount'];
- }
- }
- $list[$key] = $val;
- }
- return $list;
- }
- public static function addGoods($data)
- {
- $categoryIdList = isset($data['categoryIdList']) && !empty($data['categoryIdList']) ? $data['categoryIdList'] : [];
- unset($data['categoryIdList']);
- if (empty($categoryIdList)) {
- util::fail('没有选择分类');
- }
- $goodsStyleList = isset($data['goodsStyleList']) && !empty($data['goodsStyleList']) ? $data['goodsStyleList'] : [];
- if (isset($data['goodsStyle']) && $data['goodsStyle'] == 1) {
- if (empty($goodsStyleList)) {
- util::fail('请填写款式');
- }
- foreach ($goodsStyleList as $goodsStyle) {
- if (empty($goodsStyle['styleName'])) {
- util::fail('请填写款式名称');
- }
- if (isset($goodsStyle['price']) == false || is_numeric($goodsStyle['price']) == false) {
- util::fail('请填写款式价格');
- }
- if (empty($goodsStyle['picture'])) {
- util::fail('请上传款式图片');
- }
- }
- } else {
- if (isset($data['price']) == false || is_numeric($data['price']) == false) {
- util::fail('请填写商品价格');
- }
- }
- //用商户id生成商品的编号
- $merchantId = $data['merchantId'];
- $data['createTime'] = date("Y-m-d H:i:s");
- $data['shopImg'] = json_encode($data['shopImg']);
- $data['briefContent'] = isset($data['briefContent']) ? $data['briefContent'] : '';
- $data['content'] = isset($data['content']) ? $data['content'] : '';
- $data['stock'] = isset($data['stock']) && is_numeric($data['stock']) && $data['stock'] >= 0 ? $data['stock'] : -1;
- $data['sold'] = isset($data['sold']) && is_numeric($data['sold']) && $data['sold'] >= 0 ? $data['sold'] : 0;
- $goodsInfo = self::add($data);
- $id = $goodsInfo['id'];
- $goodsName = $goodsInfo['goodsName'];
- //处理分类
- $hasCategoryIdList = CategoryClass::getCategoryIdList($merchantId);
- $diff = array_diff($categoryIdList, $hasCategoryIdList);
- if (!empty($diff)) {
- util::fail('您使用了别人的分类');
- }
- $addCategory = [];
- foreach ($categoryIdList as $categoryId) {
- $addCategory[] = ['gId' => $id, 'goodsName' => $goodsName, 'cId' => $categoryId, 'merchantId' => $data['merchantId']];
- }
- GoodsCategoryClass::batchAdd($addCategory);
- //处理款式
- if (!empty($goodsStyleList) && $data['goodsStyle'] == 1) {
- $addStyle = [];
- foreach ($goodsStyleList as $goodsStyle) {
- $addStyle[] = ['styleName' => $goodsStyle['styleName'], 'goodsId' => $id, 'picture' => $goodsStyle['picture'], 'price' => $goodsStyle['price']];
- }
- GoodsStyleClass::batchAdd($addStyle);
- }
- return $goodsInfo;
- }
- //更新商品 shish 2019.12.7
- public static function updateGoods($id, $data)
- {
- $categoryIdList = isset($data['categoryIdList']) && !empty($data['categoryIdList']) ? $data['categoryIdList'] : [];
- unset($data['categoryIdList']);
- if (empty($categoryIdList)) {
- util::fail('没有选择分类');
- }
- $goodsStyleList = isset($data['goodsStyleList']) && !empty($data['goodsStyleList']) ? $data['goodsStyleList'] : [];
- unset($data['goodsStyleList']);
- //处理分类
- $merchantId = $data['merchantId'];
- $merchantHasCategoryIdList = CategoryClass::getCategoryIdList($merchantId);
- $diff = array_diff($categoryIdList, $merchantHasCategoryIdList);
- if (!empty($diff)) {
- util::fail('您使用了别人的分类');
- }
- //先删除再添加分类
- GoodsCategoryClass::delGoodsCategory($id);
- $addCategory = [];
- foreach ($categoryIdList as $categoryId) {
- $addCategory[] = ['gId' => $id, 'goodsName' => $data['goodsName'], 'cId' => $categoryId, 'merchantId' => $data['merchantId']];
- }
- GoodsCategoryClass::batchAdd($addCategory);
- //处理款式
- if (!empty($goodsStyleList) && $data['goodsStyle'] == 1) {
- GoodsStyleClass::updateGoodsStyle($goodsStyleList, $id);
- }
- $data['shopImg'] = json_encode($data['shopImg']);
- self::updateById($id, $data);
- }
- //产品上下架 shish 2019.12.8
- public static function changeStatus($id, $status)
- {
- self::updateById($id, ['status' => $status]);
- GoodsCategoryClass::updateByCondition(['gId' => $id], ['status' => $status]);
- }
- }
|