| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566 |
- <?php
- namespace addons\QiDingDong\common\service;
- use addons\QiDingDong\common\enums\QiDingDongEnums;
- use addons\QiDingDong\common\expand\sdk\qidingdong\src\model\DeliveryMethodModel;
- use addons\QiDingDong\common\expand\sdk\qidingdong\src\model\GoodsDetailV2Model;
- use addons\QiDingDong\common\expand\sdk\qidingdong\src\model\GoodsListV2Model;
- use addons\QiDingDong\common\expand\sdk\qidingdong\src\request\DeliveryMethodRequest;
- use addons\QiDingDong\common\expand\sdk\qidingdong\src\request\GoodsDetailV2Request;
- use addons\QiDingDong\common\expand\sdk\qidingdong\src\request\GoodsListV2Request;
- use addons\QiDingDong\common\helper\RequestHelper;
- use addons\QiDingDong\common\logic\GoodsLogic;
- use addons\QiDingDong\common\models\QiDingDongEvent;
- use addons\QiDingDong\common\models\QiDingDongGoods;
- use addons\QiDingDong\common\models\QiDingDongGoodsAttr;
- use common\enums\RabbitMqEnum;
- use common\enums\StatusEnum;
- use common\helpers\FormatHelper;
- use common\models\goods\Goods;
- use common\models\goods\GoodsAttr;
- use common\models\mall\Mall;
- 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 = QiDingDongGoods::listPage([
- 'where' => $where,
- 'limit' => $params['limit'] ?? 10,
- 'order' => 'id desc',
- 'with' => ['cateOne', 'cateTwo']
- ]);
- if (!empty(QiDingDongGoods::getStaticError())) return QiDingDongGoods::getStaticError();
- if (!empty($ret['list'])) {
- $rate = MallService::rate($mall_id);
- foreach ($ret['list'] as &$item) {
- $item['content'] = Json::decode($item['content']);
- $item['price'] = bcmul($rate, $item['price'], 2);
- $item['retail_price'] = bcmul($rate, $item['retail_price'], 2);
- $item['lowest_price'] = bcmul($rate, $item['lowest_price'], 2);
- }
- }
- return $ret;
- }
- public function thirdPage(int $mall_id, array $params)
- {
- try {
- $contentModel = RequestHelper::contentModelInstance($mall_id);
- $contentModel->content->data = new GoodsListV2Model();
- if (!empty($params['page'])) $contentModel->content->data->page = $params['page'];
- if (!empty($params['limit'])) $contentModel->content->data->page_size = $params['limit'];
- if (!empty($params['goods_id'])) $contentModel->content->data->goods_id = $params['goods_id'];
- if (!empty($params['sort'])) $contentModel->content->data->sort = $params['sort'];
- if (!empty($params['sort_type'])) $contentModel->content->data->sort_type = $params['sort_type'];
- if (!empty($params['goods_name'])) $contentModel->content->data->goods_name = $params['goods_name'];
- if (!empty($params['cate_id'])) $contentModel->content->data->cate_id = $params['cate_id'];
- if (!empty($params['discount'])) $contentModel->content->data->discount = $params['discount'];
- if (!empty($params['goods_type'])) $contentModel->content->data->goods_type = $params['goods_type'];
- $contentModel->content->data->device = QiDingDongEnums::REQUEST_PARAMS_DEVICE; // 固定值
- if (!empty($params['sort_type']) && !empty($params['sort'])) {
- // 默认最新 1、价格高到低 2、价格低到高 3、最新上架 4、修改时间 5、商品最后上下时间
- $contentModel->content->data->sort = $params['sort_type'];
- $contentModel->content->data->sort_type = $params['sort'];
- }
- return RequestHelper::send(new GoodsListV2Request(), RequestHelper::enCrypto($contentModel, $mall_id), $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['third_cate_id']];
- }
- if(!empty($params['discount'])) {
- $where[] = ['discount' => $params['discount']];
- }
- if(!empty($params['goods_id'])) {
- $where[] = ['third_goods_id' => $params['goods_id']];
- }
- if (!empty($params['goods_type'])) {
- $where[] = ['goods_type' => $params['goods_type']];
- }
- $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 = QiDingDongGoods::listPage([
- 'where' => $where,
- 'order' => $order,
- 'limit' => $params['limit'] ?? 18
- ]);
- if (!empty(QiDingDongGoods::getStaticError())) return QiDingDongGoods::getStaticError();
- if (!empty($ret['list'])) {
- foreach ($ret['list'] as &$item) {
- // 供货价,建议零售价,最低售价
- // $price = $item['cost_price'];
- // $retail_price = $item['price'];
- // $item['retail_price'] = $retail_price;
- // $item['price'] = $price;
- $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 = QiDingDongGoods::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'],
- 'retail_price' => $good['retail_price'], // 建议零售价
- 'price' => $good['price'], // 供货价
- 'discount' => ceil(bcdiv($good['price'] * 10, $good['retail_price'], 2)),
- 'goods_type' => $good['goods_type'],
- 'default_image' => $good['default_image'],
- 'lowest_price' => $good['lowest_price'],
- ];
- $ids[] = $good['goods_id'];
- }
- try {
- if (!empty($list)) {
- $t = \Yii::$app->db->beginTransaction();
- try {
- QiDingDongGoods::find()->createCommand()->batchInsert(QiDingDongGoods::tableName(), array_keys($list[0]), $list)->execute();
- QiDingDongGoods::updateAll(['can_sale' => StatusEnum::ENABLED],
- ['and', ['third_goods_id' => $ids], ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]]); // 改成可售状态
- $query = QiDingDongGoods::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 QiDingDongGoods
- */
- $goods = QiDingDongGoods::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|int|string
- * @throws \Exception
- */
- public function detail(int $mall_id, int $goods_id)
- {
- $contentModel = RequestHelper::contentModelInstance($mall_id);
- $contentModel->content->data = new GoodsDetailV2Model();
- $contentModel->content->data->id = $goods_id;
- $contentModel->content->data->device = QiDingDongEnums::REQUEST_PARAMS_DEVICE;
- return RequestHelper::send(new GoodsDetailV2Request(), RequestHelper::enCrypto($contentModel, $mall_id), $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 QiDingDongGoods
- */
- $goods = QiDingDongGoods::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 = QiDingDongGoods::find()->where(['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
- ->andWhere(['third_goods_id' => $goods->third_goods_id])
- ->andWhere(['!=', 'id', $goods->id])->select('goods_id')->column();
- QiDingDongGoods::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 array $params
- * @return string
- */
- public function write(array $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 QiDingDongGoods
- */
- $goods = QiDingDongGoods::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)) {
- QiDingDongGoodsAttr::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) {
- QiDingDongGoodsAttr::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'],
- 'retail_price' => $f_attr[$item['goods_no']]['retail_price'],
- 'lowest_price' => $f_attr[$item['goods_no']]['lowest_price'],
- ]);
- }
- } else {
- /**
- * @var $attr GoodsAttr
- */
- $attr = GoodsAttr::getOne([
- ['goods_id' => $res->id],
- ], false, [], null, 'id');
- // 没有规格的情况下
- QiDingDongGoodsAttr::setData([
- 'goods_id' => $res->id,
- 'goods_attr_id' => $attr->id,
- 'cost_price' => $res->cost_price,
- 'price' => $res->price,
- 'original_price'=> $res->original_price,
- 'third_spec_id' => $res->goods_no,
- 'mall_id' => $goods->mall_id,
- 'third_goods_id'=> $goods->third_goods_id,
- 'third_price' => $form['third_price'],
- 'retail_price' => $form['retail_price'],
- 'lowest_price' => $form['lowest_price'],
- ]);
- }
- if (empty($goods->goods_id)) {
- $goods->goods_id = $res->id;
- $goods->import_at = time();
- }
- // 获取配送方式
- $contentModel = RequestHelper::contentModelInstance($mall_id);
- $contentModel->content->data = new DeliveryMethodModel();
- $contentModel->content->data->goods_id = $goods->third_goods_id;
- $contentModel->content->data->device = QiDingDongEnums::REQUEST_PARAMS_DEVICE;
- $sRes = RequestHelper::send(new DeliveryMethodRequest(), RequestHelper::enCrypto($contentModel, $mall_id), $mall_id);
- if (!is_array($sRes)) throw new \Exception($sRes);
- $goods->ship_method = $sRes;
- if (!$goods->save()) throw new \Exception($goods->getErrorMessage());
- $t->commit();
- } catch (\Exception $e) {
- if (!empty($t)) $t->rollBack();
- return $e->getMessage();
- }
- return true;
- }
- /**
- * @param QiDingDongEvent $event
- * @return void
- * @throws \Exception
- */
- public function eventUpdate(QiDingDongEvent &$event)
- {
- $list = QiDingDongGoods::lists([
- 'where' => [
- ['third_goods_id' => $event->content['content']['data']['goods_id']],
- ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]],
- ],
- 'select'=> 'mall_id, id'
- ], false);
- if (!empty($list)) {
- /**
- * @var $goods QiDingDongGoods
- */
- 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 (QiDingDongGoods::find()
- ->where(['mall_id' => $mall_id])
- ->andWhere(['!=', 'goods_id', 0])
- ->andWhere(['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
- ->select('id, goods_id, retail_price, price, lowest_price')->each() as $each) {
- $data = ['mall_id' => $mall_id, 'id' => $each->goods_id, 'price' => $each->price,
- 'retail_price' => $each->retail_price, 'lowest_price' => $each->lowest_price];
- \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 = QiDingDongGoodsAttr::lists([
- 'where' => [
- ['goods_id' => $params['id']]
- ],
- 'select'=> 'goods_attr_id, id, price, cost_price, original_price, lowest_price, 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;
- }
- }
|