package com.hollywood.applet.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hollywood.applet.dto.AdverDto;
import com.hollywood.applet.dto.ScriptPayDto;
import com.hollywood.applet.dto.ShortPayDto;
import com.hollywood.applet.dto.TShortPlayDTO;
import com.hollywood.applet.query.TShortPlayQuery;
import com.hollywood.applet.service.*;
import com.hollywood.applet.utils.LoginInfoUtil;
import com.hollywood.applet.vo.TShortPlayVO;
import com.hollywood.common.basic.ApiResult;
import com.hollywood.common.basic.PageInfo;
import com.hollywood.common.model.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
/**
*
* 短剧管理 前端控制器
*
*
* @author xiaochen
* @since 2024-02-29
*/
@RestController
@RequestMapping("/tShortPlay")
@Api(tags = "短剧")
public class TShortPlayController {
@Autowired
private TShortPlayService shortPlayService;
@Autowired
private TAdvertisementService tAdvertisementService;
@Autowired
private LoginInfoUtil loginInfoUtil;
@Autowired
private TOrderService orderService;
@Autowired
private TAdvertisementConfigService advertisementConfigService;
@Autowired
private TShortPlayVideoService videoService;
private final TShortPlayVideoService shortPlayVideoService;
private final TShortPlayToTypeService shortPlayToTypeService;
private final TShortPlayTypeService shortPlayTypeService;
@Autowired
public TShortPlayController( TShortPlayVideoService shortPlayVideoService, TShortPlayToTypeService shortPlayToTypeService, TShortPlayTypeService shortPlayTypeService) {
this.shortPlayVideoService = shortPlayVideoService;
this.shortPlayToTypeService = shortPlayToTypeService;
this.shortPlayTypeService = shortPlayTypeService;
}
@ApiOperation(value = "首页获取短剧排名")
@GetMapping("/get-home")
public ApiResult> getHome()
{
List list = shortPlayService.getHotTwo(new QueryWrapper().eq("status", 1).orderByDesc("playNum").last("limit 4"));
return ApiResult.success(list);
}
/**
* 获取短剧管理分页列表
*/
@ApiOperation(value = "获取短剧管理分页列表")
@PostMapping(value = "/pageList")
public ApiResult> pageList(@RequestBody TShortPlayQuery query) {
Long userId = loginInfoUtil.getUserId();
List ids = new ArrayList<>();
if (query.getMine()!=null&&query.getMine()==1){
List list = orderService.list(Wrappers.lambdaQuery(TOrder.class).eq(TOrder::getProductType, 2)
.eq(TOrder::getUserId, userId)
.eq(TOrder::getIsPay, 1));
ids = list.stream()
.map(TOrder::getProductId)
.collect(Collectors.toList());
if (ids.size()==0){
return ApiResult.success(new PageInfo<>());
}
}
return ApiResult.success(shortPlayService.pageList(query,ids));
}
@ApiOperation(value = "播放接口")
@PostMapping(value = "/playNum/{id}")
public ApiResult playNum(@PathVariable Long id) {
TShortPlay byId1 = shortPlayService.getById(id);
byId1.setPlayNum(byId1.getPlayNum()==null ? 0: byId1.getPlayNum()+1);
shortPlayService.updateById(byId1);
return ApiResult.success();
}
@ApiOperation(value = "播放前播放广告")
@PostMapping(value = "/adver")
public ApiResult adver() {
List list = tAdvertisementService.list(Wrappers.lambdaQuery(TAdvertisement.class).eq(TAdvertisement::getStatus, 1));
if (list.isEmpty()){
ApiResult.success();
}
Random random = new Random();
int listSize = list.size();
int randomIndex = random.nextInt(listSize);
TAdvertisement randomElement = list.get(randomIndex);
AdverDto adverDto = new AdverDto();
adverDto.setTAdvertisement(randomElement);
if (advertisementConfigService.getOne(null).getAdvertisementConfig()!=null){
adverDto.setSeconds(advertisementConfigService.getOne(null).getAdvertisementConfig());
}
return ApiResult.success(adverDto);
}
@ApiOperation(value = "查询短剧管理详情")
@GetMapping(value = "/getDetailById")
public ApiResult getDetailById(@RequestParam Long id) {
TShortPlay shortPlay = shortPlayService.getById(id);
TShortPlayVO shortPlayVO = new TShortPlayVO();
BeanUtils.copyProperties(shortPlay,shortPlayVO);
// 查询短剧
List shortPlayVideos = shortPlayVideoService.list(Wrappers.lambdaQuery(TShortPlayVideo.class)
.eq(TShortPlayVideo::getShortPlayId, id).orderByAsc(TShortPlayVideo::getSortBy));
shortPlayVO.setShortPlayVideos(shortPlayVideos);
// 查询类型
List tShortPlayToTypes = shortPlayToTypeService.list(Wrappers.lambdaQuery(TShortPlayToType.class)
.eq(TShortPlayToType::getShortPlayId,id));
List typeIds = tShortPlayToTypes.stream().map(TShortPlayToType::getTypeId).collect(Collectors.toList());
shortPlayVO.setTypeIds(typeIds);
List tShortPlayTypes = shortPlayTypeService.list(Wrappers.lambdaQuery(TShortPlayType.class)
.in(TShortPlayType::getId, typeIds));
shortPlayVO.setShortPlayTypes(tShortPlayTypes);
StringBuilder sb = new StringBuilder();
for (TShortPlayType type : tShortPlayTypes) {
sb.append(type.getTypeName()).append(" | ");
}
// 移除末尾多余的分隔符和空格
if (sb.length() > 0) {
sb.setLength(sb.length() - 3);
}
String result = sb.toString();
shortPlayVO.setShortPlayTypesNames(result);
return ApiResult.success(shortPlayVO);
}
@ApiOperation(value = "购买短剧操作")
@PostMapping(value = "/pay")
public ApiResult pay(@RequestBody ShortPayDto shortPayDto) throws Exception {
try {
Long userId = loginInfoUtil.getUserId();
return shortPlayService.pay(shortPayDto,userId);
}catch (Exception e){
e.printStackTrace();
}
return ApiResult.success();
}
}