package com.platform.controller;

import com.alibaba.fastjson.JSONObject;
import com.platform.entity.BrandEntity;
import com.platform.entity.Result;
import com.platform.entity.SceneEntity;
import com.platform.service.BrandService;
import com.platform.service.impl.ZhiHouseService;
import com.platform.vos.RequestScene;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

/**
 * Controller
 *
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2017-08-19 17:59:15
 */
@Api(tags = "管理后台店铺操作相关接口")
@RestController
@RequestMapping("scene")
@Slf4j
public class SceneController extends AbstractController{

    @Autowired
    private BrandService brandService;

    @Autowired
    private ZhiHouseService zhiHouseService;

    /**
     * 删除
     */
    @PostMapping("/generateDefaultLiveRoom")
    public Result generateDefaultLiveRoom(@RequestBody RequestScene requestScene,@RequestHeader String token) {
        try {
            String sceneNum = requestScene.getSceneNum();
            if (ObjectUtils.isEmpty(sceneNum)) {
                return Result.failure("参数有误！");
            }

            BrandEntity brand = brandService.queryByScene(sceneNum);
            if (!ObjectUtils.isEmpty(brand)) {
                zhiHouseService.generateDefaultLiveRoom(sceneNum,true);
                return Result.failure("直播间已存在！");
            }

            Object result = zhiHouseService.generateDefaultLiveRoom(sceneNum,true);

            SceneEntity sceneEntity = JSONObject.parseObject(JSONObject.toJSONString(result),SceneEntity.class);
            sceneEntity.setToken(token);
            BrandEntity brandEntity = brandService.generateDefaultLiveRoom(sceneEntity);
            if(ObjectUtils.isEmpty(brandEntity)){
                return Result.failure("场景获取失败！");
            }
            brandService.bindUser(getUserId(),brandEntity.getId().intValue(),0);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure(ObjectUtils.isEmpty(e.getMessage()) ? "生成直播间失败！" : e.getMessage());
        }
        return Result.success("生成直播间成功，请前往直播间查看。",null);
    }
}
