| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561 |
- <?php
- namespace addons\QiShangTong\common\service;
- use addons\QiShangTong\common\expand\sdk\qishangtong\src\model\GoodsDetailModel;
- use addons\QiShangTong\common\expand\sdk\qishangtong\src\model\GoodsSelectedListModel;
- use addons\QiShangTong\common\expand\sdk\qishangtong\src\model\GoodsTotalModel;
- use addons\QiShangTong\common\expand\sdk\qishangtong\src\request\GoodsDetailRequest;
- use addons\QiShangTong\common\expand\sdk\qishangtong\src\request\GoodsSelectedListRequest;
- use addons\QiShangTong\common\expand\sdk\qishangtong\src\request\GoodsTotalRequest;
- use addons\QiShangTong\common\helper\RequestHelper;
- use addons\QiShangTong\common\logic\GoodsLogic;
- use addons\QiShangTong\common\models\QiShangTongEvent;
- use addons\QiShangTong\common\models\QiShangTongGoods;
- use addons\QiShangTong\common\models\QiShangTongGoodsAttr;
- use common\enums\RabbitMqEnum;
- use common\enums\StatusEnum;
- use common\helpers\FormatHelper;
- use common\models\goods\Goods;
- use common\models\goods\GoodsAttr;
- use yii\helpers\Json;
- class GoodsService
- {
- public function page(int $mall_id, array $params)
- {
- $where = [
- ['mall_id' => $mall_id],
- ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]
- ];
- if (!empty($params['goods_name'])) {
- $where[] = ['like', 'goods_name', $params['goods_name']];
- }
- if (!empty($params['cate_id'])) {
- $where[] = ['cate_one' => $params['cate_id']];
- }
- if (isset($params['imported']) && $params['imported'] !== '') {
- switch ($params['imported']) {
- case 1:
- $where[] = ['>', 'goods_id' , 0];
- break;
- case 0:
- $where[] = ['=', 'goods_id' , 0];
- break;
- }
- }
- $ret = QiShangTongGoods::listPage([
- 'where' => $where,
- 'limit' => $params['limit'] ?? 10,
- 'order' => 'id desc',
- 'with' => ['cateOne', 'cateTwo']
- ]);
- if (!empty(QiShangTongGoods::getStaticError())) return QiShangTongGoods::getStaticError();
- if (!empty($ret['list'])) {
- $rate = MallService::rate($mall_id);
- foreach ($ret['list'] as &$item) {
- $item['content'] = Json::decode($item['content']);
- $item['retail_price'] = bcmul($rate, $item['retail_price'], 2);
- $item['retail_price1'] = bcmul($rate, $item['retail_price'], 2);
- $item['gonghuojia'] = bcmul($rate, $item['gonghuojia'], 2);
- $item['gonghuojia1'] = bcmul($rate, $item['gonghuojia'], 2);
- $item['price'] = bcmul($rate, $item['price'], 2);
- $item['price1'] = bcmul($rate, $item['price'], 2);
- }
- }
- return $ret;
- }
- public function thirdPage(int $mall_id, array $params)
- {
- try {
- $contentModel = new GoodsSelectedListModel();
- if (!empty($params['page'])) $contentModel->page = $params['page'];
- if (!empty($params['limit'])) $contentModel->limit = $params['limit'];
- if (!empty($params['goods_name'])) $contentModel->goods_name = $params['goods_name'];
- if (!empty($params['goods_id'])) $contentModel->goods_id = $params['goods_id'];
- if (!empty($params['order'])) $contentModel->order = $params['order'];
- if (!empty($params['cate_id'])) $contentModel->cate_id_1 = $params['cate_id'];
- if (!empty($params['cate_id_2'])) $contentModel->cate_id_2 = $params['cate_id_2'];
- if (!empty($params['brand_id'])) $contentModel->brand_id = $params['brand_id'];
- if (!empty($params['lowprice'])) $contentModel->lowprice = $params['lowprice'];
- if (!empty($params['highprice'])) $contentModel->highprice = $params['highprice'];
- if (!empty($params['sort_type']) && !empty($params['sort'])) {
- // 默认最新 1、价格高到低 2、价格低到高 3、最新上架 4、修改时间 5、商品最后上下时间
- $order_map = [
- "price_desc" => 1,
- "price_asc" => 2,
- "time_desc" => 3,
- "time_asc" => 4,
- ];
- $contentModel->order = $order_map["{$params['sort_type']}_{$params['sort']}"] ?? 1;
- }
- return RequestHelper::send(new GoodsSelectedListRequest(), $contentModel, $mall_id);
- } catch (\Exception $e) {
- return $e->getMessage();
- }
- }
- /**
- * 总数
- * @param int $mall_id
- * @param array $params
- * @return array|string
- */
- public function thirdCount(int $mall_id, array $params)
- {
- try {
- $contentModel = new GoodsTotalModel();
- if (!empty($params['goods_name'])) $contentModel->goods_name = $params['goods_name'];
- if (!empty($params['goods_id'])) $contentModel->goods_id = $params['goods_id'];
- if (!empty($params['order'])) $contentModel->order = $params['order'];
- if (!empty($params['cate_id_1'])) $contentModel->cate_id_1 = $params['cate_id_1'];
- if (!empty($params['cate_id_2'])) $contentModel->cate_id_2 = $params['cate_id_2'];
- if (!empty($params['brand_id'])) $contentModel->brand_id = $params['brand_id'];
- if (!empty($params['lowprice'])) $contentModel->lowprice = $params['lowprice'];
- if (!empty($params['highprice'])) $contentModel->highprice = $params['highprice'];
- return RequestHelper::send(new GoodsTotalRequest(), $contentModel, $mall_id);
- } catch (\Exception $e) {
- return $e->getMessage();
- }
- }
- public function localPage(int $mall_id, array $params)
- {
- $where = [
- ['mall_id' => $mall_id],
- ['status' => StatusEnum::ENABLED]
- ];
- if (!empty($params['goods_name'])) {
- $where[] = ['like', 'goods_name', $params['goods_name']];
- }
- if (!empty($params['cate_id'])) {
- $where[] = ['cate_one' => $params['cate_id']];
- }
- $order = 'id desc';
- if (!empty($params['sort']) && !empty($params['sort_type'])) {
- switch ($params['sort_type']) {
- case 'time':
- $order = "created_at {$params['sort']}";
- break;
- case 'price':
- $order = "price {$params['sort']}";
- break;
- }
- }
- $ret = QiShangTongGoods::listPage([
- 'where' => $where,
- 'order' => $order,
- 'limit' => $params['limit'] ?? 18
- ]);
- if (!empty(QiShangTongGoods::getStaticError())) return QiShangTongGoods::getStaticError();
- if (!empty($ret['list'])) {
- foreach ($ret['list'] as &$item) {
- // 供货价,商品价格,建议零售价格
- $item['goods_id'] = $item['third_goods_id'];
- }
- }
- return $ret;
- }
- /**
- * 批量导入
- * @param int $mall_id
- * @param array $goods
- * @return string|true
- */
- public function batchImport(int $mall_id, array $goods)
- {
- $list = [];
- $ids = [];
- $t_ids = array_column($goods, 'goods_id');
- $t_ids = QiShangTongGoods::find()->where(['mall_id' => $mall_id])
- ->andWhere(['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
- ->andWhere(['third_goods_id' => $t_ids])
- ->select('third_goods_id')
- ->column();
- foreach ($goods as $good) {
- if (in_array($good['goods_id'], $t_ids)) continue;
- $list[] = [
- 'created_at' => time(),
- 'updated_at' => time(),
- 'mall_id' => $mall_id,
- 'status' => StatusEnum::ENABLED,
- 'third_goods_id'=> $good['goods_id'],
- 'goods_name' => $good['goods_name'],
- 'gonghuojia' => $good['gonghuojia'],
- 'retail_price' => $good['retail_price'],
- 'price' => $good['price'],
- 'default_image' => $good['default_image'],
- ];
- $ids[] = $good['goods_id'];
- }
- try {
- if (!empty($list)) {
- $t = \Yii::$app->db->beginTransaction();
- try {
- QiShangTongGoods::find()->createCommand()->batchInsert(QiShangTongGoods::tableName(), array_keys($list[0]), $list)->execute();
- QiShangTongGoods::updateAll(['can_sale' => StatusEnum::ENABLED],
- ['and', ['third_goods_id' => $ids], ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]]); // 改成可售状态
- $query = QiShangTongGoods::find()
- ->where(['third_goods_id' => $ids])
- ->andWhere(['>', 'goods_id', 0]);
- if (!SettingService::isMasterMall($mall_id)) {
- $query->andWhere(['mall_id' => $mall_id]);
- }
- $gids = $query->select('goods_id')->column();
- Goods::updateAll(['is_on_sale' => StatusEnum::ENABLED], ['id' => $gids]); // 上架
- $t->commit();
- } catch (\Exception $e) {
- $t->rollBack();
- return $e->getMessage();
- }
- foreach ($ids as $id) {
- \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, [
- 'mall_id' => $mall_id,
- 'goods_id' => $id
- ], self::class, 'asyncGoodsDetail');
- // $this->asyncGoodsDetail(['mall_id' => $mall_id, 'goods_id' => $id]);
- }
- }
- } catch (\Exception $e) {
- return $e->getMessage();
- }
- return true;
- }
- /**
- * 同步商品详情
- * @param array $data
- * @return bool
- */
- public function asyncGoodsDetail(array $data)
- {
- try {
- $resp = $this->detail($data['mall_id'], $data['goods_id']);
- /**
- * @var $goods QiShangTongGoods
- */
- $goods = QiShangTongGoods::getOne([
- ['mall_id' => $data['mall_id']],
- ['third_goods_id' => $data['goods_id']],
- ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]],
- ]);
- if (empty($goods)) throw new \Exception("商品不存在{$data['goods_id']}");
- $goods->cate_one = $resp['goods']['cate_id_1'] ?? 0;
- $goods->cate_two = $resp['goods']['cate_id_2'] ?? 0;
- $goods->content = $resp;
- if (!$goods->save()) throw new \Exception($goods->getErrorMessage());
- } catch (\Exception $e) {
- \Yii::error(FormatHelper::exception($e, __METHOD__));
- return false;
- }
- return true;
- }
- /**
- * 获取商品详情
- * @param int $mall_id
- * @param int $goods_id
- * @return array
- * @throws \Exception
- */
- public function detail(int $mall_id, int $goods_id)
- {
- $contentModel = new GoodsDetailModel();
- $contentModel->id = $goods_id;
- return RequestHelper::send(new GoodsDetailRequest(), $contentModel, $mall_id);
- }
- /**
- * 删除
- * @param int $mall_id
- * @param int $id
- * @return mixed|string|true|null
- */
- public function delete(int $mall_id, int $id)
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- /**
- * @var $goods QiShangTongGoods
- */
- $goods = QiShangTongGoods::getOne([
- ['mall_id' => $mall_id],
- ['id' => $id],
- ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]],
- ]);
- if (empty($goods)) throw new \Exception('记录不存在');
- $goods->status = StatusEnum::DELETE;
- $goods->can_sale = StatusEnum::DISABLED;
- if (!$goods->save()) throw new \Exception($goods->getErrorMessage());
- // 判断
- $ids = [];
- if (SettingService::isMasterMall($mall_id)) {
- $ids = QiShangTongGoods::find()->where(['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
- ->andWhere(['third_goods_id' => $goods->third_goods_id])->select('goods_id')->column();
- QiShangTongGoods::updateAll(['can_sale' => StatusEnum::DISABLED],
- ['and', ['third_goods_id' => $goods->third_goods_id], ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]]);
- } else {
- $ids[] = $goods->id;
- }
- // 下架商品
- if (!empty($ids)) Goods::updateAll(['is_on_sale' => StatusEnum::DISABLED], ['id' => $ids]);
- $t->commit();
- } catch (\Exception $e) {
- $t->rollBack();
- return $e->getMessage();
- }
- return true;
- }
- /**
- * @param $params
- * @return string
- */
- public function write($params)
- {
- return $this->asyncWriter(['mall_id' => $params['mall_id'], 'id' => $params['id']]);
- }
- public function asyncWriter(array $params)
- {
- $mall_id = $params['mall_id'];
- $id = $params['id'];
- /**
- * @var $goods QiShangTongGoods
- */
- $goods = QiShangTongGoods::getOne([
- ['mall_id' => $mall_id],
- ['id' => $id],
- ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]],
- ]);
- if (empty($goods)) return '记录不存在';
- $config = (new SettingService())->get($mall_id);
- // 这里写入商品数据
- try {
- $form = (new GoodsLogic($goods, $config))->execute();
- $t = \Yii::$app->db->beginTransaction();
- $res = Goods::setData($form);
- if (false === $res) throw new \Exception(Goods::getStaticError());
- if (!empty($goods->goods_id)) {
- QiShangTongGoodsAttr::deleteAll(['goods_id' => $goods->goods_id]);
- }
- if ($res->is_attr) {
- $attrs = GoodsAttr::lists([
- 'where' => [
- ['goods_id' => $res->id]
- ]
- ]);
- $f_attr = [];
- foreach ($form['attr'] as $item) {
- $f_attr[$item['goods_no']] = $item;
- }
- foreach ($attrs as $item) {
- QiShangTongGoodsAttr::setData([
- 'goods_id' => $res->id,
- 'goods_attr_id' => $item['id'],
- 'cost_price' => $item['cost_price'],
- 'price' => $item['price'],
- 'original_price'=> $item['original_price'],
- 'third_spec_id' => $item['goods_no'],
- 'mall_id' => $goods->mall_id,
- 'third_goods_id'=> $goods->third_goods_id,
- 'third_price' => $f_attr[$item['goods_no']]['third_price'],
- 'gonghuojia' => $f_attr[$item['goods_no']]['gonghuojia'],
- 'retail_price' => $f_attr[$item['goods_no']]['retail_price'],
- ]);
- }
- } else {
- /**
- * @var $attr GoodsAttr
- */
- $attr = GoodsAttr::getOne([
- ['goods_id' => $res->id],
- ], false, [], null, 'id');
- // 没有规格的情况下
- QiShangTongGoodsAttr::setData([
- 'goods_id' => $res->id,
- 'goods_attr_id' => $attr->id,
- 'third_goods_id'=> $goods->third_goods_id,
- 'cost_price' => $res->cost_price,
- 'price' => $res->price,
- 'original_price'=> $res->original_price,
- 'third_spec_id' => $res->goods_no,
- 'sign_id' => '',
- 'mall_id' => $goods->mall_id,
- 'third_price' => $form['third_price'],
- 'gonghuojia' => $form['gonghuojia'],
- 'retail_price' => $form['retail_price'],
- ]);
- }
- if (empty($goods->goods_id)) {
- $goods->goods_id = $res->id;
- $goods->import_at = time();
- if (!$goods->save()) throw new \Exception($goods->getErrorMessage());
- }
- $t->commit();
- } catch (\Exception $e) {
- if (!empty($t)) $t->rollBack();
- return $e->getMessage();
- }
- return true;
- }
- /**
- * @param QiShangTongEvent $event
- * @return void
- * @throws \Exception
- */
- public function eventUpdate(QiShangTongEvent &$event)
- {
- $list = QiShangTongGoods::lists([
- 'where' => [
- ['third_goods_id' => $event->content['goods_id']],
- ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]],
- ],
- 'select'=> 'id,mall_id'
- ], false);
- if (!empty($list)) {
- /**
- * @var $goods QiShangTongGoods
- */
- foreach ($list as $goods) {
- if (empty($goods)) throw new \Exception('记录不存在');
- $event->mall_id = $goods->mall_id;
- \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, [
- 'mall_id' => $goods->mall_id,
- 'id' => $goods->id
- ], self::class, 'asyncWriter');
- }
- }
- }
- /**
- * 修改价格
- * @param int $mall_id
- * @return void
- * @throws \Exception
- */
- public function changePrice(int $mall_id)
- {
- // $this->asyncChangePrice(['mall_id' => $mall_id]);
- \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, [
- 'mall_id' => $mall_id,
- ], self::class, 'asyncChangePrice');
- }
- /**
- * 批量修改价格
- * @param array $params
- * @return void
- * @throws \Exception
- */
- public function asyncChangePrice(array $params)
- {
- $mall_id = $params['mall_id'];
- // $config = (new SettingService())->get($mall_id);
- // 取出所有已经绑定的商品
- foreach (QiShangTongGoods::find()
- ->where(['mall_id' => $mall_id])
- ->andWhere(['!=', 'goods_id', 0])
- ->andWhere(['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
- ->select('id, goods_id, retail_price, price, gonghuojia')->each() as $each) {
- $data = ['mall_id' => $mall_id, 'id' => $each->goods_id, 'price' => $each->price,
- 'retail_price' => $each->retail_price, 'gonghuojia' => $each->gonghuojia];
- \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE,
- $data, self::class, 'asyncChangeGoodsPrice');
- // $this->asyncChangeGoodsPrice($data);
- }
- }
- /**
- * 修改价格
- * @param array $params
- * @return bool
- */
- public function asyncChangeGoodsPrice(array $params)
- {
- $mall_id = $params['mall_id'];
- $config = (new SettingService())->get($mall_id);
- $prices = !empty($config['price']) ? Json::decode($config['price']) : [];
- $t = \Yii::$app->db->beginTransaction();
- try {
- // 更新商品ID
- $price = SettingService::getPrice($mall_id, SettingService::PRICE, $params, $prices);
- $cost_price = SettingService::getPrice($mall_id, SettingService::COST_PRICE, $params, $prices);
- $original_price = SettingService::getPrice($mall_id, SettingService::ORIGINAL_PRICE, $params, $prices);
- Goods::updateAll(['price' => $price, 'cost_price' => $cost_price, 'original_price' => $original_price], ['id' => $params['id']]);
- $attrs = QiShangTongGoodsAttr::lists([
- 'where' => [
- ['goods_id' => $params['id']]
- ],
- 'select'=> 'goods_attr_id, id, price, cost_price, original_price, gonghuojia, retail_price, third_price'
- ]);
- foreach ($attrs as $attr) {
- $attr['price'] = $attr['third_price'];
- $price = SettingService::getPrice($mall_id, SettingService::PRICE, $attr, $prices);
- $cost_price = SettingService::getPrice($mall_id, SettingService::COST_PRICE, $attr, $prices);
- $original_price = SettingService::getPrice($mall_id, SettingService::ORIGINAL_PRICE, $attr, $prices);
- GoodsAttr::updateAll(['price' => $price, 'cost_price' => $cost_price, 'original_price' => $original_price], ['id' => $attr['goods_attr_id']]);
- }
- $t->commit();
- } catch (\Exception $e) {
- $t->rollBack();
- \Yii::error(FormatHelper::exception($e, __METHOD__));
- return false;
- }
- return true;
- }
- }
|