bztang-admin/plugins/video-share/src/admin/CenterController.php

297 lines
12 KiB
PHP

<?php
namespace Yunshop\VideoShare\admin;
use app\common\components\BaseController;
use app\common\exceptions\ShopException;
use app\common\models\Category;
use app\common\models\Member;
use Illuminate\Support\Facades\Redis;
use app\common\exceptions\AppException;
use Yunshop\VideoShare\common\service\TimedTaskImportService;
use Yunshop\VideoShare\common\model\CategoryModel;
use Yunshop\VideoShare\common\model\VideoShareGoods;
use Yunshop\VideoShare\common\service\CloudRequestService;
use Yunshop\VideoShare\common\job\AllImportJob;
use Yunshop\YzSupply\models\MiddlegroundConfiguration;
use Yunshop\YzSupply\models\YzGoods;
use Yunshop\YzSupply\services\GoodsImportService;
class CenterController extends BaseController
{
public function index()
{
if(!app('plugins')->isEnabled('yz-supply')){
throw new ShopException('请先开启供应链插件!');
}
$category_list = $this->shopCategory();
$video_category_list=$this->getVideoCate();
$import = $this->_getImportRedis();
return view('Yunshop\VideoShare::center.index', [
'category_level' => \Setting::get('shop.category.cat_level') ?: 2,
'category_list' => json_encode($category_list),
'video_category_list'=>json_encode($video_category_list),
'create_category' => 1,
'import' => json_encode($import),
'topic_id' => request()->topicId ?: 0,
])->render();
}
public function _getImportRedis()
{
$uniacid = \YunShop::app()->uniacid;
$redis_key = 'yz_supply_import';
$redis_import = $uniacid . '_import';
$all_page_import = $uniacid . '_import_page';
$now_page_import = $uniacid . '_import_now_page';
$total_import = $uniacid . '_import_total';
$import_status = Redis::hget($redis_key, $redis_import);
if ($import_status == '0') {
//导入进度
$import['import_process'] = number_format(Redis::hget($redis_key, $now_page_import) / Redis::hget($redis_key, $all_page_import) * 100, 2);
$import['import_total'] = Redis::hget($redis_key, $total_import);
$import['import_exits'] = Redis::hget($redis_key, $now_page_import) * $this->importSize;
$import['import_status'] = 1;
} else {
$import['import_status'] = 0;
}
return $import;
}
/**
* 选择会员
* @return \Illuminate\Http\JsonResponse
*/
public function getMemberList()
{
$kwd = request()->keyword;
$members = [];
if ($kwd) {
$members = \app\backend\modules\member\models\Member::select(['uid', 'nickname', 'avatar', 'mobile', 'realname'])->join('yz_member', function ($join) {
$join->on('mc_members.uid', 'yz_member.member_id')->whereNull('yz_member.deleted_at');
})->uniacid()->searchLike($kwd)->paginate(10)->toArray();
}
return $this->successJson('', $members);
}
protected function shopCategory()
{
$set = \Setting::get('shop.category');
$list = Category::getCategorys(0)->select('id', 'name', 'enabled')->pluginId()->get();
$list->map(function ($category) use ($set) {
$childrens = Category::getChildrenCategorys($category->id, $set)->select('id', 'name', 'enabled')->get()->toArray();
foreach ($childrens as $key => &$children) {
if ($set['cat_level'] == 3 && $children['has_many_children']) {
$children['childrens'] = $children['has_many_children'];
} else {
$children['childrens'] = [];
}
}
$category->childrens = $childrens;
});
return $list;
}
public function select()
{
//中台商品处理
$categorySearch = array_filter(request()->category, function ($item) {
return is_array($item) ? !empty($item[0]) : !empty($item);
});
$set = \Setting::get('plugin.video-share');
if(!$categorySearch['video_id']){
throw new AppException('请选择要导入的短视频分类');
}
$videoId=$categorySearch['video_id'];
unset($categorySearch['video_id']);
if($categorySearch['member_id']<=0&&$set['default_member_id']<=0){
throw new AppException('请选择关联用户,或者到基础设置里设置默认用户!');
}
$memberId=$categorySearch['member_id'];
unset($categorySearch['member_id']);
$all_yz_goods_ids =array_column(request()->goods_ids,'product_id');
$middleground_configuration_id =request()->middleground_configuration_id;
$extra['f_value_id'] = intval(request()->f_value_id);
$set = \Setting::get('plugin.yz_supply');
if ($set['create_category'] != 1) {
if (!$categorySearch) {
throw new AppException('请选择要导入的分类');
}
if (!$categorySearch['parentid']) {
throw new AppException('请选择一级分类');
}
if (!$categorySearch['childid']) {
throw new AppException('请选择二级分类');
}
if (\Setting::get('shop.category')['cat_level'] == 3 && !$categorySearch['thirdid']) {
throw new AppException('请选择三级分类');
}
}
if (!$all_yz_goods_ids) {
throw new AppException('请选择至少一件商品');
}
$errorGoodsIds = GoodsImportService::beforeImport($all_yz_goods_ids, $categorySearch, $extra,$middleground_configuration_id);
if ($errorGoodsIds) {
$string_error = implode(',', $errorGoodsIds);
return $this->errorJson('商品导入供应链选品库失败ID:' . $string_error, []);
}
//短视频处理
$videoData=[];
$set = \Setting::get('plugin.video-share');
$memberId=$memberId>0?$memberId:$set['default_member_id'];
$shopGoodsIds=YzGoods::uniacid()->whereIn('yz_goods_id',$all_yz_goods_ids)->pluck('goods_id','yz_goods_id')->toArray();
foreach (request()->goods_ids as $val) {
$exist=VideoShareGoods::uniacid()->where(['mid_ground_video_id'=>$val['id'],'mid_ground_config_id'=>request()->middleground_configuration_id])->first();
if($exist){
$exist->update([
'goods_id' => $shopGoodsIds[$val['product_id']],
'uid' => $memberId,
'title' => $val['origin_title'],
'video' => $val['video_url'] ?: '',
'file_name' => '',
'cover' => $val['cover_url'] ?: '',
'status' => 1,
'category_id' => $videoId?:0,
'updated_at'=>time()
]);
}else{
$videoData[] = [
'goods_id' => $shopGoodsIds[$val['product_id']],
'uniacid' => \YunShop::app()->uniacid,
'uid' => $memberId,
'title' => $val['origin_title'],
'video' => $val['video_url'] ?: '',
'file_name' => '',
'cover' => $val['cover_url'] ?: '',
'share_num' => 0,
'like_num' => 0,
'order_price' => 0,
'amount_total' => 0,
'status' => 1,
'category_id' => $videoId?:0,
'created_at'=>time(),
'updated_at'=>time(),
'mid_ground_video_id'=>$val['id'],
'mid_ground_config_id'=>request()->middleground_configuration_id
];
}
}
VideoShareGoods::insert($videoData);
(new CloudRequestService(request()->middleground_configuration_id))->addVideoStorage(array_column(request()->goods_ids,'id'));
return $this->successJson('导入成功!');
}
public function goodsList()
{
$page = request()->input('page', 1);
$search = request()->input('search', []);
$page_size = intval($search['goods_page_size'] ?: 20);
$search['page'] = $page;
$search['pageSize'] = $page_size;
try {
$list = (new CloudRequestService(request()->middleground_configuration_id))->goodsList($search,true);
} catch (\Exception $e) {
return $this->errorJson($e->getMessage());
}
return $this->successJson('success', $list['data']);
}
public function allSelect()
{
$categorySearch = array_filter(request()->category, function ($item) {
return is_array($item) ? !empty($item[0]) : !empty($item);
});
$set = \Setting::get('plugin.video-share');
if(!$categorySearch['video_id']){
throw new AppException('请选择要导入的短视频分类');
}
if(!$categorySearch['member_id']&&!$set['default_member_id']){
throw new AppException('请选择关联用户,或者到基础设置里设置默认用户!');
}
$middleground_configuration_id = request()->middleground_configuration_id;
$extra['f_value_id'] = intval(request()->f_value_id);
$set = \Setting::get('plugin.yz_supply');
if ($set['create_category'] != 1) {
if (!$categorySearch) {
throw new AppException('请选择要导入的分类');
}
if (!$categorySearch['parentid']) {
throw new AppException('请选择一级分类');
}
if (!$categorySearch['childid']) {
throw new AppException('请选择二级分类');
}
if (\Setting::get('shop.category')['cat_level'] == 3 && !$categorySearch['thirdid']) {
throw new AppException('请选择三级分类');
}
} else {
if ($categorySearch && !$categorySearch['childid']) {
throw new AppException('请选择二级分类');
}
if ($categorySearch && \Setting::get('shop.category')['cat_level'] == 3 && !$categorySearch['thirdid']) {
throw new AppException('请选择三级分类');
}
}
$list = (new \Yunshop\VideoShare\common\service\CloudRequestService($middleground_configuration_id))->goodsList([]);
$count = $list['data']['total'];
if($count>2000) $count = 2000;//固定最大值2000
$all_page = (int)ceil($count / 10);
$uniacid = \YunShop::app()->uniacid;
for ($page = 1; $page <= $all_page; $page++) {
usleep(50000); //延迟尽量避免同时请求中台 造成中台不返回数据
$this->dispatch(new AllImportJob($categorySearch, $page, [], $uniacid, $extra,$middleground_configuration_id));
}
$redis_key = 'yz_supply_video_import';
$redis_import = $uniacid . '_video_import';
$all_page_import = $uniacid . '_video_import_page';
$now_page_import = $uniacid . '_video_import_now_page';
$total_import = $uniacid . '_video_import_total';
Redis::hset($redis_key, $redis_import, 0);
Redis::hset($redis_key, $all_page_import, $all_page);
Redis::hset($redis_key, $now_page_import, 0);
Redis::hset($redis_key, $total_import, $count);
return $this->successJson('短视频导入请求成功', []);
}
public function getVideoCate(){
$list=CategoryModel::uniacid()->get()->toArray();
return $list;
}
public function getMidGroundList(){
if(!app('plugins')->isEnabled('yz-supply')){
return $this->successJson('success',[]);
}
$where[] = ['status',1];
$data = MiddlegroundConfiguration::uniacid()->where($where)->orderBy('sort','desc')->orderBy('id','desc')->get();
return $this->successJson("success",$data);
}
public function manual(){
(new TimedTaskImportService())->handle();
return $this->successJson('success');
}
}