123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- <?php
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- namespace app\common\command;
-
-
- use app\common\enum\LiveGoodsEnum;
- use app\common\model\live\LiveGoods;
- use app\common\server\WxMnpLiveServer;
- use think\console\Command;
- use think\console\Output;
- use think\console\Input;
- use think\facade\Log;
-
-
- class WechatLiveGoods extends Command
- {
-
- protected function configure()
- {
- $this->setName('wechat_live_goods')
- ->setDescription('微信小程序直播商品状态同步');
- }
-
-
- protected function execute(Input $input, Output $output)
- {
- try {
- $liveGoodsModel = new LiveGoods();
-
- $localGoods = $liveGoodsModel->where([
- 'del' => 0,
- 'sys_audit_status' => LiveGoodsEnum::SYS_AUDIT_STATUS_WAIT_WECHAT
- ])
- ->select()->toArray();
-
- if (empty($localGoods)) {
- return true;
- }
-
-
- $localGoods = array_chunk($localGoods, 20);
- foreach ($localGoods as $localGoodsItem) {
- $goodsIds = array_column($localGoodsItem, 'wx_goods_id');
- $wxGoodsData = $this->getGoods($goodsIds);
- $wxGoodsData = $wxGoodsData['goods'] ?? [];
- $wxGoodsDataColumn = array_column($wxGoodsData, null,'goods_id');
-
- $updateData = [];
- foreach ($localGoodsItem as $goods) {
- $wxGoodsId = $goods['wx_goods_id'];
- if (!isset($wxGoodsDataColumn[$wxGoodsId])) {
- continue;
- }
- $wxGoods = $wxGoodsDataColumn[$wxGoodsId];
- $data = [
- 'id' => $goods['id'],
- 'wx_audit_status' => $wxGoods['audit_status'],
- 'audit_remark' => LiveGoodsEnum::getWxAuditStatusDesc($wxGoods['audit_status']),
- ];
- if ($wxGoods['audit_status'] == LiveGoodsEnum::WX_AUDIT_STATUS_SUCCESS) {
- $data['sys_audit_status'] = LiveGoodsEnum::SYS_AUDIT_STATUS_SUCCESS;
- }
- if ($wxGoods['audit_status'] == LiveGoodsEnum::WX_AUDIT_STATUS_FAIL) {
- $data['sys_audit_status'] = LiveGoodsEnum::SYS_AUDIT_STATUS_FAIL;
- }
- $updateData[] = $data;
- }
-
- if (!empty($updateData)) {
- $liveGoodsModel->saveAll($updateData);
- }
- }
-
- return true;
- } catch (\Exception $e) {
- Log::write('更新直播商品状态失败:' . $e->getMessage());
- return false;
- }
- }
-
-
-
-
- protected function getGoods($goodsIds)
- {
- $result = (new WxMnpLiveServer())->handle('getGoodsWarehouse', $goodsIds);
- if (0 != $result['errcode']) {
- return [];
- }
- return $result;
- }
-
- }
|