package com.tzy.controller.living;

import com.tzy.annotation.ApiLog;
import com.tzy.annotation.RequireRoles;
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.common.utils.StringUtils;
import com.tzy.common.utils.UserType;
import com.tzy.pojo.card.CardGroupLivesConfig;
import com.tzy.sportcard.api.domain.AppUserInfoDto;
import com.tzy.sportcard.api.service.MineApiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.teasoft.bee.osql.Condition;
import org.teasoft.bee.osql.OrderType;
import org.teasoft.bee.osql.SuidRich;
import org.teasoft.honey.osql.core.ConditionImpl;

import java.util.List;


/**
 * 直播
 * @author po'yi
 */
@Api("直播api")
@RestController
@RequestMapping("/api/{version}/live")
public class GroupLivingController extends BaseController {

    @Autowired
    private SuidRich suidRich;
    @Autowired
    private MineApiService mineApiService;


    @ApiOperation("根据code查询房间信息")
    @ApiLog(title = "根据code查询房间信息", businessType = BusinessType.SEARCH)
    @PostMapping("/info")
    //@ApiVersion(1.0)
    public OutDTO getRoomDetail(@RequestBody InDto inDto) {
        CardGroupLivesConfig condition = new CardGroupLivesConfig();
        String roomCode = inDto.getString("roomCode");
        String groupInfoId = inDto.getString("groupInfoId");
        if (StringUtils.isEmpty(roomCode) && StringUtils.isEmpty(groupInfoId)) {
            return OutDTO.error(500, "参数错误");
        }
        condition.setCode(roomCode);
        condition.setCategory(inDto.getString("category"));

        if (groupInfoId != null) {
            condition.setGroupInfoId(Long.valueOf(inDto.getString("groupInfoId")));
        }
        Condition beeCondition = new ConditionImpl();
        beeCondition.orderBy("id", OrderType.DESC);
        List<CardGroupLivesConfig> list = suidRich.select(condition, beeCondition);
        return OutDTO.ok().put("lives", list.get(0));
    }


    @ApiLog(title = "获取我的直播", businessType = BusinessType.SEARCH, indto = "{{inDto}}")
    @ResponseBody
    @PostMapping("/mine")
    @RequireRoles(value = {UserType.USER_ROLE_SHIPPING, UserType.USER_ROLE_ADMIN,UserType.USER_ROLE_LIVING})
    public OutDTO getMineLives(@RequestBody InDto inDto) {
        AppUserInfoDto userInfo= mineApiService.checkUserNew(inDto);
        Object status = inDto.get("status");
        CardGroupLivesConfig condition=new CardGroupLivesConfig();
        condition.setAppUserId(userInfo.getId());
        condition.setDelFlg(0);
        condition.setCategory(inDto.getString("category"));
        Condition beeCondition=new ConditionImpl();
        beeCondition.start((inDto.getPageNo()-1)*inDto.getPageSize()).size(inDto.getPageSize());
        if(status!=null){
            beeCondition.orderBy("endTime", OrderType.DESC);
            condition.setStatus(2L);
        }else{
            beeCondition.between("status",0,1);
            beeCondition.orderBy("startTime", OrderType.ASC);
        }

        List<CardGroupLivesConfig> list = suidRich.select(condition,beeCondition);
        return OutDTO.ok().put("lives", list);
    }

    //@ApiLog(title = "添加直播间聊天记录", businessType = BusinessType.SEARCH, indto = "{{inDto}}")
    //@ResponseBody
    //@PostMapping("/chat/add")
    //public OutDTO addChatRecord(@RequestBody InDto inDto) {
    //    mineApiService.checkUserNew(inDto);
    //    RoomChat roomChat = new RoomChat();
    //    BeanUtil.populate(roomChat, inDto.getData());
    //    roomChat.setCreateTime(DateUtils.getTimestampNow());
    //    suidRich.insert(roomChat);
    //    return OutDTO.ok();
    //}
    //
    //@ApiLog(title = "查询直播间聊天记录", businessType = BusinessType.SEARCH, indto = "{{inDto}}")
    //@ResponseBody
    //@PostMapping("/chat/query")
    //public OutDTO queryChatRecord(@RequestBody InDto inDto) {
    //    RoomChat roomChat = new RoomChat();
    //    Condition beeCondition=new ConditionImpl();
    //    beeCondition.start(inDto.getPageNo()-1).size(inDto.getPageSize())
    //            .op("createTime", Op.ge, Timestamp.valueOf(inDto.getString("createTimeStart")))
    //            .op("createTime", Op.le, Timestamp.valueOf(inDto.getString("createTimeEnd")))
    //            .op("roomId", Op.eq,Long.valueOf(inDto.get("roomId").toString()));
    //    List<RoomChat> list = suidRich.select(roomChat,beeCondition);
    //    return OutDTO.ok().put("chats", list);
    //}


}
