package com.tzy.controller.appuser;

import com.tzy.annotation.ApiLog;
import com.tzy.app.domain.AppUserBarrageRecord;
import com.tzy.app.dto.AppUserBarrageRecordDTO;
import com.tzy.app.service.AppUserBarrageRecordService;
import com.tzy.common.core.controller.BaseController;
import com.tzy.common.dto.InDto;
import com.tzy.common.dto.OutDTO;
import com.tzy.common.enums.BusinessType;
import com.tzy.sportcard.api.domain.AppUserInfoDto;
import com.tzy.sportcard.api.service.MineApiService;
import com.tzy.util.BeanUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户弹幕
 */
@RestController
@RequestMapping("/api/barrage")
public class AppUserBarrageRecordController extends BaseController {

    @Resource
    private AppUserBarrageRecordService appUserBarrageRecordService;
    @Resource
    private MineApiService mineApiService;

    @PostMapping("/save")
    @ApiLog(title = "新增弹幕", businessType = BusinessType.INSERT)
    public OutDTO save(@RequestBody InDto inDto){
        if(inDto.getData() == null){
            return OutDTO.error500("参数不能为空");
        }
        AppUserBarrageRecord appUserBarrageRecord = new AppUserBarrageRecord();
        BeanUtil.populate(appUserBarrageRecord, inDto.getData());
        mineApiService.checkUserNew(inDto);
        int result = appUserBarrageRecordService.insertBarrageRecord(appUserBarrageRecord);
        if(result == 0){
            return OutDTO.error500("发送失败");
        }
        return OutDTO.ok();
    }

    @PostMapping("/list")
    @ApiLog(title = "弹幕列表", businessType = BusinessType.INSERT)
    public OutDTO list(@RequestBody InDto inDto){
        if(inDto.getData() == null || inDto.get("groupInfoId") == null){
            return OutDTO.error500("参数不能为空");
        }
        AppUserBarrageRecord appUserBarrageRecord = new AppUserBarrageRecord();
        BeanUtil.populate(appUserBarrageRecord, inDto.getData());
        List<AppUserBarrageRecordDTO> list = appUserBarrageRecordService.selectBarrageRecords(appUserBarrageRecord);
        return OutDTO.ok().put("list", list);
    }
}
